-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
329 lines (324 loc) · 9.02 KB
/
index.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
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/* global $ */
'use strict'
if (!global.isMeekoLoad) {
global.$ = require('meeko') // 作为全局 不在加载,否则会加载两次
}
const co = require('co')
const pack = require('./package.json')
const DbOpt = function (mysql, tbName, field, exColumn) {
const me = this
let sql = ''
const _name = tbName
/*
TODO: 列可见性
let ex = exColumn || {'d_flag': 1, 'm_time': 1, 'w_state': 1, 'name': 1}
let tableField = field */
function _log (sql, ifShowSql) {
if (ifShowSql) {
$.option.logTime = false
$.log(sql)
$.option.logTime = true
}
}
/*
TODO: 列可见性
function _columnFilter (colObj) {
let o = {}
tableField.map(item => {
if (!ex[item]) o[item] = 1
})
for (let i in colObj) {
if (colObj[i] === 1 && ex[i] === 1) o[i] = 1
}
return o
} */
me.get = function () {
// 返回生成的sql
const s = sql
sql = ''
return s
}
me.cmd = function (s) {
const _tail = ~s.indexOf(';') ? '' : ';'
sql += s + _tail
return me
}
me.exec = function * (ifTrans, ifShowSql) {
let sql = me.get()
let r
if (ifTrans) {
try {
sql = `begin;${sql}commit;`
_log(sql, ifShowSql)
r = yield mysql.query(sql)
return r
} catch (e) {
yield mysql.query('rollback;')
const err = e.toString()
new RegExp('(.+)', 'gm').test(err)
const light = RegExp.$1
// NOTICE:不要使用ctrl+alt+F 格式化代码
$.err(
sql.replace(light, `${$.c.r(light)}`),
`\n${err.replace(/('.+')/gm, `${$.c.y(RegExp.$1)}`)}`
)
$.log(`${$.c.g('Rollback')}`)
return -1
}
} else {
try {
_log(sql, ifShowSql)
return yield mysql.query(sql)
} catch (e) {
const err = e.toString()
new RegExp("'([^']+)'", 'gm').test(err)
const light = RegExp.$1
// NOTICE:不要使用ctrl+alt+F 格式化代码
$.err(
sql.replace(light, `${$.c.r(light)}`),
`\n${err.replace(/('[^']+')/gm, `${$.c.y(RegExp.$1)}`)}`
)
return -1
}
}
}
me.run = co.wrap(function * (ifTrans, ifShowSql) {
return yield me.exec(ifTrans, ifShowSql)
})
me.where = function (o, type) {
let _item
const a = []
for (const i in o) {
switch (typeof o[i]) {
case 'string': {
let _pre = "'"
; /[0-9a-zA-z_]+\(.+\)/g.test(o[i]) && (_pre = '') // NOTICE: 注意前面的分号
_item = `\`${i}\`=${_pre}${o[i]}${_pre}`
break
}
case 'number':
_item = `\`${i}\`=${o[i]}`
break
case 'boolean':
_item = `\`${i}\`=${o[i]}`
break
case 'object': {
if (type === 'update') {
const _preStr = o[i] instanceof Date ? '' : "'"
_item = `\`${i}\` = ${o[i] ? _preStr + (JSON.stringify(o[i]) + _preStr) : 'NULL'
}`
break
}
if (!o[i]) {
// NOTICE: 不能严格等于
_item = `\`${i}\` is NULL`
break
}
if (o[i] instanceof Date) {
_item = `\`${i}\`='${o[i].date2Str()}'`
break
}
if (o[i] instanceof Array) {
_item = `\`${i}\` in ${JSON.stringify(o[i])
.replaceAll('[', '(')
.replaceAll(']', ')')
.replaceAll('"', "'")}`
break
}
if (o[i] instanceof RegExp) {
_item = `\`${i}\` like '${o[i]
.toString()
.replaceAll('/g', '')
.replaceAll('/', '')}'`
break
}
const _objAry = []
for (const i2 in o[i]) {
const _pre1 = typeof o[i][i2] === 'string' ? "'" : ''
const _pre2 = /[0-9a-zA-z_]+\(.+\)/g.test(i) ? '' : '`'
_objAry.push(`${_pre2}${i}${_pre2}${i2}${_pre1}${o[i][i2]}${_pre1}`)
}
_item = _objAry.join(' and ')
break
}
case 'undefined':
_item = `\`${i}\` = NULL`
break
default:
}
a.push(_item)
}
return a
}
me.find = function (a, b, c, d) {
/*
a where
b col
c order by
d limit
*/
const cols = []
let colsStr = ''
let whereStr = ''
const order = []
let orderStr = ''
const limitStr = +d
for (const i in c) {
order.push('`' + i + '`' + ` ${+c[i] === -1 ? 'desc' : 'asc'}`)
}
orderStr = order.join(', ')
// if (b === 0) b = _columnFilter(b) //TODO: 列可见性加强
for (const i in b) {
cols.push('`' + i + '`')
}
colsStr = cols.join(',')
whereStr = me.where(a).join(' and ')
sql += `select ${colsStr || '*'} from \`${_name}\`${whereStr ? ' where ' + whereStr : ''
}${orderStr ? ' order by ' + orderStr : ''}${limitStr ? ' limit ' + limitStr : ''
};`
return me
}
me.findOne = function (a, b, c) {
return me.find(a, b, c, 1)
}
me.remove = function (a, ifEmpty) {
const whereStr = me.where(a).join(' and ')
if (whereStr || (ifEmpty && !whereStr)) {
sql += `delete from \`${_name}\`${whereStr ? ' where ' + whereStr : ''};`
return me
} else {
sql += '[Empty!!]'
return me
}
}
me.update = function (a, b, ifEmpty) {
const whereStr = me.where(a).join(' and ')
const colsStr = me.where(b, 'update').join(',')
if (!colsStr) {
sql += '[Empty!!]'
return me
}
if (whereStr || (ifEmpty && !whereStr)) {
sql += `update \`${_name}\` set ${colsStr}${whereStr ? ' where ' + whereStr : ''
};`
return me
} else {
sql += '[Empty!!]'
return me
}
}
me.insert = function (a, uniqCol) {
const cols = []
let values = []
const vals = []
let colsStr = ''
let valuesStr = ''
for (const i in a) {
cols.push('`' + i + '`')
values.push(a[i])
}
colsStr = cols.join(',')
values = me.where(a, 'update')
for (let i = 0; i < values.length; i++) {
const a = values[i].split('=')
a.shift(0)
vals.push(a.join('='))
}
valuesStr = vals.join(',')
if (!colsStr && !valuesStr) {
sql += '[Empty!!]'
} else {
if (!uniqCol) {
sql += `insert into \`${_name}\` (${colsStr}) values (${valuesStr});`
} else {
const v = typeof a[uniqCol] === 'string' ? `'${a[uniqCol]}'` : +a[uniqCol]
sql += `insert into \`${_name}\` (${colsStr}) select ${valuesStr} from dual WHERE NOT EXISTS(SELECT \`${uniqCol}\` FROM \`${_name}\` WHERE \`${uniqCol}\` = ${v}) limit 1;`
}
}
return me
}
me.C = function (a, uniqCol) {
return me.insert(a, uniqCol)
}
me.R = function (a, b, c, d) {
return me.find(a, b, c, d)
}
me.U = function (a, b, ifEmpty) {
return me.update(a, b, ifEmpty)
}
me.D = function (a, ifEmpty) {
return me.remove(a, ifEmpty)
}
me.select = function (a, b, c, d) {
return me.find(a, b, c, d)
}
me.delete = function (a, ifEmpty) {
return me.remove(a, ifEmpty)
}
return me
}
function getDB (dbObj) {
const dbName = dbObj.database || 'test'
const exColumn = dbObj.exColumn
const [mysqlWrapper, Mysql] = [require('co-mysql'), require('promise-mysql')]
const pool = Mysql.createPool(dbObj)
const mysql = mysqlWrapper(pool)
$.option.logTime = false
pool.on('connection', function () {
// $.log(`<-- J2sql (${pack.version}) [${$.c.yellow}${dbObj.host} : ${dbObj.port}${$.c.none}] pool connect!`)
})
pool.on('enqueue', function () {
// $.log('<-- J2sql pool enqueue!')
})
// $.log('--> J2sql Obj Init start...')
function finishLoadDB (n, mysql, _name, _field, exColumn) {
$.log(
$.c.g('✔'),
`J2sql (${pack.version}) [${$.c.y(
`${dbObj.host} : ${dbObj.port}`
)}] [${$.c.y(n)}] tables`
)
db._mysql = pool
db.cmd = new DbOpt(mysql, _name, _field, exColumn).cmd
}
let [_r, n] = [0, 0]
const db = {}
co(function * () {
_r = yield mysql.query(`use \`${dbName}\`;show tables;`)
const tableSize = _r[1].length
let unLoadTable = tableSize
if (tableSize === 0) {
$.log($.c.r('✘'), `J2sql (${pack.version}) [${$.c.y(0)} tables]`)
return
}
_r[1].forEach(function (item) {
const _name = item['Tables_in_' + dbName]
db[_name] = {}
co(function * () {
const _field = []
const _type = []
; (yield mysql.query(`desc \`${_name}\`;`)).map(item => {
_field.push(item.Field)
_type.push(item.Type)
})
$.ext(db[_name], new DbOpt(mysql, _name, _field, exColumn))
db[_name].field = _field
db[_name].type = _type
unLoadTable--
db._nowPercent = ~~(((tableSize - unLoadTable) / tableSize) * 100)
// $.log('DB Obj loading =>', db['_nowPercent'], '%')
if (unLoadTable <= 0) {
finishLoadDB(n, mysql, _name, _field, exColumn)
} else {
}
})
n++
})
})
.then(function () { })
.catch(function (e) {
$.log($.c.r('✘'), `Mysql: ${e.message}`)
})
return db
}
module.exports = getDB