Skip to content

Commit

Permalink
feat(field): implement IFNULL
Browse files Browse the repository at this point in the history
  • Loading branch information
tr1v3r committed Nov 19, 2021
1 parent f0a9c26 commit 5b99e9c
Show file tree
Hide file tree
Showing 6 changed files with 68 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 @@ -350,3 +350,7 @@ func (e expr) xor(value interface{}) expr {
func (e expr) isPure() bool {
return e.e == nil
}

func (e expr) ifNull(value interface{}) Expr {
return e.setE(clause.Expr{SQL: "IFNULL(?,?)", Vars: []interface{}{e.RawExpr(), value}})
}
4 changes: 4 additions & 0 deletions field/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ func (field Field) Value(value ScanValuer) AssignExpr {
return field.value(value)
}

func (field Field) IfNull(value ScanValuer) Expr {
return field.ifNull(value)
}

func (field Field) toSlice(values ...ScanValuer) []interface{} {
slice := make([]interface{}, len(values))
for i, v := range values {
Expand Down
8 changes: 8 additions & 0 deletions field/float.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ func (field Float64) Zero() AssignExpr {
return field.value(0)
}

func (field Float64) IfNull(value float64) Expr {
return field.ifNull(value)
}

func (field Float64) toSlice(values ...float64) []interface{} {
slice := make([]interface{}, len(values))
for i, v := range values {
Expand Down Expand Up @@ -174,6 +178,10 @@ func (field Float32) Zero() AssignExpr {
return field.value(0)
}

func (field Float32) IfNull(value float32) Expr {
return field.ifNull(value)
}

func (field Float32) toSlice(values ...float32) []interface{} {
slice := make([]interface{}, len(values))
for i, v := range values {
Expand Down
40 changes: 40 additions & 0 deletions field/int.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ func (field Int) Zero() AssignExpr {
return field.value(0)
}

func (field Int) IfNull(value int) Expr {
return field.ifNull(value)
}

func (field Int) toSlice(values ...int) []interface{} {
slice := make([]interface{}, len(values))
for i, v := range values {
Expand Down Expand Up @@ -224,6 +228,10 @@ func (field Int8) Zero() AssignExpr {
return field.value(0)
}

func (field Int8) IfNull(value int8) Expr {
return field.ifNull(value)
}

func (field Int8) toSlice(values ...int8) []interface{} {
slice := make([]interface{}, len(values))
for i, v := range values {
Expand Down Expand Up @@ -338,6 +346,10 @@ func (field Int16) Zero() AssignExpr {
return field.value(0)
}

func (field Int16) IfNull(value int16) Expr {
return field.ifNull(value)
}

func (field Int16) toSlice(values ...int16) []interface{} {
slice := make([]interface{}, len(values))
for i, v := range values {
Expand Down Expand Up @@ -452,6 +464,10 @@ func (field Int32) Zero() AssignExpr {
return field.value(0)
}

func (field Int32) IfNull(value int32) Expr {
return field.ifNull(value)
}

func (field Int32) toSlice(values ...int32) []interface{} {
slice := make([]interface{}, len(values))
for i, v := range values {
Expand Down Expand Up @@ -566,6 +582,10 @@ func (field Int64) Zero() AssignExpr {
return field.value(0)
}

func (field Int64) IfNull(value int64) Expr {
return field.ifNull(value)
}

func (field Int64) toSlice(values ...int64) []interface{} {
slice := make([]interface{}, len(values))
for i, v := range values {
Expand Down Expand Up @@ -680,6 +700,10 @@ func (field Uint) Zero() AssignExpr {
return field.value(0)
}

func (field Uint) IfNull(value uint) Expr {
return field.ifNull(value)
}

func (field Uint) toSlice(values ...uint) []interface{} {
slice := make([]interface{}, len(values))
for i, v := range values {
Expand Down Expand Up @@ -794,6 +818,10 @@ func (field Uint8) Zero() AssignExpr {
return field.value(0)
}

func (field Uint8) IfNull(value uint8) Expr {
return field.ifNull(value)
}

func (field Uint8) toSlice(values ...uint8) []interface{} {
slice := make([]interface{}, len(values))
for i, v := range values {
Expand Down Expand Up @@ -908,6 +936,10 @@ func (field Uint16) Zero() AssignExpr {
return field.value(0)
}

func (field Uint16) IfNull(value uint16) Expr {
return field.ifNull(value)
}

func (field Uint16) toSlice(values ...uint16) []interface{} {
slice := make([]interface{}, len(values))
for i, v := range values {
Expand Down Expand Up @@ -1022,6 +1054,10 @@ func (field Uint32) Zero() AssignExpr {
return field.value(0)
}

func (field Uint32) IfNull(value uint32) Expr {
return field.ifNull(value)
}

func (field Uint32) toSlice(values ...uint32) []interface{} {
slice := make([]interface{}, len(values))
for i, v := range values {
Expand Down Expand Up @@ -1136,6 +1172,10 @@ func (field Uint64) Zero() AssignExpr {
return field.value(0)
}

func (field Uint64) IfNull(value uint64) Expr {
return field.ifNull(value)
}

func (field Uint64) toSlice(values ...uint64) []interface{} {
slice := make([]interface{}, len(values))
for i, v := range values {
Expand Down
8 changes: 8 additions & 0 deletions field/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ func (field String) Zero() AssignExpr {
return field.value("")
}

func (field String) IfNull(value string) Expr {
return field.ifNull(value)
}

// FindInSet FIND_IN_SET(field_name, input_string_list)
func (field String) FindInSet(targetList string) Expr {
return expr{e: clause.Expr{SQL: "FIND_IN_SET(?,?)", Vars: []interface{}{field.RawExpr(), targetList}}}
Expand Down Expand Up @@ -154,6 +158,10 @@ func (field Bytes) Zero() AssignExpr {
return field.value([]byte{})
}

func (field Bytes) IfNull(value []byte) Expr {
return field.ifNull(value)
}

// FindInSet FIND_IN_SET(field_name, input_string_list)
func (field Bytes) FindInSet(targetList string) Expr {
return expr{e: clause.Expr{SQL: "FIND_IN_SET(?,?)", Vars: []interface{}{field.RawExpr(), targetList}}}
Expand Down
4 changes: 4 additions & 0 deletions field/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ func (field Time) Zero() AssignExpr {
return field.value(time.Time{})
}

func (field Time) IfNull(value Time) Expr {
return field.ifNull(value)
}

func (field Time) toSlice(values ...time.Time) []interface{} {
slice := make([]interface{}, len(values))
for i, v := range values {
Expand Down

0 comments on commit 5b99e9c

Please sign in to comment.