sqlite: add location method

PR-URL: https://github.com/nodejs/node/pull/57860
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
This commit is contained in:
Edy Silva authored and RafaelGSS committed 2025-05-02 17:06:37 -03:00
1 parent 7e3503fff1
commit 10eb2b079e
4 files changed
+99

No files matched your search

@@ -361,3 +361,55 @@ suite('DatabaseSync.prototype.isTransaction', () => {
});
});
});
suite('DatabaseSync.prototype.location()', () => {
test('throws if database is not open', (t) => {
const db = new DatabaseSync(nextDb(), { open: false });
t.assert.throws(() => {
db.location();
}, {
code: 'ERR_INVALID_STATE',
message: /database is not open/,
});
});
test('throws if provided dbName is not string', (t) => {
const db = new DatabaseSync(nextDb());
t.after(() => { db.close(); });
t.assert.throws(() => {
db.location(null);
}, {
code: 'ERR_INVALID_ARG_TYPE',
message: /The "dbName" argument must be a string/,
});
});
test('returns null when connected to in-memory database', (t) => {
const db = new DatabaseSync(':memory:');
t.assert.strictEqual(db.location(), null);
});
test('returns db path when connected to a persistent database', (t) => {
const dbPath = nextDb();
const db = new DatabaseSync(dbPath);
t.after(() => { db.close(); });
t.assert.strictEqual(db.location(), dbPath);
});
test('returns that specific db path when attached', (t) => {
const dbPath = nextDb();
const otherPath = nextDb();
const db = new DatabaseSync(dbPath);
t.after(() => { db.close(); });
const other = new DatabaseSync(dbPath);
t.after(() => { other.close(); });
// Adding this escape because the test with unusual chars have a single quote which breaks the query
const escapedPath = otherPath.replace("'", "''");
db.exec(`ATTACH DATABASE '${escapedPath}' AS other`);
t.assert.strictEqual(db.location('other'), otherPath);
});
});