-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Not able to use exported c function as custom function in query #310
Comments
If you want to define your own function in C, you should probably compile it together with SQLite as a single wasm file, not as a separate file. You should read both the SQLite C API documentation and the emscripten documentation. Alternatively, you can define your function directly in javascript, and use it from sql.js: // You can also use JavaScript functions inside your SQL code
// Create the js function you need
function add(a, b) {return a+b;}
// Specifies the SQL function's name, the number of it's arguments, and the js function to use
db.create_function("add_js", add);
// Run a query in which the function is used
db.run("INSERT INTO hello VALUES (add_js(7, 3), add_js('Hello ', 'world'));"); // Inserts 10 and 'Hello world' |
Thanks! Can you provide any information on where should I start using this repo ? As I see , the exported_functions is taken from the sqlite url. Will there be performance difference if those udfs are created in c/c++ instead of js ? My requirement would be to store around 5 mb of data in a table and then perform some queries. |
Certainly. But you should run tests to know whether they are significant or not. Please report your findings here !
You can use IndexedDB for that. But note that the storage space is limited
You can perform the heavy operations in a web worker in order not to block the UI thread. This should avoid lags. https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers |
I started work in PR #320 to show how it would be possible to embed non-standard C functions. In short, you would do https://github.com/kripken/sql.js/issues/310#issuecomment-558533849 shows how you can do this with a custom function implemented in JavaScript. I think it would be nice if we can get this information documented (someday). |
I wrote a c function and compiled using the command
emcc lib/alternate_case.c -s WASM=1 -s EXPORTED_FUNCTIONS="['_alternatecase']" -s EXTRA_EXPORTED_RUNTIME_METHODS='["ccall", "cwrap"]' -s ASSERTIONS=1 -o public/alternate_case.js
Here is my C function
Index.html
I took the sql-wasm.js from this repository. alternate_case.js is the compiled file from emcc.
This is the exporting-c-function.js
It is throwing this error in the console
Error: wrong number of arguments to function altcase()
And it accepts only 0 arguments.
Note: if I try from browser console , I am able to use the function and get the output.
How to use this as custom function?
Also how to create custom function in web workers and use it ?
The text was updated successfully, but these errors were encountered: