dbcaml / dbcaml

DBCaml is a database library for OCaml

Home Page:https://dbca.ml

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Petrol like query builder library

emilpriver opened this issue · comments

Petrol is a SQL library we should take inspiration from on this task. The idea is that instead of writing string queries should you be able to use types to make queries.
Petrol have a pretty good interface for this:

open Petrol
open Petrol.Sqlite3

(* define a new schema *)
let schema = StaticSchema.init ()

(* declare a table *)
let example_table, Expr.[name; age] =
    StaticSchema.declare_table schema ~name:"example"
    Schema.[
        field "name" ~ty:Type.text;
        field "age" ~ty:Type.int
    ]
    
    (* create a query *)
let insert_person ~name:n ~age:a db =
    Query.insert ~table:example_table
        ~values:Expr.[
            name := s n;
            age := i a
         ]
    |> Request.make_zero
    |> Petrol.exec db