return a clear error for set TEXT type as compare condition #2062

Merged
lunny merged 2 commits from lunny/fix_get_mssql into master 2022-04-23 09:19:37 +00:00
2 changed files with 35 additions and 20 deletions

View File

@ -30,7 +30,7 @@ func TestArrayField(t *testing.T) {
assert.NoError(t, testEngine.Sync(new(ArrayStruct)))
var as = ArrayStruct{
as := ArrayStruct{
Name: [20]byte{
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.Equal(t, as.Name, arrs[0].Name)
var newName = [20]byte{
newName := [20]byte{
90, 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)
}
type MyInt int
type MyUInt uint
type MyFloat float64
type (
MyInt int
MyUInt uint
MyFloat float64
)
type MyStruct struct {
Type MyInt
@ -273,7 +275,7 @@ type MyStruct struct {
UIA32 []uint32
UIA64 []uint64
UI uint
//C64 complex64
// C64 complex64
MSS map[string]string
}
@ -304,6 +306,13 @@ func TestCustomType1(t *testing.T) {
assert.NoError(t, err)
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)
i.NameArray = []string{}
i.MSS = map[string]string{}
@ -598,7 +607,7 @@ func TestMyArray(t *testing.T) {
assert.NoError(t, PrepareEngine())
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{
Content: v,
})

View File

@ -304,7 +304,7 @@ func (statement *Statement) needTableName() bool {
func (statement *Statement) colName(col *schemas.Column, tableName string) string {
if statement.needTableName() {
var nm = tableName
nm := tableName
if len(statement.TableAlias) > 0 {
nm = statement.TableAlias
}
@ -765,7 +765,7 @@ func (statement *Statement) asDBCond(fieldValue reflect.Value, fieldType reflect
if len(table.PrimaryKeys) == 1 {
pkField := reflect.Indirect(fieldValue).FieldByName(table.PKColumns()[0].FieldName)
// fix non-int pk issues
//if pkField.Int() != 0 {
// if pkField.Int() != 0 {
if pkField.IsValid() && !utils.IsZero(pkField.Interface()) {
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{},
includeVersion bool, includeUpdated bool, includeNil 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
for _, col := range table.Columns() {
if !includeVersion && col.IsVersion {
@ -827,17 +828,13 @@ func (statement *Statement) buildConds2(table *schemas.Table, bean interface{},
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 {
continue
}
var colName string
if addedTableName {
var nm = tableName
nm := tableName
if len(aliasName) > 0 {
nm = aliasName
}
@ -862,6 +859,15 @@ func (statement *Statement) buildConds2(table *schemas.Table, bean interface{},
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
if b, ok := getFlagForColumn(mustColumnMap, col); ok {
if b {
@ -910,7 +916,7 @@ func (statement *Statement) BuildConds(table *schemas.Table, bean interface{}, i
func (statement *Statement) mergeConds(bean interface{}) error {
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)
if err != nil {
return err
@ -948,7 +954,7 @@ func (statement *Statement) convertSQLOrArgs(sqlOrArgs ...interface{}) (string,
switch sqlOrArgs[0].(type) {
case string:
if len(sqlOrArgs) > 1 {
var newArgs = make([]interface{}, 0, len(sqlOrArgs)-1)
newArgs := make([]interface{}, 0, len(sqlOrArgs)-1)
for _, arg := range sqlOrArgs[1:] {
if v, ok := arg.(time.Time); ok {
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 {
var colnames = make([]string, len(cols))
colnames := make([]string, len(cols))
for i, col := range cols {
if includeTableName {
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.
func (statement *Statement) CondDeleted(col *schemas.Column) builder.Cond {
var colName = statement.quote(col.Name)
colName := statement.quote(col.Name)
if statement.JoinStr != "" {
var prefix string
if statement.TableAlias != "" {
@ -996,7 +1002,7 @@ func (statement *Statement) CondDeleted(col *schemas.Column) builder.Cond {
}
colName = statement.quote(prefix) + "." + statement.quote(col.Name)
}
var cond = builder.NewCond()
cond := builder.NewCond()
if col.SQLType.IsNumeric() {
cond = builder.Eq{colName: 0}
} else {