-
-
Notifications
You must be signed in to change notification settings - Fork 818
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
User Defined Functions? #140
Comments
Well, loading extensions is supported now, so I would recommend using that unless you want to code up the support for js land functions yourself. |
@springmeyer That would work if I even knew how to do that. The problem is, I wouldn't even know where to begin. Mostly all I want to be able to do is use REGEXP in sql statements and I thought it would be easy to wire up the regular expression functionality that's built into javascript, for someone who knows what they're doing in the SQLite code. I don't really know what the difference would be between hard coding the regular expressions from JS or creating some generic wrapper for passing JS functions into SQLite as user defined functions. Maybe it would be easier to make the wrapper? It would be cool, I can't do it without a lot of hand holding and pointing though, which would probably just be frustrating for you. There's no way for me to write an extension in JavaScript is there? I mean, I can write programs in JS, PHP, a little Python, MySQL, etc. I don't know C well enough to program in it though. I can read it mostly, but I never write programs in it. I'm currently under the impression that I'd need to know C in order to do anything useful with the SQLite API. |
@matthewkastor - I don't know the answers here, but I understand this would be a useful feature. Perhaps you could reach out to the fellow at #93 or the node mailing list to see if someone could code it up? I'm currently maintaining node-sqlite3 but do not have bandwidth for new features. |
It looks like he is just as lost as I am. I'll post something on the mailing list and maybe someone out there will be inspired to make a pull request. :D |
kk I posted to the mailing list. The subject starts with "Are you an expert coder?". Waiting for moderator approval. |
It is indeed possible to add the ability for custom callbacks, but it's non-trivial to implement because we're executing queries in another thread. We could either create a new v8 isolate and somehow transfer the callback function there (webworker style), or call back to the main thread (extremely slow). I'm not going to work on this, but I'll be happy to review and accept pull requests adding this feature. |
Good to know. Thanks. :D |
@kkaefer this is my first time using libuv. Would a batched queue be implemented using Also, any thoughts why this would fail with iojs on Linux? Your two comments may point to the reason, but I figured I'd ask. |
@wbyoung You'll have to initialize the Instead of |
I was thinking about attempting @kkaefer's suggestion of a separate v8 isolate and transferring the callback there, but realized that this could introduce serious problems if the function were to access anything defined outside of its scope. For instance, the below code looks innocent enough, but access to the required lib means there wouldn't be any way to safely transfer this function to a new isolate without accessing data from the current isolate. var hashlib = require('some-hash-lib');
db.registerFunction('MY_HASH', function(value) {
return hashlib.hex(value);
}); One could, however, create a new function in a separate isolate. This could be done by converting the given function to a string & adding it to the new isolate. This could either always be done or only when an option was given. db.registerFunction('MY_HASH', function(value) {
return require('some-hash-lib').hex(value);
}, { isolate: true });
// alternative syntax: new Function() style gets added to isolate
db.registerFunction('MY_HASH', 'value', "return require('some-hash-lib').hex(value);"); Any thoughts? |
https://github.com/matthewkastor/clone-function
|
@matthewkastor that's essentially what I was proposing, but via native code. I hacked around on the separate v8 isolate a bit and came up with wbyoung/node-sqlite3 user-functions-isolate. One of the largest issues in creating a separate v8 isolate would be that none of node's environment exists by default in this isolate. We'd have to initialize that as well. That seems possible via I'm hoping that one of these two approaches can eventually get merged. :) /cc @kkaefer |
You're pretty smart XD
|
Could you provide an example of defining a user function in javascript? db.registerFunction does not work: |
Just want to add in here since this is a 5+ year old thread that manually testing is surprisingly performant* - I'm using something like this and the latency is acceptable in my application:
|
I'm looking for a way to create user functions. In PHP's binding to SQLite3 there is a way to create functions at runtime. I'm thinking that if there were something similar in node-sqlite3 it would be awesome.
I don't want to try loading an extension because that would require me to keep tabs on versions and / or incorporating a compilation step in my modules. I'd rather just define functions in the javascript and be able to hook them into SQLite for use in queries.
http://www.sqlite.org/lang_expr.html#regexp
http://www.sqlite.org/c3ref/create_function.html
The text was updated successfully, but these errors were encountered: