actiontech / dms

3 stars 2 forks source link

fix: when values of in condition is empty, do not add () #320

Closed winfredLIN closed 3 weeks ago

winfredLIN commented 3 weeks ago

issue https://github.com/actiontech/dms/issues/318

改动

  1. 当in语句没有参数的时候,不拼接(),此时SQL会回退到 in ? 在这样的情况下,不会产生错误sql

影响面

在gorm代码中 gorm.io/gorm/statement.go: 235 添加变量时,如果切片或数组为空时,会添加 (NULL) 在上述语句中,表现为 in (NULL),则不会产生错误SQL

default:
            switch rv := reflect.ValueOf(v); rv.Kind() {
            case reflect.Slice, reflect.Array:
                if rv.Len() == 0 {
                    writer.WriteString("(NULL)")
                } else if rv.Type().Elem() == reflect.TypeOf(uint8(0)) {
                    stmt.Vars = append(stmt.Vars, v)
                    stmt.DB.Dialector.BindVarTo(writer, stmt, v)
                } else {
                    writer.WriteByte('(')
                    for i := 0; i < rv.Len(); i++ {
                        if i > 0 {
                            writer.WriteByte(',')
                        }
                        stmt.AddVar(writer, rv.Index(i).Interface())
                    }
                    writer.WriteByte(')')
                }