@@ -95,13 +95,19 @@ more data types than SQLite, only a subset of JavaScript types are supported. |
95 | 95 | Attempting to write an unsupported data type to SQLite will result in an |
96 | 96 | exception. |
97 | 97 |
|
98 | | -| Storage class | JavaScript to SQLite | SQLite to JavaScript | |
99 | | -| ------------- | -------------------------- | ------------------------------------- | |
100 | | -| `NULL` | {null} | {null} | |
101 | | -| `INTEGER` | {number} or {bigint} | {number} or {bigint} _(configurable)_ | |
102 | | -| `REAL` | {number} | {number} | |
103 | | -| `TEXT` | {string} | {string} | |
104 | | -| `BLOB` | {TypedArray} or {DataView} | {Uint8Array} | |
| 98 | +| Storage class | JavaScript to SQLite | SQLite to JavaScript | |
| 99 | +| ------------- | --------------------------------------------------------------- | ------------------------------------- | |
| 100 | +| `NULL` | {null} | {null} | |
| 101 | +| `INTEGER` | {number}, {bigint}, or {boolean} | {number} or {bigint} _(configurable)_ | |
| 102 | +| `REAL` | {number} | {number} | |
| 103 | +| `TEXT` | {string} | {string} | |
| 104 | +| `BLOB` | {TypedArray}, {DataView}, {ArrayBuffer}, or {SharedArrayBuffer} | {Uint8Array} | |
| 105 | + |
| 106 | +Booleans are written as the `INTEGER` values `1` and `0`. Like any other |
| 107 | +`INTEGER` value, they are read back as {number} by default, or as {bigint} |
| 108 | +values (`1n` and `0n`) when reading BigInts is enabled. Writing a {bigint} that |
| 109 | +does not fit in a signed 64-bit integer throws an `ERR_INVALID_ARG_VALUE` |
| 110 | +error. |
105 | 111 |
|
106 | 112 | APIs that read values from SQLite have a configuration option that determines |
107 | 113 | whether `INTEGER` values are converted to `number` or `bigint` in JavaScript, |
@@ -824,6 +830,7 @@ added: |
824 | 830 | --> |
825 | 831 |
|
826 | 832 | * `changeset` {Uint8Array} A binary changeset or patchset. |
| 833 | + |
827 | 834 | * `options` {Object} The configuration options for how the changes will be applied. |
828 | 835 | * `filter` {Function} for each table affected by at least |
829 | 836 | one change in the changeset, the `filter` callback is invoked with the |
@@ -852,6 +859,7 @@ added: |
852 | 859 | applying the changeset is aborted and the database is rolled back. |
853 | 860 |
|
854 | 861 | **Default**: A function that returns `SQLITE_CHANGESET_ABORT`. |
| 862 | + |
855 | 863 | * Returns: {boolean} Whether the changeset was applied successfully without being aborted. |
856 | 864 |
|
857 | 865 | An exception is thrown if the database is not |
@@ -977,11 +985,61 @@ times with different bound values. Parameters also offer protection against |
977 | 985 | [SQL injection][] attacks. For these reasons, prepared statements are preferred |
978 | 986 | over hand-crafted SQL strings when handling user input. |
979 | 987 |
|
| 988 | +### Binding parameters |
| 989 | + |
| 990 | +The `all()`, `get()`, `iterate()`, and `run()` methods bind their arguments to |
| 991 | +the parameters of the prepared statement before executing it. Parameters are |
| 992 | +either anonymous or named. |
| 993 | + |
| 994 | +Anonymous parameters are written as `?` in SQL and are bound in order from the |
| 995 | +arguments passed to the method. The `?NNN` form assigns SQLite parameter index |
| 996 | +`NNN` to a placeholder. Avoid mixing numbered and named parameters because they |
| 997 | +share parameter indexes. |
| 998 | + |
| 999 | +```js |
| 1000 | +db.prepare('SELECT ? AS a, ? AS b').get('x', 42); |
| 1001 | +// { a: 'x', b: 42 } |
| 1002 | +db.prepare('SELECT ?2 AS a, ?1 AS b').get('first', 'second'); |
| 1003 | +// { a: 'second', b: 'first' } |
| 1004 | +``` |
| 1005 | + |
| 1006 | +Named parameters begin with one of the prefix characters `$`, `:`, or `@` in |
| 1007 | +SQL. They are bound from an object passed as the first argument. Repeating a |
| 1008 | +name in the SQL binds the same value to every occurrence. |
| 1009 | + |
| 1010 | +```js |
| 1011 | +db.prepare('SELECT $a AS a, $b AS b').get({ $a: 1, $b: 2 }); |
| 1012 | +// { a: 1, b: 2 } |
| 1013 | +db.prepare('SELECT :a AS a').get({ ':a': 1 }); |
| 1014 | +// { a: 1 } |
| 1015 | +db.prepare('SELECT @a AS a').get({ '@a': 1 }); |
| 1016 | +// { a: 1 } |
| 1017 | +db.prepare('SELECT $k AS a, $k AS b').get({ k: 7 }); |
| 1018 | +// { a: 7, b: 7 } |
| 1019 | +``` |
| 1020 | + |
| 1021 | +The last example omits the prefix character from the object key. Bare names are |
| 1022 | +allowed by default; see [`statement.setAllowBareNamedParameters()`][] for their |
| 1023 | +caveats. |
| 1024 | + |
| 1025 | +Binding a key that does not name a parameter of the statement throws an |
| 1026 | +`ERR_INVALID_STATE` error unless unknown named parameters are ignored. See |
| 1027 | +[`statement.setAllowUnknownNamedParameters()`][]. |
| 1028 | + |
| 1029 | +See [Type conversion between JavaScript and SQLite][] for the values that can be |
| 1030 | +bound. Binding any other value throws an `ERR_INVALID_ARG_TYPE` error. |
| 1031 | + |
980 | 1032 | ### `statement.all([namedParameters][, ...anonymousParameters])` |
981 | 1033 |
|
982 | 1034 | <!-- YAML |
983 | 1035 | added: v22.5.0 |
984 | 1036 | changes: |
| 1037 | + - version: REPLACEME |
| 1038 | + pr-url: https://github.com/nodejs/node/pull/62001 |
| 1039 | + description: Add support for boolean values in bound parameters. |
| 1040 | + - version: REPLACEME |
| 1041 | + pr-url: https://github.com/nodejs/node/pull/62061 |
| 1042 | + description: Add support for `ArrayBuffer` and `SharedArrayBuffer` objects in bound parameters. |
985 | 1043 | - version: |
986 | 1044 | - v23.7.0 |
987 | 1045 | - v22.14.0 |
@@ -991,16 +1049,17 @@ changes: |
991 | 1049 |
|
992 | 1050 | * `namedParameters` {Object} An optional object used to bind named parameters. |
993 | 1051 | The keys of this object are used to configure the mapping. |
994 | | -* `...anonymousParameters` {null|number|bigint|string|Buffer|TypedArray|DataView} Zero or |
995 | | - more values to bind to anonymous parameters. |
| 1052 | +* `...anonymousParameters` {null|number|bigint|boolean|string|Buffer|TypedArray|DataView|ArrayBuffer|SharedArrayBuffer} |
| 1053 | + Zero or more values to bind to anonymous parameters. |
996 | 1054 | * Returns: {Array} An array of objects. Each object corresponds to a row |
997 | 1055 | returned by executing the prepared statement. The keys and values of each |
998 | 1056 | object correspond to the column names and values of the row. |
999 | 1057 |
|
1000 | 1058 | This method executes a prepared statement and returns all results as an array of |
1001 | 1059 | objects. If the prepared statement does not return any results, this method |
1002 | 1060 | returns an empty array. The prepared statement [parameters are bound][] using |
1003 | | -the values in `namedParameters` and `anonymousParameters`. |
| 1061 | +the values in `namedParameters` and `anonymousParameters`. See |
| 1062 | +[Binding parameters][]. |
1004 | 1063 |
|
1005 | 1064 | ### `statement.close()` |
1006 | 1065 |
|
@@ -1021,7 +1080,6 @@ added: |
1021 | 1080 |
|
1022 | 1081 | * Returns: {Array} An array of objects. Each object corresponds to a column |
1023 | 1082 | in the prepared statement, and contains the following properties: |
1024 | | - |
1025 | 1083 | * `column` {string|null} The unaliased name of the column in the origin |
1026 | 1084 | table, or `null` if the column is the result of an expression or subquery. |
1027 | 1085 | This property is the result of [`sqlite3_column_origin_name()`][]. |
@@ -1059,6 +1117,12 @@ execution of this prepared statement. This property is a wrapper around |
1059 | 1117 | <!-- YAML |
1060 | 1118 | added: v22.5.0 |
1061 | 1119 | changes: |
| 1120 | + - version: REPLACEME |
| 1121 | + pr-url: https://github.com/nodejs/node/pull/62001 |
| 1122 | + description: Add support for boolean values in bound parameters. |
| 1123 | + - version: REPLACEME |
| 1124 | + pr-url: https://github.com/nodejs/node/pull/62061 |
| 1125 | + description: Add support for `ArrayBuffer` and `SharedArrayBuffer` objects in bound parameters. |
1062 | 1126 | - version: |
1063 | 1127 | - v23.7.0 |
1064 | 1128 | - v22.14.0 |
@@ -1068,8 +1132,8 @@ changes: |
1068 | 1132 |
|
1069 | 1133 | * `namedParameters` {Object} An optional object used to bind named parameters. |
1070 | 1134 | The keys of this object are used to configure the mapping. |
1071 | | -* `...anonymousParameters` {null|number|bigint|string|Buffer|TypedArray|DataView} Zero or |
1072 | | - more values to bind to anonymous parameters. |
| 1135 | +* `...anonymousParameters` {null|number|bigint|boolean|string|Buffer|TypedArray|DataView|ArrayBuffer|SharedArrayBuffer} |
| 1136 | + Zero or more values to bind to anonymous parameters. |
1073 | 1137 | * Returns: {Object|undefined} An object corresponding to the first row returned |
1074 | 1138 | by executing the prepared statement. The keys and values of the object |
1075 | 1139 | correspond to the column names and values of the row. If no rows were returned |
@@ -1078,7 +1142,8 @@ changes: |
1078 | 1142 | This method executes a prepared statement and returns the first result as an |
1079 | 1143 | object. If the prepared statement does not return any results, this method |
1080 | 1144 | returns `undefined`. The prepared statement [parameters are bound][] using the |
1081 | | -values in `namedParameters` and `anonymousParameters`. |
| 1145 | +values in `namedParameters` and `anonymousParameters`. See |
| 1146 | +[Binding parameters][]. |
1082 | 1147 |
|
1083 | 1148 | ### `statement.iterate([namedParameters][, ...anonymousParameters])` |
1084 | 1149 |
|
@@ -1087,6 +1152,12 @@ added: |
1087 | 1152 | - v23.4.0 |
1088 | 1153 | - v22.13.0 |
1089 | 1154 | changes: |
| 1155 | + - version: REPLACEME |
| 1156 | + pr-url: https://github.com/nodejs/node/pull/62001 |
| 1157 | + description: Add support for boolean values in bound parameters. |
| 1158 | + - version: REPLACEME |
| 1159 | + pr-url: https://github.com/nodejs/node/pull/62061 |
| 1160 | + description: Add support for `ArrayBuffer` and `SharedArrayBuffer` objects in bound parameters. |
1090 | 1161 | - version: |
1091 | 1162 | - v23.7.0 |
1092 | 1163 | - v22.14.0 |
@@ -1096,22 +1167,29 @@ changes: |
1096 | 1167 |
|
1097 | 1168 | * `namedParameters` {Object} An optional object used to bind named parameters. |
1098 | 1169 | The keys of this object are used to configure the mapping. |
1099 | | -* `...anonymousParameters` {null|number|bigint|string|Buffer|TypedArray|DataView} Zero or |
1100 | | - more values to bind to anonymous parameters. |
| 1170 | +* `...anonymousParameters` {null|number|bigint|boolean|string|Buffer|TypedArray|DataView|ArrayBuffer|SharedArrayBuffer} |
| 1171 | + Zero or more values to bind to anonymous parameters. |
1101 | 1172 | * Returns: {Iterator} An iterable iterator of objects. Each object corresponds to a row |
1102 | 1173 | returned by executing the prepared statement. The keys and values of each |
1103 | 1174 | object correspond to the column names and values of the row. |
1104 | 1175 |
|
1105 | 1176 | This method executes a prepared statement and returns an iterator of |
1106 | 1177 | objects. If the prepared statement does not return any results, this method |
1107 | 1178 | returns an empty iterator. The prepared statement [parameters are bound][] using |
1108 | | -the values in `namedParameters` and `anonymousParameters`. |
| 1179 | +the values in `namedParameters` and `anonymousParameters`. See |
| 1180 | +[Binding parameters][]. |
1109 | 1181 |
|
1110 | 1182 | ### `statement.run([namedParameters][, ...anonymousParameters])` |
1111 | 1183 |
|
1112 | 1184 | <!-- YAML |
1113 | 1185 | added: v22.5.0 |
1114 | 1186 | changes: |
| 1187 | + - version: REPLACEME |
| 1188 | + pr-url: https://github.com/nodejs/node/pull/62001 |
| 1189 | + description: Add support for boolean values in bound parameters. |
| 1190 | + - version: REPLACEME |
| 1191 | + pr-url: https://github.com/nodejs/node/pull/62061 |
| 1192 | + description: Add support for `ArrayBuffer` and `SharedArrayBuffer` objects in bound parameters. |
1115 | 1193 | - version: |
1116 | 1194 | - v23.7.0 |
1117 | 1195 | - v22.14.0 |
@@ -1121,8 +1199,8 @@ changes: |
1121 | 1199 |
|
1122 | 1200 | * `namedParameters` {Object} An optional object used to bind named parameters. |
1123 | 1201 | The keys of this object are used to configure the mapping. |
1124 | | -* `...anonymousParameters` {null|number|bigint|string|Buffer|TypedArray|DataView} Zero or |
1125 | | - more values to bind to anonymous parameters. |
| 1202 | +* `...anonymousParameters` {null|number|bigint|boolean|string|Buffer|TypedArray|DataView|ArrayBuffer|SharedArrayBuffer} |
| 1203 | + Zero or more values to bind to anonymous parameters. |
1126 | 1204 | * Returns: {Object} |
1127 | 1205 | * `changes` {number|bigint} The number of rows modified, inserted, or deleted |
1128 | 1206 | by the most recently completed `INSERT`, `UPDATE`, or `DELETE` statement. |
@@ -1136,7 +1214,8 @@ changes: |
1136 | 1214 |
|
1137 | 1215 | This method executes a prepared statement and returns an object summarizing the |
1138 | 1216 | resulting changes. The prepared statement [parameters are bound][] using the |
1139 | | -values in `namedParameters` and `anonymousParameters`. |
| 1217 | +values in `namedParameters` and `anonymousParameters`. See |
| 1218 | +[Binding parameters][]. |
1140 | 1219 |
|
1141 | 1220 | ### `statement.setAllowBareNamedParameters(enabled)` |
1142 | 1221 |
|
@@ -1147,14 +1226,15 @@ added: v22.5.0 |
1147 | 1226 | * `enabled` {boolean} Enables or disables support for binding named parameters |
1148 | 1227 | without the prefix character. |
1149 | 1228 |
|
1150 | | -The names of SQLite parameters begin with a prefix character. By default, |
1151 | | -`node:sqlite` allows binding named parameters without this prefix character in |
1152 | | -the parameter object. With the exception of the dollar sign character, these |
1153 | | -prefix characters require extra quoting when used in object keys. |
| 1229 | +The names of SQLite parameters begin with a prefix character. However, with the |
| 1230 | +exception of the dollar sign character, these prefix characters also require |
| 1231 | +extra quoting when used in object keys. |
1154 | 1232 |
|
1155 | | -This method enables or disables support for bare named parameters, which do not |
1156 | | -require the prefix character in JavaScript code. There are several caveats to |
1157 | | -be aware of when bare named parameters are enabled: |
| 1233 | +To improve ergonomics, `node:sqlite` allows bare named parameters, which do not |
| 1234 | +require the prefix character in JavaScript code, by default. This method can be |
| 1235 | +used to disable that behavior, requiring the prefix character when binding. |
| 1236 | +There are several caveats to be aware of when bare named parameters are |
| 1237 | +allowed: |
1158 | 1238 |
|
1159 | 1239 | * The prefix character is still required in SQL. |
1160 | 1240 | * The prefix character is still allowed in JavaScript. In fact, prefixed names |
@@ -1248,11 +1328,18 @@ class execute synchronously. |
1248 | 1328 |
|
1249 | 1329 | <!-- YAML |
1250 | 1330 | added: v24.9.0 |
| 1331 | +changes: |
| 1332 | + - version: REPLACEME |
| 1333 | + pr-url: https://github.com/nodejs/node/pull/62001 |
| 1334 | + description: Add support for boolean values in bound parameters. |
| 1335 | + - version: REPLACEME |
| 1336 | + pr-url: https://github.com/nodejs/node/pull/62061 |
| 1337 | + description: Add support for `ArrayBuffer` and `SharedArrayBuffer` objects in bound parameters. |
1251 | 1338 | --> |
1252 | 1339 |
|
1253 | 1340 | * `stringElements` {string\[]} Template literal elements containing the SQL |
1254 | 1341 | query. |
1255 | | -* `...boundParameters` {null|number|bigint|string|Buffer|TypedArray|DataView} |
| 1342 | +* `...boundParameters` {null|number|bigint|boolean|string|Buffer|TypedArray|DataView|ArrayBuffer|SharedArrayBuffer} |
1256 | 1343 | Parameter values to be bound to placeholders in the template string. |
1257 | 1344 | * Returns: {Array} An array of objects representing the rows returned by the query. |
1258 | 1345 |
|
@@ -1266,11 +1353,18 @@ called directly. |
1266 | 1353 |
|
1267 | 1354 | <!-- YAML |
1268 | 1355 | added: v24.9.0 |
| 1356 | +changes: |
| 1357 | + - version: REPLACEME |
| 1358 | + pr-url: https://github.com/nodejs/node/pull/62001 |
| 1359 | + description: Add support for boolean values in bound parameters. |
| 1360 | + - version: REPLACEME |
| 1361 | + pr-url: https://github.com/nodejs/node/pull/62061 |
| 1362 | + description: Add support for `ArrayBuffer` and `SharedArrayBuffer` objects in bound parameters. |
1269 | 1363 | --> |
1270 | 1364 |
|
1271 | 1365 | * `stringElements` {string\[]} Template literal elements containing the SQL |
1272 | 1366 | query. |
1273 | | -* `...boundParameters` {null|number|bigint|string|Buffer|TypedArray|DataView} |
| 1367 | +* `...boundParameters` {null|number|bigint|boolean|string|Buffer|TypedArray|DataView|ArrayBuffer|SharedArrayBuffer} |
1274 | 1368 | Parameter values to be bound to placeholders in the template string. |
1275 | 1369 | * Returns: {Object | undefined} An object representing the first row returned by |
1276 | 1370 | the query, or `undefined` if no rows are returned. |
@@ -1284,11 +1378,18 @@ called directly. |
1284 | 1378 |
|
1285 | 1379 | <!-- YAML |
1286 | 1380 | added: v24.9.0 |
| 1381 | +changes: |
| 1382 | + - version: REPLACEME |
| 1383 | + pr-url: https://github.com/nodejs/node/pull/62001 |
| 1384 | + description: Add support for boolean values in bound parameters. |
| 1385 | + - version: REPLACEME |
| 1386 | + pr-url: https://github.com/nodejs/node/pull/62061 |
| 1387 | + description: Add support for `ArrayBuffer` and `SharedArrayBuffer` objects in bound parameters. |
1287 | 1388 | --> |
1288 | 1389 |
|
1289 | 1390 | * `stringElements` {string\[]} Template literal elements containing the SQL |
1290 | 1391 | query. |
1291 | | -* `...boundParameters` {null|number|bigint|string|Buffer|TypedArray|DataView} |
| 1392 | +* `...boundParameters` {null|number|bigint|boolean|string|Buffer|TypedArray|DataView|ArrayBuffer|SharedArrayBuffer} |
1292 | 1393 | Parameter values to be bound to placeholders in the template string. |
1293 | 1394 | * Returns: {Iterator} An iterator that yields objects representing the rows returned by the query. |
1294 | 1395 |
|
@@ -1301,11 +1402,18 @@ called directly. |
1301 | 1402 |
|
1302 | 1403 | <!-- YAML |
1303 | 1404 | added: v24.9.0 |
| 1405 | +changes: |
| 1406 | + - version: REPLACEME |
| 1407 | + pr-url: https://github.com/nodejs/node/pull/62001 |
| 1408 | + description: Add support for boolean values in bound parameters. |
| 1409 | + - version: REPLACEME |
| 1410 | + pr-url: https://github.com/nodejs/node/pull/62061 |
| 1411 | + description: Add support for `ArrayBuffer` and `SharedArrayBuffer` objects in bound parameters. |
1304 | 1412 | --> |
1305 | 1413 |
|
1306 | 1414 | * `stringElements` {string\[]} Template literal elements containing the SQL |
1307 | 1415 | query. |
1308 | | -* `...boundParameters` {null|number|bigint|string|Buffer|TypedArray|DataView} |
| 1416 | +* `...boundParameters` {null|number|bigint|boolean|string|Buffer|TypedArray|DataView|ArrayBuffer|SharedArrayBuffer} |
1309 | 1417 | Parameter values to be bound to placeholders in the template string. |
1310 | 1418 | * Returns: {Object} An object containing information about the execution, including `changes` and `lastInsertRowid`. |
1311 | 1419 |
|
@@ -1671,6 +1779,7 @@ callback function to indicate what type of operation is being authorized. |
1671 | 1779 | </tr> |
1672 | 1780 | </table> |
1673 | 1781 |
|
| 1782 | +[Binding parameters]: #binding-parameters |
1674 | 1783 | [Changesets and Patchsets]: https://www.sqlite.org/sessionintro.html#changesets_and_patchsets |
1675 | 1784 | [Constants Passed To The Conflict Handler]: https://www.sqlite.org/session/c_changeset_conflict.html |
1676 | 1785 | [Constants Returned From The Conflict Handler]: https://www.sqlite.org/session/c_changeset_abort.html |
@@ -1719,6 +1828,8 @@ callback function to indicate what type of operation is being authorized. |
1719 | 1828 | [`sqlite3session_create()`]: https://www.sqlite.org/session/sqlite3session_create.html |
1720 | 1829 | [`sqlite3session_delete()`]: https://www.sqlite.org/session/sqlite3session_delete.html |
1721 | 1830 | [`sqlite3session_patchset()`]: https://www.sqlite.org/session/sqlite3session_patchset.html |
| 1831 | +[`statement.setAllowBareNamedParameters()`]: #statementsetallowbarenamedparametersenabled |
| 1832 | +[`statement.setAllowUnknownNamedParameters()`]: #statementsetallowunknownnamedparametersenabled |
1722 | 1833 | [busy timeout]: https://sqlite.org/c3ref/busy_timeout.html |
1723 | 1834 | [connection]: https://www.sqlite.org/c3ref/sqlite3.html |
1724 | 1835 | [data types]: https://www.sqlite.org/datatype3.html |
|