@@ -1042,6 +1042,12 @@ void DatabaseSync::UntrackStatement(StatementSync* statement) {
10421042 }
10431043}
10441044
1045+ int DatabaseSync::Step (sqlite3_stmt* statement) {
1046+ statement_execution_depth_++;
1047+ auto leave = OnScopeLeave ([&]() { statement_execution_depth_--; });
1048+ return sqlite3_step (statement);
1049+ }
1050+
10451051inline bool DatabaseSync::IsOpen () {
10461052 return connection_ != nullptr ;
10471053}
@@ -1433,6 +1439,10 @@ void DatabaseSync::Close(const FunctionCallbackInfo<Value>& args) {
14331439 ASSIGN_OR_RETURN_UNWRAP (&db, args.This ());
14341440 Environment* env = Environment::GetCurrent (args);
14351441 THROW_AND_RETURN_ON_BAD_STATE (env, !db->IsOpen (), " database is not open" );
1442+ THROW_AND_RETURN_ON_BAD_STATE (
1443+ env,
1444+ db->statement_execution_depth_ != 0 ,
1445+ " cannot close database while a statement is executing" );
14361446 db->FinalizeStatements ();
14371447 db->DeleteSessions ();
14381448 int r = sqlite3_close_v2 (db->connection_ );
@@ -1828,6 +1838,10 @@ void DatabaseSync::Deserialize(const FunctionCallbackInfo<Value>& args) {
18281838 ASSIGN_OR_RETURN_UNWRAP (&db, args.This ());
18291839 Environment* env = Environment::GetCurrent (args);
18301840 THROW_AND_RETURN_ON_BAD_STATE (env, !db->IsOpen (), " database is not open" );
1841+ THROW_AND_RETURN_ON_BAD_STATE (
1842+ env,
1843+ db->statement_execution_depth_ != 0 ,
1844+ " cannot deserialize while a statement is executing" );
18311845
18321846 if (!args[0 ]->IsUint8Array ()) {
18331847 THROW_ERR_INVALID_ARG_TYPE (env->isolate (),
@@ -2897,7 +2911,7 @@ MaybeLocal<Value> StatementExecutionHelper::All(Environment* env,
28972911 LocalVector<Value> row_values (isolate);
28982912 LocalVector<Name> row_keys (isolate);
28992913
2900- while ((r = sqlite3_step (stmt)) == SQLITE_ROW ) {
2914+ while ((r = db-> Step (stmt)) == SQLITE_ROW ) {
29012915 if (num_cols == 0 ) {
29022916 num_cols = sqlite3_column_count (stmt);
29032917 }
@@ -2939,7 +2953,7 @@ MaybeLocal<Object> StatementExecutionHelper::Run(Environment* env,
29392953 bool use_big_ints) {
29402954 Isolate* isolate = env->isolate ();
29412955 EscapableHandleScope scope (isolate);
2942- sqlite3_step (stmt);
2956+ db-> Step (stmt);
29432957 int r = sqlite3_reset (stmt);
29442958 CHECK_ERROR_OR_THROW (isolate, db, r, SQLITE_OK , MaybeLocal<Object>());
29452959
@@ -3017,7 +3031,7 @@ MaybeLocal<Value> StatementExecutionHelper::Get(Environment* env,
30173031 EscapableHandleScope scope (isolate);
30183032 auto reset = OnScopeLeave ([&]() { sqlite3_reset (stmt); });
30193033
3020- int r = sqlite3_step (stmt);
3034+ int r = db-> Step (stmt);
30213035 if (r == SQLITE_DONE ) return scope.Escape (Undefined (isolate));
30223036 if (r != SQLITE_ROW ) {
30233037 THROW_ERR_SQLITE_ERROR (isolate, db);
@@ -3744,7 +3758,7 @@ void StatementSyncIterator::Next(const FunctionCallbackInfo<Value>& args) {
37443758 iter->statement_reset_generation_ != iter->stmt_ ->reset_generation_ ,
37453759 " iterator was invalidated" );
37463760
3747- int r = sqlite3_step (iter->stmt_ ->statement_ );
3761+ int r = iter-> stmt_ -> db_ -> Step (iter->stmt_ ->statement_ );
37483762 if (r != SQLITE_ROW ) {
37493763 CHECK_ERROR_OR_THROW (
37503764 env->isolate (), iter->stmt_ ->db_ .get (), r, SQLITE_DONE , void ());
0 commit comments