mysql2-core 0.3.9 → 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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mysql2-core",
3
- "version": "0.3.9",
3
+ "version": "0.5.1",
4
4
  "description": "mysql2-core",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./src/index.ts",
package/src/build.ts CHANGED
@@ -59,28 +59,47 @@ 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, ver?: string, buildParam?: (i: number) => string, i?: number): Statement {
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
- const pks: Attribute[] = []
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.name = k
79
- if (attr.key) {
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) {
@@ -88,6 +107,7 @@ export function buildToSave<T>(obj: T, table: string, attrs: Attributes, ver?: s
88
107
  cols.push(field)
89
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, ver?: s
99
119
  values.push(p)
100
120
  if (typeof v === "boolean") {
101
121
  if (v === true) {
102
- const v2 = attr.true ? attr.true : `'1'`
122
+ const v2 = attr.true !== undefined ? attr.true : `1`
103
123
  args.push(v2)
104
124
  } else {
105
- const v2 = attr.false && attr.false !== 0 ? attr.false : `'0'`
125
+ const v2 = attr.false !== undefined ? attr.false : `0`
106
126
  args.push(v2)
107
127
  }
108
128
  } else {
@@ -112,21 +132,29 @@ export function buildToSave<T>(obj: T, table: string, attrs: Attributes, ver?: s
112
132
  }
113
133
  }
114
134
  }
115
- if (!isVersion && ver && ver.length > 0) {
135
+ if (cols.length > 0 && ver && isVersion === false) {
116
136
  const attr = attrs[ver]
117
- const field = attr.column ? attr.column : ver
118
- cols.push(field)
119
- values.push(`${1}`)
137
+ if (attr) {
138
+ const field = attr.column ? attr.column : ver
139
+ cols.push(field)
140
+ values.push("1")
141
+ }
120
142
  }
121
- if (pks.length === 0) {
143
+ if (isUpdate === false || pks.length === 0) {
122
144
  if (cols.length === 0) {
123
145
  return { query: "", params: args }
124
146
  } else {
125
- const q = `insert into ${table}(${cols.join(",")})values(${values.join(",")})`
126
- return { query: q, params: args }
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
+ }
127
154
  }
128
155
  } else {
129
156
  const colSet: string[] = []
157
+ let version: string | null = null
130
158
  for (const k of ks) {
131
159
  const v = o[k]
132
160
  if (v !== undefined) {
@@ -134,104 +162,52 @@ export function buildToSave<T>(obj: T, table: string, attrs: Attributes, ver?: s
134
162
  if (attr && !attr.key && !attr.ignored && !attr.noupdate) {
135
163
  const field = attr.column ? attr.column : k
136
164
  let x: string
137
- if (v === null) {
138
- x = "null"
139
- } else if (v === "") {
140
- x = `''`
141
- } else if (typeof v === "number") {
142
- x = toString(v)
165
+ if (attr.version) {
166
+ version = k
167
+ x = `${field} + 1`
143
168
  } else {
144
- x = buildParam(i++)
145
- if (typeof v === "boolean") {
146
- if (v === true) {
147
- const v2 = attr.true ? attr.true : `'1'`
148
- args.push(v2)
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
+ }
149
183
  } else {
150
- const v2 = attr.false && attr.false !== 0 ? attr.false : `'0'`
151
- args.push(v2)
184
+ args.push(v)
152
185
  }
153
- } else {
154
- args.push(v)
155
186
  }
156
187
  }
157
188
  colSet.push(`${field}=${x}`)
158
189
  }
159
190
  }
160
191
  }
161
- if (colSet.length === 0) {
162
- const q = `insert ignore into ${table}(${cols.join(",")})values(${values.join(",")})`
163
- return { query: q, params: args }
164
- } else {
165
- const q = `insert into ${table}(${cols.join(",")})values(${values.join(",")}) on duplicate key update ${colSet.join(",")}`
166
- return { query: q, params: args }
167
- }
168
- }
169
- }
170
- export function buildToSaveBatch<T>(objs: T[], table: string, attrs: Attributes, ver?: string, buildParam?: (i: number) => string): Statement[] {
171
- if (!buildParam) {
172
- buildParam = param
173
- }
174
- const sts: Statement[] = []
175
- const ks = Object.keys(attrs)
176
- for (const obj of objs) {
177
- let i = 1
178
- const cols: string[] = []
179
- const values: string[] = []
180
- const args: any[] = []
181
- let isVersion = false
182
- const o: any = obj
183
- for (const k of ks) {
184
- const attr = attrs[k]
185
- let v = o[k]
186
- if (v == null) {
187
- v = attr.default
188
- }
189
- if (v != null && !attr.ignored && !attr.noinsert) {
190
- const field = attr.column ? attr.column : k
191
- cols.push(field)
192
- if (k === ver) {
193
- isVersion = true
194
- values.push(`${1}`)
195
- } else {
196
- if (v === "") {
197
- values.push(`''`)
198
- } else if (typeof v === "number") {
199
- values.push(toString(v))
200
- } else {
201
- const p = buildParam(i++)
202
- values.push(p)
203
- if (typeof v === "boolean") {
204
- if (v === true) {
205
- const v2 = attr.true ? attr.true : `'1'`
206
- args.push(v2)
207
- } else {
208
- const v2 = attr.false && attr.false !== 0 ? attr.false : `'0'`
209
- args.push(v2)
210
- }
211
- } else {
212
- args.push(v)
213
- }
214
- }
215
- }
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 }
216
199
  }
217
- }
218
- if (!isVersion && ver && ver.length > 0) {
219
- const attr = attrs[ver]
220
- const field = attr.column ? attr.column : ver
221
- cols.push(field)
222
- values.push(`${1}`)
223
- }
224
- const colSet: string[] = []
225
- for (const k of ks) {
226
- const v = o[k]
227
- if (v !== undefined) {
228
- const attr = attrs[k]
229
- if (attr && !attr.key && !attr.ignored && k !== ver && !attr.noupdate) {
230
- const field = attr.column ? attr.column : k
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]
231
209
  let x: string
232
- if (v === null) {
233
- x = "null"
234
- } else if (v === "") {
210
+ if (v === "") {
235
211
  x = `''`
236
212
  } else if (typeof v === "number") {
237
213
  x = toString(v)
@@ -239,27 +215,64 @@ export function buildToSaveBatch<T>(objs: T[], table: string, attrs: Attributes,
239
215
  x = buildParam(i++)
240
216
  if (typeof v === "boolean") {
241
217
  if (v === true) {
242
- const v2 = attr.true ? attr.true : `'1'`
218
+ const v2 = attr.true !== undefined ? "" + attr.true : `1`
243
219
  args.push(v2)
244
220
  } else {
245
- const v2 = attr.false && attr.false !== 0 ? attr.false : `'0'`
221
+ const v2 = attr.false !== undefined ? "" + attr.false : `0`
246
222
  args.push(v2)
247
223
  }
248
224
  } else {
249
225
  args.push(v)
250
226
  }
251
227
  }
252
- colSet.push(`${field}=${x}`)
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
+ }
253
239
  }
240
+ const q = `update ${table} set ${colSet.join(",")} where ${colQuery.join(" and ")}`
241
+ return { query: q, params: args }
254
242
  }
255
243
  }
256
- if (colSet.length === 0) {
257
- const q = `insert ignore into ${table}(${cols.join(",")})values(${values.join(",")});`
258
- const smt: Statement = { query: q, params: args }
259
- sts.push(smt)
260
- } else {
261
- const q = `insert into ${table}(${cols.join(",")})values(${values.join(",")}) on duplicate key update ${colSet.join(",")};`
262
- const smt: Statement = { query: q, params: args }
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) {
263
276
  sts.push(smt)
264
277
  }
265
278
  }