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 e0418ab

Browse filesBrowse files
committed
apply review suggestions
1 parent fa8c2eb commit e0418ab
Copy full SHA for e0418ab

File tree

Expand file treeCollapse file tree

3 files changed

+23
-15
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+23
-15
lines changed

‎doc/api/sqlite.md

Copy file name to clipboardExpand all lines: doc/api/sqlite.md
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,9 +248,9 @@ added: REPLACEME
248248
-->
249249

250250
* `dbName` {string} Name of the database. This can be `'main'` (the default primary database) or any other
251-
database that have been added with [`ATTACH DATABASE`][] **Default:** `'main'`.
252-
* Returns: {string} The location of the database file. When using an `in-memory` database,
253-
this method returns an empty string.
251+
database that has been added with [`ATTACH DATABASE`][] **Default:** `'main'`.
252+
* Returns: {string | null} The location of the database file. When using an in-memory database,
253+
this method returns null.
254254

255255
This method is a wrapper around [`sqlite3_db_filename()`][]
256256

‎src/node_sqlite.cc

Copy file name to clipboardExpand all lines: src/node_sqlite.cc
+15-8Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,23 +1181,29 @@ void DatabaseSync::Location(const FunctionCallbackInfo<Value>& args) {
11811181
Environment* env = Environment::GetCurrent(args);
11821182
THROW_AND_RETURN_ON_BAD_STATE(env, !db->IsOpen(), "database is not open");
11831183

1184-
std::string db_name = "main";
1184+
std::string_view db_name = "main";
11851185
if (!args[0]->IsUndefined()) {
11861186
if (!args[0]->IsString()) {
11871187
THROW_ERR_INVALID_ARG_TYPE(env->isolate(),
11881188
"The \"dbName\" argument must be a string.");
11891189
return;
11901190
}
11911191

1192-
db_name = Utf8Value(env->isolate(), args[0].As<String>()).ToString();
1192+
Utf8Value db_name_utf8(env->isolate(), args[0].As<String>());
1193+
db_name = *db_name_utf8 ? *db_name_utf8 : "";
11931194
}
11941195

11951196
const char* db_filename =
1196-
sqlite3_db_filename(db->connection_, db_name.c_str());
1197+
sqlite3_db_filename(db->connection_, std::string(db_name).c_str());
1198+
if (!db_filename || db_filename[0] == '\0') {
1199+
args.GetReturnValue().Set(Null(env->isolate()));
1200+
return;
1201+
}
11971202

1198-
args.GetReturnValue().Set(
1199-
String::NewFromUtf8(env->isolate(), db_filename, NewStringType::kNormal)
1200-
.ToLocalChecked());
1203+
Local<String> ret;
1204+
if (String::NewFromUtf8(env->isolate(), db_filename).ToLocal(&ret)) {
1205+
args.GetReturnValue().Set(ret);
1206+
}
12011207
}
12021208

12031209
void DatabaseSync::AggregateFunction(const FunctionCallbackInfo<Value>& args) {
@@ -1320,7 +1326,7 @@ void DatabaseSync::AggregateFunction(const FunctionCallbackInfo<Value>& args) {
13201326
return;
13211327
}
13221328

1323-
// Subract 1 because the first argument is the aggregate value.
1329+
// Sutract 1 because the first argument is the aggregate value.
13241330
argc = js_len.As<Int32>()->Value() - 1;
13251331
if (!inverseFunc.IsEmpty() &&
13261332
!inverseFunc->Get(env->context(), env->length_string())
@@ -2632,7 +2638,8 @@ static void Initialize(Local<Object> target,
26322638
SetProtoMethod(isolate, db_tmpl, "prepare", DatabaseSync::Prepare);
26332639
SetProtoMethod(isolate, db_tmpl, "exec", DatabaseSync::Exec);
26342640
SetProtoMethod(isolate, db_tmpl, "function", DatabaseSync::CustomFunction);
2635-
SetProtoMethod(isolate, db_tmpl, "location", DatabaseSync::Location);
2641+
SetProtoMethodNoSideEffect(
2642+
isolate, db_tmpl, "location", DatabaseSync::Location);
26362643
SetProtoMethod(
26372644
isolate, db_tmpl, "aggregate", DatabaseSync::AggregateFunction);
26382645
SetProtoMethod(

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

Copy file name to clipboardExpand all lines: test/parallel/test-sqlite-database-sync.js
+5-4Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ suite('DatabaseSync.prototype.location()', () => {
339339

340340
test('throws if provided dbName is not string', (t) => {
341341
const db = new DatabaseSync(nextDb());
342+
t.after(() => { db.close(); });
342343

343344
t.assert.throws(() => {
344345
db.location(null);
@@ -348,16 +349,16 @@ suite('DatabaseSync.prototype.location()', () => {
348349
});
349350
});
350351

351-
test('returns empty string when connected to in-memory database', (t) => {
352+
test('returns null when connected to in-memory database', (t) => {
352353
const db = new DatabaseSync(':memory:');
353-
t.assert.equal(db.location(), '');
354+
t.assert.strictEqual(db.location(), null);
354355
});
355356

356357
test('returns db path when connected to a persistent database', (t) => {
357358
const dbPath = nextDb();
358359
const db = new DatabaseSync(dbPath);
359360
t.after(() => { db.close(); });
360-
t.assert.equal(db.location(), dbPath);
361+
t.assert.strictEqual(db.location(), dbPath);
361362
});
362363

363364
test('returns that specific db path when attached', (t) => {
@@ -372,6 +373,6 @@ suite('DatabaseSync.prototype.location()', () => {
372373
const escapedPath = otherPath.replace("'", "''");
373374
db.exec(`ATTACH DATABASE '${escapedPath}' AS other`);
374375

375-
t.assert.equal(db.location('other'), otherPath);
376+
t.assert.strictEqual(db.location('other'), otherPath);
376377
});
377378
});

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.