PG’OCaml is a type-safe macro binding to PostgreSQL from OCaml that I wrote many moons ago.
You can write code like:
let hostid = 33 in let name = "john.smith" in let rows = PGSQL(dbh) "select id, subject from contacts where hostid = $hostid and name = $name"
and the compiler checks (at compile time) that hostid
and name
have the correct types in the program to match the database schema. And it’ll ensure that the type of rows
is something like (int * string) list
, and integrate that with type inference in the rest of the program.
The program won’t compile if you use the wrong types. It integrates OCaml’s type safety and type inference with the PostgreSQL database engine.
It also avoids SQL injection by automatically creating a safe prepared statement. What is executed when the program runs will have: ... where hostid = ? and name = ?
.
As a side-effect of the type checking, it also verifies that the SQL code is syntactically correct.