Skip to content

Commit 5652da6

Browse files
cjihrigaduh95
authored andcommitted
sqlite: add DatabaseSync.prototype.isOpen
This commit adds a getter to indicate whether or not the database is currently open. Fixes: #57521 PR-URL: #57522 Reviewed-By: Ulises Gascón <[email protected]> Reviewed-By: Edy Silva <[email protected]> Reviewed-By: Chemi Atlow <[email protected]> Reviewed-By: Tobias Nießen <[email protected]> Reviewed-By: Michaël Zasso <[email protected]>
1 parent 3ddc5cd commit 5652da6

File tree

4 files changed

+28
-0
lines changed

4 files changed

+28
-0
lines changed

doc/api/sqlite.md

+8
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,14 @@ added: v23.5.0
192192
This method is used to create SQLite user-defined functions. This method is a
193193
wrapper around [`sqlite3_create_function_v2()`][].
194194

195+
### `database.isOpen`
196+
197+
<!-- YAML
198+
added: REPLACEME
199+
-->
200+
201+
* {boolean} Whether the database is currently open or not.
202+
195203
### `database.open()`
196204

197205
<!-- YAML

src/node_sqlite.cc

+10
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,12 @@ void DatabaseSync::Open(const FunctionCallbackInfo<Value>& args) {
767767
db->Open();
768768
}
769769

770+
void DatabaseSync::IsOpenGetter(const FunctionCallbackInfo<Value>& args) {
771+
DatabaseSync* db;
772+
ASSIGN_OR_RETURN_UNWRAP(&db, args.This());
773+
args.GetReturnValue().Set(db->IsOpen());
774+
}
775+
770776
void DatabaseSync::Close(const FunctionCallbackInfo<Value>& args) {
771777
DatabaseSync* db;
772778
ASSIGN_OR_RETURN_UNWRAP(&db, args.This());
@@ -2247,6 +2253,10 @@ static void Initialize(Local<Object> target,
22472253
DatabaseSync::EnableLoadExtension);
22482254
SetProtoMethod(
22492255
isolate, db_tmpl, "loadExtension", DatabaseSync::LoadExtension);
2256+
SetSideEffectFreeGetter(isolate,
2257+
db_tmpl,
2258+
FIXED_ONE_BYTE_STRING(isolate, "isOpen"),
2259+
DatabaseSync::IsOpenGetter);
22502260
SetConstructorFunction(context, target, "DatabaseSync", db_tmpl);
22512261
SetConstructorFunction(context,
22522262
target,

src/node_sqlite.h

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ class DatabaseSync : public BaseObject {
5555
void MemoryInfo(MemoryTracker* tracker) const override;
5656
static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
5757
static void Open(const v8::FunctionCallbackInfo<v8::Value>& args);
58+
static void IsOpenGetter(const v8::FunctionCallbackInfo<v8::Value>& args);
5859
static void Close(const v8::FunctionCallbackInfo<v8::Value>& args);
5960
static void Prepare(const v8::FunctionCallbackInfo<v8::Value>& args);
6061
static void Exec(const v8::FunctionCallbackInfo<v8::Value>& args);

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

+9
Original file line numberDiff line numberDiff line change
@@ -172,41 +172,50 @@ suite('DatabaseSync.prototype.open()', () => {
172172
const db = new DatabaseSync(dbPath, { open: false });
173173
t.after(() => { db.close(); });
174174

175+
t.assert.strictEqual(db.isOpen, false);
175176
t.assert.strictEqual(existsSync(dbPath), false);
176177
t.assert.strictEqual(db.open(), undefined);
178+
t.assert.strictEqual(db.isOpen, true);
177179
t.assert.strictEqual(existsSync(dbPath), true);
178180
});
179181

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

186+
t.assert.strictEqual(db.isOpen, false);
184187
db.open();
188+
t.assert.strictEqual(db.isOpen, true);
185189
t.assert.throws(() => {
186190
db.open();
187191
}, {
188192
code: 'ERR_INVALID_STATE',
189193
message: /database is already open/,
190194
});
195+
t.assert.strictEqual(db.isOpen, true);
191196
});
192197
});
193198

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

203+
t.assert.strictEqual(db.isOpen, true);
198204
t.assert.strictEqual(db.close(), undefined);
205+
t.assert.strictEqual(db.isOpen, false);
199206
});
200207

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

211+
t.assert.strictEqual(db.isOpen, false);
204212
t.assert.throws(() => {
205213
db.close();
206214
}, {
207215
code: 'ERR_INVALID_STATE',
208216
message: /database is not open/,
209217
});
218+
t.assert.strictEqual(db.isOpen, false);
210219
});
211220
});
212221

0 commit comments

Comments
 (0)