@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,278 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const CommonBinary = require('./common-binary-cmd');
|
|
4
|
+
const Errors = require('../misc/errors');
|
|
5
|
+
const Parse = require('../misc/parse');
|
|
6
|
+
const BulkPacket = require('../io/bulk-packet');
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Protocol COM_STMT_BULK_EXECUTE
|
|
10
|
+
* see : https://mariadb.com/kb/en/library/com_stmt_bulk_execute/
|
|
11
|
+
*/
|
|
12
|
+
class BatchBulk extends CommonBinary {
|
|
13
|
+
constructor(resolve, reject, options, connOpts, sql, values) {
|
|
14
|
+
super(resolve, reject, options, connOpts, sql, values);
|
|
15
|
+
this.onPacketReceive = this.readPrepareResultPacket;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Send COM_STMT_BULK_EXECUTE
|
|
20
|
+
*
|
|
21
|
+
* @param out output writer
|
|
22
|
+
* @param opts connection options
|
|
23
|
+
* @param info connection information
|
|
24
|
+
*/
|
|
25
|
+
start(out, opts, info) {
|
|
26
|
+
this.sending = true;
|
|
27
|
+
this.info = info;
|
|
28
|
+
this.values = this.initialValues;
|
|
29
|
+
|
|
30
|
+
if (this.opts.timeout) {
|
|
31
|
+
const err = Errors.createError(
|
|
32
|
+
'Cannot use timeout for Batch statement',
|
|
33
|
+
this.sql,
|
|
34
|
+
false,
|
|
35
|
+
info,
|
|
36
|
+
'HY000',
|
|
37
|
+
Errors.ER_TIMEOUT_NOT_SUPPORTED
|
|
38
|
+
);
|
|
39
|
+
this.emit('send_end');
|
|
40
|
+
this.throwError(err, info);
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
let questionMarkSql = this.sql;
|
|
45
|
+
if (this.opts.namedPlaceholders) {
|
|
46
|
+
const res = Parse.searchPlaceholder(
|
|
47
|
+
this.sql,
|
|
48
|
+
info,
|
|
49
|
+
this.initialValues,
|
|
50
|
+
this.displaySql.bind(this)
|
|
51
|
+
);
|
|
52
|
+
questionMarkSql = res.sql;
|
|
53
|
+
this.values = res.values;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
if (!this.validateParameters(info)) {
|
|
57
|
+
this.sending = false;
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
//send COM_STMT_PREPARE command
|
|
62
|
+
this.out = out;
|
|
63
|
+
this.packet = new BulkPacket(this.opts, out, this.values[0]);
|
|
64
|
+
|
|
65
|
+
out.startPacket(this);
|
|
66
|
+
out.writeInt8(0x16);
|
|
67
|
+
out.writeString(questionMarkSql);
|
|
68
|
+
out.flushBuffer(true);
|
|
69
|
+
|
|
70
|
+
if (this.opts.pipelining) {
|
|
71
|
+
out.startPacket(this);
|
|
72
|
+
this.valueIdx = 0;
|
|
73
|
+
this.sendQueries();
|
|
74
|
+
} else {
|
|
75
|
+
this.out = out;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
sendQueries() {
|
|
80
|
+
let flushed = false;
|
|
81
|
+
while (!flushed && this.sending && this.valueIdx < this.values.length) {
|
|
82
|
+
this.valueRow = this.values[this.valueIdx++];
|
|
83
|
+
|
|
84
|
+
//********************************************
|
|
85
|
+
// send params
|
|
86
|
+
//********************************************
|
|
87
|
+
const len = this.valueRow.length;
|
|
88
|
+
for (let i = 0; i < len; i++) {
|
|
89
|
+
const value = this.valueRow[i];
|
|
90
|
+
if (value === null) {
|
|
91
|
+
flushed = this.packet.writeInt8(0x01) || flushed;
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
//********************************************
|
|
96
|
+
// param has no stream. directly write in buffer
|
|
97
|
+
//********************************************
|
|
98
|
+
flushed = this.writeParam(this.packet, value, this.opts, this.info) || flushed;
|
|
99
|
+
}
|
|
100
|
+
const last = this.valueIdx === this.values.length;
|
|
101
|
+
flushed = this.packet.mark(last, last ? null : this.values[this.valueIdx]) || flushed;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if (this.valueIdx < this.values.length && !this.packet.haveErrorResponse) {
|
|
105
|
+
//there is still data to send
|
|
106
|
+
setImmediate(this.sendQueries.bind(this));
|
|
107
|
+
} else {
|
|
108
|
+
if (this.sending && this.valueIdx === this.values.length) this.emit('send_end');
|
|
109
|
+
this.sending = false;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
displaySql() {
|
|
114
|
+
if (this.opts && this.initialValues) {
|
|
115
|
+
if (this.sql.length > this.opts.debugLen) {
|
|
116
|
+
return this.sql.substring(0, this.opts.debugLen) + '...';
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
let sqlMsg = this.sql + ' - parameters:';
|
|
120
|
+
sqlMsg += '[';
|
|
121
|
+
for (let i = 0; i < this.initialValues.length; i++) {
|
|
122
|
+
if (i !== 0) sqlMsg += ',';
|
|
123
|
+
let param = this.initialValues[i];
|
|
124
|
+
sqlMsg = this.logParameters(sqlMsg, param);
|
|
125
|
+
if (sqlMsg.length > this.opts.debugLen) {
|
|
126
|
+
sqlMsg = sqlMsg.substr(0, this.opts.debugLen) + '...';
|
|
127
|
+
break;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
sqlMsg += ']';
|
|
131
|
+
return sqlMsg;
|
|
132
|
+
}
|
|
133
|
+
return this.sql + ' - parameters:[]';
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
success(val) {
|
|
137
|
+
this.packet.waitingResponseNo--;
|
|
138
|
+
|
|
139
|
+
if (!this.opts.pipelining && this.packet.statementId === -1) {
|
|
140
|
+
this.packet.statementId = this.statementId;
|
|
141
|
+
this.out.startPacket(this);
|
|
142
|
+
this.valueIdx = 0;
|
|
143
|
+
this.sendQueries();
|
|
144
|
+
this._responseIndex++;
|
|
145
|
+
this.onPacketReceive = this.readResponsePacket;
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
if (!this.sending && this.packet.waitingResponseNo === 0) {
|
|
150
|
+
//send COM_STMT_CLOSE packet
|
|
151
|
+
if (!this.firstError || !this.firstError.fatal) {
|
|
152
|
+
this.sequenceNo = -1;
|
|
153
|
+
this.compressSequenceNo = -1;
|
|
154
|
+
this.out.startPacket(this);
|
|
155
|
+
this.out.writeInt8(0x19);
|
|
156
|
+
this.out.writeInt32(this.statementId);
|
|
157
|
+
this.out.flushBuffer(true);
|
|
158
|
+
}
|
|
159
|
+
this.sending = false;
|
|
160
|
+
this.emit('send_end');
|
|
161
|
+
|
|
162
|
+
if (this.packet.haveErrorResponse) {
|
|
163
|
+
this.packet = null;
|
|
164
|
+
this.resolve = null;
|
|
165
|
+
this.onPacketReceive = null;
|
|
166
|
+
this._columns = null;
|
|
167
|
+
this._rows = null;
|
|
168
|
+
process.nextTick(this.reject, this.firstError);
|
|
169
|
+
this.reject = null;
|
|
170
|
+
this.emit('end', this.firstError);
|
|
171
|
+
} else {
|
|
172
|
+
this.packet = null;
|
|
173
|
+
let totalAffectedRows = 0;
|
|
174
|
+
this._rows.forEach((row) => {
|
|
175
|
+
totalAffectedRows += row.affectedRows;
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
const rs = {
|
|
179
|
+
affectedRows: totalAffectedRows,
|
|
180
|
+
insertId: this._rows[0].insertId,
|
|
181
|
+
warningStatus: this._rows[this._rows.length - 1].warningStatus
|
|
182
|
+
};
|
|
183
|
+
this.successEnd(rs);
|
|
184
|
+
this._columns = null;
|
|
185
|
+
this._rows = null;
|
|
186
|
+
}
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
if (!this.packet.haveErrorResponse) {
|
|
191
|
+
this._responseIndex++;
|
|
192
|
+
this.onPacketReceive = this.readResponsePacket;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
throwError(err, info) {
|
|
197
|
+
this.packet.waitingResponseNo--;
|
|
198
|
+
this.sending = false;
|
|
199
|
+
if (this.packet && !this.packet.haveErrorResponse) {
|
|
200
|
+
if (err.fatal) {
|
|
201
|
+
this.packet.waitingResponseNo = 0;
|
|
202
|
+
}
|
|
203
|
+
if (this.stack) {
|
|
204
|
+
err = Errors.createError(
|
|
205
|
+
err.message,
|
|
206
|
+
this.sql,
|
|
207
|
+
err.fatal,
|
|
208
|
+
info,
|
|
209
|
+
err.sqlState,
|
|
210
|
+
err.errno,
|
|
211
|
+
this.stack,
|
|
212
|
+
false
|
|
213
|
+
);
|
|
214
|
+
}
|
|
215
|
+
this.firstError = err;
|
|
216
|
+
this.packet.endedWithError();
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
if (!this.sending && this.packet.waitingResponseNo === 0) {
|
|
220
|
+
this.resolve = null;
|
|
221
|
+
|
|
222
|
+
//send COM_STMT_CLOSE packet
|
|
223
|
+
if (!err.fatal && this.statementId) {
|
|
224
|
+
this.sequenceNo = -1;
|
|
225
|
+
this.compressSequenceNo = -1;
|
|
226
|
+
this.out.startPacket(this);
|
|
227
|
+
this.out.writeInt8(0x19);
|
|
228
|
+
this.out.writeInt32(this.statementId);
|
|
229
|
+
this.out.flushBuffer(true);
|
|
230
|
+
}
|
|
231
|
+
this.emit('send_end');
|
|
232
|
+
process.nextTick(this.reject, this.firstError);
|
|
233
|
+
this.reject = null;
|
|
234
|
+
this.onPacketReceive = null;
|
|
235
|
+
this.emit('end', this.firstError);
|
|
236
|
+
} else {
|
|
237
|
+
this._responseIndex++;
|
|
238
|
+
this.onPacketReceive = this.readResponsePacket;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* Validate that parameters exists and are defined.
|
|
244
|
+
*
|
|
245
|
+
* @param info connection info
|
|
246
|
+
* @returns {boolean} return false if any error occur.
|
|
247
|
+
*/
|
|
248
|
+
validateParameters(info) {
|
|
249
|
+
//validate parameter size.
|
|
250
|
+
for (let r = 0; r < this.values.length; r++) {
|
|
251
|
+
if (!Array.isArray(this.values[r])) this.values[r] = [this.values[r]];
|
|
252
|
+
|
|
253
|
+
//validate parameter is defined.
|
|
254
|
+
for (let i = 0; i < this.values[r].length; i++) {
|
|
255
|
+
if (this.values[r][i] === undefined) {
|
|
256
|
+
this.emit('send_end');
|
|
257
|
+
this.throwNewError(
|
|
258
|
+
'Parameter at position ' +
|
|
259
|
+
(i + 1) +
|
|
260
|
+
' is undefined for values ' +
|
|
261
|
+
r +
|
|
262
|
+
'\n' +
|
|
263
|
+
this.displaySql(),
|
|
264
|
+
false,
|
|
265
|
+
info,
|
|
266
|
+
'HY000',
|
|
267
|
+
Errors.ER_PARAMETER_UNDEFINED
|
|
268
|
+
);
|
|
269
|
+
return false;
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
return true;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
module.exports = BatchBulk;
|
|
@@ -0,0 +1,372 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const CommonText = require('./common-text-cmd');
|
|
4
|
+
const Errors = require('../misc/errors');
|
|
5
|
+
const Parse = require('../misc/parse');
|
|
6
|
+
const RewritePacket = require('../io/rewrite-packet');
|
|
7
|
+
const QUOTE = 0x27;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Protocol COM_QUERY
|
|
11
|
+
* see : https://mariadb.com/kb/en/library/com_query/
|
|
12
|
+
*/
|
|
13
|
+
class BatchRewrite extends CommonText {
|
|
14
|
+
constructor(resolve, reject, options, connOpts, sql, values) {
|
|
15
|
+
super(resolve, reject, options, connOpts, sql, values);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Send COM_QUERY
|
|
20
|
+
*
|
|
21
|
+
* @param out output writer
|
|
22
|
+
* @param opts connection options
|
|
23
|
+
* @param info connection information
|
|
24
|
+
*/
|
|
25
|
+
start(out, opts, info) {
|
|
26
|
+
this.sending = true;
|
|
27
|
+
this.info = info;
|
|
28
|
+
if (this.opts.timeout) {
|
|
29
|
+
const err = Errors.createError(
|
|
30
|
+
'Cannot use timeout for Batch statement',
|
|
31
|
+
this.sql,
|
|
32
|
+
false,
|
|
33
|
+
info,
|
|
34
|
+
'HY000',
|
|
35
|
+
Errors.ER_TIMEOUT_NOT_SUPPORTED
|
|
36
|
+
);
|
|
37
|
+
this.emit('send_end');
|
|
38
|
+
this.throwError(err, info);
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (this.initialValues.length === 0) this.initialValues = [[]];
|
|
43
|
+
if (this.opts.namedPlaceholders) {
|
|
44
|
+
this.parseResults = Parse.splitRewritableNamedParameterQuery(this.sql, this.initialValues);
|
|
45
|
+
this.values = this.parseResults.values;
|
|
46
|
+
} else {
|
|
47
|
+
this.parseResults = Parse.splitRewritableQuery(this.sql);
|
|
48
|
+
this.values = this.initialValues;
|
|
49
|
+
if (!this.validateParameters(info)) {
|
|
50
|
+
this.sending = false;
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
out.startPacket(this);
|
|
56
|
+
this.packet = new RewritePacket(
|
|
57
|
+
this.opts.maxAllowedPacket,
|
|
58
|
+
out,
|
|
59
|
+
this.parseResults.partList[0],
|
|
60
|
+
this.parseResults.partList[this.parseResults.partList.length - 1]
|
|
61
|
+
);
|
|
62
|
+
|
|
63
|
+
this.onPacketReceive = this.readResponsePacket;
|
|
64
|
+
this.valueIdx = 0;
|
|
65
|
+
this.sendQueries();
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
sendQueries() {
|
|
69
|
+
let flushed = false;
|
|
70
|
+
while (!flushed && this.sending && this.valueIdx < this.values.length) {
|
|
71
|
+
this.valueRow = this.values[this.valueIdx++];
|
|
72
|
+
|
|
73
|
+
//********************************************
|
|
74
|
+
// send params
|
|
75
|
+
//********************************************
|
|
76
|
+
const len = this.parseResults.partList.length - 3;
|
|
77
|
+
for (let i = 0; i < len; i++) {
|
|
78
|
+
const value = this.valueRow[i];
|
|
79
|
+
flushed = this.packet.writeString(this.parseResults.partList[i + 1]) || flushed;
|
|
80
|
+
if (value === null) {
|
|
81
|
+
flushed = this.packet.writeStringAscii('NULL') || flushed;
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
if (
|
|
86
|
+
typeof value === 'object' &&
|
|
87
|
+
typeof value.pipe === 'function' &&
|
|
88
|
+
typeof value.read === 'function'
|
|
89
|
+
) {
|
|
90
|
+
//********************************************
|
|
91
|
+
// param is stream,
|
|
92
|
+
// now all params will be written by event
|
|
93
|
+
//********************************************
|
|
94
|
+
this.registerStreamSendEvent(this.packet, this.info);
|
|
95
|
+
this.currentParam = i;
|
|
96
|
+
this.packet.writeInt8(QUOTE); //'
|
|
97
|
+
|
|
98
|
+
value.on(
|
|
99
|
+
'data',
|
|
100
|
+
function (chunk) {
|
|
101
|
+
this.packet.writeBufferEscape(chunk);
|
|
102
|
+
}.bind(this)
|
|
103
|
+
);
|
|
104
|
+
|
|
105
|
+
value.on(
|
|
106
|
+
'end',
|
|
107
|
+
function () {
|
|
108
|
+
this.packet.writeInt8(QUOTE); //'
|
|
109
|
+
this.currentParam++;
|
|
110
|
+
this.paramWritten();
|
|
111
|
+
}.bind(this)
|
|
112
|
+
);
|
|
113
|
+
|
|
114
|
+
return;
|
|
115
|
+
} else {
|
|
116
|
+
//********************************************
|
|
117
|
+
// param isn't stream. directly write in buffer
|
|
118
|
+
//********************************************
|
|
119
|
+
flushed = this.writeParam(this.packet, value, this.opts, this.info) || flushed;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
this.packet.writeString(this.parseResults.partList[this.parseResults.partList.length - 2]);
|
|
123
|
+
this.packet.mark(!this.parseResults.reWritable || this.valueIdx === this.values.length);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
if (this.valueIdx < this.values.length && !this.packet.haveErrorResponse) {
|
|
127
|
+
//there is still data to send
|
|
128
|
+
setImmediate(this.sendQueries.bind(this));
|
|
129
|
+
} else {
|
|
130
|
+
if (this.sending && this.valueIdx === this.values.length) this.emit('send_end');
|
|
131
|
+
this.sending = false;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
displaySql() {
|
|
136
|
+
if (this.opts && this.initialValues) {
|
|
137
|
+
if (this.sql.length > this.opts.debugLen) {
|
|
138
|
+
return 'sql: ' + this.sql.substring(0, this.opts.debugLen) + '...';
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
let sqlMsg = 'sql: ' + this.sql + ' - parameters:';
|
|
142
|
+
sqlMsg += '[';
|
|
143
|
+
for (let i = 0; i < this.initialValues.length; i++) {
|
|
144
|
+
if (i !== 0) sqlMsg += ',';
|
|
145
|
+
let param = this.initialValues[i];
|
|
146
|
+
sqlMsg = this.logParameters(sqlMsg, param);
|
|
147
|
+
if (sqlMsg.length > this.opts.debugLen) {
|
|
148
|
+
sqlMsg = sqlMsg.substr(0, this.opts.debugLen) + '...';
|
|
149
|
+
break;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
sqlMsg += ']';
|
|
153
|
+
return sqlMsg;
|
|
154
|
+
}
|
|
155
|
+
return 'sql: ' + this.sql + ' - parameters:[]';
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
success(val) {
|
|
159
|
+
this.packet.waitingResponseNo--;
|
|
160
|
+
|
|
161
|
+
if (this.packet.haveErrorResponse) {
|
|
162
|
+
if (!this.sending && this.packet.waitingResponseNo === 0) {
|
|
163
|
+
this.packet = null;
|
|
164
|
+
this.onPacketReceive = null;
|
|
165
|
+
this.resolve = null;
|
|
166
|
+
this._columns = null;
|
|
167
|
+
this._rows = null;
|
|
168
|
+
process.nextTick(this.reject, this.firstError);
|
|
169
|
+
this.reject = null;
|
|
170
|
+
this.emit('end', this.firstError);
|
|
171
|
+
}
|
|
172
|
+
} else {
|
|
173
|
+
if (!this.sending && this.packet.waitingResponseNo === 0) {
|
|
174
|
+
if (this.parseResults.reWritable) {
|
|
175
|
+
this.packet = null;
|
|
176
|
+
let totalAffectedRows = 0;
|
|
177
|
+
this._rows.forEach((row) => {
|
|
178
|
+
totalAffectedRows += row.affectedRows;
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
const rs = {
|
|
182
|
+
affectedRows: totalAffectedRows,
|
|
183
|
+
insertId: this._rows[0].insertId,
|
|
184
|
+
warningStatus: this._rows[this._rows.length - 1].warningStatus
|
|
185
|
+
};
|
|
186
|
+
this.successEnd(rs);
|
|
187
|
+
return;
|
|
188
|
+
} else {
|
|
189
|
+
this.successEnd(this._rows);
|
|
190
|
+
}
|
|
191
|
+
this._columns = null;
|
|
192
|
+
this._rows = null;
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
this._responseIndex++;
|
|
196
|
+
this.onPacketReceive = this.readResponsePacket;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
throwError(err, info) {
|
|
201
|
+
this.packet.waitingResponseNo--;
|
|
202
|
+
this.sending = false;
|
|
203
|
+
|
|
204
|
+
if (this.packet && !this.packet.haveErrorResponse) {
|
|
205
|
+
if (err.fatal) {
|
|
206
|
+
this.packet.waitingResponseNo = 0;
|
|
207
|
+
}
|
|
208
|
+
if (this.stack) {
|
|
209
|
+
err = Errors.createError(
|
|
210
|
+
err.message,
|
|
211
|
+
this.sql,
|
|
212
|
+
err.fatal,
|
|
213
|
+
info,
|
|
214
|
+
err.sqlState,
|
|
215
|
+
err.errno,
|
|
216
|
+
this.stack,
|
|
217
|
+
false
|
|
218
|
+
);
|
|
219
|
+
}
|
|
220
|
+
this.firstError = err;
|
|
221
|
+
this.packet.endedWithError();
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
if (!this.sending && this.packet.waitingResponseNo === 0) {
|
|
225
|
+
this.packet = null;
|
|
226
|
+
this.onPacketReceive = null;
|
|
227
|
+
this.resolve = null;
|
|
228
|
+
process.nextTick(this.reject, this.firstError);
|
|
229
|
+
this.reject = null;
|
|
230
|
+
this.emit('end', this.firstError);
|
|
231
|
+
} else {
|
|
232
|
+
this._responseIndex++;
|
|
233
|
+
this.onPacketReceive = this.readResponsePacket;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* Validate that parameters exists and are defined.
|
|
239
|
+
*
|
|
240
|
+
* @param info connection info
|
|
241
|
+
* @returns {boolean} return false if any error occur.
|
|
242
|
+
*/
|
|
243
|
+
validateParameters(info) {
|
|
244
|
+
//validate parameter size.
|
|
245
|
+
for (let r = 0; r < this.values.length; r++) {
|
|
246
|
+
let val = this.values[r];
|
|
247
|
+
if (!Array.isArray(val)) {
|
|
248
|
+
val = [val];
|
|
249
|
+
this.values[r] = val;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
if (this.parseResults.partList.length - 3 > val.length) {
|
|
253
|
+
this.emit('send_end');
|
|
254
|
+
this.throwNewError(
|
|
255
|
+
'Parameter at position ' +
|
|
256
|
+
val.length +
|
|
257
|
+
' is not set for values ' +
|
|
258
|
+
r +
|
|
259
|
+
'\n' +
|
|
260
|
+
this.displaySql(),
|
|
261
|
+
false,
|
|
262
|
+
info,
|
|
263
|
+
'HY000',
|
|
264
|
+
Errors.ER_MISSING_PARAMETER
|
|
265
|
+
);
|
|
266
|
+
return false;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
//validate parameter is defined.
|
|
270
|
+
for (let i = 0; i < this.parseResults.partList.length - 3; i++) {
|
|
271
|
+
if (val[i] === undefined) {
|
|
272
|
+
this.emit('send_end');
|
|
273
|
+
this.throwNewError(
|
|
274
|
+
'Parameter at position ' +
|
|
275
|
+
(i + 1) +
|
|
276
|
+
' is undefined for values ' +
|
|
277
|
+
r +
|
|
278
|
+
'\n' +
|
|
279
|
+
this.displaySql(),
|
|
280
|
+
false,
|
|
281
|
+
info,
|
|
282
|
+
'HY000',
|
|
283
|
+
Errors.ER_PARAMETER_UNDEFINED
|
|
284
|
+
);
|
|
285
|
+
return false;
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
return true;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* Define params events.
|
|
295
|
+
* Each parameter indicate that he is written to socket,
|
|
296
|
+
* emitting event so next parameter can be written.
|
|
297
|
+
*/
|
|
298
|
+
registerStreamSendEvent(packet, info) {
|
|
299
|
+
this.paramWritten = function () {
|
|
300
|
+
let flushed = false;
|
|
301
|
+
while (!flushed) {
|
|
302
|
+
if (this.packet.haveErrorResponse) {
|
|
303
|
+
this.sending = false;
|
|
304
|
+
this.emit('send_end');
|
|
305
|
+
return;
|
|
306
|
+
}
|
|
307
|
+
if (this.currentParam === this.valueRow.length) {
|
|
308
|
+
// all parameters from row are written.
|
|
309
|
+
flushed =
|
|
310
|
+
packet.writeString(this.parseResults.partList[this.parseResults.partList.length - 2]) ||
|
|
311
|
+
flushed;
|
|
312
|
+
flushed =
|
|
313
|
+
packet.mark(!this.parseResults.reWritable || this.valueIdx === this.values.length) ||
|
|
314
|
+
flushed;
|
|
315
|
+
if (this.valueIdx < this.values.length) {
|
|
316
|
+
// still remaining rows
|
|
317
|
+
this.valueRow = this.values[this.valueIdx++];
|
|
318
|
+
this.currentParam = 0;
|
|
319
|
+
} else {
|
|
320
|
+
// all rows are written
|
|
321
|
+
this.sending = false;
|
|
322
|
+
this.emit('send_end');
|
|
323
|
+
return;
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
flushed = packet.writeString(this.parseResults.partList[this.currentParam + 1]) || flushed;
|
|
328
|
+
const value = this.valueRow[this.currentParam];
|
|
329
|
+
|
|
330
|
+
if (value === null) {
|
|
331
|
+
flushed = packet.writeStringAscii('NULL') || flushed;
|
|
332
|
+
this.currentParam++;
|
|
333
|
+
continue;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
if (
|
|
337
|
+
typeof value === 'object' &&
|
|
338
|
+
typeof value.pipe === 'function' &&
|
|
339
|
+
typeof value.read === 'function'
|
|
340
|
+
) {
|
|
341
|
+
//********************************************
|
|
342
|
+
// param is stream,
|
|
343
|
+
//********************************************
|
|
344
|
+
flushed = packet.writeInt8(QUOTE) || flushed;
|
|
345
|
+
value.once(
|
|
346
|
+
'end',
|
|
347
|
+
function () {
|
|
348
|
+
packet.writeInt8(QUOTE);
|
|
349
|
+
this.currentParam++;
|
|
350
|
+
this.paramWritten();
|
|
351
|
+
}.bind(this)
|
|
352
|
+
);
|
|
353
|
+
|
|
354
|
+
value.on('data', function (chunk) {
|
|
355
|
+
packet.writeBufferEscape(chunk);
|
|
356
|
+
});
|
|
357
|
+
return;
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
//********************************************
|
|
361
|
+
// param isn't stream. directly write in buffer
|
|
362
|
+
//********************************************
|
|
363
|
+
flushed = this.writeParam(packet, value, this.opts, info) || flushed;
|
|
364
|
+
this.currentParam++;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
if (this.sending) setImmediate(this.paramWritten.bind(this));
|
|
368
|
+
}.bind(this);
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
module.exports = BatchRewrite;
|