-
Notifications
You must be signed in to change notification settings - Fork 0
/
base_queries.js
124 lines (111 loc) · 4.2 KB
/
base_queries.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
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
class base_queries {
constructor(tableName) {
this.tableName = tableName;
this.description = [];
}
dynamicCondition(dataCondition, operation) {
if (!dataCondition || Object.keys(dataCondition).length === 0) {
return '';
}
const conditions = Object.entries(dataCondition).map(([key, value]) => {
return `${this.escape(key)} ${operation} '${this.escape(value)}'`;
});
return `WHERE ${conditions.join(' AND ')}`;
}
dynamicInsert(assocArray) {
const keys = [];
const values = [];
for (const [key, value] of Object.entries(assocArray)) {
keys.push(this.escapeHtml(key));
if (value === '') {
values.push('null');
} else if (Array.isArray(value)) {
values.push(`'${JSON.stringify(value)}'`);
} else {
values.push(`'${this.escapeHtml(value)}'`);
}
}
return `INSERT INTO ${this.tableName}(${keys.join(',')}) VALUES(${values.join(',')})`;
}
dynamicInsert_2(assocArray) {
const keys = [];
const values = [];
for (const [key, value] of Object.entries(assocArray)) {
keys.push(this.escapeHtml(key));
if (Array.isArray(value)) {
const jsonValue = JSON.stringify(value);
const escapedJsonValue = this.escape(jsonValue);
values.push(`'${escapedJsonValue}'`);
} else if (value === '') {
values.push('null');
} else {
values.push(`'${this.escapeHtml(value)}'`);
}
}
return `INSERT INTO ${this.tableName}(${keys.join(',')}) VALUES(${values.join(',')})`;
}
dynamicInsert2(array) {
const keys = [];
const values = [];
for (const value of array) {
keys.push(this.escapeHtml(value.field));
if (value === '') {
values.push('null');
} else {
values.push(`'${this.escapeHtml(value.valeur)}'`);
}
}
return `INSERT INTO ${this.tableName}(${keys.join(',')}) VALUES(${values.join(',')})`;
}
dynamicManyInsert(tableName, simpleArray, keys) {
const rows = simpleArray.map(row => {
const values = row.map(value => {
if (value === '') {
return 'null';
} else {
return `'${this.escapeHtml(value)}'`;
}
});
return `(${values.join(',')})`;
});
return `INSERT INTO ${tableName}(${keys.join(',')}) VALUES ${rows.join(', ')}`;
}
dynamicUpdate(assocArray, condition) {
const keyValuePairs = Object.entries(assocArray).map(([key, value]) => {
if (value === '') {
return `${this.escape(key)} = null`;
} else {
return `${this.escape(key)} = '${this.escape(value)}'`;
}
});
return `UPDATE ${this.tableName} SET ${keyValuePairs.join(',')} ${condition}`;
}
dynamicUpdate_2(assocArray, condition) {
const keyValuePairs = Object.entries(assocArray).map(([key, value]) => {
const escapedKey = this.escapeHtml(key);
if (Array.isArray(value) || typeof value === 'object') {
const jsonValue = JSON.stringify(value);
const escapedJsonValue = this.escape(jsonValue);
return `${escapedKey} = '${escapedJsonValue}'`;
} else if (value === '') {
return `${escapedKey} = null`;
} else {
return `${escapedKey} = '${this.escapeHtml(value)}'`;
}
});
return `UPDATE ${this.tableName} SET ${keyValuePairs.join(',')} ${condition}`;
}
// Helper methods to escape strings and HTML
escape(value) {
return String(value).replace(/\\/g, '\\\\').replace(/'/g, "\\'");
}
escapeHtml(value) {
return String(value)
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
}
}
module.exports = base_queries;