Skip to content

Commit 1f44af2

Browse files
committed
sqlite: make custom function to throw when in async mode
1 parent c114666 commit 1f44af2

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

src/node_sqlite.cc

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1256,12 +1256,12 @@ void Database::Prepare(const FunctionCallbackInfo<Value>& args) {
12561256
int r = sqlite3_prepare_v2(db->connection_, *sql, -1, &s, 0);
12571257
CHECK_ERROR_OR_THROW(env->isolate(), db, r, SQLITE_OK, void());
12581258

1259-
if (db->has_custom_func) {
1260-
db->has_custom_func = false;
1261-
}
1259+
bool stmt_has_custom_func = db->has_custom_func;
1260+
db->has_custom_func = false;
12621261

12631262
BaseObjectPtr<Statement> stmt =
12641263
Statement::Create(env, BaseObjectPtr<Database>(db), s);
1264+
stmt->has_custom_functions_ = stmt_has_custom_func;
12651265
db->statements_.insert(stmt.get());
12661266
args.GetReturnValue().Set(stmt->object());
12671267
}
@@ -1478,7 +1478,6 @@ void Database::CustomFunction(const FunctionCallbackInfo<Value>& args) {
14781478
nullptr,
14791479
UserDefinedFunction::xDestroy);
14801480
CHECK_ERROR_OR_THROW(env->isolate(), db, r, SQLITE_OK, void());
1481-
// calls void Database::AddCustomFunction(const std::string& name) {
14821481
db->AddCustomFunction(*name);
14831482
}
14841483

@@ -2197,6 +2196,7 @@ Statement::Statement(Environment* env,
21972196
: BaseObject(env, object), db_(std::move(db)) {
21982197
MakeWeak();
21992198
statement_ = stmt;
2199+
has_custom_functions_ = false;
22002200
use_big_ints_ = db_->use_big_ints();
22012201
return_arrays_ = db_->return_arrays();
22022202
allow_bare_named_params_ = db_->allow_bare_named_params();
@@ -2884,6 +2884,11 @@ void Statement::All(const FunctionCallbackInfo<Value>& args) {
28842884
return;
28852885
}
28862886

2887+
THROW_AND_RETURN_ON_BAD_STATE(
2888+
env,
2889+
stmt->has_custom_functions_,
2890+
"Custom functions are not supported in async mode");
2891+
28872892
Local<Promise::Resolver> resolver;
28882893
if (StatementAsyncExecutionHelper::All(env, stmt).ToLocal(&resolver)) {
28892894
args.GetReturnValue().Set(resolver->GetPromise());
@@ -2941,6 +2946,11 @@ void Statement::Get(const FunctionCallbackInfo<Value>& args) {
29412946
return;
29422947
}
29432948

2949+
THROW_AND_RETURN_ON_BAD_STATE(
2950+
env,
2951+
stmt->has_custom_functions_,
2952+
"Custom functions are not supported in async mode");
2953+
29442954
Local<Promise::Resolver> resolver;
29452955
if (StatementAsyncExecutionHelper::Get(env, stmt).ToLocal(&resolver)) {
29462956
args.GetReturnValue().Set(resolver->GetPromise());
@@ -2972,6 +2982,11 @@ void Statement::Run(const FunctionCallbackInfo<Value>& args) {
29722982
return;
29732983
}
29742984

2985+
THROW_AND_RETURN_ON_BAD_STATE(
2986+
env,
2987+
stmt->has_custom_functions_,
2988+
"Custom functions are not supported in async mode");
2989+
29752990
Local<Promise::Resolver> resolver;
29762991
if (StatementAsyncExecutionHelper::Run(env, stmt).ToLocal(&resolver)) {
29772992
args.GetReturnValue().Set(resolver->GetPromise());

src/node_sqlite.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ class Statement : public BaseObject {
301301
bool BindParams(const v8::FunctionCallbackInfo<v8::Value>& args);
302302
bool BindValue(const v8::Local<v8::Value>& value, const int index);
303303

304+
friend class Database;
304305
friend class StatementIterator;
305306
friend class SQLTagStore;
306307
friend class StatementExecutionHelper;

0 commit comments

Comments
 (0)