Skip to content

Commit f46394b

Browse files
committed
apply review suggestions
1 parent fa8c2eb commit f46394b

File tree

3 files changed

+21
-13
lines changed

3 files changed

+21
-13
lines changed

doc/api/sqlite.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,8 @@ added: REPLACEME
249249

250250
* `dbName` {string} Name of the database. This can be `'main'` (the default primary database) or any other
251251
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.
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

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,7 +1181,7 @@ 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(),
@@ -1193,11 +1193,17 @@ void DatabaseSync::Location(const FunctionCallbackInfo<Value>& args) {
11931193
}
11941194

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

1198-
args.GetReturnValue().Set(
1199-
String::NewFromUtf8(env->isolate(), db_filename, NewStringType::kNormal)
1200-
.ToLocalChecked());
1202+
Local<String> ret;
1203+
if (String::NewFromUtf8(env->isolate(), db_filename).ToLocal(&ret)) {
1204+
args.GetReturnValue().Set(ret);
1205+
return;
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+
// Subtract 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

Lines 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)