mirror of
https://github.com/hanzoai/better-sqlite3-multiple-ciphers.git
synced 2026-09-24 16:57:19 +00:00
Reimplement Database.key() and Database.rekey() to use Node-API
This commit is contained in:
@@ -62,6 +62,8 @@ module.exports = function createDatabase(getAddon, allowNativeBinding) {
|
||||
|
||||
const wrappers = require('./methods/wrappers');
|
||||
Database.prototype.prepare = wrappers.prepare;
|
||||
Database.prototype.key = wrappers.key;
|
||||
Database.prototype.rekey = wrappers.rekey;
|
||||
Database.prototype.transaction = require('./methods/transaction');
|
||||
Database.prototype.pragma = require('./methods/pragma');
|
||||
Database.prototype.explain = require('./methods/explain');
|
||||
|
||||
@@ -5,6 +5,16 @@ exports.prepare = function prepare(sql) {
|
||||
return this[cppdb].prepare(sql, this, false, false);
|
||||
};
|
||||
|
||||
exports.key = function key(key) {
|
||||
if (!Buffer.isBuffer(key)) throw new TypeError('Expected the key to be a Buffer');
|
||||
return this[cppdb].key(key);
|
||||
};
|
||||
|
||||
exports.rekey = function rekey(key) {
|
||||
if (!Buffer.isBuffer(key)) throw new TypeError('Expected the key to be a Buffer');
|
||||
return this[cppdb].rekey(key);
|
||||
};
|
||||
|
||||
exports.exec = function exec(sql) {
|
||||
this[cppdb].exec(sql);
|
||||
return this;
|
||||
|
||||
@@ -124,6 +124,8 @@ void Database::FreeSerialization(Napi::Env env, char* data) {
|
||||
INIT(Database::Init) {
|
||||
return DefineClass(env, "Database", {
|
||||
PrototypeMethod<Database, &Database::JS_prepare>("prepare", addon),
|
||||
PrototypeMethod<Database, &Database::JS_key>("key", addon),
|
||||
PrototypeMethod<Database, &Database::JS_rekey>("rekey", addon),
|
||||
PrototypeMethod<Database, &Database::JS_exec>("exec", addon),
|
||||
PrototypeMethod<Database, &Database::JS_backup>("backup", addon),
|
||||
PrototypeMethod<Database, &Database::JS_serialize>("serialize", addon),
|
||||
@@ -214,6 +216,38 @@ NODE_METHOD(Database::JS_prepare) {
|
||||
return statement;
|
||||
}
|
||||
|
||||
NODE_METHOD(Database::JS_key) {
|
||||
Database* db = ::Unwrap<Database>(info.This());
|
||||
REQUIRE_ARGUMENT_BUFFER(first, Napi::Buffer<char> key);
|
||||
REQUIRE_DATABASE_OPEN(db);
|
||||
REQUIRE_DATABASE_NOT_BUSY(db);
|
||||
|
||||
int status = sqlite3_key(db->db_handle, key.Data(), static_cast<int>(key.Length()));
|
||||
|
||||
if (status != SQLITE_OK) {
|
||||
ThrowSqliteError(info.Env(), db->addon, sqlite3_errstr(status), status);
|
||||
return info.Env().Undefined();
|
||||
}
|
||||
|
||||
return Napi::Number::New(info.Env(), status);
|
||||
}
|
||||
|
||||
NODE_METHOD(Database::JS_rekey) {
|
||||
Database* db = ::Unwrap<Database>(info.This());
|
||||
REQUIRE_ARGUMENT_BUFFER(first, Napi::Buffer<char> key);
|
||||
REQUIRE_DATABASE_OPEN(db);
|
||||
REQUIRE_DATABASE_NOT_BUSY(db);
|
||||
|
||||
int status = sqlite3_rekey(db->db_handle, key.Data(), static_cast<int>(key.Length()));
|
||||
|
||||
if (status != SQLITE_OK) {
|
||||
ThrowSqliteError(info.Env(), db->addon, sqlite3_errstr(status), status);
|
||||
return info.Env().Undefined();
|
||||
}
|
||||
|
||||
return Napi::Number::New(info.Env(), status);
|
||||
}
|
||||
|
||||
NODE_METHOD(Database::JS_exec) {
|
||||
Database* db = ::Unwrap<Database>(info.This());
|
||||
REQUIRE_ARGUMENT_STRING(first, Napi::String source);
|
||||
|
||||
@@ -66,6 +66,8 @@ private:
|
||||
|
||||
NODE_METHOD(JS_new);
|
||||
static NODE_METHOD(JS_prepare);
|
||||
static NODE_METHOD(JS_key);
|
||||
static NODE_METHOD(JS_rekey);
|
||||
static NODE_METHOD(JS_exec);
|
||||
static NODE_METHOD(JS_backup);
|
||||
static NODE_METHOD(JS_serialize);
|
||||
|
||||
@@ -30,6 +30,10 @@
|
||||
_REQUIRE_ARGUMENT(at, var, Object, an object)
|
||||
#define REQUIRE_ARGUMENT_FUNCTION(at, var) \
|
||||
_REQUIRE_ARGUMENT(at, var, Function, a function)
|
||||
#define REQUIRE_ARGUMENT_BUFFER(at, var) \
|
||||
if (info.Length() <= (at()) || !info[at()].IsBuffer()) \
|
||||
return ThrowTypeError(info.Env(), "Expected "#at" argument to be a Buffer"); \
|
||||
var = info[at()].As<Napi::Buffer<char>>()
|
||||
|
||||
#define REQUIRE_DATABASE_OPEN(db) \
|
||||
if (!db->open) \
|
||||
|
||||
Reference in New Issue
Block a user