/
sql2struct.js
188 lines (183 loc) · 5.97 KB
/
sql2struct.js
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
new Vue({
el: '#app',
data() {
return {
cache: null,
sqlContent: '',
structContent: '',
activeIndex: '1',
typeMap: '',
typeMapStr: '',
useGorm: true,
useJson: true,
dialogFormVisible: false
}
},
created() {
var message = {
act: 'getOptions'
}
var that = this
// 获取缓存数据
chrome.runtime.sendMessage(message, function(res) {
if (!res) { // 不存在缓存数据
// 初始配置数据
var data = {
useGorm: this.useGorm,
useJson: this.useJson,
typeMap: getTypeMap()
}
that.setCache(data)
return
}
var obj = JSON.parse(res)
if (obj.useGorm != undefined) {
that.useGorm = obj.useGorm
}
if (obj.useJson != undefined) {
that.useJson = obj.useJson
}
if (obj.typeMap != undefined) {
for (var k in obj.typeMap) {
that.typeMapStr += k + ': ' + obj.typeMap[k] + '\n'
}
}
})
},
watch: {
sqlContent(val) {
if (!val) {
this.structContent = ''
return
}
var res = val.match(/\`[\w_]+\`\s+[\w_\(\)]+(\s+|\,)/g)
if (!res) {
this.structContent = 'invalid sql'
return
}
var types = this.typeMap
var structResult = 'type '
for (var i = 0, len = res.length; i < len; i++) {
var field = res[i].match(/\`(.+)\`\s+(tinyint|smallint|int|mediumint|bigint|float|double|decimal|varchar|char|text|mediumtext|longtext|datetime|time|date|enum|set|blob)?/)
if (i == 0) { // 第一个字段为数据表名称
if (field && field[1] != undefined && field[2] == undefined) {
var tbName = titleCase(field[1])
structResult += tbName + ' struct {'
continue
} else {
return
}
} else { // 数据表字段
if (field && field[1] != undefined && field[2] != undefined) {
if (types[field[2]] != undefined) {
var fieldName = titleCase(field[1])
var fieldType = types[field[2]]
var fieldJsonName = field[1].toLowerCase()
if (fieldName.toLowerCase() == 'id') {
fieldName = 'ID'
}
structResult += '\n\t' + fieldName + ' ' + fieldType
if (this.useGorm) {
structResult += ' `gorm:"column:'+ fieldJsonName +'"'
if (this.useJson) {
structResult += ' json:"' + fieldJsonName + '"'
}
structResult += '`'
} else if (this.useJson) {
structResult += ' `json:"' + fieldJsonName + '"`'
}
} else {
continue
}
} else {
continue
}
}
}
structResult += '\n}'
this.structContent = structResult
},
typeMapStr(val) {
var typeArr = val.split('\n')
var typeMap = {}
for (var i = 0, len = typeArr.length; i < len; i++) {
var itemArr = typeArr[i].split(/\:\s+/)
if (itemArr[0] != undefined && itemArr[1] != undefined) {
typeMap[itemArr[0]] = itemArr[1]
}
}
this.typeMap = typeMap
var data = {
useGorm: this.useGorm,
useJson: this.useJson,
typeMap: this.typeMap
}
this.setCache(data)
},
useGorm(val) {
this.useGorm = val
var data = {
useGorm: this.useGorm,
useJson: this.useJson,
typeMap: this.typeMap
}
this.setCache(data)
},
useJson(val) {
this.useJson = val
var data = {
useGorm: this.useGorm,
useJson: this.useJson,
typeMap: this.typeMap
}
this.setCache(data)
}
},
methods: {
handleSelect(key, keyPath) {
},
setCache(data) {
var message = {
act: 'setOptions',
data: JSON.stringify(data)
}
chrome.runtime.sendMessage(message, function(res) {
//console.log(res)
})
}
}
})
// 首字母大写
function titleCase(str) {
var array = str.toLowerCase().split("_");
for (var i = 0; i < array.length; i++){
array[i] = array[i][0].toUpperCase() + array[i].substring(1, array[i].length);
}
var string = array.join("");
return string;
}
// 类型映射
function getTypeMap() {
return {
'tinyint': 'int64',
'smallint': 'int64',
'int': 'int64',
'mediumint': 'int64',
'bigint': 'int64',
'float': 'float64',
'double': 'float164',
'decimal': 'float64',
'char': 'string',
'varchar': 'string',
'text': 'string',
'mediumtext': 'string',
'longtext': 'string',
'time': 'time.Time',
'date': 'time.Time',
'datetime': 'time.Time',
'timestramp': 'int64',
'enum': 'string',
'set': 'string',
'blob': 'string'
}
}