Revert all changes since v8.8.5 to reapply cleanly
This commit is contained in:
parent
850afac9a5
commit
6439afa9d5
3
.gitattributes
vendored
3
.gitattributes
vendored
@ -1,2 +1,5 @@
|
||||
*.lzz linguist-language=C++
|
||||
*.cpp -diff
|
||||
*.hpp -diff
|
||||
*.c -diff
|
||||
*.h -diff
|
||||
|
||||
@ -12,9 +12,6 @@
|
||||
'cflags_cc': ['-std=c++20'],
|
||||
'xcode_settings': {
|
||||
'OTHER_CPLUSPLUSFLAGS': ['-std=c++20', '-stdlib=libc++'],
|
||||
'GCC_ENABLE_CPP_EXCEPTIONS': 'NO',
|
||||
'GCC_SYMBOLS_PRIVATE_EXTERN': 'YES', # -fvisibility=hidden
|
||||
'LLVM_LTO': 'YES',
|
||||
},
|
||||
'msvs_settings': {
|
||||
'VCCLCompilerTool': {
|
||||
@ -32,5 +29,10 @@
|
||||
}],
|
||||
],
|
||||
},
|
||||
{
|
||||
'target_name': 'test_extension',
|
||||
'dependencies': ['deps/sqlite3.gyp:sqlite3'],
|
||||
'conditions': [['sqlite3 == ""', { 'sources': ['deps/test_extension.c'] }]],
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
6
deps/defines.gypi
vendored
6
deps/defines.gypi
vendored
@ -10,11 +10,6 @@
|
||||
'SQLITE_OMIT_TCL_VARIABLE',
|
||||
'SQLITE_OMIT_PROGRESS_CALLBACK',
|
||||
'SQLITE_OMIT_SHARED_CACHE',
|
||||
'SQLITE_OMIT_UTF16',
|
||||
'SQLITE_OMIT_COMPLETE',
|
||||
'SQLITE_OMIT_GET_TABLE',
|
||||
'SQLITE_OMIT_AUTHORIZATION',
|
||||
'SQLITE_OMIT_LOAD_EXTENSION',
|
||||
'SQLITE_TRACE_SIZE_LIMIT=32',
|
||||
'SQLITE_DEFAULT_CACHE_SIZE=-16000',
|
||||
'SQLITE_DEFAULT_FOREIGN_KEYS=1',
|
||||
@ -27,6 +22,7 @@
|
||||
'SQLITE_ENABLE_STAT4',
|
||||
'SQLITE_ENABLE_FTS5',
|
||||
'SQLITE_ENABLE_JSON1',
|
||||
'SQLITE_ENABLE_RTREE',
|
||||
'SQLITE_INTROSPECTION_PRAGMAS',
|
||||
|
||||
'HAVE_STDINT_H=1',
|
||||
|
||||
4
deps/sqlite3.gyp
vendored
4
deps/sqlite3.gyp
vendored
@ -93,12 +93,10 @@
|
||||
}
|
||||
},
|
||||
'OS == "mac"', {
|
||||
'defines': [
|
||||
'SQLCIPHER_CRYPTO_CC',
|
||||
],
|
||||
'link_settings': {
|
||||
'libraries': [
|
||||
# This statically links libcrypto, whereas -lcrypto would dynamically link it
|
||||
'<(SHARED_INTERMEDIATE_DIR)/sqlite3/OpenSSL-mac-<(target_arch)/libcrypto.a',
|
||||
'<(SHARED_INTERMEDIATE_DIR)/sqlite3/signal-tokenizer/>(rust_arch)-apple-darwin/libsignal_tokenizer.a',
|
||||
]
|
||||
}
|
||||
|
||||
2
index.d.ts
vendored
2
index.d.ts
vendored
@ -88,11 +88,13 @@ declare namespace BetterSqlite3 {
|
||||
function(name: string, cb: (...params: any[]) => any): this;
|
||||
function(name: string, options: Database.RegistrationOptions, cb: (...params: any[]) => any): this;
|
||||
aggregate(name: string, options: Database.AggregateOptions): this;
|
||||
loadExtension(path: string, entryPoint?: string): this;
|
||||
close(): this;
|
||||
defaultSafeIntegers(toggleState?: boolean): this;
|
||||
backup(destinationFile: string, options?: Database.BackupOptions): Promise<Database.BackupMetadata>;
|
||||
table(name: string, options: VirtualTableOptions): this;
|
||||
unsafeMode(unsafe?: boolean): this;
|
||||
serialize(options?: Database.SerializeOptions): Buffer;
|
||||
createFTS5Tokenizer(name: string, tokenizer: FTS5TokenizerConstructor): void;
|
||||
signalTokenize(value: string): Array<string>;
|
||||
}
|
||||
|
||||
@ -80,10 +80,12 @@ Database.prototype.prepare = wrappers.prepare;
|
||||
Database.prototype.transaction = require('./methods/transaction');
|
||||
Database.prototype.pragma = require('./methods/pragma');
|
||||
Database.prototype.backup = require('./methods/backup');
|
||||
Database.prototype.serialize = require('./methods/serialize');
|
||||
Database.prototype.function = require('./methods/function');
|
||||
Database.prototype.aggregate = require('./methods/aggregate');
|
||||
Database.prototype.table = require('./methods/table');
|
||||
Database.prototype.createFTS5Tokenizer = require('./methods/createFTS5Tokenizer');
|
||||
Database.prototype.loadExtension = wrappers.loadExtension;
|
||||
Database.prototype.exec = wrappers.exec;
|
||||
Database.prototype.close = wrappers.close;
|
||||
Database.prototype.defaultSafeIntegers = wrappers.defaultSafeIntegers;
|
||||
|
||||
16
lib/methods/serialize.js
Normal file
16
lib/methods/serialize.js
Normal file
@ -0,0 +1,16 @@
|
||||
'use strict';
|
||||
const { cppdb } = require('../util');
|
||||
|
||||
module.exports = function serialize(options) {
|
||||
if (options == null) options = {};
|
||||
|
||||
// Validate arguments
|
||||
if (typeof options !== 'object') throw new TypeError('Expected first argument to be an options object');
|
||||
|
||||
// Interpret and validate options
|
||||
const attachedName = 'attached' in options ? options.attached : 'main';
|
||||
if (typeof attachedName !== 'string') throw new TypeError('Expected the "attached" option to be a string');
|
||||
if (!attachedName) throw new TypeError('The "attached" option cannot be an empty string');
|
||||
|
||||
return this[cppdb].serialize(attachedName);
|
||||
};
|
||||
@ -15,6 +15,11 @@ exports.close = function close() {
|
||||
return this;
|
||||
};
|
||||
|
||||
exports.loadExtension = function loadExtension(...args) {
|
||||
this[cppdb].loadExtension(...args);
|
||||
return this;
|
||||
};
|
||||
|
||||
exports.defaultSafeIntegers = function defaultSafeIntegers(...args) {
|
||||
this[cppdb].defaultSafeIntegers(...args);
|
||||
return this;
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@signalapp/better-sqlite3",
|
||||
"version": "9.0.2",
|
||||
"version": "8.8.5",
|
||||
"description": "The fastest and simplest library for SQLite3 in Node.js.",
|
||||
"homepage": "http://github.com/WiseLibs/better-sqlite3",
|
||||
"author": "Joshua Wise <joshuathomaswise@gmail.com>",
|
||||
@ -34,13 +34,15 @@
|
||||
"sqlite3": "^5.0.2"
|
||||
},
|
||||
"scripts": {
|
||||
"format": "xcrun clang-format --style=chromium -Werror --verbose -i src/*.cpp src/*.hpp",
|
||||
"install": "npm run download && npm run build-release",
|
||||
"build-release": "node-gyp rebuild --release",
|
||||
"build-debug": "node-gyp rebuild --debug",
|
||||
"rebuild-release": "npm run lzz && npm run build-release",
|
||||
"rebuild-debug": "npm run lzz && npm run build-debug",
|
||||
"test": "mocha --exit --slow=75 --timeout=5000",
|
||||
"benchmark": "node benchmark",
|
||||
"download": "node ./deps/download.js"
|
||||
"download": "node ./deps/download.js",
|
||||
"lzz": "lzz -hx hpp -sx cpp -k BETTER_SQLITE3 -d -hl -sl -e ./src/better_sqlite3.lzz"
|
||||
},
|
||||
"license": "MIT",
|
||||
"keywords": [
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
130
src/better_sqlite3.lzz
Normal file
130
src/better_sqlite3.lzz
Normal file
@ -0,0 +1,130 @@
|
||||
#hdr
|
||||
#include <climits>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <set>
|
||||
#include <unordered_map>
|
||||
#include <algorithm>
|
||||
#include <sqlite3.h>
|
||||
#include <node.h>
|
||||
#include <node_object_wrap.h>
|
||||
#include <node_buffer.h>
|
||||
#include <uv.h>
|
||||
#include "signal-tokenizer.h"
|
||||
#end
|
||||
|
||||
#insert "util/macros.lzz"
|
||||
#insert "util/query-macros.lzz"
|
||||
#insert "util/constants.lzz"
|
||||
#insert "util/bind-map.lzz"
|
||||
struct Addon;
|
||||
class Statement;
|
||||
class TokenizerModule;
|
||||
class SignalTokenizerModule;
|
||||
class Backup;
|
||||
#insert "objects/database.lzz"
|
||||
#insert "objects/statement.lzz"
|
||||
#insert "objects/statement-iterator.lzz"
|
||||
#insert "objects/backup.lzz"
|
||||
#insert "objects/tokenizer.lzz"
|
||||
#insert "objects/signal-tokenizer.lzz"
|
||||
#insert "util/data-converter.lzz"
|
||||
#insert "util/custom-function.lzz"
|
||||
#insert "util/custom-aggregate.lzz"
|
||||
#insert "util/custom-table.lzz"
|
||||
#insert "util/data.lzz"
|
||||
#insert "util/binder.lzz"
|
||||
|
||||
struct Addon {
|
||||
NODE_METHOD(JS_setErrorConstructor) {
|
||||
REQUIRE_ARGUMENT_FUNCTION(first, v8::Local<v8::Function> SqliteError);
|
||||
OnlyAddon->SqliteError.Reset(OnlyIsolate, SqliteError);
|
||||
}
|
||||
|
||||
NODE_METHOD(JS_setLogHandler) {
|
||||
REQUIRE_ARGUMENT_FUNCTION(first, v8::Local<v8::Function> LogHandler);
|
||||
OnlyAddon->LogHandler.Reset(OnlyIsolate, LogHandler);
|
||||
}
|
||||
|
||||
static void Cleanup(void* ptr) {
|
||||
Addon* addon = static_cast<Addon*>(ptr);
|
||||
for (Database* db : addon->dbs) db->CloseHandles();
|
||||
addon->dbs.clear();
|
||||
delete addon;
|
||||
}
|
||||
|
||||
static void SqliteLog(void *pArg, int iErrCode, const char *zMsg) {
|
||||
Addon* addon = static_cast<Addon*>(uv_key_get(&thread_key));
|
||||
if (addon->LogHandler.IsEmpty()) {
|
||||
return;
|
||||
}
|
||||
EasyIsolate;
|
||||
v8::HandleScope scope(isolate);
|
||||
v8::Local<v8::Function> handler = addon->LogHandler.Get(isolate);
|
||||
v8::Local<v8::Value> arg[] = {
|
||||
v8::Integer::New(isolate, static_cast<int32_t>(iErrCode)),
|
||||
StringFromUtf8(isolate, zMsg, -1)
|
||||
};
|
||||
handler->Call(isolate->GetCurrentContext(), v8::Undefined(isolate), 2, arg).ToLocalChecked();
|
||||
}
|
||||
|
||||
static void InitLoggerOnce() {
|
||||
int err = uv_key_create(&thread_key);
|
||||
if (err != 0) {
|
||||
abort();
|
||||
}
|
||||
sqlite3_initialize();
|
||||
sqlite3_config(SQLITE_CONFIG_LOG, Addon::SqliteLog, nullptr);
|
||||
}
|
||||
|
||||
explicit Addon(v8::Isolate* isolate) :
|
||||
privileged_info(NULL),
|
||||
next_id(0),
|
||||
cs(isolate) {
|
||||
static uv_once_t init_once = UV_ONCE_INIT;
|
||||
uv_once(&init_once, InitLoggerOnce);
|
||||
uv_key_set(&thread_key, this);
|
||||
}
|
||||
|
||||
inline sqlite3_uint64 NextId() {
|
||||
return next_id++;
|
||||
}
|
||||
|
||||
CopyablePersistent<v8::Function> Statement;
|
||||
CopyablePersistent<v8::Function> StatementIterator;
|
||||
CopyablePersistent<v8::Function> Backup;
|
||||
CopyablePersistent<v8::Function> SqliteError;
|
||||
CopyablePersistent<v8::Function> LogHandler;
|
||||
NODE_ARGUMENTS_POINTER privileged_info;
|
||||
sqlite3_uint64 next_id;
|
||||
CS cs;
|
||||
std::set<Database*, Database::CompareDatabase> dbs;
|
||||
static uv_key_t thread_key;
|
||||
};
|
||||
|
||||
#src
|
||||
NODE_MODULE_INIT(/* exports, context */) {
|
||||
v8::Isolate* isolate = context->GetIsolate();
|
||||
v8::HandleScope scope(isolate);
|
||||
|
||||
// Initialize addon instance.
|
||||
Addon* addon = new Addon(isolate);
|
||||
v8::Local<v8::External> data = v8::External::New(isolate, addon);
|
||||
node::AddEnvironmentCleanupHook(isolate, Addon::Cleanup, addon);
|
||||
|
||||
// Create and export native-backed classes and functions.
|
||||
exports->Set(context, InternalizedFromLatin1(isolate, "Database"), Database::Init(isolate, data)).FromJust();
|
||||
exports->Set(context, InternalizedFromLatin1(isolate, "Statement"), Statement::Init(isolate, data)).FromJust();
|
||||
exports->Set(context, InternalizedFromLatin1(isolate, "StatementIterator"), StatementIterator::Init(isolate, data)).FromJust();
|
||||
exports->Set(context, InternalizedFromLatin1(isolate, "Backup"), Backup::Init(isolate, data)).FromJust();
|
||||
exports->Set(context, InternalizedFromLatin1(isolate, "setErrorConstructor"), v8::FunctionTemplate::New(isolate, Addon::JS_setErrorConstructor, data)->GetFunction(context).ToLocalChecked()).FromJust();
|
||||
exports->Set(context, InternalizedFromLatin1(isolate, "setLogHandler"), v8::FunctionTemplate::New(isolate, Addon::JS_setLogHandler, data)->GetFunction(context).ToLocalChecked()).FromJust();
|
||||
|
||||
// Store addon instance data.
|
||||
addon->Statement.Reset(isolate, exports->Get(context, InternalizedFromLatin1(isolate, "Statement")).ToLocalChecked().As<v8::Function>());
|
||||
addon->StatementIterator.Reset(isolate, exports->Get(context, InternalizedFromLatin1(isolate, "StatementIterator")).ToLocalChecked().As<v8::Function>());
|
||||
addon->Backup.Reset(isolate, exports->Get(context, InternalizedFromLatin1(isolate, "Backup")).ToLocalChecked().As<v8::Function>());
|
||||
}
|
||||
#end
|
||||
138
src/objects/backup.lzz
Normal file
138
src/objects/backup.lzz
Normal file
@ -0,0 +1,138 @@
|
||||
class Backup : public node::ObjectWrap {
|
||||
public:
|
||||
|
||||
INIT(Init) {
|
||||
v8::Local<v8::FunctionTemplate> t = NewConstructorTemplate(isolate, data, JS_new, "Backup");
|
||||
SetPrototypeMethod(isolate, data, t, "transfer", JS_transfer);
|
||||
SetPrototypeMethod(isolate, data, t, "close", JS_close);
|
||||
return t->GetFunction(OnlyContext).ToLocalChecked();
|
||||
}
|
||||
|
||||
// Used to support ordered containers.
|
||||
static inline bool Compare(Backup const * const a, Backup const * const b) {
|
||||
return a->id < b->id;
|
||||
}
|
||||
|
||||
// Whenever this is used, db->RemoveBackup must be invoked beforehand.
|
||||
void CloseHandles() {
|
||||
if (alive) {
|
||||
alive = false;
|
||||
std::string filename(sqlite3_db_filename(dest_handle, "main"));
|
||||
sqlite3_backup_finish(backup_handle);
|
||||
int status = sqlite3_close(dest_handle);
|
||||
assert(status == SQLITE_OK); ((void)status);
|
||||
if (unlink) remove(filename.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
~Backup() {
|
||||
if (alive) db->RemoveBackup(this);
|
||||
CloseHandles();
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
explicit Backup(
|
||||
Database* db,
|
||||
sqlite3* dest_handle,
|
||||
sqlite3_backup* backup_handle,
|
||||
sqlite3_uint64 id,
|
||||
bool unlink
|
||||
) :
|
||||
node::ObjectWrap(),
|
||||
db(db),
|
||||
dest_handle(dest_handle),
|
||||
backup_handle(backup_handle),
|
||||
id(id),
|
||||
alive(true),
|
||||
unlink(unlink) {
|
||||
assert(db != NULL);
|
||||
assert(dest_handle != NULL);
|
||||
assert(backup_handle != NULL);
|
||||
db->AddBackup(this);
|
||||
}
|
||||
|
||||
NODE_METHOD(JS_new) {
|
||||
UseAddon;
|
||||
if (!addon->privileged_info) return ThrowTypeError("Disabled constructor");
|
||||
assert(info.IsConstructCall());
|
||||
Database* db = Unwrap<Database>(addon->privileged_info->This());
|
||||
REQUIRE_DATABASE_OPEN(db->GetState());
|
||||
REQUIRE_DATABASE_NOT_BUSY(db->GetState());
|
||||
|
||||
v8::Local<v8::Object> database = (*addon->privileged_info)[0].As<v8::Object>();
|
||||
v8::Local<v8::String> attachedName = (*addon->privileged_info)[1].As<v8::String>();
|
||||
v8::Local<v8::String> destFile = (*addon->privileged_info)[2].As<v8::String>();
|
||||
bool unlink = (*addon->privileged_info)[3].As<v8::Boolean>()->Value();
|
||||
|
||||
UseIsolate;
|
||||
sqlite3* dest_handle;
|
||||
v8::String::Utf8Value dest_file(isolate, destFile);
|
||||
v8::String::Utf8Value attached_name(isolate, attachedName);
|
||||
int mask = (SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE);
|
||||
|
||||
if (sqlite3_open_v2(*dest_file, &dest_handle, mask, NULL) != SQLITE_OK) {
|
||||
Database::ThrowSqliteError(addon, dest_handle);
|
||||
int status = sqlite3_close(dest_handle);
|
||||
assert(status == SQLITE_OK); ((void)status);
|
||||
return;
|
||||
}
|
||||
|
||||
sqlite3_extended_result_codes(dest_handle, 1);
|
||||
sqlite3_limit(dest_handle, SQLITE_LIMIT_LENGTH, INT_MAX);
|
||||
sqlite3_backup* backup_handle = sqlite3_backup_init(dest_handle, "main", db->GetHandle(), *attached_name);
|
||||
if (backup_handle == NULL) {
|
||||
Database::ThrowSqliteError(addon, dest_handle);
|
||||
int status = sqlite3_close(dest_handle);
|
||||
assert(status == SQLITE_OK); ((void)status);
|
||||
return;
|
||||
}
|
||||
|
||||
Backup* backup = new Backup(db, dest_handle, backup_handle, addon->NextId(), unlink);
|
||||
backup->Wrap(info.This());
|
||||
SetFrozen(isolate, OnlyContext, info.This(), addon->cs.database, database);
|
||||
|
||||
info.GetReturnValue().Set(info.This());
|
||||
}
|
||||
|
||||
NODE_METHOD(JS_transfer) {
|
||||
Backup* backup = Unwrap<Backup>(info.This());
|
||||
REQUIRE_ARGUMENT_INT32(first, int pages);
|
||||
REQUIRE_DATABASE_OPEN(backup->db->GetState());
|
||||
assert(backup->db->GetState()->busy == false);
|
||||
assert(backup->alive == true);
|
||||
|
||||
sqlite3_backup* backup_handle = backup->backup_handle;
|
||||
int status = sqlite3_backup_step(backup_handle, pages) & 0xff;
|
||||
|
||||
Addon* addon = backup->db->GetAddon();
|
||||
if (status == SQLITE_OK || status == SQLITE_DONE || status == SQLITE_BUSY) {
|
||||
int total_pages = sqlite3_backup_pagecount(backup_handle);
|
||||
int remaining_pages = sqlite3_backup_remaining(backup_handle);
|
||||
UseIsolate;
|
||||
UseContext;
|
||||
v8::Local<v8::Object> result = v8::Object::New(isolate);
|
||||
result->Set(ctx, addon->cs.totalPages.Get(isolate), v8::Int32::New(isolate, total_pages)).FromJust();
|
||||
result->Set(ctx, addon->cs.remainingPages.Get(isolate), v8::Int32::New(isolate, remaining_pages)).FromJust();
|
||||
info.GetReturnValue().Set(result);
|
||||
if (status == SQLITE_DONE) backup->unlink = false;
|
||||
} else {
|
||||
Database::ThrowSqliteError(addon, sqlite3_errstr(status), status);
|
||||
}
|
||||
}
|
||||
|
||||
NODE_METHOD(JS_close) {
|
||||
Backup* backup = Unwrap<Backup>(info.This());
|
||||
assert(backup->db->GetState()->busy == false);
|
||||
if (backup->alive) backup->db->RemoveBackup(backup);
|
||||
backup->CloseHandles();
|
||||
info.GetReturnValue().Set(info.This());
|
||||
}
|
||||
|
||||
Database* const db;
|
||||
sqlite3* const dest_handle;
|
||||
sqlite3_backup* const backup_handle;
|
||||
const sqlite3_uint64 id;
|
||||
bool alive;
|
||||
bool unlink;
|
||||
};
|
||||
570
src/objects/database.lzz
Normal file
570
src/objects/database.lzz
Normal file
@ -0,0 +1,570 @@
|
||||
class Database : public node::ObjectWrap {
|
||||
public:
|
||||
|
||||
INIT(Init) {
|
||||
v8::Local<v8::FunctionTemplate> t = NewConstructorTemplate(isolate, data, JS_new, "Database");
|
||||
SetPrototypeMethod(isolate, data, t, "prepare", JS_prepare);
|
||||
SetPrototypeMethod(isolate, data, t, "exec", JS_exec);
|
||||
SetPrototypeMethod(isolate, data, t, "backup", JS_backup);
|
||||
SetPrototypeMethod(isolate, data, t, "serialize", JS_serialize);
|
||||
SetPrototypeMethod(isolate, data, t, "function", JS_function);
|
||||
SetPrototypeMethod(isolate, data, t, "aggregate", JS_aggregate);
|
||||
SetPrototypeMethod(isolate, data, t, "table", JS_table);
|
||||
SetPrototypeMethod(isolate, data, t, "loadExtension", JS_loadExtension);
|
||||
SetPrototypeMethod(isolate, data, t, "close", JS_close);
|
||||
SetPrototypeMethod(isolate, data, t, "defaultSafeIntegers", JS_defaultSafeIntegers);
|
||||
SetPrototypeMethod(isolate, data, t, "unsafeMode", JS_unsafeMode);
|
||||
SetPrototypeMethod(isolate, data, t, "createFTS5Tokenizer", JS_createFTS5Tokenizer);
|
||||
SetPrototypeMethod(isolate, data, t, "signalTokenize", JS_signalTokenize);
|
||||
SetPrototypeGetter(isolate, data, t, "open", JS_open);
|
||||
SetPrototypeGetter(isolate, data, t, "inTransaction", JS_inTransaction);
|
||||
return t->GetFunction(OnlyContext).ToLocalChecked();
|
||||
}
|
||||
|
||||
// Used to support ordered containers.
|
||||
class CompareDatabase { public:
|
||||
bool operator() (Database const * const a, Database const * const b) const {
|
||||
return a < b;
|
||||
}
|
||||
};
|
||||
class CompareStatement { public:
|
||||
bool operator() (Statement const * const a, Statement const * const b) const {
|
||||
return Statement::Compare(a, b);
|
||||
}
|
||||
};
|
||||
class CompareBackup { public:
|
||||
bool operator() (Backup const * const a, Backup const * const b) const {
|
||||
return Backup::Compare(a, b);
|
||||
}
|
||||
};
|
||||
|
||||
// Proper error handling logic for when an sqlite3 operation fails.
|
||||
void ThrowDatabaseError() {
|
||||
if (was_js_error) was_js_error = false;
|
||||
else ThrowSqliteError(addon, db_handle);
|
||||
}
|
||||
static void ThrowSqliteError(Addon* addon, sqlite3* db_handle) {
|
||||
assert(db_handle != NULL);
|
||||
ThrowSqliteError(addon, sqlite3_errmsg(db_handle), sqlite3_extended_errcode(db_handle));
|
||||
}
|
||||
static void ThrowSqliteError(Addon* addon, const char* message, int code) {
|
||||
assert(message != NULL);
|
||||
assert((code & 0xff) != SQLITE_OK);
|
||||
assert((code & 0xff) != SQLITE_ROW);
|
||||
assert((code & 0xff) != SQLITE_DONE);
|
||||
EasyIsolate;
|
||||
v8::Local<v8::Value> args[2] = {
|
||||
StringFromUtf8(isolate, message, -1),
|
||||
addon->cs.Code(isolate, code)
|
||||
};
|
||||
isolate->ThrowException(addon->SqliteError.Get(isolate)
|
||||
->NewInstance(OnlyContext, 2, args)
|
||||
.ToLocalChecked());
|
||||
}
|
||||
|
||||
// Allows Statements to log their executed SQL.
|
||||
bool Log(v8::Isolate* isolate, sqlite3_stmt* handle) {
|
||||
assert(was_js_error == false);
|
||||
if (!has_logger) return false;
|
||||
char* expanded = sqlite3_expanded_sql(handle);
|
||||
v8::Local<v8::Value> arg = StringFromUtf8(isolate, expanded ? expanded : sqlite3_sql(handle), -1);
|
||||
was_js_error = logger.Get(isolate).As<v8::Function>()
|
||||
->Call(OnlyContext, v8::Undefined(isolate), 1, &arg)
|
||||
.IsEmpty();
|
||||
if (expanded) sqlite3_free(expanded);
|
||||
return was_js_error;
|
||||
}
|
||||
|
||||
// Allow Statements to manage themselves when created and garbage collected.
|
||||
inline void AddStatement(Statement* stmt) { stmts.insert(stmts.end(), stmt); }
|
||||
inline void RemoveStatement(Statement* stmt) { stmts.erase(stmt); }
|
||||
|
||||
// Allow Backups to manage themselves when created and garbage collected.
|
||||
inline void AddBackup(Backup* backup) { backups.insert(backups.end(), backup); }
|
||||
inline void RemoveBackup(Backup* backup) { backups.erase(backup); }
|
||||
|
||||
// A view for Statements to see and modify Database state.
|
||||
// The order of these fields must exactly match their actual order.
|
||||
struct State {
|
||||
const bool open;
|
||||
bool busy;
|
||||
const bool safe_ints;
|
||||
const bool unsafe_mode;
|
||||
bool was_js_error;
|
||||
const bool has_logger;
|
||||
unsigned short iterators;
|
||||
Addon* const addon;
|
||||
};
|
||||
inline State* GetState() {
|
||||
return reinterpret_cast<State*>(&open);
|
||||
}
|
||||
inline sqlite3* GetHandle() {
|
||||
return db_handle;
|
||||
}
|
||||
inline Addon* GetAddon() {
|
||||
return addon;
|
||||
}
|
||||
|
||||
// Whenever this is used, addon->dbs.erase() must be invoked beforehand.
|
||||
void CloseHandles() {
|
||||
if (open) {
|
||||
open = false;
|
||||
for (Statement* stmt : stmts) stmt->CloseHandles();
|
||||
for (Backup* backup : backups) backup->CloseHandles();
|
||||
stmts.clear();
|
||||
backups.clear();
|
||||
int status = sqlite3_close(db_handle);
|
||||
assert(status == SQLITE_OK); ((void)status);
|
||||
}
|
||||
}
|
||||
|
||||
~Database() {
|
||||
if (open) addon->dbs.erase(this);
|
||||
CloseHandles();
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
explicit Database(
|
||||
v8::Isolate* isolate,
|
||||
Addon* addon,
|
||||
sqlite3* db_handle,
|
||||
v8::Local<v8::Value> logger
|
||||
) :
|
||||
node::ObjectWrap(),
|
||||
db_handle(db_handle),
|
||||
open(true),
|
||||
busy(false),
|
||||
safe_ints(false),
|
||||
unsafe_mode(false),
|
||||
was_js_error(false),
|
||||
has_logger(logger->IsFunction()),
|
||||
iterators(0),
|
||||
addon(addon),
|
||||
logger(isolate, logger),
|
||||
stmts(),
|
||||
backups() {
|
||||
assert(db_handle != NULL);
|
||||
addon->dbs.insert(this);
|
||||
}
|
||||
|
||||
fts5_api* GetFTS5API() {
|
||||
// Get fts5_api object
|
||||
int rc;
|
||||
sqlite3_stmt *pStmt = nullptr;
|
||||
|
||||
rc = sqlite3_prepare(db_handle, "SELECT fts5(?1)", -1, &pStmt, 0);
|
||||
if (rc != SQLITE_OK) {
|
||||
ThrowSqliteError(addon, db_handle);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
fts5_api *fts5 = nullptr;
|
||||
sqlite3_bind_pointer(pStmt, 1, (void*)&fts5, "fts5_api_ptr", nullptr);
|
||||
sqlite3_step(pStmt);
|
||||
rc = sqlite3_finalize(pStmt);
|
||||
if (rc != SQLITE_OK) {
|
||||
ThrowSqliteError(addon, db_handle);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
assert(fts5 != nullptr);
|
||||
return fts5;
|
||||
}
|
||||
|
||||
NODE_METHOD(JS_new) {
|
||||
assert(info.IsConstructCall());
|
||||
REQUIRE_ARGUMENT_STRING(first, v8::Local<v8::String> filename);
|
||||
REQUIRE_ARGUMENT_STRING(second, v8::Local<v8::String> filenameGiven);
|
||||
REQUIRE_ARGUMENT_BOOLEAN(third, bool in_memory);
|
||||
REQUIRE_ARGUMENT_BOOLEAN(fourth, bool readonly);
|
||||
REQUIRE_ARGUMENT_BOOLEAN(fifth, bool must_exist);
|
||||
REQUIRE_ARGUMENT_INT32(sixth, int timeout);
|
||||
REQUIRE_ARGUMENT_ANY(seventh, v8::Local<v8::Value> logger);
|
||||
REQUIRE_ARGUMENT_ANY(eighth, v8::Local<v8::Value> buffer);
|
||||
|
||||
UseAddon;
|
||||
UseIsolate;
|
||||
sqlite3* db_handle;
|
||||
v8::String::Utf8Value utf8(isolate, filename);
|
||||
int mask = readonly ? SQLITE_OPEN_READONLY
|
||||
: must_exist ? SQLITE_OPEN_READWRITE
|
||||
: (SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE);
|
||||
|
||||
if (sqlite3_open_v2(*utf8, &db_handle, mask, NULL) != SQLITE_OK) {
|
||||
ThrowSqliteError(addon, db_handle);
|
||||
int status = sqlite3_close(db_handle);
|
||||
assert(status == SQLITE_OK); ((void)status);
|
||||
return;
|
||||
}
|
||||
|
||||
assert(sqlite3_db_mutex(db_handle) == NULL);
|
||||
sqlite3_extended_result_codes(db_handle, 1);
|
||||
sqlite3_busy_timeout(db_handle, timeout);
|
||||
sqlite3_limit(db_handle, SQLITE_LIMIT_LENGTH, MAX_BUFFER_SIZE < MAX_STRING_SIZE ? MAX_BUFFER_SIZE : MAX_STRING_SIZE);
|
||||
sqlite3_limit(db_handle, SQLITE_LIMIT_SQL_LENGTH, MAX_STRING_SIZE);
|
||||
int status = sqlite3_db_config(db_handle, SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION, 1, NULL);
|
||||
assert(status == SQLITE_OK);
|
||||
status = sqlite3_db_config(db_handle, SQLITE_DBCONFIG_DEFENSIVE, 1, NULL);
|
||||
assert(status == SQLITE_OK);
|
||||
|
||||
if (node::Buffer::HasInstance(buffer) && !Deserialize(buffer.As<v8::Object>(), addon, db_handle, readonly)) {
|
||||
int status = sqlite3_close(db_handle);
|
||||
assert(status == SQLITE_OK); ((void)status);
|
||||
return;
|
||||
}
|
||||
|
||||
UseContext;
|
||||
Database* db = new Database(isolate, addon, db_handle, logger);
|
||||
db->Wrap(info.This());
|
||||
SetFrozen(isolate, ctx, info.This(), addon->cs.memory, v8::Boolean::New(isolate, in_memory));
|
||||
SetFrozen(isolate, ctx, info.This(), addon->cs.readonly, v8::Boolean::New(isolate, readonly));
|
||||
SetFrozen(isolate, ctx, info.This(), addon->cs.name, filenameGiven);
|
||||
|
||||
// Add ICU tokenizer
|
||||
fts5_api* fts5 = db->GetFTS5API();
|
||||
// Already threw an exception
|
||||
if (fts5 == nullptr) {
|
||||
return;
|
||||
}
|
||||
SignalTokenizerModule* icu = new SignalTokenizerModule();
|
||||
fts5->xCreateTokenizer(fts5, "signal_tokenizer", icu, icu->get_api_object(),
|
||||
&SignalTokenizerModule::xDestroy);
|
||||
|
||||
info.GetReturnValue().Set(info.This());
|
||||
}
|
||||
|
||||
NODE_METHOD(JS_prepare) {
|
||||
REQUIRE_ARGUMENT_STRING(first, v8::Local<v8::String> source);
|
||||
REQUIRE_ARGUMENT_OBJECT(second, v8::Local<v8::Object> database);
|
||||
REQUIRE_ARGUMENT_BOOLEAN(third, bool pragmaMode);
|
||||
(void)source;
|
||||
(void)database;
|
||||
(void)pragmaMode;
|
||||
UseAddon;
|
||||
UseIsolate;
|
||||
v8::Local<v8::Function> c = addon->Statement.Get(isolate);
|
||||
addon->privileged_info = &info;
|
||||
v8::MaybeLocal<v8::Object> maybeStatement = c->NewInstance(OnlyContext, 0, NULL);
|
||||
addon->privileged_info = NULL;
|
||||
if (!maybeStatement.IsEmpty()) info.GetReturnValue().Set(maybeStatement.ToLocalChecked());
|
||||
}
|
||||
|
||||
NODE_METHOD(JS_exec) {
|
||||
Database* db = Unwrap<Database>(info.This());
|
||||
REQUIRE_ARGUMENT_STRING(first, v8::Local<v8::String> source);
|
||||
REQUIRE_DATABASE_OPEN(db);
|
||||
REQUIRE_DATABASE_NOT_BUSY(db);
|
||||
REQUIRE_DATABASE_NO_ITERATORS_UNLESS_UNSAFE(db);
|
||||
db->busy = true;
|
||||
|
||||
UseIsolate;
|
||||
v8::String::Utf8Value utf8(isolate, source);
|
||||
const char* sql = *utf8;
|
||||
const char* tail;
|
||||
|
||||
int status;
|
||||
const bool has_logger = db->has_logger;
|
||||
sqlite3* const db_handle = db->db_handle;
|
||||
sqlite3_stmt* handle;
|
||||
|
||||
for (;;) {
|
||||
while (IS_SKIPPED(*sql)) ++sql;
|
||||
status = sqlite3_prepare_v2(db_handle, sql, -1, &handle, &tail);
|
||||
sql = tail;
|
||||
if (!handle) break;
|
||||
if (has_logger && db->Log(isolate, handle)) {
|
||||
sqlite3_finalize(handle);
|
||||
status = -1;
|
||||
break;
|
||||
}
|
||||
do status = sqlite3_step(handle);
|
||||
while (status == SQLITE_ROW);
|
||||
status = sqlite3_finalize(handle);
|
||||
if (status != SQLITE_OK) break;
|
||||
}
|
||||
|
||||
db->busy = false;
|
||||
if (status != SQLITE_OK) {
|
||||
db->ThrowDatabaseError();
|
||||
}
|
||||
}
|
||||
|
||||
NODE_METHOD(JS_backup) {
|
||||
REQUIRE_ARGUMENT_OBJECT(first, v8::Local<v8::Object> database);
|
||||
REQUIRE_ARGUMENT_STRING(second, v8::Local<v8::String> attachedName);
|
||||
REQUIRE_ARGUMENT_STRING(third, v8::Local<v8::String> destFile);
|
||||
REQUIRE_ARGUMENT_BOOLEAN(fourth, bool unlink);
|
||||
(void)database;
|
||||
(void)attachedName;
|
||||
(void)destFile;
|
||||
(void)unlink;
|
||||
UseAddon;
|
||||
UseIsolate;
|
||||
v8::Local<v8::Function> c = addon->Backup.Get(isolate);
|
||||
addon->privileged_info = &info;
|
||||
v8::MaybeLocal<v8::Object> maybeBackup = c->NewInstance(OnlyContext, 0, NULL);
|
||||
addon->privileged_info = NULL;
|
||||
if (!maybeBackup.IsEmpty()) info.GetReturnValue().Set(maybeBackup.ToLocalChecked());
|
||||
}
|
||||
|
||||
NODE_METHOD(JS_serialize) {
|
||||
Database* db = Unwrap<Database>(info.This());
|
||||
REQUIRE_ARGUMENT_STRING(first, v8::Local<v8::String> attachedName);
|
||||
REQUIRE_DATABASE_OPEN(db);
|
||||
REQUIRE_DATABASE_NOT_BUSY(db);
|
||||
REQUIRE_DATABASE_NO_ITERATORS(db);
|
||||
|
||||
UseIsolate;
|
||||
v8::String::Utf8Value attached_name(isolate, attachedName);
|
||||
sqlite3_int64 length = -1;
|
||||
unsigned char* data = sqlite3_serialize(db->db_handle, *attached_name, &length, 0);
|
||||
|
||||
if (!data && length) {
|
||||
ThrowError("Out of memory");
|
||||
return;
|
||||
}
|
||||
|
||||
info.GetReturnValue().Set(
|
||||
node::Buffer::New(isolate, reinterpret_cast<char*>(data), length, FreeSerialization, NULL).ToLocalChecked()
|
||||
);
|
||||
}
|
||||
|
||||
NODE_METHOD(JS_function) {
|
||||
Database* db = Unwrap<Database>(info.This());
|
||||
REQUIRE_ARGUMENT_FUNCTION(first, v8::Local<v8::Function> fn);
|
||||
REQUIRE_ARGUMENT_STRING(second, v8::Local<v8::String> nameString);
|
||||
REQUIRE_ARGUMENT_INT32(third, int argc);
|
||||
REQUIRE_ARGUMENT_INT32(fourth, int safe_ints);
|
||||
REQUIRE_ARGUMENT_BOOLEAN(fifth, bool deterministic);
|
||||
REQUIRE_ARGUMENT_BOOLEAN(sixth, bool direct_only);
|
||||
REQUIRE_DATABASE_OPEN(db);
|
||||
REQUIRE_DATABASE_NOT_BUSY(db);
|
||||
REQUIRE_DATABASE_NO_ITERATORS(db);
|
||||
|
||||
UseIsolate;
|
||||
v8::String::Utf8Value name(isolate, nameString);
|
||||
int mask = SQLITE_UTF8;
|
||||
if (deterministic) mask |= SQLITE_DETERMINISTIC;
|
||||
if (direct_only) mask |= SQLITE_DIRECTONLY;
|
||||
safe_ints = safe_ints < 2 ? safe_ints : static_cast<int>(db->safe_ints);
|
||||
|
||||
if (sqlite3_create_function_v2(db->db_handle, *name, argc, mask, new CustomFunction(isolate, db, *name, fn, safe_ints), CustomFunction::xFunc, NULL, NULL, CustomFunction::xDestroy) != SQLITE_OK) {
|
||||
db->ThrowDatabaseError();
|
||||
}
|
||||
}
|
||||
|
||||
NODE_METHOD(JS_aggregate) {
|
||||
Database* db = Unwrap<Database>(info.This());
|
||||
REQUIRE_ARGUMENT_ANY(first, v8::Local<v8::Value> start);
|
||||
REQUIRE_ARGUMENT_FUNCTION(second, v8::Local<v8::Function> step);
|
||||
REQUIRE_ARGUMENT_ANY(third, v8::Local<v8::Value> inverse);
|
||||
REQUIRE_ARGUMENT_ANY(fourth, v8::Local<v8::Value> result);
|
||||
REQUIRE_ARGUMENT_STRING(fifth, v8::Local<v8::String> nameString);
|
||||
REQUIRE_ARGUMENT_INT32(sixth, int argc);
|
||||
REQUIRE_ARGUMENT_INT32(seventh, int safe_ints);
|
||||
REQUIRE_ARGUMENT_BOOLEAN(eighth, bool deterministic);
|
||||
REQUIRE_ARGUMENT_BOOLEAN(ninth, bool direct_only);
|
||||
REQUIRE_DATABASE_OPEN(db);
|
||||
REQUIRE_DATABASE_NOT_BUSY(db);
|
||||
REQUIRE_DATABASE_NO_ITERATORS(db);
|
||||
|
||||
UseIsolate;
|
||||
v8::String::Utf8Value name(isolate, nameString);
|
||||
auto xInverse = inverse->IsFunction() ? CustomAggregate::xInverse : NULL;
|
||||
auto xValue = xInverse ? CustomAggregate::xValue : NULL;
|
||||
int mask = SQLITE_UTF8;
|
||||
if (deterministic) mask |= SQLITE_DETERMINISTIC;
|
||||
if (direct_only) mask |= SQLITE_DIRECTONLY;
|
||||
safe_ints = safe_ints < 2 ? safe_ints : static_cast<int>(db->safe_ints);
|
||||
|
||||
if (sqlite3_create_window_function(db->db_handle, *name, argc, mask, new CustomAggregate(isolate, db, *name, start, step, inverse, result, safe_ints), CustomAggregate::xStep, CustomAggregate::xFinal, xValue, xInverse, CustomAggregate::xDestroy) != SQLITE_OK) {
|
||||
db->ThrowDatabaseError();
|
||||
}
|
||||
}
|
||||
|
||||
NODE_METHOD(JS_table) {
|
||||
Database* db = Unwrap<Database>(info.This());
|
||||
REQUIRE_ARGUMENT_FUNCTION(first, v8::Local<v8::Function> factory);
|
||||
REQUIRE_ARGUMENT_STRING(second, v8::Local<v8::String> nameString);
|
||||
REQUIRE_ARGUMENT_BOOLEAN(third, bool eponymous);
|
||||
REQUIRE_DATABASE_OPEN(db);
|
||||
REQUIRE_DATABASE_NOT_BUSY(db);
|
||||
REQUIRE_DATABASE_NO_ITERATORS(db);
|
||||
|
||||
UseIsolate;
|
||||
v8::String::Utf8Value name(isolate, nameString);
|
||||
sqlite3_module* module = eponymous ? &CustomTable::EPONYMOUS_MODULE : &CustomTable::MODULE;
|
||||
|
||||
db->busy = true;
|
||||
if (sqlite3_create_module_v2(db->db_handle, *name, module, new CustomTable(isolate, db, *name, factory), CustomTable::Destructor) != SQLITE_OK) {
|
||||
db->ThrowDatabaseError();
|
||||
}
|
||||
db->busy = false;
|
||||
}
|
||||
|
||||
NODE_METHOD(JS_loadExtension) {
|
||||
Database* db = Unwrap<Database>(info.This());
|
||||
v8::Local<v8::String> entryPoint;
|
||||
REQUIRE_ARGUMENT_STRING(first, v8::Local<v8::String> filename);
|
||||
if (info.Length() > 1) { REQUIRE_ARGUMENT_STRING(second, entryPoint); }
|
||||
REQUIRE_DATABASE_OPEN(db);
|
||||
REQUIRE_DATABASE_NOT_BUSY(db);
|
||||
REQUIRE_DATABASE_NO_ITERATORS(db);
|
||||
UseIsolate;
|
||||
char* error;
|
||||
int status = sqlite3_load_extension(
|
||||
db->db_handle,
|
||||
*v8::String::Utf8Value(isolate, filename),
|
||||
entryPoint.IsEmpty() ? NULL : *v8::String::Utf8Value(isolate, entryPoint),
|
||||
&error
|
||||
);
|
||||
if (status != SQLITE_OK) {
|
||||
ThrowSqliteError(db->addon, error, status);
|
||||
}
|
||||
sqlite3_free(error);
|
||||
}
|
||||
|
||||
NODE_METHOD(JS_close) {
|
||||
Database* db = Unwrap<Database>(info.This());
|
||||
if (db->open) {
|
||||
REQUIRE_DATABASE_NOT_BUSY(db);
|
||||
REQUIRE_DATABASE_NO_ITERATORS(db);
|
||||
db->addon->dbs.erase(db);
|
||||
db->CloseHandles();
|
||||
}
|
||||
}
|
||||
|
||||
NODE_METHOD(JS_defaultSafeIntegers) {
|
||||
Database* db = Unwrap<Database>(info.This());
|
||||
if (info.Length() == 0) db->safe_ints = true;
|
||||
else { REQUIRE_ARGUMENT_BOOLEAN(first, db->safe_ints); }
|
||||
}
|
||||
|
||||
NODE_METHOD(JS_unsafeMode) {
|
||||
Database* db = Unwrap<Database>(info.This());
|
||||
if (info.Length() == 0) db->unsafe_mode = true;
|
||||
else { REQUIRE_ARGUMENT_BOOLEAN(first, db->unsafe_mode); }
|
||||
sqlite3_db_config(db->db_handle, SQLITE_DBCONFIG_DEFENSIVE, static_cast<int>(!db->unsafe_mode), NULL);
|
||||
}
|
||||
|
||||
NODE_METHOD(JS_createFTS5Tokenizer) {
|
||||
UseIsolate;
|
||||
|
||||
Database* db = Unwrap<Database>(info.This());
|
||||
REQUIRE_ARGUMENT_STRING(first, v8::Local<v8::String> name);
|
||||
REQUIRE_ARGUMENT_FUNCTION(second, v8::Local<v8::Function> create_instance_fn);
|
||||
|
||||
TokenizerModule* t = new TokenizerModule(isolate, create_instance_fn);
|
||||
|
||||
v8::String::Utf8Value utf8(isolate, name);
|
||||
fts5_api* fts5 = db->GetFTS5API();
|
||||
// Already threw an exception
|
||||
if (fts5 == nullptr) {
|
||||
return;
|
||||
}
|
||||
fts5->xCreateTokenizer(fts5, *utf8, t, t->get_api_object(),
|
||||
&TokenizerModule::xDestroy);
|
||||
}
|
||||
|
||||
static int SignalTokenizeCallback(
|
||||
void *tokensPtr,
|
||||
int _flags,
|
||||
const char *token,
|
||||
int len,
|
||||
int _start,
|
||||
int _end
|
||||
) {
|
||||
std::vector<std::string>* tokens =
|
||||
reinterpret_cast<std::vector<std::string>*>(tokensPtr);
|
||||
tokens->push_back(std::string(token, len));
|
||||
return SQLITE_OK;
|
||||
}
|
||||
|
||||
NODE_METHOD(JS_signalTokenize) {
|
||||
UseAddon;
|
||||
UseIsolate;
|
||||
UseContext;
|
||||
|
||||
REQUIRE_ARGUMENT_STRING(first, v8::Local<v8::String> value);
|
||||
|
||||
v8::String::Utf8Value utf8(isolate, value);
|
||||
|
||||
std::vector<std::string> tokens;
|
||||
int status = signal_fts5_tokenize(
|
||||
nullptr,
|
||||
reinterpret_cast<void*>(&tokens),
|
||||
0,
|
||||
*utf8,
|
||||
utf8.length(),
|
||||
SignalTokenizeCallback);
|
||||
if (status != SQLITE_OK) {
|
||||
ThrowSqliteError(addon, "Enable to tokenize string", status);
|
||||
return;
|
||||
}
|
||||
|
||||
v8::Local<v8::Array> result = v8::Array::New(isolate);
|
||||
|
||||
int i = 0;
|
||||
for (auto &str: tokens) {
|
||||
result->Set(ctx, i++, StringFromUtf8(isolate, str.c_str(), str.length()))
|
||||
.FromJust();
|
||||
}
|
||||
|
||||
info.GetReturnValue().Set(result);
|
||||
}
|
||||
|
||||
NODE_GETTER(JS_open) {
|
||||
info.GetReturnValue().Set(Unwrap<Database>(info.This())->open);
|
||||
}
|
||||
|
||||
NODE_GETTER(JS_inTransaction) {
|
||||
Database* db = Unwrap<Database>(info.This());
|
||||
info.GetReturnValue().Set(db->open && !static_cast<bool>(sqlite3_get_autocommit(db->db_handle)));
|
||||
}
|
||||
|
||||
static bool Deserialize(v8::Local<v8::Object> buffer, Addon* addon, sqlite3* db_handle, bool readonly) {
|
||||
size_t length = node::Buffer::Length(buffer);
|
||||
unsigned char* data = (unsigned char*)sqlite3_malloc64(length);
|
||||
unsigned int flags = SQLITE_DESERIALIZE_FREEONCLOSE | SQLITE_DESERIALIZE_RESIZEABLE;
|
||||
|
||||
if (readonly) {
|
||||
flags |= SQLITE_DESERIALIZE_READONLY;
|
||||
}
|
||||
if (length) {
|
||||
if (!data) {
|
||||
ThrowError("Out of memory");
|
||||
return false;
|
||||
}
|
||||
memcpy(data, node::Buffer::Data(buffer), length);
|
||||
}
|
||||
|
||||
int status = sqlite3_deserialize(db_handle, "main", data, length, length, flags);
|
||||
if (status != SQLITE_OK) {
|
||||
ThrowSqliteError(addon, status == SQLITE_ERROR ? "unable to deserialize database" : sqlite3_errstr(status), status);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static void FreeSerialization(char* data, void* _) {
|
||||
sqlite3_free(data);
|
||||
}
|
||||
|
||||
static const int MAX_BUFFER_SIZE = node::Buffer::kMaxLength > INT_MAX ? INT_MAX : static_cast<int>(node::Buffer::kMaxLength);
|
||||
static const int MAX_STRING_SIZE = v8::String::kMaxLength > INT_MAX ? INT_MAX : static_cast<int>(v8::String::kMaxLength);
|
||||
|
||||
sqlite3* const db_handle;
|
||||
bool open;
|
||||
bool busy;
|
||||
bool safe_ints;
|
||||
bool unsafe_mode;
|
||||
bool was_js_error;
|
||||
const bool has_logger;
|
||||
unsigned short iterators;
|
||||
Addon* const addon;
|
||||
const CopyablePersistent<v8::Value> logger;
|
||||
std::set<Statement*, CompareStatement> stmts;
|
||||
std::set<Backup*, CompareBackup> backups;
|
||||
};
|
||||
30
src/objects/signal-tokenizer.lzz
Normal file
30
src/objects/signal-tokenizer.lzz
Normal file
@ -0,0 +1,30 @@
|
||||
class SignalTokenizerModule {
|
||||
public:
|
||||
SignalTokenizerModule() {}
|
||||
|
||||
static void xDestroy(void* pCtx) {
|
||||
// No-op
|
||||
}
|
||||
|
||||
inline fts5_tokenizer* get_api_object() {
|
||||
return &api_object;
|
||||
}
|
||||
|
||||
private:
|
||||
static int xCreate(
|
||||
void* pCtx, const char** azArg, int nArg, Fts5Tokenizer** ppOut) {
|
||||
TokenizerModule* m = static_cast<TokenizerModule*>(pCtx);
|
||||
*ppOut = reinterpret_cast<Fts5Tokenizer*>(m);
|
||||
return SQLITE_OK;
|
||||
}
|
||||
|
||||
static void xDelete(Fts5Tokenizer* tokenizer) {
|
||||
// No-op
|
||||
}
|
||||
|
||||
static fts5_tokenizer api_object = {
|
||||
&xCreate,
|
||||
&xDelete,
|
||||
signal_fts5_tokenize,
|
||||
};
|
||||
};
|
||||
139
src/objects/statement-iterator.lzz
Normal file
139
src/objects/statement-iterator.lzz
Normal file
@ -0,0 +1,139 @@
|
||||
class StatementIterator : public node::ObjectWrap {
|
||||
public:
|
||||
|
||||
INIT(Init) {
|
||||
v8::Local<v8::FunctionTemplate> t = NewConstructorTemplate(isolate, data, JS_new, "StatementIterator");
|
||||
SetPrototypeMethod(isolate, data, t, "next", JS_next);
|
||||
SetPrototypeMethod(isolate, data, t, "return", JS_return);
|
||||
SetPrototypeSymbolMethod(isolate, data, t, v8::Symbol::GetIterator(isolate), JS_symbolIterator);
|
||||
return t->GetFunction(OnlyContext).ToLocalChecked();
|
||||
}
|
||||
|
||||
// The ~Statement destructor currently covers any state this object creates.
|
||||
// Additionally, we actually DON'T want to revert stmt->locked or db_state
|
||||
// ->iterators in this destructor, to ensure deterministic database access.
|
||||
~StatementIterator() {}
|
||||
|
||||
private:
|
||||
|
||||
explicit StatementIterator(Statement* stmt, bool bound) : node::ObjectWrap(),
|
||||
stmt(stmt),
|
||||
handle(stmt->handle),
|
||||
db_state(stmt->db->GetState()),
|
||||
bound(bound),
|
||||
safe_ints(stmt->safe_ints),
|
||||
mode(stmt->mode),
|
||||
alive(true),
|
||||
logged(!db_state->has_logger) {
|
||||
assert(stmt != NULL);
|
||||
assert(handle != NULL);
|
||||
assert(stmt->bound == bound);
|
||||
assert(stmt->alive == true);
|
||||
assert(stmt->locked == false);
|
||||
assert(db_state->iterators < USHRT_MAX);
|
||||
stmt->locked = true;
|
||||
db_state->iterators += 1;
|
||||
}
|
||||
|
||||
NODE_METHOD(JS_new) {
|
||||
UseAddon;
|
||||
if (!addon->privileged_info) return ThrowTypeError("Disabled constructor");
|
||||
assert(info.IsConstructCall());
|
||||
|
||||
StatementIterator* iter;
|
||||
{
|
||||
NODE_ARGUMENTS info = *addon->privileged_info;
|
||||
STATEMENT_START_LOGIC(REQUIRE_STATEMENT_RETURNS_DATA, DOES_ADD_ITERATOR);
|
||||
iter = new StatementIterator(stmt, bound);
|
||||
}
|
||||
UseIsolate;
|
||||
UseContext;
|
||||
iter->Wrap(info.This());
|
||||
SetFrozen(isolate, ctx, info.This(), addon->cs.statement, addon->privileged_info->This());
|
||||
|
||||
info.GetReturnValue().Set(info.This());
|
||||
}
|
||||
|
||||
NODE_METHOD(JS_next) {
|
||||
StatementIterator* iter = Unwrap<StatementIterator>(info.This());
|
||||
REQUIRE_DATABASE_NOT_BUSY(iter->db_state);
|
||||
if (iter->alive) iter->Next(info);
|
||||
else info.GetReturnValue().Set(DoneRecord(OnlyIsolate, iter->db_state->addon));
|
||||
}
|
||||
|
||||
NODE_METHOD(JS_return) {
|
||||
StatementIterator* iter = Unwrap<StatementIterator>(info.This());
|
||||
REQUIRE_DATABASE_NOT_BUSY(iter->db_state);
|
||||
if (iter->alive) iter->Return(info);
|
||||
else info.GetReturnValue().Set(DoneRecord(OnlyIsolate, iter->db_state->addon));
|
||||
}
|
||||
|
||||
NODE_METHOD(JS_symbolIterator) {
|
||||
info.GetReturnValue().Set(info.This());
|
||||
}
|
||||
|
||||
void Next(NODE_ARGUMENTS info) {
|
||||
assert(alive == true);
|
||||
db_state->busy = true;
|
||||
if (!logged) {
|
||||
logged = true;
|
||||
if (stmt->db->Log(OnlyIsolate, handle)) {
|
||||
db_state->busy = false;
|
||||
Throw();
|
||||
return;
|
||||
}
|
||||
}
|
||||
int status = sqlite3_step(handle);
|
||||
db_state->busy = false;
|
||||
if (status == SQLITE_ROW) {
|
||||
std::vector<v8::Local<v8::Name> > keys;
|
||||
UseIsolate;
|
||||
UseContext;
|
||||
info.GetReturnValue().Set(
|
||||
NewRecord(isolate, ctx, Data::GetRowJS(isolate, ctx, handle, safe_ints, mode, keys), db_state->addon, false)
|
||||
);
|
||||
} else {
|
||||
if (status == SQLITE_DONE) Return(info);
|
||||
else Throw();
|
||||
}
|
||||
}
|
||||
|
||||
void Return(NODE_ARGUMENTS info) {
|
||||
Cleanup();
|
||||
STATEMENT_RETURN_LOGIC(DoneRecord(OnlyIsolate, db_state->addon));
|
||||
}
|
||||
|
||||
void Throw() {
|
||||
Cleanup();
|
||||
Database* db = stmt->db;
|
||||
STATEMENT_THROW_LOGIC();
|
||||
}
|
||||
|
||||
void Cleanup() {
|
||||
assert(alive == true);
|
||||
alive = false;
|
||||
stmt->locked = false;
|
||||
db_state->iterators -= 1;
|
||||
sqlite3_reset(handle);
|
||||
}
|
||||
|
||||
static inline v8::Local<v8::Object> NewRecord(v8::Isolate* isolate, v8::Local<v8::Context> ctx, v8::Local<v8::Value> value, Addon* addon, bool done) {
|
||||
v8::Local<v8::Object> record = v8::Object::New(isolate);
|
||||
record->Set(ctx, addon->cs.value.Get(isolate), value).FromJust();
|
||||
record->Set(ctx, addon->cs.done.Get(isolate), v8::Boolean::New(isolate, done)).FromJust();
|
||||
return record;
|
||||
}
|
||||
|
||||
static inline v8::Local<v8::Object> DoneRecord(v8::Isolate* isolate, Addon* addon) {
|
||||
return NewRecord(isolate, OnlyContext, v8::Undefined(isolate), addon, true);
|
||||
}
|
||||
|
||||
Statement* const stmt;
|
||||
sqlite3_stmt* const handle;
|
||||
Database::State* const db_state;
|
||||
const bool bound;
|
||||
const bool safe_ints;
|
||||
const char mode;
|
||||
bool alive;
|
||||
bool logged;
|
||||
};
|
||||
331
src/objects/statement.lzz
Normal file
331
src/objects/statement.lzz
Normal file
@ -0,0 +1,331 @@
|
||||
class Statement : public node::ObjectWrap { friend class StatementIterator;
|
||||
public:
|
||||
|
||||
INIT(Init) {
|
||||
v8::Local<v8::FunctionTemplate> t = NewConstructorTemplate(isolate, data, JS_new, "Statement");
|
||||
SetPrototypeMethod(isolate, data, t, "run", JS_run);
|
||||
SetPrototypeMethod(isolate, data, t, "get", JS_get);
|
||||
SetPrototypeMethod(isolate, data, t, "all", JS_all);
|
||||
SetPrototypeMethod(isolate, data, t, "iterate", JS_iterate);
|
||||
SetPrototypeMethod(isolate, data, t, "bind", JS_bind);
|
||||
SetPrototypeMethod(isolate, data, t, "pluck", JS_pluck);
|
||||
SetPrototypeMethod(isolate, data, t, "expand", JS_expand);
|
||||
SetPrototypeMethod(isolate, data, t, "raw", JS_raw);
|
||||
SetPrototypeMethod(isolate, data, t, "safeIntegers", JS_safeIntegers);
|
||||
SetPrototypeMethod(isolate, data, t, "columns", JS_columns);
|
||||
SetPrototypeGetter(isolate, data, t, "busy", JS_busy);
|
||||
return t->GetFunction(OnlyContext).ToLocalChecked();
|
||||
}
|
||||
|
||||
// Used to support ordered containers.
|
||||
static inline bool Compare(Statement const * const a, Statement const * const b) {
|
||||
return a->extras->id < b->extras->id;
|
||||
}
|
||||
|
||||
// Returns the Statement's bind map (creates it upon first execution).
|
||||
BindMap* GetBindMap(v8::Isolate* isolate) {
|
||||
if (has_bind_map) return &extras->bind_map;
|
||||
BindMap* bind_map = &extras->bind_map;
|
||||
int param_count = sqlite3_bind_parameter_count(handle);
|
||||
for (int i = 1; i <= param_count; ++i) {
|
||||
const char* name = sqlite3_bind_parameter_name(handle, i);
|
||||
if (name != NULL) bind_map->Add(isolate, name + 1, i);
|
||||
}
|
||||
has_bind_map = true;
|
||||
return bind_map;
|
||||
}
|
||||
|
||||
// Whenever this is used, db->RemoveStatement must be invoked beforehand.
|
||||
void CloseHandles() {
|
||||
if (alive) {
|
||||
alive = false;
|
||||
sqlite3_finalize(handle);
|
||||
}
|
||||
}
|
||||
|
||||
~Statement() {
|
||||
if (alive) db->RemoveStatement(this);
|
||||
CloseHandles();
|
||||
delete extras;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
// A class for holding values that are less often used.
|
||||
class Extras { friend class Statement;
|
||||
explicit Extras(sqlite3_uint64 id) : bind_map(0), id(id) {}
|
||||
BindMap bind_map;
|
||||
const sqlite3_uint64 id;
|
||||
};
|
||||
|
||||
explicit Statement(
|
||||
Database* db,
|
||||
sqlite3_stmt* handle,
|
||||
sqlite3_uint64 id,
|
||||
bool returns_data
|
||||
) :
|
||||
node::ObjectWrap(),
|
||||
db(db),
|
||||
handle(handle),
|
||||
extras(new Extras(id)),
|
||||
alive(true),
|
||||
locked(false),
|
||||
bound(false),
|
||||
has_bind_map(false),
|
||||
safe_ints(db->GetState()->safe_ints),
|
||||
mode(Data::FLAT),
|
||||
returns_data(returns_data) {
|
||||
assert(db != NULL);
|
||||
assert(handle != NULL);
|
||||
assert(db->GetState()->open);
|
||||
assert(!db->GetState()->busy);
|
||||
db->AddStatement(this);
|
||||
}
|
||||
|
||||
NODE_METHOD(JS_new) {
|
||||
UseAddon;
|
||||
if (!addon->privileged_info) {
|
||||
return ThrowTypeError("Statements can only be constructed by the db.prepare() method");
|
||||
}
|
||||
assert(info.IsConstructCall());
|
||||
Database* db = Unwrap<Database>(addon->privileged_info->This());
|
||||
REQUIRE_DATABASE_OPEN(db->GetState());
|
||||
REQUIRE_DATABASE_NOT_BUSY(db->GetState());
|
||||
|
||||
v8::Local<v8::String> source = (*addon->privileged_info)[0].As<v8::String>();
|
||||
v8::Local<v8::Object> database = (*addon->privileged_info)[1].As<v8::Object>();
|
||||
bool pragmaMode = (*addon->privileged_info)[2].As<v8::Boolean>()->Value();
|
||||
int flags = SQLITE_PREPARE_PERSISTENT;
|
||||
|
||||
if (pragmaMode) {
|
||||
REQUIRE_DATABASE_NO_ITERATORS_UNLESS_UNSAFE(db->GetState());
|
||||
flags = 0;
|
||||
}
|
||||
|
||||
UseIsolate;
|
||||
v8::String::Utf8Value utf8(isolate, source);
|
||||
sqlite3_stmt* handle;
|
||||
const char* tail;
|
||||
|
||||
if (sqlite3_prepare_v3(db->GetHandle(), *utf8, utf8.length() + 1, flags, &handle, &tail) != SQLITE_OK) {
|
||||
return db->ThrowDatabaseError();
|
||||
}
|
||||
if (handle == NULL) {
|
||||
return ThrowRangeError("The supplied SQL string contains no statements");
|
||||
}
|
||||
for (char c; (c = *tail); ++tail) {
|
||||
if (IS_SKIPPED(c)) continue;
|
||||
if (c == '/' && tail[1] == '*') {
|
||||
tail += 2;
|
||||
for (char c; (c = *tail); ++tail) {
|
||||
if (c == '*' && tail[1] == '/') {
|
||||
tail += 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else if (c == '-' && tail[1] == '-') {
|
||||
tail += 2;
|
||||
for (char c; (c = *tail); ++tail) {
|
||||
if (c == '\n') break;
|
||||
}
|
||||
} else {
|
||||
sqlite3_finalize(handle);
|
||||
return ThrowRangeError("The supplied SQL string contains more than one statement");
|
||||
}
|
||||
}
|
||||
|
||||
UseContext;
|
||||
bool returns_data = sqlite3_column_count(handle) >= 1 || pragmaMode;
|
||||
Statement* stmt = new Statement(db, handle, addon->NextId(), returns_data);
|
||||
stmt->Wrap(info.This());
|
||||
SetFrozen(isolate, ctx, info.This(), addon->cs.reader, v8::Boolean::New(isolate, returns_data));
|
||||
SetFrozen(isolate, ctx, info.This(), addon->cs.readonly, v8::Boolean::New(isolate, sqlite3_stmt_readonly(handle) != 0));
|
||||
SetFrozen(isolate, ctx, info.This(), addon->cs.source, source);
|
||||
SetFrozen(isolate, ctx, info.This(), addon->cs.database, database);
|
||||
|
||||
info.GetReturnValue().Set(info.This());
|
||||
}
|
||||
|
||||
NODE_METHOD(JS_run) {
|
||||
STATEMENT_START(ALLOW_ANY_STATEMENT, DOES_MUTATE);
|
||||
sqlite3* db_handle = db->GetHandle();
|
||||
int total_changes_before = sqlite3_total_changes(db_handle);
|
||||
|
||||
sqlite3_step(handle);
|
||||
if (sqlite3_reset(handle) == SQLITE_OK) {
|
||||
int changes = sqlite3_total_changes(db_handle) == total_changes_before ? 0 : sqlite3_changes(db_handle);
|
||||
sqlite3_int64 id = sqlite3_last_insert_rowid(db_handle);
|
||||
Addon* addon = db->GetAddon();
|
||||
UseContext;
|
||||
v8::Local<v8::Object> result = v8::Object::New(isolate);
|
||||
result->Set(ctx, addon->cs.changes.Get(isolate), v8::Int32::New(isolate, changes)).FromJust();
|
||||
result->Set(ctx, addon->cs.lastInsertRowid.Get(isolate),
|
||||
stmt->safe_ints
|
||||
? v8::BigInt::New(isolate, id).As<v8::Value>()
|
||||
: v8::Number::New(isolate, (double)id).As<v8::Value>()
|
||||
).FromJust();
|
||||
STATEMENT_RETURN(result);
|
||||
}
|
||||
STATEMENT_THROW();
|
||||
}
|
||||
|
||||
NODE_METHOD(JS_get) {
|
||||
STATEMENT_START(REQUIRE_STATEMENT_RETURNS_DATA, DOES_NOT_MUTATE);
|
||||
int status = sqlite3_step(handle);
|
||||
if (status == SQLITE_ROW) {
|
||||
std::vector<v8::Local<v8::Name> > keys;
|
||||
v8::Local<v8::Value> result = Data::GetRowJS(isolate, OnlyContext, handle, stmt->safe_ints, stmt->mode, keys);
|
||||
sqlite3_reset(handle);
|
||||
STATEMENT_RETURN(result);
|
||||
} else if (status == SQLITE_DONE) {
|
||||
sqlite3_reset(handle);
|
||||
STATEMENT_RETURN(v8::Undefined(isolate));
|
||||
}
|
||||
sqlite3_reset(handle);
|
||||
STATEMENT_THROW();
|
||||
}
|
||||
|
||||
NODE_METHOD(JS_all) {
|
||||
STATEMENT_START(REQUIRE_STATEMENT_RETURNS_DATA, DOES_NOT_MUTATE);
|
||||
UseContext;
|
||||
v8::Local<v8::Array> result = v8::Array::New(isolate, 0);
|
||||
uint32_t row_count = 0;
|
||||
const bool safe_ints = stmt->safe_ints;
|
||||
const char mode = stmt->mode;
|
||||
bool js_error = false;
|
||||
std::vector<v8::Local<v8::Name> > keys;
|
||||
|
||||
while (sqlite3_step(handle) == SQLITE_ROW) {
|
||||
if (row_count == 0xffffffff) { ThrowRangeError("Array overflow (too many rows returned)"); js_error = true; break; }
|
||||
result->Set(ctx, row_count++, Data::GetRowJS(isolate, ctx, handle, safe_ints, mode, keys)).FromJust();
|
||||
}
|
||||
|
||||
if (sqlite3_reset(handle) == SQLITE_OK && !js_error) {
|
||||
STATEMENT_RETURN(result);
|
||||
}
|
||||
if (js_error) db->GetState()->was_js_error = true;
|
||||
STATEMENT_THROW();
|
||||
}
|
||||
|
||||
NODE_METHOD(JS_iterate) {
|
||||
UseAddon;
|
||||
UseIsolate;
|
||||
v8::Local<v8::Function> c = addon->StatementIterator.Get(isolate);
|
||||
addon->privileged_info = &info;
|
||||
v8::MaybeLocal<v8::Object> maybeIterator = c->NewInstance(OnlyContext, 0, NULL);
|
||||
addon->privileged_info = NULL;
|
||||
if (!maybeIterator.IsEmpty()) info.GetReturnValue().Set(maybeIterator.ToLocalChecked());
|
||||
}
|
||||
|
||||
NODE_METHOD(JS_bind) {
|
||||
Statement* stmt = Unwrap<Statement>(info.This());
|
||||
if (stmt->bound) return ThrowTypeError("The bind() method can only be invoked once per statement object");
|
||||
REQUIRE_DATABASE_OPEN(stmt->db->GetState());
|
||||
REQUIRE_DATABASE_NOT_BUSY(stmt->db->GetState());
|
||||
REQUIRE_STATEMENT_NOT_LOCKED(stmt);
|
||||
STATEMENT_BIND(stmt->handle);
|
||||
stmt->bound = true;
|
||||
info.GetReturnValue().Set(info.This());
|
||||
}
|
||||
|
||||
NODE_METHOD(JS_pluck) {
|
||||
Statement* stmt = Unwrap<Statement>(info.This());
|
||||
if (!stmt->returns_data) return ThrowTypeError("The pluck() method is only for statements that return data");
|
||||
REQUIRE_DATABASE_NOT_BUSY(stmt->db->GetState());
|
||||
REQUIRE_STATEMENT_NOT_LOCKED(stmt);
|
||||
bool use = true;
|
||||
if (info.Length() != 0) { REQUIRE_ARGUMENT_BOOLEAN(first, use); }
|
||||
stmt->mode = use ? Data::PLUCK : stmt->mode == Data::PLUCK ? Data::FLAT : stmt->mode;
|
||||
info.GetReturnValue().Set(info.This());
|
||||
}
|
||||
|
||||
NODE_METHOD(JS_expand) {
|
||||
Statement* stmt = Unwrap<Statement>(info.This());
|
||||
if (!stmt->returns_data) return ThrowTypeError("The expand() method is only for statements that return data");
|
||||
REQUIRE_DATABASE_NOT_BUSY(stmt->db->GetState());
|
||||
REQUIRE_STATEMENT_NOT_LOCKED(stmt);
|
||||
bool use = true;
|
||||
if (info.Length() != 0) { REQUIRE_ARGUMENT_BOOLEAN(first, use); }
|
||||
stmt->mode = use ? Data::EXPAND : stmt->mode == Data::EXPAND ? Data::FLAT : stmt->mode;
|
||||
info.GetReturnValue().Set(info.This());
|
||||
}
|
||||
|
||||
NODE_METHOD(JS_raw) {
|
||||
Statement* stmt = Unwrap<Statement>(info.This());
|
||||
if (!stmt->returns_data) return ThrowTypeError("The raw() method is only for statements that return data");
|
||||
REQUIRE_DATABASE_NOT_BUSY(stmt->db->GetState());
|
||||
REQUIRE_STATEMENT_NOT_LOCKED(stmt);
|
||||
bool use = true;
|
||||
if (info.Length() != 0) { REQUIRE_ARGUMENT_BOOLEAN(first, use); }
|
||||
stmt->mode = use ? Data::RAW : stmt->mode == Data::RAW ? Data::FLAT : stmt->mode;
|
||||
info.GetReturnValue().Set(info.This());
|
||||
}
|
||||
|
||||
NODE_METHOD(JS_safeIntegers) {
|
||||
Statement* stmt = Unwrap<Statement>(info.This());
|
||||
REQUIRE_DATABASE_NOT_BUSY(stmt->db->GetState());
|
||||
REQUIRE_STATEMENT_NOT_LOCKED(stmt);
|
||||
if (info.Length() == 0) stmt->safe_ints = true;
|
||||
else { REQUIRE_ARGUMENT_BOOLEAN(first, stmt->safe_ints); }
|
||||
info.GetReturnValue().Set(info.This());
|
||||
}
|
||||
|
||||
NODE_METHOD(JS_columns) {
|
||||
Statement* stmt = Unwrap<Statement>(info.This());
|
||||
if (!stmt->returns_data) return ThrowTypeError("The columns() method is only for statements that return data");
|
||||
REQUIRE_DATABASE_OPEN(stmt->db->GetState());
|
||||
REQUIRE_DATABASE_NOT_BUSY(stmt->db->GetState());
|
||||
Addon* addon = stmt->db->GetAddon();
|
||||
UseIsolate;
|
||||
UseContext;
|
||||
|
||||
int column_count = sqlite3_column_count(stmt->handle);
|
||||
v8::Local<v8::Array> columns = v8::Array::New(isolate);
|
||||
|
||||
v8::Local<v8::String> name = addon->cs.name.Get(isolate);
|
||||
v8::Local<v8::String> columnName = addon->cs.column.Get(isolate);
|
||||
v8::Local<v8::String> tableName = addon->cs.table.Get(isolate);
|
||||
v8::Local<v8::String> databaseName = addon->cs.database.Get(isolate);
|
||||
v8::Local<v8::String> typeName = addon->cs.type.Get(isolate);
|
||||
|
||||
for (int i = 0; i < column_count; ++i) {
|
||||
v8::Local<v8::Object> column = v8::Object::New(isolate);
|
||||
|
||||
column->Set(ctx, name,
|
||||
InternalizedFromUtf8OrNull(isolate, sqlite3_column_name(stmt->handle, i), -1)
|
||||
).FromJust();
|
||||
column->Set(ctx, columnName,
|
||||
InternalizedFromUtf8OrNull(isolate, sqlite3_column_origin_name(stmt->handle, i), -1)
|
||||
).FromJust();
|
||||
column->Set(ctx, tableName,
|
||||
InternalizedFromUtf8OrNull(isolate, sqlite3_column_table_name(stmt->handle, i), -1)
|
||||
).FromJust();
|
||||
column->Set(ctx, databaseName,
|
||||
InternalizedFromUtf8OrNull(isolate, sqlite3_column_database_name(stmt->handle, i), -1)
|
||||
).FromJust();
|
||||
column->Set(ctx, typeName,
|
||||
InternalizedFromUtf8OrNull(isolate, sqlite3_column_decltype(stmt->handle, i), -1)
|
||||
).FromJust();
|
||||
|
||||
columns->Set(ctx, i, column).FromJust();
|
||||
}
|
||||
|
||||
info.GetReturnValue().Set(columns);
|
||||
}
|
||||
|
||||
NODE_GETTER(JS_busy) {
|
||||
Statement* stmt = Unwrap<Statement>(info.This());
|
||||
info.GetReturnValue().Set(stmt->alive && stmt->locked);
|
||||
}
|
||||
|
||||
Database* const db;
|
||||
sqlite3_stmt* const handle;
|
||||
Extras* const extras;
|
||||
bool alive;
|
||||
bool locked;
|
||||
bool bound;
|
||||
bool has_bind_map;
|
||||
bool safe_ints;
|
||||
char mode;
|
||||
const bool returns_data;
|
||||
};
|
||||
158
src/objects/tokenizer.lzz
Normal file
158
src/objects/tokenizer.lzz
Normal file
@ -0,0 +1,158 @@
|
||||
class Tokenizer {
|
||||
public:
|
||||
Tokenizer(
|
||||
v8::Isolate* isolate,
|
||||
v8::Local<v8::Function> run_fn
|
||||
): isolate(isolate),
|
||||
run_fn(isolate, run_fn) {
|
||||
}
|
||||
|
||||
~Tokenizer() {}
|
||||
|
||||
int Run(
|
||||
void* pCtx,
|
||||
const char *pText,
|
||||
int nText,
|
||||
int (*xToken)(
|
||||
void* pCtx, int tflags, const char* pToken, int nToken,
|
||||
int iStart, int iEnd)
|
||||
) {
|
||||
v8::HandleScope scope(isolate);
|
||||
UseContext;
|
||||
|
||||
v8::Local<v8::Value> arg[] = {
|
||||
StringFromUtf8(isolate, pText, nText)
|
||||
};
|
||||
v8::Local<v8::Value> result = run_fn.Get(isolate)->Call(
|
||||
ctx,
|
||||
v8::Undefined(isolate),
|
||||
1,
|
||||
arg).ToLocalChecked();
|
||||
if (!result->IsArray()) {
|
||||
ThrowTypeError("Expected array return value of tokenizer");
|
||||
return SQLITE_MISUSE;
|
||||
}
|
||||
v8::Local<v8::Array> indices = result.As<v8::Array>();
|
||||
int len = indices->Length();
|
||||
if (len % 3 != 0) {
|
||||
return SQLITE_MISUSE;
|
||||
}
|
||||
for (int i = 0; i < len; i += 3) {
|
||||
int64_t segment_start =
|
||||
indices->Get(ctx, i).ToLocalChecked()->IntegerValue(ctx).ToChecked();
|
||||
int64_t segment_end =
|
||||
indices->Get(ctx, i + 1).ToLocalChecked()->IntegerValue(ctx).ToChecked();
|
||||
v8::Local<v8::Value> maybe_normalized =
|
||||
indices->Get(ctx, i + 2).ToLocalChecked();
|
||||
if (segment_start < 0 || static_cast<int64_t>(segment_start) > nText) {
|
||||
return SQLITE_MISUSE;
|
||||
}
|
||||
if (segment_end < 0 || static_cast<int64_t>(segment_end) > nText) {
|
||||
return SQLITE_MISUSE;
|
||||
}
|
||||
if (segment_start > segment_end) {
|
||||
return SQLITE_MISUSE;
|
||||
}
|
||||
|
||||
int rc;
|
||||
if (maybe_normalized->IsString()) {
|
||||
v8::String::Utf8Value normalized(
|
||||
isolate, indices->Get(ctx, i + 2).ToLocalChecked());
|
||||
rc = xToken(
|
||||
pCtx, 0, *normalized, normalized.length(),
|
||||
segment_start, segment_end);
|
||||
} else {
|
||||
// Optimization: if `maybe_normalized` is not provided - use original
|
||||
// input string to avoid copying data.
|
||||
rc = xToken(
|
||||
pCtx, 0, &pText[segment_start], segment_end - segment_start,
|
||||
segment_start, segment_end);
|
||||
}
|
||||
|
||||
if (rc != SQLITE_OK) {
|
||||
return rc;
|
||||
}
|
||||
}
|
||||
return SQLITE_OK;
|
||||
}
|
||||
|
||||
private:
|
||||
v8::Isolate* isolate;
|
||||
const CopyablePersistent<v8::Function> run_fn;
|
||||
}
|
||||
|
||||
class TokenizerModule {
|
||||
public:
|
||||
TokenizerModule(
|
||||
v8::Isolate* isolate,
|
||||
v8::Local<v8::Function> create_instance_fn
|
||||
): isolate(isolate), create_instance_fn(isolate, create_instance_fn) {
|
||||
}
|
||||
|
||||
static void xDestroy(void* pCtx) {
|
||||
TokenizerModule* m = static_cast<TokenizerModule*>(pCtx);
|
||||
delete m;
|
||||
}
|
||||
|
||||
inline fts5_tokenizer* get_api_object() {
|
||||
return &api_object;
|
||||
}
|
||||
|
||||
private:
|
||||
Tokenizer* CreateInstance(const char** azArg, int nArg) {
|
||||
v8::HandleScope scope(isolate);
|
||||
UseContext;
|
||||
|
||||
v8::Local<v8::Array> params = v8::Array::New(isolate, nArg);
|
||||
for (int i = 0; i < nArg; i++) {
|
||||
params->Set(ctx, i, StringFromUtf8(isolate, azArg[i], -1)).ToChecked();
|
||||
}
|
||||
|
||||
v8::Local<v8::Value> arg[] = {
|
||||
params,
|
||||
};
|
||||
v8::Local<v8::Function> run_fn = create_instance_fn.Get(isolate)->Call(
|
||||
ctx,
|
||||
v8::Undefined(isolate),
|
||||
1,
|
||||
arg).ToLocalChecked().As<v8::Function>();
|
||||
|
||||
return new Tokenizer(isolate, run_fn);
|
||||
}
|
||||
|
||||
static int xCreate(
|
||||
void* pCtx, const char** azArg, int nArg, Fts5Tokenizer** ppOut) {
|
||||
TokenizerModule* m = static_cast<TokenizerModule*>(pCtx);
|
||||
*ppOut = reinterpret_cast<Fts5Tokenizer*>(m->CreateInstance(azArg, nArg));
|
||||
return SQLITE_OK;
|
||||
}
|
||||
|
||||
static void xDelete(Fts5Tokenizer* tokenizer) {
|
||||
Tokenizer* t = reinterpret_cast<Tokenizer*>(tokenizer);
|
||||
delete t;
|
||||
}
|
||||
|
||||
static int xTokenize(
|
||||
Fts5Tokenizer* tokenizer,
|
||||
void *pCtx,
|
||||
int flags,
|
||||
const char *pText,
|
||||
int nText,
|
||||
int (*xToken)(
|
||||
void* pCtx, int tflags, const char* pToken, int nToken,
|
||||
int iStart, int iEnd)
|
||||
) {
|
||||
Tokenizer* t = reinterpret_cast<Tokenizer*>(tokenizer);
|
||||
|
||||
return t->Run(pCtx, pText, nText, xToken);
|
||||
}
|
||||
|
||||
static fts5_tokenizer api_object = {
|
||||
&xCreate,
|
||||
&xDelete,
|
||||
&xTokenize,
|
||||
};
|
||||
|
||||
v8::Isolate* isolate;
|
||||
const CopyablePersistent<v8::Function> create_instance_fn;
|
||||
};
|
||||
73
src/util/bind-map.lzz
Normal file
73
src/util/bind-map.lzz
Normal file
@ -0,0 +1,73 @@
|
||||
class BindMap {
|
||||
public:
|
||||
|
||||
// This nested class represents a single mapping between a parameter name
|
||||
// and its associated parameter index in a prepared statement.
|
||||
class Pair { friend class BindMap;
|
||||
public:
|
||||
|
||||
inline int GetIndex() {
|
||||
return index;
|
||||
}
|
||||
|
||||
inline v8::Local<v8::String> GetName(v8::Isolate* isolate) {
|
||||
return name.Get(isolate);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
explicit Pair(v8::Isolate* isolate, const char* name, int index)
|
||||
: name(isolate, InternalizedFromUtf8(isolate, name, -1)), index(index) {}
|
||||
|
||||
explicit Pair(v8::Isolate* isolate, Pair* pair)
|
||||
: name(isolate, pair->name), index(pair->index) {}
|
||||
|
||||
const CopyablePersistent<v8::String> name;
|
||||
const int index;
|
||||
};
|
||||
|
||||
explicit BindMap(char _) {
|
||||
assert(_ == 0);
|
||||
pairs = NULL;
|
||||
capacity = 0;
|
||||
length = 0;
|
||||
}
|
||||
|
||||
~BindMap() {
|
||||
while (length) pairs[--length].~Pair();
|
||||
FREE_ARRAY<Pair>(pairs);
|
||||
}
|
||||
|
||||
inline Pair* GetPairs() {
|
||||
return pairs;
|
||||
}
|
||||
|
||||
inline int GetSize() {
|
||||
return length;
|
||||
}
|
||||
|
||||
// Adds a pair to the bind map, expanding the capacity if necessary.
|
||||
void Add(v8::Isolate* isolate, const char* name, int index) {
|
||||
assert(name != NULL);
|
||||
if (capacity == length) Grow(isolate);
|
||||
new (pairs + length++) Pair(isolate, name, index);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
void Grow(v8::Isolate* isolate) {
|
||||
assert(capacity == length);
|
||||
capacity = (capacity << 1) | 2;
|
||||
Pair* new_pairs = ALLOC_ARRAY<Pair>(capacity);
|
||||
for (int i = 0; i < length; ++i) {
|
||||
new (new_pairs + i) Pair(isolate, pairs + i);
|
||||
pairs[i].~Pair();
|
||||
}
|
||||
FREE_ARRAY<Pair>(pairs);
|
||||
pairs = new_pairs;
|
||||
}
|
||||
|
||||
Pair* pairs;
|
||||
int capacity;
|
||||
int length;
|
||||
};
|
||||
204
src/util/binder.lzz
Normal file
204
src/util/binder.lzz
Normal file
@ -0,0 +1,204 @@
|
||||
class Binder {
|
||||
public:
|
||||
|
||||
explicit Binder(sqlite3_stmt* _handle) {
|
||||
handle = _handle;
|
||||
param_count = sqlite3_bind_parameter_count(_handle);
|
||||
anon_index = 0;
|
||||
success = true;
|
||||
}
|
||||
|
||||
bool Bind(NODE_ARGUMENTS info, int argc, Statement* stmt) {
|
||||
assert(anon_index == 0);
|
||||
Result result = BindArgs(info, argc, stmt);
|
||||
if (success && result.count != param_count) {
|
||||
if (result.count < param_count) {
|
||||
if (!result.bound_object && stmt->GetBindMap(OnlyIsolate)->GetSize()) {
|
||||
Fail(ThrowTypeError, "Missing named parameters");
|
||||
} else {
|
||||
Fail(ThrowRangeError, "Too few parameter values were provided");
|
||||
}
|
||||
} else {
|
||||
Fail(ThrowRangeError, "Too many parameter values were provided");
|
||||
}
|
||||
}
|
||||
return success;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
struct Result {
|
||||
int count;
|
||||
bool bound_object;
|
||||
};
|
||||
|
||||
#hdr
|
||||
static bool IsPlainObject(v8::Isolate* isolate, v8::Local<v8::Object> obj);
|
||||
#end
|
||||
#src
|
||||
static bool IsPlainObject(v8::Isolate* isolate, v8::Local<v8::Object> obj) {
|
||||
v8::Local<v8::Value> proto = obj->GetPrototype();
|
||||
|
||||
#if defined NODE_MODULE_VERSION && NODE_MODULE_VERSION < 93
|
||||
v8::Local<v8::Context> ctx = obj->CreationContext();
|
||||
#else
|
||||
v8::Local<v8::Context> ctx = obj->GetCreationContext().ToLocalChecked();
|
||||
#endif
|
||||
|
||||
ctx->Enter();
|
||||
v8::Local<v8::Value> baseProto = v8::Object::New(isolate)->GetPrototype();
|
||||
ctx->Exit();
|
||||
return proto->StrictEquals(baseProto) || proto->StrictEquals(v8::Null(isolate));
|
||||
}
|
||||
#end
|
||||
|
||||
void Fail(void (*Throw)(const char* _), const char* message) {
|
||||
assert(success == true);
|
||||
assert((Throw == NULL) == (message == NULL));
|
||||
assert(Throw == ThrowError || Throw == ThrowTypeError || Throw == ThrowRangeError || Throw == NULL);
|
||||
if (Throw) Throw(message);
|
||||
success = false;
|
||||
}
|
||||
|
||||
int NextAnonIndex() {
|
||||
while (sqlite3_bind_parameter_name(handle, ++anon_index) != NULL) {}
|
||||
return anon_index;
|
||||
}
|
||||
|
||||
// Binds the value at the given index or throws an appropriate error.
|
||||
void BindValue(v8::Isolate* isolate, v8::Local<v8::Value> value, int index) {
|
||||
int status = Data::BindValueFromJS(isolate, handle, index, value);
|
||||
if (status != SQLITE_OK) {
|
||||
switch (status) {
|
||||
case -1:
|
||||
return Fail(ThrowTypeError, "SQLite3 can only bind numbers, strings, bigints, buffers, and null");
|
||||
case SQLITE_TOOBIG:
|
||||
return Fail(ThrowRangeError, "The bound string, buffer, or bigint is too big");
|
||||
case SQLITE_RANGE:
|
||||
return Fail(ThrowRangeError, "Too many parameter values were provided");
|
||||
case SQLITE_NOMEM:
|
||||
return Fail(ThrowError, "Out of memory");
|
||||
default:
|
||||
return Fail(ThrowError, "An unexpected error occured while trying to bind parameters");
|
||||
}
|
||||
assert(false);
|
||||
}
|
||||
}
|
||||
|
||||
// Binds each value in the array or throws an appropriate error.
|
||||
// The number of successfully bound parameters is returned.
|
||||
int BindArray(v8::Isolate* isolate, v8::Local<v8::Array> arr) {
|
||||
UseContext;
|
||||
uint32_t length = arr->Length();
|
||||
if (length > INT_MAX) {
|
||||
Fail(ThrowRangeError, "Too many parameter values were provided");
|
||||
return 0;
|
||||
}
|
||||
int len = static_cast<int>(length);
|
||||
for (int i = 0; i < len; ++i) {
|
||||
v8::MaybeLocal<v8::Value> maybeValue = arr->Get(ctx, i);
|
||||
if (maybeValue.IsEmpty()) {
|
||||
Fail(NULL, NULL);
|
||||
return i;
|
||||
}
|
||||
BindValue(isolate, maybeValue.ToLocalChecked(), NextAnonIndex());
|
||||
if (!success) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
// Binds all named parameters using the values found in the given object.
|
||||
// The number of successfully bound parameters is returned.
|
||||
// If a named parameter is missing from the object, an error is thrown.
|
||||
// This should only be invoked once per instance.
|
||||
int BindObject(v8::Isolate* isolate, v8::Local<v8::Object> obj, Statement* stmt) {
|
||||
UseContext;
|
||||
BindMap* bind_map = stmt->GetBindMap(isolate);
|
||||
BindMap::Pair* pairs = bind_map->GetPairs();
|
||||
int len = bind_map->GetSize();
|
||||
|
||||
for (int i = 0; i < len; ++i) {
|
||||
v8::Local<v8::String> key = pairs[i].GetName(isolate);
|
||||
|
||||
// Check if the named parameter was provided.
|
||||
v8::Maybe<bool> has_property = obj->HasOwnProperty(ctx, key);
|
||||
if (has_property.IsNothing()) {
|
||||
Fail(NULL, NULL);
|
||||
return i;
|
||||
}
|
||||
if (!has_property.FromJust()) {
|
||||
v8::String::Utf8Value param_name(isolate, key);
|
||||
Fail(ThrowRangeError, (std::string("Missing named parameter \"") + *param_name + "\"").c_str());
|
||||
return i;
|
||||
}
|
||||
|
||||
// Get the current property value.
|
||||
v8::MaybeLocal<v8::Value> maybeValue = obj->Get(ctx, key);
|
||||
if (maybeValue.IsEmpty()) {
|
||||
Fail(NULL, NULL);
|
||||
return i;
|
||||
}
|
||||
|
||||
BindValue(isolate, maybeValue.ToLocalChecked(), pairs[i].GetIndex());
|
||||
if (!success) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
// Binds all parameters using the values found in the arguments object.
|
||||
// Anonymous parameter values can be directly in the arguments object or in an Array.
|
||||
// Named parameter values can be provided in a plain Object argument.
|
||||
// Only one plain Object argument may be provided.
|
||||
// If an error occurs, an appropriate error is thrown.
|
||||
// The return value is a struct indicating how many parameters were successfully bound
|
||||
// and whether or not it tried to bind an object.
|
||||
Result BindArgs(NODE_ARGUMENTS info, int argc, Statement* stmt) {
|
||||
UseIsolate;
|
||||
int count = 0;
|
||||
bool bound_object = false;
|
||||
|
||||
for (int i = 0; i < argc; ++i) {
|
||||
v8::Local<v8::Value> arg = info[i];
|
||||
|
||||
if (arg->IsArray()) {
|
||||
count += BindArray(isolate, arg.As<v8::Array>());
|
||||
if (!success) break;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (arg->IsObject() && !node::Buffer::HasInstance(arg)) {
|
||||
v8::Local<v8::Object> obj = arg.As<v8::Object>();
|
||||
if (IsPlainObject(isolate, obj)) {
|
||||
if (bound_object) {
|
||||
Fail(ThrowTypeError, "You cannot specify named parameters in two different objects");
|
||||
break;
|
||||
}
|
||||
bound_object = true;
|
||||
|
||||
count += BindObject(isolate, obj, stmt);
|
||||
if (!success) break;
|
||||
continue;
|
||||
} else if (stmt->GetBindMap(isolate)->GetSize()) {
|
||||
Fail(ThrowTypeError, "Named parameters can only be passed within plain objects");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
BindValue(isolate, arg, NextAnonIndex());
|
||||
if (!success) break;
|
||||
count += 1;
|
||||
}
|
||||
|
||||
return { count, bound_object };
|
||||
}
|
||||
|
||||
sqlite3_stmt* handle;
|
||||
int param_count;
|
||||
int anon_index; // This value should only be used by NextAnonIndex()
|
||||
bool success; // This value should only be set by Fail()
|
||||
};
|
||||
151
src/util/constants.lzz
Normal file
151
src/util/constants.lzz
Normal file
@ -0,0 +1,151 @@
|
||||
class CS {
|
||||
public:
|
||||
|
||||
v8::Local<v8::String> Code(v8::Isolate* isolate, int code) {
|
||||
auto element = codes.find(code);
|
||||
if (element != codes.end()) return element->second.Get(isolate);
|
||||
return StringFromUtf8(isolate, (std::string("UNKNOWN_SQLITE_ERROR_") + std::to_string(code)).c_str(), -1);
|
||||
}
|
||||
|
||||
explicit CS(v8::Isolate* isolate) {
|
||||
SetString(isolate, database, "database");
|
||||
SetString(isolate, reader, "reader");
|
||||
SetString(isolate, source, "source");
|
||||
SetString(isolate, memory, "memory");
|
||||
SetString(isolate, readonly, "readonly");
|
||||
SetString(isolate, name, "name");
|
||||
SetString(isolate, next, "next");
|
||||
SetString(isolate, length, "length");
|
||||
SetString(isolate, done, "done");
|
||||
SetString(isolate, value, "value");
|
||||
SetString(isolate, changes, "changes");
|
||||
SetString(isolate, lastInsertRowid, "lastInsertRowid");
|
||||
SetString(isolate, statement, "statement");
|
||||
SetString(isolate, column, "column");
|
||||
SetString(isolate, table, "table");
|
||||
SetString(isolate, type, "type");
|
||||
SetString(isolate, totalPages, "totalPages");
|
||||
SetString(isolate, remainingPages, "remainingPages");
|
||||
|
||||
SetCode(isolate, SQLITE_OK, "SQLITE_OK");
|
||||
SetCode(isolate, SQLITE_ERROR, "SQLITE_ERROR");
|
||||
SetCode(isolate, SQLITE_INTERNAL, "SQLITE_INTERNAL");
|
||||
SetCode(isolate, SQLITE_PERM, "SQLITE_PERM");
|
||||
SetCode(isolate, SQLITE_ABORT, "SQLITE_ABORT");
|
||||
SetCode(isolate, SQLITE_BUSY, "SQLITE_BUSY");
|
||||
SetCode(isolate, SQLITE_LOCKED, "SQLITE_LOCKED");
|
||||
SetCode(isolate, SQLITE_NOMEM, "SQLITE_NOMEM");
|
||||
SetCode(isolate, SQLITE_READONLY, "SQLITE_READONLY");
|
||||
SetCode(isolate, SQLITE_INTERRUPT, "SQLITE_INTERRUPT");
|
||||
SetCode(isolate, SQLITE_IOERR, "SQLITE_IOERR");
|
||||
SetCode(isolate, SQLITE_CORRUPT, "SQLITE_CORRUPT");
|
||||
SetCode(isolate, SQLITE_NOTFOUND, "SQLITE_NOTFOUND");
|
||||
SetCode(isolate, SQLITE_FULL, "SQLITE_FULL");
|
||||
SetCode(isolate, SQLITE_CANTOPEN, "SQLITE_CANTOPEN");
|
||||
SetCode(isolate, SQLITE_PROTOCOL, "SQLITE_PROTOCOL");
|
||||
SetCode(isolate, SQLITE_EMPTY, "SQLITE_EMPTY");
|
||||
SetCode(isolate, SQLITE_SCHEMA, "SQLITE_SCHEMA");
|
||||
SetCode(isolate, SQLITE_TOOBIG, "SQLITE_TOOBIG");
|
||||
SetCode(isolate, SQLITE_CONSTRAINT, "SQLITE_CONSTRAINT");
|
||||
SetCode(isolate, SQLITE_MISMATCH, "SQLITE_MISMATCH");
|
||||
SetCode(isolate, SQLITE_MISUSE, "SQLITE_MISUSE");
|
||||
SetCode(isolate, SQLITE_NOLFS, "SQLITE_NOLFS");
|
||||
SetCode(isolate, SQLITE_AUTH, "SQLITE_AUTH");
|
||||
SetCode(isolate, SQLITE_FORMAT, "SQLITE_FORMAT");
|
||||
SetCode(isolate, SQLITE_RANGE, "SQLITE_RANGE");
|
||||
SetCode(isolate, SQLITE_NOTADB, "SQLITE_NOTADB");
|
||||
SetCode(isolate, SQLITE_NOTICE, "SQLITE_NOTICE");
|
||||
SetCode(isolate, SQLITE_WARNING, "SQLITE_WARNING");
|
||||
SetCode(isolate, SQLITE_ROW, "SQLITE_ROW");
|
||||
SetCode(isolate, SQLITE_DONE, "SQLITE_DONE");
|
||||
SetCode(isolate, SQLITE_IOERR_READ, "SQLITE_IOERR_READ");
|
||||
SetCode(isolate, SQLITE_IOERR_SHORT_READ, "SQLITE_IOERR_SHORT_READ");
|
||||
SetCode(isolate, SQLITE_IOERR_WRITE, "SQLITE_IOERR_WRITE");
|
||||
SetCode(isolate, SQLITE_IOERR_FSYNC, "SQLITE_IOERR_FSYNC");
|
||||
SetCode(isolate, SQLITE_IOERR_DIR_FSYNC, "SQLITE_IOERR_DIR_FSYNC");
|
||||
SetCode(isolate, SQLITE_IOERR_TRUNCATE, "SQLITE_IOERR_TRUNCATE");
|
||||
SetCode(isolate, SQLITE_IOERR_FSTAT, "SQLITE_IOERR_FSTAT");
|
||||
SetCode(isolate, SQLITE_IOERR_UNLOCK, "SQLITE_IOERR_UNLOCK");
|
||||
SetCode(isolate, SQLITE_IOERR_RDLOCK, "SQLITE_IOERR_RDLOCK");
|
||||
SetCode(isolate, SQLITE_IOERR_DELETE, "SQLITE_IOERR_DELETE");
|
||||
SetCode(isolate, SQLITE_IOERR_BLOCKED, "SQLITE_IOERR_BLOCKED");
|
||||
SetCode(isolate, SQLITE_IOERR_NOMEM, "SQLITE_IOERR_NOMEM");
|
||||
SetCode(isolate, SQLITE_IOERR_ACCESS, "SQLITE_IOERR_ACCESS");
|
||||
SetCode(isolate, SQLITE_IOERR_CHECKRESERVEDLOCK, "SQLITE_IOERR_CHECKRESERVEDLOCK");
|
||||
SetCode(isolate, SQLITE_IOERR_LOCK, "SQLITE_IOERR_LOCK");
|
||||
SetCode(isolate, SQLITE_IOERR_CLOSE, "SQLITE_IOERR_CLOSE");
|
||||
SetCode(isolate, SQLITE_IOERR_DIR_CLOSE, "SQLITE_IOERR_DIR_CLOSE");
|
||||
SetCode(isolate, SQLITE_IOERR_SHMOPEN, "SQLITE_IOERR_SHMOPEN");
|
||||
SetCode(isolate, SQLITE_IOERR_SHMSIZE, "SQLITE_IOERR_SHMSIZE");
|
||||
SetCode(isolate, SQLITE_IOERR_SHMLOCK, "SQLITE_IOERR_SHMLOCK");
|
||||
SetCode(isolate, SQLITE_IOERR_SHMMAP, "SQLITE_IOERR_SHMMAP");
|
||||
SetCode(isolate, SQLITE_IOERR_SEEK, "SQLITE_IOERR_SEEK");
|
||||
SetCode(isolate, SQLITE_IOERR_DELETE_NOENT, "SQLITE_IOERR_DELETE_NOENT");
|
||||
SetCode(isolate, SQLITE_IOERR_MMAP, "SQLITE_IOERR_MMAP");
|
||||
SetCode(isolate, SQLITE_IOERR_GETTEMPPATH, "SQLITE_IOERR_GETTEMPPATH");
|
||||
SetCode(isolate, SQLITE_IOERR_CONVPATH, "SQLITE_IOERR_CONVPATH");
|
||||
SetCode(isolate, SQLITE_IOERR_VNODE, "SQLITE_IOERR_VNODE");
|
||||
SetCode(isolate, SQLITE_IOERR_AUTH, "SQLITE_IOERR_AUTH");
|
||||
SetCode(isolate, SQLITE_LOCKED_SHAREDCACHE, "SQLITE_LOCKED_SHAREDCACHE");
|
||||
SetCode(isolate, SQLITE_BUSY_RECOVERY, "SQLITE_BUSY_RECOVERY");
|
||||
SetCode(isolate, SQLITE_BUSY_SNAPSHOT, "SQLITE_BUSY_SNAPSHOT");
|
||||
SetCode(isolate, SQLITE_CANTOPEN_NOTEMPDIR, "SQLITE_CANTOPEN_NOTEMPDIR");
|
||||
SetCode(isolate, SQLITE_CANTOPEN_ISDIR, "SQLITE_CANTOPEN_ISDIR");
|
||||
SetCode(isolate, SQLITE_CANTOPEN_FULLPATH, "SQLITE_CANTOPEN_FULLPATH");
|
||||
SetCode(isolate, SQLITE_CANTOPEN_CONVPATH, "SQLITE_CANTOPEN_CONVPATH");
|
||||
SetCode(isolate, SQLITE_CORRUPT_VTAB, "SQLITE_CORRUPT_VTAB");
|
||||
SetCode(isolate, SQLITE_READONLY_RECOVERY, "SQLITE_READONLY_RECOVERY");
|
||||
SetCode(isolate, SQLITE_READONLY_CANTLOCK, "SQLITE_READONLY_CANTLOCK");
|
||||
SetCode(isolate, SQLITE_READONLY_ROLLBACK, "SQLITE_READONLY_ROLLBACK");
|
||||
SetCode(isolate, SQLITE_READONLY_DBMOVED, "SQLITE_READONLY_DBMOVED");
|
||||
SetCode(isolate, SQLITE_ABORT_ROLLBACK, "SQLITE_ABORT_ROLLBACK");
|
||||
SetCode(isolate, SQLITE_CONSTRAINT_CHECK, "SQLITE_CONSTRAINT_CHECK");
|
||||
SetCode(isolate, SQLITE_CONSTRAINT_COMMITHOOK, "SQLITE_CONSTRAINT_COMMITHOOK");
|
||||
SetCode(isolate, SQLITE_CONSTRAINT_FOREIGNKEY, "SQLITE_CONSTRAINT_FOREIGNKEY");
|
||||
SetCode(isolate, SQLITE_CONSTRAINT_FUNCTION, "SQLITE_CONSTRAINT_FUNCTION");
|
||||
SetCode(isolate, SQLITE_CONSTRAINT_NOTNULL, "SQLITE_CONSTRAINT_NOTNULL");
|
||||
SetCode(isolate, SQLITE_CONSTRAINT_PRIMARYKEY, "SQLITE_CONSTRAINT_PRIMARYKEY");
|
||||
SetCode(isolate, SQLITE_CONSTRAINT_TRIGGER, "SQLITE_CONSTRAINT_TRIGGER");
|
||||
SetCode(isolate, SQLITE_CONSTRAINT_UNIQUE, "SQLITE_CONSTRAINT_UNIQUE");
|
||||
SetCode(isolate, SQLITE_CONSTRAINT_VTAB, "SQLITE_CONSTRAINT_VTAB");
|
||||
SetCode(isolate, SQLITE_CONSTRAINT_ROWID, "SQLITE_CONSTRAINT_ROWID");
|
||||
SetCode(isolate, SQLITE_NOTICE_RECOVER_WAL, "SQLITE_NOTICE_RECOVER_WAL");
|
||||
SetCode(isolate, SQLITE_NOTICE_RECOVER_ROLLBACK, "SQLITE_NOTICE_RECOVER_ROLLBACK");
|
||||
SetCode(isolate, SQLITE_WARNING_AUTOINDEX, "SQLITE_WARNING_AUTOINDEX");
|
||||
SetCode(isolate, SQLITE_AUTH_USER, "SQLITE_AUTH_USER");
|
||||
SetCode(isolate, SQLITE_OK_LOAD_PERMANENTLY, "SQLITE_OK_LOAD_PERMANENTLY");
|
||||
}
|
||||
|
||||
CopyablePersistent<v8::String> database;
|
||||
CopyablePersistent<v8::String> reader;
|
||||
CopyablePersistent<v8::String> source;
|
||||
CopyablePersistent<v8::String> memory;
|
||||
CopyablePersistent<v8::String> readonly;
|
||||
CopyablePersistent<v8::String> name;
|
||||
CopyablePersistent<v8::String> next;
|
||||
CopyablePersistent<v8::String> length;
|
||||
CopyablePersistent<v8::String> done;
|
||||
CopyablePersistent<v8::String> value;
|
||||
CopyablePersistent<v8::String> changes;
|
||||
CopyablePersistent<v8::String> lastInsertRowid;
|
||||
CopyablePersistent<v8::String> statement;
|
||||
CopyablePersistent<v8::String> column;
|
||||
CopyablePersistent<v8::String> table;
|
||||
CopyablePersistent<v8::String> type;
|
||||
CopyablePersistent<v8::String> totalPages;
|
||||
CopyablePersistent<v8::String> remainingPages;
|
||||
|
||||
private:
|
||||
|
||||
static void SetString(v8::Isolate* isolate, CopyablePersistent<v8::String>& constant, const char* str) {
|
||||
constant.Reset(isolate, InternalizedFromLatin1(isolate, str));
|
||||
}
|
||||
|
||||
void SetCode(v8::Isolate* isolate, int code, const char* str) {
|
||||
codes.emplace(std::piecewise_construct,
|
||||
std::forward_as_tuple(code),
|
||||
std::forward_as_tuple(isolate, InternalizedFromLatin1(isolate, str)));
|
||||
}
|
||||
|
||||
std::unordered_map<int, CopyablePersistent<v8::String> > codes;
|
||||
};
|
||||
121
src/util/custom-aggregate.lzz
Normal file
121
src/util/custom-aggregate.lzz
Normal file
@ -0,0 +1,121 @@
|
||||
class CustomAggregate : public CustomFunction {
|
||||
public:
|
||||
|
||||
explicit CustomAggregate(
|
||||
v8::Isolate* isolate,
|
||||
Database* db,
|
||||
const char* name,
|
||||
v8::Local<v8::Value> start,
|
||||
v8::Local<v8::Function> step,
|
||||
v8::Local<v8::Value> inverse,
|
||||
v8::Local<v8::Value> result,
|
||||
bool safe_ints
|
||||
) :
|
||||
CustomFunction(isolate, db, name, step, safe_ints),
|
||||
invoke_result(result->IsFunction()),
|
||||
invoke_start(start->IsFunction()),
|
||||
inverse(isolate, inverse->IsFunction() ? inverse.As<v8::Function>() : v8::Local<v8::Function>()),
|
||||
result(isolate, result->IsFunction() ? result.As<v8::Function>() : v8::Local<v8::Function>()),
|
||||
start(isolate, start) {}
|
||||
|
||||
static void xStep(sqlite3_context* invocation, int argc, sqlite3_value** argv) {
|
||||
xStepBase(invocation, argc, argv, &CustomAggregate::fn);
|
||||
}
|
||||
|
||||
static void xInverse(sqlite3_context* invocation, int argc, sqlite3_value** argv) {
|
||||
xStepBase(invocation, argc, argv, &CustomAggregate::inverse);
|
||||
}
|
||||
|
||||
static void xValue(sqlite3_context* invocation) {
|
||||
xValueBase(invocation, false);
|
||||
}
|
||||
|
||||
static void xFinal(sqlite3_context* invocation) {
|
||||
xValueBase(invocation, true);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
static inline void xStepBase(sqlite3_context* invocation, int argc, sqlite3_value** argv, const CopyablePersistent<v8::Function> CustomAggregate::*ptrtm) {
|
||||
AGGREGATE_START();
|
||||
|
||||
v8::Local<v8::Value> args_fast[5];
|
||||
v8::Local<v8::Value>* args = argc <= 4 ? args_fast : ALLOC_ARRAY<v8::Local<v8::Value>>(argc + 1);
|
||||
args[0] = acc->value.Get(isolate);
|
||||
if (argc != 0) Data::GetArgumentsJS(isolate, args + 1, argv, argc, self->safe_ints);
|
||||
|
||||
v8::MaybeLocal<v8::Value> maybeReturnValue = (self->*ptrtm).Get(isolate)->Call(OnlyContext, v8::Undefined(isolate), argc + 1, args);
|
||||
if (args != args_fast) delete[] args;
|
||||
|
||||
if (maybeReturnValue.IsEmpty()) {
|
||||
self->PropagateJSError(invocation);
|
||||
} else {
|
||||
v8::Local<v8::Value> returnValue = maybeReturnValue.ToLocalChecked();
|
||||
if (!returnValue->IsUndefined()) acc->value.Reset(isolate, returnValue);
|
||||
}
|
||||
}
|
||||
|
||||
static inline void xValueBase(sqlite3_context* invocation, bool is_final) {
|
||||
AGGREGATE_START();
|
||||
|
||||
if (!is_final) {
|
||||
acc->is_window = true;
|
||||
} else if (acc->is_window) {
|
||||
DestroyAccumulator(invocation);
|
||||
return;
|
||||
}
|
||||
|
||||
v8::Local<v8::Value> result = acc->value.Get(isolate);
|
||||
if (self->invoke_result) {
|
||||
v8::MaybeLocal<v8::Value> maybeResult = self->result.Get(isolate)->Call(OnlyContext, v8::Undefined(isolate), 1, &result);
|
||||
if (maybeResult.IsEmpty()) {
|
||||
self->PropagateJSError(invocation);
|
||||
return;
|
||||
}
|
||||
result = maybeResult.ToLocalChecked();
|
||||
}
|
||||
|
||||
Data::ResultValueFromJS(isolate, invocation, result, self);
|
||||
if (is_final) DestroyAccumulator(invocation);
|
||||
}
|
||||
|
||||
struct Accumulator { public:
|
||||
CopyablePersistent<v8::Value> value;
|
||||
bool initialized;
|
||||
bool is_window;
|
||||
}
|
||||
|
||||
Accumulator* GetAccumulator(sqlite3_context* invocation) {
|
||||
Accumulator* acc = static_cast<Accumulator*>(sqlite3_aggregate_context(invocation, sizeof(Accumulator)));
|
||||
if (!acc->initialized) {
|
||||
assert(acc->value.IsEmpty());
|
||||
acc->initialized = true;
|
||||
if (invoke_start) {
|
||||
v8::MaybeLocal<v8::Value> maybeSeed = start.Get(isolate).As<v8::Function>()->Call(OnlyContext, v8::Undefined(isolate), 0, NULL);
|
||||
if (maybeSeed.IsEmpty()) PropagateJSError(invocation);
|
||||
else acc->value.Reset(isolate, maybeSeed.ToLocalChecked());
|
||||
} else {
|
||||
assert(!start.IsEmpty());
|
||||
acc->value.Reset(isolate, start);
|
||||
}
|
||||
}
|
||||
return acc;
|
||||
}
|
||||
|
||||
static void DestroyAccumulator(sqlite3_context* invocation) {
|
||||
Accumulator* acc = static_cast<Accumulator*>(sqlite3_aggregate_context(invocation, sizeof(Accumulator)));
|
||||
assert(acc->initialized);
|
||||
acc->value.Reset();
|
||||
}
|
||||
|
||||
void PropagateJSError(sqlite3_context* invocation) {
|
||||
DestroyAccumulator(invocation);
|
||||
CustomFunction::PropagateJSError(invocation);
|
||||
}
|
||||
|
||||
const bool invoke_result;
|
||||
const bool invoke_start;
|
||||
const CopyablePersistent<v8::Function> inverse;
|
||||
const CopyablePersistent<v8::Function> result;
|
||||
const CopyablePersistent<v8::Value> start;
|
||||
};
|
||||
59
src/util/custom-function.lzz
Normal file
59
src/util/custom-function.lzz
Normal file
@ -0,0 +1,59 @@
|
||||
class CustomFunction : protected DataConverter {
|
||||
public:
|
||||
|
||||
explicit CustomFunction(
|
||||
v8::Isolate* isolate,
|
||||
Database* db,
|
||||
const char* name,
|
||||
v8::Local<v8::Function> fn,
|
||||
bool safe_ints
|
||||
) :
|
||||
name(name),
|
||||
db(db),
|
||||
isolate(isolate),
|
||||
fn(isolate, fn),
|
||||
safe_ints(safe_ints) {}
|
||||
|
||||
virtual ~CustomFunction() {}
|
||||
|
||||
static void xDestroy(void* self) {
|
||||
delete static_cast<CustomFunction*>(self);
|
||||
}
|
||||
|
||||
static void xFunc(sqlite3_context* invocation, int argc, sqlite3_value** argv) {
|
||||
FUNCTION_START();
|
||||
|
||||
v8::Local<v8::Value> args_fast[4];
|
||||
v8::Local<v8::Value>* args = NULL;
|
||||
if (argc != 0) {
|
||||
args = argc <= 4 ? args_fast : ALLOC_ARRAY<v8::Local<v8::Value>>(argc);
|
||||
Data::GetArgumentsJS(isolate, args, argv, argc, self->safe_ints);
|
||||
}
|
||||
|
||||
v8::MaybeLocal<v8::Value> maybeReturnValue = self->fn.Get(isolate)->Call(OnlyContext, v8::Undefined(isolate), argc, args);
|
||||
if (args != args_fast) delete[] args;
|
||||
|
||||
if (maybeReturnValue.IsEmpty()) self->PropagateJSError(invocation);
|
||||
else Data::ResultValueFromJS(isolate, invocation, maybeReturnValue.ToLocalChecked(), self);
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
void PropagateJSError(sqlite3_context* invocation) {
|
||||
assert(db->GetState()->was_js_error == false);
|
||||
db->GetState()->was_js_error = true;
|
||||
sqlite3_result_error(invocation, "", 0);
|
||||
}
|
||||
|
||||
std::string GetDataErrorPrefix() {
|
||||
return std::string("User-defined function ") + name + "() returned";
|
||||
}
|
||||
|
||||
private:
|
||||
const std::string name;
|
||||
Database* const db;
|
||||
protected:
|
||||
v8::Isolate* const isolate;
|
||||
const CopyablePersistent<v8::Function> fn;
|
||||
const bool safe_ints;
|
||||
};
|
||||
404
src/util/custom-table.lzz
Normal file
404
src/util/custom-table.lzz
Normal file
@ -0,0 +1,404 @@
|
||||
class CustomTable {
|
||||
public:
|
||||
|
||||
explicit CustomTable(
|
||||
v8::Isolate* isolate,
|
||||
Database* db,
|
||||
const char* name,
|
||||
v8::Local<v8::Function> factory
|
||||
) :
|
||||
addon(db->GetAddon()),
|
||||
isolate(isolate),
|
||||
db(db),
|
||||
name(name),
|
||||
factory(isolate, factory) {}
|
||||
|
||||
static void Destructor(void* self) {
|
||||
delete static_cast<CustomTable*>(self);
|
||||
}
|
||||
|
||||
static sqlite3_module MODULE = {
|
||||
0, /* iVersion */
|
||||
xCreate, /* xCreate */
|
||||
xConnect, /* xConnect */
|
||||
xBestIndex, /* xBestIndex */
|
||||
xDisconnect, /* xDisconnect */
|
||||
xDisconnect, /* xDestroy */
|
||||
xOpen, /* xOpen */
|
||||
xClose, /* xClose */
|
||||
xFilter, /* xFilter */
|
||||
xNext, /* xNext */
|
||||
xEof, /* xEof */
|
||||
xColumn, /* xColumn */
|
||||
xRowid, /* xRowid */
|
||||
NULL, /* xUpdate */
|
||||
NULL, /* xBegin */
|
||||
NULL, /* xSync */
|
||||
NULL, /* xCommit */
|
||||
NULL, /* xRollback */
|
||||
NULL, /* xFindMethod */
|
||||
NULL, /* xRename */
|
||||
NULL, /* xSavepoint */
|
||||
NULL, /* xRelease */
|
||||
NULL, /* xRollbackTo */
|
||||
NULL /* xShadowName */
|
||||
};
|
||||
|
||||
static sqlite3_module EPONYMOUS_MODULE = {
|
||||
0, /* iVersion */
|
||||
NULL, /* xCreate */
|
||||
xConnect, /* xConnect */
|
||||
xBestIndex, /* xBestIndex */
|
||||
xDisconnect, /* xDisconnect */
|
||||
xDisconnect, /* xDestroy */
|
||||
xOpen, /* xOpen */
|
||||
xClose, /* xClose */
|
||||
xFilter, /* xFilter */
|
||||
xNext, /* xNext */
|
||||
xEof, /* xEof */
|
||||
xColumn, /* xColumn */
|
||||
xRowid, /* xRowid */
|
||||
NULL, /* xUpdate */
|
||||
NULL, /* xBegin */
|
||||
NULL, /* xSync */
|
||||
NULL, /* xCommit */
|
||||
NULL, /* xRollback */
|
||||
NULL, /* xFindMethod */
|
||||
NULL, /* xRename */
|
||||
NULL, /* xSavepoint */
|
||||
NULL, /* xRelease */
|
||||
NULL, /* xRollbackTo */
|
||||
NULL /* xShadowName */
|
||||
};
|
||||
|
||||
private:
|
||||
|
||||
// This nested class is instantiated on each CREATE VIRTUAL TABLE statement.
|
||||
class VTab { friend class CustomTable;
|
||||
explicit VTab(
|
||||
CustomTable* parent,
|
||||
v8::Local<v8::Function> generator,
|
||||
std::vector<std::string> parameter_names,
|
||||
bool safe_ints
|
||||
) :
|
||||
parent(parent),
|
||||
parameter_count(parameter_names.size()),
|
||||
safe_ints(safe_ints),
|
||||
generator(parent->isolate, generator),
|
||||
parameter_names(parameter_names) {
|
||||
((void)base);
|
||||
}
|
||||
|
||||
static inline CustomTable::VTab* Upcast(sqlite3_vtab* vtab) {
|
||||
return reinterpret_cast<VTab*>(vtab);
|
||||
}
|
||||
|
||||
inline sqlite3_vtab* Downcast() {
|
||||
return reinterpret_cast<sqlite3_vtab*>(this);
|
||||
}
|
||||
|
||||
sqlite3_vtab base;
|
||||
CustomTable * const parent;
|
||||
const int parameter_count;
|
||||
const bool safe_ints;
|
||||
const CopyablePersistent<v8::Function> generator;
|
||||
const std::vector<std::string> parameter_names;
|
||||
};
|
||||
|
||||
// This nested class is instantiated each time a virtual table is scanned.
|
||||
class Cursor { friend class CustomTable;
|
||||
static inline CustomTable::Cursor* Upcast(sqlite3_vtab_cursor* cursor) {
|
||||
return reinterpret_cast<Cursor*>(cursor);
|
||||
}
|
||||
|
||||
inline sqlite3_vtab_cursor* Downcast() {
|
||||
return reinterpret_cast<sqlite3_vtab_cursor*>(this);
|
||||
}
|
||||
|
||||
inline CustomTable::VTab* GetVTab() {
|
||||
return VTab::Upcast(base.pVtab);
|
||||
}
|
||||
|
||||
sqlite3_vtab_cursor base;
|
||||
CopyablePersistent<v8::Object> iterator;
|
||||
CopyablePersistent<v8::Function> next;
|
||||
CopyablePersistent<v8::Array> row;
|
||||
bool done;
|
||||
sqlite_int64 rowid;
|
||||
};
|
||||
|
||||
// This nested class is used by Data::ResultValueFromJS to report errors.
|
||||
class TempDataConverter : DataConverter { friend class CustomTable;
|
||||
explicit TempDataConverter(CustomTable* parent) :
|
||||
parent(parent),
|
||||
status(SQLITE_OK) {}
|
||||
|
||||
void PropagateJSError(sqlite3_context* invocation) {
|
||||
status = SQLITE_ERROR;
|
||||
parent->PropagateJSError();
|
||||
}
|
||||
|
||||
std::string GetDataErrorPrefix() {
|
||||
return std::string("Virtual table module \"") + parent->name + "\" yielded";
|
||||
}
|
||||
|
||||
CustomTable * const parent;
|
||||
int status;
|
||||
};
|
||||
|
||||
// Although this function does nothing, we cannot use xConnect directly,
|
||||
// because that would cause SQLite to register an eponymous virtual table.
|
||||
static int xCreate(sqlite3* db_handle, void* _self, int argc, const char* const * argv, sqlite3_vtab** output, char** errOutput) {
|
||||
return xConnect(db_handle, _self, argc, argv, output, errOutput);
|
||||
}
|
||||
|
||||
// This method uses the factory function to instantiate a new virtual table.
|
||||
static int xConnect(sqlite3* db_handle, void* _self, int argc, const char* const * argv, sqlite3_vtab** output, char** errOutput) {
|
||||
CustomTable* self = static_cast<CustomTable*>(_self);
|
||||
v8::Isolate* isolate = self->isolate;
|
||||
v8::HandleScope scope(isolate);
|
||||
UseContext;
|
||||
|
||||
v8::Local<v8::Value>* args = ALLOC_ARRAY<v8::Local<v8::Value>>(argc);
|
||||
for (int i = 0; i < argc; ++i) {
|
||||
args[i] = StringFromUtf8(isolate, argv[i], -1);
|
||||
}
|
||||
|
||||
// Run the factory function to receive a new virtual table definition.
|
||||
v8::MaybeLocal<v8::Value> maybeReturnValue = self->factory.Get(isolate)->Call(ctx, v8::Undefined(isolate), argc, args);
|
||||
delete[] args;
|
||||
|
||||
if (maybeReturnValue.IsEmpty()) {
|
||||
self->PropagateJSError();
|
||||
return SQLITE_ERROR;
|
||||
}
|
||||
|
||||
// Extract each part of the virtual table definition.
|
||||
v8::Local<v8::Array> returnValue = maybeReturnValue.ToLocalChecked().As<v8::Array>();
|
||||
v8::Local<v8::String> sqlString = returnValue->Get(ctx, 0).ToLocalChecked().As<v8::String>();
|
||||
v8::Local<v8::Function> generator = returnValue->Get(ctx, 1).ToLocalChecked().As<v8::Function>();
|
||||
v8::Local<v8::Array> parameterNames = returnValue->Get(ctx, 2).ToLocalChecked().As<v8::Array>();
|
||||
int safe_ints = returnValue->Get(ctx, 3).ToLocalChecked().As<v8::Int32>()->Value();
|
||||
bool direct_only = returnValue->Get(ctx, 4).ToLocalChecked().As<v8::Boolean>()->Value();
|
||||
|
||||
v8::String::Utf8Value sql(isolate, sqlString);
|
||||
safe_ints = safe_ints < 2 ? safe_ints : static_cast<int>(self->db->GetState()->safe_ints);
|
||||
|
||||
// Copy the parameter names into a std::vector.
|
||||
std::vector<std::string> parameter_names;
|
||||
for (int i = 0, len = parameterNames->Length(); i < len; ++i) {
|
||||
v8::Local<v8::String> parameterName = parameterNames->Get(ctx, i).ToLocalChecked().As<v8::String>();
|
||||
v8::String::Utf8Value parameter_name(isolate, parameterName);
|
||||
parameter_names.emplace_back(*parameter_name);
|
||||
}
|
||||
|
||||
// Pass our SQL table definition to SQLite (this should never fail).
|
||||
if (sqlite3_declare_vtab(db_handle, *sql) != SQLITE_OK) {
|
||||
*errOutput = sqlite3_mprintf("failed to declare virtual table \"%s\"", argv[2]);
|
||||
return SQLITE_ERROR;
|
||||
}
|
||||
if (direct_only && sqlite3_vtab_config(db_handle, SQLITE_VTAB_DIRECTONLY) != SQLITE_OK) {
|
||||
*errOutput = sqlite3_mprintf("failed to configure virtual table \"%s\"", argv[2]);
|
||||
return SQLITE_ERROR;
|
||||
}
|
||||
|
||||
// Return the successfully created virtual table.
|
||||
*output = (new VTab(self, generator, parameter_names, safe_ints))->Downcast();
|
||||
return SQLITE_OK;
|
||||
}
|
||||
|
||||
static int xDisconnect(sqlite3_vtab* vtab) {
|
||||
delete VTab::Upcast(vtab);
|
||||
return SQLITE_OK;
|
||||
}
|
||||
|
||||
static int xOpen(sqlite3_vtab* vtab, sqlite3_vtab_cursor** output) {
|
||||
*output = (new Cursor())->Downcast();
|
||||
return SQLITE_OK;
|
||||
}
|
||||
|
||||
static int xClose(sqlite3_vtab_cursor* cursor) {
|
||||
delete Cursor::Upcast(cursor);
|
||||
return SQLITE_OK;
|
||||
}
|
||||
|
||||
// This method uses a fresh cursor to start a new scan of a virtual table.
|
||||
// The args and idxNum are provided by xBestIndex (idxStr is unused).
|
||||
// idxNum is a bitmap that provides the proper indices of the received args.
|
||||
static int xFilter(sqlite3_vtab_cursor* _cursor, int idxNum, const char* idxStr, int argc, sqlite3_value** argv) {
|
||||
Cursor* cursor = Cursor::Upcast(_cursor);
|
||||
VTab* vtab = cursor->GetVTab();
|
||||
CustomTable* self = vtab->parent;
|
||||
Addon* addon = self->addon;
|
||||
v8::Isolate* isolate = self->isolate;
|
||||
v8::HandleScope scope(isolate);
|
||||
UseContext;
|
||||
|
||||
// Convert the SQLite arguments into JavaScript arguments. Note that
|
||||
// the values in argv may be in the wrong order, so we fix that here.
|
||||
v8::Local<v8::Value> args_fast[4];
|
||||
v8::Local<v8::Value>* args = NULL;
|
||||
int parameter_count = vtab->parameter_count;
|
||||
if (parameter_count != 0) {
|
||||
args = parameter_count <= 4 ? args_fast : ALLOC_ARRAY<v8::Local<v8::Value>>(parameter_count);
|
||||
int argn = 0;
|
||||
bool safe_ints = vtab->safe_ints;
|
||||
for (int i = 0; i < parameter_count; ++i) {
|
||||
if (idxNum & 1 << i) {
|
||||
args[i] = Data::GetValueJS(isolate, argv[argn++], safe_ints);
|
||||
// If any arguments are NULL, the result set is necessarily
|
||||
// empty, so don't bother to run the generator function.
|
||||
if (args[i]->IsNull()) {
|
||||
if (args != args_fast) delete[] args;
|
||||
cursor->done = true;
|
||||
return SQLITE_OK;
|
||||
}
|
||||
} else {
|
||||
args[i] = v8::Undefined(isolate);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Invoke the generator function to create a new iterator.
|
||||
v8::MaybeLocal<v8::Value> maybeIterator = vtab->generator.Get(isolate)->Call(ctx, v8::Undefined(isolate), parameter_count, args);
|
||||
if (args != args_fast) delete[] args;
|
||||
|
||||
if (maybeIterator.IsEmpty()) {
|
||||
self->PropagateJSError();
|
||||
return SQLITE_ERROR;
|
||||
}
|
||||
|
||||
// Store the iterator and its next() method; we'll be using it a lot.
|
||||
v8::Local<v8::Object> iterator = maybeIterator.ToLocalChecked().As<v8::Object>();
|
||||
v8::Local<v8::Function> next = iterator->Get(ctx, addon->cs.next.Get(isolate)).ToLocalChecked().As<v8::Function>();
|
||||
cursor->iterator.Reset(isolate, iterator);
|
||||
cursor->next.Reset(isolate, next);
|
||||
cursor->rowid = 0;
|
||||
|
||||
// Advance the iterator/cursor to the first row.
|
||||
return xNext(cursor->Downcast());
|
||||
}
|
||||
|
||||
// This method advances a virtual table's cursor to the next row.
|
||||
// SQLite will call this method repeatedly, driving the generator function.
|
||||
static int xNext(sqlite3_vtab_cursor* _cursor) {
|
||||
Cursor* cursor = Cursor::Upcast(_cursor);
|
||||
CustomTable* self = cursor->GetVTab()->parent;
|
||||
Addon* addon = self->addon;
|
||||
v8::Isolate* isolate = self->isolate;
|
||||
v8::HandleScope scope(isolate);
|
||||
UseContext;
|
||||
|
||||
v8::Local<v8::Object> iterator = cursor->iterator.Get(isolate);
|
||||
v8::Local<v8::Function> next = cursor->next.Get(isolate);
|
||||
|
||||
v8::MaybeLocal<v8::Value> maybeRecord = next->Call(ctx, iterator, 0, NULL);
|
||||
if (maybeRecord.IsEmpty()) {
|
||||
self->PropagateJSError();
|
||||
return SQLITE_ERROR;
|
||||
}
|
||||
|
||||
v8::Local<v8::Object> record = maybeRecord.ToLocalChecked().As<v8::Object>();
|
||||
bool done = record->Get(ctx, addon->cs.done.Get(isolate)).ToLocalChecked().As<v8::Boolean>()->Value();
|
||||
if (!done) {
|
||||
cursor->row.Reset(isolate, record->Get(ctx, addon->cs.value.Get(isolate)).ToLocalChecked().As<v8::Array>());
|
||||
}
|
||||
cursor->done = done;
|
||||
cursor->rowid += 1;
|
||||
|
||||
return SQLITE_OK;
|
||||
}
|
||||
|
||||
// If this method returns 1, SQLite will stop scanning the virtual table.
|
||||
static int xEof(sqlite3_vtab_cursor* cursor) {
|
||||
return Cursor::Upcast(cursor)->done;
|
||||
}
|
||||
|
||||
// This method extracts some column from the cursor's current row.
|
||||
static int xColumn(sqlite3_vtab_cursor* _cursor, sqlite3_context* invocation, int column) {
|
||||
Cursor* cursor = Cursor::Upcast(_cursor);
|
||||
CustomTable* self = cursor->GetVTab()->parent;
|
||||
TempDataConverter temp_data_converter(self);
|
||||
v8::Isolate* isolate = self->isolate;
|
||||
v8::HandleScope scope(isolate);
|
||||
|
||||
v8::Local<v8::Array> row = cursor->row.Get(isolate);
|
||||
v8::MaybeLocal<v8::Value> maybeColumnValue = row->Get(OnlyContext, column);
|
||||
if (maybeColumnValue.IsEmpty()) {
|
||||
temp_data_converter.PropagateJSError(NULL);
|
||||
} else {
|
||||
Data::ResultValueFromJS(isolate, invocation, maybeColumnValue.ToLocalChecked(), &temp_data_converter);
|
||||
}
|
||||
return temp_data_converter.status;
|
||||
}
|
||||
|
||||
// This method outputs the rowid of the cursor's current row.
|
||||
static int xRowid(sqlite3_vtab_cursor* cursor, sqlite_int64* output) {
|
||||
*output = Cursor::Upcast(cursor)->rowid;
|
||||
return SQLITE_OK;
|
||||
}
|
||||
|
||||
// This method tells SQLite how to *plan* queries on our virtual table.
|
||||
// It gets invoked (typically multiple times) during db.prepare().
|
||||
static int xBestIndex(sqlite3_vtab* vtab, sqlite3_index_info* output) {
|
||||
int parameter_count = VTab::Upcast(vtab)->parameter_count;
|
||||
int argument_count = 0;
|
||||
std::vector<std::pair<int, int>> forwarded;
|
||||
|
||||
for (int i = 0, len = output->nConstraint; i < len; ++i) {
|
||||
auto item = output->aConstraint[i];
|
||||
|
||||
// The SQLITE_INDEX_CONSTRAINT_LIMIT and SQLITE_INDEX_CONSTRAINT_OFFSET
|
||||
// operators have no left-hand operand, and so for those operators the
|
||||
// corresponding item.iColumn is meaningless.
|
||||
// We don't care those constraints.
|
||||
if (item.op == SQLITE_INDEX_CONSTRAINT_LIMIT || item.op == SQLITE_INDEX_CONSTRAINT_OFFSET) {
|
||||
continue;
|
||||
}
|
||||
// We only care about constraints on parameters, not regular columns.
|
||||
if (item.iColumn >= 0 && item.iColumn < parameter_count) {
|
||||
if (item.op != SQLITE_INDEX_CONSTRAINT_EQ) {
|
||||
sqlite3_free(vtab->zErrMsg);
|
||||
vtab->zErrMsg = sqlite3_mprintf(
|
||||
"virtual table parameter \"%s\" can only be constrained by the '=' operator",
|
||||
VTab::Upcast(vtab)->parameter_names.at(item.iColumn).c_str());
|
||||
return SQLITE_ERROR;
|
||||
}
|
||||
if (!item.usable) {
|
||||
// Don't allow SQLite to make plans that ignore arguments.
|
||||
// Otherwise, a user could pass arguments, but then they
|
||||
// could appear undefined in the generator function.
|
||||
return SQLITE_CONSTRAINT;
|
||||
}
|
||||
forwarded.emplace_back(item.iColumn, i);
|
||||
}
|
||||
}
|
||||
|
||||
// Tell SQLite to forward arguments to xFilter.
|
||||
std::sort(forwarded.begin(), forwarded.end());
|
||||
for (std::pair<int, int> pair : forwarded) {
|
||||
int bit = 1 << pair.first;
|
||||
if (!(output->idxNum & bit)) {
|
||||
output->idxNum |= bit;
|
||||
output->aConstraintUsage[pair.second].argvIndex = ++argument_count;
|
||||
output->aConstraintUsage[pair.second].omit = 1;
|
||||
}
|
||||
}
|
||||
|
||||
// Use a very high estimated cost so SQLite is not tempted to invoke the
|
||||
// generator function within a loop, if it can be avoided.
|
||||
output->estimatedCost = output->estimatedRows = 1000000000 / (argument_count + 1);
|
||||
return SQLITE_OK;
|
||||
}
|
||||
|
||||
void PropagateJSError() {
|
||||
assert(db->GetState()->was_js_error == false);
|
||||
db->GetState()->was_js_error = true;
|
||||
}
|
||||
|
||||
Addon* const addon;
|
||||
v8::Isolate* const isolate;
|
||||
Database* const db;
|
||||
const std::string name;
|
||||
const CopyablePersistent<v8::Function> factory;
|
||||
};
|
||||
17
src/util/data-converter.lzz
Normal file
17
src/util/data-converter.lzz
Normal file
@ -0,0 +1,17 @@
|
||||
class DataConverter {
|
||||
public:
|
||||
|
||||
void ThrowDataConversionError(sqlite3_context* invocation, bool isBigInt) {
|
||||
if (isBigInt) {
|
||||
ThrowRangeError((GetDataErrorPrefix() + " a bigint that was too big").c_str());
|
||||
} else {
|
||||
ThrowTypeError((GetDataErrorPrefix() + " an invalid value").c_str());
|
||||
}
|
||||
PropagateJSError(invocation);
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
virtual void PropagateJSError(sqlite3_context* invocation) = 0;
|
||||
virtual std::string GetDataErrorPrefix() = 0;
|
||||
};
|
||||
154
src/util/data.lzz
Normal file
154
src/util/data.lzz
Normal file
@ -0,0 +1,154 @@
|
||||
#define JS_VALUE_TO_SQLITE(to, value, isolate, ...) \
|
||||
if (value->IsNumber()) { \
|
||||
return sqlite3_##to##_double( \
|
||||
__VA_ARGS__, \
|
||||
value.As<v8::Number>()->Value() \
|
||||
); \
|
||||
} else if (value->IsBigInt()) { \
|
||||
bool lossless; \
|
||||
int64_t v = value.As<v8::BigInt>()->Int64Value(&lossless); \
|
||||
if (lossless) { \
|
||||
return sqlite3_##to##_int64(__VA_ARGS__, v); \
|
||||
} \
|
||||
} else if (value->IsString()) { \
|
||||
v8::String::Utf8Value utf8(isolate, value.As<v8::String>()); \
|
||||
return sqlite3_##to##_text( \
|
||||
__VA_ARGS__, \
|
||||
*utf8, \
|
||||
utf8.length(), \
|
||||
SQLITE_TRANSIENT \
|
||||
); \
|
||||
} else if (node::Buffer::HasInstance(value)) { \
|
||||
const char* data = node::Buffer::Data(value); \
|
||||
return sqlite3_##to##_blob( \
|
||||
__VA_ARGS__, \
|
||||
data ? data : "", \
|
||||
node::Buffer::Length(value), \
|
||||
SQLITE_TRANSIENT \
|
||||
); \
|
||||
} else if (value->IsNull() || value->IsUndefined()) { \
|
||||
return sqlite3_##to##_null(__VA_ARGS__); \
|
||||
}
|
||||
|
||||
#define SQLITE_VALUE_TO_JS(from, isolate, safe_ints, ...) \
|
||||
switch (sqlite3_##from##_type(__VA_ARGS__)) { \
|
||||
case SQLITE_INTEGER: \
|
||||
if (safe_ints) { \
|
||||
return v8::BigInt::New( \
|
||||
isolate, \
|
||||
sqlite3_##from##_int64(__VA_ARGS__) \
|
||||
); \
|
||||
} \
|
||||
case SQLITE_FLOAT: \
|
||||
return v8::Number::New( \
|
||||
isolate, \
|
||||
sqlite3_##from##_double(__VA_ARGS__) \
|
||||
); \
|
||||
case SQLITE_TEXT: \
|
||||
return StringFromUtf8( \
|
||||
isolate, \
|
||||
reinterpret_cast<const char*>(sqlite3_##from##_text(__VA_ARGS__)), \
|
||||
sqlite3_##from##_bytes(__VA_ARGS__) \
|
||||
); \
|
||||
case SQLITE_BLOB: \
|
||||
return node::Buffer::Copy( \
|
||||
isolate, \
|
||||
static_cast<const char*>(sqlite3_##from##_blob(__VA_ARGS__)), \
|
||||
sqlite3_##from##_bytes(__VA_ARGS__) \
|
||||
).ToLocalChecked(); \
|
||||
default: \
|
||||
assert(sqlite3_##from##_type(__VA_ARGS__) == SQLITE_NULL); \
|
||||
return v8::Null(isolate); \
|
||||
} \
|
||||
assert(false);
|
||||
|
||||
namespace Data {
|
||||
|
||||
static const char FLAT = 0;
|
||||
static const char PLUCK = 1;
|
||||
static const char EXPAND = 2;
|
||||
static const char RAW = 3;
|
||||
|
||||
v8::Local<v8::Value> GetValueJS(v8::Isolate* isolate, sqlite3_stmt* handle, int column, bool safe_ints) {
|
||||
SQLITE_VALUE_TO_JS(column, isolate, safe_ints, handle, column);
|
||||
}
|
||||
|
||||
v8::Local<v8::Value> GetValueJS(v8::Isolate* isolate, sqlite3_value* value, bool safe_ints) {
|
||||
SQLITE_VALUE_TO_JS(value, isolate, safe_ints, value);
|
||||
}
|
||||
|
||||
v8::Local<v8::Value> GetFlatRowJS(v8::Isolate* isolate, v8::Local<v8::Context> ctx, sqlite3_stmt* handle, bool safe_ints, std::vector<v8::Local<v8::Name> >& keys) {
|
||||
// Lazily initialize keys
|
||||
if (keys.size() == 0) {
|
||||
int column_count = sqlite3_column_count(handle);
|
||||
keys.reserve(column_count);
|
||||
for (int i = 0; i < column_count; ++i) {
|
||||
keys.emplace_back(InternalizedFromUtf8(isolate, sqlite3_column_name(handle, i), -1));
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<v8::Local<v8::Value>> values;
|
||||
values.reserve(keys.size());
|
||||
|
||||
for (size_t i = 0; i < keys.size(); ++i) {
|
||||
values.emplace_back(Data::GetValueJS(isolate, handle, i, safe_ints));
|
||||
}
|
||||
|
||||
return v8::Object::New(isolate, v8::Null(isolate), keys.data(), values.data(), keys.size());
|
||||
}
|
||||
|
||||
v8::Local<v8::Value> GetExpandedRowJS(v8::Isolate* isolate, v8::Local<v8::Context> ctx, sqlite3_stmt* handle, bool safe_ints) {
|
||||
v8::Local<v8::Object> row = v8::Object::New(isolate);
|
||||
int column_count = sqlite3_column_count(handle);
|
||||
for (int i = 0; i < column_count; ++i) {
|
||||
const char* table_raw = sqlite3_column_table_name(handle, i);
|
||||
v8::Local<v8::String> table = InternalizedFromUtf8(isolate, table_raw == NULL ? "$" : table_raw, -1);
|
||||
v8::Local<v8::String> column = InternalizedFromUtf8(isolate, sqlite3_column_name(handle, i), -1);
|
||||
v8::Local<v8::Value> value = Data::GetValueJS(isolate, handle, i, safe_ints);
|
||||
if (row->HasOwnProperty(ctx, table).FromJust()) {
|
||||
row->Get(ctx, table).ToLocalChecked().As<v8::Object>()->Set(ctx, column, value).FromJust();
|
||||
} else {
|
||||
v8::Local<v8::Object> nested = v8::Object::New(isolate);
|
||||
row->Set(ctx, table, nested).FromJust();
|
||||
nested->Set(ctx, column, value).FromJust();
|
||||
}
|
||||
}
|
||||
return row;
|
||||
}
|
||||
|
||||
v8::Local<v8::Value> GetRawRowJS(v8::Isolate* isolate, v8::Local<v8::Context> ctx, sqlite3_stmt* handle, bool safe_ints) {
|
||||
v8::Local<v8::Array> row = v8::Array::New(isolate);
|
||||
int column_count = sqlite3_column_count(handle);
|
||||
for (int i = 0; i < column_count; ++i) {
|
||||
row->Set(ctx, i, Data::GetValueJS(isolate, handle, i, safe_ints)).FromJust();
|
||||
}
|
||||
return row;
|
||||
}
|
||||
|
||||
v8::Local<v8::Value> GetRowJS(v8::Isolate* isolate, v8::Local<v8::Context> ctx, sqlite3_stmt* handle, bool safe_ints, char mode, std::vector<v8::Local<v8::Name> >& keys) {
|
||||
if (mode == FLAT) return GetFlatRowJS(isolate, ctx, handle, safe_ints, keys);
|
||||
if (mode == PLUCK) return GetValueJS(isolate, handle, 0, safe_ints);
|
||||
if (mode == EXPAND) return GetExpandedRowJS(isolate, ctx, handle, safe_ints);
|
||||
if (mode == RAW) return GetRawRowJS(isolate, ctx, handle, safe_ints);
|
||||
assert(false);
|
||||
return v8::Local<v8::Value>();
|
||||
}
|
||||
|
||||
void GetArgumentsJS(v8::Isolate* isolate, v8::Local<v8::Value>* out, sqlite3_value** values, int argument_count, bool safe_ints) {
|
||||
assert(argument_count > 0);
|
||||
for (int i = 0; i < argument_count; ++i) {
|
||||
out[i] = Data::GetValueJS(isolate, values[i], safe_ints);
|
||||
}
|
||||
}
|
||||
|
||||
int BindValueFromJS(v8::Isolate* isolate, sqlite3_stmt* handle, int index, v8::Local<v8::Value> value) {
|
||||
JS_VALUE_TO_SQLITE(bind, value, isolate, handle, index);
|
||||
return value->IsBigInt() ? SQLITE_TOOBIG : -1;
|
||||
}
|
||||
|
||||
void ResultValueFromJS(v8::Isolate* isolate, sqlite3_context* invocation, v8::Local<v8::Value> value, DataConverter* converter) {
|
||||
JS_VALUE_TO_SQLITE(result, value, isolate, invocation);
|
||||
converter->ThrowDataConversionError(invocation, value->IsBigInt());
|
||||
}
|
||||
|
||||
}
|
||||
156
src/util/macros.lzz
Normal file
156
src/util/macros.lzz
Normal file
@ -0,0 +1,156 @@
|
||||
#define NODE_ARGUMENTS const v8::FunctionCallbackInfo<v8::Value>&
|
||||
#define NODE_ARGUMENTS_POINTER const v8::FunctionCallbackInfo<v8::Value>*
|
||||
#define NODE_METHOD(name) static void name(NODE_ARGUMENTS info)
|
||||
#define NODE_GETTER(name) NODE_METHOD(name)
|
||||
#define INIT(name) static v8::Local<v8::Function> name(v8::Isolate* isolate, v8::Local<v8::External> data)
|
||||
|
||||
#define EasyIsolate v8::Isolate* isolate = v8::Isolate::GetCurrent()
|
||||
#define OnlyIsolate info.GetIsolate()
|
||||
#define OnlyContext isolate->GetCurrentContext()
|
||||
#define OnlyAddon static_cast<Addon*>(info.Data().As<v8::External>()->Value())
|
||||
#define UseIsolate v8::Isolate* isolate = OnlyIsolate
|
||||
#define UseContext v8::Local<v8::Context> ctx = OnlyContext
|
||||
#define UseAddon Addon* addon = OnlyAddon
|
||||
#define Unwrap node::ObjectWrap::Unwrap
|
||||
|
||||
inline v8::Local<v8::String> StringFromUtf8(v8::Isolate* isolate, const char* data, int length) {
|
||||
return v8::String::NewFromUtf8(isolate, data, v8::NewStringType::kNormal, length).ToLocalChecked();
|
||||
}
|
||||
inline v8::Local<v8::String> InternalizedFromUtf8(v8::Isolate* isolate, const char* data, int length) {
|
||||
return v8::String::NewFromUtf8(isolate, data, v8::NewStringType::kInternalized, length).ToLocalChecked();
|
||||
}
|
||||
inline v8::Local<v8::Value> InternalizedFromUtf8OrNull(v8::Isolate* isolate, const char* data, int length) {
|
||||
if (data == NULL) return v8::Null(isolate);
|
||||
return InternalizedFromUtf8(isolate, data, length);
|
||||
}
|
||||
inline v8::Local<v8::String> InternalizedFromLatin1(v8::Isolate* isolate, const char* str) {
|
||||
return v8::String::NewFromOneByte(isolate, reinterpret_cast<const uint8_t*>(str), v8::NewStringType::kInternalized).ToLocalChecked();
|
||||
}
|
||||
|
||||
#hdr
|
||||
template <class T> using CopyablePersistent = v8::Global<T>;
|
||||
#end
|
||||
inline void SetFrozen(v8::Isolate* isolate, v8::Local<v8::Context> ctx, v8::Local<v8::Object> obj, CopyablePersistent<v8::String>& key, v8::Local<v8::Value> value) {
|
||||
obj->DefineOwnProperty(ctx, key.Get(isolate), value, static_cast<v8::PropertyAttribute>(v8::DontDelete | v8::ReadOnly)).FromJust();
|
||||
}
|
||||
|
||||
void ThrowError(const char* message) { EasyIsolate; isolate->ThrowException(v8::Exception::Error(StringFromUtf8(isolate, message, -1))); }
|
||||
void ThrowTypeError(const char* message) { EasyIsolate; isolate->ThrowException(v8::Exception::TypeError(StringFromUtf8(isolate, message, -1))); }
|
||||
void ThrowRangeError(const char* message) { EasyIsolate; isolate->ThrowException(v8::Exception::RangeError(StringFromUtf8(isolate, message, -1))); }
|
||||
|
||||
#define REQUIRE_ARGUMENT_ANY(at, var) \
|
||||
if (info.Length() <= (at())) \
|
||||
return ThrowTypeError("Expected a "#at" argument"); \
|
||||
var = info[at()]
|
||||
|
||||
#define _REQUIRE_ARGUMENT(at, var, Type, message, ...) \
|
||||
if (info.Length() <= (at()) || !info[at()]->Is##Type()) \
|
||||
return ThrowTypeError("Expected "#at" argument to be "#message); \
|
||||
var = (info[at()].As<v8::Type>())__VA_ARGS__
|
||||
|
||||
#define REQUIRE_ARGUMENT_INT32(at, var) \
|
||||
_REQUIRE_ARGUMENT(at, var, Int32, a 32-bit signed integer, ->Value())
|
||||
#define REQUIRE_ARGUMENT_BOOLEAN(at, var) \
|
||||
_REQUIRE_ARGUMENT(at, var, Boolean, a boolean, ->Value())
|
||||
#define REQUIRE_ARGUMENT_STRING(at, var) \
|
||||
_REQUIRE_ARGUMENT(at, var, String, a string)
|
||||
#define REQUIRE_ARGUMENT_OBJECT(at, var) \
|
||||
_REQUIRE_ARGUMENT(at, var, Object, an object)
|
||||
#define REQUIRE_ARGUMENT_FUNCTION(at, var) \
|
||||
_REQUIRE_ARGUMENT(at, var, Function, a function)
|
||||
|
||||
#define REQUIRE_DATABASE_OPEN(db) \
|
||||
if (!db->open) \
|
||||
return ThrowTypeError("The database connection is not open")
|
||||
#define REQUIRE_DATABASE_NOT_BUSY(db) \
|
||||
if (db->busy) \
|
||||
return ThrowTypeError("This database connection is busy executing a query")
|
||||
#define REQUIRE_DATABASE_NO_ITERATORS(db) \
|
||||
if (db->iterators) \
|
||||
return ThrowTypeError("This database connection is busy executing a query")
|
||||
#define REQUIRE_DATABASE_NO_ITERATORS_UNLESS_UNSAFE(db) \
|
||||
if (!db->unsafe_mode) { \
|
||||
REQUIRE_DATABASE_NO_ITERATORS(db); \
|
||||
} ((void)0)
|
||||
#define REQUIRE_STATEMENT_NOT_LOCKED(stmt) \
|
||||
if (stmt->locked) \
|
||||
return ThrowTypeError("This statement is busy executing a query")
|
||||
|
||||
#define first() 0
|
||||
#define second() 1
|
||||
#define third() 2
|
||||
#define fourth() 3
|
||||
#define fifth() 4
|
||||
#define sixth() 5
|
||||
#define seventh() 6
|
||||
#define eighth() 7
|
||||
#define ninth() 8
|
||||
#define tenth() 9
|
||||
|
||||
// Determines whether to skip the given character at the start of an SQL string.
|
||||
inline bool IS_SKIPPED(char c) {
|
||||
return c == ' ' || c == ';' || (c >= '\t' && c <= '\r');
|
||||
}
|
||||
|
||||
// Allocates an empty array, without calling constructors/initializers.
|
||||
template<class T> inline T* ALLOC_ARRAY(size_t count) {
|
||||
return static_cast<T*>(::operator new[](count * sizeof(T)));
|
||||
}
|
||||
|
||||
// Deallocates an array, without calling destructors.
|
||||
template<class T> inline void FREE_ARRAY(T* array_pointer) {
|
||||
::operator delete[](array_pointer);
|
||||
}
|
||||
|
||||
v8::Local<v8::FunctionTemplate> NewConstructorTemplate(
|
||||
v8::Isolate* isolate,
|
||||
v8::Local<v8::External> data,
|
||||
v8::FunctionCallback func,
|
||||
const char* name
|
||||
) {
|
||||
v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(isolate, func, data);
|
||||
t->InstanceTemplate()->SetInternalFieldCount(1);
|
||||
t->SetClassName(InternalizedFromLatin1(isolate, name));
|
||||
return t;
|
||||
}
|
||||
void SetPrototypeMethod(
|
||||
v8::Isolate* isolate,
|
||||
v8::Local<v8::External> data,
|
||||
v8::Local<v8::FunctionTemplate> recv,
|
||||
const char* name,
|
||||
v8::FunctionCallback func
|
||||
) {
|
||||
v8::HandleScope scope(isolate);
|
||||
recv->PrototypeTemplate()->Set(
|
||||
InternalizedFromLatin1(isolate, name),
|
||||
v8::FunctionTemplate::New(isolate, func, data, v8::Signature::New(isolate, recv))
|
||||
);
|
||||
}
|
||||
void SetPrototypeSymbolMethod(
|
||||
v8::Isolate* isolate,
|
||||
v8::Local<v8::External> data,
|
||||
v8::Local<v8::FunctionTemplate> recv,
|
||||
v8::Local<v8::Symbol> symbol,
|
||||
v8::FunctionCallback func
|
||||
) {
|
||||
v8::HandleScope scope(isolate);
|
||||
recv->PrototypeTemplate()->Set(
|
||||
symbol,
|
||||
v8::FunctionTemplate::New(isolate, func, data, v8::Signature::New(isolate, recv))
|
||||
);
|
||||
}
|
||||
void SetPrototypeGetter(
|
||||
v8::Isolate* isolate,
|
||||
v8::Local<v8::External> data,
|
||||
v8::Local<v8::FunctionTemplate> recv,
|
||||
const char* name,
|
||||
v8::FunctionCallback func
|
||||
) {
|
||||
v8::HandleScope scope(isolate);
|
||||
v8::Local<v8::FunctionTemplate> func_tpl =
|
||||
v8::FunctionTemplate::New(isolate, func, data);
|
||||
recv->InstanceTemplate()->SetAccessorProperty(
|
||||
InternalizedFromLatin1(isolate, name),
|
||||
func_tpl
|
||||
);
|
||||
}
|
||||
71
src/util/query-macros.lzz
Normal file
71
src/util/query-macros.lzz
Normal file
@ -0,0 +1,71 @@
|
||||
#define STATEMENT_BIND(handle) \
|
||||
Binder binder(handle); \
|
||||
if (!binder.Bind(info, info.Length(), stmt)) { \
|
||||
sqlite3_clear_bindings(handle); \
|
||||
return; \
|
||||
} ((void)0)
|
||||
|
||||
#define STATEMENT_THROW_LOGIC() \
|
||||
db->ThrowDatabaseError(); \
|
||||
if (!bound) { sqlite3_clear_bindings(handle); } \
|
||||
return
|
||||
|
||||
#define STATEMENT_RETURN_LOGIC(return_value) \
|
||||
info.GetReturnValue().Set(return_value); \
|
||||
if (!bound) { sqlite3_clear_bindings(handle); } \
|
||||
return
|
||||
|
||||
#define STATEMENT_START_LOGIC(RETURNS_DATA_CHECK, MUTATE_CHECK) \
|
||||
Statement* stmt = Unwrap<Statement>(info.This()); \
|
||||
RETURNS_DATA_CHECK(); \
|
||||
sqlite3_stmt* handle = stmt->handle; \
|
||||
Database* db = stmt->db; \
|
||||
REQUIRE_DATABASE_OPEN(db->GetState()); \
|
||||
REQUIRE_DATABASE_NOT_BUSY(db->GetState()); \
|
||||
MUTATE_CHECK(); \
|
||||
const bool bound = stmt->bound; \
|
||||
if (!bound) { \
|
||||
STATEMENT_BIND(handle); \
|
||||
} else if (info.Length() > 0) { \
|
||||
return ThrowTypeError("This statement already has bound parameters"); \
|
||||
} ((void)0)
|
||||
|
||||
|
||||
#define STATEMENT_THROW() db->GetState()->busy = false; STATEMENT_THROW_LOGIC()
|
||||
#define STATEMENT_RETURN(x) db->GetState()->busy = false; STATEMENT_RETURN_LOGIC(x)
|
||||
#define STATEMENT_START(x, y) \
|
||||
STATEMENT_START_LOGIC(x, y); \
|
||||
db->GetState()->busy = true; \
|
||||
UseIsolate; \
|
||||
if (db->Log(isolate, handle)) { \
|
||||
STATEMENT_THROW(); \
|
||||
} ((void)0)
|
||||
|
||||
|
||||
#define DOES_NOT_MUTATE() REQUIRE_STATEMENT_NOT_LOCKED(stmt)
|
||||
#define DOES_MUTATE() \
|
||||
REQUIRE_STATEMENT_NOT_LOCKED(stmt); \
|
||||
REQUIRE_DATABASE_NO_ITERATORS_UNLESS_UNSAFE(db->GetState())
|
||||
#define DOES_ADD_ITERATOR() \
|
||||
DOES_NOT_MUTATE(); \
|
||||
if (db->GetState()->iterators == USHRT_MAX) \
|
||||
return ThrowRangeError("Too many active database iterators")
|
||||
#define REQUIRE_STATEMENT_RETURNS_DATA() \
|
||||
if (!stmt->returns_data) \
|
||||
return ThrowTypeError("This statement does not return data. Use run() instead")
|
||||
#define ALLOW_ANY_STATEMENT() \
|
||||
((void)0)
|
||||
|
||||
|
||||
#define _FUNCTION_START(type) \
|
||||
type* self = static_cast<type*>(sqlite3_user_data(invocation)); \
|
||||
v8::Isolate* isolate = self->isolate; \
|
||||
v8::HandleScope scope(isolate)
|
||||
|
||||
#define FUNCTION_START() \
|
||||
_FUNCTION_START(CustomFunction)
|
||||
|
||||
#define AGGREGATE_START() \
|
||||
_FUNCTION_START(CustomAggregate); \
|
||||
Accumulator* acc = self->GetAccumulator(invocation); \
|
||||
if (acc->value.IsEmpty()) return
|
||||
75
test/35.database.load-extension.js
Normal file
75
test/35.database.load-extension.js
Normal file
@ -0,0 +1,75 @@
|
||||
'use strict';
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const Database = require('../.');
|
||||
|
||||
describe('Database#loadExtension()', function () {
|
||||
let filepath;
|
||||
before(function () {
|
||||
const releaseFilepath = path.join(__dirname, '..', 'build', 'Release', 'test_extension.node');
|
||||
const debugFilepath = path.join(__dirname, '..', 'build', 'Debug', 'test_extension.node');
|
||||
try {
|
||||
fs.accessSync(releaseFilepath);
|
||||
filepath = releaseFilepath;
|
||||
} catch (_) {
|
||||
fs.accessSync(debugFilepath);
|
||||
filepath = debugFilepath;
|
||||
}
|
||||
});
|
||||
beforeEach(function () {
|
||||
this.db = new Database(util.next());
|
||||
});
|
||||
afterEach(function () {
|
||||
this.db.close();
|
||||
});
|
||||
|
||||
it('should throw an exception if a string argument is not given', function () {
|
||||
expect(() => this.db.loadExtension()).to.throw(TypeError);
|
||||
expect(() => this.db.loadExtension(undefined)).to.throw(TypeError);
|
||||
expect(() => this.db.loadExtension(null)).to.throw(TypeError);
|
||||
expect(() => this.db.loadExtension(123)).to.throw(TypeError);
|
||||
expect(() => this.db.loadExtension(new String(filepath))).to.throw(TypeError);
|
||||
expect(() => this.db.loadExtension([filepath])).to.throw(TypeError);
|
||||
});
|
||||
it('should throw an exception if the database is busy', function () {
|
||||
let invoked = false;
|
||||
for (const value of this.db.prepare('select 555').pluck().iterate()) {
|
||||
expect(value).to.equal(555);
|
||||
expect(() => this.db.loadExtension(filepath)).to.throw(TypeError);
|
||||
invoked = true;
|
||||
}
|
||||
expect(invoked).to.be.true;
|
||||
});
|
||||
it('should throw an exception if the extension is not found', function () {
|
||||
try {
|
||||
this.db.loadExtension(filepath + 'x');
|
||||
} catch (err) {
|
||||
expect(err).to.be.an.instanceof(Database.SqliteError);
|
||||
expect(err.message).to.be.a('string');
|
||||
expect(err.message.length).to.be.above(0);
|
||||
expect(err.message).to.not.equal('not an error');
|
||||
expect(err.code).to.equal('SQLITE_ERROR');
|
||||
return;
|
||||
}
|
||||
throw new Error('This code should not have been reached');
|
||||
});
|
||||
it('should register the specified extension', function () {
|
||||
expect(this.db.loadExtension(filepath)).to.equal(this.db);
|
||||
expect(this.db.prepare('SELECT testExtensionFunction(NULL, 123, 99, 2)').pluck().get()).to.equal(4);
|
||||
expect(this.db.prepare('SELECT testExtensionFunction(NULL, 2)').pluck().get()).to.equal(2);
|
||||
});
|
||||
it('should not allow registering extensions with SQL', function () {
|
||||
expect(() => this.db.prepare('SELECT load_extension(?)').get(filepath)).to.throw(Database.SqliteError);
|
||||
expect(this.db.loadExtension(filepath)).to.equal(this.db);
|
||||
expect(() => this.db.prepare('SELECT load_extension(?)').get(filepath)).to.throw(Database.SqliteError);
|
||||
this.db.close();
|
||||
this.db = new Database(util.next());
|
||||
try {
|
||||
this.db.loadExtension(filepath + 'x');
|
||||
} catch (err) {
|
||||
expect(() => this.db.prepare('SELECT load_extension(?)').get(filepath)).to.throw(Database.SqliteError);
|
||||
return;
|
||||
}
|
||||
throw new Error('This code should not have been reached');
|
||||
});
|
||||
});
|
||||
81
test/37.database.serialize.js
Normal file
81
test/37.database.serialize.js
Normal file
@ -0,0 +1,81 @@
|
||||
'use strict';
|
||||
const Database = require('../.');
|
||||
|
||||
describe('Database#serialize()', function () {
|
||||
beforeEach(function () {
|
||||
this.db = new Database(util.next());
|
||||
this.db.prepare("CREATE TABLE entries (a TEXT, b INTEGER, c REAL, d BLOB, e TEXT)").run();
|
||||
this.seed = () => {
|
||||
this.db.prepare("INSERT INTO entries WITH RECURSIVE temp(a, b, c, d, e) AS (SELECT 'foo', 1, 3.14, x'dddddddd', NULL UNION ALL SELECT a, b + 1, c, d, e FROM temp LIMIT 1000) SELECT * FROM temp").run();
|
||||
};
|
||||
});
|
||||
afterEach(function () {
|
||||
this.db.close();
|
||||
});
|
||||
|
||||
it('should serialize the database and return a buffer', async function () {
|
||||
let buffer = this.db.serialize();
|
||||
expect(buffer).to.be.an.instanceof(Buffer);
|
||||
expect(buffer.length).to.be.above(1000);
|
||||
const lengthBefore = buffer.length;
|
||||
this.seed();
|
||||
buffer = this.db.serialize();
|
||||
expect(buffer).to.be.an.instanceof(Buffer);
|
||||
expect(buffer.length).to.be.above(lengthBefore);
|
||||
});
|
||||
it('should return a buffer that can be used by the Database constructor', async function () {
|
||||
this.seed();
|
||||
const buffer = this.db.serialize();
|
||||
expect(buffer).to.be.an.instanceof(Buffer);
|
||||
expect(buffer.length).to.be.above(1000);
|
||||
this.db.prepare('delete from entries').run();
|
||||
this.db.close();
|
||||
this.db = new Database(buffer);
|
||||
const bufferCopy = this.db.serialize();
|
||||
expect(buffer.length).to.equal(bufferCopy.length);
|
||||
expect(buffer).to.deep.equal(bufferCopy);
|
||||
this.db.prepare('insert into entries (rowid, a, b) values (?, ?, ?)').run(0, 'bar', -999);
|
||||
expect(this.db.prepare('select a, b from entries order by rowid limit 2').all())
|
||||
.to.deep.equal([{ a: 'bar', b: -999 }, { a: 'foo', b: 1 }]);
|
||||
});
|
||||
it('should accept the "attached" option', async function () {
|
||||
const smallBuffer = this.db.serialize();
|
||||
this.seed();
|
||||
const bigBuffer = this.db.serialize();
|
||||
this.db.close();
|
||||
this.db = new Database();
|
||||
this.db.prepare('attach ? as other').run(util.current());
|
||||
const smallBuffer2 = this.db.serialize();
|
||||
const bigBuffer2 = this.db.serialize({ attached: 'other' });
|
||||
expect(bigBuffer.length === bigBuffer2.length);
|
||||
expect(bigBuffer).to.deep.equal(bigBuffer2);
|
||||
expect(smallBuffer.length < bigBuffer.length);
|
||||
expect(smallBuffer2.length < bigBuffer.length);
|
||||
expect(smallBuffer).to.not.deep.equal(smallBuffer2);
|
||||
});
|
||||
it('should return a buffer that can be opened with the "readonly" option', async function () {
|
||||
this.seed();
|
||||
const buffer = this.db.serialize();
|
||||
expect(buffer).to.be.an.instanceof(Buffer);
|
||||
expect(buffer.length).to.be.above(1000);
|
||||
this.db.close();
|
||||
this.db = new Database(buffer, { readonly: true });
|
||||
expect(() => this.db.prepare('insert into entries (rowid, a, b) values (?, ?, ?)').run(0, 'bar', -999))
|
||||
.to.throw(Database.SqliteError);
|
||||
expect(this.db.prepare('select a, b from entries order by rowid limit 2').all())
|
||||
.to.deep.equal([{ a: 'foo', b: 1 }, { a: 'foo', b: 2 }]);
|
||||
const bufferCopy = this.db.serialize();
|
||||
expect(buffer.length).to.equal(bufferCopy.length);
|
||||
expect(buffer).to.deep.equal(bufferCopy);
|
||||
});
|
||||
it.skip('should work with an empty database', async function () {
|
||||
this.db.close();
|
||||
this.db = new Database();
|
||||
const buffer = this.db.serialize();
|
||||
expect(buffer).to.be.an.instanceof(Buffer);
|
||||
expect(buffer.length).to.equal(0);
|
||||
this.db.close();
|
||||
this.db = new Database(buffer);
|
||||
expect(this.db.serialize().length).to.equal(0);
|
||||
});
|
||||
});
|
||||
531
test/42.integrity.js
Normal file
531
test/42.integrity.js
Normal file
@ -0,0 +1,531 @@
|
||||
'use strict';
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const Database = require('../.');
|
||||
|
||||
describe('integrity checks', function () {
|
||||
beforeEach(function () {
|
||||
let func = () => {};
|
||||
this.useFunc = fn => { func = fn; };
|
||||
this.db = new Database(util.next());
|
||||
this.db.prepare("CREATE TABLE entries (a TEXT, b INTEGER, c REAL, d BLOB, e TEXT)").run();
|
||||
this.db.prepare("INSERT INTO entries WITH RECURSIVE temp(a, b, c, d, e) AS (SELECT 'foo', 1, 3.14, x'dddddddd', NULL UNION ALL SELECT a, b + 1, c, d, e FROM temp LIMIT 5) SELECT * FROM temp").run();
|
||||
this.db.function('func', x => (func(), x));
|
||||
this.iterator = this.db.prepare("SELECT func(b) from entries ORDER BY rowid");
|
||||
this.reader = this.db.prepare("SELECT func(b) from entries ORDER BY rowid");
|
||||
this.writer = this.db.prepare("UPDATE entries SET c = c + 2.718")
|
||||
});
|
||||
afterEach(function () {
|
||||
this.db.close();
|
||||
});
|
||||
|
||||
const allowed = fn => () => expect(fn).to.not.throw();
|
||||
const blocked = fn => () => expect(fn).to.throw(TypeError);
|
||||
const normally = fn => fn();
|
||||
const whileIterating = (self, fn) => {
|
||||
let count = 0;
|
||||
for (const _ of self.iterator.iterate()) { count += 1; fn(); }
|
||||
expect(count).to.equal(5);
|
||||
};
|
||||
const whileBusy = (self, fn) => {
|
||||
let count = 0;
|
||||
self.useFunc(() => { count += 1; fn(); });
|
||||
self.iterator.all();
|
||||
expect(count).to.equal(5);
|
||||
};
|
||||
const whileClosed = (self, fn) => {
|
||||
self.db.close();
|
||||
fn();
|
||||
};
|
||||
|
||||
describe('Database#prepare()', function () {
|
||||
specify('while iterating (allowed)', function () {
|
||||
whileIterating(this, allowed(() => this.db.prepare('SELECT 555')));
|
||||
whileIterating(this, allowed(() => this.db.prepare('DELETE FROM entries')));
|
||||
normally(allowed(() => this.db.prepare('SELECT 555')));
|
||||
normally(allowed(() => this.db.prepare('DELETE FROM entries')));
|
||||
});
|
||||
specify('while busy (blocked)', function () {
|
||||
whileBusy(this, blocked(() => this.db.prepare('SELECT 555')));
|
||||
whileBusy(this, blocked(() => this.db.prepare('DELETE FROM entries')));
|
||||
normally(allowed(() => this.db.prepare('SELECT 555')));
|
||||
normally(allowed(() => this.db.prepare('DELETE FROM entries')));
|
||||
});
|
||||
specify('while closed (blocked)', function () {
|
||||
whileClosed(this, blocked(() => this.db.prepare('SELECT 555')));
|
||||
whileClosed(this, blocked(() => this.db.prepare('DELETE FROM entries')));
|
||||
});
|
||||
});
|
||||
|
||||
describe('Database#exec()', function () {
|
||||
specify('while iterating (blocked)', function () {
|
||||
whileIterating(this, blocked(() => this.db.exec('SELECT 555')));
|
||||
whileIterating(this, blocked(() => this.db.exec('DELETE FROM entries')));
|
||||
normally(allowed(() => this.db.exec('SELECT 555')));
|
||||
normally(allowed(() => this.db.exec('DELETE FROM entries')));
|
||||
});
|
||||
specify('while busy (blocked)', function () {
|
||||
whileBusy(this, blocked(() => this.db.exec('SELECT 555')));
|
||||
whileBusy(this, blocked(() => this.db.exec('DELETE FROM entries')));
|
||||
normally(allowed(() => this.db.exec('SELECT 555')));
|
||||
normally(allowed(() => this.db.exec('DELETE FROM entries')));
|
||||
});
|
||||
specify('while closed (blocked)', function () {
|
||||
whileClosed(this, blocked(() => this.db.exec('SELECT 555')));
|
||||
whileClosed(this, blocked(() => this.db.exec('DELETE FROM entries')));
|
||||
});
|
||||
});
|
||||
|
||||
describe('Database#pragma()', function () {
|
||||
specify('while iterating (blocked)', function () {
|
||||
whileIterating(this, blocked(() => this.db.pragma('cache_size')));
|
||||
normally(allowed(() => this.db.pragma('cache_size')));
|
||||
});
|
||||
specify('while busy (blocked)', function () {
|
||||
whileBusy(this, blocked(() => this.db.pragma('cache_size')));
|
||||
normally(allowed(() => this.db.pragma('cache_size')));
|
||||
});
|
||||
specify('while closed (blocked)', function () {
|
||||
whileClosed(this, blocked(() => this.db.pragma('cache_size')));
|
||||
});
|
||||
});
|
||||
|
||||
describe('Database#pragma(\'wal_checkpoint(RESTART)\')', function () {
|
||||
specify('while iterating (blocked)', function () {
|
||||
whileIterating(this, blocked(() => this.db.pragma('wal_checkpoint(RESTART)')));
|
||||
normally(allowed(() => this.db.pragma('wal_checkpoint(RESTART)')));
|
||||
});
|
||||
specify('while busy (blocked)', function () {
|
||||
whileBusy(this, blocked(() => this.db.pragma('wal_checkpoint(RESTART)')));
|
||||
normally(allowed(() => this.db.pragma('wal_checkpoint(RESTART)')));
|
||||
});
|
||||
specify('while closed (blocked)', function () {
|
||||
whileClosed(this, blocked(() => this.db.pragma('wal_checkpoint(RESTART)')));
|
||||
});
|
||||
});
|
||||
|
||||
describe('Database#backup()', function () {
|
||||
specify('while iterating (allowed)', async function () {
|
||||
const promises = [];
|
||||
whileIterating(this, allowed(() => promises.push(this.db.backup(util.next()))));
|
||||
expect(promises.length).to.equal(5);
|
||||
return Promise.all(promises);
|
||||
});
|
||||
specify('while busy (allowed)', async function () {
|
||||
const promises = [];
|
||||
whileBusy(this, allowed(() => promises.push(this.db.backup(util.next()))));
|
||||
expect(promises.length).to.equal(5);
|
||||
return Promise.all(promises);
|
||||
});
|
||||
specify('while closed (blocked)', async function () {
|
||||
const promises = [];
|
||||
whileClosed(this, allowed(() => promises.push(this.db.backup(util.next()))));
|
||||
expect(promises.length).to.equal(1);
|
||||
return Promise.all(promises.map(p =>
|
||||
p.then(() => { throw new Error('Promise should have been rejected'); }, () => {})
|
||||
));
|
||||
});
|
||||
});
|
||||
|
||||
describe('Database#function()', function () {
|
||||
specify('while iterating (blocked)', function () {
|
||||
let i = 0;
|
||||
whileIterating(this, blocked(() => this.db.function(`fn_${++i}`, () => {})));
|
||||
expect(i).to.equal(5);
|
||||
normally(allowed(() => this.db.function(`fn_${++i}`, () => {})));
|
||||
});
|
||||
specify('while busy (blocked)', function () {
|
||||
let i = 0;
|
||||
whileBusy(this, blocked(() => this.db.function(`fn_${++i}`, () => {})));
|
||||
expect(i).to.equal(5);
|
||||
normally(allowed(() => this.db.function(`fn_${++i}`, () => {})));
|
||||
});
|
||||
specify('while closed (blocked)', function () {
|
||||
let i = 0;
|
||||
whileClosed(this, blocked(() => this.db.function(`fn_${++i}`, () => {})));
|
||||
expect(i).to.equal(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Database#aggregate()', function () {
|
||||
specify('while iterating (blocked)', function () {
|
||||
let i = 0;
|
||||
whileIterating(this, blocked(() => this.db.aggregate(`agg_${++i}`, { step: () => {} })));
|
||||
expect(i).to.equal(5);
|
||||
normally(allowed(() => this.db.aggregate(`agg_${++i}`, { step: () => {} })));
|
||||
});
|
||||
specify('while busy (blocked)', function () {
|
||||
let i = 0;
|
||||
whileBusy(this, blocked(() => this.db.aggregate(`agg_${++i}`, { step: () => {} })));
|
||||
expect(i).to.equal(5);
|
||||
normally(allowed(() => this.db.aggregate(`agg_${++i}`, { step: () => {} })));
|
||||
});
|
||||
specify('while closed (blocked)', function () {
|
||||
let i = 0;
|
||||
whileClosed(this, blocked(() => this.db.aggregate(`agg_${++i}`, { step: () => {} })));
|
||||
expect(i).to.equal(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Database#table()', function () {
|
||||
specify('while iterating (blocked)', function () {
|
||||
let i = 0;
|
||||
whileIterating(this, blocked(() => this.db.table(`tbl_${++i}`, { columns: ['x'], *rows() {} })));
|
||||
expect(i).to.equal(5);
|
||||
normally(allowed(() => this.db.table(`tbl_${++i}`, { columns: ['x'], *rows() {} })));
|
||||
});
|
||||
specify('while busy (blocked)', function () {
|
||||
let i = 0;
|
||||
whileBusy(this, blocked(() => this.db.table(`tbl_${++i}`, { columns: ['x'], *rows() {} })));
|
||||
expect(i).to.equal(5);
|
||||
normally(allowed(() => this.db.table(`tbl_${++i}`, { columns: ['x'], *rows() {} })));
|
||||
});
|
||||
specify('while closed (blocked)', function () {
|
||||
let i = 0;
|
||||
whileClosed(this, blocked(() => this.db.table(`tbl_${++i}`, { columns: ['x'], *rows() {} })));
|
||||
expect(i).to.equal(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Database#loadExtension()', function () {
|
||||
let filepath;
|
||||
before(function () {
|
||||
const releaseFilepath = path.join(__dirname, '..', 'build', 'Release', 'test_extension.node');
|
||||
const debugFilepath = path.join(__dirname, '..', 'build', 'Debug', 'test_extension.node');
|
||||
try {
|
||||
fs.accessSync(releaseFilepath);
|
||||
filepath = releaseFilepath;
|
||||
} catch (_) {
|
||||
fs.accessSync(debugFilepath);
|
||||
filepath = debugFilepath;
|
||||
}
|
||||
});
|
||||
|
||||
specify('while iterating (blocked)', function () {
|
||||
whileIterating(this, blocked(() => this.db.loadExtension(filepath)));
|
||||
normally(allowed(() => this.db.loadExtension(filepath)));
|
||||
});
|
||||
specify('while busy (blocked)', function () {
|
||||
whileBusy(this, blocked(() => this.db.loadExtension(filepath)));
|
||||
normally(allowed(() => this.db.loadExtension(filepath)));
|
||||
});
|
||||
specify('while closed (blocked)', function () {
|
||||
whileClosed(this, blocked(() => this.db.loadExtension(filepath)));
|
||||
});
|
||||
});
|
||||
|
||||
describe('Database#close()', function () {
|
||||
specify('while iterating (blocked)', function () {
|
||||
whileIterating(this, blocked(() => this.db.close()));
|
||||
normally(allowed(() => this.db.close()));
|
||||
});
|
||||
specify('while busy (blocked)', function () {
|
||||
whileBusy(this, blocked(() => this.db.close()));
|
||||
normally(allowed(() => this.db.close()));
|
||||
});
|
||||
specify('while closed (allowed)', function () {
|
||||
whileClosed(this, allowed(() => this.db.close()));
|
||||
});
|
||||
});
|
||||
|
||||
describe('Database#defaultSafeIntegers()', function () {
|
||||
specify('while iterating (allowed)', function () {
|
||||
let bool = true;
|
||||
whileIterating(this, allowed(() => this.db.defaultSafeIntegers(bool = !bool)));
|
||||
});
|
||||
specify('while busy (allowed)', function () {
|
||||
let bool = true;
|
||||
whileBusy(this, allowed(() => this.db.defaultSafeIntegers(bool = !bool)));
|
||||
});
|
||||
specify('while closed (allowed)', function () {
|
||||
let bool = true;
|
||||
whileClosed(this, allowed(() => this.db.defaultSafeIntegers(bool = !bool)));
|
||||
});
|
||||
});
|
||||
|
||||
describe('Database#open', function () {
|
||||
specify('while iterating (allowed)', function () {
|
||||
whileIterating(this, allowed(() => expect(this.db.open).to.be.true));
|
||||
});
|
||||
specify('while busy (allowed)', function () {
|
||||
whileBusy(this, allowed(() => expect(this.db.open).to.be.true));
|
||||
});
|
||||
specify('while closed (allowed)', function () {
|
||||
whileClosed(this, allowed(() => expect(this.db.open).to.be.false));
|
||||
});
|
||||
});
|
||||
|
||||
describe('Database#inTransaction', function () {
|
||||
specify('while iterating (allowed)', function () {
|
||||
this.db.exec('BEGIN');
|
||||
whileIterating(this, allowed(() => expect(this.db.inTransaction).to.be.true));
|
||||
});
|
||||
specify('while busy (allowed)', function () {
|
||||
this.db.exec('BEGIN');
|
||||
whileBusy(this, allowed(() => expect(this.db.inTransaction).to.be.true));
|
||||
});
|
||||
specify('while closed (allowed)', function () {
|
||||
this.db.exec('BEGIN');
|
||||
whileClosed(this, allowed(() => expect(this.db.inTransaction).to.be.false));
|
||||
});
|
||||
});
|
||||
|
||||
describe('Statement#run()', function () {
|
||||
specify('while iterating (blocked)', function () {
|
||||
whileIterating(this, blocked(() => this.writer.run()));
|
||||
normally(allowed(() => this.writer.run()));
|
||||
});
|
||||
specify('while busy (blocked)', function () {
|
||||
whileBusy(this, blocked(() => this.writer.run()));
|
||||
normally(allowed(() => this.writer.run()));
|
||||
});
|
||||
specify('while closed (blocked)', function () {
|
||||
whileClosed(this, blocked(() => this.writer.run()));
|
||||
});
|
||||
});
|
||||
|
||||
describe('Statement#get()', function () {
|
||||
specify('while iterating (allowed)', function () {
|
||||
whileIterating(this, allowed(() => this.reader.get()));
|
||||
normally(allowed(() => this.reader.get()));
|
||||
});
|
||||
specify('while self-iterating (blocked)', function () {
|
||||
whileIterating(this, blocked(() => this.iterator.get()));
|
||||
normally(allowed(() => this.iterator.get()));
|
||||
});
|
||||
specify('while busy (blocked)', function () {
|
||||
whileBusy(this, blocked(() => this.reader.get()));
|
||||
normally(allowed(() => this.reader.get()));
|
||||
});
|
||||
specify('while closed (blocked)', function () {
|
||||
whileClosed(this, blocked(() => this.reader.get()));
|
||||
});
|
||||
});
|
||||
|
||||
describe('Statement#all()', function () {
|
||||
specify('while iterating (allowed)', function () {
|
||||
whileIterating(this, allowed(() => this.reader.all()));
|
||||
normally(allowed(() => this.reader.all()));
|
||||
});
|
||||
specify('while self-iterating (blocked)', function () {
|
||||
whileIterating(this, blocked(() => this.iterator.all()));
|
||||
normally(allowed(() => this.iterator.all()));
|
||||
});
|
||||
specify('while busy (blocked)', function () {
|
||||
whileBusy(this, blocked(() => this.reader.all()));
|
||||
normally(allowed(() => this.reader.all()));
|
||||
});
|
||||
specify('while closed (blocked)', function () {
|
||||
whileClosed(this, blocked(() => this.reader.all()));
|
||||
});
|
||||
});
|
||||
|
||||
describe('Statement#iterate()', function () {
|
||||
specify('while iterating (allowed)', function () {
|
||||
whileIterating(this, allowed(() => Array.from(this.reader.iterate())));
|
||||
normally(allowed(() => Array.from(this.reader.iterate())));
|
||||
});
|
||||
specify('while self-iterating (blocked)', function () {
|
||||
whileIterating(this, blocked(() => Array.from(this.iterator.iterate())));
|
||||
normally(allowed(() => Array.from(this.iterator.iterate())));
|
||||
});
|
||||
specify('while busy (blocked)', function () {
|
||||
whileBusy(this, blocked(() => Array.from(this.reader.iterate())));
|
||||
normally(allowed(() => Array.from(this.reader.iterate())));
|
||||
});
|
||||
specify('while closed (blocked)', function () {
|
||||
whileClosed(this, blocked(() => Array.from(this.reader.iterate())));
|
||||
});
|
||||
});
|
||||
|
||||
describe('Statement#bind()', function () {
|
||||
const bind = (stmt) => {
|
||||
if (!stmt.__bound) {
|
||||
stmt.bind();
|
||||
stmt.__bound = true;
|
||||
}
|
||||
};
|
||||
specify('while iterating (allowed)', function () {
|
||||
whileIterating(this, allowed(() => bind(this.reader)));
|
||||
});
|
||||
specify('while self-iterating (blocked)', function () {
|
||||
whileIterating(this, blocked(() => bind(this.iterator)));
|
||||
normally(allowed(() => bind(this.iterator)));
|
||||
});
|
||||
specify('while busy (blocked)', function () {
|
||||
whileBusy(this, blocked(() => bind(this.reader)));
|
||||
normally(allowed(() => bind(this.reader)));
|
||||
});
|
||||
specify('while closed (blocked)', function () {
|
||||
whileClosed(this, blocked(() => bind(this.reader)));
|
||||
});
|
||||
});
|
||||
|
||||
describe('Statement#pluck()', function () {
|
||||
specify('while iterating (allowed)', function () {
|
||||
whileIterating(this, allowed(() => this.reader.pluck()));
|
||||
normally(allowed(() => this.reader.pluck()));
|
||||
});
|
||||
specify('while self-iterating (blocked)', function () {
|
||||
whileIterating(this, blocked(() => this.iterator.pluck()));
|
||||
normally(allowed(() => this.iterator.pluck()));
|
||||
});
|
||||
specify('while busy (blocked)', function () {
|
||||
whileBusy(this, blocked(() => this.reader.pluck()));
|
||||
normally(allowed(() => this.reader.pluck()));
|
||||
});
|
||||
specify('while closed (allowed)', function () {
|
||||
whileClosed(this, allowed(() => this.reader.pluck()));
|
||||
});
|
||||
});
|
||||
|
||||
describe('Statement#expand()', function () {
|
||||
specify('while iterating (allowed)', function () {
|
||||
whileIterating(this, allowed(() => this.reader.expand()));
|
||||
normally(allowed(() => this.reader.expand()));
|
||||
});
|
||||
specify('while self-iterating (blocked)', function () {
|
||||
whileIterating(this, blocked(() => this.iterator.expand()));
|
||||
normally(allowed(() => this.iterator.expand()));
|
||||
});
|
||||
specify('while busy (blocked)', function () {
|
||||
whileBusy(this, blocked(() => this.reader.expand()));
|
||||
normally(allowed(() => this.reader.expand()));
|
||||
});
|
||||
specify('while closed (allowed)', function () {
|
||||
whileClosed(this, allowed(() => this.reader.expand()));
|
||||
});
|
||||
});
|
||||
|
||||
describe('Statement#raw()', function () {
|
||||
specify('while iterating (allowed)', function () {
|
||||
whileIterating(this, allowed(() => this.reader.raw()));
|
||||
normally(allowed(() => this.reader.raw()));
|
||||
});
|
||||
specify('while self-iterating (blocked)', function () {
|
||||
whileIterating(this, blocked(() => this.iterator.raw()));
|
||||
normally(allowed(() => this.iterator.raw()));
|
||||
});
|
||||
specify('while busy (blocked)', function () {
|
||||
whileBusy(this, blocked(() => this.reader.raw()));
|
||||
normally(allowed(() => this.reader.raw()));
|
||||
});
|
||||
specify('while closed (allowed)', function () {
|
||||
whileClosed(this, allowed(() => this.reader.raw()));
|
||||
});
|
||||
});
|
||||
|
||||
describe('Statement#safeIntegers()', function () {
|
||||
specify('while iterating (allowed)', function () {
|
||||
whileIterating(this, allowed(() => this.reader.safeIntegers()));
|
||||
normally(allowed(() => this.reader.safeIntegers()));
|
||||
});
|
||||
specify('while self-iterating (blocked)', function () {
|
||||
whileIterating(this, blocked(() => this.iterator.safeIntegers()));
|
||||
normally(allowed(() => this.iterator.safeIntegers()));
|
||||
});
|
||||
specify('while busy (blocked)', function () {
|
||||
whileBusy(this, blocked(() => this.reader.safeIntegers()));
|
||||
normally(allowed(() => this.reader.safeIntegers()));
|
||||
});
|
||||
specify('while closed (allowed)', function () {
|
||||
whileClosed(this, allowed(() => this.reader.safeIntegers()));
|
||||
});
|
||||
});
|
||||
|
||||
describe('Statement#columns()', function () {
|
||||
specify('while iterating (allowed)', function () {
|
||||
whileIterating(this, allowed(() => this.reader.columns()));
|
||||
normally(allowed(() => this.reader.columns()));
|
||||
});
|
||||
specify('while self-iterating (allowed)', function () {
|
||||
whileIterating(this, allowed(() => this.iterator.columns()));
|
||||
normally(allowed(() => this.iterator.columns()));
|
||||
});
|
||||
specify('while busy (blocked)', function () {
|
||||
whileBusy(this, blocked(() => this.reader.columns()));
|
||||
normally(allowed(() => this.reader.columns()));
|
||||
});
|
||||
specify('while closed (blocked)', function () {
|
||||
whileClosed(this, blocked(() => this.reader.columns()));
|
||||
});
|
||||
});
|
||||
|
||||
describe('StatementIterator#next()', function () {
|
||||
specify('while iterating (allowed)', function () {
|
||||
const iterator = this.reader.iterate();
|
||||
try {
|
||||
whileIterating(this, allowed(() => iterator.next()));
|
||||
normally(allowed(() => iterator.next()));
|
||||
} finally {
|
||||
iterator.return();
|
||||
}
|
||||
});
|
||||
specify('while self-iterating (allowed)', function () {
|
||||
const iterator = this.iterator.iterate();
|
||||
try {
|
||||
let count = 0;
|
||||
for (const _ of iterator) {
|
||||
count += 1;
|
||||
iterator.next();
|
||||
}
|
||||
expect(count).to.equal(3);
|
||||
} finally {
|
||||
iterator.return();
|
||||
}
|
||||
});
|
||||
specify('while busy (blocked)', function () {
|
||||
const iterator = this.reader.iterate();
|
||||
try {
|
||||
whileBusy(this, blocked(() => iterator.next()));
|
||||
normally(allowed(() => iterator.next()));
|
||||
} finally {
|
||||
iterator.return();
|
||||
}
|
||||
});
|
||||
specify('while closed (allowed)', function () {
|
||||
const iterator = this.reader.iterate();
|
||||
iterator.return();
|
||||
whileClosed(this, allowed(() => iterator.next()));
|
||||
});
|
||||
});
|
||||
|
||||
describe('StatementIterator#return()', function () {
|
||||
specify('while iterating (allowed)', function () {
|
||||
const iterator = this.reader.iterate();
|
||||
try {
|
||||
whileIterating(this, allowed(() => iterator.return()));
|
||||
normally(allowed(() => iterator.return()));
|
||||
} finally {
|
||||
iterator.return();
|
||||
}
|
||||
});
|
||||
specify('while self-iterating (allowed)', function () {
|
||||
const iterator = this.iterator.iterate();
|
||||
try {
|
||||
let count = 0;
|
||||
for (const _ of iterator) {
|
||||
count += 1;
|
||||
iterator.return();
|
||||
}
|
||||
expect(count).to.equal(1);
|
||||
} finally {
|
||||
iterator.return();
|
||||
}
|
||||
});
|
||||
specify('while busy (blocked)', function () {
|
||||
const iterator = this.reader.iterate();
|
||||
try {
|
||||
whileBusy(this, blocked(() => iterator.return()));
|
||||
normally(allowed(() => iterator.return()));
|
||||
} finally {
|
||||
iterator.return();
|
||||
}
|
||||
});
|
||||
specify('while closed (allowed)', function () {
|
||||
const iterator = this.reader.iterate();
|
||||
iterator.return();
|
||||
whileClosed(this, allowed(() => iterator.return()));
|
||||
});
|
||||
});
|
||||
});
|
||||
Loading…
Reference in New Issue
Block a user