@gjsify/sqlite 0.3.15 → 0.3.17
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/esm/constants.js +1 -96
- package/lib/esm/data-model-reader.js +1 -78
- package/lib/esm/database-sync.js +1 -382
- package/lib/esm/errors.js +1 -64
- package/lib/esm/index.js +1 -5
- package/lib/esm/param-binding.js +1 -112
- package/lib/esm/statement-sync.js +1 -300
- package/package.json +7 -7
- package/tsconfig.tsbuildinfo +1 -1
package/lib/esm/param-binding.js
CHANGED
|
@@ -1,112 +1 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
//#region src/param-binding.ts
|
|
4
|
-
const MAX_INT64 = 9223372036854775807n;
|
|
5
|
-
const MIN_INT64 = -9223372036854775808n;
|
|
6
|
-
function validateBindValue(value, paramIndex) {
|
|
7
|
-
if (value === null || value === undefined) return;
|
|
8
|
-
const t = typeof value;
|
|
9
|
-
if (t === "number" || t === "bigint" || t === "string" || t === "boolean") return;
|
|
10
|
-
if (value instanceof Uint8Array || value instanceof ArrayBuffer) return;
|
|
11
|
-
if (ArrayBuffer.isView(value)) return;
|
|
12
|
-
throw new InvalidArgTypeError(`Provided value cannot be bound to SQLite parameter ${paramIndex}.`);
|
|
13
|
-
}
|
|
14
|
-
function setHolderValue(holder, value) {
|
|
15
|
-
if (value === null || value === undefined) {
|
|
16
|
-
holder.set_value(null);
|
|
17
|
-
return;
|
|
18
|
-
}
|
|
19
|
-
if (typeof value === "number") {
|
|
20
|
-
holder.set_value(value);
|
|
21
|
-
return;
|
|
22
|
-
}
|
|
23
|
-
if (typeof value === "bigint") {
|
|
24
|
-
if (value > MAX_INT64 || value < MIN_INT64) {
|
|
25
|
-
throw new InvalidArgValueError(`BigInt value is too large to bind.`);
|
|
26
|
-
}
|
|
27
|
-
holder.set_value(Number(value));
|
|
28
|
-
return;
|
|
29
|
-
}
|
|
30
|
-
if (typeof value === "string") {
|
|
31
|
-
holder.set_value(value);
|
|
32
|
-
return;
|
|
33
|
-
}
|
|
34
|
-
if (typeof value === "boolean") {
|
|
35
|
-
holder.set_value(value ? 1 : 0);
|
|
36
|
-
return;
|
|
37
|
-
}
|
|
38
|
-
if (value instanceof Uint8Array || ArrayBuffer.isView(value)) {
|
|
39
|
-
const bytes = value instanceof Uint8Array ? value : new Uint8Array(value.buffer);
|
|
40
|
-
holder.set_value(bytes);
|
|
41
|
-
return;
|
|
42
|
-
}
|
|
43
|
-
holder.set_value(value);
|
|
44
|
-
}
|
|
45
|
-
function bindParameters(paramSet, anonymousArgs, namedArgs, ctx) {
|
|
46
|
-
if (!paramSet) {
|
|
47
|
-
if (anonymousArgs.length > 0) {
|
|
48
|
-
throw new SqliteError("column index out of range", 25, "column index out of range");
|
|
49
|
-
}
|
|
50
|
-
return;
|
|
51
|
-
}
|
|
52
|
-
const holders = paramSet.get_holders();
|
|
53
|
-
if (namedArgs) {
|
|
54
|
-
const usedKeys = new Set();
|
|
55
|
-
for (const holder of holders) {
|
|
56
|
-
const id = holder.get_id();
|
|
57
|
-
let value = undefined;
|
|
58
|
-
let found = false;
|
|
59
|
-
if (id in namedArgs) {
|
|
60
|
-
value = namedArgs[id];
|
|
61
|
-
usedKeys.add(id);
|
|
62
|
-
found = true;
|
|
63
|
-
}
|
|
64
|
-
if (!found && ctx.allowBareNamedParameters) {
|
|
65
|
-
const bareName = id.replace(/^[\$:@]/, "");
|
|
66
|
-
if (bareName in namedArgs) {
|
|
67
|
-
value = namedArgs[bareName];
|
|
68
|
-
usedKeys.add(bareName);
|
|
69
|
-
found = true;
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
if (!found && !ctx.allowBareNamedParameters) {
|
|
73
|
-
const bareName = id.replace(/^[\$:@]/, "");
|
|
74
|
-
if (bareName in namedArgs) {
|
|
75
|
-
throw new InvalidStateError(`Unknown named parameter '${bareName}'`);
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
if (found) {
|
|
79
|
-
const paramIdx = holders.indexOf(holder) + 1;
|
|
80
|
-
validateBindValue(value, paramIdx);
|
|
81
|
-
setHolderValue(holder, value);
|
|
82
|
-
} else {
|
|
83
|
-
setHolderValue(holder, null);
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
if (!ctx.allowUnknownNamedParameters) {
|
|
87
|
-
for (const key of Object.keys(namedArgs)) {
|
|
88
|
-
if (!usedKeys.has(key)) {
|
|
89
|
-
const matchesHolder = holders.some((h) => {
|
|
90
|
-
const id = h.get_id();
|
|
91
|
-
return id === key || id.replace(/^[\$:@]/, "") === key;
|
|
92
|
-
});
|
|
93
|
-
if (!matchesHolder) {
|
|
94
|
-
throw new InvalidStateError(`Unknown named parameter '${key}'`);
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
} else {
|
|
100
|
-
if (anonymousArgs.length > holders.length) {
|
|
101
|
-
throw new SqliteError("column index out of range", 25, "column index out of range");
|
|
102
|
-
}
|
|
103
|
-
for (let i = 0; i < holders.length; i++) {
|
|
104
|
-
const value = i < anonymousArgs.length ? anonymousArgs[i] : undefined;
|
|
105
|
-
validateBindValue(value, i + 1);
|
|
106
|
-
setHolderValue(holders[i], value ?? null);
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
//#endregion
|
|
112
|
-
export { bindParameters };
|
|
1
|
+
import{InvalidArgTypeError as e,InvalidArgValueError as t,InvalidStateError as n,SqliteError as r}from"./errors.js";function i(t,n){if(t==null)return;let r=typeof t;if(!(r===`number`||r===`bigint`||r===`string`||r===`boolean`)&&!(t instanceof Uint8Array||t instanceof ArrayBuffer)&&!ArrayBuffer.isView(t))throw new e(`Provided value cannot be bound to SQLite parameter ${n}.`)}function a(e,n){if(n==null){e.set_value(null);return}if(typeof n==`number`){e.set_value(n);return}if(typeof n==`bigint`){if(n>9223372036854775807n||n<-9223372036854775808n)throw new t(`BigInt value is too large to bind.`);e.set_value(Number(n));return}if(typeof n==`string`){e.set_value(n);return}if(typeof n==`boolean`){e.set_value(+!!n);return}if(n instanceof Uint8Array||ArrayBuffer.isView(n)){let t=n instanceof Uint8Array?n:new Uint8Array(n.buffer);e.set_value(t);return}e.set_value(n)}function o(e,t,o,s){if(!e){if(t.length>0)throw new r(`column index out of range`,25,`column index out of range`);return}let c=e.get_holders();if(o){let e=new Set;for(let t of c){let r=t.get_id(),l,u=!1;if(r in o&&(l=o[r],e.add(r),u=!0),!u&&s.allowBareNamedParameters){let t=r.replace(/^[\$:@]/,``);t in o&&(l=o[t],e.add(t),u=!0)}if(!u&&!s.allowBareNamedParameters){let e=r.replace(/^[\$:@]/,``);if(e in o)throw new n(`Unknown named parameter '${e}'`)}if(u){let e=c.indexOf(t)+1;i(l,e),a(t,l)}else a(t,null)}if(!s.allowUnknownNamedParameters){for(let t of Object.keys(o))if(!e.has(t)&&!c.some(e=>{let n=e.get_id();return n===t||n.replace(/^[\$:@]/,``)===t}))throw new n(`Unknown named parameter '${t}'`)}}else{if(t.length>c.length)throw new r(`column index out of range`,25,`column index out of range`);for(let e=0;e<c.length;e++){let n=e<t.length?t[e]:void 0;i(n,e+1),a(c[e],n??null)}}}export{o as bindParameters};
|
|
@@ -1,300 +1 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { readAllRows, readFirstRow } from "./data-model-reader.js";
|
|
3
|
-
import Gda from "@girs/gda-6.0";
|
|
4
|
-
|
|
5
|
-
//#region src/statement-sync.ts
|
|
6
|
-
const MAX_INT64 = 9223372036854775807n;
|
|
7
|
-
const MIN_INT64 = -9223372036854775808n;
|
|
8
|
-
const INTERNAL = Symbol("StatementSync.internal");
|
|
9
|
-
function validateBindValue(value, paramIndex) {
|
|
10
|
-
if (value === null) return;
|
|
11
|
-
const t = typeof value;
|
|
12
|
-
if (t === "number" || t === "bigint" || t === "string" || t === "boolean") return;
|
|
13
|
-
if (value instanceof Uint8Array || value instanceof ArrayBuffer) return;
|
|
14
|
-
if (ArrayBuffer.isView(value)) return;
|
|
15
|
-
throw new InvalidArgTypeError(`Provided value cannot be bound to SQLite parameter ${paramIndex}.`);
|
|
16
|
-
}
|
|
17
|
-
function sqlEscapeValue(value) {
|
|
18
|
-
if (value === null) return "NULL";
|
|
19
|
-
if (value === undefined) return "NULL";
|
|
20
|
-
if (typeof value === "number") return String(value);
|
|
21
|
-
if (typeof value === "boolean") return value ? "1" : "0";
|
|
22
|
-
if (typeof value === "bigint") {
|
|
23
|
-
if (value > MAX_INT64 || value < MIN_INT64) {
|
|
24
|
-
throw new InvalidArgValueError("BigInt value is too large to bind.");
|
|
25
|
-
}
|
|
26
|
-
return String(value);
|
|
27
|
-
}
|
|
28
|
-
if (typeof value === "string") return "'" + value.replace(/'/g, "''") + "'";
|
|
29
|
-
if (value instanceof Uint8Array) {
|
|
30
|
-
let hex = "";
|
|
31
|
-
for (let i = 0; i < value.length; i++) {
|
|
32
|
-
hex += value[i].toString(16).padStart(2, "0");
|
|
33
|
-
}
|
|
34
|
-
return "X'" + hex + "'";
|
|
35
|
-
}
|
|
36
|
-
if (ArrayBuffer.isView(value)) {
|
|
37
|
-
return sqlEscapeValue(new Uint8Array(value.buffer));
|
|
38
|
-
}
|
|
39
|
-
return "NULL";
|
|
40
|
-
}
|
|
41
|
-
var StatementSync = class StatementSync {
|
|
42
|
-
#connection;
|
|
43
|
-
#sql;
|
|
44
|
-
#paramMap;
|
|
45
|
-
#readBigInts;
|
|
46
|
-
#returnArrays;
|
|
47
|
-
#allowBareNamedParameters;
|
|
48
|
-
#allowUnknownNamedParameters;
|
|
49
|
-
#parser;
|
|
50
|
-
constructor(sentinel, connection, sql, options, paramMap, parser) {
|
|
51
|
-
if (sentinel !== INTERNAL) {
|
|
52
|
-
throw new IllegalConstructorError();
|
|
53
|
-
}
|
|
54
|
-
this.#connection = connection;
|
|
55
|
-
this.#sql = sql;
|
|
56
|
-
this.#paramMap = paramMap;
|
|
57
|
-
this.#readBigInts = options.readBigInts ?? false;
|
|
58
|
-
this.#returnArrays = options.returnArrays ?? false;
|
|
59
|
-
this.#allowBareNamedParameters = options.allowBareNamedParameters ?? true;
|
|
60
|
-
this.#allowUnknownNamedParameters = options.allowUnknownNamedParameters ?? false;
|
|
61
|
-
this.#parser = parser;
|
|
62
|
-
}
|
|
63
|
-
/** @internal */
|
|
64
|
-
static _create(connection, sql, options, paramMap, parser) {
|
|
65
|
-
return new StatementSync(INTERNAL, connection, sql, options, paramMap, parser);
|
|
66
|
-
}
|
|
67
|
-
get sourceSQL() {
|
|
68
|
-
return this.#sql;
|
|
69
|
-
}
|
|
70
|
-
get expandedSQL() {
|
|
71
|
-
return this.#sql;
|
|
72
|
-
}
|
|
73
|
-
#getReadOptions() {
|
|
74
|
-
return {
|
|
75
|
-
readBigInts: this.#readBigInts,
|
|
76
|
-
returnArrays: this.#returnArrays
|
|
77
|
-
};
|
|
78
|
-
}
|
|
79
|
-
#buildSql(args) {
|
|
80
|
-
if (this.#paramMap.length === 0) {
|
|
81
|
-
if (args.length > 0) {
|
|
82
|
-
const hasNamedArg = args.length > 0 && args[0] !== null && typeof args[0] === "object" && !(args[0] instanceof Uint8Array) && !ArrayBuffer.isView(args[0]);
|
|
83
|
-
if (!hasNamedArg) {
|
|
84
|
-
throw new SqliteError("column index out of range", 25, "column index out of range");
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
return this.#sql;
|
|
88
|
-
}
|
|
89
|
-
let namedArgs = null;
|
|
90
|
-
let positionalArgs = args;
|
|
91
|
-
if (args.length > 0 && args[0] !== null && typeof args[0] === "object" && !(args[0] instanceof Uint8Array) && !ArrayBuffer.isView(args[0])) {
|
|
92
|
-
namedArgs = args[0];
|
|
93
|
-
positionalArgs = args.slice(1);
|
|
94
|
-
}
|
|
95
|
-
const values = new Map();
|
|
96
|
-
const positionalValues = {};
|
|
97
|
-
if (namedArgs) {
|
|
98
|
-
for (const param of this.#paramMap) {
|
|
99
|
-
if (param.position >= 0) continue;
|
|
100
|
-
const origName = param.originalName;
|
|
101
|
-
let value = undefined;
|
|
102
|
-
let found = false;
|
|
103
|
-
if (origName in namedArgs) {
|
|
104
|
-
value = namedArgs[origName];
|
|
105
|
-
found = true;
|
|
106
|
-
}
|
|
107
|
-
if (!found && this.#allowBareNamedParameters) {
|
|
108
|
-
const bareName = origName.replace(/^[\$:@]/, "");
|
|
109
|
-
if (bareName in namedArgs) {
|
|
110
|
-
value = namedArgs[bareName];
|
|
111
|
-
found = true;
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
if (!found && !this.#allowBareNamedParameters) {
|
|
115
|
-
const bareName = origName.replace(/^[\$:@]/, "");
|
|
116
|
-
if (bareName in namedArgs) {
|
|
117
|
-
throw new SqliteError(`Unknown named parameter '${bareName}'`, 0, `Unknown named parameter '${bareName}'`);
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
if (found) {
|
|
121
|
-
validateBindValue(value, this.#paramMap.indexOf(param) + 1);
|
|
122
|
-
values.set(param.originalName, sqlEscapeValue(value));
|
|
123
|
-
} else {
|
|
124
|
-
values.set(param.originalName, "NULL");
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
const positionalParams = this.#paramMap.filter((p) => p.position >= 0);
|
|
128
|
-
for (let i = 0; i < positionalArgs.length; i++) {
|
|
129
|
-
if (i >= positionalParams.length) {
|
|
130
|
-
throw new SqliteError("column index out of range", 25, "column index out of range");
|
|
131
|
-
}
|
|
132
|
-
validateBindValue(positionalArgs[i], i + 1);
|
|
133
|
-
positionalValues[positionalParams[i].position] = sqlEscapeValue(positionalArgs[i]);
|
|
134
|
-
}
|
|
135
|
-
} else {
|
|
136
|
-
const positionalParams = this.#paramMap.filter((p) => p.position >= 0);
|
|
137
|
-
if (positionalArgs.length > positionalParams.length && positionalParams.length > 0) {
|
|
138
|
-
throw new SqliteError("column index out of range", 25, "column index out of range");
|
|
139
|
-
}
|
|
140
|
-
for (let i = 0; i < positionalParams.length; i++) {
|
|
141
|
-
if (i < positionalArgs.length) {
|
|
142
|
-
validateBindValue(positionalArgs[i], i + 1);
|
|
143
|
-
positionalValues[positionalParams[i].position] = sqlEscapeValue(positionalArgs[i]);
|
|
144
|
-
} else {
|
|
145
|
-
positionalValues[positionalParams[i].position] = "NULL";
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
let result = this.#sql;
|
|
150
|
-
const namedParams = this.#paramMap.filter((p) => p.position < 0).sort((a, b) => b.originalName.length - a.originalName.length);
|
|
151
|
-
for (const param of namedParams) {
|
|
152
|
-
const escaped = values.get(param.originalName) ?? "NULL";
|
|
153
|
-
result = result.split(param.originalName).join(escaped);
|
|
154
|
-
}
|
|
155
|
-
if (Object.keys(positionalValues).length > 0) {
|
|
156
|
-
let out = "";
|
|
157
|
-
let pIdx = 0;
|
|
158
|
-
let i = 0;
|
|
159
|
-
while (i < result.length) {
|
|
160
|
-
if (result[i] === "'") {
|
|
161
|
-
const start = i;
|
|
162
|
-
i++;
|
|
163
|
-
while (i < result.length && result[i] !== "'") {
|
|
164
|
-
if (result[i] === "'" && result[i + 1] === "'") {
|
|
165
|
-
i += 2;
|
|
166
|
-
continue;
|
|
167
|
-
}
|
|
168
|
-
i++;
|
|
169
|
-
}
|
|
170
|
-
if (i < result.length) i++;
|
|
171
|
-
out += result.substring(start, i);
|
|
172
|
-
continue;
|
|
173
|
-
}
|
|
174
|
-
if (result[i] === "?") {
|
|
175
|
-
i++;
|
|
176
|
-
let numStr = "";
|
|
177
|
-
while (i < result.length && result[i] >= "0" && result[i] <= "9") {
|
|
178
|
-
numStr += result[i];
|
|
179
|
-
i++;
|
|
180
|
-
}
|
|
181
|
-
const pos = numStr ? parseInt(numStr, 10) - 1 : pIdx;
|
|
182
|
-
pIdx = numStr ? pIdx : pIdx + 1;
|
|
183
|
-
out += pos in positionalValues ? positionalValues[pos] : "NULL";
|
|
184
|
-
continue;
|
|
185
|
-
}
|
|
186
|
-
out += result[i];
|
|
187
|
-
i++;
|
|
188
|
-
}
|
|
189
|
-
result = out;
|
|
190
|
-
}
|
|
191
|
-
return result;
|
|
192
|
-
}
|
|
193
|
-
#executeSql(sql) {
|
|
194
|
-
const [stmt] = this.#parser.parse_string(sql);
|
|
195
|
-
if (!stmt) {
|
|
196
|
-
throw new SqliteError("Failed to parse SQL statement");
|
|
197
|
-
}
|
|
198
|
-
const stmtType = stmt.get_statement_type();
|
|
199
|
-
if (stmtType === Gda.SqlStatementType.SELECT) {
|
|
200
|
-
return {
|
|
201
|
-
model: this.#connection.statement_execute_select(stmt, null),
|
|
202
|
-
isSelect: true
|
|
203
|
-
};
|
|
204
|
-
}
|
|
205
|
-
try {
|
|
206
|
-
this.#connection.statement_execute_non_select(stmt, null);
|
|
207
|
-
return {
|
|
208
|
-
model: null,
|
|
209
|
-
isSelect: false
|
|
210
|
-
};
|
|
211
|
-
} catch {
|
|
212
|
-
const model = this.#connection.statement_execute_select(stmt, null);
|
|
213
|
-
return {
|
|
214
|
-
model,
|
|
215
|
-
isSelect: true
|
|
216
|
-
};
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
run(...args) {
|
|
220
|
-
const sql = this.#buildSql(args);
|
|
221
|
-
this.#executeSql(sql);
|
|
222
|
-
let changes = 0;
|
|
223
|
-
let lastInsertRowid = 0;
|
|
224
|
-
try {
|
|
225
|
-
const chModel = this.#connection.execute_select_command("SELECT changes()");
|
|
226
|
-
if (chModel && chModel.get_n_rows() > 0) {
|
|
227
|
-
changes = chModel.get_value_at(0, 0);
|
|
228
|
-
}
|
|
229
|
-
} catch {}
|
|
230
|
-
try {
|
|
231
|
-
const ridModel = this.#connection.execute_select_command("SELECT last_insert_rowid()");
|
|
232
|
-
if (ridModel && ridModel.get_n_rows() > 0) {
|
|
233
|
-
lastInsertRowid = ridModel.get_value_at(0, 0);
|
|
234
|
-
}
|
|
235
|
-
} catch {}
|
|
236
|
-
if (this.#readBigInts) {
|
|
237
|
-
changes = BigInt(changes);
|
|
238
|
-
lastInsertRowid = BigInt(lastInsertRowid);
|
|
239
|
-
}
|
|
240
|
-
return {
|
|
241
|
-
changes,
|
|
242
|
-
lastInsertRowid
|
|
243
|
-
};
|
|
244
|
-
}
|
|
245
|
-
get(...args) {
|
|
246
|
-
const sql = this.#buildSql(args);
|
|
247
|
-
try {
|
|
248
|
-
const { model } = this.#executeSql(sql);
|
|
249
|
-
if (!model || model.get_n_rows() === 0) {
|
|
250
|
-
return undefined;
|
|
251
|
-
}
|
|
252
|
-
return readFirstRow(model, this.#getReadOptions());
|
|
253
|
-
} catch {
|
|
254
|
-
return undefined;
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
all(...args) {
|
|
258
|
-
const sql = this.#buildSql(args);
|
|
259
|
-
try {
|
|
260
|
-
const { model } = this.#executeSql(sql);
|
|
261
|
-
if (!model) {
|
|
262
|
-
return [];
|
|
263
|
-
}
|
|
264
|
-
return readAllRows(model, this.#getReadOptions());
|
|
265
|
-
} catch {
|
|
266
|
-
return [];
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
setReadBigInts(enabled) {
|
|
270
|
-
if (typeof enabled !== "boolean") {
|
|
271
|
-
throw new InvalidArgTypeError("The \"readBigInts\" argument must be a boolean.");
|
|
272
|
-
}
|
|
273
|
-
this.#readBigInts = enabled;
|
|
274
|
-
return undefined;
|
|
275
|
-
}
|
|
276
|
-
setReturnArrays(enabled) {
|
|
277
|
-
if (typeof enabled !== "boolean") {
|
|
278
|
-
throw new InvalidArgTypeError("The \"returnArrays\" argument must be a boolean.");
|
|
279
|
-
}
|
|
280
|
-
this.#returnArrays = enabled;
|
|
281
|
-
return undefined;
|
|
282
|
-
}
|
|
283
|
-
setAllowBareNamedParameters(enabled) {
|
|
284
|
-
if (typeof enabled !== "boolean") {
|
|
285
|
-
throw new InvalidArgTypeError("The \"allowBareNamedParameters\" argument must be a boolean.");
|
|
286
|
-
}
|
|
287
|
-
this.#allowBareNamedParameters = enabled;
|
|
288
|
-
return undefined;
|
|
289
|
-
}
|
|
290
|
-
setAllowUnknownNamedParameters(enabled) {
|
|
291
|
-
if (typeof enabled !== "boolean") {
|
|
292
|
-
throw new InvalidArgTypeError("The \"allowUnknownNamedParameters\" argument must be a boolean.");
|
|
293
|
-
}
|
|
294
|
-
this.#allowUnknownNamedParameters = enabled;
|
|
295
|
-
return undefined;
|
|
296
|
-
}
|
|
297
|
-
};
|
|
298
|
-
|
|
299
|
-
//#endregion
|
|
300
|
-
export { StatementSync };
|
|
1
|
+
import{IllegalConstructorError as e,InvalidArgTypeError as t,InvalidArgValueError as n,SqliteError as r}from"./errors.js";import{readAllRows as i,readFirstRow as a}from"./data-model-reader.js";import o from"@girs/gda-6.0";const s=Symbol(`StatementSync.internal`);function c(e,n){if(e===null)return;let r=typeof e;if(!(r===`number`||r===`bigint`||r===`string`||r===`boolean`)&&!(e instanceof Uint8Array||e instanceof ArrayBuffer)&&!ArrayBuffer.isView(e))throw new t(`Provided value cannot be bound to SQLite parameter ${n}.`)}function l(e){if(e==null)return`NULL`;if(typeof e==`number`)return String(e);if(typeof e==`boolean`)return e?`1`:`0`;if(typeof e==`bigint`){if(e>9223372036854775807n||e<-9223372036854775808n)throw new n(`BigInt value is too large to bind.`);return String(e)}if(typeof e==`string`)return`'`+e.replace(/'/g,`''`)+`'`;if(e instanceof Uint8Array){let t=``;for(let n=0;n<e.length;n++)t+=e[n].toString(16).padStart(2,`0`);return`X'`+t+`'`}return ArrayBuffer.isView(e)?l(new Uint8Array(e.buffer)):`NULL`}var u=class n{#e;#t;#n;#r;#i;#a;#o;#s;constructor(t,n,r,i,a,o){if(t!==s)throw new e;this.#e=n,this.#t=r,this.#n=a,this.#r=i.readBigInts??!1,this.#i=i.returnArrays??!1,this.#a=i.allowBareNamedParameters??!0,this.#o=i.allowUnknownNamedParameters??!1,this.#s=o}static _create(e,t,r,i,a){return new n(s,e,t,r,i,a)}get sourceSQL(){return this.#t}get expandedSQL(){return this.#t}#c(){return{readBigInts:this.#r,returnArrays:this.#i}}#l(e){if(this.#n.length===0){if(e.length>0&&!(e.length>0&&e[0]!==null&&typeof e[0]==`object`&&!(e[0]instanceof Uint8Array)&&!ArrayBuffer.isView(e[0])))throw new r(`column index out of range`,25,`column index out of range`);return this.#t}let t=null,n=e;e.length>0&&e[0]!==null&&typeof e[0]==`object`&&!(e[0]instanceof Uint8Array)&&!ArrayBuffer.isView(e[0])&&(t=e[0],n=e.slice(1));let i=new Map,a={};if(t){for(let e of this.#n){if(e.position>=0)continue;let n=e.originalName,a,o=!1;if(n in t&&(a=t[n],o=!0),!o&&this.#a){let e=n.replace(/^[\$:@]/,``);e in t&&(a=t[e],o=!0)}if(!o&&!this.#a){let e=n.replace(/^[\$:@]/,``);if(e in t)throw new r(`Unknown named parameter '${e}'`,0,`Unknown named parameter '${e}'`)}o?(c(a,this.#n.indexOf(e)+1),i.set(e.originalName,l(a))):i.set(e.originalName,`NULL`)}let e=this.#n.filter(e=>e.position>=0);for(let t=0;t<n.length;t++){if(t>=e.length)throw new r(`column index out of range`,25,`column index out of range`);c(n[t],t+1),a[e[t].position]=l(n[t])}}else{let e=this.#n.filter(e=>e.position>=0);if(n.length>e.length&&e.length>0)throw new r(`column index out of range`,25,`column index out of range`);for(let t=0;t<e.length;t++)t<n.length?(c(n[t],t+1),a[e[t].position]=l(n[t])):a[e[t].position]=`NULL`}let o=this.#t,s=this.#n.filter(e=>e.position<0).sort((e,t)=>t.originalName.length-e.originalName.length);for(let e of s){let t=i.get(e.originalName)??`NULL`;o=o.split(e.originalName).join(t)}if(Object.keys(a).length>0){let e=``,t=0,n=0;for(;n<o.length;){if(o[n]===`'`){let t=n;for(n++;n<o.length&&o[n]!==`'`;){if(o[n]===`'`&&o[n+1]===`'`){n+=2;continue}n++}n<o.length&&n++,e+=o.substring(t,n);continue}if(o[n]===`?`){n++;let r=``;for(;n<o.length&&o[n]>=`0`&&o[n]<=`9`;)r+=o[n],n++;let i=r?parseInt(r,10)-1:t;t=r?t:t+1,e+=i in a?a[i]:`NULL`;continue}e+=o[n],n++}o=e}return o}#u(e){let[t]=this.#s.parse_string(e);if(!t)throw new r(`Failed to parse SQL statement`);if(t.get_statement_type()===o.SqlStatementType.SELECT)return{model:this.#e.statement_execute_select(t,null),isSelect:!0};try{return this.#e.statement_execute_non_select(t,null),{model:null,isSelect:!1}}catch{return{model:this.#e.statement_execute_select(t,null),isSelect:!0}}}run(...e){let t=this.#l(e);this.#u(t);let n=0,r=0;try{let e=this.#e.execute_select_command(`SELECT changes()`);e&&e.get_n_rows()>0&&(n=e.get_value_at(0,0))}catch{}try{let e=this.#e.execute_select_command(`SELECT last_insert_rowid()`);e&&e.get_n_rows()>0&&(r=e.get_value_at(0,0))}catch{}return this.#r&&(n=BigInt(n),r=BigInt(r)),{changes:n,lastInsertRowid:r}}get(...e){let t=this.#l(e);try{let{model:e}=this.#u(t);return!e||e.get_n_rows()===0?void 0:a(e,this.#c())}catch{return}}all(...e){let t=this.#l(e);try{let{model:e}=this.#u(t);return e?i(e,this.#c()):[]}catch{return[]}}setReadBigInts(e){if(typeof e!=`boolean`)throw new t(`The "readBigInts" argument must be a boolean.`);this.#r=e}setReturnArrays(e){if(typeof e!=`boolean`)throw new t(`The "returnArrays" argument must be a boolean.`);this.#i=e}setAllowBareNamedParameters(e){if(typeof e!=`boolean`)throw new t(`The "allowBareNamedParameters" argument must be a boolean.`);this.#a=e}setAllowUnknownNamedParameters(e){if(typeof e!=`boolean`)throw new t(`The "allowUnknownNamedParameters" argument must be a boolean.`);this.#o=e}};export{u as StatementSync};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gjsify/sqlite",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.17",
|
|
4
4
|
"description": "Node.js sqlite module for Gjs using libgda",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "lib/esm/index.js",
|
|
@@ -31,14 +31,14 @@
|
|
|
31
31
|
"libgda"
|
|
32
32
|
],
|
|
33
33
|
"devDependencies": {
|
|
34
|
-
"@gjsify/cli": "^0.3.
|
|
35
|
-
"@gjsify/unit": "^0.3.
|
|
36
|
-
"@types/node": "^25.6.
|
|
34
|
+
"@gjsify/cli": "^0.3.17",
|
|
35
|
+
"@gjsify/unit": "^0.3.17",
|
|
36
|
+
"@types/node": "^25.6.2",
|
|
37
37
|
"typescript": "^6.0.3"
|
|
38
38
|
},
|
|
39
39
|
"dependencies": {
|
|
40
|
-
"@girs/gda-6.0": "6.0.0-4.0.0-rc.
|
|
41
|
-
"@girs/glib-2.0": "2.88.0-4.0.0-rc.
|
|
42
|
-
"@girs/gobject-2.0": "2.88.0-4.0.0-rc.
|
|
40
|
+
"@girs/gda-6.0": "6.0.0-4.0.0-rc.14",
|
|
41
|
+
"@girs/glib-2.0": "2.88.0-4.0.0-rc.14",
|
|
42
|
+
"@girs/gobject-2.0": "2.88.0-4.0.0-rc.14"
|
|
43
43
|
}
|
|
44
44
|
}
|