Module Os_core_db.PGOCaml
type 'a tDatabase handle.
type 'a monad = 'a Lwt.ttype isolation = [ | `Serializable| `Repeatable_read| `Read_committed| `Read_uncommitted
]type access = [ | `Read_write| `Read_only
]exception Error of stringFor library errors.
exception PostgreSQL_Error of string * (char * string) listFor errors generated by the PostgreSQL database back-end. The first argument is a printable error message. The second argument is the complete set of error fields returned from the back-end.
See http://www.postgresql.org/docs/8.1/static/protocol-error-fields.html
Connection management
type connection_desc = {user : string;port : int;password : string;host : [ `Hostname of string | `Unix_domain_socket_dir of string ];database : string;
}val describe_connection :
?host:string ->
?port:int ->
?user:string ->
?password:string ->
?database:string ->
?unix_domain_socket_dir:string ->
unit ->
connection_descProduce the actual, concrete connection parameters based on the values and * availability of the various configuration variables.
val connection_desc_to_string : connection_desc -> stringProduce a human-readable textual representation of a concrete connection * descriptor (the password is NOT included in the output of this function) * for logging and error reporting purposes.
val connect :
?host:string ->
?port:int ->
?user:string ->
?password:string ->
?database:string ->
?unix_domain_socket_dir:string ->
?desc:connection_desc ->
unit ->
'a t monadConnect to the database.
The normal $PGDATABASE, etc. environment variables are available.
Close the database handle.
You must call this after you have finished with the handle, or else you will get leaked file descriptors.
Ping the database.
If the database is not available, some sort of exception will be thrown.
This function is a wrapper of ping that returns a boolean instead of raising an exception.
Transactions
Start a transaction.
val transact :
'a t ->
?isolation:isolation ->
?access:access ->
?deferrable:bool ->
('a t -> 'b monad) ->
'b monadtransact db ?isolation ?access ?deferrable f wraps your function f inside a transactional block. First it calls begin_work with isolation, access and deferrable, then calls f and do rollback if f raises an exception, commit otherwise.
Serial column
This is a shorthand for SELECT CURRVAL(serial).
For a table called table with serial column id you would typically call this as serial dbh "table_id_seq" after the previous INSERT operation to get the serial number of the inserted row.
As serial but assumes that the column is a SERIAL or SERIAL4 type.
Miscellaneous
val max_message_length : int refMaximum message length accepted from the back-end.
The default is Sys.max_string_length, which means that we will try to read as much data from the back-end as we can, and this may cause us to run out of memory (particularly on 64 bit machines), causing a possible denial of service.
You may want to set this to a smaller size to avoid this happening.
val verbose : int refVerbosity.
0 means don't print anything. 1 means print short error messages as returned from the back-end. 2 means print all messages as returned from the back-end.
Messages are printed on stderr. Default verbosity level is 1.
val set_private_data : 'a t -> 'a -> unitAttach some private data to the database handle.
NB. The pa_pgsql camlp4 extension uses this for its own purposes, which means that in most programs you will not be able to attach private data to the database handle.
val private_data : 'a t -> 'aRetrieve some private data previously attached to the database handle. If no data has been attached, raises Not_found.
NB. The pa_pgsql camlp4 extension uses this for its own purposes, which means that in most programs you will not be able to attach private data to the database handle.
val uuid : 'a t -> stringRetrieve the unique identifier for this connection.
type pa_pg_data = (string, bool) Hashtbl.tWhen using pa_pgsql, database handles have type PGOCaml.pa_pg_data PGOCaml.t
Low level query interface - DO NOT USE DIRECTLY
type oid = int32val pp_oid :
Ppx_deriving_runtime.Format.formatter ->
oid ->
Ppx_deriving_runtime.unitval show_oid : oid -> Ppx_deriving_runtime.stringtype param = string optiontype result = string optiontype row = result listprepare conn ~query ?name ?types () prepares the statement query and optionally names it name and sets the parameter types to types.
If no name is given, then the "unnamed" statement is overwritten. If no types are given, then the PostgreSQL engine infers types. Synchronously checks for errors.
execute conn ?name ~params () executes the named or unnamed statement name, with the given parameters params, returning the result rows (if any).
There are several steps involved at the protocol layer:
(1) a "portal" is created from the statement, binding the parameters in the statement (Bind).
(2) the portal is executed (Execute).
(3) we synchronise the connection (Sync).
The optional ?portal parameter may be used to name the portal created in step (1) above (otherwise the unnamed portal is used). This is only important if you want to call describe_portal to find out the result types.
close_statement conn ?name () closes a prepared statement and frees up any resources.
close_portal conn ?portal () closes a portal and frees up any resources.
inject conn ?name query executes the statement query and optionally names it name and gives the result.
alter conn ?name query executes the statement query and optionally names it name. Same as inject but ignoring the result.
val pp_result_description :
Ppx_deriving_runtime.Format.formatter ->
result_description ->
Ppx_deriving_runtime.unitval show_result_description : result_description -> Ppx_deriving_runtime.stringtype row_description = result_description listval pp_row_description :
Ppx_deriving_runtime.Format.formatter ->
row_description ->
Ppx_deriving_runtime.unitval show_row_description : row_description -> Ppx_deriving_runtime.stringtype params_description = param_description listval describe_statement :
'a t ->
?name:string ->
unit ->
(params_description * row_description option) monaddescribe_statement conn ?name () describes the named or unnamed statement's parameter types and result types.
val describe_portal :
'a t ->
?portal:string ->
unit ->
row_description option monaddescribe_portal conn ?portal () describes the named or unnamed portal's result types.
Low level type conversion functions - DO NOT USE DIRECTLY
val name_of_type : oid -> stringReturns the OCaml equivalent type name to the PostgreSQL type oid.
For instance, name_of_type (Int32.of_int 23) returns "int32" because the OID for PostgreSQL's internal int4 type is 23.
As another example, name_of_type (Int32.of_int 25) returns "string".
type inet = Unix.inet_addr * inttype timestamptz = CalendarLib.Calendar.t * CalendarLib.Time_Zone.ttype int16 = inttype bytea = stringtype point = float * floattype hstore = (string * string option) listtype numeric = stringtype uuid = stringtype jsonb = stringtype bool_array = bool option listtype int16_array = int16 option listtype int32_array = int32 option listtype int64_array = int64 option listtype string_array = string option listtype float_array = float option listtype timestamp_array = CalendarLib.Calendar.t option listtype uuid_array = string option listThe following conversion functions are used by pa_pgsql to convert values in and out of the database.
val string_of_oid : oid -> stringval string_of_bool : bool -> stringval string_of_int : int -> stringval string_of_int16 : int16 -> stringval string_of_int32 : int32 -> stringval string_of_int64 : int64 -> stringval string_of_float : float -> stringval string_of_point : point -> stringval string_of_hstore : hstore -> stringval string_of_numeric : numeric -> stringval string_of_uuid : uuid -> stringval string_of_jsonb : jsonb -> stringval string_of_inet : inet -> stringval string_of_timestamp : CalendarLib.Calendar.t -> stringval string_of_timestamptz : timestamptz -> stringval string_of_date : CalendarLib.Date.t -> stringval string_of_time : CalendarLib.Time.t -> stringval string_of_interval : CalendarLib.Calendar.Period.t -> stringval string_of_bytea : bytea -> stringval string_of_string : string -> stringval string_of_unit : unit -> stringval string_of_bool_array : bool_array -> stringval string_of_int16_array : int16_array -> stringval string_of_int32_array : int32_array -> stringval string_of_int64_array : int64_array -> stringval string_of_string_array : string_array -> stringval string_of_bytea_array : string_array -> stringval string_of_float_array : float_array -> stringval string_of_timestamp_array : timestamp_array -> stringval string_of_arbitrary_array : ('a -> string) -> 'a option list -> stringval string_of_uuid_array : uuid_array -> stringval comment_src_loc : unit -> boolval find_custom_typconvs :
?typnam:string ->
?lookin:string ->
?colnam:string ->
?argnam:string ->
unit ->
((string * string) option, string) Rresult.resultval oid_of_string : string -> oidval bool_of_string : string -> boolval int_of_string : string -> intval int16_of_string : string -> int16val int32_of_string : string -> int32val int64_of_string : string -> int64val float_of_string : string -> floatval point_of_string : string -> pointval hstore_of_string : string -> hstoreval numeric_of_string : string -> numericval uuid_of_string : string -> uuidval jsonb_of_string : string -> jsonbval inet_of_string : string -> inetval timestamp_of_string : string -> CalendarLib.Calendar.tval timestamptz_of_string : string -> timestamptzval date_of_string : string -> CalendarLib.Date.tval time_of_string : string -> CalendarLib.Time.tval interval_of_string : string -> CalendarLib.Calendar.Period.tval bytea_of_string : string -> byteaval unit_of_string : string -> unitval bool_array_of_string : string -> bool_arrayval int16_array_of_string : string -> int16_arrayval int32_array_of_string : string -> int32_arrayval int64_array_of_string : string -> int64_arrayval string_array_of_string : string -> string_arrayval float_array_of_string : string -> float_arrayval timestamp_array_of_string : string -> timestamp_arrayval arbitrary_array_of_string : (string -> 'a) -> string -> 'a option listval return : 'a -> 'a monad