Skip to content

Commit a2eaaa0

Browse files
committed
sqlite: make SQLTagStore.prototype.size a getter
Drive-by: make callback `args` parameter names consistent
1 parent 5cf3c3e commit a2eaaa0

File tree

4 files changed

+46
-43
lines changed

4 files changed

+46
-43
lines changed

doc/api/sqlite.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -694,13 +694,17 @@ added: v24.9.0
694694

695695
Executes the given SQL query, which is expected to not return any rows (e.g., INSERT, UPDATE, DELETE).
696696

697-
### `sqlTagStore.size()`
697+
### `sqlTagStore.size`
698698

699699
<!-- YAML
700700
added: v24.9.0
701+
changes:
702+
- version: REPLACEME
703+
pr-url: https://github.com/nodejs/node/pull/60246
704+
description: Changed from a method to a getter.
701705
-->
702706

703-
* Returns: {integer} The number of prepared statements currently in the cache.
707+
* Type: {integer}
704708

705709
A read-only property that returns the number of prepared statements currently in the cache.
706710

src/node_sqlite.cc

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2659,11 +2659,13 @@ Local<FunctionTemplate> SQLTagStore::GetConstructorTemplate(Environment* env) {
26592659
SetProtoMethod(isolate, tmpl, "iterate", Iterate);
26602660
SetProtoMethod(isolate, tmpl, "run", Run);
26612661
SetProtoMethod(isolate, tmpl, "clear", Clear);
2662-
SetProtoMethod(isolate, tmpl, "size", Size);
2663-
SetSideEffectFreeGetter(
2664-
isolate, tmpl, FIXED_ONE_BYTE_STRING(isolate, "capacity"), Capacity);
2662+
SetSideEffectFreeGetter(isolate,
2663+
tmpl,
2664+
FIXED_ONE_BYTE_STRING(isolate, "capacity"),
2665+
CapacityGetter);
26652666
SetSideEffectFreeGetter(
26662667
isolate, tmpl, FIXED_ONE_BYTE_STRING(isolate, "db"), DatabaseGetter);
2668+
SetSideEffectFreeGetter(isolate, tmpl, env->size_string(), SizeGetter);
26672669
return tmpl;
26682670
}
26692671

@@ -2679,32 +2681,44 @@ BaseObjectPtr<SQLTagStore> SQLTagStore::Create(
26792681
return MakeBaseObject<SQLTagStore>(env, obj, std::move(database), capacity);
26802682
}
26812683

2684+
void SQLTagStore::CapacityGetter(const FunctionCallbackInfo<Value>& args) {
2685+
SQLTagStore* store;
2686+
ASSIGN_OR_RETURN_UNWRAP(&store, args.This());
2687+
args.GetReturnValue().Set(static_cast<double>(store->sql_tags_.Capacity()));
2688+
}
2689+
26822690
void SQLTagStore::DatabaseGetter(const FunctionCallbackInfo<Value>& args) {
26832691
SQLTagStore* store;
26842692
ASSIGN_OR_RETURN_UNWRAP(&store, args.This());
26852693
args.GetReturnValue().Set(store->database_->object());
26862694
}
26872695

