Skip to content

Commit

Permalink
兼容field函数,方便实现自定义排序规则 (go-gorm#696)
Browse files Browse the repository at this point in the history
Co-authored-by: Jinzhu <[email protected]>
  • Loading branch information
wangle201210 and jinzhu authored Aug 2, 2023
1 parent 26a5b93 commit c4d0b25
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 0 deletions.
4 changes: 4 additions & 0 deletions field/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,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()}})
}
5 changes: 5 additions & 0 deletions field/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 10 additions & 0 deletions field/float.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
50 changes: 50 additions & 0 deletions field/int.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
10 changes: 10 additions & 0 deletions field/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ func (field String) Concat(before, after string) String {
}
}

// Filed ...
func (field String) Filed(values ...string) String {
return String{field.field(values)}
}

// SubstringIndex SUBSTRING_INDEX
// https://dev.mysql.com/doc/refman/8.0/en/functions.html#function_substring-index
func (field String) SubstringIndex(delim string, count int) String {
Expand Down Expand Up @@ -238,6 +243,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)}
}

// SubstringIndex SUBSTRING_INDEX
// https://dev.mysql.com/doc/refman/8.0/en/functions.html#function_substring-index
func (field Bytes) SubstringIndex(delim string, count int) Bytes {
Expand Down

0 comments on commit c4d0b25

Please sign in to comment.