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

Support user functions in Sqlite3 driver #56

Closed
bcc32 opened this issue Jan 22, 2021 · 3 comments
Closed

Support user functions in Sqlite3 driver #56

bcc32 opened this issue Jan 22, 2021 · 3 comments

Comments

@bcc32
Copy link
Contributor

bcc32 commented Jan 22, 2021

Sqlite supports a notion of "application-defined SQL functions", namely, functions that can be used in SQL expressions but which are implemented by the user of the database driver (in this case, in OCaml). sqlite3-ocaml supports the necessary bindings already.

I understand that this probably wouldn't apply to the other database drivers, and it seems like quite a substantial task to make it work with the rest of the API, but I thought it might be reasonable to open this as a tracking issue. Please close if you disagree.

@paurkedal
Copy link
Owner

I think a way to support this and some other RDBMS specific features would be to make the underlying connection object available, e.g. using an extensible variant which can be accessed from a constructor defined by the driver module. Code which uses this would need to link against the driver (instead of relying on dynamic loading), but that's probably acceptable in this case.

@paurkedal
Copy link
Owner

Sorry for the long wait. The solution isn't ideal, but commit a04466f provides access to the connection handle for sqlite3. It requires linking against the caqti-driver-sqlite3 library. The test shows how it can be used:

let test_fun (module C : Caqti_blocking.CONNECTION) =
  (match C.driver_connection with
   | Some Caqti_driver_sqlite3.Driver_connection db ->
      Sqlite3.create_fun1 db "trim" begin function
       | TEXT s -> TEXT (String.trim s)
       | x -> x
      end;
      assert (C.find trim_req "  . " = Ok ".")
   | Some _ -> assert false
   | None -> assert false)

@bcc32
Copy link
Contributor Author

bcc32 commented Oct 27, 2022

Thanks so much! I finally got around to working on the project that needed this and it worked perfectly. I like the new infix Caqti_request interface too; it's very intuitive :)

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