Skip to content

Commit b48b546

Browse files
tniessenlouwers
authored andcommitted
sqlite: make sourceSQL and expandedSQL string-valued properties
Change sourceSQL and expandedSQL from being methods to being string-valued properties. These fields - are conceptually properties (and not actions), - are derived deterministically from the current state of the object, - require no parameters, and - are inexpensive to compute. Also, following the naming conventions of ECMAScript for new features, most function names should usually contain a verb, whereas names of (dynamically computed) properties generally should not, so the current names also seem more appropriate for properties than for functions. PR-URL: nodejs#54721 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent dd3a560 commit b48b546

File tree

4 files changed

+47
-19
lines changed

4 files changed

+47
-19
lines changed

doc/api/sqlite.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -262,17 +262,17 @@ objects. If the prepared statement does not return any results, this method
262262
returns an empty array. The prepared statement [parameters are bound][] using
263263
the values in `namedParameters` and `anonymousParameters`.
264264

265-
### `statement.expandedSQL()`
265+
### `statement.expandedSQL`
266266

267267
<!-- YAML
268268
added: v22.5.0
269269
-->
270270

271-
* Returns: {string} The source SQL expanded to include parameter values.
271+
* {string} The source SQL expanded to include parameter values.
272272

273-
This method returns the source SQL of the prepared statement with parameter
273+
The source SQL text of the prepared statement with parameter
274274
placeholders replaced by the values that were used during the most recent
275-
execution of this prepared statement. This method is a wrapper around
275+
execution of this prepared statement. This property is a wrapper around
276276
[`sqlite3_expanded_sql()`][].
277277

278278
### `statement.get([namedParameters][, ...anonymousParameters])`
@@ -361,15 +361,15 @@ be used to read `INTEGER` data using JavaScript `BigInt`s. This method has no
361361
impact on database write operations where numbers and `BigInt`s are both
362362
supported at all times.
363363

364-
### `statement.sourceSQL()`
364+
### `statement.sourceSQL`
365365

366366
<!-- YAML
367367
added: v22.5.0
368368
-->
369369

370-
* Returns: {string} The source SQL used to create this prepared statement.
370+
* {string} The source SQL used to create this prepared statement.
371371

372-
This method returns the source SQL of the prepared statement. This method is a
372+
The source SQL text of the prepared statement. This property is a
373373
wrapper around [`sqlite3_sql()`][].
374374

375375
### Type conversion between JavaScript and SQLite

src/node_sqlite.cc

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,12 @@ using v8::Array;
1818
using v8::ArrayBuffer;
1919
using v8::BigInt;
2020
using v8::Boolean;
21+
using v8::ConstructorBehavior;
2122
using v8::Context;
23+
using v8::DontDelete;
2224
using v8::Exception;
2325
using v8::Function;
26+
using v8::FunctionCallback;
2427
using v8::FunctionCallbackInfo;
2528
using v8::FunctionTemplate;
2629
using v8::Integer;
@@ -33,6 +36,7 @@ using v8::NewStringType;
3336
using v8::Null;
3437
using v8::Number;
3538
using v8::Object;
39+
using v8::SideEffectType;
3640
using v8::String;
3741
using v8::Uint8Array;
3842
using v8::Value;
@@ -825,7 +829,7 @@ void StatementSync::Run(const FunctionCallbackInfo<Value>& args) {
825829
args.GetReturnValue().Set(result);
826830
}
827831

