-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathsimpleSqlParser.withoutDeps.js
More file actions
720 lines (641 loc) · 16.6 KB
/
simpleSqlParser.withoutDeps.js
File metadata and controls
720 lines (641 loc) · 16.6 KB
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.simpleSqlParser = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
'use strict';
module.exports.sql2ast = require('./src/sql2ast.js');
module.exports.ast2sql = require('./src/ast2sql.js');
},{"./src/ast2sql.js":2,"./src/sql2ast.js":3}],2:[function(require,module,exports){
'use strict';
module.exports = function(astObject) {
/*if (typeof ast === 'object' && ast.status === true) ast = ast.value;
else return false;*/
if (typeof astObject !== 'object' || astObject.status !== true) return false;
function select(ast) {
var result = 'SELECT ';
result += ast.select.map(function(item) {
return item.expression;
}).join(', ');
return result;
}
function from(ast) {
var result = '';
if (ast.from.length > 0) {
result += 'FROM ';
result += ast.from.map(function(item) {
return item.expression;
}).join(', ');
}
return result;
}
function join(ast) {
return ast.join.map(function(item) {
var result = '';
if (item.type === 'inner') result += 'INNER JOIN ';
else if (item.type === 'left') result += 'LEFT JOIN ';
else if (item.type === 'right') result += 'RIGHT JOIN ';
else return '';
result += item.table;
if (item.alias !== null) result += ' AS ' + item.alias;
result += ' ON ';
result += item.condition.expression;
return result;
}).join(' ');
}
function where(ast) {
var result = '';
if (ast.where !== null) result += 'WHERE ' + ast.where.expression;
return result;
}
function group(ast) {
var result = '';
if (ast.group.length > 0) {
result += 'GROUP BY ';
result += ast.group.map(function(item) {
return item.expression;
}).join(', ');
}
return result;
}
function order(ast) {
var result = '';
if (ast.order.length > 0) {
result += 'ORDER BY ';
result += ast.order.map(function(item) {
return item.expression;
}).join(', ');
}
return result;
}
function limit(ast) {
var result = '';
if (ast.limit !== null) {
result += 'LIMIT ';
if (ast.limit.from !== null) result += ast.limit.from + ', ';
result += ast.limit.nb;
}
return result;
}
function into(ast) {
return 'INSERT INTO ' + ast.into.expression;
}
function values(ast) {
var result = '';
var targets = ast.values.filter(function(item) {
return item.target !== null;
});
if (targets.length > 0) {
result += '(';
result += targets.map(function(item) {
return item.target.expression;
}).join(', ');
result += ') ';
}
result += 'VALUES (';
result += ast.values.map(function(item) {
return item.value;
}).join(', ');
result += ')';
return result;
}
function table(ast) {
return 'UPDATE ' + ast.table.expression;
}
function update(ast) {
var result = 'SET ';
result += ast.values.map(function(item) {
return item.target.expression + ' = ' + item.value;
}).join(', ');
return result;
}
var ast = astObject.value;
var parts = [];
if (ast.type === 'select') {
parts.push(select(ast));
parts.push(from(ast));
parts.push(join(ast));
parts.push(where(ast));
parts.push(group(ast));
parts.push(order(ast));
parts.push(limit(ast));
}
else if (ast.type === 'insert') {
parts.push(into(ast));
parts.push(values(ast));
}
else if (ast.type === 'update') {
parts.push(table(ast));
parts.push(update(ast));
parts.push(where(ast));
}
else if (ast.type === 'delete') {
parts.push('DELETE');
parts.push(from(ast));
parts.push(where(ast));
}
else return false;
return parts.filter(function(item) {
return item !== '';
}).join(' ');
};
},{}],3:[function(require,module,exports){
(function (global){
'use strict';
var Parsimmon = (typeof window !== "undefined" ? window['parsimmon'] : typeof global !== "undefined" ? global['parsimmon'] : null);
/********************************************************************************************
ALIASES
********************************************************************************************/
var seq = Parsimmon.seq;
var alt = Parsimmon.alt;
var regex = Parsimmon.regex;
var string = Parsimmon.string;
var optWhitespace = Parsimmon.optWhitespace;
var whitespace = Parsimmon.whitespace;
var lazy = Parsimmon.lazy;
/********************************************************************************************
COMMON PATTERNS
********************************************************************************************/
// Make a parser optionnal
// "empty" parameter will be returned as result if the optionnal parser can't match
function opt(parser, empty) {
if (typeof empty == 'undefined') return parser.or(Parsimmon.succeed([]));
return parser.or(Parsimmon.succeed(empty));
}
// Join results of a parser
function mkString(node) {
return node.join('');
}
// Add an item to an optionnal list and return the final list
function mergeOptionnalList(node) {
node[0].push(node[1]);
return node[0];
}
// Generate a parser that accept a comma-separated list of something
function optionnalList(parser) {
return seq(
parser.skip(optWhitespace).skip(string(',')).skip(optWhitespace).many(),
parser.skip(optWhitespace)
).map(mergeOptionnalList);
}
// Remove first and last character of a string
function removeQuotes(str) {
return str.replace(/^([`'"])(.*)\1$/, '$2');
}
// Add the starting and ending char positions of matches of a given parser
function getPos(parser) {
return seq(
Parsimmon.index,
parser,
Parsimmon.index
).map(function(node) {
var pos = {
start: node[0],
end: node[2],
};
if (typeof node[1] == 'object') {
var n = node[1];
n.position = pos;
return n;
}
else {
pos.out = node[1];
return pos;
}
});
}
/********************************************************************************************
LOW LEVEL PARSERS
********************************************************************************************/
// The name of a column/table
var colName = alt(
regex(/(?!(FROM|WHERE|GROUP BY|ORDER BY|LIMIT|INNER|LEFT|RIGHT|JOIN|ON|VALUES|SET)\s)[a-z*][a-z0-9_:]*/i),
regex(/`[^`\\]*(?:\\.[^`\\]*)*`/)
);
// A string
var str = alt(
regex(/"[^"\\]*(?:\\.[^"\\]*)*"/),
regex(/'[^'\\]*(?:\\.[^'\\]*)*'/)
);
// A function expression
var func = seq(
alt(
regex(/[a-zA-Z0-9_]+\(/),
string('(')
),
/*eslint-disable no-use-before-define*/
opt(lazy(function() {
return argList;
})).map(mkString),
/*eslint-enable no-use-before-define*/
string(')')
).map(mkString);
// A table.column expression
var tableAndColumn = seq(
colName,
string('.'),
colName
);
// An operator
var operator = alt(
string('+'),
string('-'),
string('*'),
string('/'),
string('&&'),
string('&'),
string('~'),
string('||'),
string('|'),
string('^'),
regex(/XOR/i),
string('<=>'),
string('='),
string('!='),
string('>='),
string('>>'),
string('>'),
string('<='),
string('<<'),
string('<'),
regex(/IS NULL/i),
regex(/IS NOT/i),
regex(/IS NOT NULL/i),
regex(/IS/i),
regex(/LIKE/i),
regex(/NOT LIKE/i),
regex(/BETWEEN/i),
regex(/NOT BETWEEN/i),
string('%'),
regex(/MOD/i),
regex(/NOT/i),
regex(/OR\s/i), // A space is forced after so this doesn't get mixed up with ORDER BY
regex(/AND/i),
regex(/IN/i)
);
// A number
var number = regex(/[-]?\d+\.?\d*/);
/********************************************************************************************
EXPRESSION PARSERS
********************************************************************************************/
// List (following IN, for example)
var list = seq(
string('('),
optWhitespace,
seq(
alt(
number,
str
),
optWhitespace,
opt(string(',')),
optWhitespace,
opt(
alt(
number,
str
)
)
).map(mkString),
optWhitespace,
string(')')
).map(mkString);
// Expression
var expression = seq(
alt(
tableAndColumn.map(function(node) {
return {
expression: node.join(''),
table: removeQuotes(node[0]),
column: removeQuotes(node[2]),
};
}),
func.map(function(node) {
return {
expression: node,
table: null,
column: null,
};
}),
colName.map(function(node) {
return {
expression: node,
table: null,
column: removeQuotes(node),
};
}),
str.map(function(node) {
return {
expression: node,
table: null,
column: null,
};
}),
number.map(function(node) {
return {
expression: node,
table: null,
column: null,
};
}),
list.map(function(node) {
return {
expression: node,
table: null,
column: null,
};
})
),
opt(seq(
optWhitespace,
operator,
opt(seq(
optWhitespace,
lazy(function() {
return expression;
}).map(function(node) {
return node.expression;
})
).map(mkString), null)
).map(mkString), null)
).map(function(node) {
if (node[1] !== null) {
node[0] = node[0].expression;
return {
expression: node.join(''),
table: null,
column: null,
};
}
else return node[0];
});
// Expression following a SELECT statement
var colListExpression = seq(
expression,
opt( // Alias
seq(
optWhitespace,
opt(regex(/AS\s/i)),
alt(colName, str)
).map(function(node) {
var n = {};
n.alias = removeQuotes(node[2]);
n.expression = node.join('');
return n;
}),
null
)
).map(function(node) {
var n = node[0];
n.alias = (node[1] !== null) ? node[1].alias : null;
n.expression += ((node[1] !== null) ? node[1].expression : '');
return n;
});
// Expression inside a function
var argListExpression = expression.map(function(node) {
return node.expression;
});
// Expression following a FROM statement
var tableListExpression = seq(
alt(
tableAndColumn.map(mkString),
colName
),
opt( // Alias
seq(
optWhitespace,
opt(regex(/AS\s/i)),
alt(colName, str)
).map(function(node) {
return {
alias: removeQuotes(node[2]),
expression: node.join(''),
};
}),
null
)
).map(function(node) {
var n = {};
n.table = node[0];
n.alias = (node[1] !== null) ? node[1].alias : null;
n.expression = node[0] + ((node[1] !== null) ? node[1].expression : '');
return n;
});
// JOIN expression (including JOIN statements)
var joinExpression = seq(
opt(seq(
regex(/INNER|LEFT|RIGHT/i),
whitespace
).map(function(node) {
return node[0].toLowerCase();
}), null),
regex(/JOIN/i),
optWhitespace,
getPos(tableListExpression),
optWhitespace,
regex(/ON/i),
optWhitespace,
getPos(expression)
).map(function(node) {
var n = {};
n.type = node[0] || 'inner';
n.table = node[3].table;
n.alias = node[3].alias;
n.position = node[3].position;
n.condition = {
expression: node[7].expression,
position: node[7].position,
};
return n;
});
// Expression following a WHERE statement
var whereExpression = getPos(expression).map(function(node) {
return {
expression: node.expression,
position: node.position,
};
});
// Expression following an ORDER BY statement
var orderListExpression = seq(
expression,
opt(seq(
optWhitespace,
regex(/ASC|DESC/i)
), null)
).map(function(node) {
return {
expression: node[0].expression + ((node[1] !== null) ? node[1].join('') : ''),
order: (node[1] !== null) ? node[1][1] : 'ASC',
table: node[0].table,
column: node[0].column,
};
});
// Expression following a LIMIT statement
var limitExpression = seq(
number,
opt(seq(
optWhitespace,
string(','),
optWhitespace,
number
), null)
).map(function(node) {
if (node[1] === null) {
return {
from: null,
nb: parseInt(node[0], 10),
};
}
else {
return {
from: parseInt(node[0], 10),
nb: parseInt(node[1][3], 10),
};
}
});
// Expression designating a column before VALUES in INSERT query
var insertColListExpression = alt(
tableAndColumn.map(function(node) {
return {
expression: node.join(''),
column: removeQuotes(node[2]),
};
}),
colName.map(function(node) {
return {
expression: node,
column: removeQuotes(node),
};
})
);
// Expression following a VALUES statement
var valueExpression = expression.map(function(node) {
return node.expression;
});
// Expression that assign a value to a column
var assignExpression = seq(
insertColListExpression,
optWhitespace,
string('='),
optWhitespace,
expression
).map(function(node) {
return {
target: node[0],
value: node[4].expression,
};
});
/********************************************************************************************
HIGH LEVEL PARSERS
********************************************************************************************/
// List of arguments inside a function
var argList = seq(
seq(argListExpression, optWhitespace, string(','), optWhitespace).map(mkString).many(),
argListExpression.skip(optWhitespace)
).map(mergeOptionnalList);
// List of expressions following a SELECT statement
var colList = optionnalList(getPos(colListExpression));
// List of table following a FROM statement
var tableList = optionnalList(getPos(tableListExpression));
// List of table following an GROUP BY statement
var groupList = optionnalList(getPos(expression));
// List of table following an ORDER BY statement
var orderList = optionnalList(getPos(orderListExpression));
// List of joins (including JOIN statements)
var joinList = optWhitespace.then(joinExpression).skip(optWhitespace).many();
// List of columns before VALUES in INSERT query
var insertColList = optionnalList(insertColListExpression);
// List of values following a VALUES statement
var valuesList = optionnalList(valueExpression);
// List of assign expression following a SET statement
var assignList = optionnalList(assignExpression);
/********************************************************************************************
MAIN PARSERS
********************************************************************************************/
// SELECT parser
var selectParser = seq(
regex(/SELECT/i).skip(optWhitespace).then(opt(colList)),
opt(regex(/FROM/i).skip(optWhitespace).then(opt(tableList)), []),
opt(joinList),
opt(regex(/WHERE/i).skip(optWhitespace).then(opt(whereExpression)), null),
opt(regex(/\s?GROUP BY/i).skip(optWhitespace).then(opt(groupList))),
opt(regex(/\s?ORDER BY/i).skip(optWhitespace).then(opt(orderList))),
opt(regex(/\s?LIMIT/i).skip(optWhitespace).then(opt(limitExpression)), null)
).map(function(node) {
return {
type: 'select',
select: node[0],
from: node[1],
join: node[2],
where: node[3],
group: node[4],
order: node[5],
limit: node[6],
};
});
// INSERT parser
var insertParser = seq(
regex(/INSERT INTO/i).skip(optWhitespace).then(tableListExpression),
optWhitespace,
opt(
seq(
string('('),
insertColList,
string(')')
).map(function(node) {
return node[1];
})
),
optWhitespace,
regex(/VALUES\s?\(/i).skip(optWhitespace).then(valuesList),
string(')')
).map(function(node) {
var values = [];
var bigger = Math.max(node[2].length, node[4].length);
for (var i = 0; i < bigger; ++i) {
values[i] = {
target: node[2][i] || null,
value: node[4][i] || null,
};
}
return {
type: 'insert',
into: node[0],
values: values,
};
});
// UPDATE parser
var updateParser = seq(
regex(/UPDATE/i).skip(optWhitespace).then(tableListExpression),
optWhitespace,
regex(/SET/i).skip(optWhitespace).then(assignList),
optWhitespace,
opt(regex(/WHERE/i).skip(optWhitespace).then(opt(whereExpression)), null)
).map(function(node) {
return {
type: 'update',
table: node[0],
values: node[2],
where: node[4],
};
});
// DELETE parser
var deleteParser = seq(
regex(/DELETE FROM/i).skip(optWhitespace).then(opt(tableList)),
opt(regex(/WHERE/i).skip(optWhitespace).then(opt(whereExpression)), null)
).map(function(node) {
return {
type: 'delete',
from: node[0],
where: node[1],
};
});
// Main parser
var p = alt(selectParser, insertParser, updateParser, deleteParser);
/********************************************************************************************
PUBLIC FUNCTIONS
********************************************************************************************/
module.exports = function(sql) {
var result = p.parse(sql);
if (result.status === false) result.error = Parsimmon.formatError(sql, result);
return result;
};
}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
},{}]},{},[1])(1)
});