return a clear error for set TEXT type as compare condition (#2062)
Fix #523 Reviewed-on: https://gitea.com/xorm/xorm/pulls/2062
This commit is contained in:
parent
2c064b6da6
commit
f7e9fb74ac
|
@ -30,7 +30,7 @@ func TestArrayField(t *testing.T) {
|
||||||
|
|
||||||
assert.NoError(t, testEngine.Sync(new(ArrayStruct)))
|
assert.NoError(t, testEngine.Sync(new(ArrayStruct)))
|
||||||
|
|
||||||
var as = ArrayStruct{
|
as := ArrayStruct{
|
||||||
Name: [20]byte{
|
Name: [20]byte{
|
||||||
96, 96, 96, 96, 96,
|
96, 96, 96, 96, 96,
|
||||||
96, 96, 96, 96, 96,
|
96, 96, 96, 96, 96,
|
||||||
|
@ -54,7 +54,7 @@ func TestArrayField(t *testing.T) {
|
||||||
assert.EqualValues(t, 1, len(arrs))
|
assert.EqualValues(t, 1, len(arrs))
|
||||||
assert.Equal(t, as.Name, arrs[0].Name)
|
assert.Equal(t, as.Name, arrs[0].Name)
|
||||||
|
|
||||||
var newName = [20]byte{
|
newName := [20]byte{
|
||||||
90, 96, 96, 96, 96,
|
90, 96, 96, 96, 96,
|
||||||
96, 96, 96, 96, 96,
|
96, 96, 96, 96, 96,
|
||||||
96, 96, 96, 96, 96,
|
96, 96, 96, 96, 96,
|
||||||
|
@ -252,9 +252,11 @@ func TestConversion(t *testing.T) {
|
||||||
assert.Nil(t, c1.Nullable2)
|
assert.Nil(t, c1.Nullable2)
|
||||||
}
|
}
|
||||||
|
|
||||||
type MyInt int
|
type (
|
||||||
type MyUInt uint
|
MyInt int
|
||||||
type MyFloat float64
|
MyUInt uint
|
||||||
|
MyFloat float64
|
||||||
|
)
|
||||||
|
|
||||||
type MyStruct struct {
|
type MyStruct struct {
|
||||||
Type MyInt
|
Type MyInt
|
||||||
|
@ -273,7 +275,7 @@ type MyStruct struct {
|
||||||
UIA32 []uint32
|
UIA32 []uint32
|
||||||
UIA64 []uint64
|
UIA64 []uint64
|
||||||
UI uint
|
UI uint
|
||||||
//C64 complex64
|
// C64 complex64
|
||||||
MSS map[string]string
|
MSS map[string]string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -304,6 +306,13 @@ func TestCustomType1(t *testing.T) {
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.EqualValues(t, 1, cnt)
|
assert.EqualValues(t, 1, cnt)
|
||||||
|
|
||||||
|
// since mssql don't support use text as index condition, we have to ignore below
|
||||||
|
// get and find tests
|
||||||
|
if testEngine.Dialect().URI().DBType == schemas.MSSQL {
|
||||||
|
t.Skip()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
fmt.Println(i)
|
fmt.Println(i)
|
||||||
i.NameArray = []string{}
|
i.NameArray = []string{}
|
||||||
i.MSS = map[string]string{}
|
i.MSS = map[string]string{}
|
||||||
|
@ -598,7 +607,7 @@ func TestMyArray(t *testing.T) {
|
||||||
assert.NoError(t, PrepareEngine())
|
assert.NoError(t, PrepareEngine())
|
||||||
assertSync(t, new(MyArrayStruct))
|
assertSync(t, new(MyArrayStruct))
|
||||||
|
|
||||||
var v = [20]byte{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
|
v := [20]byte{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
|
||||||
_, err := testEngine.Insert(&MyArrayStruct{
|
_, err := testEngine.Insert(&MyArrayStruct{
|
||||||
Content: v,
|
Content: v,
|
||||||
})
|
})
|
||||||
|
|
|
@ -304,7 +304,7 @@ func (statement *Statement) needTableName() bool {
|
||||||
|
|
||||||
func (statement *Statement) colName(col *schemas.Column, tableName string) string {
|
func (statement *Statement) colName(col *schemas.Column, tableName string) string {
|
||||||
if statement.needTableName() {
|
if statement.needTableName() {
|
||||||
var nm = tableName
|
nm := tableName
|
||||||
if len(statement.TableAlias) > 0 {
|
if len(statement.TableAlias) > 0 {
|
||||||
nm = statement.TableAlias
|
nm = statement.TableAlias
|
||||||
}
|
}
|
||||||
|
@ -765,7 +765,7 @@ func (statement *Statement) asDBCond(fieldValue reflect.Value, fieldType reflect
|
||||||
if len(table.PrimaryKeys) == 1 {
|
if len(table.PrimaryKeys) == 1 {
|
||||||
pkField := reflect.Indirect(fieldValue).FieldByName(table.PKColumns()[0].FieldName)
|
pkField := reflect.Indirect(fieldValue).FieldByName(table.PKColumns()[0].FieldName)
|
||||||
// fix non-int pk issues
|
// fix non-int pk issues
|
||||||
//if pkField.Int() != 0 {
|
// if pkField.Int() != 0 {
|
||||||
if pkField.IsValid() && !utils.IsZero(pkField.Interface()) {
|
if pkField.IsValid() && !utils.IsZero(pkField.Interface()) {
|
||||||
return pkField.Interface(), true, nil
|
return pkField.Interface(), true, nil
|
||||||
}
|
}
|
||||||
|
@ -814,7 +814,8 @@ func (statement *Statement) asDBCond(fieldValue reflect.Value, fieldType reflect
|
||||||
func (statement *Statement) buildConds2(table *schemas.Table, bean interface{},
|
func (statement *Statement) buildConds2(table *schemas.Table, bean interface{},
|
||||||
includeVersion bool, includeUpdated bool, includeNil bool,
|
includeVersion bool, includeUpdated bool, includeNil bool,
|
||||||
includeAutoIncr bool, allUseBool bool, useAllCols bool, unscoped bool,
|
includeAutoIncr bool, allUseBool bool, useAllCols bool, unscoped bool,
|
||||||
mustColumnMap map[string]bool, tableName, aliasName string, addedTableName bool) (builder.Cond, error) {
|
mustColumnMap map[string]bool, tableName, aliasName string, addedTableName bool,
|
||||||
|
) (builder.Cond, error) {
|
||||||
var conds []builder.Cond
|
var conds []builder.Cond
|
||||||
for _, col := range table.Columns() {
|
for _, col := range table.Columns() {
|
||||||
if !includeVersion && col.IsVersion {
|
if !includeVersion && col.IsVersion {
|
||||||
|
@ -827,17 +828,13 @@ func (statement *Statement) buildConds2(table *schemas.Table, bean interface{},
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if statement.dialect.URI().DBType == schemas.MSSQL && (col.SQLType.Name == schemas.Text ||
|
|
||||||
col.SQLType.IsBlob() || col.SQLType.Name == schemas.TimeStampz) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if col.IsJSON {
|
if col.IsJSON {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
var colName string
|
var colName string
|
||||||
if addedTableName {
|
if addedTableName {
|
||||||
var nm = tableName
|
nm := tableName
|
||||||
if len(aliasName) > 0 {
|
if len(aliasName) > 0 {
|
||||||
nm = aliasName
|
nm = aliasName
|
||||||
}
|
}
|
||||||
|
@ -862,6 +859,15 @@ func (statement *Statement) buildConds2(table *schemas.Table, bean interface{},
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if statement.dialect.URI().DBType == schemas.MSSQL && (col.SQLType.Name == schemas.Text ||
|
||||||
|
col.SQLType.IsBlob() || col.SQLType.Name == schemas.TimeStampz) {
|
||||||
|
if utils.IsValueZero(fieldValue) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, fmt.Errorf("column %s is a TEXT type with data %#v which cannot be as compare condition", col.Name, fieldValue.Interface())
|
||||||
|
}
|
||||||
|
|
||||||
requiredField := useAllCols
|
requiredField := useAllCols
|
||||||
if b, ok := getFlagForColumn(mustColumnMap, col); ok {
|
if b, ok := getFlagForColumn(mustColumnMap, col); ok {
|
||||||
if b {
|
if b {
|
||||||
|
@ -910,7 +916,7 @@ func (statement *Statement) BuildConds(table *schemas.Table, bean interface{}, i
|
||||||
|
|
||||||
func (statement *Statement) mergeConds(bean interface{}) error {
|
func (statement *Statement) mergeConds(bean interface{}) error {
|
||||||
if !statement.NoAutoCondition && statement.RefTable != nil {
|
if !statement.NoAutoCondition && statement.RefTable != nil {
|
||||||
var addedTableName = (len(statement.JoinStr) > 0)
|
addedTableName := (len(statement.JoinStr) > 0)
|
||||||
autoCond, err := statement.BuildConds(statement.RefTable, bean, true, true, false, true, addedTableName)
|
autoCond, err := statement.BuildConds(statement.RefTable, bean, true, true, false, true, addedTableName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -948,7 +954,7 @@ func (statement *Statement) convertSQLOrArgs(sqlOrArgs ...interface{}) (string,
|
||||||
switch sqlOrArgs[0].(type) {
|
switch sqlOrArgs[0].(type) {
|
||||||
case string:
|
case string:
|
||||||
if len(sqlOrArgs) > 1 {
|
if len(sqlOrArgs) > 1 {
|
||||||
var newArgs = make([]interface{}, 0, len(sqlOrArgs)-1)
|
newArgs := make([]interface{}, 0, len(sqlOrArgs)-1)
|
||||||
for _, arg := range sqlOrArgs[1:] {
|
for _, arg := range sqlOrArgs[1:] {
|
||||||
if v, ok := arg.(time.Time); ok {
|
if v, ok := arg.(time.Time); ok {
|
||||||
newArgs = append(newArgs, v.In(statement.defaultTimeZone).Format("2006-01-02 15:04:05"))
|
newArgs = append(newArgs, v.In(statement.defaultTimeZone).Format("2006-01-02 15:04:05"))
|
||||||
|
@ -972,7 +978,7 @@ func (statement *Statement) convertSQLOrArgs(sqlOrArgs ...interface{}) (string,
|
||||||
}
|
}
|
||||||
|
|
||||||
func (statement *Statement) joinColumns(cols []*schemas.Column, includeTableName bool) string {
|
func (statement *Statement) joinColumns(cols []*schemas.Column, includeTableName bool) string {
|
||||||
var colnames = make([]string, len(cols))
|
colnames := make([]string, len(cols))
|
||||||
for i, col := range cols {
|
for i, col := range cols {
|
||||||
if includeTableName {
|
if includeTableName {
|
||||||
colnames[i] = statement.quote(statement.TableName()) +
|
colnames[i] = statement.quote(statement.TableName()) +
|
||||||
|
@ -986,7 +992,7 @@ func (statement *Statement) joinColumns(cols []*schemas.Column, includeTableName
|
||||||
|
|
||||||
// CondDeleted returns the conditions whether a record is soft deleted.
|
// CondDeleted returns the conditions whether a record is soft deleted.
|
||||||
func (statement *Statement) CondDeleted(col *schemas.Column) builder.Cond {
|
func (statement *Statement) CondDeleted(col *schemas.Column) builder.Cond {
|
||||||
var colName = statement.quote(col.Name)
|
colName := statement.quote(col.Name)
|
||||||
if statement.JoinStr != "" {
|
if statement.JoinStr != "" {
|
||||||
var prefix string
|
var prefix string
|
||||||
if statement.TableAlias != "" {
|
if statement.TableAlias != "" {
|
||||||
|
@ -996,7 +1002,7 @@ func (statement *Statement) CondDeleted(col *schemas.Column) builder.Cond {
|
||||||
}
|
}
|
||||||
colName = statement.quote(prefix) + "." + statement.quote(col.Name)
|
colName = statement.quote(prefix) + "." + statement.quote(col.Name)
|
||||||
}
|
}
|
||||||
var cond = builder.NewCond()
|
cond := builder.NewCond()
|
||||||
if col.SQLType.IsNumeric() {
|
if col.SQLType.IsNumeric() {
|
||||||
cond = builder.Eq{colName: 0}
|
cond = builder.Eq{colName: 0}
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in New Issue