From e5c333b96bae508a6194004ec99832fc7115537d Mon Sep 17 00:00:00 2001 From: piyongcai Date: Mon, 27 Dec 2021 11:08:50 +0800 Subject: [PATCH] 1, time.Time, []byte type add alias support. 2, []byte alias type, fixed statement bind var parameter. --- schema/field.go | 2 +- schema/field_test.go | 6 ++++++ statement.go | 5 ++++- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/schema/field.go b/schema/field.go index c6c89cc1..b40584d1 100644 --- a/schema/field.go +++ b/schema/field.go @@ -346,7 +346,7 @@ func (schema *Schema) ParseField(fieldStruct reflect.StructField) *Field { } } - if _, ok := field.TagSettings["EMBEDDED"]; ok || (fieldStruct.Anonymous && !isValuer && (field.Creatable || field.Updatable || field.Readable)) { + if _, ok := field.TagSettings["EMBEDDED"]; field.GORMDataType != Time && field.GORMDataType != Bytes && (ok || (fieldStruct.Anonymous && !isValuer && (field.Creatable || field.Updatable || field.Readable))) { kind := reflect.Indirect(fieldValue).Kind() switch kind { case reflect.Struct: diff --git a/schema/field_test.go b/schema/field_test.go index 8768a4c3..8a63a822 100644 --- a/schema/field_test.go +++ b/schema/field_test.go @@ -276,6 +276,8 @@ type FLOAT32 float32 type FLOAT64 float64 type BOOL bool type STRING string +type TIME time.Time +type BYTES []byte type TypeAlias struct { ID INT `gorm:"column:fint"` @@ -292,6 +294,8 @@ type TypeAlias struct { FLOAT64 `gorm:"column:ffloat64"` BOOL `gorm:"column:fbool"` STRING `gorm:"column:fstring"` + TIME `gorm:"column:ftime"` + BYTES `gorm:"column:fbytes"` } func TestTypeAliasField(t *testing.T){ @@ -316,6 +320,8 @@ func TestTypeAliasField(t *testing.T){ {Name: "FLOAT64", DBName: "ffloat64", BindNames: []string{"FLOAT64"}, DataType: schema.Float , Creatable: true, Updatable: true, Readable: true, Size: 64, Tag: `gorm:"column:ffloat64"`}, {Name: "BOOL", DBName: "fbool", BindNames: []string{"BOOL"}, DataType: schema.Bool , Creatable: true, Updatable: true, Readable: true, Tag: `gorm:"column:fbool"`}, {Name: "STRING", DBName: "fstring", BindNames: []string{"STRING"}, DataType: schema.String, Creatable: true, Updatable: true, Readable: true, Tag: `gorm:"column:fstring"`}, + {Name: "TIME", DBName: "ftime", BindNames: []string{"TIME"}, DataType: schema.Time, Creatable: true, Updatable: true, Readable: true, Tag: `gorm:"column:ftime"`}, + {Name: "BYTES", DBName: "fbytes", BindNames: []string{"BYTES"}, DataType: schema.Bytes, Creatable: true, Updatable: true, Readable: true, Tag: `gorm:"column:fbytes"`}, } for _, f := range fields { diff --git a/statement.go b/statement.go index 5a948d3f..4647050e 100644 --- a/statement.go +++ b/statement.go @@ -230,7 +230,10 @@ func (stmt *Statement) AddVar(writer clause.Writer, vars ...interface{}) { default: switch rv := reflect.ValueOf(v); rv.Kind() { case reflect.Slice, reflect.Array: - if rv.Len() == 0 { + if rv.Type().Elem() == reflect.TypeOf(uint8(0)) { + stmt.Vars = append(stmt.Vars, v) + stmt.DB.Dialector.BindVarTo(writer, stmt, v) + }else if rv.Len() == 0 { writer.WriteString("(NULL)") } else { writer.WriteByte('(')