|
3 | 3 | // or with comments have manual edits.
|
4 | 4 | //
|
5 | 5 | {
|
| 6 | + function append(arr, x) { |
| 7 | + arr[arr.length] = x; |
| 8 | + return arr; |
| 9 | + } |
6 | 10 | function flatten(x, rejectSpace, acc) {
|
7 | 11 | acc = acc || [];
|
8 |
| - if ((x.length == undefined) || // Just an object, not a string or array. |
9 |
| - (!rejectSpace || |
10 |
| - (typeof(x) != "string" || |
11 |
| - !x.match(/^\s*$/)))) { |
12 |
| - acc[acc.length] = x; |
| 12 | + if (x == null || x == undefined) { |
| 13 | + if (!rejectSpace) { |
| 14 | + return append(acc, x); |
| 15 | + } |
| 16 | + return acc; |
| 17 | + } |
| 18 | + if (x.length == undefined) { // Just an object, not a string or array. |
| 19 | + return append(acc, x); |
| 20 | + } |
| 21 | + if (rejectSpace && |
| 22 | + ((x.length == 0) || |
| 23 | + (typeof(x) == "string" && |
| 24 | + x.match(/^\s*$/)))) { |
13 | 25 | return acc;
|
14 | 26 | }
|
| 27 | + if (typeof(x) == "string") { |
| 28 | + return append(acc, x); |
| 29 | + } |
15 | 30 | for (var i = 0; i < x.length; i++) {
|
16 | 31 | flatten(x[i], rejectSpace, acc);
|
17 | 32 | }
|
@@ -47,7 +62,8 @@ sql_stmt =
|
47 | 62 | / update_stmt / update_stmt_limited
|
48 | 63 | // / vacuum_stmt
|
49 | 64 | ) )
|
50 |
| - { return { explain: flatstr(explain), stmt: stmt } } |
| 65 | + { return { explain: flatstr(explain), |
| 66 | + stmt: stmt } } |
51 | 67 |
|
52 | 68 | alter_table_stmt =
|
53 | 69 | ( ( ALTER TABLE table_ref )
|
@@ -202,8 +218,15 @@ literal_value =
|
202 | 218 | / NULL / CURRENT_TIME / CURRENT_DATE / CURRENT_TIMESTAMP )
|
203 | 219 |
|
204 | 220 | numeric_literal =
|
205 |
| - ( ( ( ( digit )+ ( decimal_point ( digit )+ )? ) |
206 |
| - / ( decimal_point ( digit )+ ) ) ( E ( plus / minus )? ( digit )+ )? ) |
| 221 | + digits:( ( ( ( digit )+ ( decimal_point ( digit )+ )? ) |
| 222 | + / ( decimal_point ( digit )+ ) ) |
| 223 | + ( E ( plus / minus )? ( digit )+ )? ) |
| 224 | + { var x = flatstr(digits); |
| 225 | + if (x.indexOf('.') >= 0) { |
| 226 | + return parseFloat(x); |
| 227 | + } |
| 228 | + return parseInt(x); |
| 229 | + } |
207 | 230 |
|
208 | 231 | insert_stmt =
|
209 | 232 | ( ( ( INSERT ( OR ( ROLLBACK / ABORT / REPLACE / FAIL / IGNORE ) )? )
|
@@ -247,24 +270,30 @@ select_core =
|
247 | 270 |
|
248 | 271 | result_column =
|
249 | 272 | ( whitespace
|
250 |
| - ( ( ( call_function |
251 |
| - / column_ref) |
252 |
| - ( ( AS )? whitespace column_alias )? ) |
253 |
| - / ( ( table_name dot )? star ) ) ) |
| 273 | + ( ( c: ( call_function |
| 274 | + / column_ref) |
| 275 | + a: ( AS ? whitespace a: column_alias |
| 276 | + { return a })? ) |
| 277 | + { return { column: c, |
| 278 | + alias: a } } |
| 279 | + / s: ( ( table_name dot )? star ) |
| 280 | + { star: s } ) ) |
254 | 281 |
|
255 | 282 | join_source =
|
256 | 283 | ( single_source ( join_op single_source join_constraint )* )
|
257 | 284 |
|
258 | 285 | single_source =
|
259 | 286 | ( whitespace
|
260 | 287 | ( ( t: ( ( database_name dot )? table_name )
|
261 |
| - a: ( ( AS ? table_alias )? ) |
| 288 | + a: ( ( AS ? a: table_alias |
| 289 | + { return a })? ) |
262 | 290 | i: ( ( ( INDEXED BY index_name )
|
263 | 291 | / ( NOT INDEXED ) )? ) )
|
264 | 292 | { return { table: flatstr(t, true),
|
265 | 293 | alias: flatstr(a, true),
|
266 | 294 | index: flatstr(i, true) } }
|
267 |
| - / ( lparen s: select_stmt rparen a: ( AS ? table_alias )? ) |
| 295 | + / ( lparen s: select_stmt rparen a: ( AS ? a: table_alias |
| 296 | + { return a })? ) |
268 | 297 | { return { select: flatstr(s, true),
|
269 | 298 | alias: flatstr(a, true) } }
|
270 | 299 | / ( lparen j: join_source rparen )
|
@@ -363,7 +392,10 @@ digit = [0-9]
|
363 | 392 | decimal_point = dot
|
364 | 393 | equal = '='
|
365 | 394 |
|
366 |
| -name = [A-Za-z0-9_]+ |
| 395 | +name = |
| 396 | + str:[A-Za-z0-9_]+ |
| 397 | + { return str.join('') } |
| 398 | + |
367 | 399 | database_name = name
|
368 | 400 | table_name = name
|
369 | 401 | table_alias = name
|
|
0 commit comments