module Dbi:Generic database interface.sig..end
Making a connection to a specific type of database:
module DB = Dbi_postgres
let dbh = new DB.connection "database_name"
Equivalent to above, except that we make a connection to a named type of database:
let dbh =
try Dbi.Factory.connect "postgres" "database_name"
with Not_found -> failwith "Postgres driver not available."
From Apache, using persistent connections (see Apache.DbiPool):
let dbh = Apache.DbiPool.get r "postgres" "database_name"
Simple usage, returning one row:
let sth = dbh#prepare "SELECT name FROM employees WHERE empid = ?" in
sth#execute [`Int 100];
let [`String name] = sth#fetch1 in ...
Simple usage, returning multiple rows:
let sth = dbh#prepare "SELECT firstname, name FROM employees
WHERE salary > ?" in
sth#execute [`Int 10000];
sth#iter(function
| [(`Null | `String _) as fname, `String name] ->
do_something_with fname name
| _ -> assert false);
Advanced usage, reusing prepared statements:
let sth =
dbh#prepare "INSERT INTO employees(name, salary) VALUES (?, ?)" in
List.iter(fun (name, salary) ->
sth#execute [`String name; `Int salary];
let id = sth#serial "" in
Printf.printf "Employee %s has been assigned ID %d\n" name id
) employees_list; type date = {
|
year : |
|
month : |
|
day : |
type time = {
|
hour : |
|
min : |
|
sec : |
|
microsec : |
|
timezone : |
typedatetime =date * time
module Decimal:sig..end
typesql_t =[ `Binary of string
| `Bool of bool
| `Date of date
| `Decimal of Decimal.t
| `Float of float
| `Int of int
| `Interval of datetime
| `Null
| `String of string
| `Time of time
| `Timestamp of datetime
| `Unknown of string ]
val sql_t_to_string : sql_t -> stringsql_t_to_string t returns a string representation of t
following the Ocaml conventions. The aim is to offer an easy way
to print sql_t values.val sdebug : sql_t list -> stringsdebug ss can be used to debug the return value from a #fetch1,
#map or other query. It converts the sql_t list type into a
printable form which can be printed on stderr to aid in debugging
the actual types returned by the database.val intoption : int option -> sql_tintoption(Some i) returns `Int i and
intoption None returns `Null.val stringoption : string option -> sql_tstringoption(Some s) returns `String s and
* stringoption None returns `Null.type precommit_handle
type postrollback_handle
val split_query : string -> string listval make_query : string -> (sql_t -> string) -> string list -> sql_t list -> stringval string_escaped : string -> stringescape_string s escapes the string s according to SQL rules
and surrounds it with single quotes. This should be hardly needed
as the data provided through the sql_t types will automatically be
escaped.val placeholders : int -> stringplaceholders n returns a string of the form "(?, ?, ..., ?)" containing
n question marks.Invalid_argument if n <= 0.exception SQL_error of string
class virtual statement :connection ->object..end
class virtual connection :?host:string -> ?port:string -> ?user:string -> ?password:string -> string ->object..end