diff --git a/field/base.go b/field/base.go new file mode 100644 index 00000000..fa018623 --- /dev/null +++ b/field/base.go @@ -0,0 +1,284 @@ +package field + +import ( + "fmt" + "strings" + "time" + + "gorm.io/gorm" + "gorm.io/gorm/clause" +) + +var _ Expr = new(Field) + +// Expr 用于查询的表达式 +// 函数Mark无实际意义,用于区分Expr和clause.Expression +type Expr interface { + clause.Expression + + As(alias string) Expr + Column() clause.Column + BuildColumn(*gorm.Statement, ...BuildOpt) string + RawExpr() interface{} + + mark() +} + +func Or(exprs ...Expr) Expr { + return &expr{expression: clause.Or(toExpression(exprs...)...)} +} + +func And(exprs ...Expr) Expr { + return &expr{expression: clause.And(toExpression(exprs...)...)} +} + +func Not(exprs ...Expr) Expr { + return &expr{expression: clause.Not(toExpression(exprs...)...)} +} + +func toExpression(conds ...Expr) []clause.Expression { + exprs := make([]clause.Expression, len(conds)) + for i, cond := range conds { + exprs[i] = cond + } + return exprs +} + +// Field 代表标准字段结构 +type Field struct { + expr +} + +type expr struct { + Col clause.Column + + expression clause.Expression +} + +// mark 限制外部实现Expr接口 +func (expr) mark() {} + +func (e expr) Build(builder clause.Builder) { + if e.expression == nil { + e.expression = clause.NamedExpr{SQL: "?", Vars: []interface{}{e.Col}} + } + e.expression.Build(builder) +} + +func (e expr) Column() clause.Column { + return e.Col +} + +type BuildOpt func(clause.Column) interface{} + +var WithAlias BuildOpt = func(col clause.Column) interface{} { + return col +} + +func (e expr) BuildColumn(stmt *gorm.Statement, opts ...BuildOpt) string { + var col interface{} = e.Col.Name + for _, opt := range opts { + col = opt(e.Col) + } + return stmt.Quote(col) +} + +func (e expr) RawExpr() interface{} { + if e.expression == nil { + return e.Col + } + return e.expression +} + +// ======================== 基本函数 ======================== +func (e expr) Count() Expr { + return &expr{Col: e.Col, expression: clause.Expr{SQL: "COUNT(?)", Vars: []interface{}{e.Col}}} +} + +func (e expr) Length() Expr { + return &expr{Col: e.Col, expression: clause.Expr{SQL: "LENGTH(?)", Vars: []interface{}{e.Col}}} +} + +func (e expr) Max() Expr { + return &expr{Col: e.Col, expression: clause.Expr{SQL: "MAX(?)", Vars: []interface{}{e.Col}}} +} + +func (e expr) Min() Expr { + return &expr{Col: e.Col, expression: clause.Expr{SQL: "MIN(?)", Vars: []interface{}{e.Col}}} +} + +func (e expr) Avg() Expr { + return &expr{Col: e.Col, expression: clause.Expr{SQL: "AVG(?)", Vars: []interface{}{e.Col}}} +} + +func (e expr) Sum() Expr { + return &expr{Col: e.Col, expression: clause.Expr{SQL: "SUM(?)", Vars: []interface{}{e.Col}}} +} + +func (e expr) WithTable(table string) Expr { + return &expr{Col: e.Col, expression: clause.Expr{SQL: "?.?", Vars: []interface{}{clause.Table{Name: table}, e.Col}}} +} + +// ======================== 列间通用比较 ======================== +func (e expr) EqCol(col Expr) Expr { + return &expr{expression: clause.Expr{SQL: "? = ?", Vars: []interface{}{e.RawExpr(), col.RawExpr()}}} +} + +func (e expr) GtCol(col Expr) Expr { + return &expr{expression: clause.Expr{SQL: "? > ?", Vars: []interface{}{e.RawExpr(), col.RawExpr()}}} +} + +func (e expr) GteCol(col Expr) Expr { + return &expr{expression: clause.Expr{SQL: "? >= ?", Vars: []interface{}{e.RawExpr(), col.RawExpr()}}} +} + +func (e expr) LtCol(col Expr) Expr { + return &expr{expression: clause.Expr{SQL: "? < ?", Vars: []interface{}{e.RawExpr(), col.RawExpr()}}} +} + +func (e expr) LteCol(col Expr) Expr { + return &expr{expression: clause.Expr{SQL: "? <= ?", Vars: []interface{}{e.RawExpr(), col.RawExpr()}}} +} + +// ======================== 关键字 ======================== +func (e expr) As(alias string) Expr { + if e.expression != nil { + return &expr{Col: e.Col, expression: clause.Expr{SQL: "? AS ?", Vars: []interface{}{e.expression, clause.Column{Name: alias}}}} + } + return &expr{Col: e.Col, expression: clause.Expr{SQL: "?", Vars: []interface{}{clause.Column{Name: e.Col.Name, Alias: alias}}}} +} + +func (e expr) Desc() Expr { + return &expr{Col: e.Col, expression: clause.Expr{SQL: "? DESC", Vars: []interface{}{e.Col}}} +} + +// ======================== 通用Experssion ======================== +func (e expr) between(values []interface{}) Expr { + return &expr{Col: e.Col, expression: clause.Expr{SQL: "? BETWEEN ? AND ?", Vars: append([]interface{}{e.Col}, values...)}} +} + +func (e expr) add(value interface{}) Expr { + switch v := value.(type) { + case time.Duration: + return &expr{Col: e.Col, expression: clause.Expr{SQL: "DATE_ADD(?, INTERVAL ? MICROSECOND)", Vars: []interface{}{e.Col, v.Microseconds()}}} + default: + return &expr{Col: e.Col, expression: clause.Expr{SQL: "?+?", Vars: []interface{}{e.Col, value}}} + } +} + +func (e expr) sub(value interface{}) Expr { + switch v := value.(type) { + case time.Duration: + return &expr{Col: e.Col, expression: clause.Expr{SQL: "DATE_SUB(?, INTERVAL ? MICROSECOND)", Vars: []interface{}{e.Col, v.Microseconds()}}} + default: + return &expr{Col: e.Col, expression: clause.Expr{SQL: "?-?", Vars: []interface{}{e.Col, value}}} + } +} + +func (e expr) mul(value interface{}) Expr { + return &expr{Col: e.Col, expression: clause.Expr{SQL: "?*?", Vars: []interface{}{e.Col, value}}} +} + +func (e expr) div(value interface{}) Expr { + return &expr{Col: e.Col, expression: clause.Expr{SQL: "?/?", Vars: []interface{}{e.Col, value}}} +} + +func (e expr) mod(value interface{}) Expr { + return &expr{Col: e.Col, expression: clause.Expr{SQL: "?%?", Vars: []interface{}{e.Col, value}}} +} + +func (e expr) floorDiv(value interface{}) Expr { + return &expr{Col: e.Col, expression: clause.Expr{SQL: "? DIV ?", Vars: []interface{}{e.Col, value}}} +} + +func (e expr) rightShift(value interface{}) Expr { + return &expr{Col: e.Col, expression: clause.Expr{SQL: "?>>?", Vars: []interface{}{e.Col, value}}} +} + +func (e expr) leftShift(value interface{}) Expr { + return &expr{Col: e.Col, expression: clause.Expr{SQL: "?< 0 + vars := make([]string, len(columns)) + queryCols := make([]interface{}, len(columns)) + for i, c := range columns { + vars[i], queryCols[i] = "?", c.RawExpr() + } + return &expr{expression: clause.Expr{ + SQL: fmt.Sprint(wrap(strings.Join(vars, ", ")), " IN ", wrap(subQuery.SQL.String())), + Vars: append(queryCols, subQuery.Statement.Vars...), + }} + } +} + +type CompareOperate string + +const ( + EqOp CompareOperate = " = " + GtOp CompareOperate = " > " + GteOp CompareOperate = " >= " + LtOp CompareOperate = " < " + LteOp CompareOperate = " <= " +) + +func CompareSubQuery(op CompareOperate, column Expr, subQuery *gorm.Statement) Expr { + return &expr{expression: clause.Expr{ + SQL: fmt.Sprint("?", op, wrap(subQuery.SQL.String())), + Vars: append([]interface{}{column.RawExpr()}, subQuery.Statement.Vars...), + }} +} + +func wrap(subQuery string) string { + return "(" + subQuery + ")" +} diff --git a/field/bool.go b/field/bool.go new file mode 100644 index 00000000..53171fe4 --- /dev/null +++ b/field/bool.go @@ -0,0 +1,35 @@ +package field + +type Bool Field + +func (field Bool) Not() Expr { + return field.not() +} + +func (field Bool) Is(value bool) Expr { + return field.is(value) +} + +func (field Bool) And(value bool) Expr { + return field.and(value) +} + +func (field Bool) Or(value bool) Expr { + return field.or(value) +} + +func (field Bool) Xor(value bool) Expr { + return field.xor(value) +} + +func (field Bool) BitXor(value bool) Expr { + return field.bitXor(value) +} + +func (field Bool) BitAnd(value bool) Expr { + return field.bitAnd(value) +} + +func (field Bool) BitOr(value bool) Expr { + return field.bitOr(value) +} diff --git a/field/doc.go b/field/doc.go new file mode 100644 index 00000000..dffa1e33 --- /dev/null +++ b/field/doc.go @@ -0,0 +1,2 @@ +// package field implement all type field and method +package field diff --git a/field/export.go b/field/export.go new file mode 100644 index 00000000..e7cfb201 --- /dev/null +++ b/field/export.go @@ -0,0 +1,83 @@ +package field + +import "gorm.io/gorm/clause" + +// ======================== 通用类型 ======================= + +func NewField(column string) Field { + return Field{expr: expr{Col: clause.Column{Name: column}}} +} + +// ======================== 整数类型 ======================= + +func NewInt(column string) Int { + return Int{expr: expr{Col: clause.Column{Name: column}}} +} + +func NewInt8(column string) Int8 { + return Int8{expr: expr{Col: clause.Column{Name: column}}} +} + +func NewInt16(column string) Int16 { + return Int16{expr: expr{Col: clause.Column{Name: column}}} +} + +func NewInt32(column string) Int32 { + return Int32{expr: expr{Col: clause.Column{Name: column}}} +} + +func NewInt64(column string) Int64 { + return Int64{expr: expr{Col: clause.Column{Name: column}}} +} + +func NewUint(column string) Uint { + return Uint{expr: expr{Col: clause.Column{Name: column}}} +} + +func NewUint8(column string) Uint8 { + return Uint8{expr: expr{Col: clause.Column{Name: column}}} +} + +func NewUint16(column string) Uint16 { + return Uint16{expr: expr{Col: clause.Column{Name: column}}} +} + +func NewUint32(column string) Uint32 { + return Uint32{expr: expr{Col: clause.Column{Name: column}}} +} + +func NewUint64(column string) Uint64 { + return Uint64{expr: expr{Col: clause.Column{Name: column}}} +} + +// ======================== 浮点数类型 ======================= + +func NewFloat32(column string) Float32 { + return Float32{expr: expr{Col: clause.Column{Name: column}}} +} + +func NewFloat64(column string) Float64 { + return Float64{expr: expr{Col: clause.Column{Name: column}}} +} + +// ======================== 字符串类型 ======================= + +func NewString(column string) String { + return String{expr: expr{Col: clause.Column{Name: column}}} +} + +func NewBytes(column string) Bytes { + return Bytes{expr: expr{Col: clause.Column{Name: column}}} +} + +// ======================== 布尔类型 ======================= + +func NewBool(column string) Bool { + return Bool{expr: expr{Col: clause.Column{Name: column}}} +} + +// ======================== 时间类型 ======================= + +func NewTime(column string) Time { + return Time{expr: expr{Col: clause.Column{Name: column}}} +} diff --git a/field/field_test.go b/field/field_test.go new file mode 100644 index 00000000..76b60ac1 --- /dev/null +++ b/field/field_test.go @@ -0,0 +1,313 @@ +package field_test + +import ( + "reflect" + "strings" + "sync" + "testing" + "time" + + "gorm.io/gorm" + "gorm.io/gorm/clause" + "gorm.io/gorm/schema" + "gorm.io/gorm/utils/tests" + + "gorm.io/gen/field" +) + +var db, _ = gorm.Open(tests.DummyDialector{}, nil) + +type User struct { + gorm.Model + Name string + Age uint + // Birthday *time.Time + // Account Account + // Pets []*Pet + // Toys []Toy `gorm:"polymorphic:Owner"` + // CompanyID *int + // Company Company + // ManagerID *uint + // Manager *User + // Team []User `gorm:"foreignkey:ManagerID"` + // Languages []Language `gorm:"many2many:UserSpeak;"` + // Friends []*User `gorm:"many2many:user_friends;"` + // Active bool +} + +func checkBuildExpr(t *testing.T, e field.Expr, result string, vars []interface{}) { + user, _ := schema.Parse(&User{}, &sync.Map{}, db.NamingStrategy) + stmt := &gorm.Statement{DB: db, Table: user.Table, Schema: user, Clauses: map[string]clause.Clause{}} + + e.Build(stmt) + + sql := strings.TrimSpace(stmt.SQL.String()) + if sql != result { + t.Errorf("SQL expects %v got %v", result, sql) + } + + if !reflect.DeepEqual(stmt.Vars, vars) { + t.Errorf("Vars expects %+v got %v", stmt.Vars, vars) + } +} + +func TestExpr_Build(t *testing.T) { + timeData, _ := time.Parse("2006-01-02 15:04:05", "2021-06-29 15:11:49") + + testcases := []struct { + Expr field.Expr + ExpectedVars []interface{} + Result string + }{ + // ======================== 通用方法数据 ======================== + { + Expr: field.NewField("id").EqCol(field.NewField("new_id")), + ExpectedVars: nil, + Result: "`id` = `new_id`", + }, + { + Expr: field.NewField("id").EqCol(field.NewField("new_id").Avg()), + ExpectedVars: nil, + Result: "`id` = AVG(`new_id`)", + }, + { + Expr: field.NewField("id").EqCol(field.NewField("new_id").WithTable("tableB")), + ExpectedVars: nil, + Result: "`id` = `tableB`.`new_id`", + }, + { + Expr: field.NewField("id").EqCol(field.NewField("new_id").WithTable("tableB")), + ExpectedVars: nil, + Result: "`id` = `tableB`.`new_id`", + }, + // ======================== 整数类型数据 ======================== + { + Expr: field.NewUint("id"), + ExpectedVars: nil, + Result: "`id`", + }, + { + Expr: field.NewUint("i`d"), + ExpectedVars: nil, + Result: "`i`d`", // should be `i``d` + }, + { + Expr: field.NewUint("id").Avg(), + ExpectedVars: nil, + Result: "AVG(`id`)", + }, + { + Expr: field.NewUint("id").Desc(), + ExpectedVars: nil, + Result: "`id` DESC", + }, + { + Expr: field.NewUint("id").As("number"), + ExpectedVars: nil, + Result: "`id` AS `number`", + }, + { + Expr: field.NewUint("id").Avg().As("number"), + ExpectedVars: nil, + Result: "AVG(`id`) AS `number`", + }, + { + Expr: field.NewUint("id").Eq(10), + ExpectedVars: []interface{}{uint(10)}, + Result: "`id` = ?", + }, + { + Expr: field.NewUint("id").Neq(10), + ExpectedVars: []interface{}{uint(10)}, + Result: "`id` <> ?", + }, + { + Expr: field.NewUint("id").Gt(1), + ExpectedVars: []interface{}{uint(1)}, + Result: "`id` > ?", + }, + { + Expr: field.NewUint("id").Gte(1), + ExpectedVars: []interface{}{uint(1)}, + Result: "`id` >= ?", + }, + { + Expr: field.NewUint("id").Lt(1), + ExpectedVars: []interface{}{uint(1)}, + Result: "`id` < ?", + }, + { + Expr: field.NewUint("id").Lte(1), + ExpectedVars: []interface{}{uint(1)}, + Result: "`id` <= ?", + }, + { + Expr: field.NewUint("id").Mod(7), + ExpectedVars: []interface{}{uint(7)}, + Result: "`id`%?", + }, + { + Expr: field.And(field.NewUint("id").Gt(1), field.NewUint("id").Lt(10)), + ExpectedVars: []interface{}{uint(1), uint(10)}, + Result: "(`id` > ? AND `id` < ?)", + }, + { + Expr: field.Or(field.NewUint("id").Lt(4), field.NewUint("id").Gt(6)), + ExpectedVars: []interface{}{uint(4), uint(6)}, + Result: "(`id` < ? OR `id` > ?)", + }, + { + Expr: field.NewUint("id").In(1, 2, 3), + ExpectedVars: []interface{}{uint(1), uint(2), uint(3)}, + Result: "`id` IN (?,?,?)", + }, + { + Expr: field.NewUint("id").NotIn(1, 2, 3), + ExpectedVars: []interface{}{uint(1), uint(2), uint(3)}, + Result: "NOT `id` IN (?,?,?)", + }, + { + Expr: field.NewUint("id").Between(1, 10), + ExpectedVars: []interface{}{uint(1), uint(10)}, + Result: "`id` BETWEEN ? AND ?", + }, + { + Expr: field.NewUint("id").Count(), + ExpectedVars: nil, + Result: "COUNT(`id`)", + }, + { + Expr: field.NewInt("age").RightShift(3), + ExpectedVars: []interface{}{3}, + Result: "`age`>>?", + }, + { + Expr: field.NewInt("age").LeftShift(3), + ExpectedVars: []interface{}{3}, + Result: "`age`< ?", + }, + { + Expr: field.NewString("name").Like("%%tom%%"), + ExpectedVars: []interface{}{"%%tom%%"}, + Result: "`name` LIKE ?", + }, + { + Expr: field.NewString("name").NotLike("%%tom%%"), + ExpectedVars: []interface{}{"%%tom%%"}, + Result: "NOT `name` LIKE ?", + }, + { + Expr: field.NewString("name").Regexp(".*"), + ExpectedVars: []interface{}{".*"}, + Result: "`name` REGEXP ?", + }, + { + Expr: field.NewString("name").NotRegxp(".*"), + ExpectedVars: []interface{}{".*"}, + Result: "NOT `name` REGEXP ?", + }, + // ======================== 时间类型数据 ======================== + { + Expr: field.NewTime("creatAt").Eq(timeData), + ExpectedVars: []interface{}{timeData}, + Result: "`creatAt` = ?", + }, + { + Expr: field.NewTime("creatAt").Gt(timeData), + ExpectedVars: []interface{}{timeData}, + Result: "`creatAt` > ?", + }, + { + Expr: field.NewTime("creatAt").Gte(timeData), + ExpectedVars: []interface{}{timeData}, + Result: "`creatAt` >= ?", + }, + { + Expr: field.NewTime("creatAt").Lt(timeData), + ExpectedVars: []interface{}{timeData}, + Result: "`creatAt` < ?", + }, + { + Expr: field.NewTime("creatAt").Lte(timeData), + ExpectedVars: []interface{}{timeData}, + Result: "`creatAt` <= ?", + }, + { + Expr: field.NewTime("creatAt").Between(timeData, timeData.Add(24*time.Hour)), + ExpectedVars: []interface{}{timeData, timeData.Add(24 * time.Hour)}, + Result: "`creatAt` BETWEEN ? AND ?", + }, + { + Expr: field.NewTime("creatAt").Add(24 * time.Hour), + ExpectedVars: []interface{}{time.Duration(24 * time.Hour).Microseconds()}, + Result: "DATE_ADD(`creatAt`, INTERVAL ? MICROSECOND)", + }, + { + Expr: field.NewTime("creatAt").Sub(24 * time.Hour), + ExpectedVars: []interface{}{time.Duration(24 * time.Hour).Microseconds()}, + Result: "DATE_SUB(`creatAt`, INTERVAL ? MICROSECOND)", + }, + // ======================== 布尔类型数据 ======================== + { + Expr: field.NewBool("male").Not(), + ExpectedVars: nil, + Result: "NOT `male`", + }, + { + Expr: field.NewBool("male").Is(true), + ExpectedVars: []interface{}{true}, + Result: "`male` IS ?", + }, + { + Expr: field.NewBool("male").And(true), + ExpectedVars: []interface{}{true}, + Result: "`male` AND ?", + }, + { + Expr: field.NewBool("male").Or(true), + ExpectedVars: []interface{}{true}, + Result: "`male` OR ?", + }, + } + + for _, testcase := range testcases { + checkBuildExpr(t, testcase.Expr, testcase.Result, testcase.ExpectedVars) + } +} diff --git a/field/float.go b/field/float.go new file mode 100644 index 00000000..64e4a41d --- /dev/null +++ b/field/float.go @@ -0,0 +1,159 @@ +package field + +import "gorm.io/gorm/clause" + +type Float64 Field + +func (field Float64) Eq(value float64) Expr { + return expr{expression: clause.Eq{Column: field.Col, Value: value}} +} + +func (field Float64) Neq(value float64) Expr { + return expr{expression: clause.Neq{Column: field.Col, Value: value}} +} + +func (field Float64) Gt(value float64) Expr { + return expr{expression: clause.Gt{Column: field.Col, Value: value}} +} + +func (field Float64) Gte(value float64) Expr { + return expr{expression: clause.Gte{Column: field.Col, Value: value}} +} + +func (field Float64) Lt(value float64) Expr { + return expr{expression: clause.Lt{Column: field.Col, Value: value}} +} + +func (field Float64) Lte(value float64) Expr { + return expr{expression: clause.Lte{Column: field.Col, Value: value}} +} + +func (field Float64) In(values ...float64) Expr { + return expr{expression: clause.IN{Column: field.Col, Values: field.toSlice(values...)}} +} + +func (field Float64) NotIn(values ...float64) Expr { + return expr{expression: clause.Not(field.In(values...))} +} + +func (field Float64) Between(left float64, right float64) Expr { + return field.between([]interface{}{left, right}) +} + +func (field Float64) NotBetween(left float64, right float64) Expr { + return Not(field.Between(left, right)) +} + +func (field Float64) Like(value float64) Expr { + return expr{expression: clause.Like{Column: field.Col, Value: value}} +} + +func (field Float64) NotLike(value float64) Expr { + return expr{expression: clause.Not(field.Like(value))} +} + +func (field Float64) Add(value float64) Expr { + return field.add(value) +} + +func (field Float64) Sub(value float64) Expr { + return field.sub(value) +} + +func (field Float64) Mul(value float64) Expr { + return field.mul(value) +} + +func (field Float64) Div(value float64) Expr { + return field.div(value) +} + +func (field Float64) FloorDiv(value float64) Expr { + return field.floorDiv(value) +} + +func (field Float64) toSlice(values ...float64) []interface{} { + slice := make([]interface{}, len(values)) + for i, v := range values { + slice[i] = v + } + return slice +} + +type Float32 Float64 + +func (field Float32) Eq(value float32) Expr { + return expr{expression: clause.Eq{Column: field.Col, Value: value}} +} + +func (field Float32) Neq(value float32) Expr { + return expr{expression: clause.Neq{Column: field.Col, Value: value}} +} + +func (field Float32) Gt(value float32) Expr { + return expr{expression: clause.Gt{Column: field.Col, Value: value}} +} + +func (field Float32) Gte(value float32) Expr { + return expr{expression: clause.Gte{Column: field.Col, Value: value}} +} + +func (field Float32) Lt(value float32) Expr { + return expr{expression: clause.Lt{Column: field.Col, Value: value}} +} + +func (field Float32) Lte(value float32) Expr { + return expr{expression: clause.Lte{Column: field.Col, Value: value}} +} + +func (field Float32) In(values ...float32) Expr { + return expr{expression: clause.IN{Column: field.Col, Values: field.toSlice(values...)}} +} + +func (field Float32) NotIn(values ...float32) Expr { + return expr{expression: clause.Not(field.In(values...))} +} + +func (field Float32) Between(left float32, right float32) Expr { + return field.between([]interface{}{left, right}) +} + +func (field Float32) NotBetween(left float32, right float32) Expr { + return Not(field.Between(left, right)) +} + +func (field Float32) Like(value float32) Expr { + return expr{expression: clause.Like{Column: field.Col, Value: value}} +} + +func (field Float32) NotLike(value float32) Expr { + return expr{expression: clause.Not(field.Like(value))} +} + +func (field Float32) Add(value float32) Expr { + return field.add(value) +} + +func (field Float32) Sub(value float32) Expr { + return field.sub(value) +} + +func (field Float32) Mul(value float32) Expr { + return field.mul(value) +} + +func (field Float32) Div(value float32) Expr { + return field.div(value) +} + +func (field Float32) FloorDiv(value float32) Expr { + return field.floorDiv(value) +} + +func (field Float32) toSlice(values ...float32) []interface{} { + slice := make([]interface{}, len(values)) + for i, v := range values { + slice[i] = v + } + return slice +} diff --git a/field/int.go b/field/int.go new file mode 100644 index 00000000..6cc5ac88 --- /dev/null +++ b/field/int.go @@ -0,0 +1,1065 @@ +package field + +import ( + "gorm.io/gorm/clause" +) + +type Int Field + +func (field Int) Eq(value int) Expr { + return expr{expression: clause.Eq{Column: field.Col, Value: value}} +} + +func (field Int) Neq(value int) Expr { + return expr{expression: clause.Neq{Column: field.Col, Value: value}} +} + +func (field Int) Gt(value int) Expr { + return expr{expression: clause.Gt{Column: field.Col, Value: value}} +} + +func (field Int) Gte(value int) Expr { + return expr{expression: clause.Gte{Column: field.Col, Value: value}} +} + +func (field Int) Lt(value int) Expr { + return expr{expression: clause.Lt{Column: field.Col, Value: value}} +} + +func (field Int) Lte(value int) Expr { + return expr{expression: clause.Lte{Column: field.Col, Value: value}} +} + +func (field Int) In(values ...int) Expr { + return expr{expression: clause.IN{Column: field.Col, Values: field.toSlice(values...)}} +} + +func (field Int) NotIn(values ...int) Expr { + return expr{expression: clause.Not(field.In(values...))} +} + +func (field Int) Between(left int, right int) Expr { + return field.between([]interface{}{left, right}) +} + +func (field Int) NotBetween(left int, right int) Expr { + return Not(field.Between(left, right)) +} + +func (field Int) Like(value int) Expr { + return expr{expression: clause.Like{Column: field.Col, Value: value}} +} + +func (field Int) NotLike(value int) Expr { + return expr{expression: clause.Not(field.Like(value))} +} + +func (field Int) Add(value int) Expr { + return field.add(value) +} + +func (field Int) Sub(value int) Expr { + return field.sub(value) +} + +func (field Int) Mul(value int) Expr { + return field.mul(value) +} + +func (field Int) Div(value int) Expr { + return field.div(value) +} + +func (field Int) Mod(value int) Expr { + return field.mod(value) +} + +func (field Int) FloorDiv(value int) Expr { + return field.floorDiv(value) +} + +func (field Int) RightShift(value int) Expr { + return field.rightShift(value) +} + +func (field Int) LeftShift(value int) Expr { + return field.leftShift(value) +} + +func (field Int) BitXor(value int) Expr { + return field.bitXor(value) +} + +func (field Int) BitAnd(value int) Expr { + return field.bitAnd(value) +} + +func (field Int) BitOr(value int) Expr { + return field.bitOr(value) +} + +func (field Int) BitFlip() Expr { + return field.bitFlip() +} + +func (field Int) toSlice(values ...int) []interface{} { + slice := make([]interface{}, len(values)) + for i, v := range values { + slice[i] = v + } + return slice +} + +type Int8 Int + +func (field Int8) Eq(value int8) Expr { + return expr{expression: clause.Eq{Column: field.Col, Value: value}} +} + +func (field Int8) Neq(value int8) Expr { + return expr{expression: clause.Neq{Column: field.Col, Value: value}} +} + +func (field Int8) Gt(value int8) Expr { + return expr{expression: clause.Gt{Column: field.Col, Value: value}} +} + +func (field Int8) Gte(value int8) Expr { + return expr{expression: clause.Gte{Column: field.Col, Value: value}} +} + +func (field Int8) Lt(value int8) Expr { + return expr{expression: clause.Lt{Column: field.Col, Value: value}} +} + +func (field Int8) Lte(value int8) Expr { + return expr{expression: clause.Lte{Column: field.Col, Value: value}} +} + +func (field Int8) In(values ...int8) Expr { + return expr{expression: clause.IN{Column: field.Col, Values: field.toSlice(values...)}} +} + +func (field Int8) NotIn(values ...int8) Expr { + return expr{expression: clause.Not(field.In(values...))} +} + +func (field Int8) Between(left int8, right int8) Expr { + return field.between([]interface{}{left, right}) +} + +func (field Int8) NotBetween(left int8, right int8) Expr { + return Not(field.Between(left, right)) +} + +func (field Int8) Like(value int8) Expr { + return expr{expression: clause.Like{Column: field.Col, Value: value}} +} + +func (field Int8) NotLike(value int8) Expr { + return expr{expression: clause.Not(field.Like(value))} +} + +func (field Int8) Add(value int8) Expr { + return field.add(value) +} + +func (field Int8) Sub(value int8) Expr { + return field.sub(value) +} + +func (field Int8) Mul(value int8) Expr { + return field.mul(value) +} + +func (field Int8) Div(value int8) Expr { + return field.div(value) +} + +func (field Int8) Mod(value int8) Expr { + return field.mod(value) +} + +func (field Int8) FloorDiv(value int8) Expr { + return field.floorDiv(value) +} + +func (field Int8) RightShift(value int8) Expr { + return field.rightShift(value) +} + +func (field Int8) LeftShift(value int8) Expr { + return field.leftShift(value) +} + +func (field Int8) BitXor(value int8) Expr { + return field.bitXor(value) +} + +func (field Int8) BitAnd(value int8) Expr { + return field.bitAnd(value) +} + +func (field Int8) BitOr(value int8) Expr { + return field.bitOr(value) +} + +func (field Int8) BitFlip() Expr { + return field.bitFlip() +} + +func (field Int8) toSlice(values ...int8) []interface{} { + slice := make([]interface{}, len(values)) + for i, v := range values { + slice[i] = v + } + return slice +} + +type Int16 Int + +func (field Int16) Eq(value int16) Expr { + return expr{expression: clause.Eq{Column: field.Col, Value: value}} +} + +func (field Int16) Neq(value int16) Expr { + return expr{expression: clause.Neq{Column: field.Col, Value: value}} +} + +func (field Int16) Gt(value int16) Expr { + return expr{expression: clause.Gt{Column: field.Col, Value: value}} +} + +func (field Int16) Gte(value int16) Expr { + return expr{expression: clause.Gte{Column: field.Col, Value: value}} +} + +func (field Int16) Lt(value int16) Expr { + return expr{expression: clause.Lt{Column: field.Col, Value: value}} +} + +func (field Int16) Lte(value int16) Expr { + return expr{expression: clause.Lte{Column: field.Col, Value: value}} +} + +func (field Int16) In(values ...int16) Expr { + return expr{expression: clause.IN{Column: field.Col, Values: field.toSlice(values...)}} +} + +func (field Int16) NotIn(values ...int16) Expr { + return expr{expression: clause.Not(field.In(values...))} +} + +func (field Int16) Between(left int16, right int16) Expr { + return field.between([]interface{}{left, right}) +} + +func (field Int16) NotBetween(left int16, right int16) Expr { + return Not(field.Between(left, right)) +} + +func (field Int16) Like(value int16) Expr { + return expr{expression: clause.Like{Column: field.Col, Value: value}} +} + +func (field Int16) NotLike(value int16) Expr { + return expr{expression: clause.Not(field.Like(value))} +} + +func (field Int16) Add(value int16) Expr { + return field.add(value) +} + +func (field Int16) Sub(value int16) Expr { + return field.sub(value) +} + +func (field Int16) Mul(value int16) Expr { + return field.mul(value) +} + +func (field Int16) Div(value int16) Expr { + return field.div(value) +} + +func (field Int16) Mod(value int16) Expr { + return field.mod(value) +} + +func (field Int16) FloorDiv(value int16) Expr { + return field.floorDiv(value) +} + +func (field Int16) RightShift(value int16) Expr { + return field.rightShift(value) +} + +func (field Int16) LeftShift(value int16) Expr { + return field.leftShift(value) +} + +func (field Int16) BitXor(value int16) Expr { + return field.bitXor(value) +} + +func (field Int16) BitAnd(value int16) Expr { + return field.bitAnd(value) +} + +func (field Int16) BitOr(value int16) Expr { + return field.bitOr(value) +} + +func (field Int16) BitFlip() Expr { + return field.bitFlip() +} + +func (field Int16) toSlice(values ...int16) []interface{} { + slice := make([]interface{}, len(values)) + for i, v := range values { + slice[i] = v + } + return slice +} + +type Int32 Int + +func (field Int32) Eq(value int32) Expr { + return expr{expression: clause.Eq{Column: field.Col, Value: value}} +} + +func (field Int32) Neq(value int32) Expr { + return expr{expression: clause.Neq{Column: field.Col, Value: value}} +} + +func (field Int32) Gt(value int32) Expr { + return expr{expression: clause.Gt{Column: field.Col, Value: value}} +} + +func (field Int32) Gte(value int32) Expr { + return expr{expression: clause.Gte{Column: field.Col, Value: value}} +} + +func (field Int32) Lt(value int32) Expr { + return expr{expression: clause.Lt{Column: field.Col, Value: value}} +} + +func (field Int32) Lte(value int32) Expr { + return expr{expression: clause.Lte{Column: field.Col, Value: value}} +} + +func (field Int32) In(values ...int32) Expr { + return expr{expression: clause.IN{Column: field.Col, Values: field.toSlice(values...)}} +} + +func (field Int32) NotIn(values ...int32) Expr { + return expr{expression: clause.Not(field.In(values...))} +} + +func (field Int32) Between(left int32, right int32) Expr { + return field.between([]interface{}{left, right}) +} + +func (field Int32) NotBetween(left int32, right int32) Expr { + return Not(field.Between(left, right)) +} + +func (field Int32) Like(value int32) Expr { + return expr{expression: clause.Like{Column: field.Col, Value: value}} +} + +func (field Int32) NotLike(value int32) Expr { + return expr{expression: clause.Not(field.Like(value))} +} + +func (field Int32) Add(value int32) Expr { + return field.add(value) +} + +func (field Int32) Sub(value int32) Expr { + return field.sub(value) +} + +func (field Int32) Mul(value int32) Expr { + return field.mul(value) +} + +func (field Int32) Div(value int32) Expr { + return field.div(value) +} + +func (field Int32) Mod(value int32) Expr { + return field.mod(value) +} + +func (field Int32) FloorDiv(value int32) Expr { + return field.floorDiv(value) +} + +func (field Int32) RightShift(value int32) Expr { + return field.rightShift(value) +} + +func (field Int32) LeftShift(value int32) Expr { + return field.leftShift(value) +} + +func (field Int32) BitXor(value int32) Expr { + return field.bitXor(value) +} + +func (field Int32) BitAnd(value int32) Expr { + return field.bitAnd(value) +} + +func (field Int32) BitOr(value int32) Expr { + return field.bitOr(value) +} + +func (field Int32) BitFlip() Expr { + return field.bitFlip() +} + +func (field Int32) toSlice(values ...int32) []interface{} { + slice := make([]interface{}, len(values)) + for i, v := range values { + slice[i] = v + } + return slice +} + +type Int64 Int + +func (field Int64) Eq(value int64) Expr { + return expr{expression: clause.Eq{Column: field.Col, Value: value}} +} + +func (field Int64) Neq(value int64) Expr { + return expr{expression: clause.Neq{Column: field.Col, Value: value}} +} + +func (field Int64) Gt(value int64) Expr { + return expr{expression: clause.Gt{Column: field.Col, Value: value}} +} + +func (field Int64) Gte(value int64) Expr { + return expr{expression: clause.Gte{Column: field.Col, Value: value}} +} + +func (field Int64) Lt(value int64) Expr { + return expr{expression: clause.Lt{Column: field.Col, Value: value}} +} + +func (field Int64) Lte(value int64) Expr { + return expr{expression: clause.Lte{Column: field.Col, Value: value}} +} + +func (field Int64) In(values ...int64) Expr { + return expr{expression: clause.IN{Column: field.Col, Values: field.toSlice(values...)}} +} + +func (field Int64) NotIn(values ...int64) Expr { + return expr{expression: clause.Not(field.In(values...))} +} + +func (field Int64) Between(left int64, right int64) Expr { + return field.between([]interface{}{left, right}) +} + +func (field Int64) NotBetween(left int64, right int64) Expr { + return Not(field.Between(left, right)) +} + +func (field Int64) Like(value int64) Expr { + return expr{expression: clause.Like{Column: field.Col, Value: value}} +} + +func (field Int64) NotLike(value int64) Expr { + return expr{expression: clause.Not(field.Like(value))} +} + +func (field Int64) Add(value int64) Expr { + return field.add(value) +} + +func (field Int64) Sub(value int64) Expr { + return field.sub(value) +} + +func (field Int64) Mul(value int64) Expr { + return field.mul(value) +} + +func (field Int64) Div(value int64) Expr { + return field.div(value) +} + +func (field Int64) Mod(value int64) Expr { + return field.mod(value) +} + +func (field Int64) FloorDiv(value int64) Expr { + return field.floorDiv(value) +} + +func (field Int64) RightShift(value int64) Expr { + return field.rightShift(value) +} + +func (field Int64) LeftShift(value int64) Expr { + return field.leftShift(value) +} + +func (field Int64) BitXor(value int64) Expr { + return field.bitXor(value) +} + +func (field Int64) BitAnd(value int64) Expr { + return field.bitAnd(value) +} + +func (field Int64) BitOr(value int64) Expr { + return field.bitOr(value) +} + +func (field Int64) BitFlip() Expr { + return field.bitFlip() +} + +func (field Int64) toSlice(values ...int64) []interface{} { + slice := make([]interface{}, len(values)) + for i, v := range values { + slice[i] = v + } + return slice +} + +type Uint Int + +func (field Uint) Eq(value uint) Expr { + return expr{expression: clause.Eq{Column: field.Col, Value: value}} +} + +func (field Uint) Neq(value uint) Expr { + return expr{expression: clause.Neq{Column: field.Col, Value: value}} +} + +func (field Uint) Gt(value uint) Expr { + return expr{expression: clause.Gt{Column: field.Col, Value: value}} +} + +func (field Uint) Gte(value uint) Expr { + return expr{expression: clause.Gte{Column: field.Col, Value: value}} +} + +func (field Uint) Lt(value uint) Expr { + return expr{expression: clause.Lt{Column: field.Col, Value: value}} +} + +func (field Uint) Lte(value uint) Expr { + return expr{expression: clause.Lte{Column: field.Col, Value: value}} +} + +func (field Uint) In(values ...uint) Expr { + return expr{expression: clause.IN{Column: field.Col, Values: field.toSlice(values...)}} +} + +func (field Uint) NotIn(values ...uint) Expr { + return expr{expression: clause.Not(field.In(values...))} +} + +func (field Uint) Between(left uint, right uint) Expr { + return field.between([]interface{}{left, right}) +} + +func (field Uint) NotBetween(left uint, right uint) Expr { + return Not(field.Between(left, right)) +} + +func (field Uint) Like(value uint) Expr { + return expr{expression: clause.Like{Column: field.Col, Value: value}} +} + +func (field Uint) NotLike(value uint) Expr { + return expr{expression: clause.Not(field.Like(value))} +} + +func (field Uint) Add(value uint) Expr { + return field.add(value) +} + +func (field Uint) Sub(value uint) Expr { + return field.sub(value) +} + +func (field Uint) Mul(value uint) Expr { + return field.mul(value) +} + +func (field Uint) Div(value uint) Expr { + return field.div(value) +} + +func (field Uint) Mod(value uint) Expr { + return field.mod(value) +} + +func (field Uint) FloorDiv(value uint) Expr { + return field.floorDiv(value) +} + +func (field Uint) RightShift(value uint) Expr { + return field.rightShift(value) +} + +func (field Uint) LeftShift(value uint) Expr { + return field.leftShift(value) +} + +func (field Uint) BitXor(value uint) Expr { + return field.bitXor(value) +} + +func (field Uint) BitAnd(value uint) Expr { + return field.bitAnd(value) +} + +func (field Uint) BitOr(value uint) Expr { + return field.bitOr(value) +} + +func (field Uint) BitFlip() Expr { + return field.bitFlip() +} + +func (field Uint) toSlice(values ...uint) []interface{} { + slice := make([]interface{}, len(values)) + for i, v := range values { + slice[i] = v + } + return slice +} + +type Uint8 Int + +func (field Uint8) Eq(value uint8) Expr { + return expr{expression: clause.Eq{Column: field.Col, Value: value}} +} + +func (field Uint8) Neq(value uint8) Expr { + return expr{expression: clause.Neq{Column: field.Col, Value: value}} +} + +func (field Uint8) Gt(value uint8) Expr { + return expr{expression: clause.Gt{Column: field.Col, Value: value}} +} + +func (field Uint8) Gte(value uint8) Expr { + return expr{expression: clause.Gte{Column: field.Col, Value: value}} +} + +func (field Uint8) Lt(value uint8) Expr { + return expr{expression: clause.Lt{Column: field.Col, Value: value}} +} + +func (field Uint8) Lte(value uint8) Expr { + return expr{expression: clause.Lte{Column: field.Col, Value: value}} +} + +func (field Uint8) In(values ...uint8) Expr { + return expr{expression: clause.IN{Column: field.Col, Values: field.toSlice(values...)}} +} + +func (field Uint8) NotIn(values ...uint8) Expr { + return expr{expression: clause.Not(field.In(values...))} +} + +func (field Uint8) Between(left uint8, right uint8) Expr { + return field.between([]interface{}{left, right}) +} + +func (field Uint8) NotBetween(left uint8, right uint8) Expr { + return Not(field.Between(left, right)) +} + +func (field Uint8) Like(value uint8) Expr { + return expr{expression: clause.Like{Column: field.Col, Value: value}} +} + +func (field Uint8) NotLike(value uint8) Expr { + return expr{expression: clause.Not(field.Like(value))} +} + +func (field Uint8) Add(value uint8) Expr { + return field.add(value) +} + +func (field Uint8) Sub(value uint8) Expr { + return field.sub(value) +} + +func (field Uint8) Mul(value uint8) Expr { + return field.mul(value) +} + +func (field Uint8) Div(value uint8) Expr { + return field.div(value) +} + +func (field Uint8) Mod(value uint8) Expr { + return field.mod(value) +} + +func (field Uint8) FloorDiv(value uint8) Expr { + return field.floorDiv(value) +} + +func (field Uint8) RightShift(value uint8) Expr { + return field.rightShift(value) +} + +func (field Uint8) LeftShift(value uint8) Expr { + return field.leftShift(value) +} + +func (field Uint8) BitXor(value uint8) Expr { + return field.bitXor(value) +} + +func (field Uint8) BitAnd(value uint8) Expr { + return field.bitAnd(value) +} + +func (field Uint8) BitOr(value uint8) Expr { + return field.bitOr(value) +} + +func (field Uint8) BitFlip() Expr { + return field.bitFlip() +} + +func (field Uint8) toSlice(values ...uint8) []interface{} { + slice := make([]interface{}, len(values)) + for i, v := range values { + slice[i] = v + } + return slice +} + +type Uint16 Int + +func (field Uint16) Eq(value uint16) Expr { + return expr{expression: clause.Eq{Column: field.Col, Value: value}} +} + +func (field Uint16) Neq(value uint16) Expr { + return expr{expression: clause.Neq{Column: field.Col, Value: value}} +} + +func (field Uint16) Gt(value uint16) Expr { + return expr{expression: clause.Gt{Column: field.Col, Value: value}} +} + +func (field Uint16) Gte(value uint16) Expr { + return expr{expression: clause.Gte{Column: field.Col, Value: value}} +} + +func (field Uint16) Lt(value uint16) Expr { + return expr{expression: clause.Lt{Column: field.Col, Value: value}} +} + +func (field Uint16) Lte(value uint16) Expr { + return expr{expression: clause.Lte{Column: field.Col, Value: value}} +} + +func (field Uint16) In(values ...uint16) Expr { + return expr{expression: clause.IN{Column: field.Col, Values: field.toSlice(values...)}} +} + +func (field Uint16) NotIn(values ...uint16) Expr { + return expr{expression: clause.Not(field.In(values...))} +} + +func (field Uint16) Between(left uint16, right uint16) Expr { + return field.between([]interface{}{left, right}) +} + +func (field Uint16) NotBetween(left uint16, right uint16) Expr { + return Not(field.Between(left, right)) +} + +func (field Uint16) Like(value uint16) Expr { + return expr{expression: clause.Like{Column: field.Col, Value: value}} +} + +func (field Uint16) NotLike(value uint16) Expr { + return expr{expression: clause.Not(field.Like(value))} +} + +func (field Uint16) Add(value uint16) Expr { + return field.add(value) +} + +func (field Uint16) Sub(value uint16) Expr { + return field.sub(value) +} + +func (field Uint16) Mul(value uint16) Expr { + return field.mul(value) +} + +func (field Uint16) Div(value uint16) Expr { + return field.div(value) +} + +func (field Uint16) Mod(value uint16) Expr { + return field.mod(value) +} + +func (field Uint16) FloorDiv(value uint16) Expr { + return field.floorDiv(value) +} + +func (field Uint16) RightShift(value uint16) Expr { + return field.rightShift(value) +} + +func (field Uint16) LeftShift(value uint16) Expr { + return field.leftShift(value) +} + +func (field Uint16) BitXor(value uint16) Expr { + return field.bitXor(value) +} + +func (field Uint16) BitAnd(value uint16) Expr { + return field.bitAnd(value) +} + +func (field Uint16) BitOr(value uint16) Expr { + return field.bitOr(value) +} + +func (field Uint16) BitFlip() Expr { + return field.bitFlip() +} + +func (field Uint16) toSlice(values ...uint16) []interface{} { + slice := make([]interface{}, len(values)) + for i, v := range values { + slice[i] = v + } + return slice +} + +type Uint32 Int + +func (field Uint32) Eq(value uint32) Expr { + return expr{expression: clause.Eq{Column: field.Col, Value: value}} +} + +func (field Uint32) Neq(value uint32) Expr { + return expr{expression: clause.Neq{Column: field.Col, Value: value}} +} + +func (field Uint32) Gt(value uint32) Expr { + return expr{expression: clause.Gt{Column: field.Col, Value: value}} +} + +func (field Uint32) Gte(value uint32) Expr { + return expr{expression: clause.Gte{Column: field.Col, Value: value}} +} + +func (field Uint32) Lt(value uint32) Expr { + return expr{expression: clause.Lt{Column: field.Col, Value: value}} +} + +func (field Uint32) Lte(value uint32) Expr { + return expr{expression: clause.Lte{Column: field.Col, Value: value}} +} + +func (field Uint32) In(values ...uint32) Expr { + return expr{expression: clause.IN{Column: field.Col, Values: field.toSlice(values...)}} +} + +func (field Uint32) NotIn(values ...uint32) Expr { + return expr{expression: clause.Not(field.In(values...))} +} + +func (field Uint32) Between(left uint32, right uint32) Expr { + return field.between([]interface{}{left, right}) +} + +func (field Uint32) NotBetween(left uint32, right uint32) Expr { + return Not(field.Between(left, right)) +} + +func (field Uint32) Like(value uint32) Expr { + return expr{expression: clause.Like{Column: field.Col, Value: value}} +} + +func (field Uint32) NotLike(value uint32) Expr { + return expr{expression: clause.Not(field.Like(value))} +} + +func (field Uint32) Add(value uint32) Expr { + return field.add(value) +} + +func (field Uint32) Sub(value uint32) Expr { + return field.sub(value) +} + +func (field Uint32) Mul(value uint32) Expr { + return field.mul(value) +} + +func (field Uint32) Div(value uint32) Expr { + return field.div(value) +} + +func (field Uint32) Mod(value uint32) Expr { + return field.mod(value) +} + +func (field Uint32) FloorDiv(value uint32) Expr { + return field.floorDiv(value) +} + +func (field Uint32) RightShift(value uint32) Expr { + return field.rightShift(value) +} + +func (field Uint32) LeftShift(value uint32) Expr { + return field.leftShift(value) +} + +func (field Uint32) BitXor(value uint32) Expr { + return field.bitXor(value) +} + +func (field Uint32) BitAnd(value uint32) Expr { + return field.bitAnd(value) +} + +func (field Uint32) BitOr(value uint32) Expr { + return field.bitOr(value) +} + +func (field Uint32) BitFlip() Expr { + return field.bitFlip() +} + +func (field Uint32) toSlice(values ...uint32) []interface{} { + slice := make([]interface{}, len(values)) + for i, v := range values { + slice[i] = v + } + return slice +} + +type Uint64 Int + +func (field Uint64) Eq(value uint64) Expr { + return expr{expression: clause.Eq{Column: field.Col, Value: value}} +} + +func (field Uint64) Neq(value uint64) Expr { + return expr{expression: clause.Neq{Column: field.Col, Value: value}} +} + +func (field Uint64) Gt(value uint64) Expr { + return expr{expression: clause.Gt{Column: field.Col, Value: value}} +} + +func (field Uint64) Gte(value uint64) Expr { + return expr{expression: clause.Gte{Column: field.Col, Value: value}} +} + +func (field Uint64) Lt(value uint64) Expr { + return expr{expression: clause.Lt{Column: field.Col, Value: value}} +} + +func (field Uint64) Lte(value uint64) Expr { + return expr{expression: clause.Lte{Column: field.Col, Value: value}} +} + +func (field Uint64) In(values ...uint64) Expr { + return expr{expression: clause.IN{Column: field.Col, Values: field.toSlice(values...)}} +} + +func (field Uint64) NotIn(values ...uint64) Expr { + return expr{expression: clause.Not(field.In(values...))} +} + +func (field Uint64) Between(left uint64, right uint64) Expr { + return field.between([]interface{}{left, right}) +} + +func (field Uint64) NotBetween(left uint64, right uint64) Expr { + return Not(field.Between(left, right)) +} + +func (field Uint64) Like(value uint64) Expr { + return expr{expression: clause.Like{Column: field.Col, Value: value}} +} + +func (field Uint64) NotLike(value uint64) Expr { + return expr{expression: clause.Not(field.Like(value))} +} + +func (field Uint64) Add(value uint64) Expr { + return field.add(value) +} + +func (field Uint64) Sub(value uint64) Expr { + return field.sub(value) +} + +func (field Uint64) Mul(value uint64) Expr { + return field.mul(value) +} + +func (field Uint64) Div(value uint64) Expr { + return field.div(value) +} + +func (field Uint64) Mod(value uint64) Expr { + return field.mod(value) +} + +func (field Uint64) FloorDiv(value uint64) Expr { + return field.floorDiv(value) +} + +func (field Uint64) RightShift(value uint64) Expr { + return field.rightShift(value) +} + +func (field Uint64) LeftShift(value uint64) Expr { + return field.leftShift(value) +} + +func (field Uint64) BitXor(value uint64) Expr { + return field.bitXor(value) +} + +func (field Uint64) BitAnd(value uint64) Expr { + return field.bitAnd(value) +} + +func (field Uint64) BitOr(value uint64) Expr { + return field.bitOr(value) +} + +func (field Uint64) BitFlip() Expr { + return field.bitFlip() +} + +func (field Uint64) toSlice(values ...uint64) []interface{} { + slice := make([]interface{}, len(values)) + for i, v := range values { + slice[i] = v + } + return slice +} diff --git a/field/string.go b/field/string.go new file mode 100644 index 00000000..a0e42fba --- /dev/null +++ b/field/string.go @@ -0,0 +1,137 @@ +package field + +import ( + "gorm.io/gorm/clause" +) + +type String Field + +func (field String) Eq(value string) Expr { + return expr{expression: clause.Eq{Column: field.Col, Value: value}} +} + +func (field String) Neq(value string) Expr { + return expr{expression: clause.Neq{Column: field.Col, Value: value}} +} + +func (field String) Gt(value string) Expr { + return expr{expression: clause.Gt{Column: field.Col, Value: value}} +} + +func (field String) Gte(value string) Expr { + return expr{expression: clause.Gte{Column: field.Col, Value: value}} +} + +func (field String) Lt(value string) Expr { + return expr{expression: clause.Lt{Column: field.Col, Value: value}} +} + +func (field String) Lte(value string) Expr { + return expr{expression: clause.Lte{Column: field.Col, Value: value}} +} + +func (field String) Between(left string, right string) Expr { + return field.between([]interface{}{left, right}) +} + +func (field String) NotBetween(left string, right string) Expr { + return Not(field.Between(left, right)) +} + +func (field String) In(values ...string) Expr { + return expr{expression: clause.IN{Column: field.Col, Values: field.toSlice(values)}} +} + +func (field String) NotIn(values ...string) Expr { + return expr{expression: clause.Not(field.In(values...))} +} + +func (field String) Like(value string) Expr { + return expr{expression: clause.Like{Column: field.Col, Value: value}} +} + +func (field String) NotLike(value string) Expr { + return expr{expression: clause.Not(field.Like(value))} +} + +func (field String) Regexp(value string) Expr { + return field.regexp(value) +} + +func (field String) NotRegxp(value string) Expr { + return Not(field.Regexp(value)) +} + +func (field String) toSlice(values []string) []interface{} { + slice := make([]interface{}, len(values)) + for i, v := range values { + slice[i] = v + } + return slice +} + +type Bytes String + +func (field Bytes) Eq(value []byte) Expr { + return expr{expression: clause.Eq{Column: field.Col, Value: value}} +} + +func (field Bytes) Neq(value []byte) Expr { + return expr{expression: clause.Neq{Column: field.Col, Value: value}} +} + +func (field Bytes) Gt(value []byte) Expr { + return expr{expression: clause.Gt{Column: field.Col, Value: value}} +} + +func (field Bytes) Gte(value []byte) Expr { + return expr{expression: clause.Gte{Column: field.Col, Value: value}} +} + +func (field Bytes) Lt(value []byte) Expr { + return expr{expression: clause.Lt{Column: field.Col, Value: value}} +} + +func (field Bytes) Lte(value []byte) Expr { + return expr{expression: clause.Lte{Column: field.Col, Value: value}} +} + +func (field Bytes) Between(left []byte, right []byte) Expr { + return field.between([]interface{}{left, right}) +} + +func (field Bytes) NotBetween(left []byte, right []byte) Expr { + return Not(field.Between(left, right)) +} + +func (field Bytes) In(values ...[]byte) Expr { + return expr{expression: clause.IN{Column: field.Col, Values: field.toSlice(values)}} +} + +func (field Bytes) NotIn(values ...[]byte) Expr { + return expr{expression: clause.Not(field.In(values...))} +} + +func (field Bytes) Like(value string) Expr { + return expr{expression: clause.Like{Column: field.Col, Value: value}} +} + +func (field Bytes) NotLike(value string) Expr { + return expr{expression: clause.Not(field.Like(value))} +} + +func (field Bytes) Regexp(value string) Expr { + return field.regexp(value) +} + +func (field Bytes) NotRegxp(value string) Expr { + return Not(field.Regexp(value)) +} + +func (field Bytes) toSlice(values [][]byte) []interface{} { + slice := make([]interface{}, len(values)) + for i, v := range values { + slice[i] = v + } + return slice +} diff --git a/field/time.go b/field/time.go new file mode 100644 index 00000000..b37c3206 --- /dev/null +++ b/field/time.go @@ -0,0 +1,65 @@ +package field + +import ( + "time" + + "gorm.io/gorm/clause" +) + +type Time Field + +func (field Time) Eq(value time.Time) Expr { + return expr{expression: clause.Eq{Column: field.Col, Value: value}} +} + +func (field Time) Neq(value time.Time) Expr { + return expr{expression: clause.Neq{Column: field.Col, Value: value}} +} + +func (field Time) Gt(value time.Time) Expr { + return expr{expression: clause.Gt{Column: field.Col, Value: value}} +} + +func (field Time) Gte(value time.Time) Expr { + return expr{expression: clause.Gte{Column: field.Col, Value: value}} +} + +func (field Time) Lt(value time.Time) Expr { + return expr{expression: clause.Lt{Column: field.Col, Value: value}} +} + +func (field Time) Lte(value time.Time) Expr { + return expr{expression: clause.Lte{Column: field.Col, Value: value}} +} + +func (field Time) Between(left time.Time, right time.Time) Expr { + return field.between([]interface{}{left, right}) +} + +func (field Time) NotBetween(left time.Time, right time.Time) Expr { + return Not(field.Between(left, right)) +} + +func (field Time) In(values ...time.Time) Expr { + return expr{expression: clause.IN{Column: field.Col, Values: field.toSlice(values...)}} +} + +func (field Time) NotIn(values ...time.Time) Expr { + return expr{expression: clause.Not(field.In(values...))} +} + +func (field Time) Add(value time.Duration) Expr { + return field.add(value) +} + +func (field Time) Sub(value time.Duration) Expr { + return field.sub(value) +} + +func (field Time) toSlice(values ...time.Time) []interface{} { + slice := make([]interface{}, len(values)) + for i, v := range values { + slice[i] = v + } + return slice +} diff --git a/go.mod b/go.mod new file mode 100644 index 00000000..816a2dfc --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module gorm.io/gen + +go 1.16 + +require gorm.io/gorm v1.21.12 diff --git a/go.sum b/go.sum new file mode 100644 index 00000000..3904ccf5 --- /dev/null +++ b/go.sum @@ -0,0 +1,6 @@ +github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= +github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= +github.com/jinzhu/now v1.1.2 h1:eVKgfIdy9b6zbWBMgFpfDPoAMifwSZagU9HmEU6zgiI= +github.com/jinzhu/now v1.1.2/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= +gorm.io/gorm v1.21.12 h1:3fQM0Eiz7jcJEhPggHEpoYnsGZqynMzverL77DV40RM= +gorm.io/gorm v1.21.12/go.mod h1:F+OptMscr0P2F2qU97WT1WimdH9GaQPoDW7AYd5i2Y0=