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‎

Lines changed: 8 additions & 0 deletions
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‎

Lines changed: 10 additions & 0 deletions
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+
voidDatabaseSync::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
voidDatabaseSync::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‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ class DatabaseSync : public BaseObject{
5555
voidMemoryInfo(MemoryTracker* tracker) constoverride;
5656
staticvoidNew(const v8::FunctionCallbackInfo<v8::Value>& args);
5757
staticvoidOpen(const v8::FunctionCallbackInfo<v8::Value>& args);
58+
staticvoidIsOpenGetter(const v8::FunctionCallbackInfo<v8::Value>& args);
5859
staticvoidClose(const v8::FunctionCallbackInfo<v8::Value>& args);
5960
staticvoidPrepare(const v8::FunctionCallbackInfo<v8::Value>& args);
6061
staticvoidExec(const v8::FunctionCallbackInfo<v8::Value>& args);

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,41 +172,50 @@ suite('DatabaseSync.prototype.open()', () =>{
172172
constdb=newDatabaseSync(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
constdb=newDatabaseSync(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: /databaseisalreadyopen/,
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
constdb=newDatabaseSync(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
constdb=newDatabaseSync(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: /databaseisnotopen/,
209217
});
218+
t.assert.strictEqual(db.isOpen,false);
210219
});
211220
});
212221

0 commit comments

Comments
(0)