github.com/unionj-cloud/go-doudou/v2@v2.3.5/toolkit/gormgen/field/string.go (about) 1 package field 2 3 import ( 4 "fmt" 5 6 "gorm.io/gorm/clause" 7 ) 8 9 // String string type field 10 type String Field 11 12 // Eq equal to 13 func (field String) Eq(value string) Expr { 14 return expr{e: clause.Eq{Column: field.RawExpr(), Value: value}} 15 } 16 17 // Neq not equal to 18 func (field String) Neq(value string) Expr { 19 return expr{e: clause.Neq{Column: field.RawExpr(), Value: value}} 20 } 21 22 // Gt greater than 23 func (field String) Gt(value string) Expr { 24 return expr{e: clause.Gt{Column: field.RawExpr(), Value: value}} 25 } 26 27 // Gte greater or equal to 28 func (field String) Gte(value string) Expr { 29 return expr{e: clause.Gte{Column: field.RawExpr(), Value: value}} 30 } 31 32 // Lt less than 33 func (field String) Lt(value string) Expr { 34 return expr{e: clause.Lt{Column: field.RawExpr(), Value: value}} 35 } 36 37 // Lte less or equal to 38 func (field String) Lte(value string) Expr { 39 return expr{e: clause.Lte{Column: field.RawExpr(), Value: value}} 40 } 41 42 // Between ... 43 func (field String) Between(left string, right string) Expr { 44 return field.between([]interface{}{left, right}) 45 } 46 47 // NotBetween ... 48 func (field String) NotBetween(left string, right string) Expr { 49 return Not(field.Between(left, right)) 50 } 51 52 // In ... 53 func (field String) In(values ...string) Expr { 54 return expr{e: clause.IN{Column: field.RawExpr(), Values: field.toSlice(values)}} 55 } 56 57 // NotIn ... 58 func (field String) NotIn(values ...string) Expr { 59 return expr{e: clause.Not(field.In(values...).expression())} 60 } 61 62 // Like ... 63 func (field String) Like(value string) Expr { 64 return expr{e: clause.Like{Column: field.RawExpr(), Value: value}} 65 } 66 67 // NotLike ... 68 func (field String) NotLike(value string) Expr { 69 return expr{e: clause.Not(field.Like(value).expression())} 70 } 71 72 // Regexp ... 73 func (field String) Regexp(value string) Expr { 74 return field.regexp(value) 75 } 76 77 // NotRegxp ... 78 func (field String) NotRegxp(value string) Expr { 79 return expr{e: clause.Not(field.Regexp(value).expression())} 80 } 81 82 // Value ... 83 func (field String) Value(value string) AssignExpr { 84 return field.value(value) 85 } 86 87 // Zero ... 88 func (field String) Zero() AssignExpr { 89 return field.value("") 90 } 91 92 // IfNull ... 93 func (field String) IfNull(value string) Expr { 94 return field.ifNull(value) 95 } 96 97 // FindInSet equal to FIND_IN_SET(field_name, input_string_list) 98 func (field String) FindInSet(targetList string) Expr { 99 return expr{e: clause.Expr{SQL: "FIND_IN_SET(?,?)", Vars: []interface{}{field.RawExpr(), targetList}}} 100 } 101 102 // FindInSetWith equal to FIND_IN_SET(input_string, field_name) 103 func (field String) FindInSetWith(target string) Expr { 104 return expr{e: clause.Expr{SQL: "FIND_IN_SET(?,?)", Vars: []interface{}{target, field.RawExpr()}}} 105 } 106 107 // Replace ... 108 func (field String) Replace(from, to string) String { 109 return String{expr{e: clause.Expr{SQL: "REPLACE(?,?,?)", Vars: []interface{}{field.RawExpr(), from, to}}}} 110 } 111 112 // Concat ... 113 func (field String) Concat(before, after string) String { 114 switch { 115 case before != "" && after != "": 116 return String{expr{e: clause.Expr{SQL: "CONCAT(?,?,?)", Vars: []interface{}{before, field.RawExpr(), after}}}} 117 case before != "": 118 return String{expr{e: clause.Expr{SQL: "CONCAT(?,?)", Vars: []interface{}{before, field.RawExpr()}}}} 119 case after != "": 120 return String{expr{e: clause.Expr{SQL: "CONCAT(?,?)", Vars: []interface{}{field.RawExpr(), after}}}} 121 default: 122 return field 123 } 124 } 125 126 // SubstringIndex SUBSTRING_INDEX 127 // https://dev.mysql.com/doc/refman/8.0/en/functions.html#function_substring-index 128 func (field String) SubstringIndex(delim string, count int) String { 129 return String{expr{e: clause.Expr{ 130 SQL: fmt.Sprintf("SUBSTRING_INDEX(?,%q,%d)", delim, count), 131 Vars: []interface{}{field.RawExpr()}, 132 }}} 133 } 134 135 func (field String) toSlice(values []string) []interface{} { 136 slice := make([]interface{}, len(values)) 137 for i, v := range values { 138 slice[i] = v 139 } 140 return slice 141 } 142 143 // Bytes []byte type field 144 type Bytes String 145 146 // Eq equal to 147 func (field Bytes) Eq(value []byte) Expr { 148 return expr{e: clause.Eq{Column: field.RawExpr(), Value: value}} 149 } 150 151 // Neq not equal to 152 func (field Bytes) Neq(value []byte) Expr { 153 return expr{e: clause.Neq{Column: field.RawExpr(), Value: value}} 154 } 155 156 // Gt greater than 157 func (field Bytes) Gt(value []byte) Expr { 158 return expr{e: clause.Gt{Column: field.RawExpr(), Value: value}} 159 } 160 161 // Gte greater or equal to 162 func (field Bytes) Gte(value []byte) Expr { 163 return expr{e: clause.Gte{Column: field.RawExpr(), Value: value}} 164 } 165 166 // Lt less than 167 func (field Bytes) Lt(value []byte) Expr { 168 return expr{e: clause.Lt{Column: field.RawExpr(), Value: value}} 169 } 170 171 // Lte less or equal to 172 func (field Bytes) Lte(value []byte) Expr { 173 return expr{e: clause.Lte{Column: field.RawExpr(), Value: value}} 174 } 175 176 // Between ... 177 func (field Bytes) Between(left []byte, right []byte) Expr { 178 return field.between([]interface{}{left, right}) 179 } 180 181 // NotBetween ... 182 func (field Bytes) NotBetween(left []byte, right []byte) Expr { 183 return Not(field.Between(left, right)) 184 } 185 186 // In ... 187 func (field Bytes) In(values ...[]byte) Expr { 188 return expr{e: clause.IN{Column: field.RawExpr(), Values: field.toSlice(values)}} 189 } 190 191 // NotIn ... 192 func (field Bytes) NotIn(values ...[]byte) Expr { 193 return expr{e: clause.Not(field.In(values...).expression())} 194 } 195 196 // Like ... 197 func (field Bytes) Like(value string) Expr { 198 return expr{e: clause.Like{Column: field.RawExpr(), Value: value}} 199 } 200 201 // NotLike ... 202 func (field Bytes) NotLike(value string) Expr { 203 return expr{e: clause.Not(field.Like(value).expression())} 204 } 205 206 // Regexp ... 207 func (field Bytes) Regexp(value string) Expr { 208 return field.regexp(value) 209 } 210 211 // NotRegxp ... 212 func (field Bytes) NotRegxp(value string) Expr { 213 return Not(field.Regexp(value)) 214 } 215 216 // Value ... 217 func (field Bytes) Value(value []byte) AssignExpr { 218 return field.value(value) 219 } 220 221 // Zero ... 222 func (field Bytes) Zero() AssignExpr { 223 return field.value([]byte{}) 224 } 225 226 // IfNull ... 227 func (field Bytes) IfNull(value []byte) Expr { 228 return field.ifNull(value) 229 } 230 231 // FindInSet FIND_IN_SET(field_name, input_string_list) 232 func (field Bytes) FindInSet(targetList string) Expr { 233 return expr{e: clause.Expr{SQL: "FIND_IN_SET(?,?)", Vars: []interface{}{field.RawExpr(), targetList}}} 234 } 235 236 // FindInSetWith FIND_IN_SET(input_string, field_name) 237 func (field Bytes) FindInSetWith(target string) Expr { 238 return expr{e: clause.Expr{SQL: "FIND_IN_SET(?,?)", Vars: []interface{}{target, field.RawExpr()}}} 239 } 240 241 // SubstringIndex SUBSTRING_INDEX 242 // https://dev.mysql.com/doc/refman/8.0/en/functions.html#function_substring-index 243 func (field Bytes) SubstringIndex(delim string, count int) Bytes { 244 return Bytes{expr{e: clause.Expr{ 245 SQL: fmt.Sprintf("SUBSTRING_INDEX(?,%q,%d)", delim, count), 246 Vars: []interface{}{field.RawExpr()}, 247 }}} 248 } 249 250 func (field Bytes) toSlice(values [][]byte) []interface{} { 251 slice := make([]interface{}, len(values)) 252 for i, v := range values { 253 slice[i] = v 254 } 255 return slice 256 }