Skip to content

Commit b0c9a2c

Browse files
committed
doc: document sqlite parameter binding
Signed-off-by: Guilherme Araújo <arauujogui@gmail.com>
1 parent 3d80990 commit b0c9a2c

1 file changed

Lines changed: 77 additions & 23 deletions

File tree

doc/api/sqlite.md

Lines changed: 77 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,17 @@ more data types than SQLite, only a subset of JavaScript types are supported.
9191
Attempting to write an unsupported data type to SQLite will result in an
9292
exception.
9393

94-
| Storage class | JavaScript to SQLite | SQLite to JavaScript |
95-
| ------------- | -------------------------- | ------------------------------------- |
96-
| `NULL` | {null} | {null} |
97-
| `INTEGER` | {number} or {bigint} | {number} or {bigint} _(configurable)_ |
98-
| `REAL` | {number} | {number} |
99-
| `TEXT` | {string} | {string} |
100-
| `BLOB` | {TypedArray} or {DataView} | {Uint8Array} |
94+
| Storage class | JavaScript to SQLite | SQLite to JavaScript |
95+
| ------------- | --------------------------------------------------------------- | ------------------------------------- |
96+
| `NULL` | {null} | {null} |
97+
| `INTEGER` | {number}, {bigint}, or {boolean} | {number} or {bigint} _(configurable)_ |
98+
| `REAL` | {number} | {number} |
99+
| `TEXT` | {string} | {string} |
100+
| `BLOB` | {TypedArray}, {DataView}, {ArrayBuffer}, or {SharedArrayBuffer} | {Uint8Array} |
101+
102+
Booleans are written as the `INTEGER` values `1` and `0`, and are read back as
103+
numbers. Writing a {bigint} that does not fit in a signed 64-bit integer throws
104+
an `ERR_INVALID_ARG_VALUE` error.
101105

102106
APIs that read values from SQLite have a configuration option that determines
103107
whether `INTEGER` values are converted to `number` or `bigint` in JavaScript,
@@ -972,6 +976,49 @@ times with different bound values. Parameters also offer protection against
972976
[SQL injection][] attacks. For these reasons, prepared statements are preferred
973977
over hand-crafted SQL strings when handling user input.
974978

979+
### Binding parameters
980+
981+
The `all()`, `get()`, `iterate()`, and `run()` methods bind their arguments to
982+
the parameters of the prepared statement before executing it. Parameters are
983+
either anonymous or named.
984+
985+
Anonymous parameters are written as `?` in SQL and are bound in order from the
986+
arguments passed to the method. The `?NNN` form assigns an explicit number to a
987+
placeholder, binding it to the argument at position `NNN`.
988+
989+
```js
990+
db.prepare('SELECT ? AS a, ? AS b').get('x', 42);
991+
// { a: 'x', b: 42 }
992+
db.prepare('SELECT ?2 AS a, ?1 AS b').get('first', 'second');
993+
// { a: 'second', b: 'first' }
994+
```
995+
996+
Named parameters begin with one of the prefix characters `$`, `:`, or `@` in
997+
SQL. They are bound from an object passed as the first argument. Repeating a
998+
name in the SQL binds the same value to every occurrence.
999+
1000+
```js
1001+
db.prepare('SELECT $a AS a, $b AS b').get({ $a: 1, $b: 2 });
1002+
// { a: 1, b: 2 }
1003+
db.prepare('SELECT :a AS a').get({ ':a': 1 });
1004+
// { a: 1 }
1005+
db.prepare('SELECT @a AS a').get({ '@a': 1 });
1006+
// { a: 1 }
1007+
db.prepare('SELECT $k AS a, $k AS b').get({ k: 7 });
1008+
// { a: 7, b: 7 }
1009+
```
1010+
1011+
The last example omits the prefix character from the object key. Bare names are
1012+
allowed by default; see [`statement.setAllowBareNamedParameters()`][] for their
1013+
caveats.
1014+
1015+
Binding a key that does not name a parameter of the statement throws an
1016+
`ERR_INVALID_STATE` error unless unknown named parameters are ignored. See
1017+
[`statement.setAllowUnknownNamedParameters()`][].
1018+
1019+
See [Type conversion between JavaScript and SQLite][] for the values that can be
1020+
bound. Binding any other value throws an `ERR_INVALID_ARG_TYPE` error.
1021+
9751022
### `statement.all([namedParameters][, ...anonymousParameters])`
9761023

