-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sql.go
142 lines (125 loc) · 2.9 KB
/
sql.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package rawsql
import (
"io/ioutil"
"os"
"path/filepath"
_ "github.com/pingcap/tidb/pkg/parser/test_driver"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"gorm.io/gorm/migrator"
"gorm.io/gorm/schema"
)
type Config struct {
DriverName string //mysql
FilePath []string //create table sql file or file path
SQL []string //create table sql content
Parser
}
type Dialector struct {
*Config
tables map[string]*Table
}
func New(config Config) gorm.Dialector {
return &Dialector{Config: &config, tables: map[string]*Table{}}
}
func (dialector Dialector) Name() string {
return dialector.DriverName
}
func (dialector Dialector) Initialize(db *gorm.DB) error {
if dialector.DriverName == "" {
dialector.DriverName = "mysql"
}
if dialector.SQL == nil {
dialector.SQL = make([]string, 0)
}
if dialector.tables == nil {
dialector.tables = make(map[string]*Table)
}
if dialector.Parser == nil {
dialector.Parser = newDefaultParse()
}
if err := dialector.fileTOSQL(); err != nil {
return err
}
if err := dialector.sqlTOTable(); err != nil {
return err
}
return nil
}
func (dialector Dialector) sqlTOTable() error {
for _, sql := range dialector.SQL {
if err := dialector.Parser.ParseSQL(sql); err != nil {
return err
}
}
tables := dialector.Parser.GetTables()
for _, v := range tables {
dialector.tables[v.Name] = v
}
return nil
}
func (dialector Dialector) fileTOSQL() error {
for _, f := range dialector.FilePath {
if f == "" {
continue
}
v, err := os.Stat(f)
if err != nil {
return err
}
if v.IsDir() {
err = dialector.readFiles(f)
} else {
err = dialector.readFile(f)
}
if err != nil {
return err
}
}
return nil
}
func (dialector Dialector) readFiles(folder string) (err error) {
files, _ := ioutil.ReadDir(folder)
for _, file := range files {
fn := filepath.Join(folder, file.Name())
if file.IsDir() {
err = dialector.readFiles(fn)
} else {
err = dialector.readFile(fn)
}
if err != nil {
return err
}
}
return nil
}
func (dialector Dialector) readFile(fileName string) error {
content, err := ioutil.ReadFile(fileName)
if err != nil {
return err
}
dialector.SQL = append(dialector.SQL, string(content))
return nil
}
func (dialector Dialector) Migrator(db *gorm.DB) gorm.Migrator {
return Migrator{
Migrator: migrator.Migrator{
Config: migrator.Config{
DB: db,
Dialector: dialector,
},
},
Dialector: dialector,
}
}
func (dialector Dialector) DataTypeOf(field *schema.Field) string {
return ""
}
func (dialector Dialector) DefaultValueOf(field *schema.Field) clause.Expression {
return clause.Expr{SQL: "DEFAULT"}
}
func (dialector Dialector) BindVarTo(writer clause.Writer, stmt *gorm.Statement, v interface{}) {}
func (dialector Dialector) QuoteTo(writer clause.Writer, str string) {}
func (dialector Dialector) Explain(sql string, vars ...interface{}) string {
return ""
}