Support customize gorm field type

This commit is contained in:
Jinzhu 2020-06-06 10:47:32 +08:00
parent 1490a062db
commit a954d772d7
5 changed files with 52 additions and 23 deletions

View File

@ -24,6 +24,10 @@ type Config struct {
gorm.Dialector gorm.Dialector
} }
type GormDataTypeInterface interface {
GormDBDataType(*gorm.DB, *schema.Field) string
}
func (m Migrator) RunWithValue(value interface{}, fc func(*gorm.Statement) error) error { func (m Migrator) RunWithValue(value interface{}, fc func(*gorm.Statement) error) error {
stmt := &gorm.Statement{DB: m.DB} stmt := &gorm.Statement{DB: m.DB}
if m.DB.Statement != nil { if m.DB.Statement != nil {
@ -44,6 +48,13 @@ func (m Migrator) DataTypeOf(field *schema.Field) string {
return field.DBDataType return field.DBDataType
} }
fieldValue := reflect.New(field.IndirectFieldType)
if dataTyper, ok := fieldValue.Interface().(GormDataTypeInterface); ok {
if dataType := dataTyper.GormDBDataType(m.DB, field); dataType != "" {
return dataType
}
}
return m.Dialector.DataTypeOf(field) return m.Dialector.DataTypeOf(field)
} }

View File

@ -220,6 +220,10 @@ func (schema *Schema) ParseField(fieldStruct reflect.StructField) *Field {
} }
} }
if dataTyper, ok := fieldValue.Interface().(GormDataTypeInterface); ok {
field.DataType = DataType(dataTyper.GormDataType())
}
if v, ok := field.TagSettings["AUTOCREATETIME"]; ok || (field.Name == "CreatedAt" && (field.DataType == Time || field.DataType == Int || field.DataType == Uint)) { if v, ok := field.TagSettings["AUTOCREATETIME"]; ok || (field.Name == "CreatedAt" && (field.DataType == Time || field.DataType == Int || field.DataType == Uint)) {
if strings.ToUpper(v) == "NANO" { if strings.ToUpper(v) == "NANO" {
field.AutoCreateTime = UnixNanosecond field.AutoCreateTime = UnixNanosecond

23
schema/interfaces.go Normal file
View File

@ -0,0 +1,23 @@
package schema
import "gorm.io/gorm/clause"
type GormDataTypeInterface interface {
GormDataType() string
}
type CreateClausesInterface interface {
CreateClauses() []clause.Interface
}
type QueryClausesInterface interface {
QueryClauses() []clause.Interface
}
type UpdateClausesInterface interface {
UpdateClauses() []clause.Interface
}
type DeleteClausesInterface interface {
DeleteClauses() []clause.Interface
}

View File

@ -42,22 +42,6 @@ type Schema struct {
cacheStore *sync.Map cacheStore *sync.Map
} }
type CreateClausesInterface interface {
CreateClauses() []clause.Interface
}
type QueryClausesInterface interface {
QueryClauses() []clause.Interface
}
type UpdateClausesInterface interface {
UpdateClauses() []clause.Interface
}
type DeleteClausesInterface interface {
DeleteClauses() []clause.Interface
}
func (schema Schema) String() string { func (schema Schema) String() string {
if schema.ModelType.Name() == "" { if schema.ModelType.Name() == "" {
return fmt.Sprintf("%v(%v)", schema.Name, schema.Table) return fmt.Sprintf("%v(%v)", schema.Name, schema.Table)

View File

@ -17,14 +17,21 @@ for dialect in "${dialects[@]}" ; do
if [ "$GORM_VERBOSE" = "" ] if [ "$GORM_VERBOSE" = "" ]
then then
DEBUG=false GORM_DIALECT=${dialect} go test $race -count=1 ./... GORM_DIALECT=${dialect} go test $race -count=1 ./...
if [ -d tests ]
then
cd tests cd tests
DEBUG=false GORM_DIALECT=${dialect} go test $race -count=1 ./... GORM_DIALECT=${dialect} go test $race -count=1 ./...
else
DEBUG=false GORM_DIALECT=${dialect} go test $race -count=1 -v ./...
cd tests
DEBUG=false GORM_DIALECT=${dialect} go test $race -count=1 -v ./...
fi
cd .. cd ..
fi fi
else
GORM_DIALECT=${dialect} go test $race -count=1 -v ./...
if [ -d tests ]
then
cd tests
GORM_DIALECT=${dialect} go test $race -count=1 -v ./...
cd ..
fi
fi
fi
done done