Skip to content

Commit f7ef507

Browse files
committed
shhh, testing in progress.
1 parent 71b8f06 commit f7ef507

File tree

7 files changed

+456
-521
lines changed

7 files changed

+456
-521
lines changed

SqlWhereParser.js

Lines changed: 56 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use strict";
22
const Symbol = require('es6-symbol');
3-
const Tokenizer = require('./Tokenizer');
3+
const TokenizeThis = require('tokenize-this');
44

55
/**
66
* To distinguish between the binary minus and unary.
@@ -39,63 +39,6 @@ const unaryMinusDefinition = {
3939
[OPERATOR_UNARY_MINUS]: OPERATOR_TYPE_UNARY
4040
};
4141

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-
9942
/**
10043
* A wrapper class around operators to distinguish them from regular tokens.
10144
*/
@@ -133,13 +76,13 @@ class SqlWhereParser {
13376
*
13477
* @type {{operators: [{}], tokenizer: {shouldTokenize: string[], shouldMatch: string[], shouldDelimitBy: string[]}}}
13578
*/
136-
config = Object.assign({}, config, defaultConfig);
79+
config = Object.assign({}, this.constructor.defaultConfig, config);
13780

13881
/**
13982
*
140-
* @type {Tokenizer}
83+
* @type {TokenizeThis}
14184
*/
142-
this.tokenizer = new Tokenizer(config.tokenizer);
85+
this.tokenizer = new TokenizeThis(config.tokenizer);
14386

14487
/**
14588
*
@@ -440,7 +383,58 @@ class SqlWhereParser {
440383
* @returns {{operators: [{}], tokenizer: {shouldTokenize: string[], shouldMatch: string[], shouldDelimitBy: string[]}}}
441384
*/
442385
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+
};
444438
}
445439

446440
static get Operator() {

0 commit comments

Comments
 (0)