mysql2-core 0.3.10 → 0.5.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/README.md +666 -78
- package/lib/index.js +183 -200
- package/package.json +1 -1
- package/src/build.ts +132 -107
- package/src/index.ts +62 -81
- package/src/metadata.ts +1 -1
package/src/build.ts
CHANGED
|
@@ -59,35 +59,55 @@ export function metadata(attrs: Attributes): Metadata {
|
|
|
59
59
|
}
|
|
60
60
|
return m
|
|
61
61
|
}
|
|
62
|
-
export function buildToSave<T>(obj: T, table: string, attrs: Attributes, buildParam?: (i: number) => string
|
|
63
|
-
if (!i) {
|
|
64
|
-
i = 1
|
|
65
|
-
}
|
|
62
|
+
export function buildToSave<T>(obj: T, table: string, attrs: Attributes, pks?: Attribute[], ver?: string, buildParam?: (i: number) => string): Statement {
|
|
66
63
|
if (!buildParam) {
|
|
67
64
|
buildParam = param
|
|
68
65
|
}
|
|
69
66
|
const ks = Object.keys(attrs)
|
|
70
|
-
|
|
67
|
+
if (!pks) {
|
|
68
|
+
pks = []
|
|
69
|
+
for (const k of ks) {
|
|
70
|
+
const attr = attrs[k]
|
|
71
|
+
attr.name = k
|
|
72
|
+
if (attr.key) {
|
|
73
|
+
pks.push(attr)
|
|
74
|
+
}
|
|
75
|
+
if (attr.version) {
|
|
76
|
+
ver = k
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
71
81
|
const cols: string[] = []
|
|
72
82
|
const values: string[] = []
|
|
73
83
|
const args: any[] = []
|
|
74
|
-
let isVersion = false
|
|
75
84
|
const o: any = obj
|
|
85
|
+
let isUpdate = true
|
|
86
|
+
let isVersion = false
|
|
87
|
+
for (const k of pks) {
|
|
88
|
+
if (k.name) {
|
|
89
|
+
let v = o[k.name]
|
|
90
|
+
if (v == null) {
|
|
91
|
+
isUpdate = false
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
let i = 1
|
|
76
96
|
for (const k of ks) {
|
|
77
97
|
const attr = attrs[k]
|
|
78
|
-
attr
|
|
79
|
-
|
|
80
|
-
pks.push(attr)
|
|
98
|
+
if (!attr) {
|
|
99
|
+
continue
|
|
81
100
|
}
|
|
82
101
|
let v = o[k]
|
|
83
|
-
if (v == null) {
|
|
102
|
+
if (v == null && attr.default !== undefined) {
|
|
84
103
|
v = attr.default
|
|
85
104
|
}
|
|
86
105
|
if (v != null && !attr.ignored && !attr.noinsert) {
|
|
87
106
|
const field = attr.column ? attr.column : k
|
|
88
107
|
cols.push(field)
|
|
89
|
-
if (
|
|
108
|
+
if (k === ver) {
|
|
90
109
|
isVersion = true
|
|
110
|
+
ver = k
|
|
91
111
|
values.push(`${1}`)
|
|
92
112
|
} else {
|
|
93
113
|
if (v === "") {
|
|
@@ -99,10 +119,10 @@ export function buildToSave<T>(obj: T, table: string, attrs: Attributes, buildPa
|
|
|
99
119
|
values.push(p)
|
|
100
120
|
if (typeof v === "boolean") {
|
|
101
121
|
if (v === true) {
|
|
102
|
-
const v2 = attr.true !== undefined ? attr.true : `
|
|
122
|
+
const v2 = attr.true !== undefined ? attr.true : `1`
|
|
103
123
|
args.push(v2)
|
|
104
124
|
} else {
|
|
105
|
-
const v2 = attr.false !== undefined ? attr.false : `
|
|
125
|
+
const v2 = attr.false !== undefined ? attr.false : `0`
|
|
106
126
|
args.push(v2)
|
|
107
127
|
}
|
|
108
128
|
} else {
|
|
@@ -112,15 +132,29 @@ export function buildToSave<T>(obj: T, table: string, attrs: Attributes, buildPa
|
|
|
112
132
|
}
|
|
113
133
|
}
|
|
114
134
|
}
|
|
115
|
-
if (
|
|
135
|
+
if (cols.length > 0 && ver && isVersion === false) {
|
|
136
|
+
const attr = attrs[ver]
|
|
137
|
+
if (attr) {
|
|
138
|
+
const field = attr.column ? attr.column : ver
|
|
139
|
+
cols.push(field)
|
|
140
|
+
values.push("1")
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
if (isUpdate === false || pks.length === 0) {
|
|
116
144
|
if (cols.length === 0) {
|
|
117
145
|
return { query: "", params: args }
|
|
118
146
|
} else {
|
|
119
|
-
|
|
120
|
-
|
|
147
|
+
if (pks.length === 0) {
|
|
148
|
+
const q = `insert into ${table}(${cols.join(",")})values(${values.join(",")})`
|
|
149
|
+
return { query: q, params: args }
|
|
150
|
+
} else {
|
|
151
|
+
const q = `insert ignore into ${table}(${cols.join(",")})values(${values.join(",")})`
|
|
152
|
+
return { query: q, params: args }
|
|
153
|
+
}
|
|
121
154
|
}
|
|
122
155
|
} else {
|
|
123
156
|
const colSet: string[] = []
|
|
157
|
+
let version: string | null = null
|
|
124
158
|
for (const k of ks) {
|
|
125
159
|
const v = o[k]
|
|
126
160
|
if (v !== undefined) {
|
|
@@ -128,98 +162,52 @@ export function buildToSave<T>(obj: T, table: string, attrs: Attributes, buildPa
|
|
|
128
162
|
if (attr && !attr.key && !attr.ignored && !attr.noupdate) {
|
|
129
163
|
const field = attr.column ? attr.column : k
|
|
130
164
|
let x: string
|
|
131
|
-
if (
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
x = `''`
|
|
135
|
-
} else if (typeof v === "number") {
|
|
136
|
-
x = toString(v)
|
|
165
|
+
if (attr.version) {
|
|
166
|
+
version = k
|
|
167
|
+
x = `${field} + 1`
|
|
137
168
|
} else {
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
169
|
+
if (v === "") {
|
|
170
|
+
x = `''`
|
|
171
|
+
} else if (typeof v === "number") {
|
|
172
|
+
x = toString(v)
|
|
173
|
+
} else {
|
|
174
|
+
x = buildParam(i++)
|
|
175
|
+
if (typeof v === "boolean") {
|
|
176
|
+
if (v === true) {
|
|
177
|
+
const v2 = attr.true !== undefined ? attr.true : `1`
|
|
178
|
+
args.push(v2)
|
|
179
|
+
} else {
|
|
180
|
+
const v2 = attr.false !== undefined ? attr.false : `0`
|
|
181
|
+
args.push(v2)
|
|
182
|
+
}
|
|
143
183
|
} else {
|
|
144
|
-
|
|
145
|
-
args.push(v2)
|
|
184
|
+
args.push(v)
|
|
146
185
|
}
|
|
147
|
-
} else {
|
|
148
|
-
args.push(v)
|
|
149
186
|
}
|
|
150
187
|
}
|
|
151
188
|
colSet.push(`${field}=${x}`)
|
|
152
189
|
}
|
|
153
190
|
}
|
|
154
191
|
}
|
|
155
|
-
if (
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
export function buildToSaveBatch<T>(objs: T[], table: string, attrs: Attributes, buildParam?: (i: number) => string): Statement[] {
|
|
165
|
-
if (!buildParam) {
|
|
166
|
-
buildParam = param
|
|
167
|
-
}
|
|
168
|
-
const sts: Statement[] = []
|
|
169
|
-
const ks = Object.keys(attrs)
|
|
170
|
-
for (const obj of objs) {
|
|
171
|
-
let i = 1
|
|
172
|
-
const cols: string[] = []
|
|
173
|
-
const values: string[] = []
|
|
174
|
-
const args: any[] = []
|
|
175
|
-
let isVersion = false
|
|
176
|
-
const o: any = obj
|
|
177
|
-
for (const k of ks) {
|
|
178
|
-
const attr = attrs[k]
|
|
179
|
-
let v = o[k]
|
|
180
|
-
if (v == null) {
|
|
181
|
-
v = attr.default
|
|
182
|
-
}
|
|
183
|
-
if (v != null && !attr.ignored && !attr.noinsert) {
|
|
184
|
-
const field = attr.column ? attr.column : k
|
|
185
|
-
cols.push(field)
|
|
186
|
-
if (attr.version) {
|
|
187
|
-
isVersion = true
|
|
188
|
-
values.push(`${1}`)
|
|
189
|
-
} else {
|
|
190
|
-
if (v === "") {
|
|
191
|
-
values.push(`''`)
|
|
192
|
-
} else if (typeof v === "number") {
|
|
193
|
-
values.push(toString(v))
|
|
194
|
-
} else {
|
|
195
|
-
const p = buildParam(i++)
|
|
196
|
-
values.push(p)
|
|
197
|
-
if (typeof v === "boolean") {
|
|
198
|
-
if (v === true) {
|
|
199
|
-
const v2 = attr.true !== undefined ? attr.true : `'1'`
|
|
200
|
-
args.push(v2)
|
|
201
|
-
} else {
|
|
202
|
-
const v2 = attr.false !== undefined ? attr.false : `'0'`
|
|
203
|
-
args.push(v2)
|
|
204
|
-
}
|
|
205
|
-
} else {
|
|
206
|
-
args.push(v)
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
}
|
|
192
|
+
if (cols.length > 0) {
|
|
193
|
+
if (colSet.length === 0) {
|
|
194
|
+
const q = `insert ignore into ${table}(${cols.join(",")})values(${values.join(",")})`
|
|
195
|
+
return { query: q, params: args }
|
|
196
|
+
} else {
|
|
197
|
+
const q = `insert into ${table}(${cols.join(",")})values(${values.join(",")}) on duplicate key update ${colSet.join(",")}`
|
|
198
|
+
return { query: q, params: args }
|
|
210
199
|
}
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
const attr
|
|
217
|
-
|
|
218
|
-
const
|
|
200
|
+
} else {
|
|
201
|
+
if (colSet.length === 0) {
|
|
202
|
+
return { query: "", params: args }
|
|
203
|
+
} else {
|
|
204
|
+
const colQuery: string[] = []
|
|
205
|
+
for (const attr of pks) {
|
|
206
|
+
const field = attr.column ? attr.column : attr.name
|
|
207
|
+
const na = attr.name ? attr.name : ""
|
|
208
|
+
const v = o[na]
|
|
219
209
|
let x: string
|
|
220
|
-
if (v ===
|
|
221
|
-
x = "null"
|
|
222
|
-
} else if (v === "") {
|
|
210
|
+
if (v === "") {
|
|
223
211
|
x = `''`
|
|
224
212
|
} else if (typeof v === "number") {
|
|
225
213
|
x = toString(v)
|
|
@@ -227,27 +215,64 @@ export function buildToSaveBatch<T>(objs: T[], table: string, attrs: Attributes,
|
|
|
227
215
|
x = buildParam(i++)
|
|
228
216
|
if (typeof v === "boolean") {
|
|
229
217
|
if (v === true) {
|
|
230
|
-
const v2 = attr.true !== undefined ? attr.true : `
|
|
218
|
+
const v2 = attr.true !== undefined ? "" + attr.true : `1`
|
|
231
219
|
args.push(v2)
|
|
232
220
|
} else {
|
|
233
|
-
const v2 = attr.false !== undefined ? attr.false : `
|
|
221
|
+
const v2 = attr.false !== undefined ? "" + attr.false : `0`
|
|
234
222
|
args.push(v2)
|
|
235
223
|
}
|
|
236
224
|
} else {
|
|
237
225
|
args.push(v)
|
|
238
226
|
}
|
|
239
227
|
}
|
|
240
|
-
|
|
228
|
+
colQuery.push(`${field}=${x}`)
|
|
229
|
+
}
|
|
230
|
+
if (version && version.length > 0) {
|
|
231
|
+
const v = o[version]
|
|
232
|
+
if (typeof v === "number" && !isNaN(v)) {
|
|
233
|
+
const attr = attrs[version]
|
|
234
|
+
if (attr) {
|
|
235
|
+
const field = attr.column ? attr.column : version
|
|
236
|
+
colQuery.push(`${field}=${v}`)
|
|
237
|
+
}
|
|
238
|
+
}
|
|
241
239
|
}
|
|
240
|
+
const q = `update ${table} set ${colSet.join(",")} where ${colQuery.join(" and ")}`
|
|
241
|
+
return { query: q, params: args }
|
|
242
242
|
}
|
|
243
243
|
}
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
export function buildToSaveBatch<T>(
|
|
247
|
+
objs: T[],
|
|
248
|
+
table: string,
|
|
249
|
+
attrs: Attributes,
|
|
250
|
+
pks?: Attribute[],
|
|
251
|
+
ver?: string,
|
|
252
|
+
buildParam?: (i: number) => string,
|
|
253
|
+
): Statement[] {
|
|
254
|
+
if (!buildParam) {
|
|
255
|
+
buildParam = param
|
|
256
|
+
}
|
|
257
|
+
const sts: Statement[] = []
|
|
258
|
+
if (!pks) {
|
|
259
|
+
pks = []
|
|
260
|
+
const ks = Object.keys(attrs)
|
|
261
|
+
for (const k of ks) {
|
|
262
|
+
const attr = attrs[k]
|
|
263
|
+
attr.name = k
|
|
264
|
+
if (attr.key) {
|
|
265
|
+
pks.push(attr)
|
|
266
|
+
}
|
|
267
|
+
if (attr.version) {
|
|
268
|
+
ver = k
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
for (const obj of objs) {
|
|
274
|
+
const smt = buildToSave(obj, table, attrs, pks, ver, buildParam)
|
|
275
|
+
if (smt.query) {
|
|
251
276
|
sts.push(smt)
|
|
252
277
|
}
|
|
253
278
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Connection, createPool as createPool2, FieldPacket, Pool, PoolConnection, ResultSetHeader } from "mysql2"
|
|
2
2
|
import { Pool as PromisePool } from "mysql2/promise"
|
|
3
|
-
import { buildToSave, buildToSaveBatch, param } from "./build"
|
|
3
|
+
import { buildToSave, buildToSaveBatch, metadata, param } from "./build"
|
|
4
4
|
import { Attribute, Attributes, DB, Statement, StringMap, Transaction } from "./metadata"
|
|
5
5
|
|
|
6
6
|
export * from "./build"
|
|
@@ -40,32 +40,24 @@ export function getConnection(pool: Pool): Promise<PoolConnection> {
|
|
|
40
40
|
})
|
|
41
41
|
})
|
|
42
42
|
}
|
|
43
|
-
export function beginTransaction(connection: PoolConnection
|
|
43
|
+
export function beginTransaction(connection: PoolConnection): Promise<void> {
|
|
44
44
|
return new Promise<void>((resolve, reject) => {
|
|
45
|
-
connection.beginTransaction((
|
|
46
|
-
if (
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
}
|
|
50
|
-
return connection.rollback(() => {
|
|
51
|
-
return reject(err)
|
|
52
|
-
})
|
|
45
|
+
connection.beginTransaction((beginError) => {
|
|
46
|
+
if (beginError) {
|
|
47
|
+
connection.release()
|
|
48
|
+
return reject(beginError)
|
|
53
49
|
}
|
|
54
50
|
return resolve()
|
|
55
51
|
})
|
|
56
52
|
})
|
|
57
53
|
}
|
|
58
|
-
export function commit(connection: PoolConnection
|
|
54
|
+
export function commit(connection: PoolConnection): Promise<void> {
|
|
59
55
|
return new Promise<void>((resolve, reject) => {
|
|
60
56
|
connection.commit((err) => {
|
|
61
57
|
if (err) {
|
|
62
|
-
|
|
63
|
-
return reject(err)
|
|
64
|
-
}
|
|
65
|
-
return connection.rollback(() => {
|
|
66
|
-
return reject(err)
|
|
67
|
-
})
|
|
58
|
+
return reject(err)
|
|
68
59
|
}
|
|
60
|
+
connection.release()
|
|
69
61
|
return resolve()
|
|
70
62
|
})
|
|
71
63
|
})
|
|
@@ -73,6 +65,7 @@ export function commit(connection: PoolConnection, rollbackIfError?: boolean): P
|
|
|
73
65
|
export function rollback(connection: PoolConnection): Promise<void> {
|
|
74
66
|
return new Promise<void>((resolve, reject) => {
|
|
75
67
|
connection.rollback((err) => {
|
|
68
|
+
connection.release()
|
|
76
69
|
if (err) {
|
|
77
70
|
return reject(err)
|
|
78
71
|
}
|
|
@@ -103,10 +96,8 @@ export class PoolManager implements DB {
|
|
|
103
96
|
}
|
|
104
97
|
connection.beginTransaction((beginError) => {
|
|
105
98
|
if (beginError) {
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
return reject(beginError)
|
|
109
|
-
})
|
|
99
|
+
connection.release()
|
|
100
|
+
return reject(beginError)
|
|
110
101
|
}
|
|
111
102
|
return resolve(new PoolConnectionManager(connection))
|
|
112
103
|
})
|
|
@@ -116,8 +107,8 @@ export class PoolManager implements DB {
|
|
|
116
107
|
execute(sql: string, args?: any[]): Promise<number> {
|
|
117
108
|
return execute(this.pool, sql, args)
|
|
118
109
|
}
|
|
119
|
-
executeBatch(statements: Statement[],
|
|
120
|
-
return executeBatch(this.pool, statements,
|
|
110
|
+
executeBatch(statements: Statement[], requireFirstAffected?: boolean): Promise<number> {
|
|
111
|
+
return executeBatch(this.pool, statements, requireFirstAffected)
|
|
121
112
|
}
|
|
122
113
|
query<T>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[]): Promise<T[]> {
|
|
123
114
|
return query(this.pool, sql, args, m, bools)
|
|
@@ -143,6 +134,7 @@ export class PoolConnectionManager implements Transaction {
|
|
|
143
134
|
this.queryOne = this.queryOne.bind(this)
|
|
144
135
|
this.executeScalar = this.executeScalar.bind(this)
|
|
145
136
|
this.count = this.count.bind(this)
|
|
137
|
+
this.release = this.release.bind(this)
|
|
146
138
|
}
|
|
147
139
|
driver = "mysql"
|
|
148
140
|
param(i: number): string {
|
|
@@ -152,10 +144,7 @@ export class PoolConnectionManager implements Transaction {
|
|
|
152
144
|
return new Promise<void>((resolve, reject) => {
|
|
153
145
|
this.connection.commit((err) => {
|
|
154
146
|
if (err) {
|
|
155
|
-
return
|
|
156
|
-
this.release()
|
|
157
|
-
return reject(err)
|
|
158
|
-
})
|
|
147
|
+
return reject(err)
|
|
159
148
|
}
|
|
160
149
|
this.release()
|
|
161
150
|
return resolve()
|
|
@@ -183,10 +172,10 @@ export class PoolConnectionManager implements Transaction {
|
|
|
183
172
|
execute(sql: string, args?: any[]): Promise<number> {
|
|
184
173
|
return execute(this.connection, sql, args)
|
|
185
174
|
}
|
|
186
|
-
executeBatch(statements: Statement[],
|
|
187
|
-
return executeBatchConnection(this.connection, statements,
|
|
175
|
+
executeBatch(statements: Statement[], requireFirstAffected?: boolean): Promise<number> {
|
|
176
|
+
return executeBatchConnection(this.connection, statements, requireFirstAffected)
|
|
188
177
|
}
|
|
189
|
-
query<T>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[]
|
|
178
|
+
query<T>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[]): Promise<T[]> {
|
|
190
179
|
return query(this.connection, sql, args, m, bools)
|
|
191
180
|
}
|
|
192
181
|
queryOne<T>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[]): Promise<T | null> {
|
|
@@ -199,7 +188,7 @@ export class PoolConnectionManager implements Transaction {
|
|
|
199
188
|
return count(this.connection, sql, args)
|
|
200
189
|
}
|
|
201
190
|
}
|
|
202
|
-
export async function executeBatch(pool: Pool, statements: Statement[],
|
|
191
|
+
export async function executeBatch(pool: Pool, statements: Statement[], requireFirstAffected?: boolean): Promise<number> {
|
|
203
192
|
if (!statements || statements.length === 0) {
|
|
204
193
|
return 0
|
|
205
194
|
}
|
|
@@ -207,31 +196,29 @@ export async function executeBatch(pool: Pool, statements: Statement[], firstAff
|
|
|
207
196
|
return execute(pool, statements[0].query, statements[0].params)
|
|
208
197
|
}
|
|
209
198
|
const connection = await getConnection(pool)
|
|
210
|
-
|
|
211
|
-
return await executeBatchConnectionTx(connection, statements, firstAffected)
|
|
212
|
-
} finally {
|
|
213
|
-
connection.release()
|
|
214
|
-
}
|
|
199
|
+
return await executeBatchConnectionTx(connection, statements, requireFirstAffected)
|
|
215
200
|
}
|
|
216
|
-
export async function executeBatchConnectionTx(connection: PoolConnection, statements: Statement[],
|
|
201
|
+
export async function executeBatchConnectionTx(connection: PoolConnection, statements: Statement[], requireFirstAffected?: boolean): Promise<number> {
|
|
217
202
|
if (!statements || statements.length === 0) {
|
|
218
203
|
return 0
|
|
219
204
|
}
|
|
220
205
|
if (statements.length === 1) {
|
|
221
|
-
|
|
206
|
+
const count = await execute(connection, statements[0].query, statements[0].params)
|
|
207
|
+
connection.release()
|
|
208
|
+
return count
|
|
222
209
|
}
|
|
223
|
-
await beginTransaction(connection
|
|
210
|
+
await beginTransaction(connection)
|
|
224
211
|
try {
|
|
225
212
|
let count = 0
|
|
226
213
|
|
|
227
214
|
if (resource.multipleStatements) {
|
|
228
|
-
if (
|
|
215
|
+
if (requireFirstAffected) {
|
|
229
216
|
const firstCount = await execute(connection, statements[0].query, statements[0].params)
|
|
230
217
|
|
|
231
218
|
count = firstCount
|
|
232
219
|
|
|
233
220
|
if (firstCount === 0) {
|
|
234
|
-
await commit(connection
|
|
221
|
+
await commit(connection)
|
|
235
222
|
return 0
|
|
236
223
|
}
|
|
237
224
|
const remaining = statements.slice(1)
|
|
@@ -240,12 +227,12 @@ export async function executeBatchConnectionTx(connection: PoolConnection, state
|
|
|
240
227
|
count = await executeMultipleStatements(connection, statements)
|
|
241
228
|
}
|
|
242
229
|
} else {
|
|
243
|
-
const start =
|
|
244
|
-
if (
|
|
230
|
+
const start = requireFirstAffected ? 1 : 0
|
|
231
|
+
if (requireFirstAffected) {
|
|
245
232
|
const firstCount = await execute(connection, statements[0].query, statements[0].params)
|
|
246
233
|
count = firstCount
|
|
247
234
|
if (firstCount === 0) {
|
|
248
|
-
await commit(connection
|
|
235
|
+
await commit(connection)
|
|
249
236
|
return 0
|
|
250
237
|
}
|
|
251
238
|
}
|
|
@@ -255,7 +242,7 @@ export async function executeBatchConnectionTx(connection: PoolConnection, state
|
|
|
255
242
|
}
|
|
256
243
|
}
|
|
257
244
|
|
|
258
|
-
await commit(connection
|
|
245
|
+
await commit(connection)
|
|
259
246
|
|
|
260
247
|
return count
|
|
261
248
|
} catch (err) {
|
|
@@ -264,11 +251,12 @@ export async function executeBatchConnectionTx(connection: PoolConnection, state
|
|
|
264
251
|
} catch {
|
|
265
252
|
// Preserve the original error.
|
|
266
253
|
}
|
|
267
|
-
|
|
254
|
+
buildError(err)
|
|
255
|
+
throw err
|
|
268
256
|
}
|
|
269
257
|
}
|
|
270
258
|
|
|
271
|
-
export async function executeBatchConnection(connection: PoolConnection, statements: Statement[],
|
|
259
|
+
export async function executeBatchConnection(connection: PoolConnection, statements: Statement[], requireFirstAffected?: boolean): Promise<number> {
|
|
272
260
|
if (!statements || statements.length === 0) {
|
|
273
261
|
return 0
|
|
274
262
|
}
|
|
@@ -278,7 +266,7 @@ export async function executeBatchConnection(connection: PoolConnection, stateme
|
|
|
278
266
|
}
|
|
279
267
|
|
|
280
268
|
if (resource.multipleStatements) {
|
|
281
|
-
if (
|
|
269
|
+
if (requireFirstAffected) {
|
|
282
270
|
const firstCount = await execute(connection, statements[0].query, statements[0].params)
|
|
283
271
|
|
|
284
272
|
if (firstCount === 0) {
|
|
@@ -292,9 +280,9 @@ export async function executeBatchConnection(connection: PoolConnection, stateme
|
|
|
292
280
|
}
|
|
293
281
|
|
|
294
282
|
let count = 0
|
|
295
|
-
const start =
|
|
283
|
+
const start = requireFirstAffected ? 1 : 0
|
|
296
284
|
|
|
297
|
-
if (
|
|
285
|
+
if (requireFirstAffected) {
|
|
298
286
|
const firstCount = await execute(connection, statements[0].query, statements[0].params)
|
|
299
287
|
|
|
300
288
|
count = firstCount
|
|
@@ -424,25 +412,6 @@ export function count(pool: Pool | PoolConnection, sql: string, args?: any[]): P
|
|
|
424
412
|
return executeScalar<number>(pool, sql, args).then((res) => (res !== null ? res : 0))
|
|
425
413
|
}
|
|
426
414
|
|
|
427
|
-
export function save<T>(pool: Pool | PoolConnection | ((sql: string, args?: any[]) => Promise<number>), obj: T, table: string, attrs: Attributes, buildParam?: (i: number) => string): Promise<number> {
|
|
428
|
-
const s = buildToSave(obj, table, attrs, buildParam)
|
|
429
|
-
if (!s.query) {
|
|
430
|
-
return Promise.resolve(-1)
|
|
431
|
-
}
|
|
432
|
-
if (typeof pool === "function") {
|
|
433
|
-
return pool(s.query, s.params)
|
|
434
|
-
} else {
|
|
435
|
-
return execute(pool, s.query, s.params)
|
|
436
|
-
}
|
|
437
|
-
}
|
|
438
|
-
export function saveBatch<T>(pool: Pool | ((statements: Statement[]) => Promise<number>), objs: T[], table: string, attrs: Attributes, buildParam?: (i: number) => string): Promise<number> {
|
|
439
|
-
const s = buildToSaveBatch(objs, table, attrs, buildParam)
|
|
440
|
-
if (typeof pool === "function") {
|
|
441
|
-
return pool(s)
|
|
442
|
-
} else {
|
|
443
|
-
return executeBatch(pool, s)
|
|
444
|
-
}
|
|
445
|
-
}
|
|
446
415
|
export function toArray(arr?: any[]): any[] {
|
|
447
416
|
if (!arr || arr.length === 0) {
|
|
448
417
|
return []
|
|
@@ -633,9 +602,13 @@ export class StringAdapter {
|
|
|
633
602
|
return execute(this.pool, s, values)
|
|
634
603
|
}
|
|
635
604
|
}
|
|
605
|
+
export const StringRepository = StringAdapter
|
|
606
|
+
export const StringService = StringAdapter
|
|
636
607
|
|
|
637
608
|
// tslint:disable-next-line:max-classes-per-file
|
|
638
609
|
export class MySQLWriter<T> {
|
|
610
|
+
protected keys: Attribute[]
|
|
611
|
+
protected version?: string
|
|
639
612
|
protected param?: (i: number) => string
|
|
640
613
|
constructor(
|
|
641
614
|
protected pool: Pool | PoolConnection,
|
|
@@ -647,6 +620,9 @@ export class MySQLWriter<T> {
|
|
|
647
620
|
) {
|
|
648
621
|
this.write = this.write.bind(this)
|
|
649
622
|
this.param = buildParam ? buildParam : param
|
|
623
|
+
const m = metadata(attributes)
|
|
624
|
+
this.keys = m.keys
|
|
625
|
+
this.version = m.version
|
|
650
626
|
}
|
|
651
627
|
write(obj: T): Promise<number> {
|
|
652
628
|
if (!obj) {
|
|
@@ -656,7 +632,7 @@ export class MySQLWriter<T> {
|
|
|
656
632
|
if (this.map) {
|
|
657
633
|
obj2 = this.map(obj)
|
|
658
634
|
}
|
|
659
|
-
const stmt = buildToSave(obj2, this.table, this.attributes, this.param)
|
|
635
|
+
const stmt = buildToSave(obj2, this.table, this.attributes, this.keys, this.version, this.param)
|
|
660
636
|
if (stmt.query) {
|
|
661
637
|
if (this.oneIfSuccess) {
|
|
662
638
|
return execute(this.pool, stmt.query, stmt.params).then((ct) => (ct > 0 ? 1 : 0))
|
|
@@ -669,8 +645,10 @@ export class MySQLWriter<T> {
|
|
|
669
645
|
}
|
|
670
646
|
}
|
|
671
647
|
// tslint:disable-next-line:max-classes-per-file
|
|
672
|
-
export class
|
|
648
|
+
export class BufferedBatchWriter<T> {
|
|
673
649
|
protected list: T[] = []
|
|
650
|
+
protected keys: Attribute[]
|
|
651
|
+
protected version?: string
|
|
674
652
|
protected param?: (i: number) => string
|
|
675
653
|
constructor(
|
|
676
654
|
protected pool: Pool,
|
|
@@ -683,6 +661,9 @@ export class MySQLStreamWriter<T> {
|
|
|
683
661
|
this.write = this.write.bind(this)
|
|
684
662
|
this.flush = this.flush.bind(this)
|
|
685
663
|
this.param = buildParam ? buildParam : param
|
|
664
|
+
const m = metadata(attributes)
|
|
665
|
+
this.keys = m.keys
|
|
666
|
+
this.version = m.version
|
|
686
667
|
}
|
|
687
668
|
write(obj: T): Promise<number> {
|
|
688
669
|
if (!obj) {
|
|
@@ -705,11 +686,11 @@ export class MySQLStreamWriter<T> {
|
|
|
705
686
|
if (!this.list || this.list.length === 0) {
|
|
706
687
|
return Promise.resolve(0)
|
|
707
688
|
} else {
|
|
708
|
-
const stmt = buildToSaveBatch(this.list, this.table, this.attributes, this.param)
|
|
689
|
+
const stmt = buildToSaveBatch(this.list, this.table, this.attributes, this.keys, this.version, this.param)
|
|
709
690
|
if (stmt.length > 0) {
|
|
710
691
|
return executeBatch(this.pool as any, stmt).then((r) => {
|
|
711
692
|
this.list = []
|
|
712
|
-
return
|
|
693
|
+
return r
|
|
713
694
|
})
|
|
714
695
|
} else {
|
|
715
696
|
this.list = []
|
|
@@ -719,18 +700,22 @@ export class MySQLStreamWriter<T> {
|
|
|
719
700
|
}
|
|
720
701
|
}
|
|
721
702
|
// tslint:disable-next-line:max-classes-per-file
|
|
722
|
-
export class
|
|
703
|
+
export class BatchWriter<T> {
|
|
704
|
+
protected keys: Attribute[]
|
|
705
|
+
protected version?: string
|
|
723
706
|
protected param?: (i: number) => string
|
|
724
707
|
constructor(
|
|
725
708
|
protected pool: Pool,
|
|
726
709
|
protected table: string,
|
|
727
710
|
protected attributes: Attributes,
|
|
728
|
-
protected oneIfSuccess?: boolean,
|
|
729
711
|
protected map?: (v: T) => T,
|
|
730
712
|
buildParam?: (i: number) => string,
|
|
731
713
|
) {
|
|
732
714
|
this.write = this.write.bind(this)
|
|
733
715
|
this.param = buildParam ? buildParam : param
|
|
716
|
+
const m = metadata(attributes)
|
|
717
|
+
this.keys = m.keys
|
|
718
|
+
this.version = m.version
|
|
734
719
|
}
|
|
735
720
|
write(objs: T[]): Promise<number> {
|
|
736
721
|
if (!objs || objs.length === 0) {
|
|
@@ -744,13 +729,9 @@ export class MySQLBatchWriter<T> {
|
|
|
744
729
|
list.push(obj2)
|
|
745
730
|
}
|
|
746
731
|
}
|
|
747
|
-
const stmts = buildToSaveBatch(list, this.table, this.attributes, this.param)
|
|
732
|
+
const stmts = buildToSaveBatch(list, this.table, this.attributes, this.keys, this.version, this.param)
|
|
748
733
|
if (stmts.length > 0) {
|
|
749
|
-
|
|
750
|
-
return executeBatch(this.pool, stmts).then((ct) => stmts.length)
|
|
751
|
-
} else {
|
|
752
|
-
return executeBatch(this.pool, stmts)
|
|
753
|
-
}
|
|
734
|
+
return executeBatch(this.pool, stmts)
|
|
754
735
|
} else {
|
|
755
736
|
return Promise.resolve(0)
|
|
756
737
|
}
|
package/src/metadata.ts
CHANGED
|
@@ -10,7 +10,7 @@ export interface Executor {
|
|
|
10
10
|
driver: string
|
|
11
11
|
param(i: number): string
|
|
12
12
|
execute(sql: string, args?: any[]): Promise<number>
|
|
13
|
-
executeBatch(statements: Statement[],
|
|
13
|
+
executeBatch(statements: Statement[], requireFirstAffected?: boolean): Promise<number>
|
|
14
14
|
query<T>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[]): Promise<T[]>
|
|
15
15
|
queryOne<T>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[]): Promise<T | null>
|
|
16
16
|
executeScalar<T>(sql: string, args?: any[]): Promise<T | null>
|