time.Time, []byte type add alias support. (rebase master) (#4992)

* time.Time, []byte type add alias support

* reformat
This commit is contained in:
piyongcai 2022-01-12 13:11:40 +08:00 committed by GitHub
parent eae73624ad
commit a0d6ff1fea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 16 deletions

View File

@ -346,7 +346,8 @@ 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() kind := reflect.Indirect(fieldValue).Kind()
switch kind { switch kind {
case reflect.Struct: case reflect.Struct:

View File

@ -277,6 +277,9 @@ type (
FLOAT64 float64 FLOAT64 float64
BOOL bool BOOL bool
STRING string STRING string
TIME time.Time
BYTES []byte
TypeAlias struct { TypeAlias struct {
ID ID
INT `gorm:"column:fint"` INT `gorm:"column:fint"`
@ -293,6 +296,8 @@ type (
FLOAT64 `gorm:"column:ffloat64"` FLOAT64 `gorm:"column:ffloat64"`
BOOL `gorm:"column:fbool"` BOOL `gorm:"column:fbool"`
STRING `gorm:"column:fstring"` STRING `gorm:"column:fstring"`
TIME `gorm:"column:ftime"`
BYTES `gorm:"column:fbytes"`
} }
) )
@ -318,6 +323,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: "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: "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: "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 { for _, f := range fields {

View File

@ -232,6 +232,9 @@ func (stmt *Statement) AddVar(writer clause.Writer, vars ...interface{}) {
case reflect.Slice, reflect.Array: case reflect.Slice, reflect.Array:
if rv.Len() == 0 { if rv.Len() == 0 {
writer.WriteString("(NULL)") 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 { } else {
writer.WriteByte('(') writer.WriteByte('(')
for i := 0; i < rv.Len(); i++ { for i := 0; i < rv.Len(); i++ {