@rocicorp/zero-sqlite3 1.0.0
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/LICENSE +21 -0
- package/README.md +92 -0
- package/binding.gyp +71 -0
- package/deps/common.gypi +68 -0
- package/deps/copy.js +32 -0
- package/deps/defines.gypi +42 -0
- package/deps/download.sh +122 -0
- package/deps/patches/1208.patch +15 -0
- package/deps/sqlite3/shell.c +33359 -0
- package/deps/sqlite3/sqlite3.c +263193 -0
- package/deps/sqlite3/sqlite3.h +13721 -0
- package/deps/sqlite3/sqlite3ext.h +719 -0
- package/deps/sqlite3.gyp +102 -0
- package/deps/test_extension.c +21 -0
- package/lib/database.js +89 -0
- package/lib/index.d.ts +151 -0
- package/lib/index.js +3 -0
- package/lib/methods/aggregate.js +43 -0
- package/lib/methods/backup.js +67 -0
- package/lib/methods/function.js +31 -0
- package/lib/methods/inspect.js +7 -0
- package/lib/methods/pragma.js +12 -0
- package/lib/methods/serialize.js +16 -0
- package/lib/methods/table.js +189 -0
- package/lib/methods/transaction.js +75 -0
- package/lib/methods/wrappers.js +49 -0
- package/lib/sqlite-error.js +20 -0
- package/lib/util.js +12 -0
- package/package.json +58 -0
- package/src/better_sqlite3.cpp +2140 -0
- package/src/better_sqlite3.hpp +1034 -0
package/deps/sqlite3.gyp
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
# ===
|
|
2
|
+
# This configuration defines options specific to compiling SQLite3 itself.
|
|
3
|
+
# Compile-time options are loaded by the auto-generated file "defines.gypi".
|
|
4
|
+
# The --sqlite3 option can be provided to use a custom amalgamation instead.
|
|
5
|
+
# ===
|
|
6
|
+
|
|
7
|
+
{
|
|
8
|
+
'includes': ['common.gypi'],
|
|
9
|
+
'targets': [
|
|
10
|
+
{
|
|
11
|
+
'target_name': 'locate_sqlite3',
|
|
12
|
+
'type': 'none',
|
|
13
|
+
'hard_dependency': 1,
|
|
14
|
+
'conditions': [
|
|
15
|
+
['sqlite3 == ""', {
|
|
16
|
+
'actions': [{
|
|
17
|
+
'action_name': 'copy_builtin_sqlite3',
|
|
18
|
+
'inputs': [
|
|
19
|
+
'sqlite3/sqlite3.c',
|
|
20
|
+
'sqlite3/shell.c',
|
|
21
|
+
'sqlite3/sqlite3.h',
|
|
22
|
+
'sqlite3/sqlite3ext.h',
|
|
23
|
+
],
|
|
24
|
+
'outputs': [
|
|
25
|
+
'<(SHARED_INTERMEDIATE_DIR)/sqlite3/sqlite3.c',
|
|
26
|
+
'<(SHARED_INTERMEDIATE_DIR)/sqlite3/shell.c',
|
|
27
|
+
'<(SHARED_INTERMEDIATE_DIR)/sqlite3/sqlite3.h',
|
|
28
|
+
'<(SHARED_INTERMEDIATE_DIR)/sqlite3/sqlite3ext.h',
|
|
29
|
+
],
|
|
30
|
+
'action': ['node', 'copy.js', '<(SHARED_INTERMEDIATE_DIR)/sqlite3', ''],
|
|
31
|
+
}],
|
|
32
|
+
}],
|
|
33
|
+
],
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
'target_name': 'sqlite3',
|
|
37
|
+
'type': 'static_library',
|
|
38
|
+
'dependencies': ['locate_sqlite3'],
|
|
39
|
+
'sources': ['<(SHARED_INTERMEDIATE_DIR)/sqlite3/sqlite3.c'],
|
|
40
|
+
'include_dirs': ['<(SHARED_INTERMEDIATE_DIR)/sqlite3/'],
|
|
41
|
+
'direct_dependent_settings': {
|
|
42
|
+
'include_dirs': ['<(SHARED_INTERMEDIATE_DIR)/sqlite3/'],
|
|
43
|
+
},
|
|
44
|
+
'cflags': ['-std=c99', '-w'],
|
|
45
|
+
'xcode_settings': {
|
|
46
|
+
'OTHER_CFLAGS': ['-std=c99'],
|
|
47
|
+
'WARNING_CFLAGS': ['-w'],
|
|
48
|
+
},
|
|
49
|
+
'conditions': [
|
|
50
|
+
['sqlite3 == ""', {
|
|
51
|
+
'includes': ['defines.gypi'],
|
|
52
|
+
}, {
|
|
53
|
+
'defines': [
|
|
54
|
+
# This is currently required by better-sqlite3.
|
|
55
|
+
'SQLITE_ENABLE_COLUMN_METADATA',
|
|
56
|
+
],
|
|
57
|
+
}]
|
|
58
|
+
],
|
|
59
|
+
'configurations': {
|
|
60
|
+
'Debug': {
|
|
61
|
+
'msvs_settings': { 'VCCLCompilerTool': { 'RuntimeLibrary': 1 } }, # static debug
|
|
62
|
+
},
|
|
63
|
+
'Release': {
|
|
64
|
+
'msvs_settings': { 'VCCLCompilerTool': { 'RuntimeLibrary': 0 } }, # static release
|
|
65
|
+
},
|
|
66
|
+
},
|
|
67
|
+
},
|
|
68
|
+
{
|
|
69
|
+
'target_name': 'shell',
|
|
70
|
+
'type': 'executable',
|
|
71
|
+
'dependencies': ['sqlite3'],
|
|
72
|
+
'sources': ['<(SHARED_INTERMEDIATE_DIR)/sqlite3/sqlite3.c', '<(SHARED_INTERMEDIATE_DIR)/sqlite3/shell.c'],
|
|
73
|
+
'include_dirs': ['<(SHARED_INTERMEDIATE_DIR)/sqlite3/'],
|
|
74
|
+
'direct_dependent_settings': {
|
|
75
|
+
'include_dirs': ['<(SHARED_INTERMEDIATE_DIR)/sqlite3/'],
|
|
76
|
+
},
|
|
77
|
+
'cflags': ['-std=c99', '-w'],
|
|
78
|
+
'xcode_settings': {
|
|
79
|
+
'OTHER_CFLAGS': ['-std=c99'],
|
|
80
|
+
'WARNING_CFLAGS': ['-w'],
|
|
81
|
+
},
|
|
82
|
+
'conditions': [
|
|
83
|
+
['sqlite3 == ""', {
|
|
84
|
+
'includes': ['defines.gypi'],
|
|
85
|
+
}, {
|
|
86
|
+
'defines': [
|
|
87
|
+
# This is currently required by better-sqlite3.
|
|
88
|
+
'SQLITE_ENABLE_COLUMN_METADATA',
|
|
89
|
+
],
|
|
90
|
+
}]
|
|
91
|
+
],
|
|
92
|
+
'configurations': {
|
|
93
|
+
'Debug': {
|
|
94
|
+
'msvs_settings': { 'VCCLCompilerTool': { 'RuntimeLibrary': 1 } }, # static debug
|
|
95
|
+
},
|
|
96
|
+
'Release': {
|
|
97
|
+
'msvs_settings': { 'VCCLCompilerTool': { 'RuntimeLibrary': 0 } }, # static release
|
|
98
|
+
},
|
|
99
|
+
},
|
|
100
|
+
},
|
|
101
|
+
],
|
|
102
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
#include <sqlite3ext.h>
|
|
2
|
+
SQLITE_EXTENSION_INIT1
|
|
3
|
+
|
|
4
|
+
/*
|
|
5
|
+
This SQLite3 extension is used only for testing purposes (npm test).
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
static void TestExtensionFunction(sqlite3_context* pCtx, int nVal, sqlite3_value** _) {
|
|
9
|
+
sqlite3_result_double(pCtx, (double)nVal);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
#ifdef _WIN32
|
|
13
|
+
__declspec(dllexport)
|
|
14
|
+
#endif
|
|
15
|
+
|
|
16
|
+
int sqlite3_extension_init(sqlite3* db, char** pzErrMsg, const sqlite3_api_routines* pApi) {
|
|
17
|
+
SQLITE_EXTENSION_INIT2(pApi)
|
|
18
|
+
if (pzErrMsg != 0) *pzErrMsg = 0;
|
|
19
|
+
sqlite3_create_function(db, "testExtensionFunction", -1, SQLITE_UTF8, 0, TestExtensionFunction, 0, 0);
|
|
20
|
+
return SQLITE_OK;
|
|
21
|
+
}
|
package/lib/database.js
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const fs = require('fs');
|
|
3
|
+
const path = require('path');
|
|
4
|
+
const util = require('./util');
|
|
5
|
+
const SqliteError = require('./sqlite-error');
|
|
6
|
+
|
|
7
|
+
let DEFAULT_ADDON;
|
|
8
|
+
|
|
9
|
+
function Database(filenameGiven, options) {
|
|
10
|
+
if (new.target == null) {
|
|
11
|
+
return new Database(filenameGiven, options);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// Apply defaults
|
|
15
|
+
let buffer;
|
|
16
|
+
if (Buffer.isBuffer(filenameGiven)) {
|
|
17
|
+
buffer = filenameGiven;
|
|
18
|
+
filenameGiven = ':memory:';
|
|
19
|
+
}
|
|
20
|
+
if (filenameGiven == null) filenameGiven = '';
|
|
21
|
+
if (options == null) options = {};
|
|
22
|
+
|
|
23
|
+
// Validate arguments
|
|
24
|
+
if (typeof filenameGiven !== 'string') throw new TypeError('Expected first argument to be a string');
|
|
25
|
+
if (typeof options !== 'object') throw new TypeError('Expected second argument to be an options object');
|
|
26
|
+
if ('readOnly' in options) throw new TypeError('Misspelled option "readOnly" should be "readonly"');
|
|
27
|
+
if ('memory' in options) throw new TypeError('Option "memory" was removed in v7.0.0 (use ":memory:" filename instead)');
|
|
28
|
+
|
|
29
|
+
// Interpret options
|
|
30
|
+
const filename = filenameGiven.trim();
|
|
31
|
+
const anonymous = filename === '' || filename === ':memory:';
|
|
32
|
+
const readonly = util.getBooleanOption(options, 'readonly');
|
|
33
|
+
const fileMustExist = util.getBooleanOption(options, 'fileMustExist');
|
|
34
|
+
const timeout = 'timeout' in options ? options.timeout : 5000;
|
|
35
|
+
const verbose = 'verbose' in options ? options.verbose : null;
|
|
36
|
+
const nativeBinding = 'nativeBinding' in options ? options.nativeBinding : null;
|
|
37
|
+
|
|
38
|
+
// Validate interpreted options
|
|
39
|
+
if (readonly && anonymous && !buffer) throw new TypeError('In-memory/temporary databases cannot be readonly');
|
|
40
|
+
if (!Number.isInteger(timeout) || timeout < 0) throw new TypeError('Expected the "timeout" option to be a positive integer');
|
|
41
|
+
if (timeout > 0x7fffffff) throw new RangeError('Option "timeout" cannot be greater than 2147483647');
|
|
42
|
+
if (verbose != null && typeof verbose !== 'function') throw new TypeError('Expected the "verbose" option to be a function');
|
|
43
|
+
if (nativeBinding != null && typeof nativeBinding !== 'string' && typeof nativeBinding !== 'object') throw new TypeError('Expected the "nativeBinding" option to be a string or addon object');
|
|
44
|
+
|
|
45
|
+
// Load the native addon
|
|
46
|
+
let addon;
|
|
47
|
+
if (nativeBinding == null) {
|
|
48
|
+
addon = DEFAULT_ADDON || (DEFAULT_ADDON = require('bindings')('better_sqlite3.node'));
|
|
49
|
+
} else if (typeof nativeBinding === 'string') {
|
|
50
|
+
// See <https://webpack.js.org/api/module-variables/#__non_webpack_require__-webpack-specific>
|
|
51
|
+
const requireFunc = typeof __non_webpack_require__ === 'function' ? __non_webpack_require__ : require;
|
|
52
|
+
addon = requireFunc(path.resolve(nativeBinding).replace(/(\.node)?$/, '.node'));
|
|
53
|
+
} else {
|
|
54
|
+
// See <https://github.com/WiseLibs/better-sqlite3/issues/972>
|
|
55
|
+
addon = nativeBinding;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
if (!addon.isInitialized) {
|
|
59
|
+
addon.setErrorConstructor(SqliteError);
|
|
60
|
+
addon.isInitialized = true;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Make sure the specified directory exists
|
|
64
|
+
if (!anonymous && !fs.existsSync(path.dirname(filename))) {
|
|
65
|
+
throw new TypeError('Cannot open database because the directory does not exist');
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
Object.defineProperties(this, {
|
|
69
|
+
[util.cppdb]: { value: new addon.Database(filename, filenameGiven, anonymous, readonly, fileMustExist, timeout, verbose || null, buffer || null) },
|
|
70
|
+
...wrappers.getters,
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const wrappers = require('./methods/wrappers');
|
|
75
|
+
Database.prototype.prepare = wrappers.prepare;
|
|
76
|
+
Database.prototype.transaction = require('./methods/transaction');
|
|
77
|
+
Database.prototype.pragma = require('./methods/pragma');
|
|
78
|
+
Database.prototype.backup = require('./methods/backup');
|
|
79
|
+
Database.prototype.serialize = require('./methods/serialize');
|
|
80
|
+
Database.prototype.function = require('./methods/function');
|
|
81
|
+
Database.prototype.aggregate = require('./methods/aggregate');
|
|
82
|
+
Database.prototype.table = require('./methods/table');
|
|
83
|
+
Database.prototype.exec = wrappers.exec;
|
|
84
|
+
Database.prototype.close = wrappers.close;
|
|
85
|
+
Database.prototype.defaultSafeIntegers = wrappers.defaultSafeIntegers;
|
|
86
|
+
Database.prototype.unsafeMode = wrappers.unsafeMode;
|
|
87
|
+
Database.prototype[util.inspect] = require('./methods/inspect');
|
|
88
|
+
|
|
89
|
+
module.exports = Database;
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
|
|
3
|
+
// FIXME: Is this `any` really necessary?
|
|
4
|
+
type VariableArgFunction = (...params: any[]) => unknown;
|
|
5
|
+
type ArgumentTypes<F extends VariableArgFunction> = F extends (...args: infer A) => unknown ? A : never;
|
|
6
|
+
type ElementOf<T> = T extends Array<infer E> ? E : T;
|
|
7
|
+
|
|
8
|
+
declare namespace BetterSqlite3 {
|
|
9
|
+
interface Statement<BindParameters extends unknown[], Result = unknown> {
|
|
10
|
+
database: Database;
|
|
11
|
+
source: string;
|
|
12
|
+
reader: boolean;
|
|
13
|
+
readonly: boolean;
|
|
14
|
+
busy: boolean;
|
|
15
|
+
|
|
16
|
+
run(...params: BindParameters): Database.RunResult;
|
|
17
|
+
get(...params: BindParameters): Result | undefined;
|
|
18
|
+
all(...params: BindParameters): Result[];
|
|
19
|
+
iterate(...params: BindParameters): IterableIterator<Result>;
|
|
20
|
+
pluck(toggleState?: boolean): this;
|
|
21
|
+
expand(toggleState?: boolean): this;
|
|
22
|
+
raw(toggleState?: boolean): this;
|
|
23
|
+
bind(...params: BindParameters): this;
|
|
24
|
+
columns(): ColumnDefinition[];
|
|
25
|
+
safeIntegers(toggleState?: boolean): this;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
interface ColumnDefinition {
|
|
29
|
+
name: string;
|
|
30
|
+
column: string | null;
|
|
31
|
+
table: string | null;
|
|
32
|
+
database: string | null;
|
|
33
|
+
type: string | null;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
interface Transaction<F extends VariableArgFunction> {
|
|
37
|
+
(...params: ArgumentTypes<F>): ReturnType<F>;
|
|
38
|
+
default(...params: ArgumentTypes<F>): ReturnType<F>;
|
|
39
|
+
deferred(...params: ArgumentTypes<F>): ReturnType<F>;
|
|
40
|
+
immediate(...params: ArgumentTypes<F>): ReturnType<F>;
|
|
41
|
+
exclusive(...params: ArgumentTypes<F>): ReturnType<F>;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
interface VirtualTableOptions {
|
|
45
|
+
rows: (...params: unknown[]) => Generator;
|
|
46
|
+
columns: string[];
|
|
47
|
+
parameters?: string[] | undefined;
|
|
48
|
+
safeIntegers?: boolean | undefined;
|
|
49
|
+
directOnly?: boolean | undefined;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
interface Database {
|
|
53
|
+
memory: boolean;
|
|
54
|
+
readonly: boolean;
|
|
55
|
+
name: string;
|
|
56
|
+
open: boolean;
|
|
57
|
+
inTransaction: boolean;
|
|
58
|
+
|
|
59
|
+
prepare<BindParameters extends unknown[] | {} = unknown[], Result = unknown>(
|
|
60
|
+
source: string,
|
|
61
|
+
): BindParameters extends unknown[] ? Statement<BindParameters, Result> : Statement<[BindParameters], Result>;
|
|
62
|
+
transaction<F extends VariableArgFunction>(fn: F): Transaction<F>;
|
|
63
|
+
exec(source: string): this;
|
|
64
|
+
pragma(source: string, options?: Database.PragmaOptions): unknown;
|
|
65
|
+
function(name: string, cb: (...params: unknown[]) => unknown): this;
|
|
66
|
+
function(name: string, options: Database.RegistrationOptions, cb: (...params: unknown[]) => unknown): this;
|
|
67
|
+
aggregate<T>(
|
|
68
|
+
name: string,
|
|
69
|
+
options: Database.RegistrationOptions & {
|
|
70
|
+
start?: T | (() => T);
|
|
71
|
+
// eslint-disable-next-line @typescript-eslint/no-invalid-void-type
|
|
72
|
+
step: (total: T, next: ElementOf<T>) => T | void;
|
|
73
|
+
inverse?: ((total: T, dropped: T) => T) | undefined;
|
|
74
|
+
result?: ((total: T) => unknown) | undefined;
|
|
75
|
+
},
|
|
76
|
+
): this;
|
|
77
|
+
loadExtension(path: string): this;
|
|
78
|
+
close(): this;
|
|
79
|
+
defaultSafeIntegers(toggleState?: boolean): this;
|
|
80
|
+
backup(destinationFile: string, options?: Database.BackupOptions): Promise<Database.BackupMetadata>;
|
|
81
|
+
table(name: string, options: VirtualTableOptions): this;
|
|
82
|
+
unsafeMode(unsafe?: boolean): this;
|
|
83
|
+
serialize(options?: Database.SerializeOptions): Buffer;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
interface DatabaseConstructor {
|
|
87
|
+
new(filename?: string | Buffer, options?: Database.Options): Database;
|
|
88
|
+
(filename?: string, options?: Database.Options): Database;
|
|
89
|
+
prototype: Database;
|
|
90
|
+
|
|
91
|
+
SqliteError: typeof SqliteError;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
declare class SqliteError extends Error {
|
|
96
|
+
name: string;
|
|
97
|
+
message: string;
|
|
98
|
+
code: string;
|
|
99
|
+
constructor(message: string, code: string);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
declare namespace Database {
|
|
103
|
+
interface RunResult {
|
|
104
|
+
changes: number;
|
|
105
|
+
lastInsertRowid: number | bigint;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
interface Options {
|
|
109
|
+
readonly?: boolean | undefined;
|
|
110
|
+
fileMustExist?: boolean | undefined;
|
|
111
|
+
timeout?: number | undefined;
|
|
112
|
+
verbose?: ((message?: unknown, ...additionalArgs: unknown[]) => void) | undefined;
|
|
113
|
+
nativeBinding?: string | undefined;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
interface SerializeOptions {
|
|
117
|
+
attached?: string;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
interface PragmaOptions {
|
|
121
|
+
simple?: boolean | undefined;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
interface RegistrationOptions {
|
|
125
|
+
varargs?: boolean | undefined;
|
|
126
|
+
deterministic?: boolean | undefined;
|
|
127
|
+
safeIntegers?: boolean | undefined;
|
|
128
|
+
directOnly?: boolean | undefined;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
type AggregateOptions = Parameters<BetterSqlite3.Database["aggregate"]>[1];
|
|
132
|
+
|
|
133
|
+
interface BackupMetadata {
|
|
134
|
+
totalPages: number;
|
|
135
|
+
remainingPages: number;
|
|
136
|
+
}
|
|
137
|
+
interface BackupOptions {
|
|
138
|
+
progress: (info: BackupMetadata) => number;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
type SqliteError = typeof SqliteError;
|
|
142
|
+
type Statement<BindParameters extends unknown[] | {} = unknown[], Result = unknown> = BindParameters extends
|
|
143
|
+
unknown[] ? BetterSqlite3.Statement<BindParameters, Result>
|
|
144
|
+
: BetterSqlite3.Statement<[BindParameters], Result>;
|
|
145
|
+
type ColumnDefinition = BetterSqlite3.ColumnDefinition;
|
|
146
|
+
type Transaction<T extends VariableArgFunction = VariableArgFunction> = BetterSqlite3.Transaction<T>;
|
|
147
|
+
type Database = BetterSqlite3.Database;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
declare const Database: BetterSqlite3.DatabaseConstructor;
|
|
151
|
+
export = Database;
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const { getBooleanOption, cppdb } = require('../util');
|
|
3
|
+
|
|
4
|
+
module.exports = function defineAggregate(name, options) {
|
|
5
|
+
// Validate arguments
|
|
6
|
+
if (typeof name !== 'string') throw new TypeError('Expected first argument to be a string');
|
|
7
|
+
if (typeof options !== 'object' || options === null) throw new TypeError('Expected second argument to be an options object');
|
|
8
|
+
if (!name) throw new TypeError('User-defined function name cannot be an empty string');
|
|
9
|
+
|
|
10
|
+
// Interpret options
|
|
11
|
+
const start = 'start' in options ? options.start : null;
|
|
12
|
+
const step = getFunctionOption(options, 'step', true);
|
|
13
|
+
const inverse = getFunctionOption(options, 'inverse', false);
|
|
14
|
+
const result = getFunctionOption(options, 'result', false);
|
|
15
|
+
const safeIntegers = 'safeIntegers' in options ? +getBooleanOption(options, 'safeIntegers') : 2;
|
|
16
|
+
const deterministic = getBooleanOption(options, 'deterministic');
|
|
17
|
+
const directOnly = getBooleanOption(options, 'directOnly');
|
|
18
|
+
const varargs = getBooleanOption(options, 'varargs');
|
|
19
|
+
let argCount = -1;
|
|
20
|
+
|
|
21
|
+
// Determine argument count
|
|
22
|
+
if (!varargs) {
|
|
23
|
+
argCount = Math.max(getLength(step), inverse ? getLength(inverse) : 0);
|
|
24
|
+
if (argCount > 0) argCount -= 1;
|
|
25
|
+
if (argCount > 100) throw new RangeError('User-defined functions cannot have more than 100 arguments');
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
this[cppdb].aggregate(start, step, inverse, result, name, argCount, safeIntegers, deterministic, directOnly);
|
|
29
|
+
return this;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
const getFunctionOption = (options, key, required) => {
|
|
33
|
+
const value = key in options ? options[key] : null;
|
|
34
|
+
if (typeof value === 'function') return value;
|
|
35
|
+
if (value != null) throw new TypeError(`Expected the "${key}" option to be a function`);
|
|
36
|
+
if (required) throw new TypeError(`Missing required option "${key}"`);
|
|
37
|
+
return null;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
const getLength = ({ length }) => {
|
|
41
|
+
if (Number.isInteger(length) && length >= 0) return length;
|
|
42
|
+
throw new TypeError('Expected function.length to be a positive integer');
|
|
43
|
+
};
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const fs = require('fs');
|
|
3
|
+
const path = require('path');
|
|
4
|
+
const { promisify } = require('util');
|
|
5
|
+
const { cppdb } = require('../util');
|
|
6
|
+
const fsAccess = promisify(fs.access);
|
|
7
|
+
|
|
8
|
+
module.exports = async function backup(filename, options) {
|
|
9
|
+
if (options == null) options = {};
|
|
10
|
+
|
|
11
|
+
// Validate arguments
|
|
12
|
+
if (typeof filename !== 'string') throw new TypeError('Expected first argument to be a string');
|
|
13
|
+
if (typeof options !== 'object') throw new TypeError('Expected second argument to be an options object');
|
|
14
|
+
|
|
15
|
+
// Interpret options
|
|
16
|
+
filename = filename.trim();
|
|
17
|
+
const attachedName = 'attached' in options ? options.attached : 'main';
|
|
18
|
+
const handler = 'progress' in options ? options.progress : null;
|
|
19
|
+
|
|
20
|
+
// Validate interpreted options
|
|
21
|
+
if (!filename) throw new TypeError('Backup filename cannot be an empty string');
|
|
22
|
+
if (filename === ':memory:') throw new TypeError('Invalid backup filename ":memory:"');
|
|
23
|
+
if (typeof attachedName !== 'string') throw new TypeError('Expected the "attached" option to be a string');
|
|
24
|
+
if (!attachedName) throw new TypeError('The "attached" option cannot be an empty string');
|
|
25
|
+
if (handler != null && typeof handler !== 'function') throw new TypeError('Expected the "progress" option to be a function');
|
|
26
|
+
|
|
27
|
+
// Make sure the specified directory exists
|
|
28
|
+
await fsAccess(path.dirname(filename)).catch(() => {
|
|
29
|
+
throw new TypeError('Cannot save backup because the directory does not exist');
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
const isNewFile = await fsAccess(filename).then(() => false, () => true);
|
|
33
|
+
return runBackup(this[cppdb].backup(this, attachedName, filename, isNewFile), handler || null);
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
const runBackup = (backup, handler) => {
|
|
37
|
+
let rate = 0;
|
|
38
|
+
let useDefault = true;
|
|
39
|
+
|
|
40
|
+
return new Promise((resolve, reject) => {
|
|
41
|
+
setImmediate(function step() {
|
|
42
|
+
try {
|
|
43
|
+
const progress = backup.transfer(rate);
|
|
44
|
+
if (!progress.remainingPages) {
|
|
45
|
+
backup.close();
|
|
46
|
+
resolve(progress);
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
if (useDefault) {
|
|
50
|
+
useDefault = false;
|
|
51
|
+
rate = 100;
|
|
52
|
+
}
|
|
53
|
+
if (handler) {
|
|
54
|
+
const ret = handler(progress);
|
|
55
|
+
if (ret !== undefined) {
|
|
56
|
+
if (typeof ret === 'number' && ret === ret) rate = Math.max(0, Math.min(0x7fffffff, Math.round(ret)));
|
|
57
|
+
else throw new TypeError('Expected progress callback to return a number or undefined');
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
setImmediate(step);
|
|
61
|
+
} catch (err) {
|
|
62
|
+
backup.close();
|
|
63
|
+
reject(err);
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const { getBooleanOption, cppdb } = require('../util');
|
|
3
|
+
|
|
4
|
+
module.exports = function defineFunction(name, options, fn) {
|
|
5
|
+
// Apply defaults
|
|
6
|
+
if (options == null) options = {};
|
|
7
|
+
if (typeof options === 'function') { fn = options; options = {}; }
|
|
8
|
+
|
|
9
|
+
// Validate arguments
|
|
10
|
+
if (typeof name !== 'string') throw new TypeError('Expected first argument to be a string');
|
|
11
|
+
if (typeof fn !== 'function') throw new TypeError('Expected last argument to be a function');
|
|
12
|
+
if (typeof options !== 'object') throw new TypeError('Expected second argument to be an options object');
|
|
13
|
+
if (!name) throw new TypeError('User-defined function name cannot be an empty string');
|
|
14
|
+
|
|
15
|
+
// Interpret options
|
|
16
|
+
const safeIntegers = 'safeIntegers' in options ? +getBooleanOption(options, 'safeIntegers') : 2;
|
|
17
|
+
const deterministic = getBooleanOption(options, 'deterministic');
|
|
18
|
+
const directOnly = getBooleanOption(options, 'directOnly');
|
|
19
|
+
const varargs = getBooleanOption(options, 'varargs');
|
|
20
|
+
let argCount = -1;
|
|
21
|
+
|
|
22
|
+
// Determine argument count
|
|
23
|
+
if (!varargs) {
|
|
24
|
+
argCount = fn.length;
|
|
25
|
+
if (!Number.isInteger(argCount) || argCount < 0) throw new TypeError('Expected function.length to be a positive integer');
|
|
26
|
+
if (argCount > 100) throw new RangeError('User-defined functions cannot have more than 100 arguments');
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
this[cppdb].function(fn, name, argCount, safeIntegers, deterministic, directOnly);
|
|
30
|
+
return this;
|
|
31
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const { getBooleanOption, cppdb } = require('../util');
|
|
3
|
+
|
|
4
|
+
module.exports = function pragma(source, options) {
|
|
5
|
+
if (options == null) options = {};
|
|
6
|
+
if (typeof source !== 'string') throw new TypeError('Expected first argument to be a string');
|
|
7
|
+
if (typeof options !== 'object') throw new TypeError('Expected second argument to be an options object');
|
|
8
|
+
const simple = getBooleanOption(options, 'simple');
|
|
9
|
+
|
|
10
|
+
const stmt = this[cppdb].prepare(`PRAGMA ${source}`, this, true);
|
|
11
|
+
return simple ? stmt.pluck().get() : stmt.all();
|
|
12
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const { cppdb } = require('../util');
|
|
3
|
+
|
|
4
|
+
module.exports = function serialize(options) {
|
|
5
|
+
if (options == null) options = {};
|
|
6
|
+
|
|
7
|
+
// Validate arguments
|
|
8
|
+
if (typeof options !== 'object') throw new TypeError('Expected first argument to be an options object');
|
|
9
|
+
|
|
10
|
+
// Interpret and validate options
|
|
11
|
+
const attachedName = 'attached' in options ? options.attached : 'main';
|
|
12
|
+
if (typeof attachedName !== 'string') throw new TypeError('Expected the "attached" option to be a string');
|
|
13
|
+
if (!attachedName) throw new TypeError('The "attached" option cannot be an empty string');
|
|
14
|
+
|
|
15
|
+
return this[cppdb].serialize(attachedName);
|
|
16
|
+
};
|