diff --git a/src/objects/database/database.cc b/src/objects/database/database.cc index 9bd283814..20c83a9e1 100644 --- a/src/objects/database/database.cc +++ b/src/objects/database/database.cc @@ -29,7 +29,7 @@ Nan::Persistent NullFactory; Database::Database() : Nan::ObjectWrap(), db_handle(NULL), - t_handles(NULL), + t_handles(), open(true), in_each(false), safe_ints(false) {} @@ -70,9 +70,8 @@ void Database::Init(v8::Local exports, v8::Local module) // Returns an SQLite3 result code. int Database::CloseHandles() { - delete t_handles; + t_handles.close(); int status = sqlite3_close(db_handle); - t_handles = NULL; db_handle = NULL; return status; } @@ -85,9 +84,7 @@ void Database::CloseChildHandles() { } int Database::OpenHandles(const char* filename) { - int status; - - status = sqlite3_open(filename, &db_handle); + int status = sqlite3_open(filename, &db_handle); if (status != SQLITE_OK) { return status; } @@ -100,6 +97,5 @@ int Database::OpenHandles(const char* filename) { sqlite3_limit(db_handle, SQLITE_LIMIT_COMPOUND_SELECT, 0x7fffffff); sqlite3_limit(db_handle, SQLITE_LIMIT_VARIABLE_NUMBER, 0x7fffffff); - t_handles = new TransactionHandles(db_handle, &status); - return status; + return t_handles.open(db_handle); } diff --git a/src/objects/database/database.h b/src/objects/database/database.h index ea4e547ce..cf57ef9fc 100644 --- a/src/objects/database/database.h +++ b/src/objects/database/database.h @@ -41,7 +41,7 @@ class Database : public Nan::ObjectWrap { // Sqlite3 interfacing sqlite3* db_handle; - TransactionHandles* t_handles; + TransactionHandles t_handles; // State bool open; diff --git a/src/objects/database/exec.cc b/src/objects/database/exec.cc index 04b712a5b..50e13f42a 100644 --- a/src/objects/database/exec.cc +++ b/src/objects/database/exec.cc @@ -21,7 +21,6 @@ NAN_METHOD(Database::Exec) { sqlite3_free(err); return Nan::ThrowError(message.c_str()); } - sqlite3_free(err); info.GetReturnValue().Set(info.This()); } diff --git a/src/objects/database/pragma.cc b/src/objects/database/pragma.cc index 3895970f0..e582fa8ec 100644 --- a/src/objects/database/pragma.cc +++ b/src/objects/database/pragma.cc @@ -50,7 +50,6 @@ NAN_METHOD(Database::Pragma) { sqlite3_free(err); return Nan::ThrowError(message.c_str()); } - sqlite3_free(err); if (simple_result && !pragma_info.after_first) { info.GetReturnValue().Set(Nan::Undefined()); diff --git a/src/objects/statement/each.cc b/src/objects/statement/each.cc index f4947faa1..204c9e68a 100644 --- a/src/objects/statement/each.cc +++ b/src/objects/statement/each.cc @@ -1,4 +1,4 @@ -// .each(...any boundValues, Function callback) -> undefined +// .each(...any boundValues, function callback) -> undefined NAN_METHOD(Statement::Each) { Statement* stmt = Nan::ObjectWrap::Unwrap(info.This()); diff --git a/src/objects/transaction/run.cc b/src/objects/transaction/run.cc index 4db03b727..7fe7481b5 100644 --- a/src/objects/transaction/run.cc +++ b/src/objects/transaction/run.cc @@ -5,11 +5,11 @@ NAN_METHOD(Transaction::Run) { QUERY_START(trans, transaction, TRANSACTION_BIND, SQLITE_STATIC, info, info.Length()); sqlite3* db_handle = trans->db->db_handle; - TransactionHandles* t_handles = trans->db->t_handles; + TransactionHandles t_handles = trans->db->t_handles; // Begin Transaction - sqlite3_step(t_handles->begin); - if (sqlite3_reset(t_handles->begin) != SQLITE_OK) { + sqlite3_step(t_handles.begin); + if (sqlite3_reset(t_handles.begin) != SQLITE_OK) { QUERY_THROW(trans, TRANSACTION_CLEAR_BINDINGS, sqlite3_errmsg(db_handle)); } @@ -22,8 +22,8 @@ NAN_METHOD(Transaction::Run) { sqlite3_step(trans->handles[i]); if (sqlite3_reset(trans->handles[i]) != SQLITE_OK) { QUERY_THROW_STAY(trans, TRANSACTION_CLEAR_BINDINGS, sqlite3_errmsg(db_handle)); - sqlite3_step(t_handles->rollback); - sqlite3_reset(t_handles->rollback); + sqlite3_step(t_handles.rollback); + sqlite3_reset(t_handles.rollback); return; } @@ -33,11 +33,11 @@ NAN_METHOD(Transaction::Run) { } // Commit Transaction - sqlite3_step(t_handles->commit); - if (sqlite3_reset(t_handles->commit) != SQLITE_OK) { + sqlite3_step(t_handles.commit); + if (sqlite3_reset(t_handles.commit) != SQLITE_OK) { QUERY_THROW_STAY(trans, TRANSACTION_CLEAR_BINDINGS, sqlite3_errmsg(db_handle)); - sqlite3_step(t_handles->rollback); - sqlite3_reset(t_handles->rollback); + sqlite3_step(t_handles.rollback); + sqlite3_reset(t_handles.rollback); return; } diff --git a/src/util/macros.h b/src/util/macros.h index 8f2fad9e1..18b344a26 100644 --- a/src/util/macros.h +++ b/src/util/macros.h @@ -117,8 +117,7 @@ inline bool IS_32BIT_INT(double num) { // is thrown and the caller returns. #define INVOKE_METHOD(result, obj, methodName, argc, argv) \ GET_METHOD(_method, obj, methodName); \ - Nan::MaybeLocal _maybeValue = \ - Nan::Call(_method, obj, argc, argv); \ + Nan::MaybeLocal _maybeValue = _method->Call(obj, argc, argv); \ if (_maybeValue.IsEmpty()) {return;} \ v8::Local result = _maybeValue.ToLocalChecked(); diff --git a/src/util/transaction-handles.h b/src/util/transaction-handles.h index 40c52100a..b41391332 100644 --- a/src/util/transaction-handles.h +++ b/src/util/transaction-handles.h @@ -4,40 +4,41 @@ #include // A simple construct for holding three sqlite3_stmt pointers. -// After construction, if statusOut is SQLITE_OK, the three pointers -// can be used to start, commit, and rollback transactions. -class TransactionHandles { - public: - explicit TransactionHandles(sqlite3* db_handle, int* statusOut) - : begin(NULL) - , commit(NULL) - , rollback(NULL) { - int status; - - status = sqlite3_prepare_v2(db_handle, "BEGIN TRANSACTION;", -1, &begin, NULL); - if (status != SQLITE_OK) { - *statusOut = status; - return; - } - - status = sqlite3_prepare_v2(db_handle, "COMMIT TRANSACTION;", -1, &commit, NULL); - if (status != SQLITE_OK) { - *statusOut = status; - return; - } - - *statusOut = sqlite3_prepare_v2(db_handle, "ROLLBACK TRANSACTION;", -1, &rollback, NULL); +// After invoking open(), if SQLITE_OK is returned, the three pointers can be +// used to start, commit, and rollback transactions. +// This object does not own its own data. The owner of this object must +// manually invoke close() to free the memory held by the three pointers. +class TransactionHandles { public: + explicit TransactionHandles() : begin(NULL), commit(NULL), rollback(NULL) {} + + int open(sqlite3* db_handle) { + int status; + + status = sqlite3_prepare_v2(db_handle, "BEGIN TRANSACTION;", -1, &begin, NULL); + if (status != SQLITE_OK) { + return status; } - ~TransactionHandles() { - sqlite3_finalize(begin); - sqlite3_finalize(commit); - sqlite3_finalize(rollback); + + status = sqlite3_prepare_v2(db_handle, "COMMIT TRANSACTION;", -1, &commit, NULL); + if (status != SQLITE_OK) { + return status; } - public: - sqlite3_stmt* begin; - sqlite3_stmt* commit; - sqlite3_stmt* rollback; + return sqlite3_prepare_v2(db_handle, "ROLLBACK TRANSACTION;", -1, &rollback, NULL); + } + + void close() { + sqlite3_finalize(begin); + sqlite3_finalize(commit); + sqlite3_finalize(rollback); + begin = NULL; + commit = NULL; + rollback = NULL; + } + + sqlite3_stmt* begin; + sqlite3_stmt* commit; + sqlite3_stmt* rollback; }; #endif \ No newline at end of file