Skip to content

Commit

Permalink
Inlined init() functions into class header files
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
daniellockyer committed Dec 27, 2023
1 parent 3372130 commit e99160a
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 73 deletions.
11 changes: 6 additions & 5 deletions src/backup.cc
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,9 @@ Backup::Backup(const Napi::CallbackInfo& info) : Napi::ObjectWrap<Backup>(info)
return;
}

auto* database = Napi::ObjectWrap<Database>::Unwrap(info[0].As<Napi::Object>());
this->db = Napi::ObjectWrap<Database>::Unwrap(info[0].As<Napi::Object>());
this->db->Ref();

auto filename = info[1].As<Napi::String>();
auto sourceName = info[2].As<Napi::String>();
auto destName = info[3].As<Napi::String>();
Expand All @@ -164,14 +166,13 @@ Backup::Backup(const Napi::CallbackInfo& info) : Napi::ObjectWrap<Backup>(info)
info.This().As<Napi::Object>().DefineProperty(Napi::PropertyDescriptor::Value("destName", destName));
info.This().As<Napi::Object>().DefineProperty(Napi::PropertyDescriptor::Value("filenameIsDest", filenameIsDest));

init(database);

auto* baton = new InitializeBaton(database, info[5].As<Napi::Function>(), this);
auto* baton = new InitializeBaton(this->db, info[5].As<Napi::Function>(), 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) {
Expand Down
39 changes: 13 additions & 26 deletions src/backup.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,21 +146,6 @@ class Backup : public Napi::ObjectWrap<Backup> {
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() {
Expand All @@ -172,6 +157,7 @@ class Backup : public Napi::ObjectWrap<Backup> {

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);
Expand Down Expand Up @@ -199,19 +185,20 @@ class Backup : public Napi::ObjectWrap<Backup> {

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<std::unique_ptr<Call>> queue;

Napi::Reference<Array> retryErrors;
Expand Down
1 change: 0 additions & 1 deletion src/database.cc
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ void Database::Schedule(Work_Callback callback, Baton* baton, bool exclusive) {
}

Database::Database(const Napi::CallbackInfo& info) : Napi::ObjectWrap<Database>(info) {
init();
auto env = info.Env();

if (info.Length() <= 0 || !info[0].IsString()) {
Expand Down
30 changes: 9 additions & 21 deletions src/database.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,18 +123,6 @@ class Database : public Napi::ObjectWrap<Database> {
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() {
Expand Down Expand Up @@ -179,20 +167,20 @@ class Database : public Napi::ObjectWrap<Database> {
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<Call*> queue;

AsyncTrace* debug_trace;
AsyncProfile* debug_profile;
AsyncUpdate* update_event;
AsyncTrace* debug_trace = NULL;
AsyncProfile* debug_profile = NULL;
AsyncUpdate* update_event = NULL;
};

}
Expand Down
10 changes: 6 additions & 4 deletions src/statement.cc
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,19 @@ Statement::Statement(const Napi::CallbackInfo& info) : Napi::ObjectWrap<Statemen
return;
}

Database* db = Napi::ObjectWrap<Database>::Unwrap(info[0].As<Napi::Object>());
this->db = Napi::ObjectWrap<Database>::Unwrap(info[0].As<Napi::Object>());
this->db->Ref();

auto sql = info[1].As<Napi::String>();

info.This().As<Napi::Object>().DefineProperty(Napi::PropertyDescriptor::Value("sql", sql, napi_default));

init(db);

Statement* stmt = this;

auto* baton = new PrepareBaton(db, info[2].As<Napi::Function>(), stmt);
auto* baton = new PrepareBaton(this->db, info[2].As<Napi::Function>(), stmt);
baton->sql = std::string(sql.As<Napi::String>().Utf8Value().c_str());
db->Schedule(Work_BeginPrepare, baton);
this->db->Schedule(Work_BeginPrepare, baton);
}

void Statement::Work_BeginPrepare(Database::Baton* baton) {
Expand Down
22 changes: 6 additions & 16 deletions src/statement.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,16 +189,6 @@ class Statement : public Napi::ObjectWrap<Statement> {
}
};

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() {
Expand Down Expand Up @@ -239,14 +229,14 @@ class Statement : public Napi::ObjectWrap<Statement> {
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<Call*> queue;
std::string message;
};

}
Expand Down

0 comments on commit e99160a

Please sign in to comment.