Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.
Sign upSQLite custom functions #1103
Comments
killercup
added
discussion desired
sqlite
labels
Aug 15, 2017
This comment has been minimized.
|
I haven't heard of any plans. But this seems interesting! Are there any sqlite bindings/crates that support this? What is the expected API for this? |
This comment has been minimized.
Boscop
commented
Aug 15, 2017
|
The sqlite crate that diesel uses exposes these functions here: |
This comment has been minimized.
|
Good. These are just the bindings, though. Looking a bit further, I found that the rusqlite crate supports this with a semi-nice API: db.create_scalar_function("halve", 1, true, |ctx| {
let value = try!(ctx.get::<f64>(0));
Ok(value / 2f64)
})This lives as long as the connection, which shouldn't be a problem when using SQLite. |
This comment has been minimized.
Boscop
commented
Aug 16, 2017
|
Nice, there's also What would have to be done to expose this functionality so that it can be used with diesel? |
This comment has been minimized.
|
My first attempt would be to add a Then, I'd try to make the API nice, e.g., create_function(diesel::sqlite::ScalarFunction::new("halve").deterministic(true).args(1).function(|ctx| …))(A builder is much more forward-compatible and easier to use than a method that takes a bunch of parameters IMHO.) After that, it might be nice to reduce the boilerplate. As you noted, we have a sqlite_function!(halve, (value: f64) -> SqliteFnResult<f64> {
Ok(value / 2f64)
});that generates something that allows you to |
This comment has been minimized.
Boscop
commented
Aug 16, 2017
|
Yes that would be great. |
This comment has been minimized.
|
@Boscop, do you want to work on this? Thinking about this some more, if we were to add a method that exposes the raw sqlite connection (i.e., an unsafe pointer), this could all live in an external crate. I'm not sure we really want to do that though. Maybe as part of an extra feature ("for-diesel-extension" or something like that)?
Have you seen/tried SQLite's built-in full text search? https://sqlite.org/fts5.html |
This comment has been minimized.
Boscop
commented
Aug 17, 2017
•
|
I'm aware of FTS but it doesn't really support the kind of fuzzy matching I need and it looks like diesel doesn't support FTS's EDIT: factored out the rest of this comment into a new issue: #1112 |
This comment has been minimized.
|
Good, didn't want you to spend time writing a fuzzy search only to discover
FTS afterwards :)
Yes, we don't support FTS currently. You should be able to add it in an
external crate though.
Boscop <notifications@github.com> schrieb am Do. 17. Aug. 2017 um 08:48:
… I'm aware of FTS but it doesn't really support the kind of fuzzy matching
I need and it looks like diesel doesn't support FTS's MATCH expression or
am I missing something?
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#1103 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AABOXzkl4vlPTlObvnIfwxIDBsdVIb7lks5sY-IngaJpZM4O3JEi>
.
|
This comment has been minimized.
Boscop
commented
Aug 20, 2017
|
Another custom function that I'd like to have in a query is Where would one have to start when adding this feature? |
This comment has been minimized.
|
@Boscop sorry, I thought #1103 (comment) answered that already. Yes, that is exactly how I'd try to do that. But I haven't experimented with this at all, so unless someone else has any ideas, you'll just have to try and see what issues you might run into. Start with a small POC and we can have a look and help you out :) |
maghoff
referenced this issue
Nov 4, 2017
Closed
Transform markdown to plainer text for search indexing? #37
This comment has been minimized.
maghoff
commented
Dec 15, 2017
|
@Boscop Did you end up doing anything with this? @killercup Would you care to mentor me a bit on this issue if I pick it up now? |
This comment has been minimized.
|
@maghoff sure! I've never done anything with libsqlite3 directly, but I'll try to help :) |
This comment has been minimized.
|
To be clear, |
This comment has been minimized.
|
We also need to consider safety here. Unlike other databases, a custom SQL function in SQLite is tied to the connection, not the database itself. This means that we should ideally enforce that custom SQL functions are always declared. Probably the best way we could do this by providing an API where you give us all your custom functions, and we give you a function that has the signature I suspect the best we can do is set this up so people define all their SQL functions in one place, we give them a function that takes |
This comment has been minimized.
Boscop
commented
Dec 19, 2017
•
This comment has been minimized.
|
There's no reason to use r2d2 with SQLite. "Connections" don't have any actual cost associated with them like they would for a database with a server. |
This comment has been minimized.
maghoff
commented
Dec 30, 2017
|
Status update: I have gotten started by translating Work in progress here: https://github.com/diesel-rs/diesel/compare/master...maghoff:sqlite_custom_function?expand=1 |
This comment has been minimized.
|
@maghoff Happy new year! That looks like a good start! It also seems to be quite self-contained, right? Could this work as a separate crate that uses an extension crate to SqliteConnection? |
This comment has been minimized.
maghoff
commented
Jan 12, 2018
|
@killercup Thank you for having a look!
You tell me |
This comment has been minimized.
Ah damn, I missed that one. Okay, I don't think we want to expose that right now. This is looking pretty good. I'd probably not want to land this |
This comment has been minimized.
maghoff
commented
Jan 12, 2018
|
At present, the application-defined functions cannot accept arguments. My plan was to implement support for that, and then push for more discussion. Maybe a WIP PR would be appropriate then? |
This comment has been minimized.
|
Sure! Probably depends on whether you think it makes sense to review as-is or if you expect to change a lot of stuff around anyway. |
This comment has been minimized.
maghoff
commented
Jan 12, 2018
|
Right. I'll definitely do a pass of clean-up/refactoring before making a PR, but there are some things I need a little bit of input on how to design properly. For example, this branch contains my first |
This comment has been minimized.
|
Fixed by #1691 |
Boscop commentedAug 15, 2017
Are there plans to support creating custom functions when using SQLite?
It would allow using Rust functions in sqlite queries, and can be made accessible in diesel through the
sql_functionmacro.E.g. in C you can define custom sqlite functions like this:
(Source)