Skip to content
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

Implement support for simple, aggregate, and window functions #75

Open
plambert opened this issue Jan 4, 2022 · 1 comment
Open

Implement support for simple, aggregate, and window functions #75

plambert opened this issue Jan 4, 2022 · 1 comment

Comments

@plambert
Copy link

plambert commented Jan 4, 2022

The create_function() support exists, and was added in order to implement the REGEX() callback function. It would be very useful to have a documented Crystal interface to sqlite3_create_function_v2() that could be used similar to this:

@db.create_function returning: Int32.class do |param1, param2|
  # ... do something, returning an Int32
end

Syntax to support returning an array, handling aggregate functions, and handling window functions would be (hopefully) similar.

In addition, for my uses, it doesn't need to be exposed through DB; this is obviously for use cases that are very specific to SQLite and not portable, so having a different interface than Database::DB would be perfectly acceptable, though obviously a lot of the functionality would have to be duplicated.

@bcardiff
Copy link
Member

bcardiff commented Jan 5, 2022

A way to access SQLite custom functions would be:

db = DB.open(DB_URL)
db.setup_connection do |connection|
  connection = connection.as(SQLite3::Connection)
  # .. use custom method of SQLite3::Connection
  connection.create_fun ...
end

I think the sqlite3_create_function_v2 would require a proc with fixed arguments, similar to REGEXP_FN.

On top of that, for convenience, there could be a macro that translate a proc with crystal arguments to the variadic argc/argv as REGEXP_FN. But you won't be able to have a proc like the one to propose since the param1 & param2 are not typed and there would be no clue on how to translate it to the variadic one.

So, the story can be divided in two. Getting binding to sqlite3_create_function_v2 and some example/spec for it seems reasonable. Having a macro for the translation might require a bit more advance crystal stuff and it can be done initially per project basis until it's battle tested enough I think.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants