/
convert.go
289 lines (247 loc) · 10.7 KB
/
convert.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package cmd
import (
"fmt"
"github.com/camry/g/glog"
"sort"
"strings"
"sync/atomic"
"github.com/asaskevich/govalidator"
"github.com/camry/g/gutil"
"github.com/golang-module/carbon/v2"
"gorm.io/gorm"
)
type Converter struct {
serverDbConfig *DbConfig
serverDb *gorm.DB
serverTable *Table
ignoreTable *IgnoreTable
serverTableColumns []string
serverTableColumnMap map[string]*MySQL2SQLiteColumn
}
type MySQL2SQLiteColumn struct {
DataType string
SQLiteDataType string
}
// NewConverter 新建转换器。
func NewConverter(serverDbConfig *DbConfig, serverDb *gorm.DB, serverTable *Table, ignoreTable *IgnoreTable) *Converter {
return &Converter{
serverDbConfig: serverDbConfig,
serverDb: serverDb,
serverTable: serverTable,
ignoreTable: ignoreTable,
serverTableColumnMap: make(map[string]*MySQL2SQLiteColumn),
}
}
// Start 启动。
func (c *Converter) Start() {
defer wg.Done()
ch <- true
switch c.serverTable.TableType {
case "BASE TABLE":
c.create()
c.insert()
case "VIEW":
// glog.Warnf("表 `%s` 不支持 VIEW 转换。", c.serverTable.TableName)
}
<-ch
}
// create SQLite CREATE TABLE 语句。
func (c *Converter) create() {
var (
serverColumnData []Column
serverStatisticsData []Statistic
)
serverTableColumnResult := c.serverDb.Table("COLUMNS").Order("`ORDINAL_POSITION` ASC").Find(
&serverColumnData,
"`TABLE_SCHEMA` = ? AND `TABLE_NAME` = ?",
c.serverDbConfig.Database, c.serverTable.TableName,
)
if serverTableColumnResult.RowsAffected > 0 {
serverStatisticsResult := c.serverDb.Table("STATISTICS").Find(
&serverStatisticsData,
"`TABLE_SCHEMA` = ? AND `TABLE_NAME` = ?",
c.serverDbConfig.Database, c.serverTable.TableName,
)
var createTableSql, createTableColumnSql, createUniqueIndexSql []string
createTableSql = append(createTableSql, fmt.Sprintf("DROP TABLE IF EXISTS `%s`;", c.serverTable.TableName))
createTableSql = append(createTableSql, fmt.Sprintf("CREATE TABLE `%s` (", c.serverTable.TableName))
// COLUMNS ...
for _, serverColumn := range serverColumnData {
if gutil.InArray(serverColumn.ColumnName, c.ignoreTable.Columns) {
continue
}
dataType := strings.ToUpper(serverColumn.DataType)
sqliteDataType := c.getDataType(dataType)
createSql := fmt.Sprintf(" `%s` %s%s",
serverColumn.ColumnName,
sqliteDataType,
c.getNotNull(serverColumn.IsNullable),
)
createTableColumnSql = append(createTableColumnSql, createSql)
c.serverTableColumns = append(c.serverTableColumns, serverColumn.ColumnName)
c.serverTableColumnMap[serverColumn.ColumnName] = &MySQL2SQLiteColumn{
DataType: dataType,
SQLiteDataType: sqliteDataType,
}
}
// KEY ...
if serverStatisticsResult.RowsAffected > 0 {
var serverStatisticIndexNameArray []string
serverStatisticsDataMap := make(map[string]map[int]Statistic)
for _, serverStatistic := range serverStatisticsData {
if _, ok := serverStatisticsDataMap[serverStatistic.IndexName]; ok {
serverStatisticsDataMap[serverStatistic.IndexName][serverStatistic.SeqInIndex] = serverStatistic
} else {
serverSeqInIndexStatisticMap := make(map[int]Statistic)
serverSeqInIndexStatisticMap[serverStatistic.SeqInIndex] = serverStatistic
serverStatisticsDataMap[serverStatistic.IndexName] = serverSeqInIndexStatisticMap
}
if !gutil.InArray(serverStatistic.IndexName, serverStatisticIndexNameArray) {
serverStatisticIndexNameArray = append(serverStatisticIndexNameArray, serverStatistic.IndexName)
}
}
for _, serverIndexName := range serverStatisticIndexNameArray {
if 1 != serverStatisticsDataMap[serverIndexName][1].NonUnique {
if serverIndexName == "PRIMARY" {
createTableColumnSql = append(createTableColumnSql, fmt.Sprintf(" %s", c.getPrimaryKey(serverStatisticsDataMap[serverIndexName])))
} else {
createUniqueIndexSql = append(createUniqueIndexSql, c.createUniqueKey(serverIndexName, serverStatisticsDataMap[serverIndexName]))
}
}
}
}
if len(createTableColumnSql) > 0 {
createTableSql = append(createTableSql, strings.Join(createTableColumnSql, ",\n"))
}
createTableSql = append(createTableSql, ");")
if len(createUniqueIndexSql) > 0 {
createTableSql = append(createTableSql, fmt.Sprintf("%s", strings.Join(createUniqueIndexSql, "\n")))
}
insertSql := c.insert()
if len(insertSql) > 0 {
createTableSql = append(createTableSql, fmt.Sprintf("%s", strings.Join(insertSql, "\n")))
}
lock.Lock()
sqlTableNames = append(sqlTableNames, c.serverTable.TableName)
sqlTableMap[c.serverTable.TableName] = strings.Join(createTableSql, "\n")
lock.Unlock()
}
}
// insert SQLite INSERT INTO 语句。
func (c *Converter) insert() []string {
var (
insertSql []string
offset = 0
limit = 2000
)
for {
var rows []map[string]any
result := c.serverDb.Table(fmt.Sprintf("`%s`.`%s`", c.serverDbConfig.Database, c.serverTable.TableName)).Offset(offset).Limit(limit).Find(&rows)
if result.RowsAffected <= 0 {
break
}
var ks, kv []string
for _, columnName := range c.serverTableColumns {
ks = append(ks, fmt.Sprintf("`%s`", columnName))
}
for _, row := range rows {
var vs []string
for _, columnName := range c.serverTableColumns {
if col, ok := c.serverTableColumnMap[columnName]; ok {
if columnValue, ok1 := row[columnName]; ok1 {
if columnValue == nil {
vs = append(vs, "NULL")
} else {
switch col.SQLiteDataType {
case "INTEGER", "REAL":
vs = append(vs, govalidator.ToString(columnValue))
case "TEXT":
switch col.DataType {
case "DATE":
vs = append(vs, fmt.Sprintf("'%s'", carbon.Parse(govalidator.ToString(columnValue)).ToDateString()))
case "TIME":
vs = append(vs, fmt.Sprintf("'%s'", carbon.Parse(govalidator.ToString(columnValue)).ToTimeString()))
case "YEAR":
vs = append(vs, fmt.Sprintf("'%d'", carbon.Parse(govalidator.ToString(columnValue)).Year()))
case "DATETIME", "TIMESTAMP":
vs = append(vs, fmt.Sprintf("'%s'", carbon.Parse(govalidator.ToString(columnValue)).ToDateTimeString()))
default:
vs = append(vs, fmt.Sprintf("'%s'", strings.ReplaceAll(govalidator.ToString(columnValue), "'", "''")))
}
case "BLOB":
vs = append(vs, fmt.Sprintf("'%s'", govalidator.ToString(columnValue)))
}
}
}
}
}
kv = append(kv, fmt.Sprintf("(%s)", strings.Join(vs, ",")))
}
insertSql = append(insertSql, fmt.Sprintf("INSERT INTO %s (%s) VALUES %s;",
c.serverTable.TableName,
strings.Join(ks, ","),
strings.Join(kv, ","),
))
offset += limit
}
return insertSql
}
// getDataType SQLite 数据类型。
func (c *Converter) getDataType(dataType string) string {
switch dataType {
case "TINYINT", "SMALLINT", "MEDIUMINT", "INT", "INTEGER", "BIGINT":
return "INTEGER"
case "FLOAT", "DOUBLE", "DECIMAL":
return "REAL"
case "DATE", "TIME", "YEAR", "DATETIME", "TIMESTAMP", "CHAR", "VARCHAR", "TINYTEXT", "TEXT", "MEDIUMTEXT", "LONGTEXT":
return "TEXT"
case "TINYBLOB", "BLOB", "MEDIUMBLOB", "LONGBLOB":
return "BLOB"
}
return "TEXT"
}
// getNotNull SQLite NOT NULL 语句。
func (c *Converter) getNotNull(isNullAble string) string {
if isNullAble == "NO" {
return " NOT NULL"
}
return ""
}
// getPrimaryKey SQLite PRIMARY KEY 语句。
func (c *Converter) getPrimaryKey(statisticMap map[int]Statistic) string {
var seqInIndexSort []int
var columnNames []string
for seqInIndex := range statisticMap {
seqInIndexSort = append(seqInIndexSort, seqInIndex)
}
sort.Ints(seqInIndexSort)
for _, seqInIndex := range seqInIndexSort {
if gutil.InArray(statisticMap[seqInIndex].ColumnName, c.ignoreTable.Columns) {
glog.Fatalf(`PRIMARY KEY Column %s is not ignore.`, statisticMap[seqInIndex].ColumnName)
}
columnNames = append(columnNames, fmt.Sprintf("`%s`", statisticMap[seqInIndex].ColumnName))
}
return fmt.Sprintf("PRIMARY KEY (%s)", strings.Join(columnNames, ","))
}
// createUniqueKey SQLite CREATE UNIQUE INDEX 语句。
func (c *Converter) createUniqueKey(indexName string, statisticMap map[int]Statistic) string {
if idx, ok := existIndexMap[indexName]; ok {
atomic.AddInt32(idx, 1)
indexName = fmt.Sprintf("%s%d", indexName, atomic.LoadInt32(idx))
} else {
existIndexMap[indexName] = new(int32)
}
var seqInIndexSort []int
var columnNames []string
for seqInIndex := range statisticMap {
seqInIndexSort = append(seqInIndexSort, seqInIndex)
}
sort.Ints(seqInIndexSort)
for _, seqInIndex := range seqInIndexSort {
if gutil.InArray(statisticMap[seqInIndex].ColumnName, c.ignoreTable.Columns) {
glog.Fatalf(`UNIQUE INDEX Column %s is not ignore.`, statisticMap[seqInIndex].ColumnName)
}
columnNames = append(columnNames, fmt.Sprintf("`%s`", statisticMap[seqInIndex].ColumnName))
}
return fmt.Sprintf("CREATE UNIQUE INDEX `%s` ON `%s` (%s);", indexName, c.serverTable.TableName, strings.Join(columnNames, ","))
}