package fun-sql
All checked builds passInstall
dune-project
Dependency
Authors
Maintainers
Sources
sha256=9403585482c0327877d89a0640f7f8163ae708d2b8031d4d39e067d744bb7528
sha512=cc1871892ce7900c8b88413e0d3f1c692521d098b4ac3dd42dc3018c6321fe3f9bf2a3ef999397974eee75f3441737d5241b958e117fb4461bc69efc2bd8a876
Description
Shared code for SQL packages, use fun-sqlite or fun-postgresql directly depending on which database you are using.
README
fun-sql - functional-style SQL queries for OCaml
Copyright 2022 Yawar Amin
This file is part of fun-sql.
fun-sql is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
fun-sql is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with fun- sql. If not, see https://www.gnu.org/licenses/.
What is this
A simple functional-style query runner and mapper for PostgreSQL and SQLite (MySQL support is also desired if anyone wants to contribute!).
Designed to make it easy to create prepared statements and then use them:
(* Prepared statement: *)
let edit_note = query db "update note set txt = $1 where id = $2"
(* Use by simply calling it: *)
let edit_note id txt = edit_note ~args:Arg.[int id; text txt] unitExamples
open Fun_postgresql
(* Or: Fun_sqlite *)
(* Test DB *)
let db = new Postgresql.connection ~conninfo:"postgresql://..." ()
(* Or: let db = Sqlite3.db_open ":memory:" *)
(* DDL query with no arguments and no return *)
let () = query db "create table people (name text not null, age int)" unit
(* Insert query with single row *)
let () = query
db
(sql "insert into people (name, age) values (%a, %a)" placeholder 0 placeholder 1)
~args:Arg.[text "A"; int 1]
unit
(* Get single column of results from DB *)
let names = query
db
(sql "select name from people where age = %a" placeholder 0)
~args:Arg.[int 1]
@@ ret @@ text 0
(* val names : string Seq.t = <fun> *)
let () =
query
db
(sql "insert into people (name) values (%a)" placeholder 0)
~args:Arg.[text "B"]
unit
(* Get optional values *)
let ages = List.of_seq
@@ query db "select age from people" @@ ret @@ opt int 0
(* val ages : int option list = [Some 1; None] *)
(* Map return data to a custom type on the fly *)
type person = { name : string; age : int option }
let person row = { name = text 0 row; age = opt int 1 row }
(* val person : row -> person *)
(* Assert resultset has a single row and map it *)
let person_1 = only
@@ query db (sql "select name, age from people where age = %a" placeholder 0) ~args:Arg.[int 1]
@@ ret person
(* val person_1 : person = {name = "A"; age = Some 1} *)
(* Assert resultset has either 0 or 1 element *)
let opt_person_1 = optional
@@ query db (sql "select name, age from people where age = %a" placeholder 0) ~args:Arg.[int 2]
@@ ret person
(* val opt_person_1 : person option = None *)
(* Batch insert *)
let ppl = [{ name = "B"; age = None }; { name = "C"; age = Some 3 }]
let () = batch_insert
db
(sql "insert into people (name, age) values (%a, %a)" placeholder 0 placeholder 1)
ppl
(fun { name; age } -> Arg.[text name; opt int age])
unit
Dev Dependencies (1)
-
odoc
with-doc
Optional Dependencies
None
Used by (2)
Conflicts
None
Build status
Results of building fun-sql with each compiler on check.ci.ocaml.org, run of against opam-repository 8a77e58. All results for this package.
Each result links to its build log. To keep crawlers from overloading the site, build logs require a GitHub sign-in (see ocurrent/opam-health-check). On your first log, GitHub asks you to “Authorize opam-health-check”, the OCurrent app that runs these checks; it sees only your public profile, not your repositories. The sign-in lasts 30 days.
| Version | OCaml 4.11 | OCaml 4.14 | OCaml 5.2 | OCaml 5.4 | OCaml 5.5 |
|---|---|---|---|---|---|
| 0.2.3 (this version) | n/a | good | good | good | good |
- good Package successfully built
- partial One of the dependencies failed to build
- bad Package failed to build
- n/a Package is not available in this environment
- internal Internal failure