-
Notifications
You must be signed in to change notification settings - Fork 1
/
interpreter.ts
108 lines (93 loc) · 4.07 KB
/
interpreter.ts
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
export class Interpreter {
private escape: any;
private tableName: string;
private partOfWhere: string[] = [];
private partOfSelect: string = "*";
constructor(escape, tableName) {
this.escape = escape;
this.tableName = tableName;
}
TransToSQLOfWhere(func: Function, tableName?: string, paramsKey?: string[], paramsValue?: any[]): string {
let param = this.MakeParams(paramsKey, paramsValue);
// add to sql part of where;
this.partOfWhere.push("(" + this.TransToSQL(func, this.tableName, param) + ")");
return this.TransToSQL(func, tableName, param);
}
TransToSQLOfSelect(func: Function) {
let r = this.TransToSQL(func, this.tableName);
// add to sql part of select
this.partOfSelect = r.split('AND').join(',');
return this.partOfSelect;
}
TransToSQLOfJoin(func: Function, foreignTable: any) {
let foreignTableName = foreignTable.toString().toLocaleLowerCase();
}
TransToSQL(func: Function, tableName?: string, param?: any): string {
let funcStr = func.toString();
let funcCharList = funcStr.split(" ");
funcCharList = this.ReplaceParam(funcCharList, param);
funcCharList = this.GetQueryCharList(funcCharList, tableName);
return funcCharList.join(" ");
}
private ReplaceParam(funcCharList: string[], param): string[] {
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;
}
private GetQueryCharList(funcCharList: string[], tableName: string): string[] {
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;
}
private MakeParams(paramsKey: string[], paramsValue: any[]) {
if (paramsKey && paramsValue) {
let p: any = {};
for (let i = 0; i < paramsKey.length; i++) {
p[paramsKey[i]] = paramsValue[i];
}
return p;
}
else return null;
}
}