-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathschema.go
77 lines (71 loc) · 1.81 KB
/
schema.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
package main
import (
"github.com/zhengyun1112/glorm/orm"
"strings"
"fmt"
"database/sql"
)
type column struct {
TableSchema string
TableName string
ColumnName string
ColumnDefault sql.NullString
DataType string
ColumnType string
ColumnKey string
Extra string
ColumnComment string
}
func (col column) GetDataType() string {
kFieldTypes := map[string]string{
"bigint": "int64",
"int": "int",
"tinyint": "int",
"smallint": "int",
"char": "string",
"varchar": "string",
"blob": "[]byte",
"date": "time.Time",
"datetime": "time.Time",
"timestamp": "time.Time",
"float": "float64",
"decimal": "float64",
"double": "float64",
"bit": "uint64",
}
if fieldType, ok := kFieldTypes[strings.ToLower(col.DataType)]; !ok {
return "string"
} else {
return fieldType
}
}
type TableSchema []column
type DbSchema map[string]TableSchema
type Driver interface {
LoadDatabaseSchema(dsnString string, schema string, tableNames string) (DbSchema, error)
}
func loadDatabaseSchema(dsnString, schema, tableNames string) (DbSchema, error) {
var dbSchema DbSchema = DbSchema{}
m := orm.NewORMWithConnNum(strings.Replace(dsnString, "/"+schema, "/information_schema", 1), 10, 5)
var columns []*column
tableFilter := ""
if len(tableNames) > 0 {
tableFilter = " AND TABLE_NAME IN ("
tables := strings.Split(tableNames, ",")
for i, table := range tables {
if i > 0 {
tableFilter += ","
}
tableFilter = tableFilter + fmt.Sprintf(`"%s"`, table)
}
tableFilter += ")"
}
err := m.Select(&columns, "SELECT * FROM COLUMNS where TABLE_SCHEMA = ?" + tableFilter, schema)
if err != nil {
return dbSchema, err
}
for _, col := range columns {
dbSchema[col.TableName] = append(dbSchema[col.TableName], *col)
}
return dbSchema, nil
}