Skip to content

sqlite: add DatabaseSync.prototype.isOpen #57522

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions doc/api/sqlite.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,14 @@ added:
This method is used to create SQLite user-defined functions. This method is a
wrapper around [`sqlite3_create_function_v2()`][].

### `database.isOpen`

<!-- YAML
added: REPLACEME
-->

* {boolean} Whether the database is currently open or not.

### `database.open()`

<!-- YAML
Expand Down
10 changes: 10 additions & 0 deletions src/node_sqlite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,12 @@ void DatabaseSync::Open(const FunctionCallbackInfo<Value>& args) {
db->Open();
}

void DatabaseSync::IsOpenGetter(const FunctionCallbackInfo<Value>& args) {
DatabaseSync* db;
ASSIGN_OR_RETURN_UNWRAP(&db, args.This());
args.GetReturnValue().Set(db->IsOpen());
}

void DatabaseSync::Close(const FunctionCallbackInfo<Value>& args) {
DatabaseSync* db;
ASSIGN_OR_RETURN_UNWRAP(&db, args.This());
Expand Down Expand Up @@ -2169,6 +2175,10 @@ static void Initialize(Local<Object> target,
DatabaseSync::EnableLoadExtension);
SetProtoMethod(
isolate, db_tmpl, "loadExtension", DatabaseSync::LoadExtension);
SetSideEffectFreeGetter(isolate,
db_tmpl,
FIXED_ONE_BYTE_STRING(isolate, "isOpen"),
DatabaseSync::IsOpenGetter);
SetConstructorFunction(context, target, "DatabaseSync", db_tmpl);
SetConstructorFunction(context,
target,
Expand Down
1 change: 1 addition & 0 deletions src/node_sqlite.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class DatabaseSync : public BaseObject {
void MemoryInfo(MemoryTracker* tracker) const override;
static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
static void Open(const v8::FunctionCallbackInfo<v8::Value>& args);
static void IsOpenGetter(const v8::FunctionCallbackInfo<v8::Value>& args);
static void Close(const v8::FunctionCallbackInfo<v8::Value>& args);
static void Prepare(const v8::FunctionCallbackInfo<v8::Value>& args);
static void Exec(const v8::FunctionCallbackInfo<v8::Value>& args);
Expand Down
9 changes: 9 additions & 0 deletions test/parallel/test-sqlite-database-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,41 +172,50 @@ suite('DatabaseSync.prototype.open()', () => {
const db = new DatabaseSync(dbPath, { open: false });
t.after(() => { db.close(); });

t.assert.strictEqual(db.isOpen, false);
t.assert.strictEqual(existsSync(dbPath), false);
t.assert.strictEqual(db.open(), undefined);
t.assert.strictEqual(db.isOpen, true);
t.assert.strictEqual(existsSync(dbPath), true);
});

test('throws if database is already open', (t) => {
const db = new DatabaseSync(nextDb(), { open: false });
t.after(() => { db.close(); });

t.assert.strictEqual(db.isOpen, false);
db.open();
t.assert.strictEqual(db.isOpen, true);
t.assert.throws(() => {
db.open();
}, {
code: 'ERR_INVALID_STATE',
message: /database is already open/,
});
t.assert.strictEqual(db.isOpen, true);
});
});

suite('DatabaseSync.prototype.close()', () => {
test('closes an open database connection', (t) => {
const db = new DatabaseSync(nextDb());

t.assert.strictEqual(db.isOpen, true);
t.assert.strictEqual(db.close(), undefined);
t.assert.strictEqual(db.isOpen, false);
});

test('throws if database is not open', (t) => {
const db = new DatabaseSync(nextDb(), { open: false });

t.assert.strictEqual(db.isOpen, false);
t.assert.throws(() => {
db.close();
}, {
code: 'ERR_INVALID_STATE',
message: /database is not open/,
});
t.assert.strictEqual(db.isOpen, false);
});
});

Expand Down
Loading