From 381f71be3e53e08d9c262b9a05ab0f9ce2ea67b9 Mon Sep 17 00:00:00 2001 From: wangle201210 <285273592@qq.com> Date: Fri, 28 Oct 2022 11:03:23 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=BC=E5=AE=B9field=E5=87=BD=E6=95=B0?= =?UTF-8?q?=EF=BC=8C=E6=96=B9=E4=BE=BF=E5=AE=9E=E7=8E=B0=E8=87=AA=E5=AE=9A?= =?UTF-8?q?=E4=B9=89=E6=8E=92=E5=BA=8F=E8=A7=84=E5=88=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- field/expr.go | 4 ++++ field/field.go | 5 +++++ field/float.go | 10 ++++++++++ field/int.go | 50 +++++++++++++++++++++++++++++++++++++++++++++++++ field/string.go | 10 ++++++++++ 5 files changed, 79 insertions(+) diff --git a/field/expr.go b/field/expr.go index 8e2747a4..0fe58ae7 100644 --- a/field/expr.go +++ b/field/expr.go @@ -383,6 +383,10 @@ func (e expr) ifNull(value interface{}) expr { return e.setE(clause.Expr{SQL: "IFNULL(?,?)", Vars: []interface{}{e.RawExpr(), value}}) } +func (e expr) field(value interface{}) expr { + return e.setE(clause.Expr{SQL: "FIELD(?, ?)", Vars: []interface{}{e.RawExpr(), value}, WithoutParentheses: true}) +} + func (e expr) sum() expr { return e.setE(clause.Expr{SQL: "SUM(?)", Vars: []interface{}{e.RawExpr()}}) } diff --git a/field/field.go b/field/field.go index 6b11efb5..023b335c 100644 --- a/field/field.go +++ b/field/field.go @@ -70,6 +70,11 @@ func (field Field) IfNull(value driver.Valuer) Expr { return field.ifNull(value) } +// Field ... +func (field Field) Field(value []interface{}) Expr { + return field.field(value) +} + func (field Field) toSlice(values ...driver.Valuer) []interface{} { slice := make([]interface{}, len(values)) for i, v := range values { diff --git a/field/float.go b/field/float.go index 34c8a3ab..d5152550 100644 --- a/field/float.go +++ b/field/float.go @@ -115,6 +115,11 @@ func (field Float64) IfNull(value float64) Expr { return field.ifNull(value) } +// Filed ... +func (field Float64) Filed(values ...float64) Float64 { + return Float64{field.field(values)} +} + func (field Float64) toSlice(values ...float64) []interface{} { slice := make([]interface{}, len(values)) for i, v := range values { @@ -236,6 +241,11 @@ func (field Float32) IfNull(value float32) Expr { return field.ifNull(value) } +// Filed ... +func (field Float32) Filed(values ...float32) Float32 { + return Float32{field.field(values)} +} + func (field Float32) toSlice(values ...float32) []interface{} { slice := make([]interface{}, len(values)) for i, v := range values { diff --git a/field/int.go b/field/int.go index 8ecf6a29..f4dad5e0 100644 --- a/field/int.go +++ b/field/int.go @@ -147,6 +147,11 @@ func (field Int) IfNull(value int) Expr { return field.ifNull(value) } +// Filed ... +func (field Int) Filed(values ...int) Int { + return Int{field.field(values)} +} + func (field Int) toSlice(values ...int) []interface{} { slice := make([]interface{}, len(values)) for i, v := range values { @@ -298,6 +303,11 @@ func (field Int8) IfNull(value int8) Expr { return field.ifNull(value) } +// Filed ... +func (field Int8) Filed(values ...int8) Int8 { + return Int8{field.field(values)} +} + func (field Int8) toSlice(values ...int8) []interface{} { slice := make([]interface{}, len(values)) for i, v := range values { @@ -449,6 +459,11 @@ func (field Int16) IfNull(value int16) Expr { return field.ifNull(value) } +// Filed ... +func (field Int16) Filed(values ...int16) Int16 { + return Int16{field.field(values)} +} + func (field Int16) toSlice(values ...int16) []interface{} { slice := make([]interface{}, len(values)) for i, v := range values { @@ -600,6 +615,11 @@ func (field Int32) IfNull(value int32) Expr { return field.ifNull(value) } +// Filed ... +func (field Int32) Filed(values ...int32) Int32 { + return Int32{field.field(values)} +} + func (field Int32) toSlice(values ...int32) []interface{} { slice := make([]interface{}, len(values)) for i, v := range values { @@ -751,6 +771,11 @@ func (field Int64) IfNull(value int64) Expr { return field.ifNull(value) } +// Filed ... +func (field Int64) Filed(values ...int64) Int64 { + return Int64{field.field(values)} +} + func (field Int64) toSlice(values ...int64) []interface{} { slice := make([]interface{}, len(values)) for i, v := range values { @@ -902,6 +927,11 @@ func (field Uint) IfNull(value uint) Expr { return field.ifNull(value) } +// Filed ... +func (field Uint) Filed(values ...uint) Uint { + return Uint{field.field(values)} +} + func (field Uint) toSlice(values ...uint) []interface{} { slice := make([]interface{}, len(values)) for i, v := range values { @@ -1053,6 +1083,11 @@ func (field Uint8) IfNull(value uint8) Expr { return field.ifNull(value) } +// Filed ... +func (field Uint8) Filed(values ...uint8) Uint8 { + return Uint8{field.field(values)} +} + func (field Uint8) toSlice(values ...uint8) []interface{} { slice := make([]interface{}, len(values)) for i, v := range values { @@ -1204,6 +1239,11 @@ func (field Uint16) IfNull(value uint16) Expr { return field.ifNull(value) } +// Filed ... +func (field Uint16) Filed(values ...uint16) Uint16 { + return Uint16{field.field(values)} +} + func (field Uint16) toSlice(values ...uint16) []interface{} { slice := make([]interface{}, len(values)) for i, v := range values { @@ -1355,6 +1395,11 @@ func (field Uint32) IfNull(value uint32) Expr { return field.ifNull(value) } +// Filed ... +func (field Uint32) Filed(values ...uint32) Uint32 { + return Uint32{field.field(values)} +} + func (field Uint32) toSlice(values ...uint32) []interface{} { slice := make([]interface{}, len(values)) for i, v := range values { @@ -1506,6 +1551,11 @@ func (field Uint64) IfNull(value uint64) Expr { return field.ifNull(value) } +// Filed ... +func (field Uint64) Filed(values ...uint64) Uint64 { + return Uint64{field.field(values)} +} + func (field Uint64) toSlice(values ...uint64) []interface{} { slice := make([]interface{}, len(values)) for i, v := range values { diff --git a/field/string.go b/field/string.go index e22ea8e7..9bbe2605 100644 --- a/field/string.go +++ b/field/string.go @@ -121,6 +121,11 @@ func (field String) Concat(before, after string) String { } } +// Filed ... +func (field String) Filed(values ...string) String { + return String{field.field(values)} +} + func (field String) toSlice(values []string) []interface{} { slice := make([]interface{}, len(values)) for i, v := range values { @@ -227,6 +232,11 @@ func (field Bytes) FindInSetWith(target string) Expr { return expr{e: clause.Expr{SQL: "FIND_IN_SET(?,?)", Vars: []interface{}{target, field.RawExpr()}}} } +// Filed ... +func (field Bytes) Filed(values ...[]byte) Bytes { + return Bytes{field.field(values)} +} + func (field Bytes) toSlice(values [][]byte) []interface{} { slice := make([]interface{}, len(values)) for i, v := range values {