github.com/ecodeclub/eorm@v0.0.2-0.20231001112437-dae71da914d0/column.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  // Column represents column
    18  // it could have alias
    19  // in general, we use it in two ways
    20  // 1. specify the column in query
    21  // 2. it's the start point of building complex expression
    22  type Column struct {
    23  	table TableReference
    24  	name  string
    25  	alias string
    26  }
    27  
    28  // C specify column
    29  func C(c string) Column {
    30  	return Column{
    31  		name: c,
    32  	}
    33  }
    34  
    35  // EQ =
    36  func (c Column) EQ(val interface{}) Predicate {
    37  	return Predicate{
    38  		left:  c,
    39  		op:    opEQ,
    40  		right: valueOf(val),
    41  	}
    42  }
    43  
    44  // NEQ !=
    45  func (c Column) NEQ(val interface{}) Predicate {
    46  	return Predicate{
    47  		left:  c,
    48  		op:    opNEQ,
    49  		right: valueOf(val),
    50  	}
    51  }
    52  
    53  // LT <
    54  func (c Column) LT(val interface{}) Predicate {
    55  	return Predicate{
    56  		left:  c,
    57  		op:    opLT,
    58  		right: valueOf(val),
    59  	}
    60  }
    61  
    62  // LTEQ <=
    63  func (c Column) LTEQ(val interface{}) Predicate {
    64  	return Predicate{
    65  		left:  c,
    66  		op:    opLTEQ,
    67  		right: valueOf(val),
    68  	}
    69  }
    70  
    71  // GT >
    72  func (c Column) GT(val interface{}) Predicate {
    73  	return Predicate{
    74  		left:  c,
    75  		op:    opGT,
    76  		right: valueOf(val),
    77  	}
    78  }
    79  
    80  // GTEQ >=
    81  func (c Column) GTEQ(val interface{}) Predicate {
    82  	return Predicate{
    83  		left:  c,
    84  		op:    opGTEQ,
    85  		right: valueOf(val),
    86  	}
    87  }
    88  
    89  // As means alias
    90  func (c Column) As(alias string) Selectable {
    91  	return Column{
    92  		table: c.table,
    93  		name:  c.name,
    94  		alias: alias,
    95  	}
    96  }
    97  
    98  // Like -> LIKE %XXX 、_x_ 、xx[xx-xx] 、xx[^xx-xx]
    99  func (c Column) Like(val interface{}) Predicate {
   100  	return Predicate{
   101  		left:  c,
   102  		op:    opLike,
   103  		right: valueOf(val),
   104  	}
   105  }
   106  
   107  // NotLike -> NOT LIKE %XXX 、_x_ 、xx[xx-xx] 、xx[^xx-xx]
   108  func (c Column) NotLike(val interface{}) Predicate {
   109  	return Predicate{
   110  		left:  c,
   111  		op:    opNotLike,
   112  		right: valueOf(val),
   113  	}
   114  }
   115  
   116  // Add generate an additive expression
   117  func (c Column) Add(val interface{}) MathExpr {
   118  	return MathExpr{
   119  		left:  c,
   120  		op:    opAdd,
   121  		right: valueOf(val),
   122  	}
   123  }
   124  
   125  // Multi generate a multiplication expression
   126  func (c Column) Multi(val interface{}) MathExpr {
   127  	return MathExpr{
   128  		left:  c,
   129  		op:    opMulti,
   130  		right: valueOf(val),
   131  	}
   132  }
   133  
   134  func (Column) assign() {
   135  	panic("implement me")
   136  }
   137  
   138  func (Column) expr() (string, error) {
   139  	panic("implement me")
   140  }
   141  
   142  func (Column) selected() {
   143  	panic("implement me")
   144  }
   145  
   146  type columns struct {
   147  	cs []string
   148  }
   149  
   150  func (columns) selected() {
   151  	panic("implement me")
   152  }
   153  
   154  func (columns) assign() {
   155  	panic("implement me")
   156  }
   157  
   158  // Columns specify columns
   159  func Columns(cs ...string) columns {
   160  	return columns{
   161  		cs: cs,
   162  	}
   163  }
   164  
   165  // In 方法没有元素传入,会被认为是false,被解释成where false这种形式
   166  // 支持一個 Subquery 子查詢
   167  func (c Column) In(data ...any) Predicate {
   168  	if len(data) == 0 {
   169  		return Predicate{
   170  			op: opFalse,
   171  		}
   172  	}
   173  
   174  	switch data[0].(type) {
   175  	case Subquery:
   176  		return Predicate{
   177  			left:  c,
   178  			op:    opIn,
   179  			right: data[0].(Subquery),
   180  		}
   181  	default:
   182  		return Predicate{
   183  			left: c,
   184  			op:   opIn,
   185  			right: values{
   186  				data: data,
   187  			},
   188  		}
   189  	}
   190  }
   191  
   192  // NotIn 方法没有元素传入,会被认为是false,被解释成where false这种形式
   193  func (c Column) NotIn(data ...any) Predicate {
   194  	if len(data) == 0 {
   195  		return Predicate{
   196  			op: opFalse,
   197  		}
   198  	}
   199  	return Predicate{
   200  		left: c,
   201  		op:   opNotIN,
   202  		right: values{
   203  			data: data,
   204  		},
   205  	}
   206  }
   207  
   208  type values struct {
   209  	data []any
   210  }
   211  
   212  func (values) expr() (string, error) {
   213  	panic("implement me")
   214  }