From e99160aae3f175a10e03079b10fce89707c217b9 Mon Sep 17 00:00:00 2001 From: Daniel Lockyer Date: Wed, 27 Dec 2023 21:06:26 +0100 Subject: [PATCH] Inlined `init()` functions into class header files - these extra functions aren't really necessary and means there is more redirection occurring - as a bonus, this fixes a variable shadowing issue in the Statement implementation --- src/backup.cc | 11 ++++++----- src/backup.h | 39 +++++++++++++-------------------------- src/database.cc | 1 - src/database.h | 30 +++++++++--------------------- src/statement.cc | 10 ++++++---- src/statement.h | 22 ++++++---------------- 6 files changed, 40 insertions(+), 73 deletions(-) diff --git a/src/backup.cc b/src/backup.cc index 5835a7e5..0f971f5e 100644 --- a/src/backup.cc +++ b/src/backup.cc @@ -153,7 +153,9 @@ Backup::Backup(const Napi::CallbackInfo& info) : Napi::ObjectWrap(info) return; } - auto* database = Napi::ObjectWrap::Unwrap(info[0].As()); + this->db = Napi::ObjectWrap::Unwrap(info[0].As()); + this->db->Ref(); + auto filename = info[1].As(); auto sourceName = info[2].As(); auto destName = info[3].As(); @@ -164,14 +166,13 @@ Backup::Backup(const Napi::CallbackInfo& info) : Napi::ObjectWrap(info) info.This().As().DefineProperty(Napi::PropertyDescriptor::Value("destName", destName)); info.This().As().DefineProperty(Napi::PropertyDescriptor::Value("filenameIsDest", filenameIsDest)); - init(database); - - auto* baton = new InitializeBaton(database, info[5].As(), this); + auto* baton = new InitializeBaton(this->db, info[5].As(), this); baton->filename = filename.Utf8Value(); baton->sourceName = sourceName.Utf8Value(); baton->destName = destName.Utf8Value(); baton->filenameIsDest = filenameIsDest.Value(); - database->Schedule(Work_BeginInitialize, baton); + + this->db->Schedule(Work_BeginInitialize, baton); } void Backup::Work_BeginInitialize(Database::Baton* baton) { diff --git a/src/backup.h b/src/backup.h index 2ce8e904..b894aca1 100644 --- a/src/backup.h +++ b/src/backup.h @@ -146,21 +146,6 @@ class Backup : public Napi::ObjectWrap { Baton* baton; }; - void init(Database* db_) { - db = db_; - _handle = NULL; - _otherDb = NULL; - _destDb = NULL; - inited = false; - locked = true; - completed = false; - failed = false; - remaining = -1; - pageCount = -1; - finished = false; - db->Ref(); - } - Backup(const Napi::CallbackInfo& info); ~Backup() { @@ -172,6 +157,7 @@ class Backup : public Napi::ObjectWrap { WORK_DEFINITION(Step) WORK_DEFINITION(Finish) + Napi::Value IdleGetter(const Napi::CallbackInfo& info); Napi::Value CompletedGetter(const Napi::CallbackInfo& info); Napi::Value FailedGetter(const Napi::CallbackInfo& info); @@ -199,19 +185,20 @@ class Backup : public Napi::ObjectWrap { Database* db; - sqlite3_backup* _handle; - sqlite3* _otherDb; - sqlite3* _destDb; + sqlite3_backup* _handle = NULL; + sqlite3* _otherDb = NULL; + sqlite3* _destDb = NULL; + + bool inited = false; + bool locked = true; + bool completed = false; + bool failed = false; + int remaining = -1; + int pageCount = -1; + bool finished = false; + int status; std::string message; - - bool inited; - bool locked; - bool completed; - bool failed; - int remaining; - int pageCount; - bool finished; std::queue> queue; Napi::Reference retryErrors; diff --git a/src/database.cc b/src/database.cc index d1e91645..656bcc03 100644 --- a/src/database.cc +++ b/src/database.cc @@ -118,7 +118,6 @@ void Database::Schedule(Work_Callback callback, Baton* baton, bool exclusive) { } Database::Database(const Napi::CallbackInfo& info) : Napi::ObjectWrap(info) { - init(); auto env = info.Env(); if (info.Length() <= 0 || !info[0].IsString()) { diff --git a/src/database.h b/src/database.h index daff3df0..8ffd300d 100644 --- a/src/database.h +++ b/src/database.h @@ -123,18 +123,6 @@ class Database : public Napi::ObjectWrap { friend class Statement; friend class Backup; - void init() { - _handle = NULL; - open = false; - closing = false; - locked = false; - pending = 0; - serialize = false; - debug_trace = NULL; - debug_profile = NULL; - update_event = NULL; - } - Database(const Napi::CallbackInfo& info); ~Database() { @@ -179,20 +167,20 @@ class Database : public Napi::ObjectWrap { void RemoveCallbacks(); protected: - sqlite3* _handle; + sqlite3* _handle = NULL; - bool open; - bool closing; - bool locked; - unsigned int pending; + bool open = false; + bool closing = false; + bool locked = false; + unsigned int pending = 0; - bool serialize; + bool serialize = false; std::queue queue; - AsyncTrace* debug_trace; - AsyncProfile* debug_profile; - AsyncUpdate* update_event; + AsyncTrace* debug_trace = NULL; + AsyncProfile* debug_profile = NULL; + AsyncUpdate* update_event = NULL; }; } diff --git a/src/statement.cc b/src/statement.cc index 58673250..fc49b90f 100644 --- a/src/statement.cc +++ b/src/statement.cc @@ -105,17 +105,19 @@ Statement::Statement(const Napi::CallbackInfo& info) : Napi::ObjectWrap::Unwrap(info[0].As()); + this->db = Napi::ObjectWrap::Unwrap(info[0].As()); + this->db->Ref(); + auto sql = info[1].As(); info.This().As().DefineProperty(Napi::PropertyDescriptor::Value("sql", sql, napi_default)); - init(db); + Statement* stmt = this; - auto* baton = new PrepareBaton(db, info[2].As(), stmt); + auto* baton = new PrepareBaton(this->db, info[2].As(), stmt); baton->sql = std::string(sql.As().Utf8Value().c_str()); - db->Schedule(Work_BeginPrepare, baton); + this->db->Schedule(Work_BeginPrepare, baton); } void Statement::Work_BeginPrepare(Database::Baton* baton) { diff --git a/src/statement.h b/src/statement.h index 1d280596..c522c0fd 100644 --- a/src/statement.h +++ b/src/statement.h @@ -189,16 +189,6 @@ class Statement : public Napi::ObjectWrap { } }; - void init(Database* db_) { - db = db_; - _handle = NULL; - status = SQLITE_OK; - prepared = false; - locked = true; - finalized = false; - db->Ref(); - } - Statement(const Napi::CallbackInfo& info); ~Statement() { @@ -239,14 +229,14 @@ class Statement : public Napi::ObjectWrap { protected: Database* db; - sqlite3_stmt* _handle; - int status; - std::string message; + sqlite3_stmt* _handle = NULL; + int status = SQLITE_OK; + bool prepared = false; + bool locked = true; + bool finalized = false; - bool prepared; - bool locked; - bool finalized; std::queue queue; + std::string message; }; }