From a2eaaa083f70f202ec357f92cbfe1bc273295a98 Mon Sep 17 00:00:00 2001 From: Renegade334 Date: Mon, 13 Oct 2025 14:59:15 +0100 Subject: [PATCH] sqlite: make `SQLTagStore.prototype.size` a getter Drive-by: make callback `args` parameter names consistent --- doc/api/sqlite.md | 8 +++- src/node_sqlite.cc | 52 +++++++++++------------ src/node_sqlite.h | 17 ++++---- test/parallel/test-sqlite-template-tag.js | 12 +++--- 4 files changed, 46 insertions(+), 43 deletions(-) diff --git a/doc/api/sqlite.md b/doc/api/sqlite.md index bb77b77f5bdb78..db6b834038203e 100644 --- a/doc/api/sqlite.md +++ b/doc/api/sqlite.md @@ -694,13 +694,17 @@ added: v24.9.0 Executes the given SQL query, which is expected to not return any rows (e.g., INSERT, UPDATE, DELETE). -### `sqlTagStore.size()` +### `sqlTagStore.size` -* Returns: {integer} The number of prepared statements currently in the cache. +* Type: {integer} A read-only property that returns the number of prepared statements currently in the cache. diff --git a/src/node_sqlite.cc b/src/node_sqlite.cc index cf9ce6686cffca..54b2d69b32a2d2 100644 --- a/src/node_sqlite.cc +++ b/src/node_sqlite.cc @@ -2659,11 +2659,13 @@ Local SQLTagStore::GetConstructorTemplate(Environment* env) { SetProtoMethod(isolate, tmpl, "iterate", Iterate); SetProtoMethod(isolate, tmpl, "run", Run); SetProtoMethod(isolate, tmpl, "clear", Clear); - SetProtoMethod(isolate, tmpl, "size", Size); - SetSideEffectFreeGetter( - isolate, tmpl, FIXED_ONE_BYTE_STRING(isolate, "capacity"), Capacity); + SetSideEffectFreeGetter(isolate, + tmpl, + FIXED_ONE_BYTE_STRING(isolate, "capacity"), + CapacityGetter); SetSideEffectFreeGetter( isolate, tmpl, FIXED_ONE_BYTE_STRING(isolate, "db"), DatabaseGetter); + SetSideEffectFreeGetter(isolate, tmpl, env->size_string(), SizeGetter); return tmpl; } @@ -2679,32 +2681,44 @@ BaseObjectPtr SQLTagStore::Create( return MakeBaseObject(env, obj, std::move(database), capacity); } +void SQLTagStore::CapacityGetter(const FunctionCallbackInfo& args) { + SQLTagStore* store; + ASSIGN_OR_RETURN_UNWRAP(&store, args.This()); + args.GetReturnValue().Set(static_cast(store->sql_tags_.Capacity())); +} + void SQLTagStore::DatabaseGetter(const FunctionCallbackInfo& args) { SQLTagStore* store; ASSIGN_OR_RETURN_UNWRAP(&store, args.This()); args.GetReturnValue().Set(store->database_->object()); } -void SQLTagStore::Run(const FunctionCallbackInfo& info) { +void SQLTagStore::SizeGetter(const FunctionCallbackInfo& args) { + SQLTagStore* store; + ASSIGN_OR_RETURN_UNWRAP(&store, args.This()); + args.GetReturnValue().Set(static_cast(store->sql_tags_.Size())); +} + +void SQLTagStore::Run(const FunctionCallbackInfo& args) { SQLTagStore* session; - ASSIGN_OR_RETURN_UNWRAP(&session, info.This()); - Environment* env = Environment::GetCurrent(info); + ASSIGN_OR_RETURN_UNWRAP(&session, args.This()); + Environment* env = Environment::GetCurrent(args); THROW_AND_RETURN_ON_BAD_STATE( env, !session->database_->IsOpen(), "database is not open"); - BaseObjectPtr stmt = PrepareStatement(info); + BaseObjectPtr stmt = PrepareStatement(args); if (!stmt) { return; } - uint32_t n_params = info.Length() - 1; + uint32_t n_params = args.Length() - 1; int r = sqlite3_reset(stmt->statement_); CHECK_ERROR_OR_THROW(env->isolate(), stmt->db_.get(), r, SQLITE_OK, void()); int param_count = sqlite3_bind_parameter_count(stmt->statement_); for (int i = 0; i < static_cast(n_params) && i < param_count; ++i) { - Local value = info[i + 1]; + Local value = args[i + 1]; if (!stmt->BindValue(value, i + 1)) { return; } @@ -2714,7 +2728,7 @@ void SQLTagStore::Run(const FunctionCallbackInfo& info) { if (StatementExecutionHelper::Run( env, stmt->db_.get(), stmt->statement_, stmt->use_big_ints_) .ToLocal(&result)) { - info.GetReturnValue().Set(result); + args.GetReturnValue().Set(result); } } @@ -2832,23 +2846,9 @@ void SQLTagStore::All(const FunctionCallbackInfo& args) { } } -void SQLTagStore::Size(const FunctionCallbackInfo& info) { +void SQLTagStore::Clear(const FunctionCallbackInfo& args) { SQLTagStore* store; - ASSIGN_OR_RETURN_UNWRAP(&store, info.This()); - info.GetReturnValue().Set( - Integer::New(info.GetIsolate(), store->sql_tags_.Size())); -} - -void SQLTagStore::Capacity(const FunctionCallbackInfo& info) { - SQLTagStore* store; - ASSIGN_OR_RETURN_UNWRAP(&store, info.This()); - info.GetReturnValue().Set( - Integer::New(info.GetIsolate(), store->sql_tags_.Capacity())); -} - -void SQLTagStore::Clear(const FunctionCallbackInfo& info) { - SQLTagStore* store; - ASSIGN_OR_RETURN_UNWRAP(&store, info.This()); + ASSIGN_OR_RETURN_UNWRAP(&store, args.This()); store->sql_tags_.Clear(); } diff --git a/src/node_sqlite.h b/src/node_sqlite.h index 862c73bf1cdb46..4e3af6400bf3d4 100644 --- a/src/node_sqlite.h +++ b/src/node_sqlite.h @@ -307,15 +307,14 @@ class SQLTagStore : public BaseObject { Environment* env, BaseObjectWeakPtr database, int capacity); static v8::Local GetConstructorTemplate( Environment* env); - static void All(const v8::FunctionCallbackInfo& info); - static void Get(const v8::FunctionCallbackInfo& info); - static void Iterate(const v8::FunctionCallbackInfo& info); - static void Run(const v8::FunctionCallbackInfo& info); - static void Size(const v8::FunctionCallbackInfo& info); - static void Capacity(const v8::FunctionCallbackInfo& info); - static void Reset(const v8::FunctionCallbackInfo& info); - static void Clear(const v8::FunctionCallbackInfo& info); - static void DatabaseGetter(const v8::FunctionCallbackInfo& info); + static void All(const v8::FunctionCallbackInfo& args); + static void Get(const v8::FunctionCallbackInfo& args); + static void Iterate(const v8::FunctionCallbackInfo& args); + static void Run(const v8::FunctionCallbackInfo& args); + static void Clear(const v8::FunctionCallbackInfo& args); + static void CapacityGetter(const v8::FunctionCallbackInfo& args); + static void DatabaseGetter(const v8::FunctionCallbackInfo& args); + static void SizeGetter(const v8::FunctionCallbackInfo& args); void MemoryInfo(MemoryTracker* tracker) const override; SET_MEMORY_INFO_NAME(SQLTagStore) SET_SELF_SIZE(SQLTagStore) diff --git a/test/parallel/test-sqlite-template-tag.js b/test/parallel/test-sqlite-template-tag.js index 8628200cf8930f..4fb8ac506f38f2 100644 --- a/test/parallel/test-sqlite-template-tag.js +++ b/test/parallel/test-sqlite-template-tag.js @@ -77,23 +77,23 @@ test('queries with no results', () => { test('TagStore capacity, size, and clear', () => { assert.strictEqual(sql.capacity, 10); - assert.strictEqual(sql.size(), 0); + assert.strictEqual(sql.size, 0); assert.strictEqual(sql.run`INSERT INTO foo (text) VALUES (${'one'})`.changes, 1); - assert.strictEqual(sql.size(), 1); + assert.strictEqual(sql.size, 1); assert.ok(sql.get`SELECT * FROM foo WHERE text = ${'one'}`); - assert.strictEqual(sql.size(), 2); + assert.strictEqual(sql.size, 2); // Using the same template string shouldn't increase the size assert.strictEqual(sql.get`SELECT * FROM foo WHERE text = ${'two'}`, undefined); - assert.strictEqual(sql.size(), 2); + assert.strictEqual(sql.size, 2); assert.strictEqual(sql.all`SELECT * FROM foo`.length, 1); - assert.strictEqual(sql.size(), 3); + assert.strictEqual(sql.size, 3); sql.clear(); - assert.strictEqual(sql.size(), 0); + assert.strictEqual(sql.size, 0); assert.strictEqual(sql.capacity, 10); });