Merge branch 'WingGao-wing'

This commit is contained in:
Jinzhu 2018-02-10 08:24:51 +08:00
commit bec55f84c6
6 changed files with 23 additions and 14 deletions

View File

@ -38,6 +38,13 @@ func (commonDialect) Quote(key string) string {
return fmt.Sprintf(`"%s"`, key) return fmt.Sprintf(`"%s"`, key)
} }
func (s *commonDialect) fieldCanAutoIncrement(field *StructField) bool {
if value, ok := field.TagSettings["AUTO_INCREMENT"]; ok {
return value != "FALSE"
}
return field.IsPrimaryKey
}
func (s *commonDialect) DataTypeOf(field *StructField) string { func (s *commonDialect) DataTypeOf(field *StructField) string {
var dataValue, sqlType, size, additionalType = ParseFieldStructForDialect(field, s) var dataValue, sqlType, size, additionalType = ParseFieldStructForDialect(field, s)
@ -46,13 +53,13 @@ func (s *commonDialect) DataTypeOf(field *StructField) string {
case reflect.Bool: case reflect.Bool:
sqlType = "BOOLEAN" sqlType = "BOOLEAN"
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uintptr: 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 { if s.fieldCanAutoIncrement(field) {
sqlType = "INTEGER AUTO_INCREMENT" sqlType = "INTEGER AUTO_INCREMENT"
} else { } else {
sqlType = "INTEGER" sqlType = "INTEGER"
} }
case reflect.Int64, reflect.Uint64: case reflect.Int64, reflect.Uint64:
if _, ok := field.TagSettings["AUTO_INCREMENT"]; ok { if s.fieldCanAutoIncrement(field) {
sqlType = "BIGINT AUTO_INCREMENT" sqlType = "BIGINT AUTO_INCREMENT"
} else { } else {
sqlType = "BIGINT" sqlType = "BIGINT"

View File

@ -44,42 +44,42 @@ func (s *mysql) DataTypeOf(field *StructField) string {
case reflect.Bool: case reflect.Bool:
sqlType = "boolean" sqlType = "boolean"
case reflect.Int8: case reflect.Int8:
if _, ok := field.TagSettings["AUTO_INCREMENT"]; ok || field.IsPrimaryKey { if s.fieldCanAutoIncrement(field) {
field.TagSettings["AUTO_INCREMENT"] = "AUTO_INCREMENT" field.TagSettings["AUTO_INCREMENT"] = "AUTO_INCREMENT"
sqlType = "tinyint AUTO_INCREMENT" sqlType = "tinyint AUTO_INCREMENT"
} else { } else {
sqlType = "tinyint" sqlType = "tinyint"
} }
case reflect.Int, reflect.Int16, reflect.Int32: case reflect.Int, reflect.Int16, reflect.Int32:
if _, ok := field.TagSettings["AUTO_INCREMENT"]; ok || field.IsPrimaryKey { if s.fieldCanAutoIncrement(field) {
field.TagSettings["AUTO_INCREMENT"] = "AUTO_INCREMENT" field.TagSettings["AUTO_INCREMENT"] = "AUTO_INCREMENT"
sqlType = "int AUTO_INCREMENT" sqlType = "int AUTO_INCREMENT"
} else { } else {
sqlType = "int" sqlType = "int"
} }
case reflect.Uint8: case reflect.Uint8:
if _, ok := field.TagSettings["AUTO_INCREMENT"]; ok || field.IsPrimaryKey { if s.fieldCanAutoIncrement(field) {
field.TagSettings["AUTO_INCREMENT"] = "AUTO_INCREMENT" field.TagSettings["AUTO_INCREMENT"] = "AUTO_INCREMENT"
sqlType = "tinyint unsigned AUTO_INCREMENT" sqlType = "tinyint unsigned AUTO_INCREMENT"
} else { } else {
sqlType = "tinyint unsigned" sqlType = "tinyint unsigned"
} }
case reflect.Uint, reflect.Uint16, reflect.Uint32, reflect.Uintptr: case reflect.Uint, reflect.Uint16, reflect.Uint32, reflect.Uintptr:
if _, ok := field.TagSettings["AUTO_INCREMENT"]; ok || field.IsPrimaryKey { if s.fieldCanAutoIncrement(field) {
field.TagSettings["AUTO_INCREMENT"] = "AUTO_INCREMENT" field.TagSettings["AUTO_INCREMENT"] = "AUTO_INCREMENT"
sqlType = "int unsigned AUTO_INCREMENT" sqlType = "int unsigned AUTO_INCREMENT"
} else { } else {
sqlType = "int unsigned" sqlType = "int unsigned"
} }
case reflect.Int64: case reflect.Int64:
if _, ok := field.TagSettings["AUTO_INCREMENT"]; ok || field.IsPrimaryKey { if s.fieldCanAutoIncrement(field) {
field.TagSettings["AUTO_INCREMENT"] = "AUTO_INCREMENT" field.TagSettings["AUTO_INCREMENT"] = "AUTO_INCREMENT"
sqlType = "bigint AUTO_INCREMENT" sqlType = "bigint AUTO_INCREMENT"
} else { } else {
sqlType = "bigint" sqlType = "bigint"
} }
case reflect.Uint64: case reflect.Uint64:
if _, ok := field.TagSettings["AUTO_INCREMENT"]; ok || field.IsPrimaryKey { if s.fieldCanAutoIncrement(field) {
field.TagSettings["AUTO_INCREMENT"] = "AUTO_INCREMENT" field.TagSettings["AUTO_INCREMENT"] = "AUTO_INCREMENT"
sqlType = "bigint unsigned AUTO_INCREMENT" sqlType = "bigint unsigned AUTO_INCREMENT"
} else { } else {

View File

@ -33,14 +33,14 @@ func (s *postgres) DataTypeOf(field *StructField) string {
case reflect.Bool: case reflect.Bool:
sqlType = "boolean" sqlType = "boolean"
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uintptr: case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uintptr:
if _, ok := field.TagSettings["AUTO_INCREMENT"]; ok || field.IsPrimaryKey { if s.fieldCanAutoIncrement(field) {
field.TagSettings["AUTO_INCREMENT"] = "AUTO_INCREMENT" field.TagSettings["AUTO_INCREMENT"] = "AUTO_INCREMENT"
sqlType = "serial" sqlType = "serial"
} else { } else {
sqlType = "integer" sqlType = "integer"
} }
case reflect.Int64, reflect.Uint32, reflect.Uint64: case reflect.Int64, reflect.Uint32, reflect.Uint64:
if _, ok := field.TagSettings["AUTO_INCREMENT"]; ok || field.IsPrimaryKey { if s.fieldCanAutoIncrement(field) {
field.TagSettings["AUTO_INCREMENT"] = "AUTO_INCREMENT" field.TagSettings["AUTO_INCREMENT"] = "AUTO_INCREMENT"
sqlType = "bigserial" sqlType = "bigserial"
} else { } else {

View File

@ -28,14 +28,14 @@ func (s *sqlite3) DataTypeOf(field *StructField) string {
case reflect.Bool: case reflect.Bool:
sqlType = "bool" sqlType = "bool"
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uintptr: case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uintptr:
if field.IsPrimaryKey { if s.fieldCanAutoIncrement(field) {
field.TagSettings["AUTO_INCREMENT"] = "AUTO_INCREMENT" field.TagSettings["AUTO_INCREMENT"] = "AUTO_INCREMENT"
sqlType = "integer primary key autoincrement" sqlType = "integer primary key autoincrement"
} else { } else {
sqlType = "integer" sqlType = "integer"
} }
case reflect.Int64, reflect.Uint64: case reflect.Int64, reflect.Uint64:
if field.IsPrimaryKey { if s.fieldCanAutoIncrement(field) {
field.TagSettings["AUTO_INCREMENT"] = "AUTO_INCREMENT" field.TagSettings["AUTO_INCREMENT"] = "AUTO_INCREMENT"
sqlType = "integer primary key autoincrement" sqlType = "integer primary key autoincrement"
} else { } else {

View File

@ -72,8 +72,10 @@ func OpenTestConnection() (db *gorm.DB, err error) {
// db.SetLogger(Logger{log.New(os.Stdout, "\r\n", 0)}) // db.SetLogger(Logger{log.New(os.Stdout, "\r\n", 0)})
// db.SetLogger(log.New(os.Stdout, "\r\n", 0)) // db.SetLogger(log.New(os.Stdout, "\r\n", 0))
if os.Getenv("DEBUG") == "true" { if debug := os.Getenv("DEBUG"); debug == "true" {
db.LogMode(true) db.LogMode(true)
} else if debug == "false" {
db.LogMode(false)
} }
db.DB().SetMaxIdleConns(10) db.DB().SetMaxIdleConns(10)

View File

@ -1,5 +1,5 @@
dialects=("postgres" "mysql" "mssql" "sqlite") dialects=("postgres" "mysql" "mssql" "sqlite")
for dialect in "${dialects[@]}" ; do for dialect in "${dialects[@]}" ; do
GORM_DIALECT=${dialect} go test DEBUG=false GORM_DIALECT=${dialect} go test
done done