Skip to content

Commit

Permalink
sqlite: refactor open options
Browse files Browse the repository at this point in the history
Move options that are only relevant for opening the database into a
self-contained class.

PR-URL: nodejs#55442
Reviewed-By: Benjamin Gruenbaum <[email protected]>
  • Loading branch information
tniessen authored Oct 26, 2024
1 parent e312d60 commit 0668e64
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 35 deletions.
52 changes: 24 additions & 28 deletions src/node_sqlite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,11 @@ inline void THROW_ERR_SQLITE_ERROR(Isolate* isolate, const char* message) {

DatabaseSync::DatabaseSync(Environment* env,
Local<Object> object,
Local<String> location,
bool open,
bool enable_foreign_keys_on_open,
bool enable_dqs_on_open)
: BaseObject(env, object) {
DatabaseOpenConfiguration&& open_config,
bool open)
: BaseObject(env, object), open_config_(std::move(open_config)) {
MakeWeak();
node::Utf8Value utf8_location(env->isolate(), location);
location_ = utf8_location.ToString();
connection_ = nullptr;
enable_foreign_keys_on_open_ = enable_foreign_keys_on_open;
enable_dqs_on_open_ = enable_dqs_on_open;

if (open) {
Open();
Expand All @@ -120,7 +114,9 @@ DatabaseSync::~DatabaseSync() {
}

void DatabaseSync::MemoryInfo(MemoryTracker* tracker) const {
tracker->TrackField("location", location_);
// TODO(tniessen): more accurately track the size of all fields
tracker->TrackFieldWithSize(
"open_config", sizeof(open_config_), "DatabaseOpenConfiguration");
}

bool DatabaseSync::Open() {
Expand All @@ -131,27 +127,29 @@ bool DatabaseSync::Open() {

// TODO(cjihrig): Support additional flags.
int flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE;
int r = sqlite3_open_v2(location_.c_str(), &connection_, flags, nullptr);
int r = sqlite3_open_v2(
open_config_.location().c_str(), &connection_, flags, nullptr);
CHECK_ERROR_OR_THROW(env()->isolate(), connection_, r, SQLITE_OK, false);

r = sqlite3_db_config(connection_,
SQLITE_DBCONFIG_DQS_DML,
static_cast<int>(enable_dqs_on_open_),
static_cast<int>(open_config_.get_enable_dqs()),
nullptr);
CHECK_ERROR_OR_THROW(env()->isolate(), connection_, r, SQLITE_OK, false);
r = sqlite3_db_config(connection_,
SQLITE_DBCONFIG_DQS_DDL,
static_cast<int>(enable_dqs_on_open_),
static_cast<int>(open_config_.get_enable_dqs()),
nullptr);
CHECK_ERROR_OR_THROW(env()->isolate(), connection_, r, SQLITE_OK, false);

int foreign_keys_enabled;
r = sqlite3_db_config(connection_,
SQLITE_DBCONFIG_ENABLE_FKEY,
static_cast<int>(enable_foreign_keys_on_open_),
&foreign_keys_enabled);
r = sqlite3_db_config(
connection_,
SQLITE_DBCONFIG_ENABLE_FKEY,
static_cast<int>(open_config_.get_enable_foreign_keys()),
&foreign_keys_enabled);
CHECK_ERROR_OR_THROW(env()->isolate(), connection_, r, SQLITE_OK, false);
CHECK_EQ(foreign_keys_enabled, enable_foreign_keys_on_open_);
CHECK_EQ(foreign_keys_enabled, open_config_.get_enable_foreign_keys());

return true;
}
Expand Down Expand Up @@ -193,9 +191,11 @@ void DatabaseSync::New(const FunctionCallbackInfo<Value>& args) {
return;
}

std::string location =
node::Utf8Value(env->isolate(), args[0].As<String>()).ToString();
DatabaseOpenConfiguration open_config(std::move(location));

bool open = true;
bool enable_foreign_keys = true;
bool enable_dqs = false;

if (args.Length() > 1) {
if (!args[1]->IsObject()) {
Expand Down Expand Up @@ -234,7 +234,8 @@ void DatabaseSync::New(const FunctionCallbackInfo<Value>& args) {
"boolean.");
return;
}
enable_foreign_keys = enable_foreign_keys_v.As<Boolean>()->Value();
open_config.set_enable_foreign_keys(
enable_foreign_keys_v.As<Boolean>()->Value());
}

Local<String> enable_dqs_string = FIXED_ONE_BYTE_STRING(
Expand All @@ -252,16 +253,11 @@ void DatabaseSync::New(const FunctionCallbackInfo<Value>& args) {
"a boolean.");
return;
}
enable_dqs = enable_dqs_v.As<Boolean>()->Value();
open_config.set_enable_dqs(enable_dqs_v.As<Boolean>()->Value());
}
}

new DatabaseSync(env,
args.This(),
args[0].As<String>(),
open,
enable_foreign_keys,
enable_dqs);
new DatabaseSync(env, args.This(), std::move(open_config), open);
}

void DatabaseSync::Open(const FunctionCallbackInfo<Value>& args) {
Expand Down
33 changes: 26 additions & 7 deletions src/node_sqlite.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,37 @@
namespace node {
namespace sqlite {

class DatabaseOpenConfiguration {
public:
explicit DatabaseOpenConfiguration(std::string&& location)
: location_(std::move(location)) {}

inline const std::string& location() const { return location_; }

inline bool get_enable_foreign_keys() const { return enable_foreign_keys_; }

inline void set_enable_foreign_keys(bool flag) {
enable_foreign_keys_ = flag;
}

inline bool get_enable_dqs() const { return enable_dqs_; }

inline void set_enable_dqs(bool flag) { enable_dqs_ = flag; }

private:
std::string location_;
bool enable_foreign_keys_ = true;
bool enable_dqs_ = false;
};

class StatementSync;

class DatabaseSync : public BaseObject {
public:
DatabaseSync(Environment* env,
v8::Local<v8::Object> object,
v8::Local<v8::String> location,
bool open,
bool enable_foreign_keys_on_open,
bool enable_dqs_on_open);
DatabaseOpenConfiguration&& open_config,
bool open);
void MemoryInfo(MemoryTracker* tracker) const override;
static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
static void Open(const v8::FunctionCallbackInfo<v8::Value>& args);
Expand All @@ -42,11 +63,9 @@ class DatabaseSync : public BaseObject {
bool Open();

~DatabaseSync() override;
std::string location_;
DatabaseOpenConfiguration open_config_;
sqlite3* connection_;
std::unordered_set<StatementSync*> statements_;
bool enable_foreign_keys_on_open_;
bool enable_dqs_on_open_;
};

class StatementSync : public BaseObject {
Expand Down

0 comments on commit 0668e64

Please sign in to comment.