diff --git a/flake.nix b/flake.nix index 5fdeb8b..3f7eba5 100644 --- a/flake.nix +++ b/flake.nix @@ -58,6 +58,9 @@ "$CC" -c subext.c "$AR" rcs libsubext.a subext.o + "$CC" -c multext.c + "$AR" rcs libmultext.a multext.o + runHook postBuild ''; @@ -83,6 +86,10 @@ library = "${sqlite-example-exts-static}/lib/libsubext.a"; init = "sqlite3_subext_init"; } + { + library = "${sqlite-example-exts-static}/lib/libmultext.a"; + init = "sqlite3_multext_init"; + } ]; }; in { diff --git a/sqlite-example-exts/multext.c b/sqlite-example-exts/multext.c new file mode 100644 index 0000000..90506ca --- /dev/null +++ b/sqlite-example-exts/multext.c @@ -0,0 +1,38 @@ +#include + +SQLITE_EXTENSION_INIT1 + +static void mult_func( + sqlite3_context *ctx, + int argc, + sqlite3_value **argv +) { + if (argc != 2) { + sqlite3_result_null(ctx); + return; + } + + int a = sqlite3_value_int(argv[0]); + int b = sqlite3_value_int(argv[1]); + + sqlite3_result_int(ctx, a * b); +} + +int sqlite3_multext_init( + sqlite3 *db, + char **pzErrMsg, + const sqlite3_api_routines *pApi +) { + SQLITE_EXTENSION_INIT2(pApi); + + return sqlite3_create_function( + db, + "mymult", + 2, + SQLITE_UTF8 | SQLITE_DETERMINISTIC, + 0, + mult_func, + 0, + 0 + ); +}