sqlite: add timeout options to DatabaseSync · nodejs/node@0df87e0 · GitHub
Skip to content

Commit 0df87e0

Browse files
geeksilva97RafaelGSS
authored andcommitted
sqlite: add timeout options to DatabaseSync
PR-URL: #57752 Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
1 parent ce1b5aa commit 0df87e0

5 files changed

Lines changed: 112 additions & 0 deletions

File tree

doc/api/sqlite.md

Lines changed: 7 additions & 0 deletions

src/node_sqlite.cc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,8 @@ bool DatabaseSync::Open() {
732732
CHECK_ERROR_OR_THROW(env()->isolate(), this, r, SQLITE_OK, false);
733733
CHECK_EQ(foreign_keys_enabled, open_config_.get_enable_foreign_keys());
734734

735+
sqlite3_busy_timeout(connection_, open_config_.get_timeout());
736+
735737
if (allow_load_extension_) {
736738
if (env()->permission()->enabled()) [[unlikely]] {
737739
THROW_ERR_LOAD_SQLITE_EXTENSION(env(),
@@ -942,6 +944,23 @@ void DatabaseSync::New(const FunctionCallbackInfo<Value>& args) {
942944
}
943945
allow_load_extension = allow_extension_v.As<Boolean>()->Value();
944946
}
947+
948+
Local<Value> timeout_v;
949+
if (!options->Get(env->context(), env->timeout_string())
950+
.ToLocal(&timeout_v)) {
951+
return;
952+
}
953+
954+
if (!timeout_v->IsUndefined()) {
955+
if (!timeout_v->IsInt32()) {
956+
THROW_ERR_INVALID_ARG_TYPE(
957+
env->isolate(),
958+
"The \"options.timeout\" argument must be an integer.");
959+
return;
960+
}
961+
962+
open_config.set_timeout(timeout_v.As<Int32>()->Value());
963+
}
945964
}
946965

947966
new DatabaseSync(

src/node_sqlite.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,16 @@ class DatabaseOpenConfiguration {
3535

3636
inline void set_enable_dqs(bool flag) { enable_dqs_ = flag; }
3737

38+
inline void set_timeout(int timeout) { timeout_ = timeout; }
39+
40+
inline int get_timeout() { return timeout_; }
41+
3842
private:
3943
std::string location_;
4044
bool read_only_ = false;
4145
bool enable_foreign_keys_ = true;
4246
bool enable_dqs_ = false;
47+
int timeout_ = 0;
4348
};
4449

4550
class StatementSync;

test/parallel/test-sqlite-database-sync.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,15 @@ suite('DatabaseSync() constructor', () => {
7777
});
7878
});
7979

80+
test('throws if options.timeout is provided but is not an integer', (t) => {
81+
t.assert.throws(() => {
82+
new DatabaseSync('foo', { timeout: .99 });
83+
}, {
84+
code: 'ERR_INVALID_ARG_TYPE',
85+
message: /The "options\.timeout" argument must be an integer/,
86+
});
87+
});
88+
8089
test('is not read-only by default', (t) => {
8190
const dbPath = nextDb();
8291
const db = new DatabaseSync(dbPath);
Lines changed: 72 additions & 0 deletions

0 commit comments

Comments
 (0)