-
Notifications
You must be signed in to change notification settings - Fork 2
/
sqlite3-diesel.js
330 lines (278 loc) · 7.11 KB
/
sqlite3-diesel.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
import sqlite3InitModule from "@sqlite.org/sqlite-wasm";
const log = console.log;
const err_log = console.error;
export class SQLiteError extends Error {
constructor(message, code) {
super(message);
this.code = code;
}
}
export class SQLite {
#module;
#sqlite3;
constructor(sqlite3) {
if (typeof sqlite3 === "undefined") {
throw new Error(
"`sqliteObject` must be defined before calling constructor",
);
}
this.sqlite3 = sqlite3;
}
static async init_module(opts) {
return await sqlite3InitModule({
print: log,
printErr: err_log,
...opts,
});
}
version() {
return this.sqlite3.version;
}
filename(db, name) {
return this.sqlite3.capi.sqlite3_db_filename(db, name);
}
extended_errcode(connection) {
return this.sqlite3.capi.sqlite3_extended_errcode(connection);
}
errstr(code) {
return this.sqlite3.capi.sqlite3_errstr(code);
}
errmsg(connection) {
return this.sqlite3.capi.sqlite3_errmsg(connection);
}
result_js(context, value) {
return this.sqlite3.capi.sqlite3_result_js(context, value);
}
result_text(context, value) {
return this.sqlite3.capi.sqlite3_result_text(context, value);
}
result_int(context, value) {
return this.sqlite3.capi.sqlite3_result_int(context, value);
}
result_int64(context, value) {
return this.sqlite3.capi.sqlite3_result_int64(context, value);
}
result_double(context, value) {
return this.sqlite3.capi.sqlite3_result_double(context, value);
}
result_blob(context, value) {
return this.sqlite3.capi.sqlite3_result_blob(context, value);
}
result_null(context) {
return this.sqlite3.capi.sqlite3_result_null(context);
}
bind_blob(stmt, i, value, len, flags) {
return this.sqlite3.capi.sqlite3_bind_blob(stmt, i, value, len, flags);
}
bind_text(stmt, i, value, len, flags) {
return this.sqlite3.capi.sqlite3_bind_text(stmt, i, value, len, flags);
}
bind_double(stmt, i, value) {
return this.sqlite3.capi.sqlite3_bind_double(stmt, i, value);
}
bind_int(stmt, i, value) {
return this.sqlite3.capi.sqlite3_bind_int(stmt, i, value);
}
bind_int64(stmt, i, value) {
return this.sqlite3.capi.sqlite3_bind_int64(stmt, i, value);
}
bind_null(stmt, i) {
this.sqlite3.capi.sqlite3_bind_null(stmt, i);
/// There's no way bind_null can fail.
return this.sqlite3.capi.SQLITE_OK;
}
bind_parameter_count(stmt) {
return this.sqlite3.capi.sqlite3_bind_parameter_count(stmt);
}
bind_parameter_name(stmt, i) {
return this.sqlite3.capi.sqlite3_bind_paramater_name(stmt, it);
}
value_dup(pValue) {
return this.sqlite3.capi.sqlite3_value_dup(pValue);
}
value_blob(pValue) {
return this.sqlite3.capi.sqlite3_value_blob(pValue);
}
value_bytes(pValue) {
return this.sqlite3.capi.sqlite3_value_bytes(pValue);
}
value_double(pValue) {
return this.sqlite3.capi.sqlite3_value_double(pValue);
}
value_int(pValue) {
return this.sqlite3.capi.sqlite3_value_int(pValue);
}
value_int64(pValue) {
return this.sqlite3.capi.sqlite3_value_int64(pValue);
}
value_text(pValue) {
return this.sqlite3.capi.sqlite3_value_text(pValue);
}
value_type(pValue) {
return this.sqlite3.capi.sqlite3_value_type(pValue);
}
open(database_url, iflags) {
try {
return new this.sqlite3.oo1.OpfsDb(database_url);
} catch (error) {
console.log("OPFS open error", error);
throw error;
}
}
exec(db, query) {
try {
return db.exec(query, {
callback: (row) => {},
});
} catch (error) {
throw error;
}
}
finalize(stmt) {
return this.sqlite3.capi.sqlite3_finalize(stmt);
}
changes(db) {
return this.sqlite3.capi.sqlite3_changes(db);
}
clear_bindings(stmt) {
return this.sqlite3.capi.sqlite3_clear_bindings(stmt);
}
reset(stmt) {
return this.sqlite3.capi.sqlite3_reset(stmt);
}
close(db) {
return this.sqlite3.capi.sqlite3_close_v2(db.pointer);
}
db_handle(stmt) {
return this.sqlite3.capi.sqlite3_db_handle(stmt);
}
prepare_v3(db, sql, nByte, prepFlags, ppStmt, pzTail) {
return this.sqlite3.capi.sqlite3_prepare_v3(
db.pointer,
sql,
nByte,
prepFlags,
ppStmt,
pzTail,
);
}
step(stmt) {
return this.sqlite3.capi.sqlite3_step(stmt);
}
column_value(stmt, i) {
return this.sqlite3.capi.sqlite3_column_value(stmt, i);
}
column_name(stmt, idx) {
return this.sqlite3.capi.sqlite3_column_name(stmt, idx);
}
column_count(stmt) {
return this.sqlite3.capi.sqlite3_column_count(stmt);
}
create_function(
database,
functionName,
nArg,
textRep,
pApp,
xFunc,
xStep,
xFinal,
) {
try {
this.sqlite3.capi.sqlite3_create_function(
database,
functionName,
nArg,
textRep,
pApp, // pApp is ignored
xFunc,
xStep,
xFinal,
);
console.log("create function");
} catch (error) {
console.log("create function err");
throw error;
}
}
//TODO: At some point need a way to register functions from rust
//but for now this is fine.
register_diesel_sql_functions(database) {
try {
this.sqlite3.capi.sqlite3_create_function(
database,
"diesel_manage_updated_at",
1,
this.sqlite3.capi.SQLITE_UTF8,
0,
async (context, values) => {
const table_name = this.sqlite3.value_text(values[0]);
database.exec(
context,
`CREATE TRIGGER __diesel_manage_updated_at_${table_name}
AFTER UPDATE ON ${table_name}
FOR EACH ROW WHEN
old.updated_at IS NULL AND
new.updated_at IS NULL OR
old.updated_at == new.updated_at
BEGIN
UPDATE ${table_name}
SET updated_at = CURRENT_TIMESTAMP
WHERE ROWID = new.ROWID;
END`,
(row) => {
log(`------------------------------------`);
log(`Created trigger for ${table_name}`);
log(row);
log(`------------------------------------`);
},
);
},
);
} catch (error) {
console.log("error creating diesel trigger");
throw error;
}
}
value_free(value) {
return this.sqlite3.capi.sqlite3_value_free(value);
}
sqlite3_serialize(database, z_schema, p_size, m_flags) {
try {
return this.sqlite3.capi.sqlite3_serialize(
database,
z_schema,
p_size,
m_flags,
);
} catch (error) {
console.log("error serializing");
throw error;
}
}
sqlite3_deserialize(
database,
z_schema,
p_data,
sz_database,
sz_buffer,
m_flags,
) {
try {
return this.sqlite3.capi.sqlite3_deserialize(
database,
z_schema,
p_data,
sz_database,
sz_buffer,
m_flags,
);
} catch (error) {
console.log("error deserializing");
throw error;
}
}
sqlite3_free(_database, arg1) {
return this.sqlite3.capi.sqlite3_free(arg1);
}
}