jsql-neo 4.0.1 → 4.0.2
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/native_client.js
CHANGED
|
@@ -11,9 +11,16 @@ function encodeBatch(rows) {
|
|
|
11
11
|
if (rows.length === 0) return new Uint8Array(0);
|
|
12
12
|
const fieldNames = Object.keys(rows[0]);
|
|
13
13
|
const nFields = fieldNames.length;
|
|
14
|
-
const est = 100 + rows.length *
|
|
15
|
-
|
|
14
|
+
const est = 100 + rows.length * 160;
|
|
15
|
+
let buf = Buffer.allocUnsafe(est);
|
|
16
16
|
let off = 0;
|
|
17
|
+
const ensure = (n) => {
|
|
18
|
+
if (off + n > buf.length) {
|
|
19
|
+
const nb = Buffer.allocUnsafe(Math.max(buf.length * 2, off + n));
|
|
20
|
+
buf.copy(nb, 0, 0, off);
|
|
21
|
+
buf = nb;
|
|
22
|
+
}
|
|
23
|
+
};
|
|
17
24
|
|
|
18
25
|
off = buf.writeUInt8(nFields, off);
|
|
19
26
|
for (let fi = 0; fi < nFields; fi++) {
|
|
@@ -34,8 +41,9 @@ function encodeBatch(rows) {
|
|
|
34
41
|
if (Number.isInteger(v)) { if (v >= -2147483648 && v <= 2147483647) { off = buf.writeUInt8(INT32_TAG, off); off = buf.writeInt32LE(v, off); } else { off = buf.writeUInt8(INT64_TAG, off); off = buf.writeBigInt64LE(BigInt(v), off); } }
|
|
35
42
|
else { off = buf.writeUInt8(FLOAT_TAG, off); off = buf.writeDoubleLE(v, off); }
|
|
36
43
|
} else if (typeof v === 'string') {
|
|
37
|
-
off = buf.writeUInt8(STR_TAG, off);
|
|
38
44
|
const sl = Buffer.byteLength(v, 'utf8');
|
|
45
|
+
ensure(5 + sl);
|
|
46
|
+
off = buf.writeUInt8(STR_TAG, off);
|
|
39
47
|
off = buf.writeUInt32LE(sl, off);
|
|
40
48
|
off += buf.write(v, off, sl, 'utf8');
|
|
41
49
|
} else { off = buf.writeUInt8(BOOL_TAG, off); off = buf.writeUInt8(v ? 1 : 0, off); }
|
|
@@ -47,8 +55,9 @@ function encodeBatch(rows) {
|
|
|
47
55
|
if (Number.isInteger(v)) { if (v >= -2147483648 && v <= 2147483647) { off = buf.writeUInt8(INT32_TAG, off); off = buf.writeInt32LE(v, off); } else { off = buf.writeUInt8(INT64_TAG, off); off = buf.writeBigInt64LE(BigInt(v), off); } }
|
|
48
56
|
else { off = buf.writeUInt8(FLOAT_TAG, off); off = buf.writeDoubleLE(v, off); }
|
|
49
57
|
} else if (typeof v === 'string') {
|
|
50
|
-
off = buf.writeUInt8(STR_TAG, off);
|
|
51
58
|
const sl = Buffer.byteLength(v, 'utf8');
|
|
59
|
+
ensure(5 + sl);
|
|
60
|
+
off = buf.writeUInt8(STR_TAG, off);
|
|
52
61
|
off = buf.writeUInt32LE(sl, off);
|
|
53
62
|
off += buf.write(v, off, sl, 'utf8');
|
|
54
63
|
} else { off = buf.writeUInt8(BOOL_TAG, off); off = buf.writeUInt8(v ? 1 : 0, off); }
|
|
@@ -60,8 +69,9 @@ function encodeBatch(rows) {
|
|
|
60
69
|
if (Number.isInteger(v)) { if (v >= -2147483648 && v <= 2147483647) { off = buf.writeUInt8(INT32_TAG, off); off = buf.writeInt32LE(v, off); } else { off = buf.writeUInt8(INT64_TAG, off); off = buf.writeBigInt64LE(BigInt(v), off); } }
|
|
61
70
|
else { off = buf.writeUInt8(FLOAT_TAG, off); off = buf.writeDoubleLE(v, off); }
|
|
62
71
|
} else if (typeof v === 'string') {
|
|
63
|
-
off = buf.writeUInt8(STR_TAG, off);
|
|
64
72
|
const sl = Buffer.byteLength(v, 'utf8');
|
|
73
|
+
ensure(5 + sl);
|
|
74
|
+
off = buf.writeUInt8(STR_TAG, off);
|
|
65
75
|
off = buf.writeUInt32LE(sl, off);
|
|
66
76
|
off += buf.write(v, off, sl, 'utf8');
|
|
67
77
|
} else { off = buf.writeUInt8(BOOL_TAG, off); off = buf.writeUInt8(v ? 1 : 0, off); }
|
|
@@ -73,8 +83,9 @@ function encodeBatch(rows) {
|
|
|
73
83
|
if (Number.isInteger(v)) { if (v >= -2147483648 && v <= 2147483647) { off = buf.writeUInt8(INT32_TAG, off); off = buf.writeInt32LE(v, off); } else { off = buf.writeUInt8(INT64_TAG, off); off = buf.writeBigInt64LE(BigInt(v), off); } }
|
|
74
84
|
else { off = buf.writeUInt8(FLOAT_TAG, off); off = buf.writeDoubleLE(v, off); }
|
|
75
85
|
} else if (typeof v === 'string') {
|
|
76
|
-
off = buf.writeUInt8(STR_TAG, off);
|
|
77
86
|
const sl = Buffer.byteLength(v, 'utf8');
|
|
87
|
+
ensure(5 + sl);
|
|
88
|
+
off = buf.writeUInt8(STR_TAG, off);
|
|
78
89
|
off = buf.writeUInt32LE(sl, off);
|
|
79
90
|
off += buf.write(v, off, sl, 'utf8');
|
|
80
91
|
} else { off = buf.writeUInt8(BOOL_TAG, off); off = buf.writeUInt8(v ? 1 : 0, off); }
|
|
@@ -86,8 +97,9 @@ function encodeBatch(rows) {
|
|
|
86
97
|
if (Number.isInteger(v)) { if (v >= -2147483648 && v <= 2147483647) { off = buf.writeUInt8(INT32_TAG, off); off = buf.writeInt32LE(v, off); } else { off = buf.writeUInt8(INT64_TAG, off); off = buf.writeBigInt64LE(BigInt(v), off); } }
|
|
87
98
|
else { off = buf.writeUInt8(FLOAT_TAG, off); off = buf.writeDoubleLE(v, off); }
|
|
88
99
|
} else if (typeof v === 'string') {
|
|
89
|
-
off = buf.writeUInt8(STR_TAG, off);
|
|
90
100
|
const sl = Buffer.byteLength(v, 'utf8');
|
|
101
|
+
ensure(5 + sl);
|
|
102
|
+
off = buf.writeUInt8(STR_TAG, off);
|
|
91
103
|
off = buf.writeUInt32LE(sl, off);
|
|
92
104
|
off += buf.write(v, off, sl, 'utf8');
|
|
93
105
|
} else { off = buf.writeUInt8(BOOL_TAG, off); off = buf.writeUInt8(v ? 1 : 0, off); }
|
|
@@ -99,8 +111,9 @@ function encodeBatch(rows) {
|
|
|
99
111
|
if (Number.isInteger(v)) { if (v >= -2147483648 && v <= 2147483647) { off = buf.writeUInt8(INT32_TAG, off); off = buf.writeInt32LE(v, off); } else { off = buf.writeUInt8(INT64_TAG, off); off = buf.writeBigInt64LE(BigInt(v), off); } }
|
|
100
112
|
else { off = buf.writeUInt8(FLOAT_TAG, off); off = buf.writeDoubleLE(v, off); }
|
|
101
113
|
} else if (typeof v === 'string') {
|
|
102
|
-
off = buf.writeUInt8(STR_TAG, off);
|
|
103
114
|
const sl = Buffer.byteLength(v, 'utf8');
|
|
115
|
+
ensure(5 + sl);
|
|
116
|
+
off = buf.writeUInt8(STR_TAG, off);
|
|
104
117
|
off = buf.writeUInt32LE(sl, off);
|
|
105
118
|
off += buf.write(v, off, sl, 'utf8');
|
|
106
119
|
} else { off = buf.writeUInt8(BOOL_TAG, off); off = buf.writeUInt8(v ? 1 : 0, off); }
|
|
@@ -328,16 +341,11 @@ class JSQL {
|
|
|
328
341
|
if (!this._runHooks('beforeInsert', [table, filtered])) return [];
|
|
329
342
|
if (arr.length > 1) {
|
|
330
343
|
await this._flush();
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
if (!result) result = r;
|
|
337
|
-
}
|
|
338
|
-
this._emit('insert', { table, count: arr.length, ids: result });
|
|
339
|
-
this._runHooks('afterInsert', [table, filtered, result]);
|
|
340
|
-
return result;
|
|
344
|
+
const r = await this._insertBatch(table, arr);
|
|
345
|
+
if (r && r.error) throw new Error(r.error);
|
|
346
|
+
this._emit('insert', { table, count: arr.length, ids: r });
|
|
347
|
+
this._runHooks('afterInsert', [table, filtered, r]);
|
|
348
|
+
return r;
|
|
341
349
|
}
|
|
342
350
|
if (!this._buffer[table]) this._buffer[table] = [];
|
|
343
351
|
this._buffer[table].push(arr[0]);
|
package/lib/sql.js
CHANGED
|
@@ -678,7 +678,7 @@ function extractEqualPushdown(expr, schema) {
|
|
|
678
678
|
if (node.type === 'and') { walk(node.left); walk(node.right); return; }
|
|
679
679
|
if (node.type === 'compare' && node.op === '=') {
|
|
680
680
|
const col = node.left && node.left.type === 'column' ? node.left.name : null;
|
|
681
|
-
const val = node.right && node.right.type === 'literal' ? node.right.value : undefined;
|
|
681
|
+
const val = node.right && (node.right.type === 'literal' || node.right.type === 'value' || node.right.type === 'param') ? node.right.value : undefined;
|
|
682
682
|
if (col && val !== undefined && val !== null && schema[col] && !(schema[col].primaryKey && schema[col].autoIncrement === false)) {
|
|
683
683
|
filter[col] = val;
|
|
684
684
|
return;
|
|
@@ -728,9 +728,10 @@ function normalizeRow(row, schema) {
|
|
|
728
728
|
if (schema) {
|
|
729
729
|
const pkCols = Object.keys(schema).filter(k => schema[k].primaryKey);
|
|
730
730
|
for (const c of pkCols) {
|
|
731
|
-
|
|
731
|
+
const v = flat[c];
|
|
732
|
+
if (row.id !== undefined && (v === undefined || v === null || v === 0 || v === '')) flat[c] = row.id;
|
|
732
733
|
}
|
|
733
|
-
} else if (row.id !== undefined && flat.id === undefined) {
|
|
734
|
+
} else if (row.id !== undefined && (flat.id === undefined || flat.id === null || flat.id === 0 || flat.id === '')) {
|
|
734
735
|
flat.id = row.id;
|
|
735
736
|
}
|
|
736
737
|
flat._rid = row.id;
|
|
@@ -858,6 +859,11 @@ class SQLExecutor {
|
|
|
858
859
|
if (this.engine.hasTable && !this.engine.hasTable(statement.table)) {
|
|
859
860
|
throw new Error(`Table '${statement.table}' does not exist`);
|
|
860
861
|
}
|
|
862
|
+
if (statement.aggregate && statement.aggregate.type === 'COUNT' && !statement.where && !statement.distinct) {
|
|
863
|
+
const aggName = statement.aggregate.alias || 'COUNT(*)';
|
|
864
|
+
const n = this.engine.count ? await this.engine.count(statement.table) : 0;
|
|
865
|
+
return { ok: true, type: 'select', table: statement.table, columns: [aggName], rows: [[n]], aggregate: statement.aggregate };
|
|
866
|
+
}
|
|
861
867
|
schema = this.engine.getTableSchema
|
|
862
868
|
? await this.engine.getTableSchema(statement.table)
|
|
863
869
|
: (this.engine._schemas ? this.engine._schemas[statement.table] : null);
|
|
@@ -905,7 +911,13 @@ class SQLExecutor {
|
|
|
905
911
|
const rowsOnly = rows;
|
|
906
912
|
const aggName = agg.alias || (agg.type === 'COUNT' ? 'COUNT(*)' : agg.type + '(' + agg.column + ')');
|
|
907
913
|
if (agg.type === 'COUNT') {
|
|
908
|
-
|
|
914
|
+
let n;
|
|
915
|
+
if (!statement.where && !statement.distinct) {
|
|
916
|
+
n = this.engine.count ? await this.engine.count(statement.table) : rowsOnly.length;
|
|
917
|
+
} else {
|
|
918
|
+
n = rowsOnly.length;
|
|
919
|
+
}
|
|
920
|
+
return { ok: true, type: 'select', table: statement.table, columns: [aggName], rows: [[n]], aggregate: agg };
|
|
909
921
|
}
|
|
910
922
|
const values = rowsOnly.map(r => r[agg.column]).filter(v => v !== null && v !== undefined);
|
|
911
923
|
let value;
|
|
Binary file
|
|
@@ -235,7 +235,7 @@ impl Table {
|
|
|
235
235
|
|
|
236
236
|
if let Some(ref pk_key) = pk_auto_key {
|
|
237
237
|
match fields.get(pk_key) {
|
|
238
|
-
Some(v) if v.is_null() => {
|
|
238
|
+
Some(v) if v.is_null() || v.as_i64() == Some(0) => {
|
|
239
239
|
fields.insert(pk_key.clone(), serde_json::Value::Number(id.into()));
|
|
240
240
|
}
|
|
241
241
|
Some(v) => {
|
|
@@ -307,7 +307,7 @@ impl Table {
|
|
|
307
307
|
if let Some(pi) = pk_idx {
|
|
308
308
|
if pi < row_vals.len() {
|
|
309
309
|
match row_vals[pi] {
|
|
310
|
-
FieldValue::Null => row_vals[pi] = FieldValue::Int(id as i64),
|
|
310
|
+
FieldValue::Null | FieldValue::Int(0) => row_vals[pi] = FieldValue::Int(id as i64),
|
|
311
311
|
FieldValue::Int(e) if e > 0 => {
|
|
312
312
|
self.next_id = self.next_id.max((e + 1) as u64);
|
|
313
313
|
}
|
|
@@ -344,7 +344,13 @@ impl Table {
|
|
|
344
344
|
for (i, remapped_si) in remap.iter().enumerate() {
|
|
345
345
|
if let Some(si) = *remapped_si {
|
|
346
346
|
if si < row_vals.len() {
|
|
347
|
-
|
|
347
|
+
let v = std::mem::take(&mut row_vals[si]);
|
|
348
|
+
let is_auto_pk = pk_auto_key.is_some() && self.field_order[i] == *pk_auto_key.as_ref().unwrap();
|
|
349
|
+
if is_auto_pk && matches!(v, FieldValue::Null | FieldValue::Int(0)) {
|
|
350
|
+
self.values.push(FieldValue::Int(id as i64));
|
|
351
|
+
} else {
|
|
352
|
+
self.values.push(v);
|
|
353
|
+
}
|
|
348
354
|
continue;
|
|
349
355
|
}
|
|
350
356
|
}
|
|
Binary file
|