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

2, []byte alias type, fixed statement bind var parameter.
This commit is contained in:
piyongcai 2021-12-27 11:08:50 +08:00
parent 03919f36ce
commit e5c333b96b
3 changed files with 11 additions and 2 deletions

View File

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

View File

@ -276,6 +276,8 @@ type FLOAT32 float32
type FLOAT64 float64 type FLOAT64 float64
type BOOL bool type BOOL bool
type STRING string type STRING string
type TIME time.Time
type BYTES []byte
type TypeAlias struct { type TypeAlias struct {
ID ID
INT `gorm:"column:fint"` INT `gorm:"column:fint"`
@ -292,6 +294,8 @@ type TypeAlias struct {
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"`
} }
func TestTypeAliasField(t *testing.T){ 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: "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

@ -230,7 +230,10 @@ func (stmt *Statement) AddVar(writer clause.Writer, vars ...interface{}) {
default: default:
switch rv := reflect.ValueOf(v); rv.Kind() { switch rv := reflect.ValueOf(v); rv.Kind() {
case reflect.Slice, reflect.Array: 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)") writer.WriteString("(NULL)")
} else { } else {
writer.WriteByte('(') writer.WriteByte('(')