@@ -1043,6 +1043,12 @@ void DatabaseSync::UntrackStatement(StatementSync* statement) {
10431043 }
10441044}
10451045
1046+ int DatabaseSync::Step (sqlite3_stmt* statement) {
1047+ statement_execution_depth_++;
1048+ auto leave = OnScopeLeave ([&]() { statement_execution_depth_--; });
1049+ return sqlite3_step (statement);
1050+ }
1051+
10461052inline bool DatabaseSync::IsOpen () {
10471053 return connection_ != nullptr ;
10481054}
@@ -1434,6 +1440,10 @@ void DatabaseSync::Close(const FunctionCallbackInfo<Value>& args) {
14341440 ASSIGN_OR_RETURN_UNWRAP (&db, args.This ());
14351441 Environment* env = Environment::GetCurrent (args);
14361442 THROW_AND_RETURN_ON_BAD_STATE (env, !db->IsOpen (), " database is not open" );
1443+ THROW_AND_RETURN_ON_BAD_STATE (
1444+ env,
1445+ db->statement_execution_depth_ != 0 ,
1446+ " cannot close database while a statement is executing" );
14371447 db->FinalizeStatements ();
14381448 db->DeleteSessions ();
14391449 int r = sqlite3_close_v2 (db->connection_ );
@@ -1829,6 +1839,10 @@ void DatabaseSync::Deserialize(const FunctionCallbackInfo<Value>& args) {
18291839 ASSIGN_OR_RETURN_UNWRAP (&db, args.This ());
18301840 Environment* env = Environment::GetCurrent (args);
18311841 THROW_AND_RETURN_ON_BAD_STATE (env, !db->IsOpen (), " database is not open" );
1842+ THROW_AND_RETURN_ON_BAD_STATE (
1843+ env,
1844+ db->statement_execution_depth_ != 0 ,
1845+ " cannot deserialize while a statement is executing" );
18321846
18331847 if (!args[0 ]->IsUint8Array ()) {
18341848 THROW_ERR_INVALID_ARG_TYPE (env->isolate (),
@@ -2884,7 +2898,7 @@ MaybeLocal<Value> StatementExecutionHelper::All(Environment* env,
28842898 LocalVector<Value> row_values (isolate);
28852899 LocalVector<Name> row_keys (isolate);
28862900
2887- while ((r = sqlite3_step (stmt)) == SQLITE_ROW ) {
2901+ while ((r = db-> Step (stmt)) == SQLITE_ROW ) {
28882902 if (num_cols == 0 ) {
28892903 num_cols = sqlite3_column_count (stmt);
28902904 }
@@ -2926,7 +2940,7 @@ MaybeLocal<Object> StatementExecutionHelper::Run(Environment* env,
29262940 bool use_big_ints) {
29272941 Isolate* isolate = env->isolate ();
29282942 EscapableHandleScope scope (isolate);
2929- sqlite3_step (stmt);
2943+ db-> Step (stmt);
29302944 int r = sqlite3_reset (stmt);
29312945 CHECK_ERROR_OR_THROW (isolate, db, r, SQLITE_OK , MaybeLocal<Object>());
29322946
@@ -3004,7 +3018,7 @@ MaybeLocal<Value> StatementExecutionHelper::Get(Environment* env,
30043018 EscapableHandleScope scope (isolate);
30053019 auto reset = OnScopeLeave ([&]() { sqlite3_reset (stmt); });
30063020
3007- int r = sqlite3_step (stmt);
3021+ int r = db-> Step (stmt);
30083022 if (r == SQLITE_DONE ) return scope.Escape (Undefined (isolate));
30093023 if (r != SQLITE_ROW ) {
30103024 THROW_ERR_SQLITE_ERROR (isolate, db);
@@ -3731,7 +3745,7 @@ void StatementSyncIterator::Next(const FunctionCallbackInfo<Value>& args) {
37313745 iter->statement_reset_generation_ != iter->stmt_ ->reset_generation_ ,
37323746 " iterator was invalidated" );
37333747
3734- int r = sqlite3_step (iter->stmt_ ->statement_ );
3748+ int r = iter-> stmt_ -> db_ -> Step (iter->stmt_ ->statement_ );
37353749 if (r != SQLITE_ROW ) {
37363750 CHECK_ERROR_OR_THROW (
37373751 env->isolate (), iter->stmt_ ->db_ .get (), r, SQLITE_DONE , void ());
0 commit comments