|
1 | 1 | "use strict"; |
2 | 2 | const Symbol = require('es6-symbol'); |
3 | | -const Tokenizer = require('./Tokenizer'); |
| 3 | +const TokenizeThis = require('tokenize-this'); |
4 | 4 |
|
5 | 5 | /** |
6 | 6 | * To distinguish between the binary minus and unary. |
@@ -39,63 +39,6 @@ const unaryMinusDefinition = { |
39 | 39 | [OPERATOR_UNARY_MINUS]: OPERATOR_TYPE_UNARY |
40 | 40 | }; |
41 | 41 |
|
42 | | -/** |
43 | | - * The default config used. |
44 | | - * |
45 | | - * @type {{operators: [{}], tokenizer: {shouldTokenize: string[], shouldMatch: string[], shouldDelimitBy: string[]}}} |
46 | | - */ |
47 | | -const defaultConfig = { |
48 | | - operators: [ // TODO: add more operator definitions |
49 | | - { |
50 | | - '!': OPERATOR_TYPE_UNARY |
51 | | - }, |
52 | | - unaryMinusDefinition, |
53 | | - { |
54 | | - '^': OPERATOR_TYPE_BINARY |
55 | | - }, |
56 | | - { |
57 | | - '*': OPERATOR_TYPE_BINARY, |
58 | | - '/': OPERATOR_TYPE_BINARY, |
59 | | - '%': OPERATOR_TYPE_BINARY |
60 | | - }, |
61 | | - { |
62 | | - '+': OPERATOR_TYPE_BINARY, |
63 | | - '-': OPERATOR_TYPE_BINARY |
64 | | - }, |
65 | | - { |
66 | | - '=': OPERATOR_TYPE_BINARY, |
67 | | - '<': OPERATOR_TYPE_BINARY, |
68 | | - '>': OPERATOR_TYPE_BINARY, |
69 | | - '<=': OPERATOR_TYPE_BINARY, |
70 | | - '>=': OPERATOR_TYPE_BINARY, |
71 | | - '!=': OPERATOR_TYPE_BINARY |
72 | | - }, |
73 | | - { |
74 | | - ',': OPERATOR_TYPE_BINARY // We treat commas as an operator, to aid in turning arbitrary numbers of comma-separated values into arrays. |
75 | | - }, |
76 | | - { |
77 | | - 'NOT': OPERATOR_TYPE_UNARY |
78 | | - }, |
79 | | - { |
80 | | - 'BETWEEN': OPERATOR_TYPE_TERNARY, |
81 | | - 'IN': OPERATOR_TYPE_BINARY, |
82 | | - 'IS': OPERATOR_TYPE_BINARY, |
83 | | - 'LIKE': OPERATOR_TYPE_BINARY |
84 | | - }, |
85 | | - { |
86 | | - 'AND': OPERATOR_TYPE_BINARY |
87 | | - }, |
88 | | - { |
89 | | - 'OR': OPERATOR_TYPE_BINARY |
90 | | - } |
91 | | - ], |
92 | | - tokenizer: { |
93 | | - shouldTokenize: ['(', ')', ',', '*', '/', '%', '+', '-', '=', '!=', '<', '>', '<=', '>=', '!'], |
94 | | - shouldMatch: ['"', "'", '`'], |
95 | | - shouldDelimitBy: [' ', "\n", "\r", "\t"] |
96 | | - } |
97 | | -}; |
98 | | - |
99 | 42 | /** |
100 | 43 | * A wrapper class around operators to distinguish them from regular tokens. |
101 | 44 | */ |
@@ -133,13 +76,13 @@ class SqlWhereParser { |
133 | 76 | * |
134 | 77 | * @type {{operators: [{}], tokenizer: {shouldTokenize: string[], shouldMatch: string[], shouldDelimitBy: string[]}}} |
135 | 78 | */ |
136 | | - config = Object.assign({}, config, defaultConfig); |
| 79 | + config = Object.assign({}, this.constructor.defaultConfig, config); |
137 | 80 |
|
138 | 81 | /** |
139 | 82 | * |
140 | | - * @type {Tokenizer} |
| 83 | + * @type {TokenizeThis} |
141 | 84 | */ |
142 | | - this.tokenizer = new Tokenizer(config.tokenizer); |
| 85 | + this.tokenizer = new TokenizeThis(config.tokenizer); |
143 | 86 |
|
144 | 87 | /** |
145 | 88 | * |
@@ -440,7 +383,58 @@ class SqlWhereParser { |
440 | 383 | * @returns {{operators: [{}], tokenizer: {shouldTokenize: string[], shouldMatch: string[], shouldDelimitBy: string[]}}} |
441 | 384 | */ |
442 | 385 | static get defaultConfig() { |
443 | | - return defaultConfig; |
| 386 | + |
| 387 | + return { |
| 388 | + operators: [ // TODO: add more operator definitions |
| 389 | + { |
| 390 | + '!': OPERATOR_TYPE_UNARY |
| 391 | + }, |
| 392 | + unaryMinusDefinition, |
| 393 | + { |
| 394 | + '^': OPERATOR_TYPE_BINARY |
| 395 | + }, |
| 396 | + { |
| 397 | + '*': OPERATOR_TYPE_BINARY, |
| 398 | + '/': OPERATOR_TYPE_BINARY, |
| 399 | + '%': OPERATOR_TYPE_BINARY |
| 400 | + }, |
| 401 | + { |
| 402 | + '+': OPERATOR_TYPE_BINARY, |
| 403 | + '-': OPERATOR_TYPE_BINARY |
| 404 | + }, |
| 405 | + { |
| 406 | + '=': OPERATOR_TYPE_BINARY, |
| 407 | + '<': OPERATOR_TYPE_BINARY, |
| 408 | + '>': OPERATOR_TYPE_BINARY, |
| 409 | + '<=': OPERATOR_TYPE_BINARY, |
| 410 | + '>=': OPERATOR_TYPE_BINARY, |
| 411 | + '!=': OPERATOR_TYPE_BINARY |
| 412 | + }, |
| 413 | + { |
| 414 | + ',': OPERATOR_TYPE_BINARY // We treat commas as an operator, to aid in turning arbitrary numbers of comma-separated values into arrays. |
| 415 | + }, |
| 416 | + { |
| 417 | + 'NOT': OPERATOR_TYPE_UNARY |
| 418 | + }, |
| 419 | + { |
| 420 | + 'BETWEEN': OPERATOR_TYPE_TERNARY, |
| 421 | + 'IN': OPERATOR_TYPE_BINARY, |
| 422 | + 'IS': OPERATOR_TYPE_BINARY, |
| 423 | + 'LIKE': OPERATOR_TYPE_BINARY |
| 424 | + }, |
| 425 | + { |
| 426 | + 'AND': OPERATOR_TYPE_BINARY |
| 427 | + }, |
| 428 | + { |
| 429 | + 'OR': OPERATOR_TYPE_BINARY |
| 430 | + } |
| 431 | + ], |
| 432 | + tokenizer: { |
| 433 | + shouldTokenize: ['(', ')', ',', '*', '/', '%', '+', '-', '=', '!=','!', '<', '>', '<=', '>=', '^'], |
| 434 | + shouldMatch: ['"', "'", '`'], |
| 435 | + shouldDelimitBy: [' ', "\n", "\r", "\t"] |
| 436 | + } |
| 437 | + }; |
444 | 438 | } |
445 | 439 |
|
446 | 440 | static get Operator() { |
|
0 commit comments