github.com/ecodeclub/eorm@v0.0.2-0.20231001112437-dae71da914d0/aggregate.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  // Aggregate represents aggregate expression, including AVG, MAX, MIN...
    18  type Aggregate struct {
    19  	table    TableReference
    20  	fn       string
    21  	arg      string
    22  	alias    string
    23  	distinct bool
    24  }
    25  
    26  // As 指定别名。一般情况下,这个别名应该等同于列名,我们会将这个列名映射过去对应的字段名。
    27  // 例如说 alias= avg_age,默认情况下,我们会找 AvgAge 这个字段来接收值。
    28  func (a Aggregate) As(alias string) Selectable {
    29  	return Aggregate{
    30  		fn:    a.fn,
    31  		arg:   a.arg,
    32  		alias: alias,
    33  		table: a.table,
    34  	}
    35  }
    36  
    37  // Avg represents AVG
    38  func Avg(c string) Aggregate {
    39  	return Aggregate{
    40  		fn:  "AVG",
    41  		arg: c,
    42  	}
    43  }
    44  
    45  // Max represents MAX
    46  func Max(c string) Aggregate {
    47  	return Aggregate{
    48  		fn:  "MAX",
    49  		arg: c,
    50  	}
    51  }
    52  
    53  // Min represents MIN
    54  func Min(c string) Aggregate {
    55  	return Aggregate{
    56  		fn:  "MIN",
    57  		arg: c,
    58  	}
    59  }
    60  
    61  // Count represents COUNT
    62  func Count(c string) Aggregate {
    63  	return Aggregate{
    64  		fn:  "COUNT",
    65  		arg: c,
    66  	}
    67  }
    68  
    69  // Sum represents SUM
    70  func Sum(c string) Aggregate {
    71  	return Aggregate{
    72  		fn:  "SUM",
    73  		arg: c,
    74  	}
    75  }
    76  
    77  // CountDistinct represents COUNT(DISTINCT XXX)
    78  func CountDistinct(col string) Aggregate {
    79  	a := Count(col)
    80  	a.distinct = true
    81  	return a
    82  }
    83  
    84  // AvgDistinct represents AVG(DISTINCT XXX)
    85  func AvgDistinct(col string) Aggregate {
    86  	a := Avg(col)
    87  	a.distinct = true
    88  	return a
    89  }
    90  
    91  // SumDistinct represents SUM(DISTINCT XXX)
    92  func SumDistinct(col string) Aggregate {
    93  	a := Sum(col)
    94  	a.distinct = true
    95  	return a
    96  }
    97  
    98  func (Aggregate) selected() {}
    99  
   100  func (a Aggregate) EQ(val interface{}) Predicate {
   101  	return Predicate{
   102  		left:  a,
   103  		op:    opEQ,
   104  		right: valueOf(val),
   105  	}
   106  }
   107  func (a Aggregate) NEQ(val interface{}) Predicate {
   108  	return Predicate{
   109  		left:  a,
   110  		op:    opNEQ,
   111  		right: valueOf(val),
   112  	}
   113  }
   114  
   115  // LT <
   116  func (a Aggregate) LT(val interface{}) Predicate {
   117  	return Predicate{
   118  		left:  a,
   119  		op:    opLT,
   120  		right: valueOf(val),
   121  	}
   122  }
   123  
   124  // LTEQ <=
   125  func (a Aggregate) LTEQ(val interface{}) Predicate {
   126  	return Predicate{
   127  		left:  a,
   128  		op:    opLTEQ,
   129  		right: valueOf(val),
   130  	}
   131  }
   132  
   133  // GT >
   134  func (a Aggregate) GT(val interface{}) Predicate {
   135  	return Predicate{
   136  		left:  a,
   137  		op:    opGT,
   138  		right: valueOf(val),
   139  	}
   140  }
   141  
   142  // GTEQ >=
   143  func (a Aggregate) GTEQ(val interface{}) Predicate {
   144  	return Predicate{
   145  		left:  a,
   146  		op:    opGTEQ,
   147  		right: valueOf(val),
   148  	}
   149  }
   150  
   151  func (Aggregate) expr() (string, error) {
   152  	return "", nil
   153  }