@tachybase/module-multi-app 0.23.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +14 -0
- package/README.md +34 -0
- package/README.zh-CN.md +34 -0
- package/client.d.ts +1 -0
- package/client.js +1 -0
- package/dist/client/AppManager.d.ts +2 -0
- package/dist/client/AppNameInput.d.ts +2 -0
- package/dist/client/MultiAppBlockInitializer.d.ts +2 -0
- package/dist/client/MultiAppManagerProvider.d.ts +2 -0
- package/dist/client/MultiAppManagerProvider.style.d.ts +5 -0
- package/dist/client/Settings.d.ts +2 -0
- package/dist/client/index.d.ts +6 -0
- package/dist/client/index.js +1 -0
- package/dist/client/settings/schemas/applications.d.ts +13 -0
- package/dist/client/utils.d.ts +4 -0
- package/dist/constants.d.ts +1 -0
- package/dist/constants.js +27 -0
- package/dist/externalVersion.js +16 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +39 -0
- package/dist/locale/en-US.json +27 -0
- package/dist/locale/es-ES.json +9 -0
- package/dist/locale/ko_KR.json +11 -0
- package/dist/locale/pt-BR.json +9 -0
- package/dist/locale/zh-CN.json +27 -0
- package/dist/node_modules/mariadb/LICENSE +502 -0
- package/dist/node_modules/mariadb/callback.js +41 -0
- package/dist/node_modules/mariadb/lib/cmd/batch-bulk.js +278 -0
- package/dist/node_modules/mariadb/lib/cmd/batch-rewrite.js +372 -0
- package/dist/node_modules/mariadb/lib/cmd/change-user.js +149 -0
- package/dist/node_modules/mariadb/lib/cmd/class/ok-packet.js +17 -0
- package/dist/node_modules/mariadb/lib/cmd/column-definition.js +102 -0
- package/dist/node_modules/mariadb/lib/cmd/command.js +168 -0
- package/dist/node_modules/mariadb/lib/cmd/common-binary-cmd.js +327 -0
- package/dist/node_modules/mariadb/lib/cmd/common-text-cmd.js +427 -0
- package/dist/node_modules/mariadb/lib/cmd/handshake/auth/caching-sha2-password-auth.js +168 -0
- package/dist/node_modules/mariadb/lib/cmd/handshake/auth/clear-password-auth.js +23 -0
- package/dist/node_modules/mariadb/lib/cmd/handshake/auth/ed25519-password-auth.js +761 -0
- package/dist/node_modules/mariadb/lib/cmd/handshake/auth/native-password-auth.js +55 -0
- package/dist/node_modules/mariadb/lib/cmd/handshake/auth/pam-password-auth.js +58 -0
- package/dist/node_modules/mariadb/lib/cmd/handshake/auth/plugin-auth.js +19 -0
- package/dist/node_modules/mariadb/lib/cmd/handshake/auth/sha256-password-auth.js +142 -0
- package/dist/node_modules/mariadb/lib/cmd/handshake/client-capabilities.js +74 -0
- package/dist/node_modules/mariadb/lib/cmd/handshake/client-handshake-response.js +126 -0
- package/dist/node_modules/mariadb/lib/cmd/handshake/handshake.js +292 -0
- package/dist/node_modules/mariadb/lib/cmd/handshake/initial-handshake.js +74 -0
- package/dist/node_modules/mariadb/lib/cmd/handshake/ssl-request.js +29 -0
- package/dist/node_modules/mariadb/lib/cmd/ping.js +52 -0
- package/dist/node_modules/mariadb/lib/cmd/query.js +255 -0
- package/dist/node_modules/mariadb/lib/cmd/quit.js +28 -0
- package/dist/node_modules/mariadb/lib/cmd/reset.js +54 -0
- package/dist/node_modules/mariadb/lib/cmd/resultset.js +607 -0
- package/dist/node_modules/mariadb/lib/cmd/stream.js +45 -0
- package/dist/node_modules/mariadb/lib/config/connection-options.js +258 -0
- package/dist/node_modules/mariadb/lib/config/pool-cluster-options.js +19 -0
- package/dist/node_modules/mariadb/lib/config/pool-options.js +47 -0
- package/dist/node_modules/mariadb/lib/connection-callback.js +160 -0
- package/dist/node_modules/mariadb/lib/connection.js +1460 -0
- package/dist/node_modules/mariadb/lib/const/capabilities.js +64 -0
- package/dist/node_modules/mariadb/lib/const/collations.js +473 -0
- package/dist/node_modules/mariadb/lib/const/connection_status.js +13 -0
- package/dist/node_modules/mariadb/lib/const/error-code.js +1282 -0
- package/dist/node_modules/mariadb/lib/const/field-detail.js +35 -0
- package/dist/node_modules/mariadb/lib/const/field-type.js +71 -0
- package/dist/node_modules/mariadb/lib/const/server-status.js +30 -0
- package/dist/node_modules/mariadb/lib/const/state-change.js +12 -0
- package/dist/node_modules/mariadb/lib/filtered-pool-cluster.js +81 -0
- package/dist/node_modules/mariadb/lib/io/bulk-packet.js +590 -0
- package/dist/node_modules/mariadb/lib/io/compression-input-stream.js +141 -0
- package/dist/node_modules/mariadb/lib/io/compression-output-stream.js +171 -0
- package/dist/node_modules/mariadb/lib/io/packet-input-stream.js +193 -0
- package/dist/node_modules/mariadb/lib/io/packet-node-encoded.js +36 -0
- package/dist/node_modules/mariadb/lib/io/packet-node-iconv.js +37 -0
- package/dist/node_modules/mariadb/lib/io/packet-output-stream.js +502 -0
- package/dist/node_modules/mariadb/lib/io/packet.js +515 -0
- package/dist/node_modules/mariadb/lib/io/rewrite-packet.js +481 -0
- package/dist/node_modules/mariadb/lib/misc/connection-information.js +96 -0
- package/dist/node_modules/mariadb/lib/misc/errors.js +123 -0
- package/dist/node_modules/mariadb/lib/misc/parse.js +1033 -0
- package/dist/node_modules/mariadb/lib/misc/utils.js +298 -0
- package/dist/node_modules/mariadb/lib/pool-base.js +611 -0
- package/dist/node_modules/mariadb/lib/pool-callback.js +202 -0
- package/dist/node_modules/mariadb/lib/pool-cluster-callback.js +66 -0
- package/dist/node_modules/mariadb/lib/pool-cluster.js +407 -0
- package/dist/node_modules/mariadb/lib/pool-promise.js +108 -0
- package/dist/node_modules/mariadb/package.json +1 -0
- package/dist/node_modules/mariadb/promise.js +34 -0
- package/dist/node_modules/mariadb/types/index.d.ts +870 -0
- package/dist/server/actions/apps.d.ts +5 -0
- package/dist/server/actions/apps.js +117 -0
- package/dist/server/app-lifecycle.d.ts +8 -0
- package/dist/server/app-lifecycle.js +99 -0
- package/dist/server/app-start-env.d.ts +2 -0
- package/dist/server/app-start-env.js +105 -0
- package/dist/server/collections/applications.d.ts +2 -0
- package/dist/server/collections/applications.js +82 -0
- package/dist/server/index.d.ts +4 -0
- package/dist/server/index.js +29 -0
- package/dist/server/middlewares/app-selector.d.ts +1 -0
- package/dist/server/middlewares/app-selector.js +47 -0
- package/dist/server/middlewares/index.d.ts +2 -0
- package/dist/server/middlewares/index.js +23 -0
- package/dist/server/middlewares/inject-app-list.d.ts +1 -0
- package/dist/server/middlewares/inject-app-list.js +48 -0
- package/dist/server/migrations/20240820153000-add-apps-tmpl.d.ts +6 -0
- package/dist/server/migrations/20240820153000-add-apps-tmpl.js +47 -0
- package/dist/server/migrations/20241126124904-add-createdBy.d.ts +6 -0
- package/dist/server/migrations/20241126124904-add-createdBy.js +41 -0
- package/dist/server/models/application.d.ts +10 -0
- package/dist/server/models/application.js +57 -0
- package/dist/server/server.d.ts +19 -0
- package/dist/server/server.js +246 -0
- package/dist/swagger/index.d.ts +197 -0
- package/dist/swagger/index.js +227 -0
- package/package.json +38 -0
- package/server.d.ts +2 -0
- package/server.js +1 -0
|
@@ -0,0 +1,1033 @@
|
|
|
1
|
+
const Errors = require('../misc/errors');
|
|
2
|
+
|
|
3
|
+
const State = {
|
|
4
|
+
Normal: 1 /* inside query */,
|
|
5
|
+
String: 2 /* inside string */,
|
|
6
|
+
SlashStarComment: 3 /* inside slash-star comment */,
|
|
7
|
+
Escape: 4 /* found backslash */,
|
|
8
|
+
EOLComment: 5 /* # comment, or // comment, or -- comment */,
|
|
9
|
+
Backtick: 6 /* found backtick */,
|
|
10
|
+
Placeholder: 7 /* found placeholder */
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Split query according to parameters (question mark).
|
|
15
|
+
* Question mark in comment are not taken in account
|
|
16
|
+
*
|
|
17
|
+
* @returns {Array} query separated by parameters
|
|
18
|
+
*/
|
|
19
|
+
module.exports.splitQuery = function (sql) {
|
|
20
|
+
let partList = [];
|
|
21
|
+
let state = State.Normal;
|
|
22
|
+
let lastChar = '\0';
|
|
23
|
+
let singleQuotes = false;
|
|
24
|
+
let lastParameterPosition = 0;
|
|
25
|
+
let idx = 0;
|
|
26
|
+
let car = sql.charAt(idx++);
|
|
27
|
+
|
|
28
|
+
while (car !== '') {
|
|
29
|
+
if (
|
|
30
|
+
state === State.Escape &&
|
|
31
|
+
!((car === "'" && singleQuotes) || (car === '"' && !singleQuotes))
|
|
32
|
+
) {
|
|
33
|
+
state = State.String;
|
|
34
|
+
car = sql.charAt(idx++);
|
|
35
|
+
continue;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
switch (car) {
|
|
39
|
+
case '*':
|
|
40
|
+
if (state === State.Normal && lastChar == '/') state = State.SlashStarComment;
|
|
41
|
+
break;
|
|
42
|
+
|
|
43
|
+
case '/':
|
|
44
|
+
if (state === State.SlashStarComment && lastChar == '*') state = State.Normal;
|
|
45
|
+
break;
|
|
46
|
+
|
|
47
|
+
case '#':
|
|
48
|
+
if (state === State.Normal) state = State.EOLComment;
|
|
49
|
+
break;
|
|
50
|
+
|
|
51
|
+
case '-':
|
|
52
|
+
if (state === State.Normal && lastChar == '-') {
|
|
53
|
+
state = State.EOLComment;
|
|
54
|
+
}
|
|
55
|
+
break;
|
|
56
|
+
|
|
57
|
+
case '\n':
|
|
58
|
+
if (state === State.EOLComment) {
|
|
59
|
+
state = State.Normal;
|
|
60
|
+
}
|
|
61
|
+
break;
|
|
62
|
+
|
|
63
|
+
case '"':
|
|
64
|
+
if (state === State.Normal) {
|
|
65
|
+
state = State.String;
|
|
66
|
+
singleQuotes = false;
|
|
67
|
+
} else if (state === State.String && !singleQuotes) {
|
|
68
|
+
state = State.Normal;
|
|
69
|
+
} else if (state === State.Escape && !singleQuotes) {
|
|
70
|
+
state = State.String;
|
|
71
|
+
}
|
|
72
|
+
break;
|
|
73
|
+
|
|
74
|
+
case "'":
|
|
75
|
+
if (state === State.Normal) {
|
|
76
|
+
state = State.String;
|
|
77
|
+
singleQuotes = true;
|
|
78
|
+
} else if (state === State.String && singleQuotes) {
|
|
79
|
+
state = State.Normal;
|
|
80
|
+
} else if (state === State.Escape && singleQuotes) {
|
|
81
|
+
state = State.String;
|
|
82
|
+
}
|
|
83
|
+
break;
|
|
84
|
+
|
|
85
|
+
case '\\':
|
|
86
|
+
if (state === State.String) state = State.Escape;
|
|
87
|
+
break;
|
|
88
|
+
|
|
89
|
+
case '?':
|
|
90
|
+
if (state === State.Normal) {
|
|
91
|
+
partList.push(sql.substring(lastParameterPosition, idx - 1));
|
|
92
|
+
lastParameterPosition = idx;
|
|
93
|
+
}
|
|
94
|
+
break;
|
|
95
|
+
case '`':
|
|
96
|
+
if (state === State.Backtick) {
|
|
97
|
+
state = State.Normal;
|
|
98
|
+
} else if (state === State.Normal) {
|
|
99
|
+
state = State.Backtick;
|
|
100
|
+
}
|
|
101
|
+
break;
|
|
102
|
+
}
|
|
103
|
+
lastChar = car;
|
|
104
|
+
|
|
105
|
+
car = sql.charAt(idx++);
|
|
106
|
+
}
|
|
107
|
+
if (lastParameterPosition === 0) {
|
|
108
|
+
partList.push(sql);
|
|
109
|
+
} else {
|
|
110
|
+
partList.push(sql.substring(lastParameterPosition));
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return partList;
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Split query according to parameters using placeholder.
|
|
118
|
+
*
|
|
119
|
+
* @param sql sql with placeholders
|
|
120
|
+
* @param info connection information
|
|
121
|
+
* @param initialValues placeholder object
|
|
122
|
+
* @param displaySql display sql function
|
|
123
|
+
* @returns {{parts: Array, values: Array}}
|
|
124
|
+
*/
|
|
125
|
+
module.exports.splitQueryPlaceholder = function (sql, info, initialValues, displaySql) {
|
|
126
|
+
let partList = [];
|
|
127
|
+
let values = [];
|
|
128
|
+
let state = State.Normal;
|
|
129
|
+
let lastChar = '\0';
|
|
130
|
+
|
|
131
|
+
let singleQuotes = false;
|
|
132
|
+
let lastParameterPosition = 0;
|
|
133
|
+
|
|
134
|
+
let idx = 0;
|
|
135
|
+
let car = sql.charAt(idx++);
|
|
136
|
+
let placeholderName;
|
|
137
|
+
|
|
138
|
+
while (car !== '') {
|
|
139
|
+
if (
|
|
140
|
+
state === State.Escape &&
|
|
141
|
+
!((car === "'" && singleQuotes) || (car === '"' && !singleQuotes))
|
|
142
|
+
) {
|
|
143
|
+
state = State.String;
|
|
144
|
+
car = sql.charAt(idx++);
|
|
145
|
+
continue;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
switch (car) {
|
|
149
|
+
case '*':
|
|
150
|
+
if (state === State.Normal && lastChar == '/') state = State.SlashStarComment;
|
|
151
|
+
break;
|
|
152
|
+
|
|
153
|
+
case '/':
|
|
154
|
+
if (state === State.SlashStarComment && lastChar == '*') state = State.Normal;
|
|
155
|
+
break;
|
|
156
|
+
|
|
157
|
+
case '#':
|
|
158
|
+
if (state === State.Normal) state = State.EOLComment;
|
|
159
|
+
break;
|
|
160
|
+
|
|
161
|
+
case '-':
|
|
162
|
+
if (state === State.Normal && lastChar == '-') {
|
|
163
|
+
state = State.EOLComment;
|
|
164
|
+
}
|
|
165
|
+
break;
|
|
166
|
+
|
|
167
|
+
case '\n':
|
|
168
|
+
if (state === State.EOLComment) {
|
|
169
|
+
state = State.Normal;
|
|
170
|
+
}
|
|
171
|
+
break;
|
|
172
|
+
|
|
173
|
+
case '"':
|
|
174
|
+
if (state === State.Normal) {
|
|
175
|
+
state = State.String;
|
|
176
|
+
singleQuotes = false;
|
|
177
|
+
} else if (state === State.String && !singleQuotes) {
|
|
178
|
+
state = State.Normal;
|
|
179
|
+
} else if (state === State.Escape && !singleQuotes) {
|
|
180
|
+
state = State.String;
|
|
181
|
+
}
|
|
182
|
+
break;
|
|
183
|
+
|
|
184
|
+
case "'":
|
|
185
|
+
if (state === State.Normal) {
|
|
186
|
+
state = State.String;
|
|
187
|
+
singleQuotes = true;
|
|
188
|
+
} else if (state === State.String && singleQuotes) {
|
|
189
|
+
state = State.Normal;
|
|
190
|
+
singleQuotes = false;
|
|
191
|
+
} else if (state === State.Escape && singleQuotes) {
|
|
192
|
+
state = State.String;
|
|
193
|
+
}
|
|
194
|
+
break;
|
|
195
|
+
|
|
196
|
+
case '\\':
|
|
197
|
+
if (state === State.String) state = State.Escape;
|
|
198
|
+
break;
|
|
199
|
+
|
|
200
|
+
case ':':
|
|
201
|
+
if (state === State.Normal) {
|
|
202
|
+
partList.push(sql.substring(lastParameterPosition, idx - 1));
|
|
203
|
+
placeholderName = '';
|
|
204
|
+
while (
|
|
205
|
+
((car = sql.charAt(idx++)) !== '' && car >= '0' && car <= '9') ||
|
|
206
|
+
(car >= 'A' && car <= 'Z') ||
|
|
207
|
+
(car >= 'a' && car <= 'z') ||
|
|
208
|
+
car === '-' ||
|
|
209
|
+
car === '_'
|
|
210
|
+
) {
|
|
211
|
+
placeholderName += car;
|
|
212
|
+
}
|
|
213
|
+
idx--;
|
|
214
|
+
const val = initialValues[placeholderName];
|
|
215
|
+
if (val === undefined) {
|
|
216
|
+
throw Errors.createError(
|
|
217
|
+
"Placeholder '" + placeholderName + "' is not defined",
|
|
218
|
+
displaySql.call(),
|
|
219
|
+
false,
|
|
220
|
+
info,
|
|
221
|
+
'HY000',
|
|
222
|
+
Errors.ER_PLACEHOLDER_UNDEFINED
|
|
223
|
+
);
|
|
224
|
+
}
|
|
225
|
+
values.push(val);
|
|
226
|
+
lastParameterPosition = idx;
|
|
227
|
+
}
|
|
228
|
+
break;
|
|
229
|
+
case '`':
|
|
230
|
+
if (state === State.Backtick) {
|
|
231
|
+
state = State.Normal;
|
|
232
|
+
} else if (state === State.Normal) {
|
|
233
|
+
state = State.Backtick;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
lastChar = car;
|
|
237
|
+
|
|
238
|
+
car = sql.charAt(idx++);
|
|
239
|
+
}
|
|
240
|
+
if (lastParameterPosition === 0) {
|
|
241
|
+
partList.push(sql);
|
|
242
|
+
} else {
|
|
243
|
+
partList.push(sql.substring(lastParameterPosition));
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
return { parts: partList, values: values };
|
|
247
|
+
};
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* Split query according to parameters (question mark).
|
|
251
|
+
*
|
|
252
|
+
* The only rewritten queries follow these notation: INSERT [LOW_PRIORITY | DELAYED |
|
|
253
|
+
* HIGH_PRIORITY] [IGNORE] [INTO] tbl_name [PARTITION (partition_list)] [(col,...)] {VALUES |
|
|
254
|
+
* VALUE} (...) [ ON DUPLICATE KEY UPDATE col=expr [, col=expr] ... ] With expr without
|
|
255
|
+
* parameter.
|
|
256
|
+
*
|
|
257
|
+
* Query with INSERT ... SELECT / containing LAST_INSERT_ID() will not be rewritten
|
|
258
|
+
*
|
|
259
|
+
* query parts will be split this way :
|
|
260
|
+
* - pre-value part
|
|
261
|
+
* - after value part
|
|
262
|
+
* [- after parameter part] (after each parameter)
|
|
263
|
+
* - ending part
|
|
264
|
+
*
|
|
265
|
+
* example : INSERT INTO MyTABLE VALUES (9, ?, 5, ?, 8) ON DUPLICATE KEY UPDATE col2=col2+10
|
|
266
|
+
* will result in :
|
|
267
|
+
* - pre-value : "INSERT INTO MyTABLE VALUES"
|
|
268
|
+
* - after value : " (9, "
|
|
269
|
+
* - after parameter : ", 5, "
|
|
270
|
+
* - after parameter : ", 8)"
|
|
271
|
+
* - ending : " ON DUPLICATE KEY UPDATE col2=col2+10"
|
|
272
|
+
*
|
|
273
|
+
*
|
|
274
|
+
* @returns {JSON} query separated by parameters
|
|
275
|
+
*/
|
|
276
|
+
module.exports.splitRewritableQuery = function (sql) {
|
|
277
|
+
let reWritablePrepare = true;
|
|
278
|
+
let multipleQueriesPrepare = true;
|
|
279
|
+
let partList = [];
|
|
280
|
+
let lastChar = '\0';
|
|
281
|
+
|
|
282
|
+
let lastParameterPosition = 0;
|
|
283
|
+
|
|
284
|
+
let preValuePart1 = null;
|
|
285
|
+
let preValuePart2 = null;
|
|
286
|
+
let postValuePart = null;
|
|
287
|
+
|
|
288
|
+
let singleQuotes = false;
|
|
289
|
+
|
|
290
|
+
let isInParenthesis = 0;
|
|
291
|
+
let isFirstChar = true;
|
|
292
|
+
let isInsert = false;
|
|
293
|
+
let semicolon = false;
|
|
294
|
+
let hasParam = false;
|
|
295
|
+
let state = State.Normal;
|
|
296
|
+
|
|
297
|
+
let idx = 0;
|
|
298
|
+
let car = sql.charAt(idx++);
|
|
299
|
+
while (car !== '') {
|
|
300
|
+
if (
|
|
301
|
+
state === State.Escape &&
|
|
302
|
+
!((car === "'" && singleQuotes) || (car === '"' && !singleQuotes))
|
|
303
|
+
) {
|
|
304
|
+
state = State.String;
|
|
305
|
+
car = sql.charAt(idx++);
|
|
306
|
+
continue;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
switch (car) {
|
|
310
|
+
case '*':
|
|
311
|
+
if (state === State.Normal && lastChar == '/') {
|
|
312
|
+
state = State.SlashStarComment;
|
|
313
|
+
}
|
|
314
|
+
break;
|
|
315
|
+
|
|
316
|
+
case '/':
|
|
317
|
+
if (state === State.SlashStarComment && lastChar == '*') {
|
|
318
|
+
state = State.Normal;
|
|
319
|
+
}
|
|
320
|
+
break;
|
|
321
|
+
|
|
322
|
+
case '#':
|
|
323
|
+
if (state === State.Normal) {
|
|
324
|
+
state = State.EOLComment;
|
|
325
|
+
}
|
|
326
|
+
break;
|
|
327
|
+
|
|
328
|
+
case '-':
|
|
329
|
+
if (state === State.Normal && lastChar == '-') {
|
|
330
|
+
state = State.EOLComment;
|
|
331
|
+
}
|
|
332
|
+
break;
|
|
333
|
+
|
|
334
|
+
case '\n':
|
|
335
|
+
if (state === State.EOLComment) {
|
|
336
|
+
state = State.Normal;
|
|
337
|
+
}
|
|
338
|
+
break;
|
|
339
|
+
|
|
340
|
+
case '"':
|
|
341
|
+
if (state === State.Normal) {
|
|
342
|
+
state = State.String;
|
|
343
|
+
singleQuotes = false;
|
|
344
|
+
} else if (state === State.String && !singleQuotes) {
|
|
345
|
+
state = State.Normal;
|
|
346
|
+
} else if (state === State.Escape && !singleQuotes) {
|
|
347
|
+
state = State.String;
|
|
348
|
+
}
|
|
349
|
+
break;
|
|
350
|
+
case ';':
|
|
351
|
+
if (state === State.Normal) {
|
|
352
|
+
semicolon = true;
|
|
353
|
+
multipleQueriesPrepare = false;
|
|
354
|
+
}
|
|
355
|
+
break;
|
|
356
|
+
case "'":
|
|
357
|
+
if (state === State.Normal) {
|
|
358
|
+
state = State.String;
|
|
359
|
+
singleQuotes = true;
|
|
360
|
+
} else if (state === State.String && singleQuotes) {
|
|
361
|
+
state = State.Normal;
|
|
362
|
+
} else if (state === State.Escape && singleQuotes) {
|
|
363
|
+
state = State.String;
|
|
364
|
+
}
|
|
365
|
+
break;
|
|
366
|
+
|
|
367
|
+
case '\\':
|
|
368
|
+
if (state === State.String) {
|
|
369
|
+
state = State.Escape;
|
|
370
|
+
}
|
|
371
|
+
break;
|
|
372
|
+
|
|
373
|
+
case '?':
|
|
374
|
+
if (state === State.Normal) {
|
|
375
|
+
hasParam = true;
|
|
376
|
+
let part = sql.substring(lastParameterPosition, idx - 1);
|
|
377
|
+
lastParameterPosition = idx;
|
|
378
|
+
|
|
379
|
+
if (preValuePart1 === null) {
|
|
380
|
+
preValuePart1 = part;
|
|
381
|
+
preValuePart2 = '';
|
|
382
|
+
} else if (preValuePart2 === null) {
|
|
383
|
+
preValuePart2 = part;
|
|
384
|
+
} else {
|
|
385
|
+
if (postValuePart) {
|
|
386
|
+
//having parameters after the last ")" of value is not rewritable
|
|
387
|
+
reWritablePrepare = false;
|
|
388
|
+
partList.push(postValuePart + part);
|
|
389
|
+
postValuePart = null;
|
|
390
|
+
} else partList.push(part);
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
break;
|
|
394
|
+
case '`':
|
|
395
|
+
if (state === State.Backtick) {
|
|
396
|
+
state = State.Normal;
|
|
397
|
+
} else if (state === State.Normal) {
|
|
398
|
+
state = State.Backtick;
|
|
399
|
+
}
|
|
400
|
+
break;
|
|
401
|
+
|
|
402
|
+
case 's':
|
|
403
|
+
case 'S':
|
|
404
|
+
if (
|
|
405
|
+
state === State.Normal &&
|
|
406
|
+
postValuePart === null &&
|
|
407
|
+
sql.length > idx + 5 &&
|
|
408
|
+
(sql.charAt(idx) === 'e' || sql.charAt(idx) === 'E') &&
|
|
409
|
+
(sql.charAt(idx + 1) === 'l' || sql.charAt(idx + 1) === 'L') &&
|
|
410
|
+
(sql.charAt(idx + 2) === 'e' || sql.charAt(idx + 2) === 'E') &&
|
|
411
|
+
(sql.charAt(idx + 3) === 'c' || sql.charAt(idx + 3) === 'C') &&
|
|
412
|
+
(sql.charAt(idx + 4) === 't' || sql.charAt(idx + 4) === 'T')
|
|
413
|
+
) {
|
|
414
|
+
//field/table name might contain 'select'
|
|
415
|
+
if (
|
|
416
|
+
idx > 1 &&
|
|
417
|
+
sql.charAt(idx - 2) > ' ' &&
|
|
418
|
+
'();><=-+,'.indexOf(sql.charAt(idx - 2)) === -1
|
|
419
|
+
) {
|
|
420
|
+
break;
|
|
421
|
+
}
|
|
422
|
+
if (sql.charAt(idx + 5) > ' ' && '();><=-+,'.indexOf(sql.charAt(idx + 5)) === -1) {
|
|
423
|
+
break;
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
//SELECT queries, INSERT FROM SELECT not rewritable
|
|
427
|
+
reWritablePrepare = false;
|
|
428
|
+
}
|
|
429
|
+
break;
|
|
430
|
+
case 'v':
|
|
431
|
+
case 'V':
|
|
432
|
+
if (
|
|
433
|
+
state === State.Normal &&
|
|
434
|
+
!preValuePart1 &&
|
|
435
|
+
(lastChar == ')' || lastChar <= ' ') &&
|
|
436
|
+
sql.length > idx + 6 &&
|
|
437
|
+
(sql.charAt(idx) === 'a' || sql.charAt(idx) === 'A') &&
|
|
438
|
+
(sql.charAt(idx + 1) === 'l' || sql.charAt(idx + 1) === 'L') &&
|
|
439
|
+
(sql.charAt(idx + 2) === 'u' || sql.charAt(idx + 2) === 'U') &&
|
|
440
|
+
(sql.charAt(idx + 3) === 'e' || sql.charAt(idx + 3) === 'E') &&
|
|
441
|
+
(sql.charAt(idx + 4) === 's' || sql.charAt(idx + 4) === 'S') &&
|
|
442
|
+
(sql.charAt(idx + 5) === '(' || sql.charAt(idx + 5) <= ' ')
|
|
443
|
+
) {
|
|
444
|
+
idx += 5;
|
|
445
|
+
preValuePart1 = sql.substring(lastParameterPosition, idx);
|
|
446
|
+
lastParameterPosition = idx;
|
|
447
|
+
}
|
|
448
|
+
break;
|
|
449
|
+
case 'l':
|
|
450
|
+
case 'L':
|
|
451
|
+
if (
|
|
452
|
+
state === State.Normal &&
|
|
453
|
+
sql.length > idx + 13 &&
|
|
454
|
+
(sql.charAt(idx) === 'a' || sql.charAt(idx) === 'A') &&
|
|
455
|
+
(sql.charAt(idx + 1) === 's' || sql.charAt(idx + 1) === 'S') &&
|
|
456
|
+
(sql.charAt(idx + 2) === 't' || sql.charAt(idx + 2) === 'T') &&
|
|
457
|
+
sql.charAt(idx + 3) === '_' &&
|
|
458
|
+
(sql.charAt(idx + 4) === 'i' || sql.charAt(idx + 4) === 'I') &&
|
|
459
|
+
(sql.charAt(idx + 5) === 'n' || sql.charAt(idx + 5) === 'N') &&
|
|
460
|
+
(sql.charAt(idx + 6) === 's' || sql.charAt(idx + 6) === 'S') &&
|
|
461
|
+
(sql.charAt(idx + 7) === 'e' || sql.charAt(idx + 7) === 'E') &&
|
|
462
|
+
(sql.charAt(idx + 8) === 'r' || sql.charAt(idx + 8) === 'R') &&
|
|
463
|
+
(sql.charAt(idx + 9) === 't' || sql.charAt(idx + 9) === 'T') &&
|
|
464
|
+
sql.charAt(idx + 10) === '_' &&
|
|
465
|
+
(sql.charAt(idx + 11) === 'i' || sql.charAt(idx + 11) === 'I') &&
|
|
466
|
+
(sql.charAt(idx + 12) === 'd' || sql.charAt(idx + 12) === 'D') &&
|
|
467
|
+
sql.charAt(idx + 13) === '('
|
|
468
|
+
) {
|
|
469
|
+
reWritablePrepare = false;
|
|
470
|
+
idx += 13;
|
|
471
|
+
}
|
|
472
|
+
break;
|
|
473
|
+
case '(':
|
|
474
|
+
if (state === State.Normal) {
|
|
475
|
+
isInParenthesis++;
|
|
476
|
+
}
|
|
477
|
+
break;
|
|
478
|
+
case ')':
|
|
479
|
+
if (state === State.Normal) {
|
|
480
|
+
isInParenthesis--;
|
|
481
|
+
if (isInParenthesis === 0 && preValuePart2 !== null && postValuePart === null) {
|
|
482
|
+
postValuePart = sql.substring(lastParameterPosition, idx);
|
|
483
|
+
lastParameterPosition = idx;
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
break;
|
|
487
|
+
default:
|
|
488
|
+
if (state === State.Normal && isFirstChar && car > ' ') {
|
|
489
|
+
if (
|
|
490
|
+
(car === 'I' || car === 'i') &&
|
|
491
|
+
sql.length > idx + 6 &&
|
|
492
|
+
(sql.charAt(idx) === 'n' || sql.charAt(idx) === 'N') &&
|
|
493
|
+
(sql.charAt(idx + 1) === 's' || sql.charAt(idx + 1) === 'S') &&
|
|
494
|
+
(sql.charAt(idx + 2) === 'e' || sql.charAt(idx + 2) === 'E') &&
|
|
495
|
+
(sql.charAt(idx + 3) === 'r' || sql.charAt(idx + 3) === 'R') &&
|
|
496
|
+
(sql.charAt(idx + 4) === 't' || sql.charAt(idx + 4) === 'T') &&
|
|
497
|
+
(sql.charAt(idx + 5) === '(' || sql.charAt(idx + 5) <= ' ')
|
|
498
|
+
) {
|
|
499
|
+
isInsert = true;
|
|
500
|
+
}
|
|
501
|
+
isFirstChar = false;
|
|
502
|
+
}
|
|
503
|
+
//multiple queries
|
|
504
|
+
if (state === State.Normal && semicolon && car >= ' ') {
|
|
505
|
+
reWritablePrepare = false;
|
|
506
|
+
multipleQueriesPrepare = true;
|
|
507
|
+
}
|
|
508
|
+
break;
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
lastChar = car;
|
|
512
|
+
car = sql.charAt(idx++);
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
if (state === State.EOLComment) multipleQueriesPrepare = false;
|
|
516
|
+
|
|
517
|
+
if (!hasParam) {
|
|
518
|
+
//permit to have rewrite without parameter
|
|
519
|
+
if (preValuePart1 === null) {
|
|
520
|
+
partList.unshift('');
|
|
521
|
+
partList.unshift(sql);
|
|
522
|
+
} else {
|
|
523
|
+
partList.unshift(sql.substring(lastParameterPosition, idx));
|
|
524
|
+
partList.unshift(preValuePart1);
|
|
525
|
+
}
|
|
526
|
+
lastParameterPosition = idx;
|
|
527
|
+
} else {
|
|
528
|
+
partList.unshift(preValuePart2 !== null ? preValuePart2 : '');
|
|
529
|
+
partList.unshift(preValuePart1 !== null ? preValuePart1 : '');
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
if (!isInsert) {
|
|
533
|
+
reWritablePrepare = false;
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
//postValuePart is the value after the last parameter and parenthesis
|
|
537
|
+
//if no param, don't add to the list.
|
|
538
|
+
if (hasParam) {
|
|
539
|
+
partList.push(postValuePart !== null ? postValuePart : '');
|
|
540
|
+
}
|
|
541
|
+
partList.push(sql.substring(lastParameterPosition, idx));
|
|
542
|
+
|
|
543
|
+
return {
|
|
544
|
+
partList: partList,
|
|
545
|
+
reWritable: reWritablePrepare,
|
|
546
|
+
multipleQueries: multipleQueriesPrepare
|
|
547
|
+
};
|
|
548
|
+
};
|
|
549
|
+
|
|
550
|
+
module.exports.searchPlaceholder = function (sql, info, initialValues, displaySql) {
|
|
551
|
+
let sqlPlaceHolder = '';
|
|
552
|
+
const rowNumber = initialValues.length;
|
|
553
|
+
let values = new Array(rowNumber);
|
|
554
|
+
for (let i = 0; i < rowNumber; i++) values[i] = [];
|
|
555
|
+
let state = State.Normal;
|
|
556
|
+
let lastChar = '\0';
|
|
557
|
+
|
|
558
|
+
let singleQuotes = false;
|
|
559
|
+
let lastParameterPosition = 0;
|
|
560
|
+
|
|
561
|
+
let idx = 0;
|
|
562
|
+
let car = sql.charAt(idx++);
|
|
563
|
+
let placeholderName;
|
|
564
|
+
|
|
565
|
+
while (car !== '') {
|
|
566
|
+
if (
|
|
567
|
+
state === State.Escape &&
|
|
568
|
+
!((car === "'" && singleQuotes) || (car === '"' && !singleQuotes))
|
|
569
|
+
) {
|
|
570
|
+
state = State.String;
|
|
571
|
+
lastChar = car;
|
|
572
|
+
car = sql.charAt(idx++);
|
|
573
|
+
continue;
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
switch (car) {
|
|
577
|
+
case '*':
|
|
578
|
+
if (state === State.Normal && lastChar == '/') state = State.SlashStarComment;
|
|
579
|
+
break;
|
|
580
|
+
|
|
581
|
+
case '/':
|
|
582
|
+
if (state === State.SlashStarComment && lastChar == '*') state = State.Normal;
|
|
583
|
+
break;
|
|
584
|
+
|
|
585
|
+
case '#':
|
|
586
|
+
if (state === State.Normal) state = State.EOLComment;
|
|
587
|
+
break;
|
|
588
|
+
|
|
589
|
+
case '-':
|
|
590
|
+
if (state === State.Normal && lastChar == '-') {
|
|
591
|
+
state = State.EOLComment;
|
|
592
|
+
}
|
|
593
|
+
break;
|
|
594
|
+
|
|
595
|
+
case '\n':
|
|
596
|
+
if (state === State.EOLComment) {
|
|
597
|
+
state = State.Normal;
|
|
598
|
+
}
|
|
599
|
+
break;
|
|
600
|
+
|
|
601
|
+
case '"':
|
|
602
|
+
if (state === State.Normal) {
|
|
603
|
+
state = State.String;
|
|
604
|
+
singleQuotes = false;
|
|
605
|
+
} else if (state === State.String && !singleQuotes) {
|
|
606
|
+
state = State.Normal;
|
|
607
|
+
} else if (state === State.Escape && !singleQuotes) {
|
|
608
|
+
state = State.String;
|
|
609
|
+
}
|
|
610
|
+
break;
|
|
611
|
+
|
|
612
|
+
case "'":
|
|
613
|
+
if (state === State.Normal) {
|
|
614
|
+
state = State.String;
|
|
615
|
+
singleQuotes = true;
|
|
616
|
+
} else if (state === State.String && singleQuotes) {
|
|
617
|
+
state = State.Normal;
|
|
618
|
+
singleQuotes = false;
|
|
619
|
+
} else if (state === State.Escape && singleQuotes) {
|
|
620
|
+
state = State.String;
|
|
621
|
+
}
|
|
622
|
+
break;
|
|
623
|
+
|
|
624
|
+
case '\\':
|
|
625
|
+
if (state === State.String) state = State.Escape;
|
|
626
|
+
break;
|
|
627
|
+
|
|
628
|
+
case ':':
|
|
629
|
+
if (state === State.Normal) {
|
|
630
|
+
sqlPlaceHolder += sql.substring(lastParameterPosition, idx - 1) + '?';
|
|
631
|
+
placeholderName = '';
|
|
632
|
+
while (
|
|
633
|
+
((car = sql.charAt(idx++)) !== '' && car >= '0' && car <= '9') ||
|
|
634
|
+
(car >= 'A' && car <= 'Z') ||
|
|
635
|
+
(car >= 'a' && car <= 'z') ||
|
|
636
|
+
car === '-' ||
|
|
637
|
+
car === '_'
|
|
638
|
+
) {
|
|
639
|
+
placeholderName += car;
|
|
640
|
+
}
|
|
641
|
+
idx--;
|
|
642
|
+
for (let i = 0; i < rowNumber; i++) {
|
|
643
|
+
const val = initialValues[i][placeholderName];
|
|
644
|
+
if (val !== undefined) {
|
|
645
|
+
values[i].push(val);
|
|
646
|
+
} else {
|
|
647
|
+
values[i].push(null);
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
lastParameterPosition = idx;
|
|
651
|
+
}
|
|
652
|
+
break;
|
|
653
|
+
case '`':
|
|
654
|
+
if (state === State.Backtick) {
|
|
655
|
+
state = State.Normal;
|
|
656
|
+
} else if (state === State.Normal) {
|
|
657
|
+
state = State.Backtick;
|
|
658
|
+
}
|
|
659
|
+
}
|
|
660
|
+
lastChar = car;
|
|
661
|
+
|
|
662
|
+
car = sql.charAt(idx++);
|
|
663
|
+
}
|
|
664
|
+
if (lastParameterPosition === 0) {
|
|
665
|
+
sqlPlaceHolder = sql;
|
|
666
|
+
} else {
|
|
667
|
+
sqlPlaceHolder += sql.substring(lastParameterPosition);
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
return { sql: sqlPlaceHolder, values: values };
|
|
671
|
+
};
|
|
672
|
+
|
|
673
|
+
/**
|
|
674
|
+
* Split query according to named parameters.
|
|
675
|
+
*
|
|
676
|
+
* The only rewritten queries follow these notation: INSERT [LOW_PRIORITY | DELAYED |
|
|
677
|
+
* HIGH_PRIORITY] [IGNORE] [INTO] tbl_name [PARTITION (partition_list)] [(col,...)] {VALUES |
|
|
678
|
+
* VALUE} (...) [ ON DUPLICATE KEY UPDATE col=expr [, col=expr] ... ] With expr without
|
|
679
|
+
* parameter.
|
|
680
|
+
*
|
|
681
|
+
* Query with INSERT ... SELECT / containing LAST_INSERT_ID() will not be rewritten
|
|
682
|
+
*
|
|
683
|
+
* query parts will be split this way :
|
|
684
|
+
* - pre-value part
|
|
685
|
+
* - after value part
|
|
686
|
+
* [- after parameter part] (after each parameter)
|
|
687
|
+
* - ending part
|
|
688
|
+
*
|
|
689
|
+
* example : INSERT INTO MyTABLE VALUES (9, :param1, 5, :param2, 8) ON DUPLICATE KEY UPDATE col2=col2+10
|
|
690
|
+
* will result in :
|
|
691
|
+
* - pre-value : "INSERT INTO MyTABLE VALUES"
|
|
692
|
+
* - after value : " (9, "
|
|
693
|
+
* - after parameter : ", 5, "
|
|
694
|
+
* - after parameter : ", 8)"
|
|
695
|
+
* - ending : " ON DUPLICATE KEY UPDATE col2=col2+10"
|
|
696
|
+
*
|
|
697
|
+
*
|
|
698
|
+
* @returns {JSON} query separated by parameters
|
|
699
|
+
*/
|
|
700
|
+
module.exports.splitRewritableNamedParameterQuery = function (sql, initialValues) {
|
|
701
|
+
let reWritablePrepare = true;
|
|
702
|
+
let multipleQueriesPrepare = true;
|
|
703
|
+
let partList = [];
|
|
704
|
+
let values = new Array(initialValues.length);
|
|
705
|
+
for (let i = 0; i < values.length; i++) values[i] = [];
|
|
706
|
+
let lastChar = '\0';
|
|
707
|
+
|
|
708
|
+
let lastParameterPosition = 0;
|
|
709
|
+
|
|
710
|
+
let preValuePart1 = null;
|
|
711
|
+
let preValuePart2 = null;
|
|
712
|
+
let postValuePart = null;
|
|
713
|
+
|
|
714
|
+
let singleQuotes = false;
|
|
715
|
+
|
|
716
|
+
let isInParenthesis = 0;
|
|
717
|
+
let isFirstChar = true;
|
|
718
|
+
let isInsert = false;
|
|
719
|
+
let semicolon = false;
|
|
720
|
+
let hasParam = false;
|
|
721
|
+
let placeholderName;
|
|
722
|
+
let state = State.Normal;
|
|
723
|
+
|
|
724
|
+
let idx = 0;
|
|
725
|
+
let car = sql.charAt(idx++);
|
|
726
|
+
while (car !== '') {
|
|
727
|
+
if (
|
|
728
|
+
state === State.Escape &&
|
|
729
|
+
!((car === "'" && singleQuotes) || (car === '"' && !singleQuotes))
|
|
730
|
+
) {
|
|
731
|
+
state = State.String;
|
|
732
|
+
car = sql.charAt(idx++);
|
|
733
|
+
continue;
|
|
734
|
+
}
|
|
735
|
+
|
|
736
|
+
switch (car) {
|
|
737
|
+
case '*':
|
|
738
|
+
if (state === State.Normal && lastChar == '/') {
|
|
739
|
+
state = State.SlashStarComment;
|
|
740
|
+
}
|
|
741
|
+
break;
|
|
742
|
+
|
|
743
|
+
case '/':
|
|
744
|
+
if (state === State.SlashStarComment && lastChar == '*') {
|
|
745
|
+
state = State.Normal;
|
|
746
|
+
}
|
|
747
|
+
break;
|
|
748
|
+
|
|
749
|
+
case '#':
|
|
750
|
+
if (state === State.Normal) {
|
|
751
|
+
state = State.EOLComment;
|
|
752
|
+
}
|
|
753
|
+
break;
|
|
754
|
+
|
|
755
|
+
case '-':
|
|
756
|
+
if (state === State.Normal && lastChar == '-') {
|
|
757
|
+
state = State.EOLComment;
|
|
758
|
+
}
|
|
759
|
+
break;
|
|
760
|
+
|
|
761
|
+
case '\n':
|
|
762
|
+
if (state === State.EOLComment) {
|
|
763
|
+
state = State.Normal;
|
|
764
|
+
}
|
|
765
|
+
break;
|
|
766
|
+
|
|
767
|
+
case '"':
|
|
768
|
+
if (state === State.Normal) {
|
|
769
|
+
state = State.String;
|
|
770
|
+
singleQuotes = false;
|
|
771
|
+
} else if (state === State.String && !singleQuotes) {
|
|
772
|
+
state = State.Normal;
|
|
773
|
+
} else if (state === State.Escape && !singleQuotes) {
|
|
774
|
+
state = State.String;
|
|
775
|
+
}
|
|
776
|
+
break;
|
|
777
|
+
case ';':
|
|
778
|
+
if (state === State.Normal) {
|
|
779
|
+
semicolon = true;
|
|
780
|
+
multipleQueriesPrepare = false;
|
|
781
|
+
}
|
|
782
|
+
break;
|
|
783
|
+
case "'":
|
|
784
|
+
if (state === State.Normal) {
|
|
785
|
+
state = State.String;
|
|
786
|
+
singleQuotes = true;
|
|
787
|
+
} else if (state === State.String && singleQuotes) {
|
|
788
|
+
state = State.Normal;
|
|
789
|
+
} else if (state === State.Escape && singleQuotes) {
|
|
790
|
+
state = State.String;
|
|
791
|
+
}
|
|
792
|
+
break;
|
|
793
|
+
|
|
794
|
+
case '\\':
|
|
795
|
+
if (state === State.String) {
|
|
796
|
+
state = State.Escape;
|
|
797
|
+
}
|
|
798
|
+
break;
|
|
799
|
+
|
|
800
|
+
case ':':
|
|
801
|
+
if (state === State.Normal) {
|
|
802
|
+
let part = sql.substring(lastParameterPosition, idx - 1);
|
|
803
|
+
placeholderName = '';
|
|
804
|
+
while (
|
|
805
|
+
((car = sql.charAt(idx++)) !== '' && car >= '0' && car <= '9') ||
|
|
806
|
+
(car >= 'A' && car <= 'Z') ||
|
|
807
|
+
(car >= 'a' && car <= 'z') ||
|
|
808
|
+
car === '-' ||
|
|
809
|
+
car === '_'
|
|
810
|
+
) {
|
|
811
|
+
placeholderName += car;
|
|
812
|
+
}
|
|
813
|
+
idx--;
|
|
814
|
+
hasParam = true;
|
|
815
|
+
initialValues.forEach((row, idx) => {
|
|
816
|
+
if (row[placeholderName] !== undefined) {
|
|
817
|
+
values[idx].push(row[placeholderName]);
|
|
818
|
+
} else {
|
|
819
|
+
values[idx].push(null);
|
|
820
|
+
}
|
|
821
|
+
});
|
|
822
|
+
|
|
823
|
+
lastParameterPosition = idx;
|
|
824
|
+
|
|
825
|
+
if (preValuePart1 === null) {
|
|
826
|
+
preValuePart1 = part;
|
|
827
|
+
preValuePart2 = '';
|
|
828
|
+
} else if (preValuePart2 === null) {
|
|
829
|
+
preValuePart2 = part;
|
|
830
|
+
} else {
|
|
831
|
+
if (postValuePart) {
|
|
832
|
+
//having parameters after the last ")" of value is not rewritable
|
|
833
|
+
reWritablePrepare = false;
|
|
834
|
+
partList.push(postValuePart + part);
|
|
835
|
+
postValuePart = null;
|
|
836
|
+
} else partList.push(part);
|
|
837
|
+
}
|
|
838
|
+
}
|
|
839
|
+
break;
|
|
840
|
+
|
|
841
|
+
case '`':
|
|
842
|
+
if (state === State.Backtick) {
|
|
843
|
+
state = State.Normal;
|
|
844
|
+
} else if (state === State.Normal) {
|
|
845
|
+
state = State.Backtick;
|
|
846
|
+
}
|
|
847
|
+
break;
|
|
848
|
+
|
|
849
|
+
case 's':
|
|
850
|
+
case 'S':
|
|
851
|
+
if (
|
|
852
|
+
state === State.Normal &&
|
|
853
|
+
postValuePart === null &&
|
|
854
|
+
sql.length > idx + 5 &&
|
|
855
|
+
(sql.charAt(idx) === 'e' || sql.charAt(idx) === 'E') &&
|
|
856
|
+
(sql.charAt(idx + 1) === 'l' || sql.charAt(idx + 1) === 'L') &&
|
|
857
|
+
(sql.charAt(idx + 2) === 'e' || sql.charAt(idx + 2) === 'E') &&
|
|
858
|
+
(sql.charAt(idx + 3) === 'c' || sql.charAt(idx + 3) === 'C') &&
|
|
859
|
+
(sql.charAt(idx + 4) === 't' || sql.charAt(idx + 4) === 'T')
|
|
860
|
+
) {
|
|
861
|
+
//field/table name might contain 'select'
|
|
862
|
+
if (
|
|
863
|
+
idx > 1 &&
|
|
864
|
+
sql.charAt(idx - 2) > ' ' &&
|
|
865
|
+
'();><=-+,'.indexOf(sql.charAt(idx - 2)) === -1
|
|
866
|
+
) {
|
|
867
|
+
break;
|
|
868
|
+
}
|
|
869
|
+
if (sql.charAt(idx + 5) > ' ' && '();><=-+,'.indexOf(sql.charAt(idx + 5)) === -1) {
|
|
870
|
+
break;
|
|
871
|
+
}
|
|
872
|
+
|
|
873
|
+
//SELECT queries, INSERT FROM SELECT not rewritable
|
|
874
|
+
reWritablePrepare = false;
|
|
875
|
+
}
|
|
876
|
+
break;
|
|
877
|
+
case 'v':
|
|
878
|
+
case 'V':
|
|
879
|
+
if (
|
|
880
|
+
state === State.Normal &&
|
|
881
|
+
!preValuePart1 &&
|
|
882
|
+
(lastChar == ')' || lastChar <= ' ') &&
|
|
883
|
+
sql.length > idx + 6 &&
|
|
884
|
+
(sql.charAt(idx) === 'a' || sql.charAt(idx) === 'A') &&
|
|
885
|
+
(sql.charAt(idx + 1) === 'l' || sql.charAt(idx + 1) === 'L') &&
|
|
886
|
+
(sql.charAt(idx + 2) === 'u' || sql.charAt(idx + 2) === 'U') &&
|
|
887
|
+
(sql.charAt(idx + 3) === 'e' || sql.charAt(idx + 3) === 'E') &&
|
|
888
|
+
(sql.charAt(idx + 4) === 's' || sql.charAt(idx + 4) === 'S') &&
|
|
889
|
+
(sql.charAt(idx + 5) === '(' || sql.charAt(idx + 5) <= ' ')
|
|
890
|
+
) {
|
|
891
|
+
idx += 5;
|
|
892
|
+
preValuePart1 = sql.substring(lastParameterPosition, idx);
|
|
893
|
+
lastParameterPosition = idx;
|
|
894
|
+
}
|
|
895
|
+
break;
|
|
896
|
+
case 'l':
|
|
897
|
+
case 'L':
|
|
898
|
+
if (
|
|
899
|
+
state === State.Normal &&
|
|
900
|
+
sql.length > idx + 13 &&
|
|
901
|
+
(sql.charAt(idx) === 'a' || sql.charAt(idx) === 'A') &&
|
|
902
|
+
(sql.charAt(idx + 1) === 's' || sql.charAt(idx + 1) === 'S') &&
|
|
903
|
+
(sql.charAt(idx + 2) === 't' || sql.charAt(idx + 2) === 'T') &&
|
|
904
|
+
sql.charAt(idx + 3) === '_' &&
|
|
905
|
+
(sql.charAt(idx + 4) === 'i' || sql.charAt(idx + 4) === 'I') &&
|
|
906
|
+
(sql.charAt(idx + 5) === 'n' || sql.charAt(idx + 5) === 'N') &&
|
|
907
|
+
(sql.charAt(idx + 6) === 's' || sql.charAt(idx + 6) === 'S') &&
|
|
908
|
+
(sql.charAt(idx + 7) === 'e' || sql.charAt(idx + 7) === 'E') &&
|
|
909
|
+
(sql.charAt(idx + 8) === 'r' || sql.charAt(idx + 8) === 'R') &&
|
|
910
|
+
(sql.charAt(idx + 9) === 't' || sql.charAt(idx + 9) === 'T') &&
|
|
911
|
+
sql.charAt(idx + 10) === '_' &&
|
|
912
|
+
(sql.charAt(idx + 11) === 'i' || sql.charAt(idx + 11) === 'I') &&
|
|
913
|
+
(sql.charAt(idx + 12) === 'd' || sql.charAt(idx + 12) === 'D') &&
|
|
914
|
+
sql.charAt(idx + 13) === '('
|
|
915
|
+
) {
|
|
916
|
+
reWritablePrepare = false;
|
|
917
|
+
idx += 13;
|
|
918
|
+
}
|
|
919
|
+
break;
|
|
920
|
+
case '(':
|
|
921
|
+
if (state === State.Normal) {
|
|
922
|
+
isInParenthesis++;
|
|
923
|
+
}
|
|
924
|
+
break;
|
|
925
|
+
case ')':
|
|
926
|
+
if (state === State.Normal) {
|
|
927
|
+
isInParenthesis--;
|
|
928
|
+
if (isInParenthesis === 0 && preValuePart2 !== null && postValuePart === null) {
|
|
929
|
+
postValuePart = sql.substring(lastParameterPosition, idx);
|
|
930
|
+
lastParameterPosition = idx;
|
|
931
|
+
}
|
|
932
|
+
}
|
|
933
|
+
break;
|
|
934
|
+
default:
|
|
935
|
+
if (state === State.Normal && isFirstChar && car > ' ') {
|
|
936
|
+
if (
|
|
937
|
+
(car === 'I' || car === 'i') &&
|
|
938
|
+
sql.length > idx + 6 &&
|
|
939
|
+
(sql.charAt(idx) === 'n' || sql.charAt(idx) === 'N') &&
|
|
940
|
+
(sql.charAt(idx + 1) === 's' || sql.charAt(idx + 1) === 'S') &&
|
|
941
|
+
(sql.charAt(idx + 2) === 'e' || sql.charAt(idx + 2) === 'E') &&
|
|
942
|
+
(sql.charAt(idx + 3) === 'r' || sql.charAt(idx + 3) === 'R') &&
|
|
943
|
+
(sql.charAt(idx + 4) === 't' || sql.charAt(idx + 4) === 'T') &&
|
|
944
|
+
(sql.charAt(idx + 5) === '(' || sql.charAt(idx + 5) <= ' ')
|
|
945
|
+
) {
|
|
946
|
+
isInsert = true;
|
|
947
|
+
}
|
|
948
|
+
isFirstChar = false;
|
|
949
|
+
}
|
|
950
|
+
//multiple queries
|
|
951
|
+
if (state === State.Normal && semicolon && car >= ' ') {
|
|
952
|
+
reWritablePrepare = false;
|
|
953
|
+
multipleQueriesPrepare = true;
|
|
954
|
+
}
|
|
955
|
+
break;
|
|
956
|
+
}
|
|
957
|
+
|
|
958
|
+
lastChar = car;
|
|
959
|
+
car = sql.charAt(idx++);
|
|
960
|
+
}
|
|
961
|
+
|
|
962
|
+
if (state === State.EOLComment) multipleQueriesPrepare = false;
|
|
963
|
+
|
|
964
|
+
if (!hasParam) {
|
|
965
|
+
//permit to have rewrite without parameter
|
|
966
|
+
if (preValuePart1 === null) {
|
|
967
|
+
partList.unshift('');
|
|
968
|
+
partList.unshift(sql);
|
|
969
|
+
} else {
|
|
970
|
+
partList.unshift(sql.substring(lastParameterPosition, idx));
|
|
971
|
+
partList.unshift(preValuePart1);
|
|
972
|
+
}
|
|
973
|
+
lastParameterPosition = idx;
|
|
974
|
+
} else {
|
|
975
|
+
partList.unshift(preValuePart2 !== null ? preValuePart2 : '');
|
|
976
|
+
partList.unshift(preValuePart1 !== null ? preValuePart1 : '');
|
|
977
|
+
}
|
|
978
|
+
|
|
979
|
+
if (!isInsert) {
|
|
980
|
+
reWritablePrepare = false;
|
|
981
|
+
}
|
|
982
|
+
|
|
983
|
+
//postValuePart is the value after the last parameter and parenthesis
|
|
984
|
+
//if no param, don't add to the list.
|
|
985
|
+
if (hasParam) {
|
|
986
|
+
partList.push(postValuePart !== null ? postValuePart : '');
|
|
987
|
+
}
|
|
988
|
+
partList.push(sql.substring(lastParameterPosition, idx));
|
|
989
|
+
|
|
990
|
+
return {
|
|
991
|
+
partList: partList,
|
|
992
|
+
reWritable: reWritablePrepare,
|
|
993
|
+
multipleQueries: multipleQueriesPrepare,
|
|
994
|
+
values: values
|
|
995
|
+
};
|
|
996
|
+
};
|
|
997
|
+
|
|
998
|
+
/**
|
|
999
|
+
* Ensure that filename requested by server corresponds to query
|
|
1000
|
+
* protocol : https://mariadb.com/kb/en/library/local_infile-packet/
|
|
1001
|
+
*
|
|
1002
|
+
* @param sql query
|
|
1003
|
+
* @param parameters parameters if any
|
|
1004
|
+
* @param fileName server requested file
|
|
1005
|
+
* @returns {boolean} is filename corresponding to query
|
|
1006
|
+
*/
|
|
1007
|
+
module.exports.validateFileName = function (sql, parameters, fileName) {
|
|
1008
|
+
// in case of windows, file name in query are escaped
|
|
1009
|
+
// so for example LOAD DATA LOCAL INFILE 'C:\\Temp\\myFile.txt' ...
|
|
1010
|
+
// but server return 'C:\Temp\myFile.txt'
|
|
1011
|
+
// so with regex escaped, must test LOAD DATA LOCAL INFILE 'C:\\\\Temp\\\\myFile.txt'
|
|
1012
|
+
let queryValidator = new RegExp(
|
|
1013
|
+
"^(\\s*\\/\\*([^\\*]|\\*[^\\/])*\\*\\/)*\\s*LOAD\\s+DATA\\s+((LOW_PRIORITY|CONCURRENT)\\s+)?LOCAL\\s+INFILE\\s+'" +
|
|
1014
|
+
fileName.replace(/\\/g, '\\\\\\\\').replace('.', '\\.') +
|
|
1015
|
+
"'",
|
|
1016
|
+
'i'
|
|
1017
|
+
);
|
|
1018
|
+
if (queryValidator.test(sql)) return true;
|
|
1019
|
+
|
|
1020
|
+
if (parameters != null) {
|
|
1021
|
+
queryValidator = new RegExp(
|
|
1022
|
+
'^(\\s*\\/\\*([^\\*]|\\*[^\\/])*\\*\\/)*\\s*LOAD\\s+DATA\\s+((LOW_PRIORITY|CONCURRENT)\\s+)?LOCAL\\s+INFILE\\s+\\?',
|
|
1023
|
+
'i'
|
|
1024
|
+
);
|
|
1025
|
+
if (queryValidator.test(sql) && parameters.length > 0) {
|
|
1026
|
+
if (Array.isArray(parameters)) {
|
|
1027
|
+
return parameters[0].toLowerCase() === fileName.toLowerCase();
|
|
1028
|
+
}
|
|
1029
|
+
return parameters.toLowerCase() === fileName.toLowerCase();
|
|
1030
|
+
}
|
|
1031
|
+
}
|
|
1032
|
+
return false;
|
|
1033
|
+
};
|