module Dbi: 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 row = sth#fetch1 in let name = row.(0) in
Simple usage, returning multiple rows:
let sth = dbh#prepare "select name from employees where salary > ?" in sth#execute [ `Int 10000 ]; sth#iter (fun row -> Printf.printf "name = %s\n" row.(0))
Advanced usage, binding columns for maximum efficiency:
let sth = dbh#prepare "select name from employees where salary > ?" in sth#execute [ `Int 10000 ]; let name = ref "" in sth#bind_columns [ `StringRef name ]; while sth#next do Printf.printf "name = %s\n" !name done
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 ) employee_list;
typearg_t =
[ `Bool of bool
| `BoolOption of bool option
| `Int of int
| `IntOption of int option
| `Interval of interval_t
| `IntervalOption of interval_t option
| `Null
| `Numeric of float
| `NumericOption of float option
| `String of string
| `StringOption of string option
| `Timestamp of timestamp_t
| `TimestampOption of timestamp_t option ]
statement#execute
method. The *Option
arguments can be Some
thing or None
meaning SQL NULL
.type
timestamp_t = {
|
ts_is_null : |
(* | Null? Other fields will be 0. | *) |
|
ts_year : |
|||
|
ts_month : |
|||
|
ts_day : |
|||
|
ts_hour : |
|||
|
ts_min : |
|||
|
ts_sec : |
|||
|
ts_microsecs : |
|||
|
ts_utc_offset : |
type
interval_t = {
|
iv_is_null : |
(* | Null? Other fields will be 0. | *) |
|
iv_years : |
|||
|
iv_months : |
|||
|
iv_days : |
|||
|
iv_hours : |
|||
|
iv_mins : |
|||
|
iv_secs : |
|||
|
iv_microsecs : |
val null_timestamp : timestamp_t
ts_is_null
field set to true.val null_interval : interval_t
iv_is_null
field set to true.typeref_t =
[ `IntOptionRef of int option Pervasives.ref
| `IntRef of int Pervasives.ref
| `NumericOptionRef of float option Pervasives.ref
| `NumericRef of float Pervasives.ref
| `StringOptionRef of string option Pervasives.ref
| `StringRef of string Pervasives.ref ]
statement#bind_columns
method.type
precommit_handle
type
postrollback_handle
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
module Factory: sig end