From c63a7453f39d3cb223d8dd6d6e9912833c58d977 Mon Sep 17 00:00:00 2001 From: piyongcai Date: Wed, 12 Jan 2022 12:04:08 +0800 Subject: [PATCH 1/6] time.Time, []byte type add alias support --- schema/field.go | 2 +- schema/field_test.go | 37 ++++++++++++++++++++++--------------- statement.go | 3 +++ 3 files changed, 26 insertions(+), 16 deletions(-) diff --git a/schema/field.go b/schema/field.go index d4f879c5..3505f3dd 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 2cf2d083..8fa46b87 100644 --- a/schema/field_test.go +++ b/schema/field_test.go @@ -262,21 +262,24 @@ func TestParseFieldWithPermission(t *testing.T) { } type ( - ID int64 - INT int - INT8 int8 - INT16 int16 - INT32 int32 - INT64 int64 - UINT uint - UINT8 uint8 - UINT16 uint16 - UINT32 uint32 - UINT64 uint64 - FLOAT32 float32 - FLOAT64 float64 - BOOL bool - STRING string + ID int64 + INT int + INT8 int8 + INT16 int16 + INT32 int32 + INT64 int64 + UINT uint + UINT8 uint8 + UINT16 uint16 + UINT32 uint32 + UINT64 uint64 + FLOAT32 float32 + FLOAT64 float64 + BOOL bool + STRING string + TIME time.Time + BYTES []byte + TypeAlias struct { ID INT `gorm:"column:fint"` @@ -293,6 +296,8 @@ type ( FLOAT64 `gorm:"column:ffloat64"` BOOL `gorm:"column:fbool"` 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: "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 f69339d4..146722a9 100644 --- a/statement.go +++ b/statement.go @@ -232,6 +232,9 @@ func (stmt *Statement) AddVar(writer clause.Writer, vars ...interface{}) { case reflect.Slice, reflect.Array: if rv.Len() == 0 { 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 { writer.WriteByte('(') for i := 0; i < rv.Len(); i++ { From e5be7ba778eef568d2b8da31f72255abf32b7d3f Mon Sep 17 00:00:00 2001 From: piyongcai Date: Wed, 12 Jan 2022 12:08:05 +0800 Subject: [PATCH 2/6] reformat --- schema/field.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/schema/field.go b/schema/field.go index 3505f3dd..485bbdf3 100644 --- a/schema/field.go +++ b/schema/field.go @@ -346,7 +346,8 @@ func (schema *Schema) ParseField(fieldStruct reflect.StructField) *Field { } } - if _, ok := field.TagSettings["EMBEDDED"]; field.GORMDataType != Time && field.GORMDataType != Bytes && (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: From 0e25da906f257405c3ae0008e05a11610ba56870 Mon Sep 17 00:00:00 2001 From: piyongcai Date: Sat, 22 Jan 2022 21:57:15 +0800 Subject: [PATCH 3/6] finisher_app.go Add ExecRaw Method. it use Do no processing and execute the original sql --- finisher_api.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/finisher_api.go b/finisher_api.go index 355d89bd..bbb1ba4d 100644 --- a/finisher_api.go +++ b/finisher_api.go @@ -662,3 +662,16 @@ func (db *DB) Exec(sql string, values ...interface{}) (tx *DB) { return tx.callbacks.Raw().Execute(tx) } + +// ExecRaw Do no processing and execute the original sql +func (db *DB) ExecRaw(sql string, values ...interface{}) (tx *DB) { + tx = db.getInstance() + + builder := strings.Builder{} + builder.WriteString(sql) + + tx.Statement.SQL = builder + tx.Statement.Vars = values + + return tx.callbacks.Raw().Execute(tx) +} From 2c13ea008e0d44887d973c1ffab64cec5786a2c2 Mon Sep 17 00:00:00 2001 From: piyongcai Date: Fri, 13 May 2022 17:35:11 +0800 Subject: [PATCH 4/6] =?UTF-8?q?1.Add=20UniqueIndex=20field=20to=20Field's?= =?UTF-8?q?=20struct=E3=80=82=202.Resolve=20UniqueIndex=20value=20from=20S?= =?UTF-8?q?tructField.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- schema/field.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/schema/field.go b/schema/field.go index d6df6596..7d8704f6 100644 --- a/schema/field.go +++ b/schema/field.go @@ -68,6 +68,7 @@ type Field struct { DefaultValue string DefaultValueInterface interface{} NotNull bool + UniqueIndex bool Unique bool Comment string Size int @@ -114,6 +115,7 @@ func (schema *Schema) ParseField(fieldStruct reflect.StructField) *Field { HasDefaultValue: utils.CheckTruth(tagSetting["AUTOINCREMENT"]), NotNull: utils.CheckTruth(tagSetting["NOT NULL"], tagSetting["NOTNULL"]), Unique: utils.CheckTruth(tagSetting["UNIQUE"]), + UniqueIndex: utils.CheckTruth(tagSetting["UNIQUEINDEX"]), Comment: tagSetting["COMMENT"], AutoIncrementIncrement: 1, } From 286e4ea884fc31a1a7ee67d2f6bd721392ce370d Mon Sep 17 00:00:00 2001 From: piyongcai Date: Mon, 16 May 2022 08:57:10 +0800 Subject: [PATCH 5/6] migrator.go -> MigrateColumn. // check unique, if column has "uniqueIndex" tag, it's unique = true. --- migrator/migrator.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/migrator/migrator.go b/migrator/migrator.go index d4989410..afc3e44b 100644 --- a/migrator/migrator.go +++ b/migrator/migrator.go @@ -440,7 +440,7 @@ func (m Migrator) MigrateColumn(value interface{}, field *schema.Field, columnTy } // check unique - if unique, ok := columnType.Unique(); ok && unique != field.Unique { + if unique, ok := columnType.Unique(); ok && !(unique == field.Unique || field.UniqueIndex == unique) { // not primary key if !field.PrimaryKey { alterColumn = true From 51abb15f12885218161e2bd7ffb9c2b23c63ec0e Mon Sep 17 00:00:00 2001 From: piyongcai Date: Mon, 16 May 2022 09:42:14 +0800 Subject: [PATCH 6/6] reset finisher_api.go,remove ExecRaw method --- finisher_api.go | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/finisher_api.go b/finisher_api.go index 89a7e296..663d532b 100644 --- a/finisher_api.go +++ b/finisher_api.go @@ -696,16 +696,3 @@ func (db *DB) Exec(sql string, values ...interface{}) (tx *DB) { return tx.callbacks.Raw().Execute(tx) } - -// ExecRaw Do no processing and execute the original sql -func (db *DB) ExecRaw(sql string, values ...interface{}) (tx *DB) { - tx = db.getInstance() - - builder := strings.Builder{} - builder.WriteString(sql) - - tx.Statement.SQL = builder - tx.Statement.Vars = values - - return tx.callbacks.Raw().Execute(tx) -}