forked from eaigner/hood
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysql.go
109 lines (97 loc) · 2.25 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
package hood
import (
"fmt"
_ "github.com/ziutek/mymysql/godrv"
"reflect"
"strings"
"time"
_ "github.com/ziutek/mymysql/godrv"
)
func init() {
RegisterDialect("mymysql", NewMysql())
}
type mysql struct {
base
}
func NewMysql() Dialect {
d := &mysql{}
d.base.Dialect = d
return d
}
func (d *mysql) NextMarker(pos *int) string {
return "?"
}
func (d *mysql) Quote(s string) string {
return fmt.Sprintf("`%s`", s)
}
func (d *mysql) ParseBool(value reflect.Value) bool {
return value.Int() != 0
}
func (d *mysql) SqlType(f interface{}, size int) string {
switch f.(type) {
case Id:
return "int"
case time.Time, Created, Updated:
return "timestamp"
case bool:
return "boolean"
case int, int8, int16, int32, uint, uint8, uint16, uint32:
return "int"
case int64, uint64:
return "bigint"
case float32, float64:
return "double"
case []byte:
if size > 0 && size < 65532 {
return fmt.Sprintf("varbinary(%d)", size)
}
return "longblob"
case string:
if size > 0 && size < 65532 {
return fmt.Sprintf("varchar(%d)", size)
}
return "text"
}
panic("invalid sql type")
}
func (d *mysql) KeywordAutoIncrement() string {
return "AUTO_INCREMENT"
}
func (d *mysql) DropIndexSql(table_name, name string) string {
return fmt.Sprintf("DROP INDEX %v on %v", d.Quote(name), d.Quote(table_name))
}
func (d *mysql) CreateTable(hood *Hood, model *Model) error {
_, err := hood.Exec(d.CreateTableSql(model, false) + " CHARSET = 'utf8'")
return err
}
func (d *mysql) CreateTableSql(model *Model, ifNotExists bool) string {
a := []string{"CREATE TABLE "}
if ifNotExists {
a = append(a, "IF NOT EXISTS ")
}
a = append(a, d.Quote(model.Table), " ( ")
for i, field := range model.Fields {
b := []string{
d.Quote(field.Name),
d.SqlType(field.Value, field.Size()),
}
if field.NotNull() {
b = append(b, d.KeywordNotNull())
}
if x := field.Default(); x != "" {
b = append(b, d.KeywordDefault(x))
}
if field.PrimaryKey() {
b = append(b, d.KeywordPrimaryKey())
}
if incKeyword := d.Dialect.KeywordAutoIncrement(); field.AutoIncr() && incKeyword != "" {
b = append(b, incKeyword)
}
a = append(a, strings.Join(b, " "))
if i < len(model.Fields)-1 {
a = append(a, ", ")
}
}
a = append(a, " )")
return strings.Join(a, "")
}