mirror of https://github.com/go-gorm/gorm.git
Allow customize data type via ParseFieldStructForDialect
This commit is contained in:
parent
c62e9bcabe
commit
a3b8b332ed
18
dialect.go
18
dialect.go
|
@ -68,10 +68,14 @@ func RegisterDialect(name string, dialect Dialect) {
|
||||||
dialectsMap[name] = dialect
|
dialectsMap[name] = dialect
|
||||||
}
|
}
|
||||||
|
|
||||||
// ParseFieldStructForDialect parse field struct for dialect
|
// ParseFieldStructForDialect get field's sql data type
|
||||||
func ParseFieldStructForDialect(field *StructField) (fieldValue reflect.Value, sqlType string, size int, additionalType string) {
|
var ParseFieldStructForDialect = func(field *StructField, dialect Dialect) (fieldValue reflect.Value, sqlType string, size int, additionalType string) {
|
||||||
// Get redirected field type
|
// Get redirected field type
|
||||||
var reflectType = field.Struct.Type
|
var (
|
||||||
|
reflectType = field.Struct.Type
|
||||||
|
dataType = field.TagSettings["TYPE"]
|
||||||
|
)
|
||||||
|
|
||||||
for reflectType.Kind() == reflect.Ptr {
|
for reflectType.Kind() == reflect.Ptr {
|
||||||
reflectType = reflectType.Elem()
|
reflectType = reflectType.Elem()
|
||||||
}
|
}
|
||||||
|
@ -79,6 +83,12 @@ func ParseFieldStructForDialect(field *StructField) (fieldValue reflect.Value, s
|
||||||
// Get redirected field value
|
// Get redirected field value
|
||||||
fieldValue = reflect.Indirect(reflect.New(reflectType))
|
fieldValue = reflect.Indirect(reflect.New(reflectType))
|
||||||
|
|
||||||
|
if gormDataType, ok := fieldValue.Interface().(interface {
|
||||||
|
GormDataType(Dialect) string
|
||||||
|
}); ok {
|
||||||
|
dataType = gormDataType.GormDataType(dialect)
|
||||||
|
}
|
||||||
|
|
||||||
// Get scanner's real value
|
// Get scanner's real value
|
||||||
var getScannerValue func(reflect.Value)
|
var getScannerValue func(reflect.Value)
|
||||||
getScannerValue = func(value reflect.Value) {
|
getScannerValue = func(value reflect.Value) {
|
||||||
|
@ -102,5 +112,5 @@ func ParseFieldStructForDialect(field *StructField) (fieldValue reflect.Value, s
|
||||||
additionalType = additionalType + " DEFAULT " + value
|
additionalType = additionalType + " DEFAULT " + value
|
||||||
}
|
}
|
||||||
|
|
||||||
return fieldValue, field.TagSettings["TYPE"], size, strings.TrimSpace(additionalType)
|
return fieldValue, dataType, size, strings.TrimSpace(additionalType)
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,8 +39,8 @@ func (commonDialect) Quote(key string) string {
|
||||||
return fmt.Sprintf(`"%s"`, key)
|
return fmt.Sprintf(`"%s"`, key)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (commonDialect) DataTypeOf(field *StructField) string {
|
func (s *commonDialect) DataTypeOf(field *StructField) string {
|
||||||
var dataValue, sqlType, size, additionalType = ParseFieldStructForDialect(field)
|
var dataValue, sqlType, size, additionalType = ParseFieldStructForDialect(field, s)
|
||||||
|
|
||||||
if sqlType == "" {
|
if sqlType == "" {
|
||||||
switch dataValue.Kind() {
|
switch dataValue.Kind() {
|
||||||
|
|
|
@ -27,8 +27,8 @@ func (mysql) Quote(key string) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get Data Type for MySQL Dialect
|
// Get Data Type for MySQL Dialect
|
||||||
func (mysql) DataTypeOf(field *StructField) string {
|
func (s *mysql) DataTypeOf(field *StructField) string {
|
||||||
var dataValue, sqlType, size, additionalType = ParseFieldStructForDialect(field)
|
var dataValue, sqlType, size, additionalType = ParseFieldStructForDialect(field, s)
|
||||||
|
|
||||||
// MySQL allows only one auto increment column per table, and it must
|
// MySQL allows only one auto increment column per table, and it must
|
||||||
// be a KEY column.
|
// be a KEY column.
|
||||||
|
|
|
@ -23,8 +23,8 @@ func (postgres) BindVar(i int) string {
|
||||||
return fmt.Sprintf("$%v", i)
|
return fmt.Sprintf("$%v", i)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (postgres) DataTypeOf(field *StructField) string {
|
func (s *postgres) DataTypeOf(field *StructField) string {
|
||||||
var dataValue, sqlType, size, additionalType = ParseFieldStructForDialect(field)
|
var dataValue, sqlType, size, additionalType = ParseFieldStructForDialect(field, s)
|
||||||
|
|
||||||
if sqlType == "" {
|
if sqlType == "" {
|
||||||
switch dataValue.Kind() {
|
switch dataValue.Kind() {
|
||||||
|
|
|
@ -21,8 +21,8 @@ func (sqlite3) GetName() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get Data Type for Sqlite Dialect
|
// Get Data Type for Sqlite Dialect
|
||||||
func (sqlite3) DataTypeOf(field *StructField) string {
|
func (s *sqlite3) DataTypeOf(field *StructField) string {
|
||||||
var dataValue, sqlType, size, additionalType = ParseFieldStructForDialect(field)
|
var dataValue, sqlType, size, additionalType = ParseFieldStructForDialect(field, s)
|
||||||
|
|
||||||
if sqlType == "" {
|
if sqlType == "" {
|
||||||
switch dataValue.Kind() {
|
switch dataValue.Kind() {
|
||||||
|
|
|
@ -44,8 +44,8 @@ func (mssql) Quote(key string) string {
|
||||||
return fmt.Sprintf(`"%s"`, key)
|
return fmt.Sprintf(`"%s"`, key)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (mssql) DataTypeOf(field *gorm.StructField) string {
|
func (s *mssql) DataTypeOf(field *gorm.StructField) string {
|
||||||
var dataValue, sqlType, size, additionalType = gorm.ParseFieldStructForDialect(field)
|
var dataValue, sqlType, size, additionalType = gorm.ParseFieldStructForDialect(field, s)
|
||||||
|
|
||||||
if sqlType == "" {
|
if sqlType == "" {
|
||||||
switch dataValue.Kind() {
|
switch dataValue.Kind() {
|
||||||
|
|
Loading…
Reference in New Issue