From 99094630031e66db55677a8af392090ed21aa272 Mon Sep 17 00:00:00 2001 From: cjihrig Date: Sun, 24 Nov 2024 09:58:46 -0500 Subject: [PATCH] sqlite: add support for custom functions This commit adds support to node:sqlite for defining custom functions that can be invoked from SQL. Fixes: https://github.com/nodejs/node/issues/54349 --- doc/api/sqlite.md | 28 ++ src/node_sqlite.cc | 265 +++++++++++++ src/node_sqlite.h | 1 + test/parallel/test-sqlite-custom-functions.js | 373 ++++++++++++++++++ 4 files changed, 667 insertions(+) create mode 100644 test/parallel/test-sqlite-custom-functions.js diff --git a/doc/api/sqlite.md b/doc/api/sqlite.md index 4e27d0d8bc5f26..ef18222c997444 100644 --- a/doc/api/sqlite.md +++ b/doc/api/sqlite.md @@ -132,6 +132,31 @@ This method allows one or more SQL statements to be executed without returning any results. This method is useful when executing SQL statements read from a file. This method is a wrapper around [`sqlite3_exec()`][]. +### `database.function(name[, options], function)` + + + +* `name` {string} The name of the SQLite function to create. +* `options` {Object} Optional configuration settings for the function. The + following properties are supported: + * `deterministic` {boolean} If `true`, the [`SQLITE_DETERMINISTIC`][] flag is + set on the created function. **Default:** `false`. + * `directOnly` {boolean} If `true`, the [`SQLITE_DIRECTONLY`][] flag is set on + the created function. **Default:** `false`. + * `useBigIntArguments` {boolean} If `true`, integer arguments to `function` + are converted to `BigInt`s. If `false`, integer arguments are passed as + JavaScript numbers. **Default:** `false`. + * `varargs` {boolean} If `true`, `function` can accept a variable number of + arguments. If `false`, `function` must be invoked with exactly + `function.length` arguments. **Default:** `false`. +* `function` {Function} The JavaScript function to call when the SQLite + function is invoked. + +This method is used to create SQLite user-defined functions. This method is a +wrapper around [`sqlite3_create_function_v2()`][]. + ### `database.open()`