-
Notifications
You must be signed in to change notification settings - Fork 101
/
mysql.go
111 lines (103 loc) · 2.7 KB
/
mysql.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package qbs
import (
"database/sql"
"fmt"
"reflect"
"time"
)
type mysql struct {
base
}
func NewMysql() Dialect {
d := new(mysql)
d.base.dialect = d
return d
}
func DefaultMysqlDataSourceName(dbName string) *DataSourceName {
dsn := new(DataSourceName)
dsn.Dialect = new(mysql)
dsn.Username = "root"
dsn.DbName = dbName
dsn.Append("loc", "Local")
dsn.Append("charset", "utf8")
dsn.Append("parseTime", "true")
return dsn
}
func (d mysql) parseBool(value reflect.Value) bool {
return value.Int() != 0
}
func (d mysql) sqlType(field modelField) string {
f := field.value
fieldValue := reflect.ValueOf(f)
kind := fieldValue.Kind()
if field.nullable != reflect.Invalid {
kind = field.nullable
}
switch kind {
case reflect.Bool:
return "boolean"
case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Uint8, reflect.Uint16, reflect.Uint32:
return "int"
case reflect.Uint, reflect.Uint64, reflect.Int, reflect.Int64:
return "bigint"
case reflect.Float32, reflect.Float64:
return "double"
case reflect.String:
if field.size > 0 && field.size < 65532 {
return fmt.Sprintf("varchar(%d)", field.size)
}
return "longtext"
case reflect.Slice:
if reflect.TypeOf(f).Elem().Kind() == reflect.Uint8 {
if field.size > 0 && field.size < 65532 {
return fmt.Sprintf("varbinary(%d)", field.size)
}
return "longblob"
}
case reflect.Struct:
switch fieldValue.Interface().(type) {
case time.Time:
return "timestamp"
case sql.NullBool:
return "boolean"
case sql.NullInt64:
return "bigint"
case sql.NullFloat64:
return "double"
case sql.NullString:
if field.size > 0 && field.size < 65532 {
return fmt.Sprintf("varchar(%d)", field.size)
}
return "longtext"
default:
if len(field.colType) != 0 {
switch field.colType {
case QBS_COLTYPE_BOOL, QBS_COLTYPE_INT, QBS_COLTYPE_BIGINT, QBS_COLTYPE_DOUBLE, QBS_COLTYPE_TIME:
return field.colType
case QBS_COLTYPE_TEXT:
if field.size > 0 && field.size < 65532 {
return fmt.Sprintf("varchar(%d)", field.size)
}
return "longtext"
default:
panic("Qbs doesn't support column type " + field.colType + " for MySQL")
}
}
}
}
panic("invalid sql type for field:" + field.name)
}
func (d mysql) indexExists(mg *Migration, tableName, indexName string) bool {
var row *sql.Row
var name string
row = mg.db.QueryRow("SELECT INDEX_NAME FROM INFORMATION_SCHEMA.STATISTICS "+
"WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ? AND INDEX_NAME = ?", mg.dbName, tableName, indexName)
row.Scan(&name)
return name != ""
}
func (d mysql) primaryKeySql(isString bool, size int) string {
if isString {
return fmt.Sprintf("varchar(%d) PRIMARY KEY", size)
}
return "bigint PRIMARY KEY AUTO_INCREMENT"
}