github.com/ecodeclub/eorm@v0.0.2-0.20231001112437-dae71da914d0/expression.go (about)

     1  // Copyright 2021 ecodeclub
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  // http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package eorm
    16  
    17  import operator "github.com/ecodeclub/eorm/internal/operator"
    18  
    19  // Expr is the top interface. It represents everything.
    20  type Expr interface {
    21  	expr() (string, error)
    22  }
    23  
    24  // RawExpr uses string alias Expr
    25  type RawExpr struct {
    26  	raw  string
    27  	args []interface{}
    28  }
    29  
    30  // Raw just take expr alias Expr
    31  func Raw(expr string, args ...interface{}) RawExpr {
    32  	return RawExpr{
    33  		raw:  expr,
    34  		args: args,
    35  	}
    36  }
    37  
    38  func (r RawExpr) expr() (string, error) {
    39  	return r.raw, nil
    40  }
    41  
    42  // AsPredicate 将会返回一个 Predicate,RawExpr 将会作为这个 Predicate 的左边部分
    43  // eorm 将不会校验任何从 RawExpr 生成的 Predicate
    44  func (r RawExpr) AsPredicate() Predicate {
    45  	return Predicate{
    46  		left: r,
    47  	}
    48  }
    49  
    50  func (RawExpr) selected() {}
    51  
    52  type binaryExpr struct {
    53  	left  Expr
    54  	op    operator.Op
    55  	right Expr
    56  }
    57  
    58  func (binaryExpr) expr() (string, error) {
    59  	return "", nil
    60  }
    61  
    62  type MathExpr binaryExpr
    63  
    64  func (m MathExpr) Add(val interface{}) Expr {
    65  	return MathExpr{
    66  		left:  m,
    67  		op:    opAdd,
    68  		right: valueOf(val),
    69  	}
    70  }
    71  
    72  func (m MathExpr) Multi(val interface{}) MathExpr {
    73  	return MathExpr{
    74  		left:  m,
    75  		op:    opMulti,
    76  		right: valueOf(val),
    77  	}
    78  }
    79  
    80  func (MathExpr) expr() (string, error) {
    81  	return "", nil
    82  }
    83  
    84  func valueOf(val interface{}) Expr {
    85  	switch v := val.(type) {
    86  	case Expr:
    87  		return v
    88  	default:
    89  		return valueExpr{val: val}
    90  	}
    91  }
    92  
    93  type SubqueryExpr struct {
    94  	s Subquery
    95  	// 謂詞: ALL、ANY、SOME
    96  	pred string
    97  }
    98  
    99  func (SubqueryExpr) expr() (string, error) {
   100  	panic("implement me")
   101  }
   102  
   103  func Any(sub Subquery) SubqueryExpr {
   104  	return SubqueryExpr{
   105  		s:    sub,
   106  		pred: "ANY",
   107  	}
   108  }
   109  
   110  func All(sub Subquery) SubqueryExpr {
   111  	return SubqueryExpr{
   112  		s:    sub,
   113  		pred: "ALL",
   114  	}
   115  }
   116  
   117  func Some(sub Subquery) SubqueryExpr {
   118  	return SubqueryExpr{
   119  		s:    sub,
   120  		pred: "SOME",
   121  	}
   122  }