-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
268 lines (219 loc) · 6.79 KB
/
main.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
package main
import (
"encoding/json"
"fmt"
"sort"
"strings"
)
func main() {
fmt.Println("hello world")
}
type OplogEntry struct {
Op string `json:"op"`
NS string `json:"ns"`
O map[string]interface{} `json:"o"`
O2 map[string]interface{} `json:"o2"`
}
func GenerateSQL(oplog string) ([]string, error) {
sqls := []string{}
var oplogEntries []OplogEntry
if err := json.Unmarshal([]byte(oplog), &oplogEntries); err != nil {
var oplogObj OplogEntry
if err := json.Unmarshal([]byte(oplog), &oplogObj); err != nil {
return sqls, err
}
oplogEntries = append(oplogEntries, oplogObj)
}
cacheMap := make(map[string]bool)
for _, oplogEntry := range oplogEntries {
innerSqls, err := generateSQL(oplogEntry, cacheMap)
if err != nil {
return []string{}, err
}
sqls = append(sqls, innerSqls...)
}
return sqls, nil
}
func generateSQL(oplogObj OplogEntry, cacheMap map[string]bool) ([]string, error) {
sqls := []string{}
switch oplogObj.Op {
case "i":
// Create schema
nsParts := strings.Split(oplogObj.NS, ".")
schemaName := nsParts[0]
if exits := cacheMap[schemaName]; !exits {
sqls = append(sqls, generateCreateSchemaSQL(schemaName))
cacheMap[schemaName] = true
}
// Create table
if exits := cacheMap[oplogObj.NS]; !exits {
sqls = append(sqls, generateCreateTableSQL(oplogObj, cacheMap))
cacheMap[oplogObj.NS] = true
} else if isEligibleForAlterTable(oplogObj, cacheMap) {
sqls = append(sqls, generateAlterTableSQL(oplogObj, cacheMap))
}
sql, err := generateInsertSQL(oplogObj)
if err != nil {
return sqls, err
}
sqls = append(sqls, sql)
case "u":
sql, err := generateUpdateSQL(oplogObj)
if err != nil {
return sqls, err
}
sqls = append(sqls, sql)
case "d":
sql, err := generateDeleteSQL(oplogObj)
if err != nil {
return sqls, err
}
sqls = append(sqls, sql)
}
return sqls, nil
}
func generateCreateSchemaSQL(schemaName string) string {
return fmt.Sprintf("CREATE SCHEMA %s;", schemaName)
}
func generateCreateTableSQL(oplogObj OplogEntry, cacheMap map[string]bool) string {
var sb strings.Builder
sb.WriteString(fmt.Sprintf("CREATE TABLE %s (", oplogObj.NS))
columnNames := getColumnNames(oplogObj.O)
sep := ""
for _, columnName := range columnNames {
value := oplogObj.O[columnName]
colDataType := getColumnSQLDataType(columnName, value)
cacheKey := fmt.Sprintf("%s.%s", oplogObj.NS, columnName)
cacheMap[cacheKey] = true
sb.WriteString(fmt.Sprintf("%s%s %s", sep, columnName, colDataType))
sep = ", "
}
sb.WriteString(");")
return sb.String()
}
func isEligibleForAlterTable(oplogObj OplogEntry, cacheMap map[string]bool) bool {
for columnName := range oplogObj.O {
cacheKey := fmt.Sprintf("%s.%s", oplogObj.NS, columnName)
if !cacheMap[cacheKey] {
return true
}
}
return false
}
func generateAlterTableSQL(oplogObj OplogEntry, cacheMap map[string]bool) string {
var sb strings.Builder
sb.WriteString(fmt.Sprintf("ALTER TABLE %s", oplogObj.NS))
sep := " "
for columnName := range oplogObj.O {
value := oplogObj.O[columnName]
columnDataType := getColumnSQLDataType(columnName, value)
cacheKey := fmt.Sprintf("%s.%s", oplogObj.NS, columnName)
if !cacheMap[cacheKey] {
sb.WriteString(fmt.Sprintf("%sADD COLUMN %s %s", sep, columnName, columnDataType))
sep = ", "
}
}
sb.WriteString(";")
return sb.String()
}
func generateInsertSQL(oplogObj OplogEntry) (string, error) {
switch oplogObj.Op {
case "i":
sql := fmt.Sprintf("INSERT INTO %s", oplogObj.NS)
columnNames := make([]string, 0, len(oplogObj.O))
for columnName := range oplogObj.O {
columnNames = append(columnNames, columnName)
}
sort.Strings(columnNames)
columnValues := make([]string, 0, len(oplogObj.O))
for _, columnName := range columnNames {
columnValues = append(columnValues, getColumnValue(oplogObj.O[columnName]))
}
sql = fmt.Sprintf("%s (%s) VALUES (%s);", sql, strings.Join(columnNames, ", "), strings.Join(columnValues, ", "))
return sql, nil
}
return "", nil
}
func generateUpdateSQL(oplogObj OplogEntry) (string, error) {
switch oplogObj.Op {
case "u":
sql := fmt.Sprintf("UPDATE %s SET", oplogObj.NS)
diffMap, ok := oplogObj.O["diff"].(map[string]interface{})
if !ok {
return "", fmt.Errorf("invalid oplog")
}
if setMap, ok := diffMap["u"].(map[string]interface{}); ok {
columnValues := make([]string, 0, len(setMap))
for columnName, value := range setMap {
columnValues = append(columnValues, fmt.Sprintf("%s = %s", columnName, getColumnValue(value)))
}
sort.Strings(columnValues)
sql = fmt.Sprintf("%s %s", sql, strings.Join(columnValues, ", "))
} else if unsetMap, ok := diffMap["d"].(map[string]interface{}); ok {
columnValues := make([]string, 0, len(unsetMap))
for columnName := range unsetMap {
columnValues = append(columnValues, fmt.Sprintf("%s = NULL", columnName))
}
sort.Strings(columnValues)
sql = fmt.Sprintf("%s %s", sql, strings.Join(columnValues, ", "))
} else {
return "", fmt.Errorf("invalid oplog")
}
whereColumnValues := make([]string, 0, len(oplogObj.O2))
for columnName, value := range oplogObj.O2 {
whereColumnValues = append(whereColumnValues, fmt.Sprintf("%s = %s", columnName, getColumnValue(value)))
}
sql = fmt.Sprintf("%s WHERE %s;", sql, strings.Join(whereColumnValues, " AND "))
return sql, nil
}
return "", fmt.Errorf("invalid oplog")
}
func generateDeleteSQL(oplogObj OplogEntry) (string, error) {
switch oplogObj.Op {
case "d":
// DELETE FROM test.student WHERE _id = '635b79e231d82a8ab1de863b';
sql := fmt.Sprintf("DELETE FROM %s WHERE", oplogObj.NS)
whereColumnValues := make([]string, 0, len(oplogObj.O))
for columnName, value := range oplogObj.O {
whereColumnValues = append(whereColumnValues, fmt.Sprintf("%s = %s", columnName, getColumnValue(value)))
}
sql = fmt.Sprintf("%s %s;", sql, strings.Join(whereColumnValues, " AND "))
return sql, nil
}
return "", fmt.Errorf("invalid oplog")
}
func getColumnNames(data map[string]interface{}) []string {
columnNames := make([]string, 0, len(data))
for columnName := range data {
columnNames = append(columnNames, columnName)
}
sort.Strings(columnNames)
return columnNames
}
func getColumnSQLDataType(columnName string, value interface{}) string {
colDataType := ""
switch value.(type) {
case int, int8, int16, int32, int64:
colDataType = "INTEGER"
case float32, float64:
colDataType = "FLOAT"
case bool:
colDataType = "BOOLEAN"
default:
colDataType = "VARCHAR(255)"
}
if columnName == "_id" {
colDataType += " PRIMARY KEY"
}
return colDataType
}
func getColumnValue(value interface{}) string {
switch value.(type) {
case int, int8, int16, int32, int64, float32, float64:
return fmt.Sprintf("%v", value)
case bool:
return fmt.Sprintf("%t", value)
default:
return fmt.Sprintf("'%v'", value)
}
}