@@ -597,7 +597,8 @@ void DatabaseSync::Prepare(const FunctionCallbackInfo<Value>& args) {
597597 sqlite3_stmt* s = nullptr ;
598598int r = sqlite3_prepare_v2 (db->connection_ , *sql, -1 , &s, 0 );
599599CHECK_ERROR_OR_THROW (env->isolate (), db, r, SQLITE_OK , void ());
600- BaseObjectPtr<StatementSync> stmt = StatementSync::Create (env, db, s);
600+ BaseObjectPtr<StatementSync> stmt =
601+ StatementSync::Create (env, BaseObjectPtr<DatabaseSync>(db), s);
601602 db->statements_ .insert (stmt.get ());
602603 args.GetReturnValue ().Set (stmt->object ());
603604}
@@ -1033,11 +1034,10 @@ void DatabaseSync::LoadExtension(const FunctionCallbackInfo<Value>& args) {
10331034
10341035StatementSync::StatementSync (Environment* env,
10351036 Local<Object> object,
1036- DatabaseSync* db,
1037+ BaseObjectPtr< DatabaseSync> db,
10371038 sqlite3_stmt* stmt)
1038- : BaseObject(env, object) {
1039+ : BaseObject(env, object), db_(std::move(db)) {
10391040MakeWeak ();
1040- db_ = db;
10411041 statement_ = stmt;
10421042// In the future, some of these options could be set at the database
10431043// connection level and inherited by statements to reduce boilerplate.
@@ -1065,7 +1065,7 @@ inline bool StatementSync::IsFinalized() {
10651065
10661066bool StatementSync::BindParams (const FunctionCallbackInfo<Value>& args) {
10671067int r = sqlite3_clear_bindings (statement_);
1068- CHECK_ERROR_OR_THROW (env ()->isolate (), db_, r, SQLITE_OK , false );
1068+ CHECK_ERROR_OR_THROW (env ()->isolate (), db_. get () , r, SQLITE_OK , false );
10691069
10701070int anon_idx = 1 ;
10711071int anon_start = 0 ;
@@ -1199,7 +1199,7 @@ bool StatementSync::BindValue(const Local<Value>& value, const int index) {
11991199return false ;
12001200 }
12011201
1202- CHECK_ERROR_OR_THROW (env ()->isolate (), db_, r, SQLITE_OK , false );
1202+ CHECK_ERROR_OR_THROW (env ()->isolate (), db_. get () , r, SQLITE_OK , false );
12031203return true ;
12041204}
12051205
@@ -1265,7 +1265,7 @@ void StatementSync::All(const FunctionCallbackInfo<Value>& args) {
12651265 env, stmt->IsFinalized (), " statement has been finalized" );
12661266 Isolate* isolate = env->isolate ();
12671267int r = sqlite3_reset (stmt->statement_ );
1268- CHECK_ERROR_OR_THROW (isolate, stmt->db_ , r, SQLITE_OK , void ());
1268+ CHECK_ERROR_OR_THROW (isolate, stmt->db_ . get () , r, SQLITE_OK , void ());
12691269
12701270if (!stmt->BindParams (args)) {
12711271return ;
@@ -1298,7 +1298,7 @@ void StatementSync::All(const FunctionCallbackInfo<Value>& args) {
12981298 rows.emplace_back (row);
12991299 }
13001300
1301- CHECK_ERROR_OR_THROW (isolate, stmt->db_ , r, SQLITE_DONE , void ());
1301+ CHECK_ERROR_OR_THROW (isolate, stmt->db_ . get () , r, SQLITE_DONE , void ());
13021302 args.GetReturnValue ().Set (Array::New (isolate, rows.data (), rows.size ()));
13031303}
13041304
@@ -1383,7 +1383,8 @@ void StatementSync::IterateNextCallback(
13831383
13841384int r = sqlite3_step (stmt->statement_ );
13851385if (r != SQLITE_ROW ) {
1386- CHECK_ERROR_OR_THROW (env->isolate (), stmt->db_ , r, SQLITE_DONE , void ());
1386+ CHECK_ERROR_OR_THROW (
1387+ env->isolate (), stmt->db_ .get (), r, SQLITE_DONE , void ());
13871388
13881389// cleanup when no more rows to fetch
13891390sqlite3_reset (stmt->statement_ );
@@ -1439,7 +1440,7 @@ void StatementSync::Iterate(const FunctionCallbackInfo<Value>& args) {
14391440auto isolate = env->isolate ();
14401441auto context = env->context ();
14411442int r = sqlite3_reset (stmt->statement_ );
1442- CHECK_ERROR_OR_THROW (env->isolate (), stmt->db_ , r, SQLITE_OK , void ());
1443+ CHECK_ERROR_OR_THROW (env->isolate (), stmt->db_ . get () , r, SQLITE_OK , void ());
14431444
14441445if (!stmt->BindParams (args)) {
14451446return ;
@@ -1516,7 +1517,7 @@ void StatementSync::Get(const FunctionCallbackInfo<Value>& args) {
15161517 env, stmt->IsFinalized (), " statement has been finalized" );
15171518 Isolate* isolate = env->isolate ();
15181519int r = sqlite3_reset (stmt->statement_ );
1519- CHECK_ERROR_OR_THROW (isolate, stmt->db_ , r, SQLITE_OK , void ());
1520+ CHECK_ERROR_OR_THROW (isolate, stmt->db_ . get () , r, SQLITE_OK , void ());
15201521
15211522if (!stmt->BindParams (args)) {
15221523return ;
@@ -1526,7 +1527,7 @@ void StatementSync::Get(const FunctionCallbackInfo<Value>& args) {
15261527 r = sqlite3_step (stmt->statement_ );
15271528if (r == SQLITE_DONE ) return ;
15281529if (r != SQLITE_ROW ) {
1529- THROW_ERR_SQLITE_ERROR (isolate, stmt->db_ );
1530+ THROW_ERR_SQLITE_ERROR (isolate, stmt->db_ . get () );
15301531return ;
15311532 }
15321533
@@ -1562,7 +1563,7 @@ void StatementSync::Run(const FunctionCallbackInfo<Value>& args) {
15621563THROW_AND_RETURN_ON_BAD_STATE (
15631564 env, stmt->IsFinalized (), " statement has been finalized" );
15641565int r = sqlite3_reset (stmt->statement_ );
1565- CHECK_ERROR_OR_THROW (env->isolate (), stmt->db_ , r, SQLITE_OK , void ());
1566+ CHECK_ERROR_OR_THROW (env->isolate (), stmt->db_ . get () , r, SQLITE_OK , void ());
15661567
15671568if (!stmt->BindParams (args)) {
15681569return ;
@@ -1571,7 +1572,7 @@ void StatementSync::Run(const FunctionCallbackInfo<Value>& args) {
15711572auto reset = OnScopeLeave ([&]() { sqlite3_reset (stmt->statement_ ); });
15721573 r = sqlite3_step (stmt->statement_ );
15731574if (r != SQLITE_ROW && r != SQLITE_DONE ) {
1574- THROW_ERR_SQLITE_ERROR (env->isolate (), stmt->db_ );
1575+ THROW_ERR_SQLITE_ERROR (env->isolate (), stmt->db_ . get () );
15751576return ;
15761577 }
15771578
@@ -1748,9 +1749,8 @@ Local<FunctionTemplate> StatementSync::GetConstructorTemplate(
17481749return tmpl;
17491750}
17501751
1751- BaseObjectPtr<StatementSync> StatementSync::Create (Environment* env,
1752- DatabaseSync* db,
1753- sqlite3_stmt* stmt) {
1752+ BaseObjectPtr<StatementSync> StatementSync::Create (
1753+ Environment* env, BaseObjectPtr<DatabaseSync> db, sqlite3_stmt* stmt) {
17541754 Local<Object> obj;
17551755if (!GetConstructorTemplate (env)
17561756 ->InstanceTemplate ()
@@ -1759,7 +1759,7 @@ BaseObjectPtr<StatementSync> StatementSync::Create(Environment* env,
17591759return BaseObjectPtr<StatementSync>();
17601760 }
17611761
1762- return MakeBaseObject<StatementSync>(env, obj, db , stmt);
1762+ return MakeBaseObject<StatementSync>(env, obj, std::move (db) , stmt);
17631763}
17641764
17651765Session::Session (Environment* env,
@@ -1826,7 +1826,7 @@ void Session::Changeset(const FunctionCallbackInfo<Value>& args) {
18261826void * pChangeset;
18271827int r = sqliteChangesetFunc (session->session_ , &nChangeset, &pChangeset);
18281828CHECK_ERROR_OR_THROW (
1829- env->isolate (), session->database_ , r, SQLITE_OK , void ());
1829+ env->isolate (), session->database_ . get () , r, SQLITE_OK , void ());
18301830
18311831auto freeChangeset = OnScopeLeave ([&] { sqlite3_free (pChangeset); });
18321832
0 commit comments