From 79864af9ffee6e12051f6bbdfaab31df77f3bc61 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Wed, 30 Dec 2020 11:16:40 +0800 Subject: [PATCH] Allow customize auto increment increment --- callbacks/create.go | 4 +- schema/field.go | 92 ++++++++++++++++++++++++--------------------- 2 files changed, 51 insertions(+), 45 deletions(-) diff --git a/callbacks/create.go b/callbacks/create.go index 052f3344..9166eb67 100644 --- a/callbacks/create.go +++ b/callbacks/create.go @@ -71,7 +71,7 @@ func Create(config *Config) func(db *gorm.DB) { _, isZero := db.Statement.Schema.PrioritizedPrimaryField.ValueOf(rv) if isZero { db.Statement.Schema.PrioritizedPrimaryField.Set(rv, insertID) - insertID-- + insertID -= db.Statement.Schema.PrioritizedPrimaryField.AutoIncrementIncrement } } } else { @@ -83,7 +83,7 @@ func Create(config *Config) func(db *gorm.DB) { if _, isZero := db.Statement.Schema.PrioritizedPrimaryField.ValueOf(rv); isZero { db.Statement.Schema.PrioritizedPrimaryField.Set(rv, insertID) - insertID++ + insertID += db.Statement.Schema.PrioritizedPrimaryField.AutoIncrementIncrement } } } diff --git a/schema/field.go b/schema/field.go index 86b4a061..17cc6c43 100644 --- a/schema/field.go +++ b/schema/field.go @@ -37,55 +37,57 @@ const ( ) type Field struct { - Name string - DBName string - BindNames []string - DataType DataType - GORMDataType DataType - PrimaryKey bool - AutoIncrement bool - Creatable bool - Updatable bool - Readable bool - HasDefaultValue bool - AutoCreateTime TimeType - AutoUpdateTime TimeType - DefaultValue string - DefaultValueInterface interface{} - NotNull bool - Unique bool - Comment string - Size int - Precision int - Scale int - FieldType reflect.Type - IndirectFieldType reflect.Type - StructField reflect.StructField - Tag reflect.StructTag - TagSettings map[string]string - Schema *Schema - EmbeddedSchema *Schema - OwnerSchema *Schema - ReflectValueOf func(reflect.Value) reflect.Value - ValueOf func(reflect.Value) (value interface{}, zero bool) - Set func(reflect.Value, interface{}) error + Name string + DBName string + BindNames []string + DataType DataType + GORMDataType DataType + PrimaryKey bool + AutoIncrement bool + AutoIncrementIncrement int64 + Creatable bool + Updatable bool + Readable bool + HasDefaultValue bool + AutoCreateTime TimeType + AutoUpdateTime TimeType + DefaultValue string + DefaultValueInterface interface{} + NotNull bool + Unique bool + Comment string + Size int + Precision int + Scale int + FieldType reflect.Type + IndirectFieldType reflect.Type + StructField reflect.StructField + Tag reflect.StructTag + TagSettings map[string]string + Schema *Schema + EmbeddedSchema *Schema + OwnerSchema *Schema + ReflectValueOf func(reflect.Value) reflect.Value + ValueOf func(reflect.Value) (value interface{}, zero bool) + Set func(reflect.Value, interface{}) error } func (schema *Schema) ParseField(fieldStruct reflect.StructField) *Field { var err error field := &Field{ - Name: fieldStruct.Name, - BindNames: []string{fieldStruct.Name}, - FieldType: fieldStruct.Type, - IndirectFieldType: fieldStruct.Type, - StructField: fieldStruct, - Creatable: true, - Updatable: true, - Readable: true, - Tag: fieldStruct.Tag, - TagSettings: ParseTagSetting(fieldStruct.Tag.Get("gorm"), ";"), - Schema: schema, + Name: fieldStruct.Name, + BindNames: []string{fieldStruct.Name}, + FieldType: fieldStruct.Type, + IndirectFieldType: fieldStruct.Type, + StructField: fieldStruct, + Creatable: true, + Updatable: true, + Readable: true, + Tag: fieldStruct.Tag, + TagSettings: ParseTagSetting(fieldStruct.Tag.Get("gorm"), ";"), + Schema: schema, + AutoIncrementIncrement: 1, } for field.IndirectFieldType.Kind() == reflect.Ptr { @@ -149,6 +151,10 @@ func (schema *Schema) ParseField(fieldStruct reflect.StructField) *Field { field.HasDefaultValue = true } + if num, ok := field.TagSettings["AUTOINCREMENTINCREMENT"]; ok { + field.AutoIncrementIncrement, _ = strconv.ParseInt(num, 10, 64) + } + if v, ok := field.TagSettings["DEFAULT"]; ok { field.HasDefaultValue = true field.DefaultValue = v