This commit is contained in:
Lunny Xiao 2022-04-23 12:05:12 +08:00
parent 94ac175648
commit ecdccfe41d
No known key found for this signature in database
GPG Key ID: C3B7C91B632F738A
2 changed files with 27 additions and 17 deletions

View File

@ -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,
}) })

View File

@ -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 {
@ -833,7 +834,7 @@ func (statement *Statement) buildConds2(table *schemas.Table, bean interface{},
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
} }
@ -864,7 +865,7 @@ func (statement *Statement) buildConds2(table *schemas.Table, bean interface{},
continue continue
} }
return nil, fmt.Errorf("column %s is a TEXT type which cannot be as compare condition", col.Name) 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
@ -915,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
@ -953,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"))
@ -977,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()) +
@ -991,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 != "" {
@ -1001,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 {