-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathdialect_mysql.go
79 lines (75 loc) · 2.57 KB
/
dialect_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
package goper
import "fmt"
import "bytes"
type MysqlDialect int
func (this MysqlDialect) Name() string { return "mysql" }
func (this MysqlDialect) CreateTable(table Table) string {
var buf bytes.Buffer
fmt.Fprintf(&buf, "CREATE TABLE %s(", table.Name)
count := len(table.Columns)
for i := range table.Columns {
c := table.Columns[i]
fmt.Fprintf(&buf, "\n%s\t%s", c.Name, c.DbType)
if i != (count - 1) {
fmt.Fprintf(&buf, ",")
}
}
fmt.Fprintf(&buf, "\n);\n")
return string(buf.Bytes())
}
func (this MysqlDialect) DropTable(table Table) string {
var buf bytes.Buffer
fmt.Fprintf(&buf, "DROP TABLE %s\n", table.Name)
return string(buf.Bytes())
}
func (this MysqlDialect) InsertOne(table Table) string {
var buf bytes.Buffer
fmt.Fprintf(&buf, "INSERT INTO %s(", table.Name)
count := len(table.Columns)
for i := range table.Columns {
c := table.Columns[i]
fmt.Fprintf(&buf, "\n\t%s", c.Name)
if i != (count - 1) {
fmt.Fprintf(&buf, ",")
}
}
fmt.Fprintf(&buf, "\n) VALUES (")
for i := range table.Columns {
fmt.Fprintf(&buf, "?")
if i != (count - 1) {
fmt.Fprintf(&buf, ",")
}
}
fmt.Fprintf(&buf, ");")
return string(buf.Bytes())
}
func (this MysqlDialect) ListTables(dbname string) string {
return fmt.Sprintf(`SELECT table_name FROM information_schema.tables where
table_schema = '%s' and table_type = 'BASE TABLE'`, dbname)
}
func (this MysqlDialect) ListColumns(dbname string, table Table) string {
return fmt.Sprintf(`select column_name, data_type from information_schema.columns
where table_schema = '%s' and table_name='%s'`,dbname, table.Name)
}
func (this MysqlDialect) ListCollections(dbname string, table Table) string {
return fmt.Sprintf(`
SELECT DISTINCT i.TABLE_NAME, k.referenced_table_name, k.column_name, k.referenced_column_name
FROM information_schema.TABLE_CONSTRAINTS i
LEFT JOIN information_schema.KEY_COLUMN_USAGE k
ON i.CONSTRAINT_NAME = k.CONSTRAINT_NAME
WHERE i.CONSTRAINT_TYPE = 'FOREIGN KEY'
AND i.TABLE_SCHEMA = '%s'
AND k.referenced_table_name='%s' and
CONSTRAINT_TYPE='FOREIGN KEY'`, dbname, table.Name)
}
func (this MysqlDialect) ListReferences(dbname string, table Table) string {
return fmt.Sprintf(`
SELECT DISTINCT i.TABLE_NAME, k.referenced_table_name, k.column_name, k.referenced_column_name
FROM information_schema.TABLE_CONSTRAINTS i
LEFT JOIN information_schema.KEY_COLUMN_USAGE k
ON i.CONSTRAINT_NAME = k.CONSTRAINT_NAME
WHERE i.CONSTRAINT_TYPE = 'FOREIGN KEY'
AND i.TABLE_SCHEMA = '%s'
AND k.table_name='%s' and
CONSTRAINT_TYPE='FOREIGN KEY'`, dbname, table.Name)
}