2013-10-26 12:01:15 +04:00
|
|
|
package gorm
|
|
|
|
|
|
|
|
import (
|
2013-11-10 04:57:11 +04:00
|
|
|
"database/sql"
|
2013-11-10 15:38:28 +04:00
|
|
|
"database/sql/driver"
|
2013-10-26 12:01:15 +04:00
|
|
|
"fmt"
|
2013-11-10 15:38:28 +04:00
|
|
|
"reflect"
|
2013-10-26 12:01:15 +04:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2013-11-10 15:38:28 +04:00
|
|
|
func formatColumnValue(column interface{}) interface{} {
|
|
|
|
if v, ok := column.(reflect.Value); ok {
|
|
|
|
column = v.Interface()
|
|
|
|
}
|
|
|
|
|
|
|
|
if valuer, ok := interface{}(column).(driver.Valuer); ok {
|
|
|
|
column = reflect.New(reflect.ValueOf(valuer).Field(0).Type()).Elem().Interface()
|
|
|
|
}
|
|
|
|
return column
|
|
|
|
}
|
|
|
|
|
2013-10-26 13:56:00 +04:00
|
|
|
func getPrimaryKeySqlType(adaptor string, column interface{}, size int) string {
|
2013-11-10 15:38:28 +04:00
|
|
|
column = formatColumnValue(column)
|
|
|
|
|
2013-10-26 13:56:00 +04:00
|
|
|
switch adaptor {
|
2013-11-04 16:47:45 +04:00
|
|
|
case "sqlite3":
|
|
|
|
return "INTEGER PRIMARY KEY"
|
2013-10-26 13:56:00 +04:00
|
|
|
case "mysql":
|
|
|
|
suffix_str := " NOT NULL AUTO_INCREMENT PRIMARY KEY"
|
|
|
|
switch column.(type) {
|
|
|
|
case int, int8, int16, int32, uint, uint8, uint16, uint32:
|
|
|
|
return "int" + suffix_str
|
|
|
|
case int64, uint64:
|
|
|
|
return "bigint" + suffix_str
|
|
|
|
}
|
|
|
|
case "postgres":
|
|
|
|
switch column.(type) {
|
|
|
|
case int, int8, int16, int32, uint, uint8, uint16, uint32:
|
|
|
|
return "serial"
|
|
|
|
case int64, uint64:
|
|
|
|
return "bigserial"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
panic("unsupported sql adaptor, please submit an issue in github")
|
|
|
|
}
|
|
|
|
|
2013-10-26 12:01:15 +04:00
|
|
|
func getSqlType(adaptor string, column interface{}, size int) string {
|
2013-11-10 15:38:28 +04:00
|
|
|
column = formatColumnValue(column)
|
|
|
|
|
2013-10-26 12:01:15 +04:00
|
|
|
switch adaptor {
|
2013-11-04 16:47:45 +04:00
|
|
|
case "sqlite3":
|
|
|
|
switch column.(type) {
|
|
|
|
case time.Time:
|
|
|
|
return "datetime"
|
2013-11-10 04:57:11 +04:00
|
|
|
case bool, sql.NullBool:
|
2013-11-04 16:47:45 +04:00
|
|
|
return "bool"
|
|
|
|
case int, int8, int16, int32, uint, uint8, uint16, uint32:
|
|
|
|
return "integer"
|
2013-11-10 04:57:11 +04:00
|
|
|
case int64, uint64, sql.NullInt64:
|
2013-11-04 16:47:45 +04:00
|
|
|
return "bigint"
|
2013-11-10 04:57:11 +04:00
|
|
|
case float32, float64, sql.NullFloat64:
|
2013-11-04 16:47:45 +04:00
|
|
|
return "real"
|
2013-11-10 04:57:11 +04:00
|
|
|
case string, sql.NullString:
|
2013-11-04 16:47:45 +04:00
|
|
|
if size > 0 && size < 65532 {
|
|
|
|
return fmt.Sprintf("varchar(%d)", size)
|
|
|
|
}
|
|
|
|
return "text"
|
|
|
|
default:
|
|
|
|
panic("invalid sql type")
|
|
|
|
}
|
2013-10-26 12:01:15 +04:00
|
|
|
case "mysql":
|
|
|
|
switch column.(type) {
|
|
|
|
case time.Time:
|
|
|
|
return "timestamp"
|
2013-11-10 04:57:11 +04:00
|
|
|
case bool, sql.NullBool:
|
2013-10-26 12:01:15 +04:00
|
|
|
return "boolean"
|
|
|
|
case int, int8, int16, int32, uint, uint8, uint16, uint32:
|
|
|
|
return "int"
|
2013-11-10 04:57:11 +04:00
|
|
|
case int64, uint64, sql.NullInt64:
|
2013-10-26 12:01:15 +04:00
|
|
|
return "bigint"
|
2013-11-10 04:57:11 +04:00
|
|
|
case float32, float64, sql.NullFloat64:
|
2013-10-26 12:01:15 +04:00
|
|
|
return "double"
|
|
|
|
case []byte:
|
|
|
|
if size > 0 && size < 65532 {
|
|
|
|
return fmt.Sprintf("varbinary(%d)", size)
|
|
|
|
}
|
|
|
|
return "longblob"
|
2013-11-10 04:57:11 +04:00
|
|
|
case string, sql.NullString:
|
2013-10-26 12:01:15 +04:00
|
|
|
if size > 0 && size < 65532 {
|
|
|
|
return fmt.Sprintf("varchar(%d)", size)
|
|
|
|
}
|
|
|
|
return "longtext"
|
|
|
|
default:
|
|
|
|
panic("invalid sql type")
|
|
|
|
}
|
|
|
|
|
|
|
|
case "postgres":
|
|
|
|
switch column.(type) {
|
|
|
|
case time.Time:
|
|
|
|
return "timestamp with time zone"
|
2013-11-10 04:57:11 +04:00
|
|
|
case bool, sql.NullBool:
|
2013-10-26 12:01:15 +04:00
|
|
|
return "boolean"
|
|
|
|
case int, int8, int16, int32, uint, uint8, uint16, uint32:
|
|
|
|
return "integer"
|
2013-11-10 04:57:11 +04:00
|
|
|
case int64, uint64, sql.NullInt64:
|
2013-10-26 12:01:15 +04:00
|
|
|
return "bigint"
|
2013-11-10 04:57:11 +04:00
|
|
|
case float32, float64, sql.NullFloat64:
|
2013-10-26 12:01:15 +04:00
|
|
|
return "double precision"
|
|
|
|
case []byte:
|
|
|
|
return "bytea"
|
2013-11-10 04:57:11 +04:00
|
|
|
case string, sql.NullString:
|
2013-10-26 12:01:15 +04:00
|
|
|
if size > 0 && size < 65532 {
|
|
|
|
return fmt.Sprintf("varchar(%d)", size)
|
|
|
|
}
|
|
|
|
return "text"
|
|
|
|
default:
|
|
|
|
panic("invalid sql type")
|
|
|
|
}
|
2013-10-26 13:56:00 +04:00
|
|
|
default:
|
|
|
|
panic("unsupported sql adaptor, please submit an issue in github")
|
2013-10-26 12:01:15 +04:00
|
|
|
}
|
|
|
|
}
|