gorm/dialect_common.go

150 lines
3.9 KiB
Go
Raw Normal View History

2014-07-02 13:47:30 +04:00
package gorm
import (
2016-02-15 09:09:24 +03:00
"database/sql"
2014-07-02 13:47:30 +04:00
"fmt"
"reflect"
"regexp"
"strings"
2015-02-17 17:55:14 +03:00
"time"
2014-07-02 13:47:30 +04:00
)
// DefaultForeignKeyNamer contains the default foreign key name generator method
type DefaultForeignKeyNamer struct {
}
2016-02-15 09:09:24 +03:00
type commonDialect struct {
db *sql.DB
DefaultForeignKeyNamer
2016-02-15 09:09:24 +03:00
}
2014-07-02 13:47:30 +04:00
2016-02-14 13:06:42 +03:00
func init() {
RegisterDialect("common", &commonDialect{})
}
func (commonDialect) GetName() string {
return "common"
}
2016-02-15 09:09:24 +03:00
func (s *commonDialect) SetDB(db *sql.DB) {
s.db = db
}
2016-01-19 06:53:53 +03:00
func (commonDialect) BindVar(i int) string {
2015-03-17 05:40:42 +03:00
return "$$" // ?
2014-07-02 13:47:30 +04:00
}
2016-01-18 15:32:52 +03:00
func (commonDialect) Quote(key string) string {
return fmt.Sprintf(`"%s"`, key)
}
2016-02-13 18:51:36 +03:00
func (commonDialect) DataTypeOf(field *StructField) string {
var dataValue, sqlType, size, additionalType = ParseFieldStructForDialect(field)
2016-01-19 15:58:38 +03:00
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:
2014-07-02 13:47:30 +04:00
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)"
}
2014-07-02 13:47:30 +04:00
}
}
}
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)
2014-07-02 13:47:30 +04:00
}
2016-02-15 09:09:24 +03:00
func (s commonDialect) HasIndex(tableName string, indexName string) bool {
var count int
s.db.QueryRow("SELECT count(*) FROM INFORMATION_SCHEMA.STATISTICS WHERE table_schema = ? AND table_name = ? AND index_name = ?", s.currentDatabase(), tableName, indexName).Scan(&count)
2016-01-18 15:32:52 +03:00
return count > 0
}
2016-02-15 09:09:24 +03:00
func (s commonDialect) RemoveIndex(tableName string, indexName string) error {
_, err := s.db.Exec(fmt.Sprintf("DROP INDEX %v", indexName))
return err
2014-07-02 13:47:30 +04:00
}
func (s commonDialect) HasForeignKey(tableName string, foreignKeyName string) bool {
return false
}
2016-02-15 09:09:24 +03:00
func (s commonDialect) HasTable(tableName string) bool {
var count int
s.db.QueryRow("SELECT count(*) FROM INFORMATION_SCHEMA.TABLES WHERE table_schema = ? AND table_name = ?", s.currentDatabase(), tableName).Scan(&count)
2014-07-02 13:47:30 +04:00
return count > 0
}
2016-02-15 09:09:24 +03:00
func (s commonDialect) HasColumn(tableName string, columnName string) bool {
var count int
s.db.QueryRow("SELECT count(*) FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema = ? AND table_name = ? AND column_name = ?", s.currentDatabase(), tableName, columnName).Scan(&count)
return count > 0
}
2016-02-15 09:09:24 +03:00
func (s commonDialect) currentDatabase() (name string) {
s.db.QueryRow("SELECT DATABASE()").Scan(&name)
return
}
2016-01-18 15:32:52 +03:00
func (commonDialect) LimitAndOffsetSQL(limit, offset int64) (sql string) {
2016-02-15 12:00:28 +03:00
if limit > 0 || offset > 0 {
if limit >= 0 {
sql += fmt.Sprintf(" LIMIT %d", limit)
}
if offset >= 0 {
sql += fmt.Sprintf(" OFFSET %d", offset)
}
2016-01-18 15:32:52 +03:00
}
return
}
func (commonDialect) SelectFromDummyTable() string {
return ""
}
2016-03-07 09:54:20 +03:00
func (commonDialect) LastInsertIDReturningSuffix(tableName, columnName string) string {
2016-01-19 06:53:53 +03:00
return ""
2016-01-18 15:32:52 +03:00
}
func (DefaultForeignKeyNamer) BuildForeignKeyName(tableName, field, dest string) string {
keyName := fmt.Sprintf("%s_%s_%s_foreign", tableName, field, dest)
keyName = regexp.MustCompile("(_*[^a-zA-Z]+_*|_+)").ReplaceAllString(keyName, "_")
return keyName
}