forked from hidu/mysql-schema-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_schema.go
80 lines (68 loc) · 1.71 KB
/
db_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
78
79
80
package sync
import (
"database/sql"
"log"
"regexp"
"github.com/admpub/mysql-schema-sync/internal"
)
var (
sqlTableName = regexp.MustCompile("CREATE TABLE [^`]*`([^`]+)` \\(")
_ internal.DBOperator = &MySchemaData{}
)
// MySchemaData db schema data
type MySchemaData struct {
Data string
dbType string
engine string
}
// NewMySchemaData object
func NewMySchemaData(schema string, dbType string) *MySchemaData {
return &MySchemaData{
Data: schema,
dbType: dbType,
engine: `mysql`,
}
}
func (m *MySchemaData) DBEngine() string {
return m.engine
}
// GetTableNames table names
func (m *MySchemaData) GetTableNames() []string {
matches := sqlTableName.FindAllStringSubmatch(m.Data, -1)
var tables []string
if matches != nil {
for _, match := range matches {
tables = append(tables, match[1])
}
}
return tables
}
// GetTableSchema table schema
func (m *MySchemaData) GetTableSchema(name string) (schema string) {
schemaStruct, err := regexp.Compile("(?sm)CREATE TABLE [^`]*`" + name + "` \\((.+?)\\) ENGINE\\=[^\\r\\n]*;[\\r]?\\n")
if err != nil {
log.Println(err)
}
matches := schemaStruct.FindStringSubmatch(m.Data)
//log.Printf("%#v\n", matches)
if matches != nil {
schema = matches[0]
}
return
}
// Query execute sql query
func (m *MySchemaData) Query(query string, args ...interface{}) (*sql.Rows, error) {
log.Println("[SQL]", "["+m.dbType+"]", query, args)
return nil, nil
}
// Exec execute sql query
func (m *MySchemaData) Exec(query string) (sql.Result, error) {
log.Println("[SQL]", "["+m.dbType+"]", query)
return nil, nil
}
func (m *MySchemaData) Begin() (*sql.Tx, error) {
return nil, nil
}
func (m *MySchemaData) Close() error {
return nil
}