Fix Join with slice IN, close #4133

This commit is contained in:
Jinzhu 2021-03-04 18:40:47 +08:00
parent 42999e9809
commit 90476fea7a
1 changed files with 34 additions and 4 deletions

View File

@ -78,9 +78,10 @@ type NamedExpr struct {
// Build build raw expression // Build build raw expression
func (expr NamedExpr) Build(builder Builder) { func (expr NamedExpr) Build(builder Builder) {
var ( var (
idx int idx int
inName bool inName bool
namedMap = make(map[string]interface{}, len(expr.Vars)) afterParenthesis bool
namedMap = make(map[string]interface{}, len(expr.Vars))
) )
for _, v := range expr.Vars { for _, v := range expr.Vars {
@ -131,13 +132,42 @@ func (expr NamedExpr) Build(builder Builder) {
inName = false inName = false
} }
afterParenthesis = false
builder.WriteByte(v) builder.WriteByte(v)
} else if v == '?' && len(expr.Vars) > idx { } else if v == '?' && len(expr.Vars) > idx {
builder.AddVar(builder, expr.Vars[idx]) if afterParenthesis {
if _, ok := expr.Vars[idx].(driver.Valuer); ok {
builder.AddVar(builder, expr.Vars[idx])
} else {
switch rv := reflect.ValueOf(expr.Vars[idx]); rv.Kind() {
case reflect.Slice, reflect.Array:
if rv.Len() == 0 {
builder.AddVar(builder, nil)
} else {
for i := 0; i < rv.Len(); i++ {
if i > 0 {
builder.WriteByte(',')
}
builder.AddVar(builder, rv.Index(i).Interface())
}
}
default:
builder.AddVar(builder, expr.Vars[idx])
}
}
} else {
builder.AddVar(builder, expr.Vars[idx])
}
idx++ idx++
} else if inName { } else if inName {
name = append(name, v) name = append(name, v)
} else { } else {
if v == '(' {
afterParenthesis = true
} else {
afterParenthesis = false
}
builder.WriteByte(v) builder.WriteByte(v)
} }
} }