mysql2-core 0.2.4 → 0.3.1
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/lib/build.js +1 -1
- package/lib/index.js +656 -125
- package/package.json +1 -1
- package/src/build.ts +2 -2
- package/src/index.ts +490 -71
- package/src/metadata.ts +10 -46
package/src/index.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { Connection, createPool as createPool2, FieldPacket,
|
|
1
|
+
import { Connection, createPool as createPool2, FieldPacket, Pool, PoolConnection, ResultSetHeader } from "mysql2"
|
|
2
2
|
import { buildToSave, buildToSaveBatch } from "./build"
|
|
3
|
-
import { Attribute, Attributes,
|
|
3
|
+
import { Attribute, Attributes, DB, Statement, StringMap, Transaction } from "./metadata"
|
|
4
4
|
|
|
5
5
|
export * from "./build"
|
|
6
6
|
export * from "./metadata"
|
|
@@ -8,6 +8,7 @@ export * from "./metadata"
|
|
|
8
8
|
// tslint:disable-next-line:class-name
|
|
9
9
|
export class resource {
|
|
10
10
|
static string?: boolean
|
|
11
|
+
static multipleStatements?: boolean
|
|
11
12
|
}
|
|
12
13
|
export interface Config {
|
|
13
14
|
host?: string | undefined
|
|
@@ -29,29 +30,96 @@ export function createPool(conf: Config): Pool {
|
|
|
29
30
|
const pool = createPool2({ ...conf, rowsAsArray: true })
|
|
30
31
|
return pool
|
|
31
32
|
}
|
|
32
|
-
|
|
33
|
+
export function getConnection(pool: Pool): Promise<PoolConnection> {
|
|
34
|
+
return new Promise<PoolConnection>((resolve, reject) => {
|
|
35
|
+
pool.getConnection((err, connection) => {
|
|
36
|
+
if (err) {
|
|
37
|
+
return reject(err)
|
|
38
|
+
}
|
|
39
|
+
return resolve(connection)
|
|
40
|
+
})
|
|
41
|
+
})
|
|
42
|
+
}
|
|
43
|
+
export function beginTransaction(connection: PoolConnection, rollbackIfError?: boolean): Promise<void> {
|
|
44
|
+
return new Promise<void>((resolve, reject) => {
|
|
45
|
+
connection.beginTransaction((err) => {
|
|
46
|
+
if (err) {
|
|
47
|
+
if (!rollbackIfError) {
|
|
48
|
+
return reject(err)
|
|
49
|
+
}
|
|
50
|
+
connection.rollback(() => {
|
|
51
|
+
return reject(err)
|
|
52
|
+
})
|
|
53
|
+
}
|
|
54
|
+
return resolve()
|
|
55
|
+
})
|
|
56
|
+
})
|
|
57
|
+
}
|
|
58
|
+
export function commit(connection: PoolConnection, rollbackIfError?: boolean): Promise<void> {
|
|
59
|
+
return new Promise<void>((resolve, reject) => {
|
|
60
|
+
connection.commit((err) => {
|
|
61
|
+
if (err) {
|
|
62
|
+
if (!rollbackIfError) {
|
|
63
|
+
return reject(err)
|
|
64
|
+
}
|
|
65
|
+
connection.rollback(() => {
|
|
66
|
+
return reject(err)
|
|
67
|
+
})
|
|
68
|
+
}
|
|
69
|
+
resolve()
|
|
70
|
+
})
|
|
71
|
+
})
|
|
72
|
+
}
|
|
73
|
+
export function rollback(connection: PoolConnection): Promise<void> {
|
|
74
|
+
return new Promise<void>((resolve, reject) => {
|
|
75
|
+
connection.rollback((err) => {
|
|
76
|
+
if (err) {
|
|
77
|
+
return reject(err)
|
|
78
|
+
}
|
|
79
|
+
resolve()
|
|
80
|
+
})
|
|
81
|
+
})
|
|
82
|
+
}
|
|
33
83
|
// tslint:disable-next-line:max-classes-per-file
|
|
34
|
-
export class PoolManager implements
|
|
35
|
-
constructor(
|
|
84
|
+
export class PoolManager implements DB {
|
|
85
|
+
constructor(protected pool: Pool) {
|
|
36
86
|
this.param = this.param.bind(this)
|
|
37
|
-
this.
|
|
38
|
-
this.
|
|
87
|
+
this.execute = this.execute.bind(this)
|
|
88
|
+
this.executeBatch = this.executeBatch.bind(this)
|
|
39
89
|
this.query = this.query.bind(this)
|
|
40
90
|
this.queryOne = this.queryOne.bind(this)
|
|
41
|
-
this.
|
|
91
|
+
this.executeScalar = this.executeScalar.bind(this)
|
|
42
92
|
this.count = this.count.bind(this)
|
|
43
93
|
}
|
|
44
94
|
driver = "mysql"
|
|
45
95
|
param(i: number): string {
|
|
46
96
|
return "?"
|
|
47
97
|
}
|
|
48
|
-
|
|
98
|
+
beginTransaction(): Promise<Transaction> {
|
|
99
|
+
return new Promise<Transaction>((resolve, reject) => {
|
|
100
|
+
this.pool.getConnection((er0, connection) => {
|
|
101
|
+
if (er0) {
|
|
102
|
+
return reject(er0)
|
|
103
|
+
}
|
|
104
|
+
connection.beginTransaction((er1) => {
|
|
105
|
+
if (er1) {
|
|
106
|
+
connection.rollback(() => {
|
|
107
|
+
return reject(er1)
|
|
108
|
+
})
|
|
109
|
+
}
|
|
110
|
+
const tx = new PoolConnectionManager(connection)
|
|
111
|
+
return resolve(tx)
|
|
112
|
+
})
|
|
113
|
+
})
|
|
114
|
+
})
|
|
115
|
+
}
|
|
116
|
+
execute(sql: string, args?: any[], ctx?: any): Promise<number> {
|
|
49
117
|
const p = ctx ? ctx : this.pool
|
|
50
|
-
return
|
|
118
|
+
return execute(p, sql, args)
|
|
51
119
|
}
|
|
52
|
-
|
|
120
|
+
executeBatch(statements: Statement[], firstSuccess?: boolean, ctx?: any): Promise<number> {
|
|
53
121
|
const p = ctx ? ctx : this.pool
|
|
54
|
-
return
|
|
122
|
+
return executeBatch(p, statements, firstSuccess)
|
|
55
123
|
}
|
|
56
124
|
query<T>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any): Promise<T[]> {
|
|
57
125
|
const p = ctx ? ctx : this.pool
|
|
@@ -61,58 +129,252 @@ export class PoolManager implements Manager {
|
|
|
61
129
|
const p = ctx ? ctx : this.pool
|
|
62
130
|
return queryOne(p, sql, args, m, bools)
|
|
63
131
|
}
|
|
64
|
-
|
|
132
|
+
executeScalar<T>(sql: string, args?: any[], ctx?: any): Promise<T> {
|
|
65
133
|
const p = ctx ? ctx : this.pool
|
|
66
|
-
return
|
|
134
|
+
return executeScalar<T>(p, sql, args)
|
|
67
135
|
}
|
|
68
136
|
count(sql: string, args?: any[], ctx?: any): Promise<number> {
|
|
69
137
|
const p = ctx ? ctx : this.pool
|
|
70
138
|
return count(p, sql, args)
|
|
71
139
|
}
|
|
72
140
|
}
|
|
73
|
-
|
|
141
|
+
// tslint:disable-next-line:max-classes-per-file
|
|
142
|
+
export class PoolConnectionManager implements Transaction {
|
|
143
|
+
constructor(protected connection: PoolConnection) {
|
|
144
|
+
this.param = this.param.bind(this)
|
|
145
|
+
this.execute = this.execute.bind(this)
|
|
146
|
+
this.executeBatch = this.executeBatch.bind(this)
|
|
147
|
+
this.query = this.query.bind(this)
|
|
148
|
+
this.queryOne = this.queryOne.bind(this)
|
|
149
|
+
this.executeScalar = this.executeScalar.bind(this)
|
|
150
|
+
this.count = this.count.bind(this)
|
|
151
|
+
}
|
|
152
|
+
driver = "mysql"
|
|
153
|
+
param(i: number): string {
|
|
154
|
+
return "?"
|
|
155
|
+
}
|
|
156
|
+
commit(): Promise<void> {
|
|
157
|
+
return new Promise<void>((resolve, reject) => {
|
|
158
|
+
this.connection.commit((er3) => {
|
|
159
|
+
if (er3) {
|
|
160
|
+
return reject(er3)
|
|
161
|
+
}
|
|
162
|
+
return resolve()
|
|
163
|
+
})
|
|
164
|
+
})
|
|
165
|
+
}
|
|
166
|
+
rollback(): Promise<void> {
|
|
167
|
+
return new Promise<void>((resolve, reject) => {
|
|
168
|
+
this.connection.rollback((er3) => {
|
|
169
|
+
if (er3) {
|
|
170
|
+
return reject(er3)
|
|
171
|
+
}
|
|
172
|
+
return resolve()
|
|
173
|
+
})
|
|
174
|
+
})
|
|
175
|
+
}
|
|
176
|
+
execute(sql: string, args?: any[], ctx?: any): Promise<number> {
|
|
177
|
+
const p = ctx ? ctx : this.connection
|
|
178
|
+
return execute(p, sql, args)
|
|
179
|
+
}
|
|
180
|
+
executeBatch(statements: Statement[], firstSuccess?: boolean, ctx?: any): Promise<number> {
|
|
181
|
+
const p = ctx ? ctx : this.connection
|
|
182
|
+
return executeBatchConnection(p, statements, firstSuccess)
|
|
183
|
+
}
|
|
184
|
+
query<T>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any): Promise<T[]> {
|
|
185
|
+
const p = ctx ? ctx : this.connection
|
|
186
|
+
return query(p, sql, args, m, bools)
|
|
187
|
+
}
|
|
188
|
+
queryOne<T>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any): Promise<T | null> {
|
|
189
|
+
const p = ctx ? ctx : this.connection
|
|
190
|
+
return queryOne(p, sql, args, m, bools)
|
|
191
|
+
}
|
|
192
|
+
executeScalar<T>(sql: string, args?: any[], ctx?: any): Promise<T> {
|
|
193
|
+
const p = ctx ? ctx : this.connection
|
|
194
|
+
return executeScalar<T>(p, sql, args)
|
|
195
|
+
}
|
|
196
|
+
count(sql: string, args?: any[], ctx?: any): Promise<number> {
|
|
197
|
+
const p = ctx ? ctx : this.connection
|
|
198
|
+
return count(p, sql, args)
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
export async function executeBatch(pool: Pool, statements: Statement[], firstSuccess?: boolean): Promise<number> {
|
|
74
202
|
if (!statements || statements.length === 0) {
|
|
75
203
|
return Promise.resolve(0)
|
|
76
204
|
} else if (statements.length === 1) {
|
|
77
|
-
return
|
|
205
|
+
return execute(pool, statements[0].query, statements[0].params)
|
|
78
206
|
}
|
|
79
|
-
if (
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
207
|
+
if (resource.multipleStatements) {
|
|
208
|
+
if (firstSuccess) {
|
|
209
|
+
return new Promise<number>((resolve, reject) => {
|
|
210
|
+
pool.getConnection((er0, connection) => {
|
|
211
|
+
if (er0) {
|
|
212
|
+
return reject(er0)
|
|
213
|
+
}
|
|
214
|
+
connection.beginTransaction((er1) => {
|
|
215
|
+
if (er1) {
|
|
216
|
+
connection.rollback(() => {
|
|
217
|
+
return reject(er1)
|
|
218
|
+
})
|
|
219
|
+
} else {
|
|
220
|
+
connection.execute<ResultSetHeader>(statements[0].query, toArray(statements[0].params), (er2a, results0) => {
|
|
221
|
+
if (er2a) {
|
|
222
|
+
connection.rollback(() => {
|
|
223
|
+
return reject(er2a)
|
|
224
|
+
})
|
|
225
|
+
} else {
|
|
226
|
+
if (results0 && results0.affectedRows === 0) {
|
|
227
|
+
connection.commit((er3) => {
|
|
228
|
+
if (er3) {
|
|
229
|
+
connection.rollback(() => {
|
|
230
|
+
return reject(er3)
|
|
231
|
+
})
|
|
232
|
+
}
|
|
233
|
+
})
|
|
234
|
+
return 0
|
|
235
|
+
} else {
|
|
236
|
+
const queries: string[] = []
|
|
237
|
+
const params: any[] = []
|
|
238
|
+
const l = statements.length
|
|
239
|
+
for (let j = 1; j < l; j++) {
|
|
240
|
+
const item = statements[j]
|
|
241
|
+
if (item.query.endsWith(";")) {
|
|
242
|
+
queries.push(item.query)
|
|
243
|
+
} else {
|
|
244
|
+
queries.push(item.query + ";")
|
|
245
|
+
}
|
|
246
|
+
if (item.params && item.params.length > 0) {
|
|
247
|
+
for (const p of item.params) {
|
|
248
|
+
params.push(p)
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
connection.query<ResultSetHeader>(queries.join(""), toArray(params), (er2, results) => {
|
|
253
|
+
if (er2) {
|
|
254
|
+
connection.rollback(() => {
|
|
255
|
+
return reject(er2)
|
|
256
|
+
})
|
|
257
|
+
} else {
|
|
258
|
+
connection.commit((er3) => {
|
|
259
|
+
if (er3) {
|
|
260
|
+
connection.rollback(() => {
|
|
261
|
+
return reject(er3)
|
|
262
|
+
})
|
|
263
|
+
}
|
|
264
|
+
})
|
|
265
|
+
let c = 0
|
|
266
|
+
c += results0.affectedRows + results.affectedRows
|
|
267
|
+
return resolve(c)
|
|
268
|
+
}
|
|
269
|
+
})
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
})
|
|
273
|
+
}
|
|
274
|
+
})
|
|
275
|
+
})
|
|
276
|
+
})
|
|
277
|
+
} else {
|
|
278
|
+
return new Promise<number>((resolve, reject) => {
|
|
279
|
+
pool.getConnection((er0, connection) => {
|
|
280
|
+
if (er0) {
|
|
281
|
+
return reject(er0)
|
|
282
|
+
}
|
|
283
|
+
connection.beginTransaction((er1) => {
|
|
284
|
+
if (er1) {
|
|
285
|
+
connection.rollback(() => {
|
|
286
|
+
return reject(er1)
|
|
287
|
+
})
|
|
288
|
+
} else {
|
|
289
|
+
const queries: string[] = []
|
|
290
|
+
const params: any[] = []
|
|
291
|
+
statements.forEach((item) => {
|
|
292
|
+
if (item.query.endsWith(";")) {
|
|
293
|
+
queries.push(item.query)
|
|
294
|
+
} else {
|
|
295
|
+
queries.push(item.query + ";")
|
|
296
|
+
}
|
|
297
|
+
if (item.params && item.params.length > 0) {
|
|
298
|
+
for (const p of item.params) {
|
|
299
|
+
params.push(p)
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
})
|
|
303
|
+
connection.query<ResultSetHeader>(queries.join(""), toArray(params), (er2, results) => {
|
|
304
|
+
if (er2) {
|
|
305
|
+
connection.rollback(() => {
|
|
306
|
+
buildError(er2)
|
|
307
|
+
return reject(er2)
|
|
308
|
+
})
|
|
309
|
+
} else {
|
|
310
|
+
connection.commit((er3) => {
|
|
311
|
+
if (er3) {
|
|
312
|
+
connection.rollback(() => {
|
|
313
|
+
return reject(er3)
|
|
314
|
+
})
|
|
315
|
+
}
|
|
316
|
+
})
|
|
317
|
+
return resolve(results.affectedRows)
|
|
318
|
+
}
|
|
319
|
+
})
|
|
320
|
+
}
|
|
321
|
+
})
|
|
322
|
+
})
|
|
323
|
+
})
|
|
324
|
+
}
|
|
325
|
+
} else {
|
|
326
|
+
const connection = await getConnection(pool)
|
|
327
|
+
return executeBatchConnectionTx(connection, statements, firstSuccess)
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
export async function executeBatchConnectionTx(connection: PoolConnection, statements: Statement[], firstSuccess?: boolean): Promise<number> {
|
|
331
|
+
if (!statements || statements.length === 0) {
|
|
332
|
+
return Promise.resolve(0)
|
|
333
|
+
} else if (statements.length === 1) {
|
|
334
|
+
return execute(connection, statements[0].query, statements[0].params)
|
|
335
|
+
}
|
|
336
|
+
if (resource.multipleStatements) {
|
|
337
|
+
if (firstSuccess) {
|
|
338
|
+
return new Promise<number>((resolve, reject) => {
|
|
85
339
|
connection.beginTransaction((er1) => {
|
|
86
340
|
if (er1) {
|
|
87
341
|
connection.rollback(() => {
|
|
88
342
|
return reject(er1)
|
|
89
343
|
})
|
|
90
344
|
} else {
|
|
91
|
-
|
|
92
|
-
const queries: string[] = []
|
|
93
|
-
const l = statements.length
|
|
94
|
-
for (let j = 0; j < l; j++) {
|
|
95
|
-
const item = statements[j]
|
|
96
|
-
if (j === 0) {
|
|
97
|
-
query0 = format(item.query, toArray(item.params))
|
|
98
|
-
} else {
|
|
99
|
-
if (item.query.endsWith(";")) {
|
|
100
|
-
queries.push(format(item.query, toArray(item.params)))
|
|
101
|
-
} else {
|
|
102
|
-
queries.push(format(item.query + ";", toArray(item.params)))
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
connection.execute<ResultSetHeader>(query0, (er2a, results0) => {
|
|
345
|
+
connection.execute<ResultSetHeader>(statements[0].query, toArray(statements[0].params), (er2a, results0) => {
|
|
107
346
|
if (er2a) {
|
|
108
347
|
connection.rollback(() => {
|
|
109
348
|
return reject(er2a)
|
|
110
349
|
})
|
|
111
350
|
} else {
|
|
112
351
|
if (results0 && results0.affectedRows === 0) {
|
|
352
|
+
connection.commit((er3) => {
|
|
353
|
+
if (er3) {
|
|
354
|
+
connection.rollback(() => {
|
|
355
|
+
return reject(er3)
|
|
356
|
+
})
|
|
357
|
+
}
|
|
358
|
+
})
|
|
113
359
|
return 0
|
|
114
360
|
} else {
|
|
115
|
-
|
|
361
|
+
const queries: string[] = []
|
|
362
|
+
const params: any[] = []
|
|
363
|
+
const l = statements.length
|
|
364
|
+
for (let j = 1; j < l; j++) {
|
|
365
|
+
const item = statements[j]
|
|
366
|
+
if (item.query.endsWith(";")) {
|
|
367
|
+
queries.push(item.query)
|
|
368
|
+
} else {
|
|
369
|
+
queries.push(item.query + ";")
|
|
370
|
+
}
|
|
371
|
+
if (item.params && item.params.length > 0) {
|
|
372
|
+
for (const p of item.params) {
|
|
373
|
+
params.push(p)
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
connection.query<ResultSetHeader>(queries.join(""), toArray(params), (er2, results) => {
|
|
116
378
|
if (er2) {
|
|
117
379
|
connection.rollback(() => {
|
|
118
380
|
return reject(er2)
|
|
@@ -136,13 +398,8 @@ export function execBatch(pool: Pool, statements: Statement[], firstSuccess?: bo
|
|
|
136
398
|
}
|
|
137
399
|
})
|
|
138
400
|
})
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
return new Promise<number>((resolve, reject) => {
|
|
142
|
-
pool.getConnection((er0, connection) => {
|
|
143
|
-
if (er0) {
|
|
144
|
-
return reject(er0)
|
|
145
|
-
}
|
|
401
|
+
} else {
|
|
402
|
+
return new Promise<number>((resolve, reject) => {
|
|
146
403
|
connection.beginTransaction((er1) => {
|
|
147
404
|
if (er1) {
|
|
148
405
|
connection.rollback(() => {
|
|
@@ -150,14 +407,20 @@ export function execBatch(pool: Pool, statements: Statement[], firstSuccess?: bo
|
|
|
150
407
|
})
|
|
151
408
|
} else {
|
|
152
409
|
const queries: string[] = []
|
|
410
|
+
const params: any[] = []
|
|
153
411
|
statements.forEach((item) => {
|
|
154
412
|
if (item.query.endsWith(";")) {
|
|
155
|
-
queries.push(
|
|
413
|
+
queries.push(item.query)
|
|
156
414
|
} else {
|
|
157
|
-
queries.push(
|
|
415
|
+
queries.push(item.query + ";")
|
|
416
|
+
}
|
|
417
|
+
if (item.params && item.params.length > 0) {
|
|
418
|
+
for (const p of item.params) {
|
|
419
|
+
params.push(p)
|
|
420
|
+
}
|
|
158
421
|
}
|
|
159
422
|
})
|
|
160
|
-
connection.
|
|
423
|
+
connection.query<ResultSetHeader>(queries.join(""), toArray(params), (er2, results) => {
|
|
161
424
|
if (er2) {
|
|
162
425
|
connection.rollback(() => {
|
|
163
426
|
buildError(er2)
|
|
@@ -177,7 +440,163 @@ export function execBatch(pool: Pool, statements: Statement[], firstSuccess?: bo
|
|
|
177
440
|
}
|
|
178
441
|
})
|
|
179
442
|
})
|
|
180
|
-
}
|
|
443
|
+
}
|
|
444
|
+
} else {
|
|
445
|
+
if (firstSuccess) {
|
|
446
|
+
try {
|
|
447
|
+
await beginTransaction(connection, true)
|
|
448
|
+
let count = await execute(connection, statements[0].query, toArray(statements[0].params))
|
|
449
|
+
if (count === 0) {
|
|
450
|
+
commit(connection, true)
|
|
451
|
+
return 0
|
|
452
|
+
}
|
|
453
|
+
const l = statements.length
|
|
454
|
+
|
|
455
|
+
for (let j = 1; j < l; j++) {
|
|
456
|
+
const item = statements[j]
|
|
457
|
+
const c = await execute(connection, item.query, toArray(item.params))
|
|
458
|
+
count = count + c
|
|
459
|
+
}
|
|
460
|
+
commit(connection, true)
|
|
461
|
+
return count
|
|
462
|
+
} catch (err) {
|
|
463
|
+
rollback(connection)
|
|
464
|
+
throw err
|
|
465
|
+
}
|
|
466
|
+
} else {
|
|
467
|
+
try {
|
|
468
|
+
await beginTransaction(connection, true)
|
|
469
|
+
let count = 0
|
|
470
|
+
const l = statements.length
|
|
471
|
+
for (let j = 0; j < l; j++) {
|
|
472
|
+
const item = statements[j]
|
|
473
|
+
const c = await execute(connection, item.query, toArray(item.params))
|
|
474
|
+
count = count + c
|
|
475
|
+
}
|
|
476
|
+
commit(connection, true)
|
|
477
|
+
return count
|
|
478
|
+
} catch (err) {
|
|
479
|
+
rollback(connection)
|
|
480
|
+
throw err
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
export async function executeBatchConnection(connection: PoolConnection, statements: Statement[], firstSuccess?: boolean): Promise<number> {
|
|
486
|
+
if (!statements || statements.length === 0) {
|
|
487
|
+
return Promise.resolve(0)
|
|
488
|
+
} else if (statements.length === 1) {
|
|
489
|
+
return execute(connection, statements[0].query, statements[0].params)
|
|
490
|
+
}
|
|
491
|
+
if (resource.multipleStatements) {
|
|
492
|
+
if (firstSuccess) {
|
|
493
|
+
return new Promise<number>((resolve, reject) => {
|
|
494
|
+
connection.execute<ResultSetHeader>(statements[0].query, toArray(statements[0].params), (er2a, results0) => {
|
|
495
|
+
if (er2a) {
|
|
496
|
+
return reject(er2a)
|
|
497
|
+
} else {
|
|
498
|
+
if (results0 && results0.affectedRows === 0) {
|
|
499
|
+
return 0
|
|
500
|
+
} else {
|
|
501
|
+
const queries: string[] = []
|
|
502
|
+
const params: any[] = []
|
|
503
|
+
const l = statements.length
|
|
504
|
+
for (let j = 1; j < l; j++) {
|
|
505
|
+
const item = statements[j]
|
|
506
|
+
if (item.query.endsWith(";")) {
|
|
507
|
+
queries.push(item.query)
|
|
508
|
+
} else {
|
|
509
|
+
queries.push(item.query + ";")
|
|
510
|
+
}
|
|
511
|
+
if (item.params && item.params.length > 0) {
|
|
512
|
+
for (const p of item.params) {
|
|
513
|
+
params.push(p)
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
connection.query<ResultSetHeader>(queries.join(""), toArray(params), (er2, results) => {
|
|
518
|
+
if (er2) {
|
|
519
|
+
return reject(er2)
|
|
520
|
+
} else {
|
|
521
|
+
let c = 0
|
|
522
|
+
c += results0.affectedRows + results.affectedRows
|
|
523
|
+
return resolve(c)
|
|
524
|
+
}
|
|
525
|
+
})
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
})
|
|
529
|
+
})
|
|
530
|
+
} else {
|
|
531
|
+
return new Promise<number>((resolve, reject) => {
|
|
532
|
+
const queries: string[] = []
|
|
533
|
+
const params: any[] = []
|
|
534
|
+
statements.forEach((item) => {
|
|
535
|
+
if (item.query.endsWith(";")) {
|
|
536
|
+
queries.push(item.query)
|
|
537
|
+
} else {
|
|
538
|
+
queries.push(item.query + ";")
|
|
539
|
+
}
|
|
540
|
+
if (item.params && item.params.length > 0) {
|
|
541
|
+
for (const p of item.params) {
|
|
542
|
+
params.push(p)
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
})
|
|
546
|
+
connection.query<ResultSetHeader>(queries.join(""), toArray(params), (er2, results) => {
|
|
547
|
+
if (er2) {
|
|
548
|
+
connection.rollback(() => {
|
|
549
|
+
buildError(er2)
|
|
550
|
+
return reject(er2)
|
|
551
|
+
})
|
|
552
|
+
} else {
|
|
553
|
+
connection.commit((er3) => {
|
|
554
|
+
if (er3) {
|
|
555
|
+
connection.rollback(() => {
|
|
556
|
+
return reject(er3)
|
|
557
|
+
})
|
|
558
|
+
}
|
|
559
|
+
})
|
|
560
|
+
return resolve(results.affectedRows)
|
|
561
|
+
}
|
|
562
|
+
})
|
|
563
|
+
})
|
|
564
|
+
}
|
|
565
|
+
} else {
|
|
566
|
+
if (firstSuccess) {
|
|
567
|
+
try {
|
|
568
|
+
let count = await execute(connection, statements[0].query, toArray(statements[0].params))
|
|
569
|
+
if (count === 0) {
|
|
570
|
+
return 0
|
|
571
|
+
}
|
|
572
|
+
const l = statements.length
|
|
573
|
+
try {
|
|
574
|
+
for (let j = 1; j < l; j++) {
|
|
575
|
+
const item = statements[j]
|
|
576
|
+
const c = await execute(connection, item.query, toArray(item.params))
|
|
577
|
+
count = count + c
|
|
578
|
+
}
|
|
579
|
+
return count
|
|
580
|
+
} catch (er1) {
|
|
581
|
+
throw er1
|
|
582
|
+
}
|
|
583
|
+
} catch (er0) {
|
|
584
|
+
throw er0
|
|
585
|
+
}
|
|
586
|
+
} else {
|
|
587
|
+
let count = 0
|
|
588
|
+
const l = statements.length
|
|
589
|
+
try {
|
|
590
|
+
for (let j = 0; j < l; j++) {
|
|
591
|
+
const item = statements[j]
|
|
592
|
+
const c = await execute(connection, item.query, toArray(item.params))
|
|
593
|
+
count = count + c
|
|
594
|
+
}
|
|
595
|
+
return count
|
|
596
|
+
} catch (err) {
|
|
597
|
+
throw err
|
|
598
|
+
}
|
|
599
|
+
}
|
|
181
600
|
}
|
|
182
601
|
}
|
|
183
602
|
function buildError(err: any): any {
|
|
@@ -187,7 +606,7 @@ function buildError(err: any): any {
|
|
|
187
606
|
return err
|
|
188
607
|
}
|
|
189
608
|
|
|
190
|
-
export async function
|
|
609
|
+
export async function execute(pool: Pool | PoolConnection, sql: string, args?: any[]): Promise<number> {
|
|
191
610
|
const p = toArray(args)
|
|
192
611
|
return new Promise<number>((resolve, reject) => {
|
|
193
612
|
return pool.execute<ResultSetHeader>(sql, p, (err, res) => {
|
|
@@ -229,7 +648,7 @@ export function queryOne<T>(pool: Pool | PoolConnection, sql: string, args?: any
|
|
|
229
648
|
return r && r.length > 0 ? r[0] : null
|
|
230
649
|
})
|
|
231
650
|
}
|
|
232
|
-
export function
|
|
651
|
+
export function executeScalar<T>(pool: Pool | PoolConnection, sql: string, args?: any[]): Promise<T> {
|
|
233
652
|
return queryOne<T>(pool, sql, args).then((r) => {
|
|
234
653
|
if (!r) {
|
|
235
654
|
return null
|
|
@@ -241,7 +660,7 @@ export function execScalar<T>(pool: Pool | PoolConnection, sql: string, args?: a
|
|
|
241
660
|
}
|
|
242
661
|
|
|
243
662
|
export function count(pool: Pool | PoolConnection, sql: string, args?: any[]): Promise<number> {
|
|
244
|
-
return
|
|
663
|
+
return executeScalar<number>(pool, sql, args)
|
|
245
664
|
}
|
|
246
665
|
|
|
247
666
|
export function save<T>(
|
|
@@ -254,13 +673,13 @@ export function save<T>(
|
|
|
254
673
|
i?: number,
|
|
255
674
|
): Promise<number> {
|
|
256
675
|
const s = buildToSave(obj, table, attrs, ver, buildParam)
|
|
257
|
-
if (!s) {
|
|
676
|
+
if (!s.query) {
|
|
258
677
|
return Promise.resolve(-1)
|
|
259
678
|
}
|
|
260
679
|
if (typeof pool === "function") {
|
|
261
680
|
return pool(s.query, s.params)
|
|
262
681
|
} else {
|
|
263
|
-
return
|
|
682
|
+
return execute(pool, s.query, s.params)
|
|
264
683
|
}
|
|
265
684
|
}
|
|
266
685
|
export function saveBatch<T>(
|
|
@@ -275,7 +694,7 @@ export function saveBatch<T>(
|
|
|
275
694
|
if (typeof pool === "function") {
|
|
276
695
|
return pool(s)
|
|
277
696
|
} else {
|
|
278
|
-
return
|
|
697
|
+
return executeBatch(pool, s)
|
|
279
698
|
}
|
|
280
699
|
}
|
|
281
700
|
export function toArray(arr?: any[]): any[] {
|
|
@@ -470,7 +889,7 @@ export class StringService {
|
|
|
470
889
|
arr.push("(?)")
|
|
471
890
|
}
|
|
472
891
|
const s = `insert ignore into ${this.table}(${this.column})values${arr.join(",")}`
|
|
473
|
-
return
|
|
892
|
+
return execute(this.pool, s, values)
|
|
474
893
|
}
|
|
475
894
|
}
|
|
476
895
|
|
|
@@ -489,7 +908,7 @@ export function version(attrs: Attributes): Attribute | undefined {
|
|
|
489
908
|
export class MySQLWriter<T> {
|
|
490
909
|
pool?: Pool | PoolConnection
|
|
491
910
|
version?: string
|
|
492
|
-
|
|
911
|
+
execute?: (sql: string, args?: any[]) => Promise<number>
|
|
493
912
|
map?: (v: T) => T
|
|
494
913
|
param?: (i: number) => string
|
|
495
914
|
constructor(
|
|
@@ -502,7 +921,7 @@ export class MySQLWriter<T> {
|
|
|
502
921
|
) {
|
|
503
922
|
this.write = this.write.bind(this)
|
|
504
923
|
if (typeof pool === "function") {
|
|
505
|
-
this.
|
|
924
|
+
this.execute = pool
|
|
506
925
|
} else {
|
|
507
926
|
this.pool = pool
|
|
508
927
|
}
|
|
@@ -523,17 +942,17 @@ export class MySQLWriter<T> {
|
|
|
523
942
|
}
|
|
524
943
|
const stmt = buildToSave(obj2, this.table, this.attributes, this.version, this.param)
|
|
525
944
|
if (stmt) {
|
|
526
|
-
if (this.
|
|
945
|
+
if (this.execute) {
|
|
527
946
|
if (this.oneIfSuccess) {
|
|
528
|
-
return this.
|
|
947
|
+
return this.execute(stmt.query, stmt.params).then((ct) => (ct > 0 ? 1 : 0))
|
|
529
948
|
} else {
|
|
530
|
-
return this.
|
|
949
|
+
return this.execute(stmt.query, stmt.params)
|
|
531
950
|
}
|
|
532
951
|
} else {
|
|
533
952
|
if (this.oneIfSuccess) {
|
|
534
|
-
return
|
|
953
|
+
return execute(this.pool as any, stmt.query, stmt.params).then((ct) => (ct > 0 ? 1 : 0))
|
|
535
954
|
} else {
|
|
536
|
-
return
|
|
955
|
+
return execute(this.pool as any, stmt.query, stmt.params)
|
|
537
956
|
}
|
|
538
957
|
}
|
|
539
958
|
} else {
|
|
@@ -547,7 +966,7 @@ export class MySQLStreamWriter<T> {
|
|
|
547
966
|
size = 0
|
|
548
967
|
pool?: Pool
|
|
549
968
|
version?: string
|
|
550
|
-
|
|
969
|
+
executeBatch?: (statements: Statement[]) => Promise<number>
|
|
551
970
|
map?: (v: T) => T
|
|
552
971
|
param?: (i: number) => string
|
|
553
972
|
constructor(
|
|
@@ -561,7 +980,7 @@ export class MySQLStreamWriter<T> {
|
|
|
561
980
|
this.write = this.write.bind(this)
|
|
562
981
|
this.flush = this.flush.bind(this)
|
|
563
982
|
if (typeof pool === "function") {
|
|
564
|
-
this.
|
|
983
|
+
this.executeBatch = pool
|
|
565
984
|
} else {
|
|
566
985
|
this.pool = pool
|
|
567
986
|
}
|
|
@@ -599,13 +1018,13 @@ export class MySQLStreamWriter<T> {
|
|
|
599
1018
|
const total = this.list.length
|
|
600
1019
|
const stmt = buildToSaveBatch(this.list, this.table, this.attributes, this.version, this.param)
|
|
601
1020
|
if (stmt) {
|
|
602
|
-
if (this.
|
|
603
|
-
return this.
|
|
1021
|
+
if (this.executeBatch) {
|
|
1022
|
+
return this.executeBatch(stmt).then((r) => {
|
|
604
1023
|
this.list = []
|
|
605
1024
|
return total
|
|
606
1025
|
})
|
|
607
1026
|
} else {
|
|
608
|
-
return
|
|
1027
|
+
return executeBatch(this.pool as any, stmt).then((r) => {
|
|
609
1028
|
this.list = []
|
|
610
1029
|
return total
|
|
611
1030
|
})
|
|
@@ -666,9 +1085,9 @@ export class MySQLBatchWriter<T> {
|
|
|
666
1085
|
}
|
|
667
1086
|
} else {
|
|
668
1087
|
if (this.oneIfSuccess) {
|
|
669
|
-
return
|
|
1088
|
+
return executeBatch(this.pool as any, stmts).then((ct) => stmts.length)
|
|
670
1089
|
} else {
|
|
671
|
-
return
|
|
1090
|
+
return executeBatch(this.pool as any, stmts)
|
|
672
1091
|
}
|
|
673
1092
|
}
|
|
674
1093
|
} else {
|