From b4abd125c16ab55f3dfbdd5be5ecbc755ae7df94 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Sun, 14 Feb 2016 13:51:34 +0800 Subject: [PATCH] Refactor DataTypeOf for postgres, mssql --- dialect_common.go | 86 +++++++++++++++++++++++-------------------- dialect_mssql.go | 86 +++++++++++++++++++++++-------------------- dialect_postgres.go | 90 +++++++++++++++++++++++---------------------- 3 files changed, 139 insertions(+), 123 deletions(-) diff --git a/dialect_common.go b/dialect_common.go index f95f3903..9f10a287 100644 --- a/dialect_common.go +++ b/dialect_common.go @@ -3,7 +3,7 @@ package gorm import ( "fmt" "reflect" - "strconv" + "strings" "time" ) @@ -18,49 +18,55 @@ func (commonDialect) Quote(key string) string { } func (commonDialect) DataTypeOf(field *StructField) string { - var ( - size int - dataValue = reflect.Indirect(reflect.New(field.Struct.Type)) - tagSettings = field.TagSettings - ) + var dataValue, sqlType, size, additionalType = ParseFieldStructForDialect(field) - if num, ok := tagSettings["SIZE"]; ok { - size, _ = strconv.Atoi(num) - } - - switch dataValue.Kind() { - case reflect.Bool: - return "BOOLEAN" - case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uintptr: - if _, ok := tagSettings["AUTO_INCREMENT"]; ok { - return "INTEGER AUTO_INCREMENT" - } - return "INTEGER" - case reflect.Int64, reflect.Uint64: - if _, ok := tagSettings["AUTO_INCREMENT"]; ok { - return "BIGINT AUTO_INCREMENT" - } - return "BIGINT" - case reflect.Float32, reflect.Float64: - return "FLOAT" - case reflect.String: - if size > 0 && size < 65532 { - return fmt.Sprintf("VARCHAR(%d)", size) - } - return "VARCHAR(65532)" - case reflect.Struct: - if _, ok := dataValue.Interface().(time.Time); ok { - return "TIMESTAMP" - } - default: - if _, ok := dataValue.Interface().([]byte); ok { - if size > 0 && size < 65532 { - return fmt.Sprintf("BINARY(%d)", size) + if sqlType == "" { + switch dataValue.Kind() { + case reflect.Bool: + sqlType = "BOOLEAN" + case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uintptr: + if _, ok := field.TagSettings["AUTO_INCREMENT"]; ok { + sqlType = "INTEGER AUTO_INCREMENT" + } else { + sqlType = "INTEGER" + } + case reflect.Int64, reflect.Uint64: + if _, ok := field.TagSettings["AUTO_INCREMENT"]; ok { + sqlType = "BIGINT AUTO_INCREMENT" + } else { + sqlType = "BIGINT" + } + case reflect.Float32, reflect.Float64: + sqlType = "FLOAT" + case reflect.String: + if size > 0 && size < 65532 { + sqlType = fmt.Sprintf("VARCHAR(%d)", size) + } else { + sqlType = "VARCHAR(65532)" + } + case reflect.Struct: + if _, ok := dataValue.Interface().(time.Time); ok { + sqlType = "TIMESTAMP" + } + default: + if _, ok := dataValue.Interface().([]byte); ok { + if size > 0 && size < 65532 { + sqlType = fmt.Sprintf("BINARY(%d)", size) + } else { + sqlType = "BINARY(65532)" + } } - return "BINARY(65532)" } } - panic(fmt.Sprintf("invalid sql type %s (%s) for commonDialect", dataValue.Type().Name(), dataValue.Kind().String())) + + if sqlType == "" { + panic(fmt.Sprintf("invalid sql type %s (%s) for commonDialect", dataValue.Type().Name(), dataValue.Kind().String())) + } + + if strings.TrimSpace(additionalType) == "" { + return sqlType + } + return fmt.Sprintf("%v %v", sqlType, additionalType) } func (c commonDialect) HasIndex(scope *Scope, tableName string, indexName string) bool { diff --git a/dialect_mssql.go b/dialect_mssql.go index aa3f7f5d..63971e34 100644 --- a/dialect_mssql.go +++ b/dialect_mssql.go @@ -3,7 +3,7 @@ package gorm import ( "fmt" "reflect" - "strconv" + "strings" "time" ) @@ -12,49 +12,55 @@ type mssql struct { } func (mssql) DataTypeOf(field *StructField) string { - var ( - size int - dataValue = reflect.Indirect(reflect.New(field.Struct.Type)) - tagSettings = field.TagSettings - ) + var dataValue, sqlType, size, additionalType = ParseFieldStructForDialect(field) - if num, ok := tagSettings["SIZE"]; ok { - size, _ = strconv.Atoi(num) - } - - switch dataValue.Kind() { - case reflect.Bool: - return "bit" - case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uintptr: - if _, ok := tagSettings["AUTO_INCREMENT"]; ok { - return "int IDENTITY(1,1)" - } - return "int" - case reflect.Int64, reflect.Uint64: - if _, ok := tagSettings["AUTO_INCREMENT"]; ok { - return "bigint IDENTITY(1,1)" - } - return "bigint" - case reflect.Float32, reflect.Float64: - return "float" - case reflect.String: - if size > 0 && size < 65532 { - return fmt.Sprintf("nvarchar(%d)", size) - } - return "text" - case reflect.Struct: - if _, ok := dataValue.Interface().(time.Time); ok { - return "datetime2" - } - default: - if _, ok := dataValue.Interface().([]byte); ok { - if size > 0 && size < 65532 { - return fmt.Sprintf("varchar(%d)", size) + if sqlType == "" { + switch dataValue.Kind() { + case reflect.Bool: + sqlType = "bit" + case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uintptr: + if _, ok := field.TagSettings["AUTO_INCREMENT"]; ok || field.IsPrimaryKey { + sqlType = "int IDENTITY(1,1)" + } else { + sqlType = "int" + } + case reflect.Int64, reflect.Uint64: + if _, ok := field.TagSettings["AUTO_INCREMENT"]; ok || field.IsPrimaryKey { + sqlType = "bigint IDENTITY(1,1)" + } else { + sqlType = "bigint" + } + case reflect.Float32, reflect.Float64: + sqlType = "float" + case reflect.String: + if size > 0 && size < 65532 { + sqlType = fmt.Sprintf("nvarchar(%d)", size) + } else { + sqlType = "text" + } + case reflect.Struct: + if _, ok := dataValue.Interface().(time.Time); ok { + sqlType = "datetime2" + } + default: + if _, ok := dataValue.Interface().([]byte); ok { + if size > 0 && size < 65532 { + sqlType = fmt.Sprintf("varchar(%d)", size) + } else { + sqlType = "text" + } } - return "text" } } - panic(fmt.Sprintf("invalid sql type %s (%s) for mssql", dataValue.Type().Name(), dataValue.Kind().String())) + + if sqlType == "" { + panic(fmt.Sprintf("invalid sql type %s (%s) for mssql", dataValue.Type().Name(), dataValue.Kind().String())) + } + + if strings.TrimSpace(additionalType) == "" { + return sqlType + } + return fmt.Sprintf("%v %v", sqlType, additionalType) } func (s mssql) HasIndex(scope *Scope, tableName string, indexName string) bool { diff --git a/dialect_postgres.go b/dialect_postgres.go index e49df3d2..b35b918a 100644 --- a/dialect_postgres.go +++ b/dialect_postgres.go @@ -5,7 +5,6 @@ import ( "database/sql/driver" "fmt" "reflect" - "strconv" "strings" "time" @@ -21,52 +20,57 @@ func (postgres) BindVar(i int) string { } func (postgres) DataTypeOf(field *StructField) string { - var ( - size int - dataValue = reflect.Indirect(reflect.New(field.Struct.Type)) - tagSettings = field.TagSettings - ) + var dataValue, sqlType, size, additionalType = ParseFieldStructForDialect(field) - if num, ok := tagSettings["SIZE"]; ok { - size, _ = strconv.Atoi(num) + if sqlType == "" { + switch dataValue.Kind() { + case reflect.Bool: + sqlType = "boolean" + case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uintptr: + if _, ok := field.TagSettings["AUTO_INCREMENT"]; ok || field.IsPrimaryKey { + sqlType = "serial" + } else { + sqlType = "integer" + } + case reflect.Int64, reflect.Uint64: + if _, ok := field.TagSettings["AUTO_INCREMENT"]; ok || field.IsPrimaryKey { + sqlType = "bigserial" + } else { + sqlType = "bigint" + } + case reflect.Float32, reflect.Float64: + sqlType = "numeric" + case reflect.String: + if size > 0 && size < 65532 { + sqlType = fmt.Sprintf("varchar(%d)", size) + } else { + sqlType = "text" + } + case reflect.Struct: + if _, ok := dataValue.Interface().(time.Time); ok { + sqlType = "timestamp with time zone" + } + case reflect.Map: + if dataValue.Type() == hstoreType { + sqlType = "hstore" + } + default: + if isByteArrayOrSlice(dataValue) { + sqlType = "bytea" + } else if isUUID(dataValue) { + sqlType = "uuid" + } + } } - switch dataValue.Kind() { - case reflect.Bool: - return "boolean" - case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uintptr: - if _, ok := tagSettings["AUTO_INCREMENT"]; ok { - return "serial" - } - return "integer" - case reflect.Int64, reflect.Uint64: - if _, ok := tagSettings["AUTO_INCREMENT"]; ok { - return "bigserial" - } - return "bigint" - case reflect.Float32, reflect.Float64: - return "numeric" - case reflect.String: - if size > 0 && size < 65532 { - return fmt.Sprintf("varchar(%d)", size) - } - return "text" - case reflect.Struct: - if _, ok := dataValue.Interface().(time.Time); ok { - return "timestamp with time zone" - } - case reflect.Map: - if dataValue.Type() == hstoreType { - return "hstore" - } - default: - if isByteArrayOrSlice(dataValue) { - return "bytea" - } else if isUUID(dataValue) { - return "uuid" - } + if sqlType == "" { + panic(fmt.Sprintf("invalid sql type %s (%s) for postgres", dataValue.Type().Name(), dataValue.Kind().String())) } - panic(fmt.Sprintf("invalid sql type %s (%s) for postgres", dataValue.Type().Name(), dataValue.Kind().String())) + + if strings.TrimSpace(additionalType) == "" { + return sqlType + } + return fmt.Sprintf("%v %v", sqlType, additionalType) } func (s postgres) HasIndex(scope *Scope, tableName string, indexName string) bool {