Skip to content

Commit a1f421f

Browse files
authored
sqlite: make SQLTagStore.prototype.size a getter
Drive-by: make callback `args` parameter names consistent PR-URL: #60246 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Yagiz Nizipli <[email protected]>
1 parent 060deae commit a1f421f

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
@@ -1076,13 +1076,17 @@ Executes the given SQL query, which is expected to not return any rows (e.g., IN
10761076
This function is intended to be used as a template literal tag, not to be
10771077
called directly.
10781078

1079-
### `sqlTagStore.size()`
1079+
### `sqlTagStore.size`
10801080

10811081
<!-- YAML
10821082
added: v24.9.0
1083+
changes:
1084+
- version: REPLACEME
1085+
pr-url: https://github.com/nodejs/node/pull/60246
1086+
description: Changed from a method to a getter.
10831087
-->
10841088

1085-
* Returns: {integer} The number of prepared statements currently in the cache.
1089+
* Type: {integer}
10861090

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

src/node_sqlite.cc

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2700,11 +2700,13 @@ Local<FunctionTemplate> SQLTagStore::GetConstructorTemplate(Environment* env) {
27002700
SetProtoMethod(isolate, tmpl, "iterate", Iterate);
27012701
SetProtoMethod(isolate, tmpl, "run", Run);
27022702
SetProtoMethod(isolate, tmpl, "clear", Clear);
2703-
SetProtoMethod(isolate, tmpl, "size", Size);
2704-
SetSideEffectFreeGetter(
2705-
isolate, tmpl, FIXED_ONE_BYTE_STRING(isolate, "capacity"), Capacity);
2703+
SetSideEffectFreeGetter(isolate,
2704+
tmpl,
2705+
FIXED_ONE_BYTE_STRING(isolate, "capacity"),
2706+
CapacityGetter);
27062707
SetSideEffectFreeGetter(
27072708
isolate, tmpl, FIXED_ONE_BYTE_STRING(isolate, "db"), DatabaseGetter);
2709+
SetSideEffectFreeGetter(isolate, tmpl, env->size_string(), SizeGetter);
27082710
return tmpl;
27092711
}
27102712

@@ -2720,32 +2722,44 @@ BaseObjectPtr<SQLTagStore> SQLTagStore::Create(
27202722
return MakeBaseObject<SQLTagStore>(env, obj, std::move(database), capacity);
27212723
}
27222724

2725+
void SQLTagStore::CapacityGetter(const FunctionCallbackInfo<Value>& args) {
2726+
SQLTagStore* store;
2727+
ASSIGN_OR_RETURN_UNWRAP(&store, args.This());
2728+
args.GetReturnValue().Set(static_cast<double>(store->sql_tags_.Capacity()));
2729+
}
2730+
27232731
void SQLTagStore::DatabaseGetter(const FunctionCallbackInfo<Value>& args) {
27242732
SQLTagStore* store;
27252733
ASSIGN_OR_RETURN_UNWRAP(&store, args.This());
27262734
args.GetReturnValue().Set(store->database_->object());
27272735
}
27282736

2729-
void SQLTagStore::Run(const FunctionCallbackInfo<Value>& info) {
2737+
void SQLTagStore::SizeGetter(const FunctionCallbackInfo<Value>& args) {
2738+
SQLTagStore* store;
2739+
ASSIGN_OR_RETURN_UNWRAP(&store, args.This());
2740+
args.GetReturnValue().Set(static_cast<double>(store->sql_tags_.Size()));
2741+
}
2742+
2743+
void SQLTagStore::Run(const FunctionCallbackInfo<Value>& args) {
27302744
SQLTagStore* session;
2731-
ASSIGN_OR_RETURN_UNWRAP(&session, info.This());
2732-
Environment* env = Environment::GetCurrent(info);
2745+
ASSIGN_OR_RETURN_UNWRAP(&session, args.This());
2746+
Environment* env = Environment::GetCurrent(args);
27332747

27342748
THROW_AND_RETURN_ON_BAD_STATE(
27352749
env, !session->database_->IsOpen(), "database is not open");
27362750

2737-
BaseObjectPtr<StatementSync> stmt = PrepareStatement(info);
2751+
BaseObjectPtr<StatementSync> stmt = PrepareStatement(args);
27382752

27392753
if (!stmt) {
27402754
return;
27412755
}
27422756

2743-
uint32_t n_params = info.Length() - 1;
2757+
uint32_t n_params = args.Length() - 1;
27442758
int r = sqlite3_reset(stmt->statement_);
27452759
CHECK_ERROR_OR_THROW(env->isolate(), stmt->db_.get(), r, SQLITE_OK, void());
27462760
int param_count = sqlite3_bind_parameter_count(stmt->statement_);
27472761
for (int i = 0; i < static_cast<int>(n_params) && i < param_count; ++i) {
2748-
Local<Value> value = info[i + 1];
2762+
Local<Value> value = args[i + 1];
27492763
if (!stmt->BindValue(value, i + 1)) {
27502764
return;
27512765
}
@@ -2755,7 +2769,7 @@ void SQLTagStore::Run(const FunctionCallbackInfo<Value>& info) {
27552769
if (StatementExecutionHelper::Run(
27562770
env, stmt->db_.get(), stmt->statement_, stmt->use_big_ints_)
27572771
.ToLocal(&result)) {
2758-
info.GetReturnValue().Set(result);
2772+
args.GetReturnValue().Set(result);
27592773
}
27602774
}
27612775

@@ -2873,23 +2887,9 @@ void SQLTagStore::All(const FunctionCallbackInfo<Value>& args) {
28732887
}
28742888
}
28752889

2876-
void SQLTagStore::Size(const FunctionCallbackInfo<Value>& info) {
2890+
void SQLTagStore::Clear(const FunctionCallbackInfo<Value>& args) {
28772891
SQLTagStore* store;
2878-
ASSIGN_OR_RETURN_UNWRAP(&store, info.This());
2879-
info.GetReturnValue().Set(
2880-
Integer::New(info.GetIsolate(), store->sql_tags_.Size()));
2881-
}
2882-
2883-
void SQLTagStore::Capacity(const FunctionCallbackInfo<Value>& info) {
2884-
SQLTagStore* store;
2885-
ASSIGN_OR_RETURN_UNWRAP(&store, info.This());
2886-
info.GetReturnValue().Set(
2887-
Integer::New(info.GetIsolate(), store->sql_tags_.Capacity()));
2888-
}
2889-
2890-
void SQLTagStore::Clear(const FunctionCallbackInfo<Value>& info) {
2891-
SQLTagStore* store;
2892-
ASSIGN_OR_RETURN_UNWRAP(&store, info.This());
2892+
ASSIGN_OR_RETURN_UNWRAP(&store, args.This());
28932893
store->sql_tags_.Clear();
28942894
}
28952895

src/node_sqlite.h

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -313,15 +313,14 @@ class SQLTagStore : public BaseObject {
313313
Environment* env, BaseObjectWeakPtr<DatabaseSync> database, int capacity);
314314
static v8::Local<v8::FunctionTemplate> GetConstructorTemplate(
315315
Environment* env);
316-
static void All(const v8::FunctionCallbackInfo<v8::Value>& info);
317-
static void Get(const v8::FunctionCallbackInfo<v8::Value>& info);
318-
static void Iterate(const v8::FunctionCallbackInfo<v8::Value>& info);
319-
static void Run(const v8::FunctionCallbackInfo<v8::Value>& info);
320-
static void Size(const v8::FunctionCallbackInfo<v8::Value>& info);
321-
static void Capacity(const v8::FunctionCallbackInfo<v8::Value>& info);
322-
static void Reset(const v8::FunctionCallbackInfo<v8::Value>& info);
323-
static void Clear(const v8::FunctionCallbackInfo<v8::Value>& info);
324-
static void DatabaseGetter(const v8::FunctionCallbackInfo<v8::Value>& info);
316+
static void All(const v8::FunctionCallbackInfo<v8::Value>& args);
317+
static void Get(const v8::FunctionCallbackInfo<v8::Value>& args);
318+
static void Iterate(const v8::FunctionCallbackInfo<v8::Value>& args);
319+
static void Run(const v8::FunctionCallbackInfo<v8::Value>& args);
320+
static void Clear(const v8::FunctionCallbackInfo<v8::Value>& args);
321+
static void CapacityGetter(const v8::FunctionCallbackInfo<v8::Value>& args);
322+
static void DatabaseGetter(const v8::FunctionCallbackInfo<v8::Value>& args);
323+
static void SizeGetter(const v8::FunctionCallbackInfo<v8::Value>& args);
325324
void MemoryInfo(MemoryTracker* tracker) const override;
326325
SET_MEMORY_INFO_NAME(SQLTagStore)
327326
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)