Repository navigation
Expand file tree
/
Copy pathclient-sqlite.js
More file actions
880 lines (700 loc) · 20.3 KB
/
Copy pathclient-sqlite.js
File metadata and controls
880 lines (700 loc) · 20.3 KB
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
// Total.js Module: SQLite client
// A tiny dependency-free client for SQLite, it uses the built-in Node.js module "node:sqlite"
// The API is the same as the API of the PostgreSQL client (client-postgresql.js)
//
// const SQLite = require('./client-sqlite');
//
// A single connection:
// SQLite.connect('sqlite:///www/app/databases/app.db?wal=1', function(err, client) {
// client.query('SELECT * FROM tbl_user WHERE id=$1', ['123'], function(err, response) {
// console.log(response.rows, response.rowCount);
// client.close();
// });
// });
//
// Pooling:
// var pool = SQLite.pool('sqlite:///www/app/databases/app.db?wal=1', 10);
// pool.connect(function(err, client, release) {
// client.query('SELECT 1 AS value', function(err, response) {
// release(); // or client.release() or client.close()
// });
// });
//
// Connection string:
// "sqlite:///absolute/path/app.db", "sqlite://relative/path/app.db", "sqlite://:memory:", ":memory:" or a filename
// ?wal=1 it switches the database to the WAL journal mode
// ?readonly=1 it opens the database for reading only
// ?foreignkeys=0 it disables foreign key constraints (enabled by default)
// ?timeout=5000 how long (in ms) a query waits for a locked database (default: 5000)
//
// Parameters:
// "$1, $2" or "?" with an array, ":name", "@name" or "$name" with an object
//
// IMPORTANT: "node:sqlite" is synchronous, so a query blocks the process until it is finished.
// Cache of parsed connection strings and SQL statements
const OPTIONS = new Map();
const PARSED = new Map();
const REG_DATE = /^\d{4}-\d\d-\d\d[ T]\d\d:\d\d(:\d\d(\.\d+)?)?$/;
const REG_SIZE = /\(.*$/;
const BIGMAX = BigInt(Number.MAX_SAFE_INTEGER);
const BIGMIN = BigInt(Number.MIN_SAFE_INTEGER);
const EMPTY = [];
// Commands that change the schema: the prepared statements are released
const SCHEMA = { CREATE: 1, ALTER: 1, DROP: 1, ATTACH: 1, DETACH: 1, VACUUM: 1, PRAGMA: 1, REINDEX: 1 };
// Commands that return the count of changed rows
const CHANGES = { INSERT: 1, UPDATE: 1, DELETE: 1, REPLACE: 1, WITH: 1 };
var Sqlite = null;
// ============================================================================
// Types: declared type of the column (uppercase) -> parser(value)
// ============================================================================
const TYPES = {};
function parsebool(val) {
return typeof(val) === 'string' ? (val === '1' || val === 'true' || val === 't') : !!val;
}
function jsondateparser(key, value) {
return typeof(value) === 'string' && value.isJSONDate() ? new Date(value) : value;
}
function parsejson(val) {
if (typeof(val) === 'string') {
try {
return JSON.parse(val, jsondateparser);
} catch (e) {}
}
return val;
}
function parsedate(val) {
var dt;
if (typeof(val) === 'number') {
// Unix time in seconds (a value lower than 100000000000) or in milliseconds
dt = new Date(val < 1e11 && val > -1e11 ? (val * 1000) : val);
} else if (typeof(val) === 'string') {
// SQLite keeps "CURRENT_TIMESTAMP" and "datetime()" in UTC without a time zone
dt = new Date(REG_DATE.test(val) ? (val.replace(' ', 'T') + 'Z') : val);
} else
return val;
return isNaN(dt.getTime()) ? val : dt;
}
TYPES.BOOLEAN = parsebool;
TYPES.BOOL = parsebool;
TYPES.DATE = parsedate;
TYPES.DATETIME = parsedate;
TYPES.TIMESTAMP = parsedate;
TYPES.TIMESTAMPTZ = parsedate;
TYPES.JSON = parsejson;
TYPES.JSONB = parsejson;
// NOTE: an integer that doesn't fit into the JS number is returned as a string (like "bigint" in the PostgreSQL client)
// A parser can be changed via: require('./client-sqlite').types.DATE = val => val;
function tonumber(val) {
return val <= BIGMAX && val >= BIGMIN ? Number(val) : val.toString();
}
// ============================================================================
// Serialization of parameters
// ============================================================================
function param(val) {
switch (typeof(val)) {
case 'undefined':
return null;
case 'boolean':
return val ? 1 : 0;
case 'object':
if (val === null || ArrayBuffer.isView(val))
return val;
if (val instanceof Date)
return val.toISOString();
return JSON.stringify(val);
}
return val;
}
// ============================================================================
// Parser of SQL statements
// ============================================================================
function isword(c) {
return (c > 64 && c < 91) || (c > 96 && c < 123) || c === 95;
}
// Splits SQL into statements: [{ sql: String, command: String, numbered: Boolean }]
// "numbered" means that the statement contains PostgreSQL parameters ($1, $2)
function parse(sql) {
var output = PARSED.get(sql);
if (output)
return output;
output = [];
var length = sql.length;
var beg = 0;
var index = 0;
var words = []; // first words of the statement
var last = ''; // the last token
var prev = ''; // the token before the last token
var numbered = false;
var istoken = false;
var flush = function(end) {
if (istoken) {
// "CREATE TRIGGER" contains statements, it ends with ";END;"
var trigger = words[0] === 'CREATE' && (words[1] === 'TRIGGER' || ((words[1] === 'TEMP' || words[1] === 'TEMPORARY') && words[2] === 'TRIGGER'));
if (trigger && end < length && !(last === 'END' && prev === ';')) {
prev = last;
last = ';';
return;
}
output.push({ sql: sql.substring(beg, end).trim(), command: words[0] || '', numbered: numbered });
}
beg = end + 1;
words = [];
last = prev = '';
numbered = istoken = false;
};
while (index < length) {
var c = sql.charCodeAt(index);
// Whitespace
if (c === 32 || c === 9 || c === 10 || c === 13) {
index++;
continue;
}
// A comment: -- text
if (c === 45 && sql.charCodeAt(index + 1) === 45) {
index = sql.indexOf('\n', index);
if (index === -1)
index = length;
continue;
}
// A comment: /* text */
if (c === 47 && sql.charCodeAt(index + 1) === 42) {
index = sql.indexOf('*/', index + 2);
index = index === -1 ? length : (index + 2);
continue;
}
// The end of the statement
if (c === 59) {
flush(index);
index++;
continue;
}
istoken = true;
prev = last;
// A string or a quoted name: 'text', "name", `name`, [name]
if (c === 39 || c === 34 || c === 96 || c === 91) {
var quote = c === 91 ? 93 : c;
index++;
while (index < length) {
if (sql.charCodeAt(index) === quote) {
// An escaped quote
if (quote !== 93 && sql.charCodeAt(index + 1) === quote) {
index += 2;
continue;
}
break;
}
index++;
}
index++;
last = '';
continue;
}
// A word
if (isword(c)) {
var start = index;
index++;
while (index < length) {
c = sql.charCodeAt(index);
if (isword(c) || (c > 47 && c < 58))
index++;
else
break;
}
last = sql.substring(start, index).toUpperCase();
words.length < 3 && words.push(last);
continue;
}
// A PostgreSQL parameter: $1
if (c === 36) {
c = sql.charCodeAt(index + 1);
if (c > 47 && c < 58)
numbered = true;
}
last = '';
index++;
}
flush(length);
PARSED.size > 1000 && PARSED.clear();
PARSED.set(sql, output);
return output;
}
// ============================================================================
// Options
// ============================================================================
function istrue(val) {
return val === true || val === 1 || val === '1' || val === 'true';
}
// Parses a connection string or object with options
function parseoptions(opt) {
if (opt && opt.$parsed)
return opt;
var output;
if (typeof(opt) === 'string') {
output = OPTIONS.get(opt);
if (output)
return output;
}
output = { $parsed: true, filename: ':memory:', readonly: false, foreignkeys: true, wal: false, timeout: 5000 };
var connstring = typeof(opt) === 'string' ? opt : opt ? (opt.connectionString || opt.connstring) : null;
if (connstring) {
var filename = connstring;
if ((/^sqlite:/i).test(connstring)) {
filename = connstring.substring(7).replace(/^\/\//, '');
var index = filename.indexOf('?');
if (index !== -1) {
for (var [key, val] of new URLSearchParams(filename.substring(index + 1))) {
switch (key) {
case 'wal':
case 'readonly':
case 'foreignkeys':
output[key] = istrue(val);
break;
case 'timeout':
output.timeout = +val || 0;
break;
}
}
filename = filename.substring(0, index);
}
try {
filename = decodeURIComponent(filename);
} catch (e) {}
}
if (filename)
output.filename = filename;
}
if (opt && typeof(opt) === 'object') {
for (var key of ['filename', 'timeout']) {
if (opt[key] != null)
output[key] = opt[key];
}
for (var key of ['wal', 'readonly', 'foreignkeys']) {
if (opt[key] != null)
output[key] = istrue(opt[key]);
}
}
if (typeof(opt) === 'string') {
OPTIONS.size > 100 && OPTIONS.clear();
OPTIONS.set(opt, output);
}
return output;
}
// The database is locked by another connection: SQLITE_BUSY (5), SQLITE_LOCKED (6)
function islocked(err) {
var code = err ? (err.errcode & 255) : 0;
return code === 5 || code === 6;
}
// ============================================================================
// Client
// ============================================================================
function Client(opt) {
var t = this;
t.opt = parseoptions(opt);
t.db = null;
t.statements = new Map();
t.queue = [];
t.current = null;
t.ready = false;
t.closed = false;
t.ending = false;
t.status = 'I'; // I: idle, T: in a transaction
t.pool = null;
t.$end = null;
t.$retry = null;
t.onclose = null;
}
const CP = Client.prototype;
CP.connect = function(callback) {
var t = this;
var opt = t.opt;
try {
if (!Sqlite)
Sqlite = require('node:sqlite');
t.db = new Sqlite.DatabaseSync(opt.filename, { readOnly: opt.readonly, enableForeignKeyConstraints: opt.foreignkeys });
opt.wal && !opt.readonly && t.db.exec('PRAGMA journal_mode=WAL');
t.ready = true;
} catch (e) {
t.destroy(e);
callback && process.nextTick(callback, e);
return t;
}
callback && process.nextTick(callback, null, t);
t.queue.length && process.nextTick(() => t.next());
return t;
};
// client.query(sql, [params], callback(err, response))
// response: { command: String, rowCount: Number, rows: Array, fields: Array, lastid: Number } or Array of responses (multiple statements)
CP.query = function(sql, params, callback) {
var t = this;
if (typeof(params) === 'function') {
callback = params;
params = null;
}
if (!callback)
return new Promise((resolve, reject) => t.query(sql, params, (err, response) => err ? reject(err) : resolve(response)));
if (t.closed || t.ending)
process.nextTick(callback, new Error('Connection is closed'));
else {
t.queue.push({ sql: sql, params: params, callback: callback, results: [], index: 0, ticks: 0 });
t.next();
}
};
CP.next = function() {
var t = this;
if (!t.ready || t.closed || t.$retry)
return;
while (true) {
var q = t.queue[0];
if (!q) {
t.ending && t.destroy();
return;
}
var err = null;
t.current = q;
try {
t.exec(q);
} catch (e) {
err = e;
}
t.current = null;
if (t.db)
t.status = t.db.isTransaction ? 'T' : 'I';
// The database is locked by another connection: the query tries it again (the process isn't blocked)
if (err && islocked(err) && t.opt.timeout) {
if (!q.ticks)
q.ticks = Date.now();
if ((Date.now() - q.ticks) < t.opt.timeout) {
t.$retry = setTimeout(function() {
t.$retry = null;
t.next();
}, 20);
return;
}
}
t.queue.shift();
if (err)
process.nextTick(q.callback, err);
else
process.nextTick(q.callback, null, q.results.length > 1 ? q.results : (q.results[0] || { command: '', rowCount: 0, rows: [], fields: [] }));
}
};
// Executes all statements of the query, it continues with "q.index" (after a locked database)
CP.exec = function(q) {
var t = this;
var statements = parse(q.sql);
var params = q.params;
var isparams = params && (params instanceof Array ? params.length > 0 : typeof(params) === 'object');
if (isparams && statements.length > 1)
throw new Error('Multiple statements can\'t be used with parameters');
while (q.index < statements.length) {
var item = statements[q.index];
var meta = t.statements.get(item.sql);
if (!meta) {
var stmt = t.db.prepare(item.sql);
var columns = stmt.columns();
// Integers are read as BigInt, otherwise a big integer throws an exception
stmt.setReadBigInts(true);
meta = { stmt: stmt, fields: [], parsers: null };
for (var column of columns) {
var type = column.type ? column.type.toUpperCase().replace(REG_SIZE, '').trim() : null;
var parser = type ? TYPES[type] : null;
meta.fields.push({ name: column.name, type: type, parser: parser });
if (parser) {
if (!meta.parsers)
meta.parsers = [];
meta.parsers.push(column.name, parser);
}
}
t.statements.size > 500 && t.statements.clear();
t.statements.set(item.sql, meta);
}
var args = EMPTY;
if (isparams) {
if (params instanceof Array) {
if (item.numbered) {
// PostgreSQL parameters: $1, $2
var named = {};
for (var i = 0; i < params.length; i++)
named['$' + (i + 1)] = param(params[i]);
args = [named];
} else {
args = new Array(params.length);
for (var i = 0; i < params.length; i++)
args[i] = param(params[i]);
}
} else {
var named = {};
for (var key in params)
named[key] = param(params[key]);
args = [named];
}
}
var result = { command: item.command, rowCount: 0, rows: [], fields: meta.fields };
if (meta.fields.length) {
var rows = result.rows = meta.stmt.all.apply(meta.stmt, args);
var parsers = meta.parsers;
result.rowCount = rows.length;
for (var row of rows) {
for (var key in row) {
var val = row[key];
if (typeof(val) === 'bigint')
row[key] = tonumber(val);
else if (val instanceof Uint8Array)
row[key] = Buffer.from(val.buffer, val.byteOffset, val.byteLength);
}
if (parsers) {
for (var i = 0; i < parsers.length; i += 2) {
var val = row[parsers[i]];
if (val != null)
row[parsers[i]] = parsers[i + 1](val);
}
}
}
} else {
var info = meta.stmt.run.apply(meta.stmt, args);
// SQLite keeps the count of changes of the last INSERT, UPDATE or DELETE, so other commands have 0
if (CHANGES[item.command]) {
result.rowCount = tonumber(info.changes);
if (item.command === 'INSERT' || item.command === 'REPLACE')
result.lastid = tonumber(info.lastInsertRowid);
}
}
// The schema can be changed: the prepared statements are released
SCHEMA[item.command] && t.statements.clear();
q.results.push(result);
q.index++;
}
};
// Closes the connection (waits for pending queries)
CP.end = function(callback) {
var t = this;
if (t.closed) {
callback && process.nextTick(callback);
return;
}
if (callback)
t.$end = callback;
t.ending = true;
if (t.queue.length)
t.next();
else
t.destroy();
};
// Closes the connection or it returns the connection to the pool
CP.close = CP.release = function(callback) {
var t = this;
if (t.pool) {
t.$release && t.$release();
callback && process.nextTick(callback);
} else
t.end(callback);
};
// Destroys the connection immediately
CP.destroy = function(err) {
var t = this;
if (t.closed)
return;
t.closed = true;
t.ready = false;
t.statements.clear();
if (t.$retry) {
clearTimeout(t.$retry);
t.$retry = null;
}
if (t.db) {
// An unfinished transaction is rolled back
try {
t.db.close();
} catch (e) {}
t.db = null;
}
t.status = 'I';
t.onclose && t.onclose(err);
var error = err || new Error('Connection is closed');
var callbacks = [];
for (var q of t.queue)
callbacks.push([q.callback, error]);
t.queue = [];
if (t.$end) {
callbacks.push([t.$end, null]);
t.$end = null;
}
for (var item of callbacks)
process.nextTick(item[0], item[1]);
};
// ============================================================================
// Pool
// ============================================================================
function Pool(opt, max) {
var t = this;
t.opt = parseoptions(opt);
// "opt" can be a connection string and Total.js extends the String prototype (e.g. "String.prototype.max")
var obj = opt && typeof(opt) === 'object' ? opt : null;
t.max = max || (obj && obj.max) || 4;
t.timeout = (obj && obj.idle) || 10000; // idle timeout
t.clients = [];
t.idle = [];
t.pending = [];
t.ended = false;
t.$end = null;
// Each connection to the in-memory database is a new database
if (t.opt.filename === ':memory:' || !t.opt.filename) {
t.max = 1;
t.timeout = 0;
}
}
const PP = Pool.prototype;
// pool.connect(function(err, client, release) {})
PP.connect = function(callback) {
var t = this;
if (t.ended)
process.nextTick(callback, new Error('Pool is closed'));
else {
t.pending.push(callback);
t.dispatch();
}
};
PP.dispatch = function() {
var t = this;
while (t.pending.length) {
var client = t.idle.pop();
if (client) {
client.$idle && clearTimeout(client.$idle);
client.$idle = null;
t.assign(client, t.pending.shift());
} else if (t.clients.length < t.max)
t.create(t.pending.shift());
else
break;
}
};
PP.create = function(callback) {
var t = this;
var client = new Client(t.opt);
client.pool = t;
client.$busy = false;
client.$idle = null;
client.$release = null;
client.onclose = function() {
var index = t.clients.indexOf(client);
if (index !== -1)
t.clients.splice(index, 1);
index = t.idle.indexOf(client);
if (index !== -1)
t.idle.splice(index, 1);
if (client.$idle) {
clearTimeout(client.$idle);
client.$idle = null;
}
if (t.ended) {
if (!t.clients.length && t.$end) {
process.nextTick(t.$end);
t.$end = null;
}
} else
t.dispatch();
};
t.clients.push(client);
client.connect(function(err) {
if (err)
callback(err);
else
t.assign(client, callback);
});
};
PP.assign = function(client, callback) {
var t = this;
var released = false;
// Each checkout has its own "release" function, so a double call can't release the connection to somebody else
client.$release = function() {
if (!released) {
released = true;
t.release(client);
}
};
client.$busy = true;
callback(null, client, client.$release);
};
PP.release = function(client) {
var t = this;
if (!client.$busy)
return;
client.$busy = false;
if (client.closed)
return;
// The connection with pending queries can't be reused
if (t.ended || client.queue.length) {
client.end();
return;
}
// An unfinished transaction is rolled back
if (client.status !== 'I') {
try {
client.db.exec('ROLLBACK');
client.status = 'I';
} catch (e) {
client.destroy(e);
return;
}
}
// The in-memory database is kept open (closing destroys its data)
if (t.timeout) {
client.$idle = setTimeout(function() {
client.$idle = null;
client.end();
}, t.timeout);
}
t.idle.push(client);
t.dispatch();
};
// pool.query(sql, [params], callback(err, response))
PP.query = function(sql, params, callback) {
var t = this;
if (typeof(params) === 'function') {
callback = params;
params = null;
}
if (!callback)
return new Promise((resolve, reject) => t.query(sql, params, (err, response) => err ? reject(err) : resolve(response)));
t.connect(function(err, client, release) {
if (err)
callback(err);
else {
client.query(sql, params, function(err, response) {
release();
callback(err, response);
});
}
});
};
// Closes all connections
PP.end = function(callback) {
var t = this;
var err = new Error('Pool is closed');
t.ended = true;
for (var fn of t.pending)
process.nextTick(fn, err);
t.pending = [];
if (t.clients.length) {
if (callback)
t.$end = callback;
// Busy connections will be closed when they are released
for (var client of t.idle.slice(0))
client.end();
} else
callback && process.nextTick(callback);
};
exports.Client = Client;
exports.Pool = Pool;
exports.types = TYPES;
// Parses a connection string or object with options
exports.options = parseoptions;
// Creates a new connection
// callback(err, client)
exports.connect = function(opt, callback) {
return new Client(opt).connect(callback);
};
// Creates a pool of connections
exports.pool = function(opt, max) {
return new Pool(opt, max);
};