Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Please note that this is not the first or only PGSQL bindings for OCaml. Here are the others which you may want to consider:
PG'OCAML is different than the above bindings:
PGOCaml
) provided is synchronous. But it also supports any asynchronous interface that implements the PGOCaml_generic.THREAD
signature.PG'OCaml uses environment variables (or in-code parameters, which are [ill advised] (https://hackernoon.com/how-to-use-environment-variables-keep-your-secret-keys-safe-secure-8b1a7877d69c)) to connect to your database both at compile-time and at runtime.
Variable | Default | Additional information |
---|---|---|
| If this starts with a | |
|
| This is also the default PostgreSQL port. |
| The username of the current user, or | |
| falls back on | |
| empty string | |
| no profiling | Indicates the file to write profiling information to. If it doesn't exist, don't profile |
|
| If set to |
The PPX aims to be more or less a carbon copy of the former extension.
let () =
let dbh = PGOCaml.connect () in
let insert name salary =
[%pgsql dbh "insert into employees (name, salary) VALUES ($name, $salary)"]
in
ignore(insert "Chris" 1_000.0);
let get name =
[%pgsql dbh "select salary from employees where name = $name"]
in
let () = [%pgsql dbh
"execute"
"CREATE TEMP TABLE IF NOT EXISTS employees (
name TEXT PRIMARY KEY,
salary FLOAT)"]
in
let name = "Chris" in
let salary = get name
|> List.hd
|> function
| Some(x) -> x
| None -> raise(Failure "The database is probably broken.")
in
Printf.printf "%s's salary is %.02f\n" name salary;
PGOCaml.close(dbh)
The PPX allows you to specify that queries returning results should be returned as objects, rather than tuples.
let%lwt res =
[%pgsql.object dbh "SELECT * FROM employees"]
in
List.iter
(fun row ->
Printf.printf "%s makes $%f\n" row#name row#salary)
res
The PPX now also supports ${...}
expansions.
(* where [e] is a row returned by a [pgsql.object] query *)
let%lwt incr_sal e =
[%pgsql dbh "UPDATE employees SET salary = ${e#salary +. 1.0}"]
PG'OCaml (C) Copyright 2005-2009 Merjis Ltd, Richard W.M. Jones (rich@annexia.org) and other authors (see CONTRIBUTORS.txt for details).
This software is distributed under the GNU LGPL with OCaml linking exception. Please see the file COPYING.LIB for full license.
For an example, please see tests