mirror of https://github.com/go-gorm/gorm.git
Eq, Neq support slice of data
This commit is contained in:
parent
363f9b7863
commit
14e96080d8
|
@ -233,12 +233,25 @@ type Eq struct {
|
||||||
func (eq Eq) Build(builder Builder) {
|
func (eq Eq) Build(builder Builder) {
|
||||||
builder.WriteQuoted(eq.Column)
|
builder.WriteQuoted(eq.Column)
|
||||||
|
|
||||||
|
switch eq.Value.(type) {
|
||||||
|
case []string, []int, []int32, []int64, []uint, []uint32, []uint64, []interface{}:
|
||||||
|
builder.WriteString(" IN (")
|
||||||
|
rv := reflect.ValueOf(eq.Value)
|
||||||
|
for i := 0; i < rv.Len(); i++ {
|
||||||
|
if i > 0 {
|
||||||
|
builder.WriteByte(',')
|
||||||
|
}
|
||||||
|
builder.AddVar(builder, rv.Index(i))
|
||||||
|
}
|
||||||
|
builder.WriteByte(')')
|
||||||
|
default:
|
||||||
if eqNil(eq.Value) {
|
if eqNil(eq.Value) {
|
||||||
builder.WriteString(" IS NULL")
|
builder.WriteString(" IS NULL")
|
||||||
} else {
|
} else {
|
||||||
builder.WriteString(" = ")
|
builder.WriteString(" = ")
|
||||||
builder.AddVar(builder, eq.Value)
|
builder.AddVar(builder, eq.Value)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (eq Eq) NegationBuild(builder Builder) {
|
func (eq Eq) NegationBuild(builder Builder) {
|
||||||
|
@ -251,12 +264,25 @@ type Neq Eq
|
||||||
func (neq Neq) Build(builder Builder) {
|
func (neq Neq) Build(builder Builder) {
|
||||||
builder.WriteQuoted(neq.Column)
|
builder.WriteQuoted(neq.Column)
|
||||||
|
|
||||||
|
switch neq.Value.(type) {
|
||||||
|
case []string, []int, []int32, []int64, []uint, []uint32, []uint64, []interface{}:
|
||||||
|
builder.WriteString(" NOT IN (")
|
||||||
|
rv := reflect.ValueOf(neq.Value)
|
||||||
|
for i := 0; i < rv.Len(); i++ {
|
||||||
|
if i > 0 {
|
||||||
|
builder.WriteByte(',')
|
||||||
|
}
|
||||||
|
builder.AddVar(builder, rv.Index(i))
|
||||||
|
}
|
||||||
|
builder.WriteByte(')')
|
||||||
|
default:
|
||||||
if eqNil(neq.Value) {
|
if eqNil(neq.Value) {
|
||||||
builder.WriteString(" IS NOT NULL")
|
builder.WriteString(" IS NOT NULL")
|
||||||
} else {
|
} else {
|
||||||
builder.WriteString(" <> ")
|
builder.WriteString(" <> ")
|
||||||
builder.AddVar(builder, neq.Value)
|
builder.AddVar(builder, neq.Value)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (neq Neq) NegationBuild(builder Builder) {
|
func (neq Neq) NegationBuild(builder Builder) {
|
||||||
|
|
|
@ -136,6 +136,16 @@ func TestExpression(t *testing.T) {
|
||||||
clause.Neq{Column: column, Value: (interface{})(nil)},
|
clause.Neq{Column: column, Value: (interface{})(nil)},
|
||||||
},
|
},
|
||||||
Result: "`column-name` IS NOT NULL",
|
Result: "`column-name` IS NOT NULL",
|
||||||
|
}, {
|
||||||
|
Expressions: []clause.Expression{
|
||||||
|
clause.Eq{Column: column, Value: []string{"a", "b"}},
|
||||||
|
},
|
||||||
|
Result: "`column-name` IN (?,?)",
|
||||||
|
}, {
|
||||||
|
Expressions: []clause.Expression{
|
||||||
|
clause.Neq{Column: column, Value: []string{"a", "b"}},
|
||||||
|
},
|
||||||
|
Result: "`column-name` NOT IN (?,?)",
|
||||||
}}
|
}}
|
||||||
|
|
||||||
for idx, result := range results {
|
for idx, result := range results {
|
||||||
|
|
Loading…
Reference in New Issue