github.com/unionj-cloud/go-doudou/v2@v2.3.5/toolkit/gormgen/field/time.go (about) 1 package field 2 3 import ( 4 "fmt" 5 "time" 6 7 "gorm.io/gorm/clause" 8 ) 9 10 // Time time type field 11 type Time Field 12 13 // Eq equal to 14 func (field Time) Eq(value time.Time) Expr { 15 return expr{e: clause.Eq{Column: field.RawExpr(), Value: value}} 16 } 17 18 // Neq not equal to 19 func (field Time) Neq(value time.Time) Expr { 20 return expr{e: clause.Neq{Column: field.RawExpr(), Value: value}} 21 } 22 23 // Gt greater than 24 func (field Time) Gt(value time.Time) Expr { 25 return expr{e: clause.Gt{Column: field.RawExpr(), Value: value}} 26 } 27 28 // Gte greater or equal to 29 func (field Time) Gte(value time.Time) Expr { 30 return expr{e: clause.Gte{Column: field.RawExpr(), Value: value}} 31 } 32 33 // Lt less than 34 func (field Time) Lt(value time.Time) Expr { 35 return expr{e: clause.Lt{Column: field.RawExpr(), Value: value}} 36 } 37 38 // Lte less or equal to 39 func (field Time) Lte(value time.Time) Expr { 40 return expr{e: clause.Lte{Column: field.RawExpr(), Value: value}} 41 } 42 43 // Between ... 44 func (field Time) Between(left time.Time, right time.Time) Expr { 45 return field.between([]interface{}{left, right}) 46 } 47 48 // NotBetween ... 49 func (field Time) NotBetween(left time.Time, right time.Time) Expr { 50 return Not(field.Between(left, right)) 51 } 52 53 // In ... 54 func (field Time) In(values ...time.Time) Expr { 55 return expr{e: clause.IN{Column: field.RawExpr(), Values: field.toSlice(values...)}} 56 } 57 58 // NotIn ... 59 func (field Time) NotIn(values ...time.Time) Expr { 60 return expr{e: clause.Not(field.In(values...).expression())} 61 } 62 63 // Add ... 64 func (field Time) Add(value time.Duration) Time { 65 return Time{field.add(value)} 66 } 67 68 // Sub ... 69 func (field Time) Sub(value time.Duration) Time { 70 return Time{field.sub(value)} 71 } 72 73 // Date convert to data, equal to "DATE(time_expr)" 74 func (field Time) Date() Time { 75 return Time{expr{e: clause.Expr{SQL: "DATE(?)", Vars: []interface{}{field.RawExpr()}}}} 76 } 77 78 // DateDiff equal to DATADIFF(self, value) 79 func (field Time) DateDiff(value time.Time) Int { 80 return Int{expr{e: clause.Expr{SQL: "DATEDIFF(?,?)", Vars: []interface{}{field.RawExpr(), value}}}} 81 } 82 83 // DateFormat equal to DATE_FORMAT(self, value) 84 func (field Time) DateFormat(value string) String { 85 return String{expr{e: clause.Expr{SQL: "DATE_FORMAT(?,?)", Vars: []interface{}{field.RawExpr(), value}}}} 86 } 87 88 // Now return result of NOW() 89 func (field Time) Now() Time { 90 return Time{expr{e: clause.Expr{SQL: "NOW()"}}} 91 } 92 93 // CurDate return result of CURDATE() 94 func (field Time) CurDate() Time { 95 return Time{expr{e: clause.Expr{SQL: "CURDATE()"}}} 96 } 97 98 // CurTime return result of CURTIME() 99 func (field Time) CurTime() Time { 100 return Time{expr{e: clause.Expr{SQL: "CURTIME()"}}} 101 } 102 103 // DayName equal to DAYNAME(self) 104 func (field Time) DayName() String { 105 return String{expr{e: clause.Expr{SQL: "DAYNAME(?)", Vars: []interface{}{field.RawExpr()}}}} 106 } 107 108 // MonthName equal to MONTHNAME(self) 109 func (field Time) MonthName() String { 110 return String{expr{e: clause.Expr{SQL: "MONTHNAME(?)", Vars: []interface{}{field.RawExpr()}}}} 111 } 112 113 func (field Time) Year() Int { 114 return Int{expr{e: clause.Expr{SQL: "YEAR(?)", Vars: []interface{}{field.RawExpr()}}}} 115 } 116 117 // Month equal to MONTH(self) 118 func (field Time) Month() Int { 119 return Int{expr{e: clause.Expr{SQL: "MONTH(?)", Vars: []interface{}{field.RawExpr()}}}} 120 } 121 122 // Day equal to DAY(self) 123 func (field Time) Day() Int { 124 return Int{expr{e: clause.Expr{SQL: "DAY(?)", Vars: []interface{}{field.RawExpr()}}}} 125 } 126 127 // Hour equal to HOUR(self) 128 func (field Time) Hour() Int { 129 return Int{expr{e: clause.Expr{SQL: "HOUR(?)", Vars: []interface{}{field.RawExpr()}}}} 130 } 131 132 // Minute equal to MINUTE(self) 133 func (field Time) Minute() Int { 134 return Int{expr{e: clause.Expr{SQL: "MINUTE(?)", Vars: []interface{}{field.RawExpr()}}}} 135 } 136 137 // Second equal to SECOND(self) 138 func (field Time) Second() Int { 139 return Int{expr{e: clause.Expr{SQL: "SECOND(?)", Vars: []interface{}{field.RawExpr()}}}} 140 } 141 142 // MicroSecond equal to MICROSECOND(self) 143 func (field Time) MicroSecond() Int { 144 return Int{expr{e: clause.Expr{SQL: "MICROSECOND(?)", Vars: []interface{}{field.RawExpr()}}}} 145 } 146 147 // DayOfWeek equal to DAYOFWEEK(self) 148 func (field Time) DayOfWeek() Int { 149 return Int{expr{e: clause.Expr{SQL: "DAYOFWEEK(?)", Vars: []interface{}{field.RawExpr()}}}} 150 } 151 152 // DayOfMonth equal to DAYOFMONTH(self) 153 func (field Time) DayOfMonth() Int { 154 return Int{expr{e: clause.Expr{SQL: "DAYOFMONTH(?)", Vars: []interface{}{field.RawExpr()}}}} 155 } 156 157 // DayOfYear equal to DAYOFYEAR(self) 158 func (field Time) DayOfYear() Int { 159 return Int{expr{e: clause.Expr{SQL: "DAYOFYEAR(?)", Vars: []interface{}{field.RawExpr()}}}} 160 } 161 162 // FromDays equal to FROM_DAYS(self) 163 func (field Time) FromDays(value int) Time { 164 return Time{expr{e: clause.Expr{SQL: fmt.Sprintf("FROM_DAYS(%d)", value)}}} 165 } 166 167 // FromUnixtime equal to FROM_UNIXTIME(self) 168 func (field Time) FromUnixtime(value int) Time { 169 return Time{expr{e: clause.Expr{SQL: fmt.Sprintf("FROM_UNIXTIME(%d)", value)}}} 170 } 171 172 // Value set value 173 func (field Time) Value(value time.Time) AssignExpr { 174 return field.value(value) 175 } 176 177 // Zero set zero value 178 func (field Time) Zero() AssignExpr { 179 return field.value(time.Time{}) 180 } 181 182 // Sum calc sum 183 func (field Time) Sum() Time { 184 return Time{field.sum()} 185 } 186 187 // IfNull ... 188 func (field Time) IfNull(value time.Time) Expr { 189 return field.ifNull(value) 190 } 191 192 func (field Time) toSlice(values ...time.Time) []interface{} { 193 slice := make([]interface{}, len(values)) 194 for i, v := range values { 195 slice[i] = v 196 } 197 return slice 198 }