9771024
<!-- YAML
@@ -986,16 +1033,17 @@ changes:
9861033

9871034
* `namedParameters` {Object} An optional object used to bind named parameters.
9881035
The keys of this object are used to configure the mapping.
989-
* `...anonymousParameters` {null|number|bigint|string|Buffer|TypedArray|DataView} Zero or
990-
more values to bind to anonymous parameters.
1036+
* `...anonymousParameters` {null|number|bigint|boolean|string|Buffer|TypedArray|DataView|ArrayBuffer|SharedArrayBuffer}
1037+
Zero or more values to bind to anonymous parameters.
9911038
* Returns: {Array} An array of objects. Each object corresponds to a row
9921039
returned by executing the prepared statement. The keys and values of each
9931040
object correspond to the column names and values of the row.
9941041

9951042
This method executes a prepared statement and returns all results as an array of
9961043
objects. If the prepared statement does not return any results, this method
9971044
returns an empty array. The prepared statement [parameters are bound][] using
998-
the values in `namedParameters` and `anonymousParameters`.
1045+
the values in `namedParameters` and `anonymousParameters`. See
1046+
[Binding parameters][].
9991047

10001048
### `statement.close()`
10011049

@@ -1063,8 +1111,8 @@ changes:
10631111

10641112
* `namedParameters` {Object} An optional object used to bind named parameters.
10651113
The keys of this object are used to configure the mapping.
1066-
* `...anonymousParameters` {null|number|bigint|string|Buffer|TypedArray|DataView} Zero or
1067-
more values to bind to anonymous parameters.
1114+
* `...anonymousParameters` {null|number|bigint|boolean|string|Buffer|TypedArray|DataView|ArrayBuffer|SharedArrayBuffer}
1115+
Zero or more values to bind to anonymous parameters.
10681116
* Returns: {Object|undefined} An object corresponding to the first row returned
10691117
by executing the prepared statement. The keys and values of the object
10701118
correspond to the column names and values of the row. If no rows were returned
@@ -1073,7 +1121,8 @@ changes:
10731121
This method executes a prepared statement and returns the first result as an
10741122
object. If the prepared statement does not return any results, this method
10751123
returns `undefined`. The prepared statement [parameters are bound][] using the
1076-
values in `namedParameters` and `anonymousParameters`.
1124+
values in `namedParameters` and `anonymousParameters`. See
1125+
[Binding parameters][].
10771126

10781127
### `statement.iterate([namedParameters][, ...anonymousParameters])`
10791128

@@ -1091,16 +1140,17 @@ changes:
10911140

10921141
* `namedParameters` {Object} An optional object used to bind named parameters.
10931142
The keys of this object are used to configure the mapping.
1094-
* `...anonymousParameters` {null|number|bigint|string|Buffer|TypedArray|DataView} Zero or
1095-
more values to bind to anonymous parameters.
1143+
* `...anonymousParameters` {null|number|bigint|boolean|string|Buffer|TypedArray|DataView|ArrayBuffer|SharedArrayBuffer}
1144+
Zero or more values to bind to anonymous parameters.
10961145
* Returns: {Iterator} An iterable iterator of objects. Each object corresponds to a row
10971146
returned by executing the prepared statement. The keys and values of each
10981147
object correspond to the column names and values of the row.
10991148

11001149
This method executes a prepared statement and returns an iterator of
11011150
objects. If the prepared statement does not return any results, this method
11021151
returns an empty iterator. The prepared statement [parameters are bound][] using
1103-
the values in `namedParameters` and `anonymousParameters`.
1152+
the values in `namedParameters` and `anonymousParameters`. See
1153+
[Binding parameters][].
11041154

11051155
### `statement.run([namedParameters][, ...anonymousParameters])`
11061156

@@ -1116,8 +1166,8 @@ changes:
11161166

11171167
* `namedParameters` {Object} An optional object used to bind named parameters.
11181168
The keys of this object are used to configure the mapping.
1119-
* `...anonymousParameters` {null|number|bigint|string|Buffer|TypedArray|DataView} Zero or
1120-
more values to bind to anonymous parameters.
1169+
* `...anonymousParameters` {null|number|bigint|boolean|string|Buffer|TypedArray|DataView|ArrayBuffer|SharedArrayBuffer}
1170+
Zero or more values to bind to anonymous parameters.
11211171
* Returns: {Object}
11221172
* `changes` {number|bigint} The number of rows modified, inserted, or deleted
11231173
by the most recently completed `INSERT`, `UPDATE`, or `DELETE` statement.
@@ -1131,7 +1181,8 @@ changes:
11311181

