feat: support auto_random

This commit is contained in:
Icemap 2023-01-13 16:27:35 +08:00
parent baf1afa1fc
commit ec850f8b60
9 changed files with 46 additions and 26 deletions

View File

@ -42,6 +42,7 @@ type ColumnType interface {
ColumnType() (columnType string, ok bool) // varchar(64) ColumnType() (columnType string, ok bool) // varchar(64)
PrimaryKey() (isPrimaryKey bool, ok bool) PrimaryKey() (isPrimaryKey bool, ok bool)
AutoIncrement() (isAutoIncrement bool, ok bool) AutoIncrement() (isAutoIncrement bool, ok bool)
AutoRandom() (isAutoRandom bool, ok bool)
Length() (length int64, ok bool) Length() (length int64, ok bool)
DecimalSize() (precision int64, scale int64, ok bool) DecimalSize() (precision int64, scale int64, ok bool)
Nullable() (nullable bool, ok bool) Nullable() (nullable bool, ok bool)

View File

@ -14,6 +14,7 @@ type ColumnType struct {
PrimaryKeyValue sql.NullBool PrimaryKeyValue sql.NullBool
UniqueValue sql.NullBool UniqueValue sql.NullBool
AutoIncrementValue sql.NullBool AutoIncrementValue sql.NullBool
AutoRandomValue sql.NullBool
LengthValue sql.NullInt64 LengthValue sql.NullInt64
DecimalSizeValue sql.NullInt64 DecimalSizeValue sql.NullInt64
ScaleValue sql.NullInt64 ScaleValue sql.NullInt64
@ -59,6 +60,11 @@ func (ct ColumnType) AutoIncrement() (isAutoIncrement bool, ok bool) {
return ct.AutoIncrementValue.Bool, ct.AutoIncrementValue.Valid 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 // Length returns the column type length for variable length column types
func (ct ColumnType) Length() (length int64, ok bool) { func (ct ColumnType) Length() (length int64, ok bool) {
if ct.LengthValue.Valid { if ct.LengthValue.Valid {

View File

@ -4,9 +4,10 @@ import "time"
// Model a basic GoLang struct which includes the following fields: ID, CreatedAt, UpdatedAt, DeletedAt // 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 // It may be embedded into your model or you may build your own model without it
// type User struct { //
// gorm.Model // type User struct {
// } // gorm.Model
// }
type Model struct { type Model struct {
ID uint `gorm:"primarykey"` ID uint `gorm:"primarykey"`
CreatedAt time.Time CreatedAt time.Time

View File

@ -59,6 +59,7 @@ type Field struct {
PrimaryKey bool PrimaryKey bool
AutoIncrement bool AutoIncrement bool
AutoIncrementIncrement int64 AutoIncrementIncrement int64
AutoRandom bool
Creatable bool Creatable bool
Updatable bool Updatable bool
Readable bool Readable bool
@ -111,7 +112,8 @@ func (schema *Schema) ParseField(fieldStruct reflect.StructField) *Field {
Readable: true, Readable: true,
PrimaryKey: utils.CheckTruth(tagSetting["PRIMARYKEY"], tagSetting["PRIMARY_KEY"]), PrimaryKey: utils.CheckTruth(tagSetting["PRIMARYKEY"], tagSetting["PRIMARY_KEY"]),
AutoIncrement: utils.CheckTruth(tagSetting["AUTOINCREMENT"]), 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"]), NotNull: utils.CheckTruth(tagSetting["NOT NULL"], tagSetting["NOTNULL"]),
Unique: utils.CheckTruth(tagSetting["UNIQUE"]), Unique: utils.CheckTruth(tagSetting["UNIQUE"]),
Comment: tagSetting["COMMENT"], 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"]) { if !utils.CheckTruth(ef.TagSettings["PRIMARYKEY"], ef.TagSettings["PRIMARY_KEY"]) {
ef.PrimaryKey = false 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 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 ef.HasDefaultValue = false
} }
} }

View File

@ -123,16 +123,17 @@ func (schema *Schema) parseRelation(field *Field) *Relationship {
} }
// User has many Toys, its `Polymorphic` is `Owner`, Pet has one Toy, its `Polymorphic` is `Owner` // User has many Toys, its `Polymorphic` is `Owner`, Pet has one Toy, its `Polymorphic` is `Owner`
// type User struct { //
// Toys []Toy `gorm:"polymorphic:Owner;"` // type User struct {
// } // Toys []Toy `gorm:"polymorphic:Owner;"`
// type Pet struct { // }
// Toy Toy `gorm:"polymorphic:Owner;"` // type Pet struct {
// } // Toy Toy `gorm:"polymorphic:Owner;"`
// type Toy struct { // }
// OwnerID int // type Toy struct {
// OwnerType string // OwnerID int
// } // OwnerType string
// }
func (schema *Schema) buildPolymorphicRelation(relation *Relationship, field *Field, polymorphic string) { func (schema *Schema) buildPolymorphicRelation(relation *Relationship, field *Field, polymorphic string) {
relation.Polymorphic = &Polymorphic{ relation.Polymorphic = &Polymorphic{
Value: schema.Table, Value: schema.Table,
@ -641,7 +642,7 @@ func (rel *Relationship) ToQueryConditions(ctx context.Context, reflectValue ref
} }
func copyableDataType(str DataType) bool { 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) { if strings.Contains(strings.ToLower(string(str)), s) {
return false return false
} }

View File

@ -239,12 +239,15 @@ func ParseWithSpecialTableName(dest interface{}, cacheStore *sync.Map, namer Nam
switch field.GORMDataType { switch field.GORMDataType {
case Int, Uint: case Int, Uint:
if _, ok := field.TagSettings["AUTOINCREMENT"]; !ok { if _, ok := field.TagSettings["AUTOINCREMENT"]; !ok {
if !field.HasDefaultValue || field.DefaultValueInterface != nil { // also not have AUTORANDOM tag
schema.FieldsWithDefaultDBValue = append(schema.FieldsWithDefaultDBValue, field) if _, ok := field.TagSettings["AUTORANDOM"]; !ok {
} if !field.HasDefaultValue || field.DefaultValueInterface != nil {
schema.FieldsWithDefaultDBValue = append(schema.FieldsWithDefaultDBValue, field)
}
field.HasDefaultValue = true field.HasDefaultValue = true
field.AutoIncrement = true field.AutoIncrement = true
}
} }
case String: case String:
if _, ok := field.TagSettings["PRIMARYKEY"]; !ok { if _, ok := field.TagSettings["PRIMARYKEY"]; !ok {

View File

@ -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. // 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) // 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. // 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) { func (c *wrapperConnPool) BeginTx(ctx context.Context, opts *sql.TxOptions) (gorm.ConnPool, error) {
tx, err := c.db.BeginTx(ctx, opts) tx, err := c.db.BeginTx(ctx, opts)

View File

@ -8,6 +8,7 @@ require (
github.com/lib/pq v1.10.7 github.com/lib/pq v1.10.7
github.com/mattn/go-sqlite3 v1.14.16 // indirect github.com/mattn/go-sqlite3 v1.14.16 // indirect
github.com/microsoft/go-mssqldb v0.19.0 // 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/mysql v1.4.5
gorm.io/driver/postgres v1.4.6 gorm.io/driver/postgres v1.4.6
gorm.io/driver/sqlite v1.4.4 gorm.io/driver/sqlite v1.4.4

View File

@ -1,6 +1,7 @@
#!/bin/bash -e #!/bin/bash -e
dialects=("sqlite" "mysql" "postgres" "sqlserver") # dialects=("sqlite" "mysql" "postgres" "sqlserver")
dialects=("tidb")
if [[ $(pwd) == *"gorm/tests"* ]]; then if [[ $(pwd) == *"gorm/tests"* ]]; then
cd .. cd ..