Skip to content

Commit 97a8095

Browse files
committed
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.
1 parent 2f0b371 commit 97a8095

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
@@ -189,16 +189,16 @@ objects. If the prepared statement does not return any results, this method
189189
returns an empty array. The prepared statement [parameters are bound][] using
190190
the values in `namedParameters` and `anonymousParameters`.
191191

192-
### `statement.expandedSQL()`
192+
### `statement.expandedSQL`
193193

194194
<!-- YAML
195195
added: v22.5.0
196196
-->
197197

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

200-
This method returns the source SQL of the prepared statement with parameter
201-
placeholders replaced by values. This method is a wrapper around
200+
The source SQL text of the prepared statement with parameter
201+
placeholders replaced by values. This property is a wrapper around
202202
[`sqlite3_expanded_sql()`][].
203203

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

290-
### `statement.sourceSQL()`
290+
### `statement.sourceSQL`
291291

292292
<!-- YAML
293293
added: v22.5.0
294294
-->
295295

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

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

301301
### 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,8 +18,11 @@ 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;
25+
using v8::FunctionCallback;
2326
using v8::FunctionCallbackInfo;
2427
using v8::FunctionTemplate;
2528
using v8::Integer;
@@ -31,6 +34,7 @@ using v8::Name;
3134
using v8::Null;
3235
using v8::Number;
3336
using v8::Object;
37+
using v8::SideEffectType;
3438
using v8::String;
3539
using v8::Uint8Array;
3640
using v8::Value;
@@ -603,7 +607,7 @@ void StatementSync::Run(const FunctionCallbackInfo<Value>& args) {
603607
args.GetReturnValue().Set(result);
604608
}
605609

606-
void StatementSync::SourceSQL(const FunctionCallbackInfo<Value>& args) {
610+
void StatementSync::SourceSQLGetter(const FunctionCallbackInfo<Value>& args) {
607611
StatementSync* stmt;
608612
ASSIGN_OR_RETURN_UNWRAP(&stmt, args.This());
609613
Environment* env = Environment::GetCurrent(args);
@@ -617,7 +621,7 @@ void StatementSync::SourceSQL(const FunctionCallbackInfo<Value>& args) {
617621
args.GetReturnValue().Set(sql);
618622
}
619623

620-
void StatementSync::ExpandedSQL(const FunctionCallbackInfo<Value>& args) {
624+
void StatementSync::ExpandedSQLGetter(const FunctionCallbackInfo<Value>& args) {
621625
StatementSync* stmt;
622626
ASSIGN_OR_RETURN_UNWRAP(&stmt, args.This());
623627
Environment* env = Environment::GetCurrent(args);
@@ -671,6 +675,23 @@ void IllegalConstructor(const FunctionCallbackInfo<Value>& args) {
671675
node::THROW_ERR_ILLEGAL_CONSTRUCTOR(Environment::GetCurrent(args));
672676
}
673677

678+
static inline void SetSideEffectFreeGetter(
679+
Isolate* isolate,
680+
Local<FunctionTemplate> class_template,
681+
Local<String> name,
682+
FunctionCallback fn) {
683+
Local<FunctionTemplate> getter =
684+
FunctionTemplate::New(isolate,
685+
fn,
686+
Local<Value>(),
687+
v8::Signature::New(isolate, class_template),
688+
/* length */ 0,
689+
ConstructorBehavior::kThrow,
690+
SideEffectType::kHasNoSideEffect);
691+
class_template->InstanceTemplate()->SetAccessorProperty(
692+
name, getter, Local<FunctionTemplate>(), DontDelete);
693+
}
694+
674695
Local<FunctionTemplate> StatementSync::GetConstructorTemplate(
675696
Environment* env) {
676697
Local<FunctionTemplate> tmpl =
@@ -684,8 +705,14 @@ Local<FunctionTemplate> StatementSync::GetConstructorTemplate(
684705
SetProtoMethod(isolate, tmpl, "all", StatementSync::All);
685706
SetProtoMethod(isolate, tmpl, "get", StatementSync::Get);
686707
SetProtoMethod(isolate, tmpl, "run", StatementSync::Run);
687-
SetProtoMethod(isolate, tmpl, "sourceSQL", StatementSync::SourceSQL);
688-
SetProtoMethod(isolate, tmpl, "expandedSQL", StatementSync::ExpandedSQL);
708+
SetSideEffectFreeGetter(isolate,
709+
tmpl,
710+
FIXED_ONE_BYTE_STRING(isolate, "sourceSQL"),
711+
StatementSync::SourceSQLGetter);
712+
SetSideEffectFreeGetter(isolate,
713+
tmpl,
714+
FIXED_ONE_BYTE_STRING(isolate, "expandedSQL"),
715+
StatementSync::ExpandedSQLGetter);
689716
SetProtoMethod(isolate,
690717
tmpl,
691718
"setAllowBareNamedParameters",

src/node_sqlite.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,9 @@ class StatementSync : public BaseObject {
6060
static void All(const v8::FunctionCallbackInfo<v8::Value>& args);
6161
static void Get(const v8::FunctionCallbackInfo<v8::Value>& args);
6262
static void Run(const v8::FunctionCallbackInfo<v8::Value>& args);
63-
static void SourceSQL(const v8::FunctionCallbackInfo<v8::Value>& args);
64-
static void ExpandedSQL(const v8::FunctionCallbackInfo<v8::Value>& args);
63+
static void SourceSQLGetter(const v8::FunctionCallbackInfo<v8::Value>& args);
64+
static void ExpandedSQLGetter(
65+
const v8::FunctionCallbackInfo<v8::Value>& args);
6566
static void SetAllowBareNamedParameters(
6667
const v8::FunctionCallbackInfo<v8::Value>& args);
6768
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)