Allow customize auto increment increment

This commit is contained in:
Jinzhu 2020-12-30 11:16:40 +08:00
parent 6c0ee2700a
commit 79864af9ff
2 changed files with 51 additions and 45 deletions

View File

@ -71,7 +71,7 @@ func Create(config *Config) func(db *gorm.DB) {
_, isZero := db.Statement.Schema.PrioritizedPrimaryField.ValueOf(rv) _, isZero := db.Statement.Schema.PrioritizedPrimaryField.ValueOf(rv)
if isZero { if isZero {
db.Statement.Schema.PrioritizedPrimaryField.Set(rv, insertID) db.Statement.Schema.PrioritizedPrimaryField.Set(rv, insertID)
insertID-- insertID -= db.Statement.Schema.PrioritizedPrimaryField.AutoIncrementIncrement
} }
} }
} else { } else {
@ -83,7 +83,7 @@ func Create(config *Config) func(db *gorm.DB) {
if _, isZero := db.Statement.Schema.PrioritizedPrimaryField.ValueOf(rv); isZero { if _, isZero := db.Statement.Schema.PrioritizedPrimaryField.ValueOf(rv); isZero {
db.Statement.Schema.PrioritizedPrimaryField.Set(rv, insertID) db.Statement.Schema.PrioritizedPrimaryField.Set(rv, insertID)
insertID++ insertID += db.Statement.Schema.PrioritizedPrimaryField.AutoIncrementIncrement
} }
} }
} }

View File

@ -37,55 +37,57 @@ const (
) )
type Field struct { type Field struct {
Name string Name string
DBName string DBName string
BindNames []string BindNames []string
DataType DataType DataType DataType
GORMDataType DataType GORMDataType DataType
PrimaryKey bool PrimaryKey bool
AutoIncrement bool AutoIncrement bool
Creatable bool AutoIncrementIncrement int64
Updatable bool Creatable bool
Readable bool Updatable bool
HasDefaultValue bool Readable bool
AutoCreateTime TimeType HasDefaultValue bool
AutoUpdateTime TimeType AutoCreateTime TimeType
DefaultValue string AutoUpdateTime TimeType
DefaultValueInterface interface{} DefaultValue string
NotNull bool DefaultValueInterface interface{}
Unique bool NotNull bool
Comment string Unique bool
Size int Comment string
Precision int Size int
Scale int Precision int
FieldType reflect.Type Scale int
IndirectFieldType reflect.Type FieldType reflect.Type
StructField reflect.StructField IndirectFieldType reflect.Type
Tag reflect.StructTag StructField reflect.StructField
TagSettings map[string]string Tag reflect.StructTag
Schema *Schema TagSettings map[string]string
EmbeddedSchema *Schema Schema *Schema
OwnerSchema *Schema EmbeddedSchema *Schema
ReflectValueOf func(reflect.Value) reflect.Value OwnerSchema *Schema
ValueOf func(reflect.Value) (value interface{}, zero bool) ReflectValueOf func(reflect.Value) reflect.Value
Set func(reflect.Value, interface{}) error ValueOf func(reflect.Value) (value interface{}, zero bool)
Set func(reflect.Value, interface{}) error
} }
func (schema *Schema) ParseField(fieldStruct reflect.StructField) *Field { func (schema *Schema) ParseField(fieldStruct reflect.StructField) *Field {
var err error var err error
field := &Field{ field := &Field{
Name: fieldStruct.Name, Name: fieldStruct.Name,
BindNames: []string{fieldStruct.Name}, BindNames: []string{fieldStruct.Name},
FieldType: fieldStruct.Type, FieldType: fieldStruct.Type,
IndirectFieldType: fieldStruct.Type, IndirectFieldType: fieldStruct.Type,
StructField: fieldStruct, StructField: fieldStruct,
Creatable: true, Creatable: true,
Updatable: true, Updatable: true,
Readable: true, Readable: true,
Tag: fieldStruct.Tag, Tag: fieldStruct.Tag,
TagSettings: ParseTagSetting(fieldStruct.Tag.Get("gorm"), ";"), TagSettings: ParseTagSetting(fieldStruct.Tag.Get("gorm"), ";"),
Schema: schema, Schema: schema,
AutoIncrementIncrement: 1,
} }
for field.IndirectFieldType.Kind() == reflect.Ptr { for field.IndirectFieldType.Kind() == reflect.Ptr {
@ -149,6 +151,10 @@ func (schema *Schema) ParseField(fieldStruct reflect.StructField) *Field {
field.HasDefaultValue = true field.HasDefaultValue = true
} }
if num, ok := field.TagSettings["AUTOINCREMENTINCREMENT"]; ok {
field.AutoIncrementIncrement, _ = strconv.ParseInt(num, 10, 64)
}
if v, ok := field.TagSettings["DEFAULT"]; ok { if v, ok := field.TagSettings["DEFAULT"]; ok {
field.HasDefaultValue = true field.HasDefaultValue = true
field.DefaultValue = v field.DefaultValue = v