-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
454 lines (392 loc) · 13.5 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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/**
* sql语句生成器
*
* 此工具用于生成sql语句,返回值只有拼装好的sql语句
*/
'use strict';
/**
* 包装error对象
*/
const codeMap = {
8400: '参数错误'
};
function getError(code, table, calleeName) {
let msg = codeMap[code] || '未知错误';
throw new Error(`在执行生成表${table}的${calleeName}语句时,出现了${msg}`);
}
/**
* 判断是否是字符串、数字、布尔值、对象、数组,时间对象
*/
var plainObject = {};
function isString(str) {
return plainObject.toString.call(str).toLowerCase() === '[object string]';
}
function isNumber(num) {
return plainObject.toString.call(num).toLowerCase() === '[object number]';
}
function isBoolean(bool) {
return plainObject.toString.call(bool).toLowerCase() === '[object boolean]';
}
function isObject(obj) {
return plainObject.toString.call(obj).toLowerCase() === '[object object]';
}
function isArray(arr) {
return plainObject.toString.call(arr).toLowerCase() === '[object array]';
}
function isDate(arr) {
return plainObject.toString.call(arr).toLowerCase() === '[object date]';
}
/**
* 判断是否原始值,只允许字符串、数字、布尔值、时间对象和null
*/
function isOrigin(val) {
return isString(val) || isNumber(val) || isBoolean(val) || isDate(val) || val === null;
}
/**
* 构造条件字符串
*/
function constructCondition(stmt, table, darr) {
let condition = [];
if(isString(stmt)) {
// 字符串条件
condition = stmt;
} else if(isArray(stmt)) {
// 条件对象
for(let stmtItem of stmt) {
let conditionItem = [];
let keys = Object.keys(stmtItem);
for(let key of keys) {
// 遍历条件
let itm = stmtItem[key];
if (!isNaN(parseInt(key, 10))) {
// key 为数字,则以值为条件
conditionItem.push(itm);
} else {
let attrName = splitAttrName(key, table);
if(isOrigin(itm)) {
// 值类型
darr.push(itm);
conditionItem.push(`${attrName} = ?`);
} else if(isArray(itm)) {
// 数组类型
let fragment = [];
for(let val of itm) {
darr.push(val);
fragment.push('?');
}
fragment = fragment.join(' , ');
conditionItem.push(`${attrName} in (${fragment})`);
continue;
} else {
// 其他
continue;
}
}
}
condition.push(conditionItem.join(' AND '));
}
condition = condition.join(' OR ');
} else {
// 其他
return getError(8400, table, '生成条件对象');
}
return condition;
}
/**
* 拆分数据库字段值
*/
function splitAttrName(key, table) {
if (key.indexOf('(') >= 0 || key.indexOf('`') >= 0) {
// 不作拼装
return key;
}
let keyArr = key.split('.');
let keyTable = `\`${table}\``;
if(keyArr.length === 2) {
// 针对不同表的字段加表前缀
key = keyArr[1];
keyTable = `\`${keyArr[0]}\``;
}
key = `\`${key}\``;
return `${keyTable}.${key}`
}
class GenSql {
constructor() {}
/**
* 生成增语句
*/
add(obj) {
let data = obj.data; // 要添加的数据,可为数组
let table = obj.table; // 表名
if(!data || (!isArray(data) && !isObject(data)) || !table) return getError(8400, table, 'add');
data = isArray(data) ? data : [data];
let name = []; // 存放需插入字段名
let dbName = []; // 存放需插入的数据库字段名
let q = []; // 存放需要插入的值
let attrs = Object.keys(data[0]);
let darr = []; // 插入数据
for(let attr of attrs) {
// 遍历添加字段
name.push(attr);
dbName.push(`\`${attr}\``);
}
for(let itm of data) {
// 获取插值
let tmpQ = [];
for(let attr of name) {
if(!isOrigin(itm[attr])) {
// 存在非原始类型的值
return getError(8401, table, 'add');
}
tmpQ.push('?');
darr.push(itm[attr]);
}
q.push(tmpQ.join(' , '));
}
dbName = dbName.join(' , ');
if(q.length > 1) {
q = '(' + q.join(') , (') + ')';
} else {
q = `( ${q[0]} )`;
}
return {
sql: `INSERT INTO \`${table}\` ( ${dbName} ) VALUES ${q}`,
data: darr
};
}
/**
* 生成删语句
*/
del(obj) {
let table = obj.table; // 表名
let stmt = isObject(obj.condition) ? [obj.condition] : obj.condition; // 条件
if(!table) return getError(8400, table, 'del');
let condition; // 存放条件
let darr = []; // 插入数据
// 条件
if(!stmt) condition = '';
else {
condition = constructCondition(stmt, table, darr);
condition = `WHERE ${condition}`;
}
return {
sql: `DELETE FROM \`${table}\` ${condition}`,
data: darr
};
}
/**
* 生成改语句
*/
update(obj) {
let data = obj.data; // 要修改的数据
let table = obj.table; // 表名
let stmt = isObject(obj.condition) ? [obj.condition] : obj.condition; // 条件
if(!data || !isObject(data) || !table) return getError(8400, table, 'update');
let name = [];
let condition; // 获取条件
let darr = []; // 插入数据
let keys = Object.keys(data);
if(!keys.length) return getError(8400, table, 'update');
// 更新字段
for(let key of keys) {
let itm = data[key];
let dbkey = key;
if(isOrigin(itm)) {
// 值类型
darr.push(itm);
name.push(`\`${dbkey}\` = ?`);
} else {
// 其他
continue;
}
}
name = name.join(' , ');
// 条件
if(!stmt) condition = '';
else {
condition = constructCondition(stmt, table, darr);
condition = `WHERE ${condition}`;
}
return {
sql: `UPDATE \`${table}\` SET ${name} ${condition}`,
data: darr
};
}
/**
* 生成获取语句
*/
find(obj) {
if(isArray(obj)) {
// 针对传入数组,做union操作
let sqlArr = [];
let dataArr = [];
for(let item of obj) {
let ret = this.find(item);
sqlArr.push(ret.sql);
dataArr = dataArr.concat(ret.data);
}
return {
sql: sqlArr.join(' UNION '),
data: dataArr
};
}
let table = obj.table; // 表名
let stmt = !obj.condition ? [] : isObject(obj.condition) ? [obj.condition] : obj.condition; // 条件
let alias = obj.alias; // 表的别名映射
// 取字段,值只可为对象
// 其对象结构类似{map:{'xx_yy': 'xxYy'}, attrs: ['xx', 'yy']}
let field = obj.field;
// 连接,值可为对象或对象数组
// 其对象结构类似{table: 'xxx', type: 'right|inner|full|left(默认值,左外连接)', on: 'xxx'}
let join = obj.join;
join = isObject(join) ? [join] : join;
// 分组,值只可为对象
// 器对象结构类似{attr: 'xxx'|['xxx'], having: 'sss'}
let group = obj.group;
// 搜索,值可为对象或对象数组
// 其对象结构类似{value: 'xxx', type: 'front|back|all(默认值,前后补%)', attr: 'xxx'|['xxx']}
let like = obj.like;
like = isObject(like) ? [like] : like;
let likeType = obj.likeType && obj.likeType.toLowerCase() === 'and' ? ' AND ' : ' OR '; // 搜索的连接方式,默认使用or
// 排序,值可为对象或对象数组
// 其对象结构类似{attr: 'xxx', type: 'desc|asc(默认值,升序)'}
let order = obj.order;
order = isObject(order) ? [order] : order;
// 分页
let limit = obj.limit; // 单页数量
let offset = obj.offset; // 偏移量
let name = [];
let condition = []; // 获取条件
let darr = []; // 插入数据
// 字段
if(isObject(field)) {
// 传入字段对象
let attrs = field.attr;
let map = field.map;
if(isArray(attrs)) {
// 无别名字段
attrs = attrs.map(function(attr) {
return splitAttrName(attr, table);
});
name = name.concat(attrs);
}
if(isObject(map)) {
// 有别名字段
let keys = Object.keys(map);
for(let key of keys) {
name.push(`${splitAttrName(key, table)} AS \`${map[key]}\``);
}
}
} else if (isString(field)) {
// 传入字段字符串
name.push(field);
} else {
// 选择所有字段
name.push('*');
}
name = name.join(' , ');
let sql = `SELECT ${name} FROM \`${table}\``;
// 主表别名
if(alias && alias[table]) {
sql += ` AS \`${alias[table]}\``;
}
// 连接
if(isArray(join)) {
let joinArr = [];
for(let itm of join) {
// 遍历join对象
let joinTable = itm.table;
let on = itm.on;
let type = itm.type && itm.type.toLowerCase() || 'left';
type = type === 'right' ? 'RIGHT JOIN' : type === 'inner' ? 'INNER JOIN' : type === 'full' ? 'FULL JOIN' : 'LEFT JOIN';
let fragment = `${type} \`${joinTable}\``;
if(alias && alias[joinTable]) fragment += ` AS \`${alias[joinTable]}\``; // 连接表的别名
// 连接条件
if(isString(on)) {
// 字符串条件
fragment += ` ON ${on}`;
} else if(isObject(on)) {
// 对象条件
let tmpOnArr = [];
if(on.def && isString(on.def)) tmpOnArr.push(on.def);
if(on.condition) {
on.condition = isObject(on.condition) ? [on.condition] : on.condition;
tmpOnArr.push(constructCondition(on.condition, table, darr));
}
tmpOnArr = tmpOnArr.join(' AND ');
fragment += ` ON ${tmpOnArr}`;
}
joinArr.push(fragment);
}
joinArr = joinArr.join(' ');
if(joinArr) {
sql += ` ${joinArr}`;
}
}
// 条件
condition = constructCondition(stmt, table, darr);
// 搜索
if(isArray(like)) {
condition = condition ? [condition] : [];
for(let itm of like) {
let attrs = itm.attr;
let type = (itm.type || 'all').toLowerCase();
let val = itm.value;
let likeCondition = [];
attrs = isArray(attrs) ? attrs : [attrs];
for(let attr of attrs) {
// 遍历like对象中的attr字段
let fragment = type === 'front' ? `%${val}` : type === 'back' ? `${val}%` : `%${val}%`;
likeCondition.push(`${splitAttrName(attr, table)} LIKE "${fragment}"`);
}
condition.push(likeCondition.join(likeType));
}
condition = condition.join(likeType);
}
if(condition && isString(condition)) {
sql += ` WHERE ${condition}`;
}
// 分组
if(isObject(group)) {
let attrs = group.attr;
let having = group.having;
attrs = isArray(attrs) ? attrs : [attrs];
attrs = attrs.map(function(attr) {
return splitAttrName(attr, table);
});
attrs = attrs.join(' , ');
sql += ` GROUP BY ${attrs}`;
if(having) {
sql += ` HAVING ${having}`;
}
}
// 排序
if(isArray(order)) {
let orderArr = [];
for(let itm of order) {
// 遍历order对象
let attr = itm.attr;
let type = itm.type && itm.type.toLowerCase() === 'desc' ? 'DESC' : 'ASC';
orderArr.push(`${splitAttrName(attr, table)} ${type}`);
}
orderArr = orderArr.join(' , ');
if(orderArr) {
sql += ` ORDER BY ${orderArr}`;
}
}
// 分页
if(isNumber(limit)) {
darr.push(limit);
sql += ` LIMIT ?`;
}
if(isNumber(offset)) {
darr.push(offset);
sql += ` OFFSET ?`
}
return {
sql,
data: darr
};
}
};
module.exports = GenSql;