2688-
void SQLTagStore::Run(const FunctionCallbackInfo<Value>& info) {
2696+
void SQLTagStore::SizeGetter(const FunctionCallbackInfo<Value>& args) {
2697+
SQLTagStore* store;
2698+
ASSIGN_OR_RETURN_UNWRAP(&store, args.This());
2699+
args.GetReturnValue().Set(static_cast<double>(store->sql_tags_.Size()));
2700+
}
2701+
2702+
void SQLTagStore::Run(const FunctionCallbackInfo<Value>& args) {
26892703
SQLTagStore* session;
2690-
ASSIGN_OR_RETURN_UNWRAP(&session, info.This());
2691-
Environment* env = Environment::GetCurrent(info);
2704+
ASSIGN_OR_RETURN_UNWRAP(&session, args.This());
2705+
Environment* env = Environment::GetCurrent(args);
26922706

26932707
THROW_AND_RETURN_ON_BAD_STATE(
26942708
env, !session->database_->IsOpen(), "database is not open");
26952709

2696-
BaseObjectPtr<StatementSync> stmt = PrepareStatement(info);
2710+
BaseObjectPtr<StatementSync> stmt = PrepareStatement(args);
26972711

26982712
if (!stmt) {
26992713
return;
27002714
}
27012715

2702-
uint32_t n_params = info.Length() - 1;
2716+
uint32_t n_params = args.Length() - 1;
27032717
int r = sqlite3_reset(stmt->statement_);
27042718
CHECK_ERROR_OR_THROW(env->isolate(), stmt->db_.get(), r, SQLITE_OK, void());
27052719
int param_count = sqlite3_bind_parameter_count(stmt->statement_);
27062720
for (int i = 0; i < static_cast<int>(n_params) && i < param_count; ++i) {
2707-
Local<Value> value = info[i + 1];
2721+
Local<Value> value = args[i + 1];
27082722
if (!stmt->BindValue(value, i + 1)) {
27092723
return;
27102724
}
@@ -2714,7 +2728,7 @@ void SQLTagStore::Run(const FunctionCallbackInfo<Value>& info) {
27142728
if (StatementExecutionHelper::Run(
27152729
env, stmt->db_.get(), stmt->statement_, stmt->use_big_ints_)
27162730
.ToLocal(&result)) {
2717-
info.GetReturnValue().Set(result);
2731+
args.GetReturnValue().Set(result);
27182732
}
27192733
}
27202734

@@ -2832,23 +2846,9 @@ void SQLTagStore::All(const FunctionCallbackInfo<Value>& args) {
28322846
}
28332847
}
28342848

2835-
void SQLTagStore::Size(const FunctionCallbackInfo<Value>& info) {
2849+
void SQLTagStore::Clear(const FunctionCallbackInfo<Value>& args) {
28362850
SQLTagStore* store;
2837-
ASSIGN_OR_RETURN_UNWRAP(&store, info.This());
2838-
info.GetReturnValue().Set(
2839-
Integer::New(info.GetIsolate(), store->sql_tags_.Size()));
2840-
}
2841-
2842-
void SQLTagStore::Capacity(const FunctionCallbackInfo<Value>& info) {
2843-
SQLTagStore* store;
2844-
ASSIGN_OR_RETURN_UNWRAP(&store, info.This());
2845-
info.GetReturnValue().Set(
2846-
Integer::New(info.GetIsolate(), store->sql_tags_.Capacity()));
2847-
}
2848-
2849-
void SQLTagStore::Clear(const FunctionCallbackInfo<Value>& info) {
2850-
SQLTagStore* store;
2851-
ASSIGN_OR_RETURN_UNWRAP(&store, info.This());
2851+
ASSIGN_OR_RETURN_UNWRAP(&store, args.This());
28522852
store->sql_tags_.Clear();
28532853
}
28542854

src/node_sqlite.h

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -307,15 +307,14 @@ class SQLTagStore : public BaseObject {
307307
Environment* env, BaseObjectWeakPtr<DatabaseSync> database, int capacity);
308308
static v8::Local<v8::FunctionTemplate> GetConstructorTemplate(
309309
Environment* env);
310-
static void All(const v8::FunctionCallbackInfo<v8::Value>& info);
311-
static void Get(const v8::FunctionCallbackInfo<v8::Value>& info);
312-
static void Iterate(const v8::FunctionCallbackInfo<v8::Value>& info);
313-
static void Run(const v8::FunctionCallbackInfo<v8::Value>& info);
314-
static void Size(const v8::FunctionCallbackInfo<v8::Value>& info);
315-
static void Capacity(const v8::FunctionCallbackInfo<v8::Value>& info);
316-
static void Reset(const v8::FunctionCallbackInfo<v8::Value>& info);
317-
static void Clear(const v8::FunctionCallbackInfo<v8::Value>& info);
318-
static void DatabaseGetter(const v8::FunctionCallbackInfo<v8::Value>& info);
310+
static void All(const v8::FunctionCallbackInfo<v8::Value>& args);
311+
static void Get(const v8::FunctionCallbackInfo<v8::Value>& args);
312+
static void Iterate(const v8::FunctionCallbackInfo<v8::Value>& args);
313+
static void Run(const v8::FunctionCallbackInfo<v8::Value>& args);
314+
static void Clear(const v8::FunctionCallbackInfo<v8::Value>& args);
315+
static void CapacityGetter(const v8::FunctionCallbackInfo<v8::Value>& args);
316+
static void DatabaseGetter(const v8::FunctionCallbackInfo<v8::Value>& args);
317+
static void SizeGetter(const v8::FunctionCallbackInfo<v8::Value>& args);
319318
void MemoryInfo(MemoryTracker* tracker) const override;
320319
SET_MEMORY_INFO_NAME(SQLTagStore)
321320
SET_SELF_SIZE(SQLTagStore)

test/parallel/test-sqlite-template-tag.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,23 +77,23 @@ test('queries with no results', () => {
7777

7878
test('TagStore capacity, size, and clear', () => {
7979
assert.strictEqual(sql.capacity, 10);
80-
assert.strictEqual(sql.size(), 0);
80+
assert.strictEqual(sql.size, 0);
8181

8282
assert.strictEqual(sql.run`INSERT INTO foo (text) VALUES (${'one'})`.changes, 1);
83-
assert.strictEqual(sql.size(), 1);
83+
assert.strictEqual(sql.size, 1);
8484

8585
assert.ok(sql.get`SELECT * FROM foo WHERE text = ${'one'}`);
86-
assert.strictEqual(sql.size(), 2);
86+
assert.strictEqual(sql.size, 2);
8787

8888
// Using the same template string shouldn't increase the size
8989
assert.strictEqual(sql.get`SELECT * FROM foo WHERE text = ${'two'}`, undefined);
90-
assert.strictEqual(sql.size(), 2);
90+
assert.strictEqual(sql.size, 2);
9191

9292
assert.strictEqual(sql.all`SELECT * FROM foo`.length, 1);
93-
assert.strictEqual(sql.size(), 3);
93+
assert.strictEqual(sql.size, 3);
9494

9595
sql.clear();
96-
assert.strictEqual(sql.size(), 0);
96+
assert.strictEqual(sql.size, 0);
9797
assert.strictEqual(sql.capacity, 10);
9898
});
9999

0 commit comments

Comments
 (0)