From e5524eaefae48034e4c8cc1a960fd65df4d03161 Mon Sep 17 00:00:00 2001 From: Colin Ihrig Date: Thu, 12 Dec 2024 11:48:12 -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 PR-URL: https://github.com/nodejs/node/pull/55985 Reviewed-By: Zeyu "Alex" Yang Reviewed-By: James M Snell --- 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 33d7f516035d95..4a73b4367c95eb 100644 --- a/doc/api/sqlite.md +++ b/doc/api/sqlite.md @@ -160,6 +160,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()`