修改前端架构到Electron ,增加llm_api页面,增加chat SSE渲染,修改若安按钮,卡片名称修改等小功能,增加agent-core,增加SQlite,增加llm UDS Pipe websocke
This commit is contained in:
+59
@@ -0,0 +1,59 @@
|
||||
struct Addon {
|
||||
explicit Addon(Napi::Env env) :
|
||||
privileged_info(NULL),
|
||||
next_id(0),
|
||||
cs(env) {}
|
||||
|
||||
static void Cleanup(Addon* addon) {
|
||||
for (Database* db : addon->dbs) db->CloseHandles();
|
||||
addon->dbs.clear();
|
||||
delete addon;
|
||||
}
|
||||
|
||||
inline sqlite3_uint64 NextId() {
|
||||
return next_id++;
|
||||
}
|
||||
|
||||
static void ConfigureURI() {
|
||||
static std::once_flag init_flag;
|
||||
std::call_once(init_flag, [](){
|
||||
const char* env = getenv("SQLITE_USE_URI");
|
||||
if (env != NULL) {
|
||||
if (strcmp(env, "1") == 0) {
|
||||
int status = sqlite3_config(SQLITE_CONFIG_URI, 1);
|
||||
assert(status == SQLITE_OK); ((void)status);
|
||||
} else if (strcmp(env, "0") == 0) {
|
||||
int status = sqlite3_config(SQLITE_CONFIG_URI, 0);
|
||||
assert(status == SQLITE_OK); ((void)status);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
static NODE_METHOD(JS_initialize) {
|
||||
REQUIRE_ARGUMENT_FUNCTION(first, Napi::Function SqliteError);
|
||||
REQUIRE_ARGUMENT_FUNCTION(second, Napi::Function ArrayFactory);
|
||||
REQUIRE_ARGUMENT_FUNCTION(third, Napi::Function ArrayAppender);
|
||||
REQUIRE_ARGUMENT_FUNCTION(fourth, Napi::Function RowFactory);
|
||||
REQUIRE_ARGUMENT_FUNCTION(fifth, Napi::Function RecordFactory);
|
||||
OnlyAddon->SqliteError = Napi::Persistent(SqliteError);
|
||||
OnlyAddon->ArrayFactory = Napi::Persistent(ArrayFactory);
|
||||
OnlyAddon->ArrayAppender = Napi::Persistent(ArrayAppender);
|
||||
OnlyAddon->RowFactory = Napi::Persistent(RowFactory);
|
||||
OnlyAddon->RecordFactory = Napi::Persistent(RecordFactory);
|
||||
return info.Env().Undefined();
|
||||
}
|
||||
|
||||
Napi::FunctionReference Statement;
|
||||
Napi::FunctionReference StatementIterator;
|
||||
Napi::FunctionReference Backup;
|
||||
Napi::FunctionReference SqliteError;
|
||||
Napi::FunctionReference ArrayFactory;
|
||||
Napi::FunctionReference ArrayAppender;
|
||||
Napi::FunctionReference RowFactory;
|
||||
Napi::FunctionReference RecordFactory;
|
||||
NODE_ARGUMENTS_POINTER privileged_info;
|
||||
sqlite3_uint64 next_id;
|
||||
CS cs;
|
||||
std::set<Database*, Database::CompareDatabase> dbs;
|
||||
};
|
||||
+73
@@ -0,0 +1,73 @@
|
||||
#include <cassert>
|
||||
#include <climits>
|
||||
#include <cmath>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <set>
|
||||
#include <random>
|
||||
#include <unordered_map>
|
||||
#include <algorithm>
|
||||
#include <mutex>
|
||||
#include <sqlite3.h>
|
||||
#include <napi.h>
|
||||
|
||||
struct Addon;
|
||||
class Database;
|
||||
class Statement;
|
||||
class StatementIterator;
|
||||
class Backup;
|
||||
|
||||
#include "util/macros.cpp"
|
||||
#include "util/helpers.cpp"
|
||||
#include "util/constants.cpp"
|
||||
#include "util/bind-map.cpp"
|
||||
#include "util/data-converter.cpp"
|
||||
|
||||
#include "util/row-builder.hpp"
|
||||
#include "objects/backup.hpp"
|
||||
#include "objects/statement.hpp"
|
||||
#include "objects/database.hpp"
|
||||
#include "addon.cpp"
|
||||
#include "objects/statement-iterator.hpp"
|
||||
|
||||
#include "util/data.cpp"
|
||||
#include "util/row-builder.cpp"
|
||||
#include "util/query-macros.cpp"
|
||||
#include "util/custom-function.cpp"
|
||||
#include "util/custom-aggregate.cpp"
|
||||
#include "util/custom-table.cpp"
|
||||
#include "util/binder.cpp"
|
||||
|
||||
#include "objects/backup.cpp"
|
||||
#include "objects/statement.cpp"
|
||||
#include "objects/database.cpp"
|
||||
#include "objects/statement-iterator.cpp"
|
||||
|
||||
Napi::Object InitAll(Napi::Env env, Napi::Object exports) {
|
||||
Napi::HandleScope scope(env);
|
||||
Addon::ConfigureURI();
|
||||
|
||||
// Initialize addon instance. The addon is bound to each native-backed class
|
||||
// as callback data (rather than per-environment instance data) so that
|
||||
// multiple copies of the addon can be loaded into a single environment.
|
||||
Addon* addon = new Addon(env);
|
||||
env.AddCleanupHook(Addon::Cleanup, addon);
|
||||
|
||||
// Create and export native-backed classes and functions.
|
||||
exports.Set("Database", Database::Init(env, addon));
|
||||
exports.Set("Statement", Statement::Init(env, addon));
|
||||
exports.Set("StatementIterator", StatementIterator::Init(env, addon));
|
||||
exports.Set("Backup", Backup::Init(env, addon));
|
||||
exports.Set("initialize", Napi::Function::New(env, Addon::JS_initialize, "initialize", addon));
|
||||
|
||||
// Store addon instance data.
|
||||
addon->Statement = Napi::Persistent(exports.Get("Statement").As<Napi::Function>());
|
||||
addon->StatementIterator = Napi::Persistent(exports.Get("StatementIterator").As<Napi::Function>());
|
||||
addon->Backup = Napi::Persistent(exports.Get("Backup").As<Napi::Function>());
|
||||
|
||||
return exports;
|
||||
}
|
||||
|
||||
NODE_API_MODULE(better_sqlite3, InitAll)
|
||||
+121
@@ -0,0 +1,121 @@
|
||||
const napi_type_tag Backup::TYPE_TAG = RandomTypeTag();
|
||||
|
||||
Backup::Backup(const Napi::CallbackInfo& info) :
|
||||
Napi::ObjectWrap<Backup>(info),
|
||||
db(NULL),
|
||||
dest_handle(NULL),
|
||||
backup_handle(NULL),
|
||||
id(0),
|
||||
alive(false),
|
||||
unlink(false) {
|
||||
TYPE_TAG_CONSTRUCTOR(info);
|
||||
JS_new(info);
|
||||
}
|
||||
|
||||
Backup::~Backup() {
|
||||
if (alive) db->RemoveBackup(this);
|
||||
CloseHandles();
|
||||
}
|
||||
|
||||
// Whenever this is used, db->RemoveBackup must be invoked beforehand.
|
||||
void Backup::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());
|
||||
}
|
||||
}
|
||||
|
||||
INIT(Backup::Init) {
|
||||
return DefineClass(env, "Backup", {
|
||||
PrototypeMethod<Backup, &Backup::JS_transfer>("transfer", addon),
|
||||
PrototypeMethod<Backup, &Backup::JS_close>("close", addon),
|
||||
}, addon);
|
||||
}
|
||||
|
||||
NODE_METHOD(Backup::JS_new) {
|
||||
UseAddon;
|
||||
if (!addon->privileged_info) return ThrowTypeError(info.Env(), "Disabled constructor");
|
||||
assert(info.IsConstructCall());
|
||||
const Napi::CallbackInfo& pinfo = *addon->privileged_info;
|
||||
UNWRAP_OR_RETURN(Database, db, pinfo.This());
|
||||
REQUIRE_DATABASE_OPEN(db->GetState());
|
||||
REQUIRE_DATABASE_NOT_BUSY(db->GetState());
|
||||
|
||||
Napi::Object database = pinfo[0].As<Napi::Object>();
|
||||
Napi::String attachedName = pinfo[1].As<Napi::String>();
|
||||
Napi::String destFile = pinfo[2].As<Napi::String>();
|
||||
bool unlink = pinfo[3].As<Napi::Boolean>().Value();
|
||||
|
||||
UseIsolate;
|
||||
sqlite3* dest_handle;
|
||||
std::string dest_file = destFile.Utf8Value();
|
||||
std::string attached_name = attachedName.Utf8Value();
|
||||
int mask = (SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE);
|
||||
|
||||
if (sqlite3_open_v2(dest_file.c_str(), &dest_handle, mask, NULL) != SQLITE_OK) {
|
||||
Database::ThrowSqliteError(env, addon, dest_handle);
|
||||
int status = sqlite3_close(dest_handle);
|
||||
assert(status == SQLITE_OK); ((void)status);
|
||||
return env.Undefined();
|
||||
}
|
||||
|
||||
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.c_str());
|
||||
if (backup_handle == NULL) {
|
||||
Database::ThrowSqliteError(env, addon, dest_handle);
|
||||
int status = sqlite3_close(dest_handle);
|
||||
assert(status == SQLITE_OK); ((void)status);
|
||||
return env.Undefined();
|
||||
}
|
||||
|
||||
this->db = db;
|
||||
this->dest_handle = dest_handle;
|
||||
this->backup_handle = backup_handle;
|
||||
this->id = addon->NextId();
|
||||
this->unlink = unlink;
|
||||
this->alive = true;
|
||||
db->AddBackup(this);
|
||||
|
||||
SetFrozen(env, info.This().As<Napi::Object>(), addon->cs.database, database);
|
||||
|
||||
return info.This();
|
||||
}
|
||||
|
||||
NODE_METHOD(Backup::JS_transfer) {
|
||||
UNWRAP_OR_RETURN(Backup, 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);
|
||||
|
||||
UseIsolate;
|
||||
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);
|
||||
Napi::Object result = Napi::Object::New(env);
|
||||
result.Set(addon->cs.totalPages.Value(), Napi::Number::New(env, total_pages));
|
||||
result.Set(addon->cs.remainingPages.Value(), Napi::Number::New(env, remaining_pages));
|
||||
if (status == SQLITE_DONE) backup->unlink = false;
|
||||
return result;
|
||||
} else {
|
||||
Database::ThrowSqliteError(env, addon, sqlite3_errstr(status), status);
|
||||
return env.Undefined();
|
||||
}
|
||||
}
|
||||
|
||||
NODE_METHOD(Backup::JS_close) {
|
||||
UNWRAP_OR_RETURN(Backup, backup, info.This());
|
||||
assert(backup->db->GetState()->busy == false);
|
||||
if (backup->alive) backup->db->RemoveBackup(backup);
|
||||
backup->CloseHandles();
|
||||
return info.This();
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
class Backup : public Napi::ObjectWrap<Backup> {
|
||||
public:
|
||||
|
||||
explicit Backup(const Napi::CallbackInfo& info);
|
||||
~Backup();
|
||||
|
||||
// Whenever this is used, db->RemoveBackup must be invoked beforehand.
|
||||
void CloseHandles();
|
||||
|
||||
// Used to support ordered containers.
|
||||
static inline bool Compare(Backup const * const a, Backup const * const b) {
|
||||
return a->id < b->id;
|
||||
}
|
||||
|
||||
// Identifies objects that are backed by this class (see IsInstanceOf).
|
||||
static const napi_type_tag TYPE_TAG;
|
||||
|
||||
static INIT(Init);
|
||||
|
||||
private:
|
||||
|
||||
NODE_METHOD(JS_new);
|
||||
static NODE_METHOD(JS_transfer);
|
||||
static NODE_METHOD(JS_close);
|
||||
|
||||
Database* db;
|
||||
sqlite3* dest_handle;
|
||||
sqlite3_backup* backup_handle;
|
||||
sqlite3_uint64 id;
|
||||
bool alive;
|
||||
bool unlink;
|
||||
};
|
||||
+432
@@ -0,0 +1,432 @@
|
||||
// Node-API does not expose the engine's maximum Buffer/String sizes, so we
|
||||
// hardcode V8's known limits: Buffers are larger than what sqlite3_limit can
|
||||
// express (INT_MAX), and strings are limited to v8::String::kMaxLength, which
|
||||
// is (1 << 29) - 24 bytes on 64-bit platforms (as of Node.js 22). Keeping
|
||||
// these limits ensures that oversized values fail cleanly inside SQLite with
|
||||
// SQLITE_TOOBIG, instead of failing during data conversion.
|
||||
const int Database::MAX_BUFFER_SIZE = INT_MAX;
|
||||
const int Database::MAX_STRING_SIZE = (1 << 29) - 24;
|
||||
|
||||
const napi_type_tag Database::TYPE_TAG = RandomTypeTag();
|
||||
|
||||
Database::Database(const Napi::CallbackInfo& info) :
|
||||
Napi::ObjectWrap<Database>(info),
|
||||
db_handle(NULL),
|
||||
open(false),
|
||||
busy(false),
|
||||
safe_ints(false),
|
||||
unsafe_mode(false),
|
||||
was_js_error(false),
|
||||
has_logger(false),
|
||||
iterators(0),
|
||||
addon(static_cast<Addon*>(info.Data())),
|
||||
logger(),
|
||||
stmts(),
|
||||
backups() {
|
||||
TYPE_TAG_CONSTRUCTOR(info);
|
||||
JS_new(info);
|
||||
}
|
||||
|
||||
Database::~Database() {
|
||||
if (open) addon->dbs.erase(this);
|
||||
CloseHandles();
|
||||
}
|
||||
|
||||
// Whenever this is used, addon->dbs.erase() must be invoked beforehand.
|
||||
void Database::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);
|
||||
}
|
||||
}
|
||||
|
||||
void Database::ThrowDatabaseError(Napi::Env env) {
|
||||
if (was_js_error) was_js_error = false;
|
||||
else ThrowSqliteError(env, addon, db_handle);
|
||||
}
|
||||
|
||||
void Database::ThrowSqliteError(Napi::Env env, Addon* addon, sqlite3* db_handle) {
|
||||
assert(db_handle != NULL);
|
||||
ThrowSqliteError(env, addon, sqlite3_errmsg(db_handle), sqlite3_extended_errcode(db_handle));
|
||||
}
|
||||
|
||||
void Database::ThrowSqliteError(Napi::Env env, 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);
|
||||
Napi::Object error = addon->SqliteError.New({
|
||||
StringFromUtf8(env, message, -1),
|
||||
addon->cs.Code(env, code)
|
||||
});
|
||||
// Constructing the SqliteError can itself throw (e.g., if the user's error
|
||||
// constructor throws); in that case, let that exception propagate instead
|
||||
// of trying to throw a second time.
|
||||
if (!env.IsExceptionPending()) {
|
||||
Napi::Error(env, error).ThrowAsJavaScriptException();
|
||||
}
|
||||
}
|
||||
|
||||
// Allows Statements to log their executed SQL.
|
||||
bool Database::Log(Napi::Env env, sqlite3_stmt* handle) {
|
||||
assert(was_js_error == false);
|
||||
if (!has_logger) return false;
|
||||
char* expanded = sqlite3_expanded_sql(handle);
|
||||
napi_value arg = StringFromUtf8(env, expanded ? expanded : sqlite3_sql(handle), -1);
|
||||
SafeCall(env, logger.Value().As<Napi::Function>(), env.Undefined(), 1, &arg);
|
||||
was_js_error = env.IsExceptionPending();
|
||||
if (expanded) sqlite3_free(expanded);
|
||||
return was_js_error;
|
||||
}
|
||||
|
||||
bool Database::Deserialize(
|
||||
Napi::Env env,
|
||||
Napi::Object buffer,
|
||||
Addon* addon,
|
||||
sqlite3* db_handle,
|
||||
bool readonly
|
||||
) {
|
||||
Napi::Buffer<char> buf = buffer.As<Napi::Buffer<char>>();
|
||||
size_t length = buf.Length();
|
||||
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(env, "Out of memory");
|
||||
return false;
|
||||
}
|
||||
memcpy(data, buf.Data(), length);
|
||||
}
|
||||
|
||||
int status = sqlite3_deserialize(db_handle, "main", data, length, length, flags);
|
||||
if (status != SQLITE_OK) {
|
||||
ThrowSqliteError(env, addon, status == SQLITE_ERROR ? "unable to deserialize database" : sqlite3_errstr(status), status);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void Database::FreeSerialization(Napi::Env env, char* data) {
|
||||
sqlite3_free(data);
|
||||
}
|
||||
|
||||
INIT(Database::Init) {
|
||||
return DefineClass(env, "Database", {
|
||||
PrototypeMethod<Database, &Database::JS_prepare>("prepare", addon),
|
||||
PrototypeMethod<Database, &Database::JS_exec>("exec", addon),
|
||||
PrototypeMethod<Database, &Database::JS_backup>("backup", addon),
|
||||
PrototypeMethod<Database, &Database::JS_serialize>("serialize", addon),
|
||||
PrototypeMethod<Database, &Database::JS_function>("function", addon),
|
||||
PrototypeMethod<Database, &Database::JS_aggregate>("aggregate", addon),
|
||||
PrototypeMethod<Database, &Database::JS_table>("table", addon),
|
||||
PrototypeMethod<Database, &Database::JS_loadExtension>("loadExtension", addon),
|
||||
PrototypeMethod<Database, &Database::JS_close>("close", addon),
|
||||
PrototypeMethod<Database, &Database::JS_defaultSafeIntegers>("defaultSafeIntegers", addon),
|
||||
PrototypeMethod<Database, &Database::JS_unsafeMode>("unsafeMode", addon),
|
||||
}, addon);
|
||||
}
|
||||
|
||||
NODE_METHOD(Database::JS_new) {
|
||||
assert(info.IsConstructCall());
|
||||
REQUIRE_ARGUMENT_STRING(first, Napi::String filename);
|
||||
REQUIRE_ARGUMENT_STRING(second, Napi::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, Napi::Value logger);
|
||||
REQUIRE_ARGUMENT_ANY(eighth, Napi::Value buffer);
|
||||
|
||||
UseAddon;
|
||||
UseIsolate;
|
||||
sqlite3* db_handle;
|
||||
std::string utf8 = filename.Utf8Value();
|
||||
int mask = readonly ? SQLITE_OPEN_READONLY
|
||||
: must_exist ? SQLITE_OPEN_READWRITE
|
||||
: (SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE);
|
||||
|
||||
if (sqlite3_open_v2(utf8.c_str(), &db_handle, mask, NULL) != SQLITE_OK) {
|
||||
ThrowSqliteError(env, addon, db_handle);
|
||||
int status = sqlite3_close(db_handle);
|
||||
assert(status == SQLITE_OK); ((void)status);
|
||||
return env.Undefined();
|
||||
}
|
||||
|
||||
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); ((void)status);
|
||||
status = sqlite3_db_config(db_handle, SQLITE_DBCONFIG_DEFENSIVE, 1, NULL);
|
||||
assert(status == SQLITE_OK); ((void)status);
|
||||
|
||||
if (buffer.IsBuffer() && !Deserialize(env, buffer.As<Napi::Object>(), addon, db_handle, readonly)) {
|
||||
int status = sqlite3_close(db_handle);
|
||||
assert(status == SQLITE_OK); ((void)status);
|
||||
return env.Undefined();
|
||||
}
|
||||
|
||||
this->db_handle = db_handle;
|
||||
open = true;
|
||||
has_logger = logger.IsFunction();
|
||||
if (has_logger) this->logger.Reset(logger, 1);
|
||||
addon->dbs.insert(this);
|
||||
|
||||
Napi::Object _this = info.This().As<Napi::Object>();
|
||||
SetFrozen(env, _this, addon->cs.memory, Napi::Boolean::New(env, in_memory));
|
||||
SetFrozen(env, _this, addon->cs.readonly, Napi::Boolean::New(env, readonly));
|
||||
SetFrozen(env, _this, addon->cs.name, filenameGiven);
|
||||
SetInstanceGetter<Database, &Database::JS_open>(_this, "open", addon);
|
||||
SetInstanceGetter<Database, &Database::JS_inTransaction>(_this, "inTransaction", addon);
|
||||
|
||||
return info.This();
|
||||
}
|
||||
|
||||
NODE_METHOD(Database::JS_prepare) {
|
||||
REQUIRE_ARGUMENT_STRING(first, Napi::String source);
|
||||
REQUIRE_ARGUMENT_OBJECT(second, Napi::Object database);
|
||||
REQUIRE_ARGUMENT_BOOLEAN(third, bool pragmaMode);
|
||||
REQUIRE_ARGUMENT_BOOLEAN(fourth, bool explainMode);
|
||||
(void)source;
|
||||
(void)database;
|
||||
(void)pragmaMode;
|
||||
(void)explainMode;
|
||||
UseAddon;
|
||||
UseIsolate;
|
||||
Napi::Function c = addon->Statement.Value();
|
||||
addon->privileged_info = &info;
|
||||
Napi::Object statement = SafeConstruct(env, c);
|
||||
addon->privileged_info = NULL;
|
||||
if (env.IsExceptionPending()) return env.Undefined();
|
||||
return statement;
|
||||
}
|
||||
|
||||
NODE_METHOD(Database::JS_exec) {
|
||||
UNWRAP_OR_RETURN(Database, db, info.This());
|
||||
REQUIRE_ARGUMENT_STRING(first, Napi::String source);
|
||||
REQUIRE_DATABASE_OPEN(db);
|
||||
REQUIRE_DATABASE_NOT_BUSY(db);
|
||||
REQUIRE_DATABASE_NO_ITERATORS_UNLESS_UNSAFE(db);
|
||||
db->busy = true;
|
||||
|
||||
UseIsolate;
|
||||
std::string utf8 = source.Utf8Value();
|
||||
const char* sql = utf8.c_str();
|
||||
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(env, 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(env);
|
||||
}
|
||||
return env.Undefined();
|
||||
}
|
||||
|
||||
NODE_METHOD(Database::JS_backup) {
|
||||
REQUIRE_ARGUMENT_OBJECT(first, Napi::Object database);
|
||||
REQUIRE_ARGUMENT_STRING(second, Napi::String attachedName);
|
||||
REQUIRE_ARGUMENT_STRING(third, Napi::String destFile);
|
||||
REQUIRE_ARGUMENT_BOOLEAN(fourth, bool unlink);
|
||||
(void)database;
|
||||
(void)attachedName;
|
||||
(void)destFile;
|
||||
(void)unlink;
|
||||
UseAddon;
|
||||
UseIsolate;
|
||||
Napi::Function c = addon->Backup.Value();
|
||||
addon->privileged_info = &info;
|
||||
Napi::Object backup = SafeConstruct(env, c);
|
||||
addon->privileged_info = NULL;
|
||||
if (env.IsExceptionPending()) return env.Undefined();
|
||||
return backup;
|
||||
}
|
||||
|
||||
NODE_METHOD(Database::JS_serialize) {
|
||||
UNWRAP_OR_RETURN(Database, db, info.This());
|
||||
REQUIRE_ARGUMENT_STRING(first, Napi::String attachedName);
|
||||
REQUIRE_DATABASE_OPEN(db);
|
||||
REQUIRE_DATABASE_NOT_BUSY(db);
|
||||
REQUIRE_DATABASE_NO_ITERATORS(db);
|
||||
|
||||
UseIsolate;
|
||||
std::string attached_name = attachedName.Utf8Value();
|
||||
sqlite3_int64 length = -1;
|
||||
unsigned char* data = sqlite3_serialize(db->db_handle, attached_name.c_str(), &length, 0);
|
||||
|
||||
if (!data && length) {
|
||||
return ThrowError(env, "Out of memory");
|
||||
}
|
||||
|
||||
return Napi::Buffer<char>::NewOrCopy(env, reinterpret_cast<char*>(data), length, FreeSerialization);
|
||||
}
|
||||
|
||||
NODE_METHOD(Database::JS_function) {
|
||||
UNWRAP_OR_RETURN(Database, db, info.This());
|
||||
REQUIRE_ARGUMENT_FUNCTION(first, Napi::Function fn);
|
||||
REQUIRE_ARGUMENT_STRING(second, Napi::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;
|
||||
std::string name = nameString.Utf8Value();
|
||||
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.c_str(), argc, mask, new CustomFunction(env, db, name.c_str(), fn, safe_ints), CustomFunction::xFunc, NULL, NULL, CustomFunction::xDestroy) != SQLITE_OK) {
|
||||
db->ThrowDatabaseError(env);
|
||||
}
|
||||
return env.Undefined();
|
||||
}
|
||||
|
||||
NODE_METHOD(Database::JS_aggregate) {
|
||||
UNWRAP_OR_RETURN(Database, db, info.This());
|
||||
REQUIRE_ARGUMENT_ANY(first, Napi::Value start);
|
||||
REQUIRE_ARGUMENT_FUNCTION(second, Napi::Function step);
|
||||
REQUIRE_ARGUMENT_ANY(third, Napi::Value inverse);
|
||||
REQUIRE_ARGUMENT_ANY(fourth, Napi::Value result);
|
||||
REQUIRE_ARGUMENT_STRING(fifth, Napi::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;
|
||||
std::string name = nameString.Utf8Value();
|
||||
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.c_str(), argc, mask, new CustomAggregate(env, db, name.c_str(), start, step, inverse, result, safe_ints), CustomAggregate::xStep, CustomAggregate::xFinal, xValue, xInverse, CustomAggregate::xDestroy) != SQLITE_OK) {
|
||||
db->ThrowDatabaseError(env);
|
||||
}
|
||||
return env.Undefined();
|
||||
}
|
||||
|
||||
NODE_METHOD(Database::JS_table) {
|
||||
UNWRAP_OR_RETURN(Database, db, info.This());
|
||||
REQUIRE_ARGUMENT_FUNCTION(first, Napi::Function factory);
|
||||
REQUIRE_ARGUMENT_STRING(second, Napi::String nameString);
|
||||
REQUIRE_ARGUMENT_BOOLEAN(third, bool eponymous);
|
||||
REQUIRE_DATABASE_OPEN(db);
|
||||
REQUIRE_DATABASE_NOT_BUSY(db);
|
||||
REQUIRE_DATABASE_NO_ITERATORS(db);
|
||||
|
||||
UseIsolate;
|
||||
std::string name = nameString.Utf8Value();
|
||||
sqlite3_module* module = eponymous ? &CustomTable::EPONYMOUS_MODULE : &CustomTable::MODULE;
|
||||
|
||||
db->busy = true;
|
||||
if (sqlite3_create_module_v2(db->db_handle, name.c_str(), module, new CustomTable(env, db, name.c_str(), factory), CustomTable::Destructor) != SQLITE_OK) {
|
||||
db->ThrowDatabaseError(env);
|
||||
}
|
||||
db->busy = false;
|
||||
return env.Undefined();
|
||||
}
|
||||
|
||||
NODE_METHOD(Database::JS_loadExtension) {
|
||||
UNWRAP_OR_RETURN(Database, db, info.This());
|
||||
Napi::String entryPoint;
|
||||
REQUIRE_ARGUMENT_STRING(first, Napi::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;
|
||||
std::string filename_utf8 = filename.Utf8Value();
|
||||
std::string entry_utf8;
|
||||
bool has_entry = !entryPoint.IsEmpty();
|
||||
if (has_entry) entry_utf8 = entryPoint.Utf8Value();
|
||||
int status = sqlite3_load_extension(
|
||||
db->db_handle,
|
||||
filename_utf8.c_str(),
|
||||
has_entry ? entry_utf8.c_str() : NULL,
|
||||
&error
|
||||
);
|
||||
if (status != SQLITE_OK) {
|
||||
ThrowSqliteError(env, db->addon, error, status);
|
||||
}
|
||||
sqlite3_free(error);
|
||||
return env.Undefined();
|
||||
}
|
||||
|
||||
NODE_METHOD(Database::JS_close) {
|
||||
UNWRAP_OR_RETURN(Database, db, info.This());
|
||||
if (db->open) {
|
||||
REQUIRE_DATABASE_NOT_BUSY(db);
|
||||
REQUIRE_DATABASE_NO_ITERATORS(db);
|
||||
db->addon->dbs.erase(db);
|
||||
db->CloseHandles();
|
||||
}
|
||||
return info.Env().Undefined();
|
||||
}
|
||||
|
||||
NODE_METHOD(Database::JS_defaultSafeIntegers) {
|
||||
UNWRAP_OR_RETURN(Database, db, info.This());
|
||||
if (info.Length() == 0) db->safe_ints = true;
|
||||
else { REQUIRE_ARGUMENT_BOOLEAN(first, db->safe_ints); }
|
||||
return info.Env().Undefined();
|
||||
}
|
||||
|
||||
NODE_METHOD(Database::JS_unsafeMode) {
|
||||
UNWRAP_OR_RETURN(Database, db, 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);
|
||||
return info.Env().Undefined();
|
||||
}
|
||||
|
||||
NODE_GETTER(Database::JS_open) {
|
||||
UNWRAP_OR_RETURN(Database, db, info.This());
|
||||
return Napi::Boolean::New(info.Env(), db->open);
|
||||
}
|
||||
|
||||
NODE_GETTER(Database::JS_inTransaction) {
|
||||
UNWRAP_OR_RETURN(Database, db, info.This());
|
||||
return Napi::Boolean::New(info.Env(), db->open && !static_cast<bool>(sqlite3_get_autocommit(db->db_handle)));
|
||||
}
|
||||
+100
@@ -0,0 +1,100 @@
|
||||
class Database : public Napi::ObjectWrap<Database> {
|
||||
public:
|
||||
|
||||
explicit Database(const Napi::CallbackInfo& info);
|
||||
~Database();
|
||||
|
||||
// Whenever this is used, addon->dbs.erase() must be invoked beforehand.
|
||||
void CloseHandles();
|
||||
|
||||
// Used to support ordered containers.
|
||||
class CompareDatabase { public:
|
||||
inline bool operator() (Database const * const a, Database const * const b) const {
|
||||
return a < b;
|
||||
}
|
||||
};
|
||||
class CompareStatement { public:
|
||||
inline bool operator() (Statement const * const a, Statement const * const b) const {
|
||||
return Statement::Compare(a, b);
|
||||
}
|
||||
};
|
||||
class CompareBackup { public:
|
||||
inline 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(Napi::Env env);
|
||||
static void ThrowSqliteError(Napi::Env env, Addon* addon, sqlite3* db_handle);
|
||||
static void ThrowSqliteError(Napi::Env env, Addon* addon, const char* message, int code);
|
||||
|
||||
// Allows Statements to log their executed SQL.
|
||||
bool Log(Napi::Env env, sqlite3_stmt* handle);
|
||||
|
||||
// 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; }
|
||||
|
||||
// Identifies objects that are backed by this class (see IsInstanceOf).
|
||||
static const napi_type_tag TYPE_TAG;
|
||||
|
||||
static INIT(Init);
|
||||
|
||||
private:
|
||||
|
||||
NODE_METHOD(JS_new);
|
||||
static NODE_METHOD(JS_prepare);
|
||||
static NODE_METHOD(JS_exec);
|
||||
static NODE_METHOD(JS_backup);
|
||||
static NODE_METHOD(JS_serialize);
|
||||
static NODE_METHOD(JS_function);
|
||||
static NODE_METHOD(JS_aggregate);
|
||||
static NODE_METHOD(JS_table);
|
||||
static NODE_METHOD(JS_loadExtension);
|
||||
static NODE_METHOD(JS_close);
|
||||
static NODE_METHOD(JS_defaultSafeIntegers);
|
||||
static NODE_METHOD(JS_unsafeMode);
|
||||
static NODE_GETTER(JS_open);
|
||||
static NODE_GETTER(JS_inTransaction);
|
||||
|
||||
static bool Deserialize(Napi::Env env, Napi::Object buffer, Addon* addon, sqlite3* db_handle, bool readonly);
|
||||
static void FreeSerialization(Napi::Env env, char* data);
|
||||
|
||||
static const int MAX_BUFFER_SIZE;
|
||||
static const int MAX_STRING_SIZE;
|
||||
|
||||
sqlite3* db_handle;
|
||||
bool open;
|
||||
bool busy;
|
||||
bool safe_ints;
|
||||
bool unsafe_mode;
|
||||
bool was_js_error;
|
||||
bool has_logger;
|
||||
unsigned short iterators;
|
||||
Addon* const addon;
|
||||
Napi::Reference<Napi::Value> logger;
|
||||
std::set<Statement*, CompareStatement> stmts;
|
||||
std::set<Backup*, CompareBackup> backups;
|
||||
};
|
||||
+119
@@ -0,0 +1,119 @@
|
||||
const napi_type_tag StatementIterator::TYPE_TAG = RandomTypeTag();
|
||||
|
||||
StatementIterator::StatementIterator(const Napi::CallbackInfo& info) :
|
||||
Napi::ObjectWrap<StatementIterator>(info),
|
||||
stmt(NULL),
|
||||
handle(NULL),
|
||||
db_state(NULL),
|
||||
bound(false),
|
||||
safe_ints(false),
|
||||
mode(Data::FLAT),
|
||||
alive(false),
|
||||
logged(false) {
|
||||
TYPE_TAG_CONSTRUCTOR(info);
|
||||
JS_new(info);
|
||||
}
|
||||
|
||||
// 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::~StatementIterator() {}
|
||||
|
||||
Napi::Value StatementIterator::Next(Napi::Env env) {
|
||||
assert(alive == true);
|
||||
db_state->busy = true;
|
||||
if (!logged) {
|
||||
logged = true;
|
||||
if (stmt->db->Log(env, handle)) {
|
||||
db_state->busy = false;
|
||||
return Throw(env);
|
||||
}
|
||||
}
|
||||
int status = sqlite3_step(handle);
|
||||
db_state->busy = false;
|
||||
|
||||
if (status == SQLITE_ROW) {
|
||||
Napi::Value row = Data::GetRowJS(env, stmt, handle, safe_ints, mode);
|
||||
return NewRecord(env, row, db_state->addon, false);
|
||||
} else {
|
||||
if (status == SQLITE_DONE) return Return(env);
|
||||
return Throw(env);
|
||||
}
|
||||
}
|
||||
|
||||
Napi::Value StatementIterator::Return(Napi::Env env) {
|
||||
Cleanup();
|
||||
STATEMENT_RETURN_LOGIC(DoneRecord(env, db_state->addon));
|
||||
}
|
||||
|
||||
Napi::Value StatementIterator::Throw(Napi::Env env) {
|
||||
Cleanup();
|
||||
Database* db = stmt->db;
|
||||
STATEMENT_THROW_LOGIC();
|
||||
}
|
||||
|
||||
void StatementIterator::Cleanup() {
|
||||
assert(alive == true);
|
||||
alive = false;
|
||||
stmt->locked = false;
|
||||
db_state->iterators -= 1;
|
||||
sqlite3_reset(handle);
|
||||
}
|
||||
|
||||
INIT(StatementIterator::Init) {
|
||||
return DefineClass(env, "StatementIterator", {
|
||||
PrototypeMethod<StatementIterator, &StatementIterator::JS_next>("next", addon),
|
||||
PrototypeMethod<StatementIterator, &StatementIterator::JS_return>("return", addon),
|
||||
PrototypeSymbolMethod<StatementIterator, &StatementIterator::JS_symbolIterator>(Napi::Symbol::WellKnown(env, "iterator"), addon),
|
||||
}, addon);
|
||||
}
|
||||
|
||||
NODE_METHOD(StatementIterator::JS_new) {
|
||||
UseAddon;
|
||||
if (!addon->privileged_info) return ThrowTypeError(info.Env(), "Disabled constructor");
|
||||
assert(info.IsConstructCall());
|
||||
|
||||
{
|
||||
const Napi::CallbackInfo& info = *addon->privileged_info;
|
||||
STATEMENT_START_LOGIC(REQUIRE_STATEMENT_RETURNS_DATA, DOES_ADD_ITERATOR);
|
||||
this->stmt = stmt;
|
||||
this->handle = stmt->handle;
|
||||
this->db_state = stmt->db->GetState();
|
||||
this->bound = bound;
|
||||
this->safe_ints = stmt->safe_ints;
|
||||
this->mode = stmt->mode;
|
||||
this->alive = true;
|
||||
this->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;
|
||||
}
|
||||
|
||||
UseIsolate;
|
||||
SetFrozen(env, info.This().As<Napi::Object>(), addon->cs.statement, addon->privileged_info->This());
|
||||
|
||||
return info.This();
|
||||
}
|
||||
|
||||
NODE_METHOD(StatementIterator::JS_next) {
|
||||
UNWRAP_OR_RETURN(StatementIterator, iter, info.This());
|
||||
REQUIRE_DATABASE_NOT_BUSY(iter->db_state);
|
||||
if (iter->alive) return iter->Next(info.Env());
|
||||
return DoneRecord(info.Env(), iter->db_state->addon);
|
||||
}
|
||||
|
||||
NODE_METHOD(StatementIterator::JS_return) {
|
||||
UNWRAP_OR_RETURN(StatementIterator, iter, info.This());
|
||||
REQUIRE_DATABASE_NOT_BUSY(iter->db_state);
|
||||
if (iter->alive) return iter->Return(info.Env());
|
||||
return DoneRecord(info.Env(), iter->db_state->addon);
|
||||
}
|
||||
|
||||
NODE_METHOD(StatementIterator::JS_symbolIterator) {
|
||||
return info.This();
|
||||
}
|
||||
+71
@@ -0,0 +1,71 @@
|
||||
class StatementIterator : public Napi::ObjectWrap<StatementIterator> {
|
||||
public:
|
||||
|
||||
// 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.
|
||||
explicit StatementIterator(const Napi::CallbackInfo& info);
|
||||
~StatementIterator();
|
||||
|
||||
// Identifies objects that are backed by this class (see IsInstanceOf).
|
||||
static const napi_type_tag TYPE_TAG;
|
||||
|
||||
static INIT(Init);
|
||||
|
||||
private:
|
||||
|
||||
Napi::Value Next(Napi::Env env);
|
||||
Napi::Value Return(Napi::Env env);
|
||||
Napi::Value Throw(Napi::Env env);
|
||||
void Cleanup();
|
||||
|
||||
static inline Napi::Object NewRecord(
|
||||
Napi::Env env,
|
||||
Napi::Value value,
|
||||
Addon* addon,
|
||||
bool done
|
||||
) {
|
||||
assert(!addon->RecordFactory.IsEmpty());
|
||||
|
||||
// Fast path, using a factory function from JS land.
|
||||
if (!done) {
|
||||
napi_value arg = value;
|
||||
return SafeCall(env, addon->RecordFactory.Value(), env.Undefined(), 1, &arg)
|
||||
.As<Napi::Object>();
|
||||
}
|
||||
|
||||
// Slow path, only used after the iterator is done.
|
||||
napi_property_descriptor properties[2] = {};
|
||||
properties[0].name = addon->cs.value.Value();
|
||||
properties[0].value = value;
|
||||
properties[0].attributes = DEFAULT_ATTRIBUTES;
|
||||
properties[1].name = addon->cs.done.Value();
|
||||
properties[1].value = Napi::Boolean::New(env, done);
|
||||
properties[1].attributes = DEFAULT_ATTRIBUTES;
|
||||
|
||||
napi_value record;
|
||||
napi_status status = napi_create_object(env, &record);
|
||||
assert(status == napi_ok || status == napi_cannot_run_js);
|
||||
status = napi_define_properties(env, record, 2, properties);
|
||||
assert(status == napi_ok || status == napi_cannot_run_js); ((void)status);
|
||||
return Napi::Object(env, record);
|
||||
}
|
||||
|
||||
static inline Napi::Object DoneRecord(Napi::Env env, Addon* addon) {
|
||||
return NewRecord(env, env.Undefined(), addon, true);
|
||||
}
|
||||
|
||||
NODE_METHOD(JS_new);
|
||||
static NODE_METHOD(JS_next);
|
||||
static NODE_METHOD(JS_return);
|
||||
static NODE_METHOD(JS_symbolIterator);
|
||||
|
||||
Statement* stmt;
|
||||
sqlite3_stmt* handle;
|
||||
Database::State* db_state;
|
||||
bool bound;
|
||||
bool safe_ints;
|
||||
char mode;
|
||||
bool alive;
|
||||
bool logged;
|
||||
};
|
||||
+380
@@ -0,0 +1,380 @@
|
||||
const napi_type_tag Statement::TYPE_TAG = RandomTypeTag();
|
||||
|
||||
Statement::Statement(const Napi::CallbackInfo& info) :
|
||||
Napi::ObjectWrap<Statement>(info),
|
||||
db(NULL),
|
||||
handle(NULL),
|
||||
extras(NULL),
|
||||
alive(false),
|
||||
locked(false),
|
||||
bound(false),
|
||||
has_bind_map(false),
|
||||
safe_ints(false),
|
||||
mode(Data::FLAT),
|
||||
returns_data(false) {
|
||||
TYPE_TAG_CONSTRUCTOR(info);
|
||||
JS_new(info);
|
||||
}
|
||||
|
||||
Statement::~Statement() {
|
||||
if (alive) db->RemoveStatement(this);
|
||||
CloseHandles();
|
||||
delete extras;
|
||||
}
|
||||
|
||||
// Whenever this is used, db->RemoveStatement must be invoked beforehand.
|
||||
void Statement::CloseHandles() {
|
||||
if (alive) {
|
||||
alive = false;
|
||||
sqlite3_finalize(handle);
|
||||
}
|
||||
}
|
||||
|
||||
// Returns the Statement's bind map (creates it upon first execution).
|
||||
BindMap& Statement::GetBindMap(Napi::Env env) {
|
||||
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(env, name + 1, i);
|
||||
}
|
||||
has_bind_map = true;
|
||||
return bind_map;
|
||||
}
|
||||
|
||||
// Returns the Statement's row builder.
|
||||
RowBuilder& Statement::GetRowBuilder() {
|
||||
return extras->row_builder;
|
||||
}
|
||||
|
||||
Statement::Extras::Extras(
|
||||
Napi::Env env,
|
||||
Napi::Function row_factory,
|
||||
Napi::Function array_factory,
|
||||
sqlite3_uint64 id
|
||||
) :
|
||||
bind_map(0),
|
||||
row_builder(env, row_factory, array_factory),
|
||||
id(id) {}
|
||||
|
||||
INIT(Statement::Init) {
|
||||
return DefineClass(env, "Statement", {
|
||||
PrototypeMethod<Statement, &Statement::JS_run>("run", addon),
|
||||
PrototypeMethod<Statement, &Statement::JS_get>("get", addon),
|
||||
PrototypeMethod<Statement, &Statement::JS_all>("all", addon),
|
||||
PrototypeMethod<Statement, &Statement::JS_iterate>("iterate", addon),
|
||||
PrototypeMethod<Statement, &Statement::JS_bind>("bind", addon),
|
||||
PrototypeMethod<Statement, &Statement::JS_pluck>("pluck", addon),
|
||||
PrototypeMethod<Statement, &Statement::JS_expand>("expand", addon),
|
||||
PrototypeMethod<Statement, &Statement::JS_raw>("raw", addon),
|
||||
PrototypeMethod<Statement, &Statement::JS_safeIntegers>("safeIntegers", addon),
|
||||
PrototypeMethod<Statement, &Statement::JS_columns>("columns", addon),
|
||||
PrototypeMethod<Statement, &Statement::JS_toString>("toString", addon),
|
||||
}, addon);
|
||||
}
|
||||
|
||||
NODE_METHOD(Statement::JS_new) {
|
||||
UseAddon;
|
||||
if (!addon->privileged_info) {
|
||||
return ThrowTypeError(info.Env(), "Statements can only be constructed by the db.prepare() method");
|
||||
}
|
||||
assert(info.IsConstructCall());
|
||||
const Napi::CallbackInfo& pinfo = *addon->privileged_info;
|
||||
UNWRAP_OR_RETURN(Database, db, pinfo.This());
|
||||
REQUIRE_DATABASE_OPEN(db->GetState());
|
||||
REQUIRE_DATABASE_NOT_BUSY(db->GetState());
|
||||
|
||||
Napi::String source = pinfo[0].As<Napi::String>();
|
||||
Napi::Object database = pinfo[1].As<Napi::Object>();
|
||||
bool pragmaMode = pinfo[2].As<Napi::Boolean>().Value();
|
||||
bool explainMode = pinfo[3].As<Napi::Boolean>().Value();
|
||||
int flags = SQLITE_PREPARE_PERSISTENT;
|
||||
|
||||
if (pragmaMode) {
|
||||
REQUIRE_DATABASE_NO_ITERATORS_UNLESS_UNSAFE(db->GetState());
|
||||
flags = 0;
|
||||
}
|
||||
if (explainMode) {
|
||||
flags = 0;
|
||||
}
|
||||
|
||||
UseIsolate;
|
||||
std::string utf8 = source.Utf8Value();
|
||||
sqlite3_stmt* handle;
|
||||
const char* tail;
|
||||
|
||||
if (sqlite3_prepare_v3(db->GetHandle(), utf8.c_str(), utf8.length() + 1, flags, &handle, &tail) != SQLITE_OK) {
|
||||
db->ThrowDatabaseError(env);
|
||||
return env.Undefined();
|
||||
}
|
||||
if (handle == NULL) {
|
||||
return ThrowRangeError(env, "The supplied SQL string contains no statements");
|
||||
}
|
||||
// https://github.com/WiseLibs/better-sqlite3/issues/975#issuecomment-1520934678
|
||||
for (char c; (c = *tail); ) {
|
||||
if (IS_SKIPPED(c)) {
|
||||
++tail;
|
||||
continue;
|
||||
}
|
||||
if (c == '/' && tail[1] == '*') {
|
||||
tail += 2;
|
||||
for (char c; (c = *tail); ++tail) {
|
||||
if (c == '*' && tail[1] == '/') {
|
||||
tail += 2;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else if (c == '-' && tail[1] == '-') {
|
||||
tail += 2;
|
||||
for (char c; (c = *tail); ++tail) {
|
||||
if (c == '\n') {
|
||||
++tail;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
sqlite3_finalize(handle);
|
||||
return ThrowRangeError(env, "The supplied SQL string contains more than one statement");
|
||||
}
|
||||
}
|
||||
|
||||
bool returns_data = sqlite3_column_count(handle) >= 1 || pragmaMode;
|
||||
this->db = db;
|
||||
this->handle = handle;
|
||||
this->extras = new Extras(env, addon->RowFactory.Value(), addon->ArrayFactory.Value(), addon->NextId());
|
||||
this->bound = explainMode;
|
||||
this->safe_ints = db->GetState()->safe_ints;
|
||||
this->returns_data = returns_data;
|
||||
this->alive = true;
|
||||
assert(db->GetState()->open);
|
||||
assert(!db->GetState()->busy);
|
||||
db->AddStatement(this);
|
||||
|
||||
Napi::Object _this = info.This().As<Napi::Object>();
|
||||
SetFrozen(env, _this, addon->cs.reader, Napi::Boolean::New(env, returns_data));
|
||||
SetFrozen(env, _this, addon->cs.readonly, Napi::Boolean::New(env, sqlite3_stmt_readonly(handle) != 0));
|
||||
SetFrozen(env, _this, addon->cs.source, source);
|
||||
SetFrozen(env, _this, addon->cs.database, database);
|
||||
SetInstanceGetter<Statement, &Statement::JS_busy>(_this, "busy", addon);
|
||||
|
||||
return info.This();
|
||||
}
|
||||
|
||||
NODE_METHOD(Statement::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();
|
||||
|
||||
napi_property_descriptor properties[2] = {};
|
||||
properties[0].name = addon->cs.changes.Value();
|
||||
properties[0].value = Napi::Number::New(env, changes);
|
||||
properties[0].attributes = DEFAULT_ATTRIBUTES;
|
||||
properties[1].name = addon->cs.lastInsertRowid.Value();
|
||||
if (stmt->safe_ints) {
|
||||
properties[1].value = Napi::BigInt::New(env, (int64_t)id);
|
||||
} else {
|
||||
properties[1].value = Napi::Number::New(env, (double)id);
|
||||
}
|
||||
properties[1].attributes = DEFAULT_ATTRIBUTES;
|
||||
|
||||
napi_value result;
|
||||
napi_status status = napi_create_object(env, &result);
|
||||
assert(status == napi_ok || status == napi_cannot_run_js);
|
||||
status = napi_define_properties(env, result, 2, properties);
|
||||
assert(status == napi_ok || status == napi_cannot_run_js); ((void)status);
|
||||
STATEMENT_RETURN(Napi::Object(env, result));
|
||||
}
|
||||
STATEMENT_THROW();
|
||||
}
|
||||
|
||||
NODE_METHOD(Statement::JS_get) {
|
||||
STATEMENT_START(REQUIRE_STATEMENT_RETURNS_DATA, DOES_NOT_MUTATE);
|
||||
int status = sqlite3_step(handle);
|
||||
if (status == SQLITE_ROW) {
|
||||
Napi::Value result = Data::GetRowJS(env, stmt, handle, stmt->safe_ints, stmt->mode);
|
||||
sqlite3_reset(handle);
|
||||
STATEMENT_RETURN(result);
|
||||
} else if (status == SQLITE_DONE) {
|
||||
sqlite3_reset(handle);
|
||||
STATEMENT_RETURN(env.Undefined());
|
||||
}
|
||||
sqlite3_reset(handle);
|
||||
STATEMENT_THROW();
|
||||
}
|
||||
|
||||
NODE_METHOD(Statement::JS_all) {
|
||||
STATEMENT_START(REQUIRE_STATEMENT_RETURNS_DATA, DOES_NOT_MUTATE);
|
||||
const bool safe_ints = stmt->safe_ints;
|
||||
const char mode = stmt->mode;
|
||||
|
||||
std::vector<napi_value> rows;
|
||||
rows.reserve(8);
|
||||
|
||||
while (sqlite3_step(handle) == SQLITE_ROW) {
|
||||
rows.emplace_back(Data::GetRowJS(env, stmt, handle, safe_ints, mode));
|
||||
}
|
||||
|
||||
if (sqlite3_reset(handle) == SQLITE_OK) {
|
||||
if (rows.size() > 0xffffffff) {
|
||||
ThrowRangeError(env, "Array overflow (too many rows returned)");
|
||||
db->GetState()->was_js_error = true;
|
||||
} else {
|
||||
Addon* addon = db->GetAddon();
|
||||
assert(!addon->ArrayFactory.IsEmpty());
|
||||
assert(!addon->ArrayAppender.IsEmpty());
|
||||
static const size_t batch_size = 1024;
|
||||
size_t first_batch_size = std::min(rows.size(), batch_size);
|
||||
Napi::Value result = SafeCall(env, addon->ArrayFactory.Value(), env.Undefined(), first_batch_size, rows.data());
|
||||
if (!env.IsExceptionPending()) {
|
||||
napi_value args[batch_size + 1];
|
||||
args[0] = result;
|
||||
for (size_t offset = first_batch_size; offset < rows.size(); offset += batch_size) {
|
||||
size_t count = std::min(rows.size() - offset, batch_size);
|
||||
std::copy_n(rows.data() + offset, count, args + 1);
|
||||
SafeCall(env, addon->ArrayAppender.Value(), env.Undefined(), count + 1, args);
|
||||
if (env.IsExceptionPending()) break;
|
||||
}
|
||||
}
|
||||
if (env.IsExceptionPending()) {
|
||||
db->GetState()->was_js_error = true;
|
||||
} else {
|
||||
STATEMENT_RETURN(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
STATEMENT_THROW();
|
||||
}
|
||||
|
||||
NODE_METHOD(Statement::JS_iterate) {
|
||||
UseAddon;
|
||||
UseIsolate;
|
||||
Napi::Function c = addon->StatementIterator.Value();
|
||||
addon->privileged_info = &info;
|
||||
Napi::Object iterator = SafeConstruct(env, c);
|
||||
addon->privileged_info = NULL;
|
||||
if (env.IsExceptionPending()) return env.Undefined();
|
||||
return iterator;
|
||||
}
|
||||
|
||||
NODE_METHOD(Statement::JS_bind) {
|
||||
UNWRAP_OR_RETURN(Statement, stmt, info.This());
|
||||
if (stmt->bound) return ThrowTypeError(info.Env(), "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;
|
||||
return info.This();
|
||||
}
|
||||
|
||||
NODE_METHOD(Statement::JS_pluck) {
|
||||
UNWRAP_OR_RETURN(Statement, stmt, info.This());
|
||||
if (!stmt->returns_data) return ThrowTypeError(info.Env(), "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;
|
||||
return info.This();
|
||||
}
|
||||
|
||||
NODE_METHOD(Statement::JS_expand) {
|
||||
UNWRAP_OR_RETURN(Statement, stmt, info.This());
|
||||
if (!stmt->returns_data) return ThrowTypeError(info.Env(), "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;
|
||||
return info.This();
|
||||
}
|
||||
|
||||
NODE_METHOD(Statement::JS_raw) {
|
||||
UNWRAP_OR_RETURN(Statement, stmt, info.This());
|
||||
if (!stmt->returns_data) return ThrowTypeError(info.Env(), "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;
|
||||
return info.This();
|
||||
}
|
||||
|
||||
NODE_METHOD(Statement::JS_safeIntegers) {
|
||||
UNWRAP_OR_RETURN(Statement, stmt, 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); }
|
||||
return info.This();
|
||||
}
|
||||
|
||||
NODE_METHOD(Statement::JS_columns) {
|
||||
UNWRAP_OR_RETURN(Statement, stmt, info.This());
|
||||
if (!stmt->returns_data) return ThrowTypeError(info.Env(), "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;
|
||||
|
||||
int column_count = sqlite3_column_count(stmt->handle);
|
||||
Napi::Array columns = Napi::Array::New(env, column_count);
|
||||
|
||||
Napi::String name = addon->cs.name.Value();
|
||||
Napi::String columnName = addon->cs.column.Value();
|
||||
Napi::String tableName = addon->cs.table.Value();
|
||||
Napi::String databaseName = addon->cs.database.Value();
|
||||
Napi::String typeName = addon->cs.type.Value();
|
||||
|
||||
for (int i = 0; i < column_count; ++i) {
|
||||
Napi::Object column = Napi::Object::New(env);
|
||||
|
||||
column.Set(name,
|
||||
InternalizedFromUtf8OrNull(env, sqlite3_column_name(stmt->handle, i), -1)
|
||||
);
|
||||
column.Set(columnName,
|
||||
InternalizedFromUtf8OrNull(env, sqlite3_column_origin_name(stmt->handle, i), -1)
|
||||
);
|
||||
column.Set(tableName,
|
||||
InternalizedFromUtf8OrNull(env, sqlite3_column_table_name(stmt->handle, i), -1)
|
||||
);
|
||||
column.Set(databaseName,
|
||||
InternalizedFromUtf8OrNull(env, sqlite3_column_database_name(stmt->handle, i), -1)
|
||||
);
|
||||
column.Set(typeName,
|
||||
InternalizedFromUtf8OrNull(env, sqlite3_column_decltype(stmt->handle, i), -1)
|
||||
);
|
||||
|
||||
columns.Set(i, column);
|
||||
}
|
||||
|
||||
return columns;
|
||||
}
|
||||
|
||||
NODE_METHOD(Statement::JS_toString) {
|
||||
UNWRAP_OR_RETURN(Statement, stmt, info.This());
|
||||
Addon* addon = stmt->db->GetAddon();
|
||||
|
||||
char* expanded = stmt->alive && stmt->bound ? sqlite3_expanded_sql(stmt->handle) : NULL;
|
||||
if (expanded != NULL) {
|
||||
Napi::Value ret = StringFromUtf8(info.Env(), expanded, -1);
|
||||
sqlite3_free(expanded);
|
||||
return ret;
|
||||
}
|
||||
|
||||
return info.This().As<Napi::Object>()
|
||||
.Get(addon->cs.source.Value())
|
||||
.As<Napi::String>();
|
||||
}
|
||||
|
||||
NODE_GETTER(Statement::JS_busy) {
|
||||
UNWRAP_OR_RETURN(Statement, stmt, info.This());
|
||||
return Napi::Boolean::New(info.Env(), stmt->alive && stmt->locked);
|
||||
}
|
||||
+65
@@ -0,0 +1,65 @@
|
||||
class Statement : public Napi::ObjectWrap<Statement> { friend class StatementIterator;
|
||||
public:
|
||||
|
||||
explicit Statement(const Napi::CallbackInfo& info);
|
||||
~Statement();
|
||||
|
||||
// Whenever this is used, db->RemoveStatement must be invoked beforehand.
|
||||
void CloseHandles();
|
||||
|
||||
// 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(Napi::Env env);
|
||||
|
||||
// Returns the Statement's row builder.
|
||||
RowBuilder& GetRowBuilder();
|
||||
|
||||
// Identifies objects that are backed by this class (see IsInstanceOf).
|
||||
static const napi_type_tag TYPE_TAG;
|
||||
|
||||
static INIT(Init);
|
||||
|
||||
private:
|
||||
|
||||
// A class for holding values that are less often used.
|
||||
class Extras { friend class Statement; friend class StatementIterator;
|
||||
explicit Extras(
|
||||
Napi::Env env,
|
||||
Napi::Function row_factory,
|
||||
Napi::Function array_Factory,
|
||||
sqlite3_uint64 id
|
||||
);
|
||||
BindMap bind_map;
|
||||
RowBuilder row_builder;
|
||||
const sqlite3_uint64 id;
|
||||
};
|
||||
|
||||
NODE_METHOD(JS_new);
|
||||
static NODE_METHOD(JS_run);
|
||||
static NODE_METHOD(JS_get);
|
||||
static NODE_METHOD(JS_all);
|
||||
static NODE_METHOD(JS_iterate);
|
||||
static NODE_METHOD(JS_bind);
|
||||
static NODE_METHOD(JS_pluck);
|
||||
static NODE_METHOD(JS_expand);
|
||||
static NODE_METHOD(JS_raw);
|
||||
static NODE_METHOD(JS_safeIntegers);
|
||||
static NODE_METHOD(JS_columns);
|
||||
static NODE_METHOD(JS_toString);
|
||||
static NODE_GETTER(JS_busy);
|
||||
|
||||
Database* db;
|
||||
sqlite3_stmt* handle;
|
||||
Extras* extras;
|
||||
bool alive;
|
||||
bool locked;
|
||||
bool bound;
|
||||
bool has_bind_map;
|
||||
bool safe_ints;
|
||||
char mode;
|
||||
bool returns_data;
|
||||
};
|
||||
+73
@@ -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 Napi::String GetName(Napi::Env env) {
|
||||
return name.Value();
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
explicit Pair(Napi::Env env, const char* name, int index)
|
||||
: name(Napi::Persistent(InternalizedFromUtf8(env, name, -1))), index(index) {}
|
||||
|
||||
explicit Pair(Napi::Env env, Pair* pair)
|
||||
: name(Napi::Persistent(pair->name.Value())), index(pair->index) {}
|
||||
|
||||
const Napi::Reference<Napi::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(Napi::Env env, const char* name, int index) {
|
||||
assert(name != NULL);
|
||||
if (capacity == length) Grow(env);
|
||||
new (pairs + length++) Pair(env, name, index);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
void Grow(Napi::Env env) {
|
||||
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(env, pairs + i);
|
||||
pairs[i].~Pair();
|
||||
}
|
||||
FREE_ARRAY<Pair>(pairs);
|
||||
pairs = new_pairs;
|
||||
}
|
||||
|
||||
Pair* pairs;
|
||||
int capacity;
|
||||
int length;
|
||||
};
|
||||
+210
@@ -0,0 +1,210 @@
|
||||
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);
|
||||
Napi::Env env = info.Env();
|
||||
Result result = BindArgs(info, argc, stmt);
|
||||
if (success && result.count != param_count) {
|
||||
if (result.count < param_count) {
|
||||
if (!result.bound_object && stmt->GetBindMap(env).GetSize()) {
|
||||
Fail(ThrowTypeError, env, "Missing named parameters");
|
||||
} else {
|
||||
Fail(ThrowRangeError, env, "Too few parameter values were provided");
|
||||
}
|
||||
} else {
|
||||
Fail(ThrowRangeError, env, "Too many parameter values were provided");
|
||||
}
|
||||
}
|
||||
return success;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
struct Result {
|
||||
int count;
|
||||
bool bound_object;
|
||||
};
|
||||
|
||||
static Napi::Value GetPrototype(Napi::Env env, Napi::Object obj) {
|
||||
napi_value proto;
|
||||
// This can fail (e.g., a Proxy whose getPrototypeOf trap throws), in
|
||||
// which case an empty value is returned and an exception is pending.
|
||||
if (napi_get_prototype(env, obj, &proto) != napi_ok) return Napi::Value();
|
||||
return Napi::Value(env, proto);
|
||||
}
|
||||
|
||||
// An object is "plain" if its prototype is null or a top-level prototype
|
||||
// (i.e., some realm's Object.prototype). Rather than comparing against the
|
||||
// current realm's Object.prototype -- which would reject plain objects from
|
||||
// other contexts, such as the "vm" module -- we walk to the top of the
|
||||
// prototype chain. A plain object's chain is exactly "obj -> proto -> null",
|
||||
// so its immediate prototype must itself have a null prototype. This mirrors
|
||||
// the cross-realm heuristic used by lodash's isPlainObject.
|
||||
static bool IsPlainObject(Napi::Env env, Napi::Object obj) {
|
||||
Napi::Value proto = GetPrototype(env, obj);
|
||||
if (proto.IsEmpty()) return false;
|
||||
if (proto.IsNull()) return true;
|
||||
Napi::Value grandproto = GetPrototype(env, proto.As<Napi::Object>());
|
||||
if (grandproto.IsEmpty()) return false;
|
||||
return grandproto.IsNull();
|
||||
}
|
||||
|
||||
void Fail(Napi::Value (*Throw)(Napi::Env, const char*), Napi::Env env, const char* message) {
|
||||
assert(success == true);
|
||||
assert((Throw == NULL) == (message == NULL));
|
||||
assert(Throw == ThrowError || Throw == ThrowTypeError || Throw == ThrowRangeError || Throw == NULL);
|
||||
if (Throw) Throw(env, 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(Napi::Env env, Napi::Value value, int index) {
|
||||
int status = Data::BindValueFromJS(env, handle, index, value);
|
||||
if (status != SQLITE_OK) {
|
||||
switch (status) {
|
||||
case -1:
|
||||
return Fail(ThrowTypeError, env, "SQLite3 can only bind numbers, strings, bigints, buffers, and null");
|
||||
case SQLITE_TOOBIG:
|
||||
return Fail(ThrowRangeError, env, "The bound string, buffer, or bigint is too big");
|
||||
case SQLITE_RANGE:
|
||||
return Fail(ThrowRangeError, env, "Too many parameter values were provided");
|
||||
case SQLITE_NOMEM:
|
||||
return Fail(ThrowError, env, "Out of memory");
|
||||
default:
|
||||
return Fail(ThrowError, env, "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(Napi::Env env, Napi::Array arr) {
|
||||
uint32_t length = arr.Length();
|
||||
if (length > INT_MAX) {
|
||||
Fail(ThrowRangeError, env, "Too many parameter values were provided");
|
||||
return 0;
|
||||
}
|
||||
int len = static_cast<int>(length);
|
||||
for (int i = 0; i < len; ++i) {
|
||||
Napi::Value value = SafeGetElement(env, arr, static_cast<uint32_t>(i));
|
||||
if (value.IsEmpty()) {
|
||||
Fail(NULL, env, NULL);
|
||||
return i;
|
||||
}
|
||||
BindValue(env, value, 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(Napi::Env env, Napi::Object obj, Statement* stmt) {
|
||||
BindMap& bind_map = stmt->GetBindMap(env);
|
||||
BindMap::Pair* pairs = bind_map.GetPairs();
|
||||
int len = bind_map.GetSize();
|
||||
|
||||
for (int i = 0; i < len; ++i) {
|
||||
Napi::String key = pairs[i].GetName(env);
|
||||
|
||||
// Check if the named parameter was provided.
|
||||
bool has_property;
|
||||
if (!SafeHasOwnProperty(env, obj, key, &has_property)) {
|
||||
Fail(NULL, env, NULL);
|
||||
return i;
|
||||
}
|
||||
if (!has_property) {
|
||||
std::string param_name = key.Utf8Value();
|
||||
Fail(ThrowRangeError, env, (std::string("Missing named parameter \"") + param_name + "\"").c_str());
|
||||
return i;
|
||||
}
|
||||
|
||||
// Get the current property value.
|
||||
Napi::Value value = SafeGet(env, obj, key);
|
||||
if (value.IsEmpty()) {
|
||||
Fail(NULL, env, NULL);
|
||||
return i;
|
||||
}
|
||||
|
||||
BindValue(env, value, 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) {
|
||||
Napi::Env env = info.Env();
|
||||
int count = 0;
|
||||
bool bound_object = false;
|
||||
|
||||
for (int i = 0; i < argc; ++i) {
|
||||
Napi::Value arg = info[i];
|
||||
|
||||
if (arg.IsArray()) {
|
||||
count += BindArray(env, arg.As<Napi::Array>());
|
||||
if (!success) break;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (arg.IsObject() && !arg.IsBuffer()) {
|
||||
Napi::Object obj = arg.As<Napi::Object>();
|
||||
if (IsPlainObject(env, obj)) {
|
||||
if (bound_object) {
|
||||
Fail(ThrowTypeError, env, "You cannot specify named parameters in two different objects");
|
||||
break;
|
||||
}
|
||||
bound_object = true;
|
||||
|
||||
count += BindObject(env, obj, stmt);
|
||||
if (!success) break;
|
||||
continue;
|
||||
} else if (env.IsExceptionPending()) {
|
||||
Fail(NULL, env, NULL);
|
||||
break;
|
||||
} else if (stmt->GetBindMap(env).GetSize()) {
|
||||
Fail(ThrowTypeError, env, "Named parameters can only be passed within plain objects");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
BindValue(env, 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()
|
||||
};
|
||||
+172
@@ -0,0 +1,172 @@
|
||||
// Caches JavaScript strings that are used frequently as property keys or
|
||||
// error codes. Persistent references to strings require Node-API version 10.
|
||||
class CS {
|
||||
public:
|
||||
|
||||
Napi::String Code(Napi::Env env, int code) {
|
||||
auto element = codes.find(code);
|
||||
if (element != codes.end()) return element->second.Value();
|
||||
return StringFromUtf8(env, (std::string("UNKNOWN_SQLITE_ERROR_") + std::to_string(code)).c_str(), -1);
|
||||
}
|
||||
|
||||
explicit CS(Napi::Env env) {
|
||||
SetString(env, database, "database");
|
||||
SetString(env, reader, "reader");
|
||||
SetString(env, source, "source");
|
||||
SetString(env, memory, "memory");
|
||||
SetString(env, readonly, "readonly");
|
||||
SetString(env, name, "name");
|
||||
SetString(env, next, "next");
|
||||
SetString(env, length, "length");
|
||||
SetString(env, done, "done");
|
||||
SetString(env, value, "value");
|
||||
SetString(env, changes, "changes");
|
||||
SetString(env, lastInsertRowid, "lastInsertRowid");
|
||||
SetString(env, statement, "statement");
|
||||
SetString(env, column, "column");
|
||||
SetString(env, table, "table");
|
||||
SetString(env, type, "type");
|
||||
SetString(env, totalPages, "totalPages");
|
||||
SetString(env, remainingPages, "remainingPages");
|
||||
|
||||
SetCode(env, SQLITE_OK, "SQLITE_OK");
|
||||
SetCode(env, SQLITE_ERROR, "SQLITE_ERROR");
|
||||
SetCode(env, SQLITE_INTERNAL, "SQLITE_INTERNAL");
|
||||
SetCode(env, SQLITE_PERM, "SQLITE_PERM");
|
||||
SetCode(env, SQLITE_ABORT, "SQLITE_ABORT");
|
||||
SetCode(env, SQLITE_BUSY, "SQLITE_BUSY");
|
||||
SetCode(env, SQLITE_LOCKED, "SQLITE_LOCKED");
|
||||
SetCode(env, SQLITE_NOMEM, "SQLITE_NOMEM");
|
||||
SetCode(env, SQLITE_READONLY, "SQLITE_READONLY");
|
||||
SetCode(env, SQLITE_INTERRUPT, "SQLITE_INTERRUPT");
|
||||
SetCode(env, SQLITE_IOERR, "SQLITE_IOERR");
|
||||
SetCode(env, SQLITE_CORRUPT, "SQLITE_CORRUPT");
|
||||
SetCode(env, SQLITE_NOTFOUND, "SQLITE_NOTFOUND");
|
||||
SetCode(env, SQLITE_FULL, "SQLITE_FULL");
|
||||
SetCode(env, SQLITE_CANTOPEN, "SQLITE_CANTOPEN");
|
||||
SetCode(env, SQLITE_PROTOCOL, "SQLITE_PROTOCOL");
|
||||
SetCode(env, SQLITE_EMPTY, "SQLITE_EMPTY");
|
||||
SetCode(env, SQLITE_SCHEMA, "SQLITE_SCHEMA");
|
||||
SetCode(env, SQLITE_TOOBIG, "SQLITE_TOOBIG");
|
||||
SetCode(env, SQLITE_CONSTRAINT, "SQLITE_CONSTRAINT");
|
||||
SetCode(env, SQLITE_MISMATCH, "SQLITE_MISMATCH");
|
||||
SetCode(env, SQLITE_MISUSE, "SQLITE_MISUSE");
|
||||
SetCode(env, SQLITE_NOLFS, "SQLITE_NOLFS");
|
||||
SetCode(env, SQLITE_AUTH, "SQLITE_AUTH");
|
||||
SetCode(env, SQLITE_FORMAT, "SQLITE_FORMAT");
|
||||
SetCode(env, SQLITE_RANGE, "SQLITE_RANGE");
|
||||
SetCode(env, SQLITE_NOTADB, "SQLITE_NOTADB");
|
||||
SetCode(env, SQLITE_NOTICE, "SQLITE_NOTICE");
|
||||
SetCode(env, SQLITE_WARNING, "SQLITE_WARNING");
|
||||
SetCode(env, SQLITE_ROW, "SQLITE_ROW");
|
||||
SetCode(env, SQLITE_DONE, "SQLITE_DONE");
|
||||
|
||||
SetCode(env, SQLITE_ERROR_MISSING_COLLSEQ, "SQLITE_ERROR_MISSING_COLLSEQ");
|
||||
SetCode(env, SQLITE_ERROR_RETRY, "SQLITE_ERROR_RETRY");
|
||||
SetCode(env, SQLITE_ERROR_SNAPSHOT, "SQLITE_ERROR_SNAPSHOT");
|
||||
SetCode(env, SQLITE_IOERR_READ, "SQLITE_IOERR_READ");
|
||||
SetCode(env, SQLITE_IOERR_SHORT_READ, "SQLITE_IOERR_SHORT_READ");
|
||||
SetCode(env, SQLITE_IOERR_WRITE, "SQLITE_IOERR_WRITE");
|
||||
SetCode(env, SQLITE_IOERR_FSYNC, "SQLITE_IOERR_FSYNC");
|
||||
SetCode(env, SQLITE_IOERR_DIR_FSYNC, "SQLITE_IOERR_DIR_FSYNC");
|
||||
SetCode(env, SQLITE_IOERR_TRUNCATE, "SQLITE_IOERR_TRUNCATE");
|
||||
SetCode(env, SQLITE_IOERR_FSTAT, "SQLITE_IOERR_FSTAT");
|
||||
SetCode(env, SQLITE_IOERR_UNLOCK, "SQLITE_IOERR_UNLOCK");
|
||||
SetCode(env, SQLITE_IOERR_RDLOCK, "SQLITE_IOERR_RDLOCK");
|
||||
SetCode(env, SQLITE_IOERR_DELETE, "SQLITE_IOERR_DELETE");
|
||||
SetCode(env, SQLITE_IOERR_BLOCKED, "SQLITE_IOERR_BLOCKED");
|
||||
SetCode(env, SQLITE_IOERR_NOMEM, "SQLITE_IOERR_NOMEM");
|
||||
SetCode(env, SQLITE_IOERR_ACCESS, "SQLITE_IOERR_ACCESS");
|
||||
SetCode(env, SQLITE_IOERR_CHECKRESERVEDLOCK, "SQLITE_IOERR_CHECKRESERVEDLOCK");
|
||||
SetCode(env, SQLITE_IOERR_LOCK, "SQLITE_IOERR_LOCK");
|
||||
SetCode(env, SQLITE_IOERR_CLOSE, "SQLITE_IOERR_CLOSE");
|
||||
SetCode(env, SQLITE_IOERR_DIR_CLOSE, "SQLITE_IOERR_DIR_CLOSE");
|
||||
SetCode(env, SQLITE_IOERR_SHMOPEN, "SQLITE_IOERR_SHMOPEN");
|
||||
SetCode(env, SQLITE_IOERR_SHMSIZE, "SQLITE_IOERR_SHMSIZE");
|
||||
SetCode(env, SQLITE_IOERR_SHMLOCK, "SQLITE_IOERR_SHMLOCK");
|
||||
SetCode(env, SQLITE_IOERR_SHMMAP, "SQLITE_IOERR_SHMMAP");
|
||||
SetCode(env, SQLITE_IOERR_SEEK, "SQLITE_IOERR_SEEK");
|
||||
SetCode(env, SQLITE_IOERR_DELETE_NOENT, "SQLITE_IOERR_DELETE_NOENT");
|
||||
SetCode(env, SQLITE_IOERR_MMAP, "SQLITE_IOERR_MMAP");
|
||||
SetCode(env, SQLITE_IOERR_GETTEMPPATH, "SQLITE_IOERR_GETTEMPPATH");
|
||||
SetCode(env, SQLITE_IOERR_CONVPATH, "SQLITE_IOERR_CONVPATH");
|
||||
SetCode(env, SQLITE_IOERR_VNODE, "SQLITE_IOERR_VNODE");
|
||||
SetCode(env, SQLITE_IOERR_AUTH, "SQLITE_IOERR_AUTH");
|
||||
SetCode(env, SQLITE_IOERR_BEGIN_ATOMIC, "SQLITE_IOERR_BEGIN_ATOMIC");
|
||||
SetCode(env, SQLITE_IOERR_COMMIT_ATOMIC, "SQLITE_IOERR_COMMIT_ATOMIC");
|
||||
SetCode(env, SQLITE_IOERR_ROLLBACK_ATOMIC, "SQLITE_IOERR_ROLLBACK_ATOMIC");
|
||||
SetCode(env, SQLITE_IOERR_DATA, "SQLITE_IOERR_DATA");
|
||||
SetCode(env, SQLITE_IOERR_CORRUPTFS, "SQLITE_IOERR_CORRUPTFS");
|
||||
SetCode(env, SQLITE_IOERR_IN_PAGE, "SQLITE_IOERR_IN_PAGE");
|
||||
SetCode(env, SQLITE_LOCKED_SHAREDCACHE, "SQLITE_LOCKED_SHAREDCACHE");
|
||||
SetCode(env, SQLITE_LOCKED_VTAB, "SQLITE_LOCKED_VTAB");
|
||||
SetCode(env, SQLITE_BUSY_RECOVERY, "SQLITE_BUSY_RECOVERY");
|
||||
SetCode(env, SQLITE_BUSY_SNAPSHOT, "SQLITE_BUSY_SNAPSHOT");
|
||||
SetCode(env, SQLITE_CANTOPEN_NOTEMPDIR, "SQLITE_CANTOPEN_NOTEMPDIR");
|
||||
SetCode(env, SQLITE_CANTOPEN_ISDIR, "SQLITE_CANTOPEN_ISDIR");
|
||||
SetCode(env, SQLITE_CANTOPEN_FULLPATH, "SQLITE_CANTOPEN_FULLPATH");
|
||||
SetCode(env, SQLITE_CANTOPEN_CONVPATH, "SQLITE_CANTOPEN_CONVPATH");
|
||||
SetCode(env, SQLITE_CANTOPEN_DIRTYWAL, "SQLITE_CANTOPEN_DIRTYWAL");
|
||||
SetCode(env, SQLITE_CANTOPEN_SYMLINK, "SQLITE_CANTOPEN_SYMLINK");
|
||||
SetCode(env, SQLITE_CORRUPT_VTAB, "SQLITE_CORRUPT_VTAB");
|
||||
SetCode(env, SQLITE_CORRUPT_SEQUENCE, "SQLITE_CORRUPT_SEQUENCE");
|
||||
SetCode(env, SQLITE_CORRUPT_INDEX, "SQLITE_CORRUPT_INDEX");
|
||||
SetCode(env, SQLITE_READONLY_RECOVERY, "SQLITE_READONLY_RECOVERY");
|
||||
SetCode(env, SQLITE_READONLY_CANTLOCK, "SQLITE_READONLY_CANTLOCK");
|
||||
SetCode(env, SQLITE_READONLY_ROLLBACK, "SQLITE_READONLY_ROLLBACK");
|
||||
SetCode(env, SQLITE_READONLY_DBMOVED, "SQLITE_READONLY_DBMOVED");
|
||||
SetCode(env, SQLITE_READONLY_CANTINIT, "SQLITE_READONLY_CANTINIT");
|
||||
SetCode(env, SQLITE_READONLY_DIRECTORY, "SQLITE_READONLY_DIRECTORY");
|
||||
SetCode(env, SQLITE_ABORT_ROLLBACK, "SQLITE_ABORT_ROLLBACK");
|
||||
SetCode(env, SQLITE_CONSTRAINT_CHECK, "SQLITE_CONSTRAINT_CHECK");
|
||||
SetCode(env, SQLITE_CONSTRAINT_COMMITHOOK, "SQLITE_CONSTRAINT_COMMITHOOK");
|
||||
SetCode(env, SQLITE_CONSTRAINT_FOREIGNKEY, "SQLITE_CONSTRAINT_FOREIGNKEY");
|
||||
SetCode(env, SQLITE_CONSTRAINT_FUNCTION, "SQLITE_CONSTRAINT_FUNCTION");
|
||||
SetCode(env, SQLITE_CONSTRAINT_NOTNULL, "SQLITE_CONSTRAINT_NOTNULL");
|
||||
SetCode(env, SQLITE_CONSTRAINT_PRIMARYKEY, "SQLITE_CONSTRAINT_PRIMARYKEY");
|
||||
SetCode(env, SQLITE_CONSTRAINT_TRIGGER, "SQLITE_CONSTRAINT_TRIGGER");
|
||||
SetCode(env, SQLITE_CONSTRAINT_UNIQUE, "SQLITE_CONSTRAINT_UNIQUE");
|
||||
SetCode(env, SQLITE_CONSTRAINT_VTAB, "SQLITE_CONSTRAINT_VTAB");
|
||||
SetCode(env, SQLITE_CONSTRAINT_ROWID, "SQLITE_CONSTRAINT_ROWID");
|
||||
SetCode(env, SQLITE_CONSTRAINT_PINNED, "SQLITE_CONSTRAINT_PINNED");
|
||||
SetCode(env, SQLITE_CONSTRAINT_DATATYPE, "SQLITE_CONSTRAINT_DATATYPE");
|
||||
SetCode(env, SQLITE_NOTICE_RECOVER_WAL, "SQLITE_NOTICE_RECOVER_WAL");
|
||||
SetCode(env, SQLITE_NOTICE_RECOVER_ROLLBACK, "SQLITE_NOTICE_RECOVER_ROLLBACK");
|
||||
SetCode(env, SQLITE_NOTICE_RBU, "SQLITE_NOTICE_RBU");
|
||||
SetCode(env, SQLITE_WARNING_AUTOINDEX, "SQLITE_WARNING_AUTOINDEX");
|
||||
SetCode(env, SQLITE_AUTH_USER, "SQLITE_AUTH_USER");
|
||||
SetCode(env, SQLITE_OK_LOAD_PERMANENTLY, "SQLITE_OK_LOAD_PERMANENTLY");
|
||||
SetCode(env, SQLITE_OK_SYMLINK, "SQLITE_OK_SYMLINK");
|
||||
}
|
||||
|
||||
Napi::Reference<Napi::String> database;
|
||||
Napi::Reference<Napi::String> reader;
|
||||
Napi::Reference<Napi::String> source;
|
||||
Napi::Reference<Napi::String> memory;
|
||||
Napi::Reference<Napi::String> readonly;
|
||||
Napi::Reference<Napi::String> name;
|
||||
Napi::Reference<Napi::String> next;
|
||||
Napi::Reference<Napi::String> length;
|
||||
Napi::Reference<Napi::String> done;
|
||||
Napi::Reference<Napi::String> value;
|
||||
Napi::Reference<Napi::String> changes;
|
||||
Napi::Reference<Napi::String> lastInsertRowid;
|
||||
Napi::Reference<Napi::String> statement;
|
||||
Napi::Reference<Napi::String> column;
|
||||
Napi::Reference<Napi::String> table;
|
||||
Napi::Reference<Napi::String> type;
|
||||
Napi::Reference<Napi::String> totalPages;
|
||||
Napi::Reference<Napi::String> remainingPages;
|
||||
|
||||
private:
|
||||
|
||||
static void SetString(Napi::Env env, Napi::Reference<Napi::String>& constant, const char* str) {
|
||||
constant = Napi::Persistent(InternalizedFromLatin1(env, str));
|
||||
}
|
||||
|
||||
void SetCode(Napi::Env env, int code, const char* str) {
|
||||
codes.emplace(code, Napi::Persistent(InternalizedFromLatin1(env, str)));
|
||||
}
|
||||
|
||||
std::unordered_map<int, Napi::Reference<Napi::String>> codes;
|
||||
};
|
||||
+121
@@ -0,0 +1,121 @@
|
||||
class CustomAggregate : public CustomFunction {
|
||||
public:
|
||||
|
||||
explicit CustomAggregate(
|
||||
Napi::Env env,
|
||||
Database* db,
|
||||
const char* name,
|
||||
Napi::Value start,
|
||||
Napi::Function step,
|
||||
Napi::Value inverse,
|
||||
Napi::Value result,
|
||||
bool safe_ints
|
||||
) :
|
||||
CustomFunction(env, db, name, step, safe_ints),
|
||||
invoke_result(result.IsFunction()),
|
||||
invoke_start(start.IsFunction()),
|
||||
inverse(inverse.IsFunction() ? Napi::Persistent(inverse.As<Napi::Function>()) : Napi::FunctionReference()),
|
||||
result(result.IsFunction() ? Napi::Persistent(result.As<Napi::Function>()) : Napi::FunctionReference()),
|
||||
start(Napi::Persistent(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 Napi::FunctionReference CustomAggregate::*ptrtm) {
|
||||
AGGREGATE_START();
|
||||
|
||||
napi_value args_fast[5];
|
||||
napi_value* args = argc <= 4 ? args_fast : ALLOC_ARRAY<napi_value>(argc + 1);
|
||||
args[0] = acc->value.Value();
|
||||
if (argc != 0) Data::GetArgumentsJS(env, args + 1, argv, argc, self->safe_ints);
|
||||
|
||||
Napi::Value returnValue = SafeCall(env, (self->*ptrtm).Value(), env.Undefined(), argc + 1, args);
|
||||
if (args != args_fast) delete[] args;
|
||||
|
||||
if (env.IsExceptionPending()) {
|
||||
self->PropagateJSError(invocation);
|
||||
} else {
|
||||
if (!returnValue.IsUndefined()) acc->value.Reset(returnValue, 1);
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
Napi::Value result = acc->value.Value();
|
||||
if (self->invoke_result) {
|
||||
napi_value arg = result;
|
||||
Napi::Value maybeResult = SafeCall(env, self->result.Value(), env.Undefined(), 1, &arg);
|
||||
if (env.IsExceptionPending()) {
|
||||
self->PropagateJSError(invocation);
|
||||
return;
|
||||
}
|
||||
result = maybeResult;
|
||||
}
|
||||
|
||||
Data::ResultValueFromJS(env, invocation, result, self);
|
||||
if (is_final) DestroyAccumulator(invocation);
|
||||
}
|
||||
|
||||
struct Accumulator { public:
|
||||
Napi::Reference<Napi::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) {
|
||||
Napi::Value maybeSeed = SafeCall(env, start.Value().As<Napi::Function>(), env.Undefined(), 0, NULL);
|
||||
if (env.IsExceptionPending()) PropagateJSError(invocation);
|
||||
else acc->value.Reset(maybeSeed, 1);
|
||||
} else {
|
||||
assert(!start.IsEmpty());
|
||||
acc->value.Reset(start.Value(), 1);
|
||||
}
|
||||
}
|
||||
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 Napi::FunctionReference inverse;
|
||||
const Napi::FunctionReference result;
|
||||
const Napi::Reference<Napi::Value> start;
|
||||
};
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
class CustomFunction : protected DataConverter {
|
||||
public:
|
||||
|
||||
explicit CustomFunction(
|
||||
Napi::Env env,
|
||||
Database* db,
|
||||
const char* name,
|
||||
Napi::Function fn,
|
||||
bool safe_ints
|
||||
) :
|
||||
name(name),
|
||||
db(db),
|
||||
env(env),
|
||||
fn(Napi::Persistent(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();
|
||||
|
||||
napi_value args_fast[4];
|
||||
napi_value* args = NULL;
|
||||
if (argc != 0) {
|
||||
args = argc <= 4 ? args_fast : ALLOC_ARRAY<napi_value>(argc);
|
||||
Data::GetArgumentsJS(env, args, argv, argc, self->safe_ints);
|
||||
}
|
||||
|
||||
Napi::Value returnValue = SafeCall(env, self->fn.Value(), env.Undefined(), argc, args);
|
||||
if (args != args_fast) delete[] args;
|
||||
|
||||
if (env.IsExceptionPending()) self->PropagateJSError(invocation);
|
||||
else Data::ResultValueFromJS(env, invocation, returnValue, 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:
|
||||
const Napi::Env env;
|
||||
const Napi::FunctionReference fn;
|
||||
const bool safe_ints;
|
||||
};
|
||||
+406
@@ -0,0 +1,406 @@
|
||||
class CustomTable {
|
||||
public:
|
||||
|
||||
explicit CustomTable(
|
||||
Napi::Env env,
|
||||
Database* db,
|
||||
const char* name,
|
||||
Napi::Function factory
|
||||
) :
|
||||
addon(db->GetAddon()),
|
||||
env(env),
|
||||
db(db),
|
||||
name(name),
|
||||
factory(Napi::Persistent(factory)) {}
|
||||
|
||||
static void Destructor(void* self) {
|
||||
delete static_cast<CustomTable*>(self);
|
||||
}
|
||||
|
||||
static sqlite3_module MODULE;
|
||||
static sqlite3_module EPONYMOUS_MODULE;
|
||||
|
||||
private:
|
||||
|
||||
// This nested class is instantiated on each CREATE VIRTUAL TABLE statement.
|
||||
class VTab { friend class CustomTable;
|
||||
explicit VTab(
|
||||
CustomTable* parent,
|
||||
Napi::Function generator,
|
||||
std::vector<std::string> parameter_names,
|
||||
bool safe_ints
|
||||
) :
|
||||
parent(parent),
|
||||
parameter_count(parameter_names.size()),
|
||||
safe_ints(safe_ints),
|
||||
generator(Napi::Persistent(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 Napi::FunctionReference 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;
|
||||
Napi::ObjectReference iterator;
|
||||
Napi::FunctionReference next;
|
||||
Napi::Reference<Napi::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);
|
||||
Napi::Env env = self->env;
|
||||
Napi::HandleScope scope(env);
|
||||
|
||||
napi_value* args = ALLOC_ARRAY<napi_value>(argc);
|
||||
for (int i = 0; i < argc; ++i) {
|
||||
args[i] = StringFromUtf8(env, argv[i], -1);
|
||||
}
|
||||
|
||||
// Run the factory function to receive a new virtual table definition.
|
||||
Napi::Value returnValue = SafeCall(env, self->factory.Value(), env.Undefined(), argc, args);
|
||||
delete[] args;
|
||||
|
||||
if (env.IsExceptionPending()) {
|
||||
self->PropagateJSError();
|
||||
return SQLITE_ERROR;
|
||||
}
|
||||
|
||||
// Extract each part of the virtual table definition.
|
||||
Napi::Array array = returnValue.As<Napi::Array>();
|
||||
Napi::String sqlString = array.Get((uint32_t)0).As<Napi::String>();
|
||||
Napi::Function generator = array.Get((uint32_t)1).As<Napi::Function>();
|
||||
Napi::Array parameterNames = array.Get((uint32_t)2).As<Napi::Array>();
|
||||
int safe_ints = array.Get((uint32_t)3).As<Napi::Number>().Int32Value();
|
||||
bool direct_only = array.Get((uint32_t)4).As<Napi::Boolean>().Value();
|
||||
|
||||
std::string sql = sqlString.Utf8Value();
|
||||
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) {
|
||||
Napi::String parameterName = parameterNames.Get((uint32_t)i).As<Napi::String>();
|
||||
parameter_names.emplace_back(parameterName.Utf8Value());
|
||||
}
|
||||
|
||||
// Pass our SQL table definition to SQLite (this should never fail).
|
||||
if (sqlite3_declare_vtab(db_handle, sql.c_str()) != 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;
|
||||
Napi::Env env = self->env;
|
||||
Napi::HandleScope scope(env);
|
||||
|
||||
// Convert the SQLite arguments into JavaScript arguments. Note that
|
||||
// the values in argv may be in the wrong order, so we fix that here.
|
||||
napi_value args_fast[4];
|
||||
napi_value* args = NULL;
|
||||
int parameter_count = vtab->parameter_count;
|
||||
if (parameter_count != 0) {
|
||||
args = parameter_count <= 4 ? args_fast : ALLOC_ARRAY<napi_value>(parameter_count);
|
||||
int argn = 0;
|
||||
bool safe_ints = vtab->safe_ints;
|
||||
for (int i = 0; i < parameter_count; ++i) {
|
||||
if (idxNum & 1 << i) {
|
||||
Napi::Value arg = Data::GetValueJS(env, argv[argn++], safe_ints);
|
||||
args[i] = arg;
|
||||
// If any arguments are NULL, the result set is necessarily
|
||||
// empty, so don't bother to run the generator function.
|
||||
if (arg.IsNull()) {
|
||||
if (args != args_fast) delete[] args;
|
||||
cursor->done = true;
|
||||
return SQLITE_OK;
|
||||
}
|
||||
} else {
|
||||
args[i] = env.Undefined();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Invoke the generator function to create a new iterator.
|
||||
Napi::Value maybeIterator = SafeCall(env, vtab->generator.Value(), env.Undefined(), parameter_count, args);
|
||||
if (args != args_fast) delete[] args;
|
||||
|
||||
if (env.IsExceptionPending()) {
|
||||
self->PropagateJSError();
|
||||
return SQLITE_ERROR;
|
||||
}
|
||||
|
||||
// Store the iterator and its next() method; we'll be using it a lot.
|
||||
Napi::Object iterator = maybeIterator.As<Napi::Object>();
|
||||
Napi::Function next = iterator.Get(addon->cs.next.Value()).As<Napi::Function>();
|
||||
cursor->iterator.Reset(iterator, 1);
|
||||
cursor->next.Reset(next, 1);
|
||||
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;
|
||||
Napi::Env env = self->env;
|
||||
Napi::HandleScope scope(env);
|
||||
|
||||
Napi::Object iterator = cursor->iterator.Value();
|
||||
Napi::Function next = cursor->next.Value();
|
||||
|
||||
Napi::Value maybeRecord = SafeCall(env, next, iterator, 0, NULL);
|
||||
if (env.IsExceptionPending()) {
|
||||
self->PropagateJSError();
|
||||
return SQLITE_ERROR;
|
||||
}
|
||||
|
||||
Napi::Object record = maybeRecord.As<Napi::Object>();
|
||||
bool done = record.Get(addon->cs.done.Value()).As<Napi::Boolean>().Value();
|
||||
if (!done) {
|
||||
cursor->row.Reset(record.Get(addon->cs.value.Value()).As<Napi::Array>(), 1);
|
||||
}
|
||||
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);
|
||||
Napi::Env env = self->env;
|
||||
Napi::HandleScope scope(env);
|
||||
|
||||
Napi::Array row = cursor->row.Value();
|
||||
Napi::Value maybeColumnValue = SafeGetElement(env, row, (uint32_t)column);
|
||||
if (maybeColumnValue.IsEmpty()) {
|
||||
temp_data_converter.PropagateJSError(NULL);
|
||||
} else {
|
||||
Data::ResultValueFromJS(env, invocation, maybeColumnValue, &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;
|
||||
const Napi::Env env;
|
||||
Database* const db;
|
||||
const std::string name;
|
||||
const Napi::FunctionReference factory;
|
||||
};
|
||||
|
||||
sqlite3_module CustomTable::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 */
|
||||
NULL /* xIntegrity */
|
||||
};
|
||||
|
||||
sqlite3_module CustomTable::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 */
|
||||
NULL /* xIntegrity */
|
||||
};
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
class DataConverter {
|
||||
public:
|
||||
|
||||
void ThrowDataConversionError(Napi::Env env, sqlite3_context* invocation, bool isBigInt) {
|
||||
if (isBigInt) {
|
||||
ThrowRangeError(env, (GetDataErrorPrefix() + " a bigint that was too big").c_str());
|
||||
} else {
|
||||
ThrowTypeError(env, (GetDataErrorPrefix() + " an invalid value").c_str());
|
||||
}
|
||||
PropagateJSError(invocation);
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
virtual void PropagateJSError(sqlite3_context* invocation) = 0;
|
||||
virtual std::string GetDataErrorPrefix() = 0;
|
||||
};
|
||||
+134
@@ -0,0 +1,134 @@
|
||||
#define JS_VALUE_TO_SQLITE(to, value, env, ...) \
|
||||
if (value.IsNumber()) { \
|
||||
return sqlite3_##to##_double( \
|
||||
__VA_ARGS__, \
|
||||
value.As<Napi::Number>().DoubleValue() \
|
||||
); \
|
||||
} else if (value.IsBigInt()) { \
|
||||
bool lossless; \
|
||||
int64_t v = value.As<Napi::BigInt>().Int64Value(&lossless); \
|
||||
if (lossless) { \
|
||||
return sqlite3_##to##_int64(__VA_ARGS__, v); \
|
||||
} \
|
||||
} else if (value.IsString()) { \
|
||||
std::string utf8 = value.As<Napi::String>().Utf8Value(); \
|
||||
return sqlite3_##to##_text( \
|
||||
__VA_ARGS__, \
|
||||
utf8.c_str(), \
|
||||
utf8.length(), \
|
||||
SQLITE_TRANSIENT \
|
||||
); \
|
||||
} else if (value.IsBuffer()) { \
|
||||
Napi::Buffer<char> buffer = value.As<Napi::Buffer<char>>(); \
|
||||
const char* data = buffer.Data(); \
|
||||
return sqlite3_##to##_blob( \
|
||||
__VA_ARGS__, \
|
||||
data ? data : "", \
|
||||
buffer.Length(), \
|
||||
SQLITE_TRANSIENT \
|
||||
); \
|
||||
} else if (value.IsNull() || value.IsUndefined()) { \
|
||||
return sqlite3_##to##_null(__VA_ARGS__); \
|
||||
}
|
||||
|
||||
#define SQLITE_VALUE_TO_JS(from, env, safe_ints, ...) \
|
||||
switch (sqlite3_##from##_type(__VA_ARGS__)) { \
|
||||
case SQLITE_INTEGER: \
|
||||
if (safe_ints) { \
|
||||
return Napi::BigInt::New( \
|
||||
env, \
|
||||
(int64_t)sqlite3_##from##_int64(__VA_ARGS__) \
|
||||
); \
|
||||
} \
|
||||
case SQLITE_FLOAT: \
|
||||
return Napi::Number::New( \
|
||||
env, \
|
||||
sqlite3_##from##_double(__VA_ARGS__) \
|
||||
); \
|
||||
case SQLITE_TEXT: \
|
||||
return StringFromUtf8( \
|
||||
env, \
|
||||
reinterpret_cast<const char*>(sqlite3_##from##_text(__VA_ARGS__)), \
|
||||
sqlite3_##from##_bytes(__VA_ARGS__) \
|
||||
); \
|
||||
case SQLITE_BLOB: \
|
||||
return Napi::Buffer<char>::Copy( \
|
||||
env, \
|
||||
static_cast<const char*>(sqlite3_##from##_blob(__VA_ARGS__)), \
|
||||
sqlite3_##from##_bytes(__VA_ARGS__) \
|
||||
); \
|
||||
default: \
|
||||
assert(sqlite3_##from##_type(__VA_ARGS__) == SQLITE_NULL); \
|
||||
return env.Null(); \
|
||||
} \
|
||||
assert(false);
|
||||
|
||||
namespace Data {
|
||||
|
||||
static const char FLAT = 0;
|
||||
static const char PLUCK = 1;
|
||||
static const char EXPAND = 2;
|
||||
static const char RAW = 3;
|
||||
|
||||
Napi::Value GetValueJS(Napi::Env env, sqlite3_stmt* handle, int column, bool safe_ints) {
|
||||
SQLITE_VALUE_TO_JS(column, env, safe_ints, handle, column);
|
||||
}
|
||||
|
||||
Napi::Value GetValueJS(Napi::Env env, sqlite3_value* value, bool safe_ints) {
|
||||
SQLITE_VALUE_TO_JS(value, env, safe_ints, value);
|
||||
}
|
||||
|
||||
Napi::Value GetExpandedRowJS(Napi::Env env, sqlite3_stmt* handle, bool safe_ints) {
|
||||
Napi::Object row = Napi::Object::New(env);
|
||||
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);
|
||||
Napi::String table = InternalizedFromUtf8(env, table_raw == NULL ? "$" : table_raw, -1);
|
||||
Napi::String column = InternalizedFromUtf8(env, sqlite3_column_name(handle, i), -1);
|
||||
Napi::Value value = Data::GetValueJS(env, handle, i, safe_ints);
|
||||
if (row.HasOwnProperty(table)) {
|
||||
row.Get(table).As<Napi::Object>().Set(column, value);
|
||||
} else {
|
||||
Napi::Object nested = Napi::Object::New(env);
|
||||
row.Set(table, nested);
|
||||
nested.Set(column, value);
|
||||
}
|
||||
}
|
||||
return row;
|
||||
}
|
||||
|
||||
Napi::Value GetFlatRowJS(Napi::Env env, Statement* stmt, sqlite3_stmt* handle, bool safe_ints) {
|
||||
return stmt->GetRowBuilder().GetRowJS(env, handle, safe_ints);
|
||||
}
|
||||
|
||||
Napi::Value GetRawRowJS(Napi::Env env, Statement* stmt, sqlite3_stmt* handle, bool safe_ints) {
|
||||
return stmt->GetRowBuilder().GetRawRowJS(env, handle, safe_ints);
|
||||
}
|
||||
|
||||
Napi::Value GetRowJS(Napi::Env env, Statement* stmt, sqlite3_stmt* handle, bool safe_ints, char mode) {
|
||||
if (mode == Data::FLAT) return GetFlatRowJS(env, stmt, handle, safe_ints);
|
||||
if (mode == PLUCK) return GetValueJS(env, handle, 0, safe_ints);
|
||||
if (mode == EXPAND) return GetExpandedRowJS(env, handle, safe_ints);
|
||||
if (mode == RAW) return GetRawRowJS(env, stmt, handle, safe_ints);
|
||||
assert(false);
|
||||
return Napi::Value();
|
||||
}
|
||||
|
||||
void GetArgumentsJS(Napi::Env env, napi_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(env, values[i], safe_ints);
|
||||
}
|
||||
}
|
||||
|
||||
int BindValueFromJS(Napi::Env env, sqlite3_stmt* handle, int index, Napi::Value value) {
|
||||
JS_VALUE_TO_SQLITE(bind, value, env, handle, index);
|
||||
return value.IsBigInt() ? SQLITE_TOOBIG : -1;
|
||||
}
|
||||
|
||||
void ResultValueFromJS(Napi::Env env, sqlite3_context* invocation, Napi::Value value, DataConverter* converter) {
|
||||
JS_VALUE_TO_SQLITE(result, value, env, invocation);
|
||||
converter->ThrowDataConversionError(env, invocation, value.IsBigInt());
|
||||
}
|
||||
|
||||
}
|
||||
+190
@@ -0,0 +1,190 @@
|
||||
inline Napi::String StringFromUtf8(Napi::Env env, const char* data, int length) {
|
||||
if (length < 0) return Napi::String::New(env, data);
|
||||
return Napi::String::New(env, data, length);
|
||||
}
|
||||
|
||||
// Node-API has no equivalent of V8's internalized strings, so these are simple
|
||||
// aliases of StringFromUtf8; the names are kept to preserve the intent of call
|
||||
// sites (strings that are used repeatedly as property keys). If Node-API ever
|
||||
// gains this functionality, we can easily restore it here.
|
||||
inline Napi::String InternalizedFromUtf8(Napi::Env env, const char* data, int length) {
|
||||
return StringFromUtf8(env, data, length);
|
||||
}
|
||||
|
||||
inline Napi::Value InternalizedFromUtf8OrNull(Napi::Env env, const char* data, int length) {
|
||||
if (data == NULL) return env.Null();
|
||||
return InternalizedFromUtf8(env, data, length);
|
||||
}
|
||||
|
||||
inline Napi::String InternalizedFromLatin1(Napi::Env env, const char* str) {
|
||||
return Napi::String::New(env, str);
|
||||
}
|
||||
|
||||
// Replicates the semantics of v8::Value::IsInt32() (an integral number within
|
||||
// the range of a 32-bit signed integer, excluding -0), which has no direct
|
||||
// Node-API equivalent.
|
||||
inline bool IsInt32(Napi::Value value) {
|
||||
if (!value.IsNumber()) return false;
|
||||
double num = value.As<Napi::Number>().DoubleValue();
|
||||
if (!(num >= INT_MIN && num <= INT_MAX)) return false;
|
||||
if (num == 0) return !std::signbit(num);
|
||||
return static_cast<double>(static_cast<int32_t>(num)) == num;
|
||||
}
|
||||
|
||||
inline void SetFrozen(Napi::Env env, Napi::Object obj, const Napi::Reference<Napi::String>& key, Napi::Value value) {
|
||||
obj.DefineProperty(Napi::PropertyDescriptor::Value(key.Value(), value, napi_enumerable));
|
||||
}
|
||||
|
||||
// The following helpers perform JavaScript operations that can execute
|
||||
// arbitrary user code (functions, proxies, getters). If the user code throws,
|
||||
// the exception is left pending in the environment and an empty value is
|
||||
// returned. The node-addon-api equivalents (Napi::Function::Call,
|
||||
// Napi::Object::Get, etc.) must NOT be used for such operations, because their
|
||||
// failure paths re-throw the pending exception through Napi::Error, which
|
||||
// aborts the process when the thrown value is null or undefined (a
|
||||
// node-addon-api bug, present as of v8.9.0, triggered by Node-API version 10).
|
||||
|
||||
inline Napi::Value SafeCall(Napi::Env env, Napi::Function fn, napi_value recv, size_t argc, const napi_value* args) {
|
||||
napi_value result;
|
||||
if (napi_call_function(env, recv, fn, argc, args, &result) != napi_ok) return Napi::Value();
|
||||
return Napi::Value(env, result);
|
||||
}
|
||||
|
||||
inline Napi::Object SafeConstruct(Napi::Env env, Napi::Function constructor) {
|
||||
napi_value result;
|
||||
if (napi_new_instance(env, constructor, 0, NULL, &result) != napi_ok) return Napi::Object();
|
||||
return Napi::Object(env, result);
|
||||
}
|
||||
|
||||
inline Napi::Value SafeGet(Napi::Env env, Napi::Object obj, napi_value key) {
|
||||
napi_value result;
|
||||
if (napi_get_property(env, obj, key, &result) != napi_ok) return Napi::Value();
|
||||
return Napi::Value(env, result);
|
||||
}
|
||||
|
||||
inline Napi::Value SafeGetElement(Napi::Env env, Napi::Object obj, uint32_t index) {
|
||||
napi_value result;
|
||||
if (napi_get_element(env, obj, index, &result) != napi_ok) return Napi::Value();
|
||||
return Napi::Value(env, result);
|
||||
}
|
||||
|
||||
// Returns false if the check itself failed (leaving an exception pending).
|
||||
inline bool SafeHasOwnProperty(Napi::Env env, Napi::Object obj, napi_value key, bool* result) {
|
||||
return napi_has_own_property(env, obj, key, result) == napi_ok;
|
||||
}
|
||||
|
||||
Napi::Value ThrowError(Napi::Env env, const char* message) {
|
||||
Napi::Error::New(env, message).ThrowAsJavaScriptException();
|
||||
return env.Undefined();
|
||||
}
|
||||
Napi::Value ThrowTypeError(Napi::Env env, const char* message) {
|
||||
Napi::TypeError::New(env, message).ThrowAsJavaScriptException();
|
||||
return env.Undefined();
|
||||
}
|
||||
Napi::Value ThrowRangeError(Napi::Env env, const char* message) {
|
||||
Napi::RangeError::New(env, message).ThrowAsJavaScriptException();
|
||||
return env.Undefined();
|
||||
}
|
||||
|
||||
// Unwraps a native-backed object into its associated C++ instance. This must
|
||||
// only be used on values that are already known to be backed by the given
|
||||
// class (e.g., a receiver that was validated by TypeSafeCallback below, or a
|
||||
// value produced by our own code), because napi_unwrap does not check the
|
||||
// object's type. Returns NULL if the environment can no longer run JavaScript
|
||||
// (e.g., worker.terminate()), so callers should use UNWRAP_OR_RETURN, which
|
||||
// checks for that case.
|
||||
template <typename T> inline T* Unwrap(Napi::Value value) {
|
||||
return T::Unwrap(value.As<Napi::Object>());
|
||||
}
|
||||
|
||||
// Generates a random type tag when the addon is loaded. Type tags must not be
|
||||
// hardcoded, because multiple copies (or even different versions) of the addon
|
||||
// can be loaded into a single process (see the "nativeBinding" option), and
|
||||
// objects created by one copy must not be accepted by another copy's methods,
|
||||
// whose class layouts may differ.
|
||||
inline napi_type_tag RandomTypeTag() {
|
||||
std::random_device rd;
|
||||
auto random64 = [&rd]() {
|
||||
return (static_cast<uint64_t>(rd()) << 32) | static_cast<uint64_t>(rd());
|
||||
};
|
||||
return { random64(), random64() };
|
||||
}
|
||||
|
||||
// Determines whether the given value is an object that is backed by the given
|
||||
// native class. Each native class is identified by a unique type tag, which
|
||||
// gets applied to each of its instances upon construction.
|
||||
template <typename T> inline bool IsInstanceOf(Napi::Env env, Napi::Value value) {
|
||||
bool result = false;
|
||||
napi_status status = napi_check_object_type_tag(env, value.As<Napi::Object>(), &T::TYPE_TAG, &result);
|
||||
return status == napi_ok && result;
|
||||
}
|
||||
|
||||
// Wraps a native method so that it can only be invoked on a receiver that is
|
||||
// backed by the expected native class. Node-API has no equivalent of
|
||||
// v8::Signature, and napi_unwrap alone does not check the object's type, so
|
||||
// without this check, a method borrowed onto a foreign wrapped object (e.g.,
|
||||
// db.exec.call(stmt)) would reinterpret the wrapped pointer as the wrong type.
|
||||
template <typename T, Napi::Value (*method)(const Napi::CallbackInfo&)>
|
||||
napi_value TypeSafeCallback(napi_env env, napi_callback_info info) {
|
||||
Napi::CallbackInfo cbinfo(env, info);
|
||||
if (!IsInstanceOf<T>(env, cbinfo.This())) {
|
||||
Napi::TypeError::New(env, "Illegal invocation").ThrowAsJavaScriptException();
|
||||
return NULL;
|
||||
}
|
||||
return method(cbinfo);
|
||||
}
|
||||
|
||||
// These match the default attributes of properties created by a V8 template.
|
||||
constexpr napi_property_attributes DEFAULT_ATTRIBUTES =
|
||||
static_cast<napi_property_attributes>(napi_writable | napi_enumerable | napi_configurable);
|
||||
|
||||
template <typename T, Napi::Value (*method)(const Napi::CallbackInfo&)>
|
||||
napi_property_descriptor PrototypeMethod(const char* name, Addon* addon) {
|
||||
napi_property_descriptor desc = {};
|
||||
desc.utf8name = name;
|
||||
desc.method = TypeSafeCallback<T, method>;
|
||||
desc.attributes = DEFAULT_ATTRIBUTES;
|
||||
desc.data = addon;
|
||||
return desc;
|
||||
}
|
||||
|
||||
template <typename T, Napi::Value (*method)(const Napi::CallbackInfo&)>
|
||||
napi_property_descriptor PrototypeSymbolMethod(Napi::Symbol symbol, Addon* addon) {
|
||||
napi_property_descriptor desc = {};
|
||||
desc.name = symbol;
|
||||
desc.method = TypeSafeCallback<T, method>;
|
||||
desc.attributes = DEFAULT_ATTRIBUTES;
|
||||
desc.data = addon;
|
||||
return desc;
|
||||
}
|
||||
|
||||
// Defines a getter as an own property of the given object. V8 exposed these
|
||||
// getters as native data properties on each instance, which made them visible
|
||||
// to console.log(); Node-API has no native data properties, so an own accessor
|
||||
// property is the closest equivalent (unlike a prototype accessor, it is still
|
||||
// displayed by console.log(), albeit as [Getter]).
|
||||
template <typename T, Napi::Value (*method)(const Napi::CallbackInfo&)>
|
||||
void SetInstanceGetter(Napi::Object obj, const char* name, Addon* addon) {
|
||||
napi_property_descriptor desc = {};
|
||||
desc.utf8name = name;
|
||||
desc.getter = TypeSafeCallback<T, method>;
|
||||
desc.attributes = napi_enumerable;
|
||||
desc.data = addon;
|
||||
napi_status status = napi_define_properties(obj.Env(), obj, 1, &desc);
|
||||
assert(status == napi_ok || status == napi_cannot_run_js); ((void)status);
|
||||
}
|
||||
|
||||
// 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);
|
||||
}
|
||||
+74
@@ -0,0 +1,74 @@
|
||||
#define NODE_ARGUMENTS const Napi::CallbackInfo&
|
||||
#define NODE_ARGUMENTS_POINTER const Napi::CallbackInfo*
|
||||
#define NODE_METHOD(name) Napi::Value name(const Napi::CallbackInfo& info)
|
||||
#define NODE_GETTER(name) Napi::Value name(const Napi::CallbackInfo& info)
|
||||
#define INIT(name) Napi::Function name(Napi::Env env, Addon* addon)
|
||||
|
||||
#define OnlyAddon (static_cast<Addon*>(info.Data()))
|
||||
#define UseIsolate Napi::Env env = info.Env()
|
||||
#define UseAddon Addon* addon = static_cast<Addon*>(info.Data())
|
||||
|
||||
// Type-tag an object, but bail if the thread is terminating, to avoid
|
||||
// undesirable side-effects in the subsequent JS_new call (e.g, sqlite3_open()).
|
||||
#define TYPE_TAG_CONSTRUCTOR(info) \
|
||||
napi_status status = \
|
||||
napi_type_tag_object((info).Env(), (info).This(), &TYPE_TAG); \
|
||||
assert(status == napi_ok || status == napi_cannot_run_js); \
|
||||
if (status != napi_ok) return
|
||||
|
||||
// Unwrap a native-backed object (see ::Unwrap in helpers.cpp), but bail if the
|
||||
// the unwrap fails (which should only happen if the thread is terminating).
|
||||
#define UNWRAP_OR_RETURN(T, var, value) \
|
||||
T* var = ::Unwrap<T>(value); \
|
||||
if (var == NULL) return info.Env().Undefined()
|
||||
|
||||
#define REQUIRE_ARGUMENT_ANY(at, var) \
|
||||
if (info.Length() <= (at())) \
|
||||
return ThrowTypeError(info.Env(), "Expected a "#at" argument"); \
|
||||
var = info[at()]
|
||||
|
||||
#define _REQUIRE_ARGUMENT(at, var, Type, message, ...) \
|
||||
if (info.Length() <= (at()) || !info[at()].Is##Type()) \
|
||||
return ThrowTypeError(info.Env(), "Expected "#at" argument to be "#message); \
|
||||
var = (info[at()].As<Napi::Type>())__VA_ARGS__
|
||||
|
||||
#define REQUIRE_ARGUMENT_INT32(at, var) \
|
||||
if (info.Length() <= (at()) || !IsInt32(info[at()])) \
|
||||
return ThrowTypeError(info.Env(), "Expected "#at" argument to be a 32-bit signed integer"); \
|
||||
var = info[at()].As<Napi::Number>().Int32Value()
|
||||
#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(info.Env(), "The database connection is not open")
|
||||
#define REQUIRE_DATABASE_NOT_BUSY(db) \
|
||||
if (db->busy) \
|
||||
return ThrowTypeError(info.Env(), "This database connection is busy executing a query")
|
||||
#define REQUIRE_DATABASE_NO_ITERATORS(db) \
|
||||
if (db->iterators) \
|
||||
return ThrowTypeError(info.Env(), "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(info.Env(), "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
|
||||
+71
@@ -0,0 +1,71 @@
|
||||
#define STATEMENT_BIND(handle) \
|
||||
Binder binder(handle); \
|
||||
if (!binder.Bind(info, info.Length(), stmt)) { \
|
||||
sqlite3_clear_bindings(handle); \
|
||||
return info.Env().Undefined(); \
|
||||
} ((void)0)
|
||||
|
||||
#define STATEMENT_THROW_LOGIC() \
|
||||
db->ThrowDatabaseError(env); \
|
||||
if (!bound) { sqlite3_clear_bindings(handle); } \
|
||||
return env.Undefined()
|
||||
|
||||
#define STATEMENT_RETURN_LOGIC(return_value) \
|
||||
Napi::Value _return_value = (return_value); \
|
||||
if (!bound) { sqlite3_clear_bindings(handle); } \
|
||||
return _return_value
|
||||
|
||||
#define STATEMENT_START_LOGIC(RETURNS_DATA_CHECK, MUTATE_CHECK) \
|
||||
UNWRAP_OR_RETURN(Statement, stmt, 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(info.Env(), "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(env, 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(info.Env(), "Too many active database iterators")
|
||||
#define REQUIRE_STATEMENT_RETURNS_DATA() \
|
||||
if (!stmt->returns_data) \
|
||||
return ThrowTypeError(info.Env(), "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)); \
|
||||
Napi::Env env = self->env; \
|
||||
Napi::HandleScope scope(env)
|
||||
|
||||
#define FUNCTION_START() \
|
||||
_FUNCTION_START(CustomFunction)
|
||||
|
||||
#define AGGREGATE_START() \
|
||||
_FUNCTION_START(CustomAggregate); \
|
||||
Accumulator* acc = self->GetAccumulator(invocation); \
|
||||
if (acc->value.IsEmpty()) return
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
RowBuilder::RowBuilder(
|
||||
Napi::Env env,
|
||||
Napi::Function row_factory,
|
||||
Napi::Function array_factory
|
||||
) :
|
||||
row_factory(Napi::Persistent(row_factory)),
|
||||
array_factory(Napi::Persistent(array_factory)),
|
||||
column_count(-1),
|
||||
reprepare_count(-1) {}
|
||||
|
||||
Napi::Value RowBuilder::GetRowJS(Napi::Env env, sqlite3_stmt* handle, bool safe_ints) {
|
||||
int current_reprepare_count = sqlite3_stmt_status(handle, SQLITE_STMTSTATUS_REPREPARE, false);
|
||||
if (current_reprepare_count != reprepare_count) {
|
||||
column_count = sqlite3_column_count(handle);
|
||||
std::vector<napi_value> keys(column_count);
|
||||
for (int i = 0; i < column_count; ++i) {
|
||||
keys[i] = InternalizedFromUtf8(env, sqlite3_column_name(handle, i), -1);
|
||||
}
|
||||
create_row = Napi::Persistent(
|
||||
SafeCall(env, row_factory.Value(), env.Undefined(), column_count, keys.data())
|
||||
.As<Napi::Function>()
|
||||
);
|
||||
reprepare_count = current_reprepare_count;
|
||||
}
|
||||
|
||||
napi_value value_storage[16];
|
||||
std::vector<napi_value> extra_values;
|
||||
napi_value* values = value_storage;
|
||||
if (column_count > 16) {
|
||||
extra_values.resize(column_count);
|
||||
values = extra_values.data();
|
||||
}
|
||||
for (int i = 0; i < column_count; ++i) {
|
||||
values[i] = Data::GetValueJS(env, handle, i, safe_ints);
|
||||
}
|
||||
|
||||
return SafeCall(env, create_row.Value(), env.Undefined(), column_count, values);
|
||||
}
|
||||
|
||||
Napi::Value RowBuilder::GetRawRowJS(Napi::Env env, sqlite3_stmt* handle, bool safe_ints) {
|
||||
column_count = sqlite3_column_count(handle);
|
||||
napi_value arg_storage[16];
|
||||
std::vector<napi_value> extra_args;
|
||||
napi_value* args = arg_storage;
|
||||
if (column_count > 16) {
|
||||
extra_args.resize(column_count);
|
||||
args = extra_args.data();
|
||||
}
|
||||
for (int i = 0; i < column_count; ++i) {
|
||||
args[i] = Data::GetValueJS(env, handle, i, safe_ints);
|
||||
}
|
||||
return SafeCall(env, array_factory.Value(), env.Undefined(), column_count, args);
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
// Builds row objects efficiently by utilizing a factory function in JS land.
|
||||
// The column names are initialized only once and reused for every row/query.
|
||||
// The cache is rebuilt if SQLite reparses the statement after a schema change.
|
||||
class RowBuilder {
|
||||
public:
|
||||
|
||||
explicit RowBuilder(
|
||||
Napi::Env env,
|
||||
Napi::Function row_factory,
|
||||
Napi::Function array_factory
|
||||
);
|
||||
|
||||
Napi::Value GetRowJS(Napi::Env env, sqlite3_stmt* handle, bool safe_ints);
|
||||
Napi::Value GetRawRowJS(Napi::Env env, sqlite3_stmt* handle, bool safe_ints);
|
||||
|
||||
private:
|
||||
Napi::FunctionReference row_factory;
|
||||
Napi::FunctionReference create_row;
|
||||
Napi::FunctionReference array_factory;
|
||||
int column_count;
|
||||
int reprepare_count;
|
||||
};
|
||||
Reference in New Issue
Block a user