Skip to content

Commit 2d437c7

Browse files
tniessentpoisseau
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 c4692b7 commit 2d437c7

4 files changed

Lines changed: 47 additions & 19 deletions

File tree

doc/api/sqlite.md

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

197-
### `statement.expandedSQL()`
197+
### `statement.expandedSQL`
198198

199199
<!-- YAML
200200
added: v22.5.0
201201
-->
202202

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

205-
This method returns the source SQL of the prepared statement with parameter
205+
The source SQL text of the prepared statement with parameter
206206
placeholders replaced by the values that were used during the most recent
207-
execution of this prepared statement. This method is a wrapper around
207+
execution of this prepared statement. This property is a wrapper around
208208
[`sqlite3_expanded_sql()`][].
209209

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

315-
### `statement.sourceSQL()`
315+
### `statement.sourceSQL`
316316

317317
<!-- YAML
318318
added: v22.5.0
319319
-->
320320

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

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

326326
### 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,10 +18,13 @@ 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::External;
2426
using v8::Function;
27+
using v8::FunctionCallback;
2528
using v8::FunctionCallbackInfo;
2629
using v8::FunctionTemplate;
2730
using v8::Integer;
@@ -33,6 +36,7 @@ using v8::Name;
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;
@@ -819,7 +823,7 @@ void StatementSync::Run(const FunctionCallbackInfo<Value>& args) {
819823
args.GetReturnValue().Set(result);
820824
}
821825

822-
void StatementSync::SourceSQL(const FunctionCallbackInfo<Value>& args) {
826+
void StatementSync::SourceSQLGetter(const FunctionCallbackInfo<Value>& args) {
823827
StatementSync* stmt;
824828
ASSIGN_OR_RETURN_UNWRAP(&stmt, args.This());
825829
Environment* env = Environment::GetCurrent(args);
@@ -833,7 +837,7 @@ void StatementSync::SourceSQL(const FunctionCallbackInfo<Value>& args) {
833837
args.GetReturnValue().Set(sql);
834838
}
835839

836-
void StatementSync::ExpandedSQL(const FunctionCallbackInfo<Value>& args) {
840+
void StatementSync::ExpandedSQLGetter(const FunctionCallbackInfo<Value>& args) {
837841
StatementSync* stmt;
838842
ASSIGN_OR_RETURN_UNWRAP(&stmt, args.This());
839843
Environment* env = Environment::GetCurrent(args);
@@ -893,6 +897,23 @@ void IllegalConstructor(const FunctionCallbackInfo<Value>& args) {
893897
node::THROW_ERR_ILLEGAL_CONSTRUCTOR(Environment::GetCurrent(args));
894898
}
895899

900+
static inline void SetSideEffectFreeGetter(
901+
Isolate* isolate,
902+
Local<FunctionTemplate> class_template,
903+
Local<String> name,
904+
FunctionCallback fn) {
905+
Local<FunctionTemplate> getter =
906+
FunctionTemplate::New(isolate,
907+
fn,
908+
Local<Value>(),
909+
v8::Signature::New(isolate, class_template),
910+
/* length */ 0,
911+
ConstructorBehavior::kThrow,
912+
SideEffectType::kHasNoSideEffect);
913+
class_template->InstanceTemplate()->SetAccessorProperty(
914+
name, getter, Local<FunctionTemplate>(), DontDelete);
915+
}
916+
896917
Local<FunctionTemplate> StatementSync::GetConstructorTemplate(
897918
Environment* env) {
898919
Local<FunctionTemplate> tmpl =
@@ -907,8 +928,14 @@ Local<FunctionTemplate> StatementSync::GetConstructorTemplate(
907928
SetProtoMethod(isolate, tmpl, "all", StatementSync::All);
908929
SetProtoMethod(isolate, tmpl, "get", StatementSync::Get);
909930
SetProtoMethod(isolate, tmpl, "run", StatementSync::Run);
910-
SetProtoMethod(isolate, tmpl, "sourceSQL", StatementSync::SourceSQL);
911-
SetProtoMethod(isolate, tmpl, "expandedSQL", StatementSync::ExpandedSQL);
931+
SetSideEffectFreeGetter(isolate,
932+
tmpl,
933+
FIXED_ONE_BYTE_STRING(isolate, "sourceSQL"),
934+
StatementSync::SourceSQLGetter);
935+
SetSideEffectFreeGetter(isolate,
936+
tmpl,
937+
FIXED_ONE_BYTE_STRING(isolate, "expandedSQL"),
938+
StatementSync::ExpandedSQLGetter);
912939
SetProtoMethod(isolate,
913940
tmpl,
914941
"setAllowBareNamedParameters",

src/node_sqlite.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,9 @@ class StatementSync : public BaseObject {
6363
static void Iterate(const v8::FunctionCallbackInfo<v8::Value>& args);
6464
static void Get(const v8::FunctionCallbackInfo<v8::Value>& args);
6565
static void Run(const v8::FunctionCallbackInfo<v8::Value>& args);
66-
static void SourceSQL(const v8::FunctionCallbackInfo<v8::Value>& args);
67-
static void ExpandedSQL(const v8::FunctionCallbackInfo<v8::Value>& args);
66+
static void SourceSQLGetter(const v8::FunctionCallbackInfo<v8::Value>& args);
67+
static void ExpandedSQLGetter(
68+
const v8::FunctionCallbackInfo<v8::Value>& args);
6869
static void SetAllowBareNamedParameters(
6970
const v8::FunctionCallbackInfo<v8::Value>& args);
7071
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
@@ -163,8 +163,8 @@ suite('StatementSync.prototype.run()', () => {
163163
});
164164
});
165165

166-
suite('StatementSync.prototype.sourceSQL()', () => {
167-
test('returns input SQL', (t) => {
166+
suite('StatementSync.prototype.sourceSQL', () => {
167+
test('equals input SQL', (t) => {
168168
const db = new DatabaseSync(nextDb());
169169
t.after(() => { db.close(); });
170170
const setup = db.exec(
@@ -173,12 +173,12 @@ suite('StatementSync.prototype.sourceSQL()', () => {
173173
t.assert.strictEqual(setup, undefined);
174174
const sql = 'INSERT INTO types (key, val) VALUES ($k, $v)';
175175
const stmt = db.prepare(sql);
176-
t.assert.strictEqual(stmt.sourceSQL(), sql);
176+
t.assert.strictEqual(stmt.sourceSQL, sql);
177177
});
178178
});
179179

180-
suite('StatementSync.prototype.expandedSQL()', () => {
181-
test('returns expanded SQL', (t) => {
180+
suite('StatementSync.prototype.expandedSQL', () => {
181+
test('equals expanded SQL', (t) => {
182182
const db = new DatabaseSync(nextDb());
183183
t.after(() => { db.close(); });
184184
const setup = db.exec(
@@ -192,7 +192,7 @@ suite('StatementSync.prototype.expandedSQL()', () => {
192192
stmt.run({ $k: '33' }, '42'),
193193
{ changes: 1, lastInsertRowid: 33 },
194194
);
195-
t.assert.strictEqual(stmt.expandedSQL(), expanded);
195+
t.assert.strictEqual(stmt.expandedSQL, expanded);
196196
});
197197
});
198198

0 commit comments

Comments
 (0)