Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 5652da6

Browse filesBrowse 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 <ulisesgascongonzalez@gmail.com> Reviewed-By: Edy Silva <edigleyssonsilva@gmail.com> Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Michaël Zasso <targos@protonmail.com>
1 parent 3ddc5cd commit 5652da6
Copy full SHA for 5652da6

File tree

Expand file treeCollapse file tree

4 files changed

+28
-0
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

4 files changed

+28
-0
lines changed
Open diff view settings
Collapse file

‎doc/api/sqlite.md‎

Copy file name to clipboardExpand all lines: doc/api/sqlite.md
+8Lines changed: 8 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
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
Collapse file

‎src/node_sqlite.cc‎

Copy file name to clipboardExpand all lines: src/node_sqlite.cc
+10Lines 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+
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,
Collapse file

‎src/node_sqlite.h‎

Copy file name to clipboardExpand all lines: src/node_sqlite.h
+1Lines changed: 1 addition & 0 deletions
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);
Collapse file

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

Copy file name to clipboardExpand all lines: test/parallel/test-sqlite-database-sync.js
+9Lines changed: 9 additions & 0 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.