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 Antoine du Hamel committed 2025-05-19 12:14:08 +02:00
1 parent c12cd2a190
commit 7485309d7e
4 files changed
+99

No files matched your search

+32
View File
@@ -1182,6 +1182,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());
@@ -2617,6 +2647,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(