feat: support auto_random
This commit is contained in:
parent
baf1afa1fc
commit
ec850f8b60
@ -42,6 +42,7 @@ type ColumnType interface {
|
||||
ColumnType() (columnType string, ok bool) // varchar(64)
|
||||
PrimaryKey() (isPrimaryKey bool, ok bool)
|
||||
AutoIncrement() (isAutoIncrement bool, ok bool)
|
||||
AutoRandom() (isAutoRandom bool, ok bool)
|
||||
Length() (length int64, ok bool)
|
||||
DecimalSize() (precision int64, scale int64, ok bool)
|
||||
Nullable() (nullable bool, ok bool)
|
||||
|
@ -14,6 +14,7 @@ type ColumnType struct {
|
||||
PrimaryKeyValue sql.NullBool
|
||||
UniqueValue sql.NullBool
|
||||
AutoIncrementValue sql.NullBool
|
||||
AutoRandomValue sql.NullBool
|
||||
LengthValue sql.NullInt64
|
||||
DecimalSizeValue sql.NullInt64
|
||||
ScaleValue sql.NullInt64
|
||||
@ -59,6 +60,11 @@ func (ct ColumnType) AutoIncrement() (isAutoIncrement bool, ok bool) {
|
||||
return ct.AutoIncrementValue.Bool, ct.AutoIncrementValue.Valid
|
||||
}
|
||||
|
||||
// AutoRandom returns the column is auto random or not.
|
||||
func (ct ColumnType) AutoRandom() (isAutoRandom bool, ok bool) {
|
||||
return ct.AutoRandomValue.Bool, ct.AutoRandomValue.Valid
|
||||
}
|
||||
|
||||
// Length returns the column type length for variable length column types
|
||||
func (ct ColumnType) Length() (length int64, ok bool) {
|
||||
if ct.LengthValue.Valid {
|
||||
|
1
model.go
1
model.go
@ -4,6 +4,7 @@ import "time"
|
||||
|
||||
// Model a basic GoLang struct which includes the following fields: ID, CreatedAt, UpdatedAt, DeletedAt
|
||||
// It may be embedded into your model or you may build your own model without it
|
||||
//
|
||||
// type User struct {
|
||||
// gorm.Model
|
||||
// }
|
||||
|
@ -59,6 +59,7 @@ type Field struct {
|
||||
PrimaryKey bool
|
||||
AutoIncrement bool
|
||||
AutoIncrementIncrement int64
|
||||
AutoRandom bool
|
||||
Creatable bool
|
||||
Updatable bool
|
||||
Readable bool
|
||||
@ -111,7 +112,8 @@ func (schema *Schema) ParseField(fieldStruct reflect.StructField) *Field {
|
||||
Readable: true,
|
||||
PrimaryKey: utils.CheckTruth(tagSetting["PRIMARYKEY"], tagSetting["PRIMARY_KEY"]),
|
||||
AutoIncrement: utils.CheckTruth(tagSetting["AUTOINCREMENT"]),
|
||||
HasDefaultValue: utils.CheckTruth(tagSetting["AUTOINCREMENT"]),
|
||||
AutoRandom: utils.CheckTruth(tagSetting["AUTORANDOM"]),
|
||||
HasDefaultValue: utils.CheckTruth(tagSetting["AUTOINCREMENT"], tagSetting["AUTORANDOM"]),
|
||||
NotNull: utils.CheckTruth(tagSetting["NOT NULL"], tagSetting["NOTNULL"]),
|
||||
Unique: utils.CheckTruth(tagSetting["UNIQUE"]),
|
||||
Comment: tagSetting["COMMENT"],
|
||||
@ -406,11 +408,13 @@ func (schema *Schema) ParseField(fieldStruct reflect.StructField) *Field {
|
||||
if !utils.CheckTruth(ef.TagSettings["PRIMARYKEY"], ef.TagSettings["PRIMARY_KEY"]) {
|
||||
ef.PrimaryKey = false
|
||||
|
||||
if val, ok := ef.TagSettings["AUTOINCREMENT"]; !ok || !utils.CheckTruth(val) {
|
||||
if autoIncrVal, ok := ef.TagSettings["AUTOINCREMENT"]; !ok || !utils.CheckTruth(autoIncrVal) {
|
||||
ef.AutoIncrement = false
|
||||
} else if autoRandVal, ok := ef.TagSettings["AUTORANDOM"]; !ok || !utils.CheckTruth(autoRandVal) {
|
||||
ef.AutoRandom = false
|
||||
}
|
||||
|
||||
if !ef.AutoIncrement && ef.DefaultValue == "" {
|
||||
if !ef.AutoIncrement && !ef.AutoRandom && ef.DefaultValue == "" {
|
||||
ef.HasDefaultValue = false
|
||||
}
|
||||
}
|
||||
|
@ -123,6 +123,7 @@ func (schema *Schema) parseRelation(field *Field) *Relationship {
|
||||
}
|
||||
|
||||
// User has many Toys, its `Polymorphic` is `Owner`, Pet has one Toy, its `Polymorphic` is `Owner`
|
||||
//
|
||||
// type User struct {
|
||||
// Toys []Toy `gorm:"polymorphic:Owner;"`
|
||||
// }
|
||||
@ -641,7 +642,7 @@ func (rel *Relationship) ToQueryConditions(ctx context.Context, reflectValue ref
|
||||
}
|
||||
|
||||
func copyableDataType(str DataType) bool {
|
||||
for _, s := range []string{"auto_increment", "primary key"} {
|
||||
for _, s := range []string{"increment", "primary key"} {
|
||||
if strings.Contains(strings.ToLower(string(str)), s) {
|
||||
return false
|
||||
}
|
||||
|
@ -239,6 +239,8 @@ func ParseWithSpecialTableName(dest interface{}, cacheStore *sync.Map, namer Nam
|
||||
switch field.GORMDataType {
|
||||
case Int, Uint:
|
||||
if _, ok := field.TagSettings["AUTOINCREMENT"]; !ok {
|
||||
// also not have AUTORANDOM tag
|
||||
if _, ok := field.TagSettings["AUTORANDOM"]; !ok {
|
||||
if !field.HasDefaultValue || field.DefaultValueInterface != nil {
|
||||
schema.FieldsWithDefaultDBValue = append(schema.FieldsWithDefaultDBValue, field)
|
||||
}
|
||||
@ -246,6 +248,7 @@ func ParseWithSpecialTableName(dest interface{}, cacheStore *sync.Map, namer Nam
|
||||
field.HasDefaultValue = true
|
||||
field.AutoIncrement = true
|
||||
}
|
||||
}
|
||||
case String:
|
||||
if _, ok := field.TagSettings["PRIMARYKEY"]; !ok {
|
||||
if !field.HasDefaultValue || field.DefaultValueInterface != nil {
|
||||
|
@ -48,9 +48,11 @@ func (c *wrapperConnPool) Ping() error {
|
||||
}
|
||||
|
||||
// If you use BeginTx returned *sql.Tx as shown below then you can't record queries in a transaction.
|
||||
//
|
||||
// func (c *wrapperConnPool) BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error) {
|
||||
// return c.db.BeginTx(ctx, opts)
|
||||
// }
|
||||
//
|
||||
// You should use BeginTx returned gorm.Tx which could wrap *sql.Tx then you can record all queries.
|
||||
func (c *wrapperConnPool) BeginTx(ctx context.Context, opts *sql.TxOptions) (gorm.ConnPool, error) {
|
||||
tx, err := c.db.BeginTx(ctx, opts)
|
||||
|
@ -8,6 +8,7 @@ require (
|
||||
github.com/lib/pq v1.10.7
|
||||
github.com/mattn/go-sqlite3 v1.14.16 // indirect
|
||||
github.com/microsoft/go-mssqldb v0.19.0 // indirect
|
||||
golang.org/x/crypto v0.5.0 // indirect
|
||||
gorm.io/driver/mysql v1.4.5
|
||||
gorm.io/driver/postgres v1.4.6
|
||||
gorm.io/driver/sqlite v1.4.4
|
||||
|
@ -1,6 +1,7 @@
|
||||
#!/bin/bash -e
|
||||
|
||||
dialects=("sqlite" "mysql" "postgres" "sqlserver")
|
||||
# dialects=("sqlite" "mysql" "postgres" "sqlserver")
|
||||
dialects=("tidb")
|
||||
|
||||
if [[ $(pwd) == *"gorm/tests"* ]]; then
|
||||
cd ..
|
||||
|
Loading…
x
Reference in New Issue
Block a user