Skip to content

sqlite: add location method #57860

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions doc/api/sqlite.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,19 @@ Enables or disables the `loadExtension` SQL function, and the `loadExtension()`
method. When `allowExtension` is `false` when constructing, you cannot enable
loading extensions for security reasons.

### `database.location([dbName])`

<!-- YAML
added: REPLACEME
-->

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

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

### `database.exec(sql)`

<!-- YAML
Expand Down Expand Up @@ -846,6 +859,7 @@ resolution handler passed to [`database.applyChangeset()`][]. See also
[`sqlite3_column_table_name()`]: https://www.sqlite.org/c3ref/column_database_name.html
[`sqlite3_create_function_v2()`]: https://www.sqlite.org/c3ref/create_function.html
[`sqlite3_create_window_function()`]: https://www.sqlite.org/c3ref/create_function.html
[`sqlite3_db_filename()`]: https://sqlite.org/c3ref/db_filename.html
[`sqlite3_exec()`]: https://www.sqlite.org/c3ref/exec.html
[`sqlite3_expanded_sql()`]: https://www.sqlite.org/c3ref/expanded_sql.html
[`sqlite3_get_autocommit()`]: https://sqlite.org/c3ref/get_autocommit.html
Expand Down
32 changes: 32 additions & 0 deletions src/node_sqlite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1184,6 +1184,36 @@ void DatabaseSync::CustomFunction(const FunctionCallbackInfo<Value>& args) {
CHECK_ERROR_OR_THROW(env->isolate(), db, r, SQLITE_OK, void());
}

void DatabaseSync::Location(const FunctionCallbackInfo<Value>& args) {
DatabaseSync* db;
ASSIGN_OR_RETURN_UNWRAP(&db, args.This());
Environment* env = Environment::GetCurrent(args);
THROW_AND_RETURN_ON_BAD_STATE(env, !db->IsOpen(), "database is not open");

std::string db_name = "main";
if (!args[0]->IsUndefined()) {
if (!args[0]->IsString()) {
THROW_ERR_INVALID_ARG_TYPE(env->isolate(),
"The \"dbName\" argument must be a string.");
return;
}

db_name = Utf8Value(env->isolate(), args[0].As<String>()).ToString();
}

const char* db_filename =
sqlite3_db_filename(db->connection_, db_name.c_str());
if (!db_filename || db_filename[0] == '\0') {
args.GetReturnValue().Set(Null(env->isolate()));
return;
}

Local<String> ret;
if (String::NewFromUtf8(env->isolate(), db_filename).ToLocal(&ret)) {
args.GetReturnValue().Set(ret);
}
}

void DatabaseSync::AggregateFunction(const FunctionCallbackInfo<Value>& args) {
DatabaseSync* db;
ASSIGN_OR_RETURN_UNWRAP(&db, args.This());
Expand Down Expand Up @@ -2616,6 +2646,8 @@ static void Initialize(Local<Object> target,
SetProtoMethod(isolate, db_tmpl, "prepare", DatabaseSync::Prepare);
SetProtoMethod(isolate, db_tmpl, "exec", DatabaseSync::Exec);
SetProtoMethod(isolate, db_tmpl, "function", DatabaseSync::CustomFunction);
SetProtoMethodNoSideEffect(
isolate, db_tmpl, "location", DatabaseSync::Location);
SetProtoMethod(
isolate, db_tmpl, "aggregate", DatabaseSync::AggregateFunction);
SetProtoMethod(
Expand Down
1 change: 1 addition & 0 deletions src/node_sqlite.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class DatabaseSync : public BaseObject {
static void Close(const v8::FunctionCallbackInfo<v8::Value>& args);
static void Prepare(const v8::FunctionCallbackInfo<v8::Value>& args);
static void Exec(const v8::FunctionCallbackInfo<v8::Value>& args);
static void Location(const v8::FunctionCallbackInfo<v8::Value>& args);
static void CustomFunction(const v8::FunctionCallbackInfo<v8::Value>& args);
static void AggregateFunction(
const v8::FunctionCallbackInfo<v8::Value>& args);
Expand Down
52 changes: 52 additions & 0 deletions test/parallel/test-sqlite-database-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
Loading