Skip to content

Commit 48a8353

Browse files
committed
apply review suggestions
1 parent fa8c2eb commit 48a8353

File tree

3 files changed

+24
-15
lines changed

3 files changed

+24
-15
lines changed

doc/api/sqlite.md

Lines 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

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,23 +1181,30 @@ 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+
return;
1207+
}
12011208
}
12021209

12031210
void DatabaseSync::AggregateFunction(const FunctionCallbackInfo<Value>& args) {
@@ -1320,7 +1327,7 @@ void DatabaseSync::AggregateFunction(const FunctionCallbackInfo<Value>& args) {
13201327
return;
13211328
}
13221329

1323-
// Subract 1 because the first argument is the aggregate value.
1330+
// Sutract 1 because the first argument is the aggregate value.
13241331
argc = js_len.As<Int32>()->Value() - 1;
13251332
if (!inverseFunc.IsEmpty() &&
13261333
!inverseFunc->Get(env->context(), env->length_string())
@@ -2632,7 +2639,8 @@ static void Initialize(Local<Object> target,
26322639
SetProtoMethod(isolate, db_tmpl, "prepare", DatabaseSync::Prepare);
26332640
SetProtoMethod(isolate, db_tmpl, "exec", DatabaseSync::Exec);
26342641
SetProtoMethod(isolate, db_tmpl, "function", DatabaseSync::CustomFunction);
2635-
SetProtoMethod(isolate, db_tmpl, "location", DatabaseSync::Location);
2642+
SetProtoMethodNoSideEffect(
2643+
isolate, db_tmpl, "location", DatabaseSync::Location);
26362644
SetProtoMethod(
26372645
isolate, db_tmpl, "aggregate", DatabaseSync::AggregateFunction);
26382646
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)