2014-04-25 03:20:23 +04:00
|
|
|
package gorm
|
2013-11-14 14:59:11 +04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2014-03-16 05:28:43 +04:00
|
|
|
"reflect"
|
2016-02-14 08:34:32 +03:00
|
|
|
"strings"
|
2015-02-17 17:55:14 +03:00
|
|
|
"time"
|
2013-11-14 14:59:11 +04:00
|
|
|
)
|
|
|
|
|
2015-03-17 05:40:42 +03:00
|
|
|
type mysql struct {
|
|
|
|
commonDialect
|
2014-09-16 00:03:14 +04:00
|
|
|
}
|
|
|
|
|
2016-02-14 13:06:42 +03:00
|
|
|
func init() {
|
|
|
|
RegisterDialect("mysql", &mysql{})
|
|
|
|
}
|
|
|
|
|
2016-01-18 15:32:52 +03:00
|
|
|
func (mysql) Quote(key string) string {
|
|
|
|
return fmt.Sprintf("`%s`", key)
|
|
|
|
}
|
|
|
|
|
2016-02-14 08:34:32 +03:00
|
|
|
// Get Data Type for MySQL Dialect
|
2016-02-13 18:51:36 +03:00
|
|
|
func (mysql) DataTypeOf(field *StructField) string {
|
2016-02-14 08:34:32 +03:00
|
|
|
var dataValue, sqlType, size, additionalType = ParseFieldStructForDialect(field)
|
2016-02-13 18:51:36 +03:00
|
|
|
|
2016-02-14 08:34:32 +03:00
|
|
|
if sqlType == "" {
|
|
|
|
switch dataValue.Kind() {
|
|
|
|
case reflect.Bool:
|
|
|
|
sqlType = "boolean"
|
|
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32:
|
|
|
|
if _, ok := field.TagSettings["AUTO_INCREMENT"]; ok || field.IsPrimaryKey {
|
|
|
|
sqlType = "int AUTO_INCREMENT"
|
|
|
|
} else {
|
|
|
|
sqlType = "int"
|
|
|
|
}
|
|
|
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uintptr:
|
|
|
|
if _, ok := field.TagSettings["AUTO_INCREMENT"]; ok || field.IsPrimaryKey {
|
|
|
|
sqlType = "int unsigned AUTO_INCREMENT"
|
|
|
|
} else {
|
|
|
|
sqlType = "int unsigned"
|
|
|
|
}
|
|
|
|
case reflect.Int64:
|
|
|
|
if _, ok := field.TagSettings["AUTO_INCREMENT"]; ok || field.IsPrimaryKey {
|
|
|
|
sqlType = "bigint AUTO_INCREMENT"
|
|
|
|
} else {
|
|
|
|
sqlType = "bigint"
|
|
|
|
}
|
|
|
|
case reflect.Uint64:
|
|
|
|
if _, ok := field.TagSettings["AUTO_INCREMENT"]; ok || field.IsPrimaryKey {
|
|
|
|
sqlType = "bigint unsigned AUTO_INCREMENT"
|
|
|
|
} else {
|
|
|
|
sqlType = "bigint unsigned"
|
|
|
|
}
|
|
|
|
case reflect.Float32, reflect.Float64:
|
|
|
|
sqlType = "double"
|
|
|
|
case reflect.String:
|
2014-03-16 05:28:43 +04:00
|
|
|
if size > 0 && size < 65532 {
|
2016-02-14 08:34:32 +03:00
|
|
|
sqlType = fmt.Sprintf("varchar(%d)", size)
|
|
|
|
} else {
|
|
|
|
sqlType = "longtext"
|
|
|
|
}
|
|
|
|
case reflect.Struct:
|
|
|
|
if _, ok := dataValue.Interface().(time.Time); ok {
|
2016-02-14 17:42:17 +03:00
|
|
|
if _, ok := field.TagSettings["NOT NULL"]; ok {
|
|
|
|
sqlType = "timestamp"
|
|
|
|
} else {
|
|
|
|
sqlType = "timestamp NULL"
|
|
|
|
}
|
2016-02-14 08:34:32 +03:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
if _, ok := dataValue.Interface().([]byte); ok {
|
|
|
|
if size > 0 && size < 65532 {
|
|
|
|
sqlType = fmt.Sprintf("varbinary(%d)", size)
|
|
|
|
} else {
|
|
|
|
sqlType = "longblob"
|
|
|
|
}
|
2014-03-16 05:28:43 +04:00
|
|
|
}
|
|
|
|
}
|
2013-11-14 14:59:11 +04:00
|
|
|
}
|
2016-02-14 08:34:32 +03:00
|
|
|
|
|
|
|
if sqlType == "" {
|
|
|
|
panic(fmt.Sprintf("invalid sql type %s (%s) for mysql", dataValue.Type().Name(), dataValue.Kind().String()))
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.TrimSpace(additionalType) == "" {
|
|
|
|
return sqlType
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%v %v", sqlType, additionalType)
|
2013-11-14 14:59:11 +04:00
|
|
|
}
|
|
|
|
|
2016-02-15 09:09:24 +03:00
|
|
|
func (s mysql) RemoveIndex(tableName string, indexName string) error {
|
|
|
|
_, err := s.db.Exec(fmt.Sprintf("DROP INDEX %v ON %v", indexName, s.Quote(tableName)))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s mysql) currentDatabase() (name string) {
|
|
|
|
s.db.QueryRow("SELECT DATABASE()").Scan(&name)
|
2016-01-18 15:32:52 +03:00
|
|
|
return
|
2013-11-30 10:52:01 +04:00
|
|
|
}
|
2014-04-25 03:20:23 +04:00
|
|
|
|
2015-03-17 05:40:42 +03:00
|
|
|
func (mysql) SelectFromDummyTable() string {
|
|
|
|
return "FROM DUAL"
|
2014-07-29 08:02:03 +04:00
|
|
|
}
|