mirror of
https://github.com/nodejs/node.git
synced 2026-11-26 21:58:26 -05:00
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:
1 parent
7e3503fff1
commit
10eb2b079e
4 files changed
+99
No files matched your search
@@ -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
|
||||
@@ -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
|
||||
|
||||
@@ -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());
|
||||
@@ -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(
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user