-
Notifications
You must be signed in to change notification settings - Fork 1
/
interpreter.js
99 lines (99 loc) · 3.96 KB
/
interpreter.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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
class Interpreter {
constructor(escape, tableName) {
this.partOfWhere = [];
this.partOfSelect = "*";
this.escape = escape;
this.tableName = tableName;
}
TransToSQLOfWhere(func, tableName, paramsKey, paramsValue) {
let param = this.MakeParams(paramsKey, paramsValue);
this.partOfWhere.push("(" + this.TransToSQL(func, this.tableName, param) + ")");
return this.TransToSQL(func, tableName, param);
}
TransToSQLOfSelect(func) {
let r = this.TransToSQL(func, this.tableName);
this.partOfSelect = r.split('AND').join(',');
return this.partOfSelect;
}
TransToSQLOfJoin(func, foreignTable) {
let foreignTableName = foreignTable.toString().toLocaleLowerCase();
}
TransToSQL(func, tableName, param) {
let funcStr = func.toString();
let funcCharList = funcStr.split(" ");
funcCharList = this.ReplaceParam(funcCharList, param);
funcCharList = this.GetQueryCharList(funcCharList, tableName);
return funcCharList.join(" ");
}
ReplaceParam(funcCharList, param) {
if (param) {
for (let key in param) {
let indexs = [];
for (let i = 0; i < funcCharList.length; i++) {
let x = funcCharList[i];
if (x.indexOf(key) > -1 && x.indexOf("." + key) <= -1) {
indexs.push(i);
}
}
if (indexs && indexs.length) {
for (let index of indexs) {
funcCharList[index] = funcCharList[index].replace(new RegExp(key, "gm"), this.escape(param[key]));
}
}
}
}
return funcCharList;
}
GetQueryCharList(funcCharList, tableName) {
let fChar = funcCharList[0];
if (tableName)
tableName = tableName.toLocaleLowerCase();
for (let index = 0; index < funcCharList.length; index++) {
let item = funcCharList[index];
if (item.indexOf(fChar + ".") > -1) {
if (tableName)
funcCharList[index] = funcCharList[index].replace(new RegExp(fChar + "\\.", "gm"), "`" + tableName + "`.");
else
funcCharList[index] = funcCharList[index].replace(new RegExp(fChar + "\\.", "gm"), "");
}
if (item === "==")
funcCharList[index] = "=";
if (item === "&&")
funcCharList[index] = "AND";
if (item === "||")
funcCharList[index] = "OR";
if (item.toLocaleLowerCase() == "null") {
if (funcCharList[index - 1] === "=")
funcCharList[index - 1] = "IS";
else if (funcCharList[index - 1] === "!=")
funcCharList[index - 1] = "IS NOT";
funcCharList[index] = "NULL";
}
if (item.indexOf(".IndexOf") > -1) {
funcCharList[index] = funcCharList[index].replace(new RegExp("\\.IndexOf", "gm"), " LIKE ");
funcCharList[index] = funcCharList[index].replace(/\(\"/g, '"%');
funcCharList[index] = funcCharList[index].replace(/\"\)/g, '%"');
funcCharList[index] = funcCharList[index].replace(/\(\'/g, '"%');
funcCharList[index] = funcCharList[index].replace(/\'\)/g, '%"');
}
}
funcCharList.splice(0, 1);
funcCharList.splice(0, 1);
return funcCharList;
}
MakeParams(paramsKey, paramsValue) {
if (paramsKey && paramsValue) {
let p = {};
for (let i = 0; i < paramsKey.length; i++) {
p[paramsKey[i]] = paramsValue[i];
}
return p;
}
else
return null;
}
}
exports.Interpreter = Interpreter;
//# sourceMappingURL=interpreter.js.map