11321182
This method executes a prepared statement and returns an object summarizing the
11331183
resulting changes. The prepared statement [parameters are bound][] using the
1134-
values in `namedParameters` and `anonymousParameters`.
1184+
values in `namedParameters` and `anonymousParameters`. See
1185+
[Binding parameters][].
11351186

11361187
### `statement.setAllowBareNamedParameters(enabled)`
11371188

@@ -1247,7 +1298,7 @@ added: v24.9.0
12471298

12481299
* `stringElements` {string\[]} Template literal elements containing the SQL
12491300
query.
1250-
* `...boundParameters` {null|number|bigint|string|Buffer|TypedArray|DataView}
1301+
* `...boundParameters` {null|number|bigint|boolean|string|Buffer|TypedArray|DataView|ArrayBuffer|SharedArrayBuffer}
12511302
Parameter values to be bound to placeholders in the template string.
12521303
* Returns: {Array} An array of objects representing the rows returned by the query.
12531304

@@ -1265,7 +1316,7 @@ added: v24.9.0
12651316

12661317
* `stringElements` {string\[]} Template literal elements containing the SQL
12671318
query.
1268-
* `...boundParameters` {null|number|bigint|string|Buffer|TypedArray|DataView}
1319+
* `...boundParameters` {null|number|bigint|boolean|string|Buffer|TypedArray|DataView|ArrayBuffer|SharedArrayBuffer}
12691320
Parameter values to be bound to placeholders in the template string.
12701321
* Returns: {Object | undefined} An object representing the first row returned by
12711322
the query, or `undefined` if no rows are returned.
@@ -1283,7 +1334,7 @@ added: v24.9.0
12831334

12841335
* `stringElements` {string\[]} Template literal elements containing the SQL
12851336
query.
1286-
* `...boundParameters` {null|number|bigint|string|Buffer|TypedArray|DataView}
1337+
* `...boundParameters` {null|number|bigint|boolean|string|Buffer|TypedArray|DataView|ArrayBuffer|SharedArrayBuffer}
12871338
Parameter values to be bound to placeholders in the template string.
12881339
* Returns: {Iterator} An iterator that yields objects representing the rows returned by the query.
12891340

@@ -1300,7 +1351,7 @@ added: v24.9.0
13001351

13011352
* `stringElements` {string\[]} Template literal elements containing the SQL
13021353
query.
1303-
* `...boundParameters` {null|number|bigint|string|Buffer|TypedArray|DataView}
1354+
* `...boundParameters` {null|number|bigint|boolean|string|Buffer|TypedArray|DataView|ArrayBuffer|SharedArrayBuffer}
13041355
Parameter values to be bound to placeholders in the template string.
13051356
* Returns: {Object} An object containing information about the execution, including `changes` and `lastInsertRowid`.
13061357

@@ -1666,6 +1717,7 @@ callback function to indicate what type of operation is being authorized.
16661717
</tr>
16671718
</table>
16681719

1720+
[Binding parameters]: #binding-parameters
16691721
[Changesets and Patchsets]: https://www.sqlite.org/sessionintro.html#changesets_and_patchsets
16701722
[Constants Passed To The Conflict Handler]: https://www.sqlite.org/session/c_changeset_conflict.html
16711723
[Constants Returned From The Conflict Handler]: https://www.sqlite.org/session/c_changeset_abort.html
@@ -1714,6 +1766,8 @@ callback function to indicate what type of operation is being authorized.
17141766
[`sqlite3session_create()`]: https://www.sqlite.org/session/sqlite3session_create.html
17151767
[`sqlite3session_delete()`]: https://www.sqlite.org/session/sqlite3session_delete.html
17161768
[`sqlite3session_patchset()`]: https://www.sqlite.org/session/sqlite3session_patchset.html
1769+
[`statement.setAllowBareNamedParameters()`]: #statementsetallowbarenamedparametersenabled
1770+
[`statement.setAllowUnknownNamedParameters()`]: #statementsetallowunknownnamedparametersenabled
17171771
[busy timeout]: https://sqlite.org/c3ref/busy_timeout.html
17181772
[connection]: https://www.sqlite.org/c3ref/sqlite3.html
17191773
[data types]: https://www.sqlite.org/datatype3.html

0 commit comments

Comments
 (0)