Module Dbi


module Dbi: sig .. end
Generic database interface.

Making a connection to a specific type of database:

  module DB = Dbi_postgres
  let dbh = new DB.connection "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\n                         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;                                         



Caml mappings of SQL types.



type date = {
   year : int;
   month : int;
   day : int;
}
type time = {
   hour : int;
   min : int;
   sec : int;
   microsec : int;
   timezone : int option;
}
type datetime = date * time 
module Decimal: sig .. end
type sql_t = [ `Bigint of Big_int.big_int
| `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 -> string
sql_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 -> string
sdebug 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 may be printed on stderr to aid in debugging the actual types returned by the database.
val intoption : int option -> sql_t
intoption(Some i) returns `Int i and intoption None returns `Null.
val stringoption : string option -> sql_t
stringoption(Some s) returns `String s and stringoption None returns `Null.
type precommit_handle 
See Dbi.connection.register_precommit.
type postrollback_handle 
See Dbi.connection.register_postrollback.

SQL utility functions.


val string_escaped : string -> string
escape_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 for you.
val placeholders : int -> string
placeholders n returns a string of the form "(?, ?, ..., ?)" containing n question marks.
Raises Invalid_argument if n <= 0.
exception SQL_error of string
Exceptions thrown by subclasses on SQL errors.
class virtual statement : connection -> object .. end
class virtual connection : ?host:string -> ?port:string -> ?user:string -> ?password:string -> string -> object .. end