-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
162 lines (140 loc) · 5.34 KB
/
index.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import { DeleteStatement, QueryObject, RenameStatement, UpdateObject, WhereStatement } from './types/plainsqlbuilderTypes'
export const generateConditionQuery = (where : WhereStatement, filters: string[]) => {
filters.push(
`${where["column"]} ${where["type"]} (${
typeof(where["value"]) === "string" ? where["value"] : generateQuery(where["value"])
}) ${
where["connector"] ? where["connector"] : ""
} `
)
}
export function generateQuery(queries : QueryObject) {
let statement : string = "SELECT ";
// SELECT comes as a string array or object to rename
/*
* renameObject
* {
* column, -> original name
* rename -> name to override
* }
* */
if (queries["SELECT"]) {
let indexToDeleteDistinct : number = 0;
if (Array.isArray(queries["SELECT"]) && queries["SELECT"].find((val, index) => {
if (typeof val == 'string' && val.toLowerCase() === 'distinct') indexToDeleteDistinct = index;
let res : boolean;
if (typeof val === 'string') res = val.toLowerCase() === 'distinct';
else res = false;
return res;
})) {
statement += "DISTINCT ";
delete queries["SELECT"][indexToDeleteDistinct];
}
const selectColumns : string[] = [];
queries["SELECT"].forEach(select => {
let preparedStr = "";
typeof(select) === 'string' ? preparedStr += select + " " : preparedStr += `${select["column"]} AS ${select["rename"]} `;
selectColumns.push(preparedStr);
})
statement += selectColumns.join(", ")
}
else statement += `* `
// FROM comes as a string array and Object to specify the JOIN
/*
* JOIN setting object
* {
* type, -> "INNER JOIN" | "OUTER JOIN"
* table, -> Table to join | Can be a query object with "rename" property to exec a rename
* condition : [{
column,
type, -> ">" | "<" | ">=" | "LIKE" | "ON"
value, -> Can be query object.
connector? -> "AND" | "OR" to the next filter
}]
* }
* */
if (queries["FROM"]){
const joinFilters : string[] = [];
if (queries["FROM"].length > 2) throw ('JOIN solo se puede hacer con 2 tablas');
if (queries["FROM"].length == 1) statement += `FROM ${queries["FROM"][0]} `
else {
(queries["FROM"][1])?.condition.forEach((condition) => {
joinFilters.push(
`${condition["column"]} ${condition["type"]} (${
typeof(condition["value"]) === "string" ? condition["value"] : generateQuery(condition["value"])
}) ${
condition["connector"] ? condition["connector"] : ""
} `
)
})
statement += `FROM ${queries["FROM"][0]} ${queries["FROM"][1]?.table} (${
(typeof(queries["FROM"][1]?.table) === 'string' ? queries["FROM"][1].table : generateQuery(queries["FROM"][1]?.table as QueryObject))
}) ON ${joinFilters.join("")} `
}
}
else throw ('Debe existir un FROM o alguna tabla a la cual consultar')
// WHERE comes as a object array
/*
* {
* column,
* type, -> ">" | "<" | ">=" | "LIKE" | "ON"
* value, -> Can be query object with "rename" property.
* connector? -> "AND" | "OR" to the next filter
* }
* */
if (queries["WHERE"]) {
const filters : string[] = []
queries["WHERE"].forEach(where => {
generateConditionQuery(where, filters);
})
statement += `WHERE ${filters.join("")} `
}
// GROUP BY comes as string array
if(queries["GROUP_BY"]) {
statement += `GROUP BY (${queries["GROUP_BY"].join(",")}) `;
}
if (queries["HAVING"]) {
const filters : string[] = []
queries["HAVING"].forEach(where => {
generateConditionQuery(where, filters);
})
statement += `HAVING ${filters.join("")} ;`
}
return statement + ";";
}
export function generateInsertStatement(table : string, data : object) {
return `INSERT INTO ${table} (${Object.keys(data).join(", ")}) VALUES(${Object.values(data).join(", ")});`
}
/*
* update object
* {
* column, -> Column affected
* newValue, -> New value for the column,
* condition: [{
* column,
* type, -> ">" | "<" | ">=" | "LIKE" | "ON"
* value, -> Can be query object with "rename" property.
* connector? -> "AND" | "OR" to the next filter
* }]
* }
*
* */
export function generateUpdateStatement(table : string, update : UpdateObject) {
let statement = `UPDATE ${table} SET ${update["column"]} = ${update["newValue"]} `
const filters : string[] = [];
update["condition"].forEach(where => {
generateConditionQuery(where, filters);
})
statement += `WHERE ${filters.join("")} `
return statement + ";";
}
export function generateDeleteStatement(table : string, deleteData : DeleteStatement) {
if (!deleteData["condition"]) throw ('Para eliminar debe tener un Condition Object')
let statement = `DELETE FROM ${table} `;
const filters : string[] = [];
deleteData["condition"].forEach(where => {
generateConditionQuery(where, filters);
})
statement += `WHERE ${filters.join("")} `;
return statement + ";";
}