828-
void StatementSync::SourceSQL(const FunctionCallbackInfo<Value>& args) {
832+
void StatementSync::SourceSQLGetter(const FunctionCallbackInfo<Value>& args) {
829833
StatementSync* stmt;
830834
ASSIGN_OR_RETURN_UNWRAP(&stmt, args.This());
831835
Environment* env = Environment::GetCurrent(args);
@@ -839,7 +843,7 @@ void StatementSync::SourceSQL(const FunctionCallbackInfo<Value>& args) {
839843
args.GetReturnValue().Set(sql);
840844
}
841845

842-
void StatementSync::ExpandedSQL(const FunctionCallbackInfo<Value>& args) {
846+
void StatementSync::ExpandedSQLGetter(const FunctionCallbackInfo<Value>& args) {
843847
StatementSync* stmt;
844848
ASSIGN_OR_RETURN_UNWRAP(&stmt, args.This());
845849
Environment* env = Environment::GetCurrent(args);
@@ -899,6 +903,23 @@ void IllegalConstructor(const FunctionCallbackInfo<Value>& args) {
899903
node::THROW_ERR_ILLEGAL_CONSTRUCTOR(Environment::GetCurrent(args));
900904
}
901905

906+
static inline void SetSideEffectFreeGetter(
907+
Isolate* isolate,
908+
Local<FunctionTemplate> class_template,
909+
Local<String> name,
910+
FunctionCallback fn) {
911+
Local<FunctionTemplate> getter =
912+
FunctionTemplate::New(isolate,
913+
fn,
914+
Local<Value>(),
915+
v8::Signature::New(isolate, class_template),
916+
/* length */ 0,
917+
ConstructorBehavior::kThrow,
918+
SideEffectType::kHasNoSideEffect);
919+
class_template->InstanceTemplate()->SetAccessorProperty(
920+
name, getter, Local<FunctionTemplate>(), DontDelete);
921+
}
922+
902923
Local<FunctionTemplate> StatementSync::GetConstructorTemplate(
903924
Environment* env) {
904925
Local<FunctionTemplate> tmpl =
@@ -912,8 +933,14 @@ Local<FunctionTemplate> StatementSync::GetConstructorTemplate(
912933
SetProtoMethod(isolate, tmpl, "all", StatementSync::All);
913934
SetProtoMethod(isolate, tmpl, "get", StatementSync::Get);
914935
SetProtoMethod(isolate, tmpl, "run", StatementSync::Run);
915-
SetProtoMethod(isolate, tmpl, "sourceSQL", StatementSync::SourceSQL);
916-
SetProtoMethod(isolate, tmpl, "expandedSQL", StatementSync::ExpandedSQL);
936+
SetSideEffectFreeGetter(isolate,
937+
tmpl,
938+
FIXED_ONE_BYTE_STRING(isolate, "sourceSQL"),
939+
StatementSync::SourceSQLGetter);
940+
SetSideEffectFreeGetter(isolate,
941+
tmpl,
942+
FIXED_ONE_BYTE_STRING(isolate, "expandedSQL"),
943+
StatementSync::ExpandedSQLGetter);
917944
SetProtoMethod(isolate,
918945
tmpl,
919946
"setAllowBareNamedParameters",

src/node_sqlite.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,9 @@ class StatementSync : public BaseObject {
7171
static void All(const v8::FunctionCallbackInfo<v8::Value>& args);
7272
static void Get(const v8::FunctionCallbackInfo<v8::Value>& args);
7373
static void Run(const v8::FunctionCallbackInfo<v8::Value>& args);
74-
static void SourceSQL(const v8::FunctionCallbackInfo<v8::Value>& args);
75-
static void ExpandedSQL(const v8::FunctionCallbackInfo<v8::Value>& args);
74+
static void SourceSQLGetter(const v8::FunctionCallbackInfo<v8::Value>& args);
75+
static void ExpandedSQLGetter(
76+
const v8::FunctionCallbackInfo<v8::Value>& args);
7677
static void SetAllowBareNamedParameters(
7778
const v8::FunctionCallbackInfo<v8::Value>& args);
7879
static void SetReadBigInts(const v8::FunctionCallbackInfo<v8::Value>& args);

test/parallel/test-sqlite-statement-sync.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ suite('StatementSync.prototype.run()', () => {
135135
});
136136
});
137137

138-
suite('StatementSync.prototype.sourceSQL()', () => {
139-
test('returns input SQL', (t) => {
138+
suite('StatementSync.prototype.sourceSQL', () => {
139+
test('equals input SQL', (t) => {
140140
const db = new DatabaseSync(nextDb());
141141
t.after(() => { db.close(); });
142142
const setup = db.exec(
@@ -145,12 +145,12 @@ suite('StatementSync.prototype.sourceSQL()', () => {
145145
t.assert.strictEqual(setup, undefined);
146146
const sql = 'INSERT INTO types (key, val) VALUES ($k, $v)';
147147
const stmt = db.prepare(sql);
148-
t.assert.strictEqual(stmt.sourceSQL(), sql);
148+
t.assert.strictEqual(stmt.sourceSQL, sql);
149149
});
150150
});
151151

152-
suite('StatementSync.prototype.expandedSQL()', () => {
153-
test('returns expanded SQL', (t) => {
152+
suite('StatementSync.prototype.expandedSQL', () => {
153+
test('equals expanded SQL', (t) => {
154154
const db = new DatabaseSync(nextDb());
155155
t.after(() => { db.close(); });
156156
const setup = db.exec(
@@ -164,7 +164,7 @@ suite('StatementSync.prototype.expandedSQL()', () => {
164164
stmt.run({ $k: '33' }, '42'),
165165
{ changes: 1, lastInsertRowid: 33 },
166166
);
167-
t.assert.strictEqual(stmt.expandedSQL(), expanded);
167+
t.assert.strictEqual(stmt.expandedSQL, expanded);
168168
});
169169
});
170170

0 commit comments

Comments
 (0)