Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file postgresql.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182(*
PostgreSQL-OCAML - OCaml-interface to the PostgreSQL database
Copyright (C) 2004- Markus Mottl
email: markus.mottl@gmail.com
WWW: http://www.ocaml.info
Copyright (C) 2001 Alain Frisch (version: postgres-20010808)
email: Alain.Frisch@ens.fr
WWW: http://www.eleves.ens.fr/home/frisch
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*)openPrintftypeoid=inttypelarge_object=intexceptionOidofoidletinvalid_oid=0moduleError_field=Error_fieldmoduleError_code=Error_codemoduleFFormat=structtypet=|TEXT|BINARYendtypeftype=|BOOL|BYTEA|CHAR|NAME|INT8|INT2|INT2VECTOR|INT4|REGPROC|TEXT|OID|TID|XID|CID|OIDVECTOR|JSON|POINT|LSEG|PATH|BOX|POLYGON|LINE|FLOAT4|FLOAT8|ABSTIME|RELTIME|TINTERVAL|UNKNOWN|CIRCLE|CASH|MACADDR|INET|CIDR|ACLITEM|BPCHAR|VARCHAR|DATE|TIME|TIMESTAMP|TIMESTAMPTZ|INTERVAL|TIMETZ|BIT|VARBIT|NUMERIC|REFCURSOR|REGPROCEDURE|REGOPER|REGOPERATOR|REGCLASS|REGTYPE|RECORD|CSTRING|ANY|ANYARRAY|VOID|TRIGGER|LANGUAGE_HANDLER|INTERNAL|OPAQUE|ANYELEMENT|JSONBexternalftype_of_oid:(oid[@untagged])->ftype="ftype_of_oid_stub_bc""ftype_of_oid_stub"externaloid_of_ftype:ftype->(oid[@untagged])="oid_of_ftype_stub_bc""oid_of_ftype_stub"[@@noalloc]letstring_of_ftype=function|BOOL->"BOOL"|BYTEA->"BYTEA"|CHAR->"CHAR"|NAME->"NAME"|INT8->"INT8"|INT2->"INT2"|INT2VECTOR->"INT2VECTOR"|INT4->"INT4"|REGPROC->"REGPROC"|TEXT->"TEXT"|OID->"OID"|TID->"TID"|XID->"XID"|CID->"CID"|OIDVECTOR->"OIDVECTOR"|JSON->"JSON"|POINT->"POINT"|LSEG->"LSEG"|PATH->"PATH"|BOX->"BOX"|POLYGON->"POLYGON"|LINE->"LINE"|FLOAT4->"FLOAT4"|FLOAT8->"FLOAT8"|ABSTIME->"ABSTIME"|RELTIME->"RELTIME"|TINTERVAL->"TINTERVAL"|UNKNOWN->"UNKNOWN"|CIRCLE->"CIRCLE"|CASH->"CASH"|MACADDR->"MACADDR"|INET->"INET"|CIDR->"CIDR"|ACLITEM->"ACLITEM"|BPCHAR->"BPCHAR"|VARCHAR->"VARCHAR"|DATE->"DATE"|TIME->"TIME"|TIMESTAMP->"TIMESTAMP"|TIMESTAMPTZ->"TIMESTAMPTZ"|INTERVAL->"INTERVAL"|TIMETZ->"TIMETZ"|BIT->"BIT"|VARBIT->"VARBIT"|NUMERIC->"NUMERIC"|REFCURSOR->"REFCURSOR"|REGPROCEDURE->"REGPROCEDURE"|REGOPER->"REGOPER"|REGOPERATOR->"REGOPERATOR"|REGCLASS->"REGCLASS"|REGTYPE->"REGTYPE"|RECORD->"RECORD"|CSTRING->"CSTRING"|ANY->"ANY"|ANYARRAY->"ANYARRAY"|VOID->"VOID"|TRIGGER->"TRIGGER"|LANGUAGE_HANDLER->"LANGUAGE_HANDLER"|INTERNAL->"INTERNAL"|OPAQUE->"OPAQUE"|ANYELEMENT->"ANYELEMENT"|JSONB->"JSONB"letftype_of_string=function|"BOOL"->BOOL|"BYTEA"->BYTEA|"CHAR"->CHAR|"NAME"->NAME|"INT8"->INT8|"INT2"->INT2|"INT2VECTOR"->INT2VECTOR|"INT4"->INT4|"REGPROC"->REGPROC|"TEXT"->TEXT|"OID"->OID|"TID"->TID|"XID"->XID|"CID"->CID|"OIDVECTOR"->OIDVECTOR|"JSON"->JSON|"POINT"->POINT|"LSEG"->LSEG|"PATH"->PATH|"BOX"->BOX|"POLYGON"->POLYGON|"LINE"->LINE|"FLOAT4"->FLOAT4|"FLOAT8"->FLOAT8|"ABSTIME"->ABSTIME|"RELTIME"->RELTIME|"TINTERVAL"->TINTERVAL|"UNKNOWN"->UNKNOWN|"CIRCLE"->CIRCLE|"CASH"->CASH|"MACADDR"->MACADDR|"INET"->INET|"CIDR"->CIDR|"ACLITEM"->ACLITEM|"BPCHAR"->BPCHAR|"VARCHAR"->VARCHAR|"DATE"->DATE|"TIME"->TIME|"TIMESTAMP"->TIMESTAMP|"TIMESTAMPTZ"->TIMESTAMPTZ|"INTERVAL"->INTERVAL|"TIMETZ"->TIMETZ|"BIT"->BIT|"VARBIT"->VARBIT|"NUMERIC"->NUMERIC|"REFCURSOR"->REFCURSOR|"REGPROCEDURE"->REGPROCEDURE|"REGOPER"->REGOPER|"REGOPERATOR"->REGOPERATOR|"REGCLASS"->REGCLASS|"REGTYPE"->REGTYPE|"RECORD"->RECORD|"CSTRING"->CSTRING|"ANY"->ANY|"ANYARRAY"->ANYARRAY|"VOID"->VOID|"TRIGGER"->TRIGGER|"LANGUAGE_HANDLER"->LANGUAGE_HANDLER|"INTERNAL"->INTERNAL|"OPAQUE"->OPAQUE|"ANYELEMENT"->ANYELEMENT|"JSONB"->JSONB|str->failwith("ftype_of_string: unknown ftype: "^str)externalinit:unit->unit="PQocaml_init"letnull=""let()=Callback.register_exception"Postgresql.Oid"(Oidinvalid_oid);Callback.register"Postgresql.null"null;init()typeconnection_status=|Ok|Bad|Connection_started|Connection_made|Connection_awaiting_response|Connection_auth_ok|Connection_setenv|Connection_ssl_startuptypepolling_status=|Polling_failed|Polling_reading|Polling_writing|Polling_oktypeflush_status=|Successful|Data_left_to_sendtypeconninfo_option={cio_keyword:string;cio_envvar:stringoption;cio_compiled:stringoption;cio_val:stringoption;cio_label:string;cio_dispchar:string;cio_dispsize:int;}typeresult_status=|Empty_query|Command_ok|Tuples_ok|Copy_out|Copy_in|Bad_response|Nonfatal_error|Fatal_error|Copy_both|Single_tupleexternalresult_status:result_status->string="PQresStatus_stub"typegetline_result=EOF|LineRead|BufFulltypegetline_async_result=|EndOfData|NoData|DataReadofint|PartDataReadofinttypeseek_cmd=|SEEK_SET|SEEK_CUR|SEEK_ENDtypeerror=|Field_out_of_rangeofint*int|Tuple_out_of_rangeofint*int|Binary|Connection_failureofstring|Unexpected_statusofresult_status*string*(result_statuslist)|Cancel_failureofstringletstring_of_error=function|Field_out_of_range(i,n)->sprintf"Field number %i is out of range [0..%i]"i(n-1)|Tuple_out_of_range(i,n)->sprintf"Tuple number %i is out of range [0..%i]"i(n-1)|Binary->sprintf"This function does not accept binary tuples"|Connection_failures->"Connection failure: "^s|Unexpected_status(s,msg,sl)->sprintf"Result status %s unexpected (expected status:%s); %s"(result_statuss)(String.concat","(List.mapresult_statussl))msg|Cancel_failures->"Cancel failure: "^sexceptionErroroferrormoduleNotification=structtypet={name:string;pid:int;extra:string}end(* Notification *)moduleStub=struct(* Database Connection Functions *)typeconnectiontyperesultexternalconn_isnull:connection->bool="PQconn_isnull"[@@noalloc]externalconnect:string->bool->connection="PQconnectdb_stub"externalfinish:connection->unit="PQfinish_stub"externalreset:connection->unit="PQreset_stub"externaldb:connection->string="PQdb_stub"externaluser:connection->string="PQuser_stub"externalpass:connection->string="PQpass_stub"externalhost:connection->string="PQhost_stub"externalport:connection->string="PQport_stub"externaltty:connection->string="PQtty_stub"externaloptions:connection->string="PQoptions_stub"externalconnection_status:connection->connection_status="PQstatus_stub"[@@noalloc]externalerror_message:connection->string="PQerrorMessage_stub"externalbackend_pid:connection->(int[@untagged])="PQbackendPID_stub_bc""PQbackendPID_stub"[@@noalloc]externalserver_version:connection->(int[@untagged])="PQserverVersion_stub_bc""PQserverVersion_stub"[@@noalloc](* Command Execution Functions *)externalresult_isnull:result->bool="PQres_isnull"[@@noalloc]externalexec_params:connection->string->stringarray->boolarray->bool->result="PQexecParams_stub"externalprepare:connection->string->string->result="PQprepare_stub"externalexec_prepared:connection->string->stringarray->boolarray->result="PQexecPrepared_stub"externaldescribe_prepared:connection->string->result="PQdescribePrepared_stub"externalresult_status:result->result_status="PQresultStatus_stub"[@@noalloc]externalresult_error:result->string="PQresultErrorMessage_stub"externalresult_error_field:result->Error_field.t->string="PQresultErrorField_stub"externalmake_empty_res:connection->result_status->result="PQmakeEmptyPGresult_stub"externalntuples:result->(int[@untagged])="PQntuples_stub_bc""PQntuples_stub"[@@noalloc]externalnparams:result->(int[@untagged])="PQnparams_stub_bc""PQnparams_stub"externalnfields:result->(int[@untagged])="PQnfields_stub_bc""PQnfields_stub"[@@noalloc]externalfname:result->(int[@untagged])->string="PQfname_stub_bc""PQfname_stub"externalfnumber:result->string->(int[@untagged])="PQfnumber_stub_bc""PQfnumber_stub"[@@noalloc]externalfformat:result->(int[@untagged])->FFormat.t="PQfformat_stub_bc""PQfformat_stub"[@@noalloc]externalftype:result->(int[@untagged])->(oid[@untagged])="PQftype_stub_bc""PQftype_stub"[@@noalloc]externalparamtype:result->(int[@untagged])->(oid[@untagged])="PQparamtype_stub_bc""PQparamtype_stub"externalfmod:result->(int[@untagged])->(int[@untagged])="PQfmod_stub_bc""PQfmod_stub"[@@noalloc]externalfsize:result->(int[@untagged])->(int[@untagged])="PQfsize_stub_bc""PQfsize_stub"[@@noalloc]externalbinary_tuples:result->bool="PQbinaryTuples_stub"[@@noalloc]externalgetvalue:result->(int[@untagged])->(int[@untagged])->string="PQgetvalue_stub_bc""PQgetvalue_stub"externalget_escaped_value:result->(int[@untagged])->(int[@untagged])->string="PQgetescval_stub_bc""PQgetescval_stub"externalgetisnull:result->(int[@untagged])->(int[@untagged])->bool="PQgetisnull_stub_bc""PQgetisnull_stub"[@@noalloc]externalgetlength:result->(int[@untagged])->(int[@untagged])->(int[@untagged])="PQgetlength_stub_bc""PQgetlength_stub"[@@noalloc]externalcmd_status:result->string="PQcmdStatus_stub"externalcmd_tuples:result->string="PQcmdTuples_stub"externaloid_value:result->(oid[@untagged])="PQoidValue_stub_bc""PQoidValue_stub"[@@noalloc](* Asynchronous Query Processing *)externalconnect_poll:connection->polling_status="PQconnectPoll_stub"[@@noalloc]externalreset_start:connection->bool="PQresetStart_stub"[@@noalloc]externalreset_poll:connection->polling_status="PQresetPoll_stub"[@@noalloc]externalset_nonblocking:connection->bool->(int[@untagged])="PQsetnonblocking_stub_bc""PQsetnonblocking_stub"[@@noalloc]externalis_nonblocking:connection->bool="PQisnonblocking_stub"[@@noalloc]externalsend_query_params:connection->string->stringarray->boolarray->(int[@untagged])="PQsendQueryParams_stub_bc""PQsendQueryParams_stub"externalsend_prepare:connection->string->string->(int[@untagged])="PQsendPrepare_stub_bc""PQsendPrepare_stub"[@@noalloc]externalsend_query_prepared:connection->string->stringarray->boolarray->(int[@untagged])="PQsendQueryPrepared_stub_bc""PQsendQueryPrepared_stub"externalsend_describe_prepared:connection->string->(int[@untagged])="PQsendDescribePrepared_stub_bc""PQsendDescribePrepared_stub"externalsend_describe_portal:connection->string->(int[@untagged])="PQsendDescribePortal_stub_bc""PQsendDescribePortal_stub"externalset_single_row_mode:connection->(int[@untagged])="PQsetSingleRowMode_stub_bc""PQsetSingleRowMode_stub"externalget_result:connection->result="PQgetResult_stub"externalconsume_input:connection->(int[@untagged])="PQconsumeInput_stub_bc""PQconsumeInput_stub"[@@noalloc]externalis_busy:connection->bool="PQisBusy_stub"[@@noalloc]externalflush:connection->(int[@untagged])="PQflush_stub_bc""PQflush_stub"[@@noalloc]externalsocket:connection->(int[@untagged])="PQsocket_stub_bc""PQsocket_stub"[@@noalloc]externalrequest_cancel:connection->stringoption="PQCancel_stub"(* Asynchronous Notification *)externalnotifies:connection->Notification.toption="PQnotifies_stub"(* Functions Associated with the COPY Command *)externalgetline:connection->Bytes.t->(int[@untagged])->(int[@untagged])->(int[@untagged])="PQgetline_stub_bc""PQgetline_stub"externalgetline_async:connection->Bytes.t->(int[@untagged])->(int[@untagged])->(int[@untagged])="PQgetlineAsync_stub_bc""PQgetlineAsync_stub"[@@noalloc]externalputline:connection->string->(int[@untagged])="PQputline_stub_bc""PQputline_stub"externalputnbytes:connection->string->(int[@untagged])->(int[@untagged])->(int[@untagged])="PQputnbytes_stub_bc""PQputnbytes_stub"externalendcopy:connection->(int[@untagged])="PQendcopy_stub_bc""PQendcopy_stub"externalescape_string_conn:connection->string->pos:(int[@untagged])->len:(int[@untagged])->string="PQescapeStringConn_stub_bc""PQescapeStringConn_stub"externalescape_bytea_conn:connection->string->pos:(int[@untagged])->len:(int[@untagged])->string="PQescapeByteaConn_stub_bc""PQescapeByteaConn_stub"(* Control Functions *)externalset_notice_processor:connection->(string->unit)->unit="PQsetNoticeProcessor_stub"(* Large objects *)externallo_creat:connection->(oid[@untagged])="lo_creat_stub_bc""lo_creat_stub"externallo_import:connection->string->(oid[@untagged])="lo_import_stub_bc""lo_import_stub"externallo_export:connection->(oid[@untagged])->string->(int[@untagged])="lo_export_stub_bc""lo_export_stub"externallo_open:connection->(oid[@untagged])->(large_object[@untagged])="lo_open_stub_bc""lo_open_stub"externallo_close:connection->(large_object[@untagged])->(int[@untagged])="lo_close_stub_bc""lo_close_stub"externallo_tell:connection->(large_object[@untagged])->(int[@untagged])="lo_tell_stub_bc""lo_tell_stub"externallo_unlink:connection->(oid[@untagged])->(oid[@untagged])="lo_unlink_stub_bc""lo_unlink_stub"externallo_read:connection->(large_object[@untagged])->Bytes.t->(int[@untagged])->(int[@untagged])->(int[@untagged])="lo_read_stub_bc""lo_read_stub"externallo_read_ba:connection->(large_object[@untagged])->(char,Bigarray.int8_unsigned_elt,Bigarray.c_layout)Bigarray.Array1.t->(int[@untagged])->(int[@untagged])->(int[@untagged])="lo_read_ba_stub_bc""lo_read_ba_stub"externallo_write:connection->(large_object[@untagged])->string->(int[@untagged])->(int[@untagged])->(int[@untagged])="lo_write_stub_bc""lo_write_stub"externallo_write_ba:connection->(large_object[@untagged])->(char,Bigarray.int8_unsigned_elt,Bigarray.c_layout)Bigarray.Array1.t->(int[@untagged])->(int[@untagged])->(int[@untagged])="lo_write_ba_stub_bc""lo_write_ba_stub"externallo_seek:connection->(large_object[@untagged])->(int[@untagged])->seek_cmd->(int[@untagged])="lo_lseek_stub_bc""lo_lseek_stub"end(* Escaping *)externalunescape_bytea:string->string="PQunescapeBytea_stub"(* Query results *)classresultres=letnfields=Stub.nfieldsresinletntuples=Stub.ntuplesresinletnparams=lazy(Stub.nparamsres)inletcheck_fieldfield=iffield<0||field>=nfieldsthenraise(Error(Field_out_of_range(field,nfields)))inletcheck_paramparam=letnparams=Lazy.forcenparamsinifparam<0||param>=nparamsthenraise(Error(Field_out_of_range(param,nparams)))inletcheck_tupletuple=iftuple<0||tuple>=ntuplesthenraise(Error(Tuple_out_of_range(tuple,ntuples)))inobjectmethodstatus=Stub.result_statusresmethoderror=Stub.result_errorresmethoderror_fieldfield_name=Stub.result_error_fieldresfield_namemethoderror_code=Error_code.of_sqlstate(Stub.result_error_fieldresError_field.SQLSTATE)methodntuples=ntuplesmethodnparams=Lazy.forcenparamsmethodnfields=nfieldsmethodbinary_tuples=Stub.binary_tuplesresmethodfnamefield=check_fieldfield;Stub.fnameresfieldmethodfnumbers=letn=Stub.fnumberressinifn=-1thenraiseNot_foundelsenmethodfformatfield=check_fieldfield;Stub.fformatresfieldmethodftypefield=check_fieldfield;ftype_of_oid(Stub.ftyperesfield)methodftype_oidfield=check_fieldfield;Stub.ftyperesfieldmethodparamtypefield=check_paramfield;ftype_of_oid(Stub.paramtyperesfield)methodparamtype_oidfield=check_paramfield;Stub.paramtyperesfieldmethodfmodfield=check_fieldfield;Stub.fmodresfieldmethodfsizefield=check_fieldfield;Stub.fsizeresfieldmethodgetvaluetuplefield=check_fieldfield;check_tupletuple;Stub.getvaluerestuplefieldmethodget_escaped_valuetuplefield=check_fieldfield;check_tupletuple;Stub.get_escaped_valuerestuplefieldmethodgetisnulltuplefield=check_fieldfield;check_tupletuple;Stub.getisnullrestuplefieldmethodgetlengthtuplefield=check_fieldfield;check_tupletuple;Stub.getlengthrestuplefieldmethodcmd_status=Stub.cmd_statusresmethodcmd_tuples=Stub.cmd_tuplesresmethodoid_value=Stub.oid_valueresmethodget_fnames=Array.initnfields(Stub.fnameres)methodget_fnames_lst=letlst_ref=ref[]infori=nfields-1downto0dolst_ref:=Stub.fnameresi::!lst_ref;done;!lst_refmethodget_tuplet=check_tuplet;Array.initnfields(Stub.getvaluerest)methodget_tuple_lstt=check_tuplet;lettpl_ref=ref[]infori=nfields-1downto0dotpl_ref:=Stub.getvalueresti::!tpl_ref;done;!tpl_refmethodget_all=Array.initntuples(funt->Array.initnfields(Stub.getvaluerest))methodget_all_lst=letlst_ref=ref[]inletnfields_1=nfields-1infort=ntuples-1downto0dolettpl_ref=ref[]infori=nfields_1downto0dotpl_ref:=Stub.getvalueresti::!tpl_refdone;lst_ref:=!tpl_ref::!lst_refdone;!lst_refend(* Connections *)externalconndefaults:unit->conninfo_optionarray="PQconndefaults_stub"exceptionFinallyofexn*exnletprotectx~fx~(finally:'a->unit)=letres=tryfxwithexn->(tryfinallyxwithfinal_exn->raise(Finally(exn,final_exn)));raiseexninfinallyx;resclassconnection?host?hostaddr?port?dbname?user?password?options?tty?requiressl?conninfo?(startonly=false)=letconn_info=matchconninfowith|Someconn_info->conn_info|None->letb=Buffer.create512inletfieldname=function|None->()|Somex->Printf.bprintfb"%s='"name;fori=0toString.lengthx-1doifx.[i]='\''thenBuffer.add_stringb"\\'"elseBuffer.add_charbx.[i]done;Buffer.add_stringb"' "infield"host"host;field"hostaddr"hostaddr;field"port"port;field"dbname"dbname;field"user"user;field"password"password;field"options"options;field"tty"tty;field"requiressl"requiressl;Buffer.contentsbinfun()->letmy_conn=Stub.connectconn_infostartonlyinlet()=ifStub.connection_statusmy_conn=Badthen(lets=Stub.error_messagemy_conninStub.finishmy_conn;raise(Error(Connection_failures)))elseGc.finaliseStub.finishmy_conninletconn_mtx=Mutex.create()inletconn_cnd=Condition.create()inletconn_state=ref`Freeinletcheck_null()=ifStub.conn_isnullmy_connthenfailwith"Postgresql.check_null: connection already finished"inletwrap_mtxf=Mutex.lockconn_mtx;protectxconn_mtx~f:(fun_->check_null();(* Check now to avoid blocking *)f())~finally:Mutex.unlockinletwrap_conn?(state=`Used)f=wrap_mtx(fun()->while!conn_state<>`FreedoCondition.waitconn_cndconn_mtxdone;conn_state:=state);protectxconn_state~f:(fun_->check_null();(* Check again in case the world has changed *)fmy_conn)~finally:(fun_->Mutex.lockconn_mtx;conn_state:=`Free;Condition.signalconn_cnd;Mutex.unlockconn_mtx)inletsignal_errorconn=raise(Error(Connection_failure(Stub.error_messageconn)))inletrequest_cancel()=wrap_mtx(fun_->match!conn_statewith|`Finishing|`Free->()|`Used->matchStub.request_cancelmy_connwith|None->()|Someerr->raise(Error(Cancel_failureerr)))inletget_str_pos_len~loc?pos?lenstr=letstr_len=String.lengthstrinmatchpos,lenwith|None,None->0,str_len|Somepos,_whenpos<0->invalid_arg(sprintf"Postgresql.%s: pos < 0"loc)|_,Somelenwhenlen<0->invalid_arg(sprintf"Postgresql.%s: len < 0"loc)|Somepos,Nonewhenpos>str_len->invalid_arg(sprintf"Postgresql.%s: pos > length(str)"loc)|Somepos,None->pos,str_len-pos|None,Somelenwhenlen>str_len->invalid_arg(sprintf"Postgresql.%s: len > length(str)"loc)|None,Somelen->0,len|Somepos,Somelenwhenpos+len>str_len->invalid_arg(sprintf"Postgresql.%s: pos + len > length(str)"loc)|Somepos,Somelen->pos,leninobject(self)(* Main routines *)methodfinish=wrap_conn~state:`FinishingStub.finishmethodtry_reset=wrap_conn(funconn->ifStub.connection_statusconn=Badthen(Stub.resetconn;ifStub.connection_statusconn<>Okthensignal_errorconn))methodreset=wrap_connStub.reset(* Asynchronous Notification *)methodnotifies=wrap_connStub.notifies(* Control Functions *)methodset_notice_processorf=wrap_conn(funconn->Stub.set_notice_processorconnf)(* Accessors *)methoddb=wrap_connStub.dbmethoduser=wrap_connStub.usermethodpass=wrap_connStub.passmethodhost=wrap_connStub.hostmethodport=wrap_connStub.portmethodtty=wrap_connStub.ttymethodoptions=wrap_connStub.optionsmethodstatus=wrap_connStub.connection_statusmethoderror_message=wrap_connStub.error_messagemethodbackend_pid=wrap_connStub.backend_pidmethodserver_version=letversion=wrap_conn(funconn->letversion=Stub.server_versionconninifversion<>0thenversionelseletmsg=ifStub.connection_statusconn=Badthen"server_version failed because the connection was bad"else"server_version failed for an unknown reason"inraise(Error(Connection_failuremsg)))inletmajor=version/(100*100)inletminor=(version/100)mod100inletrevision=versionmod100inmajor,minor,revision(* Commands and Queries *)methodempty_resultstatus=newresult(wrap_conn(funconn->(Stub.make_empty_resconnstatus)))methodexec?(expect=[])?(params=[||])?(binary_params=[||])?(binary_result=false)query=letr=wrap_conn(funconn->letr=Stub.exec_paramsconnqueryparamsbinary_paramsbinary_resultinifStub.result_isnullrthensignal_errorconnelser)inletres=newresultrinletstat=res#statusinifnot(expect=[])&¬(List.memstatexpect)thenraise(Error(Unexpected_status(stat,res#error,expect)))elseresmethodpreparestm_namequery=newresult(wrap_conn(funconn->letr=Stub.prepareconnstm_namequeryinifStub.result_isnullrthensignal_errorconnelser))methodexec_prepared?(expect=[])?(params=[||])?(binary_params=[||])stm_name=letr=wrap_conn(funconn->letr=Stub.exec_preparedconnstm_nameparamsbinary_paramsinifStub.result_isnullrthensignal_errorconnelser)inletres=newresultrinletstat=res#statusinifnot(expect=[])&¬(List.memstatexpect)thenraise(Error(Unexpected_status(stat,res#error,expect)))elseresmethoddescribe_preparedquery=newresult(wrap_conn(funconn->letr=Stub.describe_preparedconnqueryinifStub.result_isnullrthensignal_errorconnelser))methodsend_query?(params=[||])?(binary_params=[||])query=wrap_conn(funconn->ifStub.send_query_paramsconnqueryparamsbinary_params<>1thensignal_errorconn)methodsend_preparestm_namequery=wrap_conn(funconn->ifStub.send_prepareconnstm_namequery<>1thensignal_errorconn)methodsend_query_prepared?(params=[||])?(binary_params=[||])stm_name=wrap_conn(funconn->ifStub.send_query_preparedconnstm_nameparamsbinary_params<>1thensignal_errorconn)methodsend_describe_preparedstm_name=wrap_conn(funconn->ifStub.send_describe_preparedconnstm_name<>1thensignal_errorconn)methodsend_describe_portalportal_name=wrap_conn(funconn->ifStub.send_describe_portalconnportal_name<>1thensignal_errorconn)methodset_single_row_mode=wrap_conn(funconn->ifStub.set_single_row_modeconn<>1thensignal_errorconn)methodget_result=letres=wrap_connStub.get_resultinifStub.result_isnullresthenNoneelseSome(newresultres)(* Copy operations *)(* Low level *)methodgetline?(pos=0)?lenbuf=letbuf_len=Bytes.lengthbufinletlen=matchlenwithSomelen->len|None->buf_len-posiniflen<0||pos<0||pos+len>buf_lentheninvalid_arg"Postgresql.connection#getline";wrap_conn(funconn->matchStub.getlineconnbufposlenwith|-1->EOF|0->LineRead|1->BufFull|_->assertfalse)methodgetline_async?(pos=0)?lenbuf=letbuf_len=Bytes.lengthbufinletlen=matchlenwithSomelen->len|None->buf_len-posiniflen<0||pos<0||pos+len>buf_lentheninvalid_arg"Postgresql.connection#getline_async";wrap_conn(funconn->matchStub.getline_asyncconnbufposlenwith|-1->ifStub.endcopyconn<>0thensignal_errorconnelseEndOfData|0->NoData|nwhenn>0->ifBytes.getbuf(pos+n-1)='\n'thenDataReadnelsePartDataReadn|_->assertfalse)methodputlinebuf=wrap_conn(funconn->ifStub.putlineconnbuf<>0&¬(Stub.is_nonblockingconn)thensignal_errorconn)methodputnbytes?(pos=0)?lenbuf=letbuf_len=String.lengthbufinletlen=matchlenwithSomelen->len|None->buf_len-posiniflen<0||pos<0||pos+len>buf_lentheninvalid_arg"Postgresql.connection#putnbytes";wrap_conn(funconn->ifStub.putnbytesconnbufposlen<>0&¬(Stub.is_nonblockingconn)thensignal_errorconn)methodendcopy=wrap_conn(funconn->ifStub.endcopyconn<>0&¬(Stub.is_nonblockingconn)thensignal_errorconn)(* High level *)methodcopy_outf=letbuf=Buffer.create1024inletlen=512inletbts=Bytes.createleninwrap_conn(funconn->letrecloop()=letr=Stub.getlineconnbts0leninifr=1thenbegin(* Buffer full *)Buffer.add_subbytesbufbts0len;loop()endelseifr=0then(* Line read *)letzero=Bytes.indexbts'\000'inBuffer.add_subbytesbufbts0zero;matchBuffer.contentsbufwith|"\\."->()|line->Buffer.clearbuf;fline;loop()elseifr=-1thenraiseEnd_of_fileelseassertfalse(* impossible *)inloop());self#endcopymethodcopy_out_channeloc=self#copy_out(funs->output_stringoc(s^"\n"))methodcopy_in_channelic=trywhiletruedoself#putline(input_lineic^"\n")done;withEnd_of_file->self#putline"\\.\n";self#endcopy(* Asynchronous operations and non blocking mode *)methodconnect_poll=wrap_connStub.connect_pollmethodreset_start=wrap_connStub.reset_startmethodreset_poll=wrap_connStub.reset_pollmethodset_nonblockingb=wrap_conn(funconn->ifStub.set_nonblockingconnb<>0thensignal_errorconn)methodis_nonblocking=wrap_connStub.is_nonblockingmethodconsume_input=wrap_conn(funconn->ifStub.consume_inputconn<>1thensignal_errorconn)methodis_busy=wrap_connStub.is_busymethodflush=wrap_conn(funconn->matchStub.flushconnwith|0->Successful|1->Data_left_to_send|_->signal_errorconn)methodsocket=wrap_conn(funconn->lets=Stub.socketconninifs=-1thensignal_errorconnelses)methodrequest_cancel=request_cancel()(* Large objects *)methodlo_creat=wrap_conn(funconn->letlo=Stub.lo_creatconniniflo<=0thensignal_errorconn;lo)methodlo_importfilename=wrap_conn(funconn->letoid=Stub.lo_importconnfilenameinifoid=0thensignal_errorconn;oid)methodlo_exportoidfilename=wrap_conn(funconn->ifStub.lo_exportconnoidfilename<=0thensignal_errorconn)methodlo_openoid=wrap_conn(funconn->letlo=Stub.lo_openconnoidiniflo=-1thensignal_errorconn;lo)methodlo_write?(pos=0)?lenbuflo=letbuf_len=String.lengthbufinletlen=matchlenwithSomelen->len|None->buf_len-posiniflen<0||pos<0||pos+len>String.lengthbuftheninvalid_arg"Postgresql.connection#lo_write";wrap_conn(funconn->letw=Stub.lo_writeconnlobufposleninifw<lenthensignal_errorconn)methodlo_write_ba?(pos=0)?lenbuflo=letbuf_len=Bigarray.Array1.dimbufinletlen=matchlenwithSomelen->len|None->buf_len-posiniflen<0||pos<0||pos+len>buf_lentheninvalid_arg"Postgresql.connection#lo_write_ba";wrap_conn(funconn->letw=Stub.lo_write_baconnlobufposleninifw<lenthensignal_errorconn)methodlo_readlo?(pos=0)?lenbuf=letbuf_len=Bytes.lengthbufinletlen=matchlenwithSomelen->len|None->buf_len-posiniflen<0||pos<0||pos+len>buf_lentheninvalid_arg"Postgresql.connection#lo_read";wrap_conn(funconn->letread=Stub.lo_readconnlobufposleninifread=-1thensignal_errorconn;read)methodlo_read_balo?(pos=0)?lenbuf=letbuf_len=Bigarray.Array1.dimbufinletlen=matchlenwithSomelen->len|None->buf_len-posiniflen<0||pos<0||pos+len>buf_lentheninvalid_arg"Postgresql.connection#lo_read_ba";wrap_conn(funconn->letread=Stub.lo_read_baconnlobufposleninifread=-1thensignal_errorconn;read)methodlo_seek?(pos=0)?(whence=SEEK_SET)lo=wrap_conn(funconn->ifStub.lo_seekconnloposwhence<0thensignal_errorconn)methodlo_telllo=wrap_conn(funconn->letpos=Stub.lo_tellconnloinifpos=-1thensignal_errorconn;pos)methodlo_closeoid=wrap_conn(funconn->ifStub.lo_closeconnoid=-1thensignal_errorconn)methodlo_unlinkoid=wrap_conn(funconn->letoid=Stub.lo_unlinkconnoidinifoid=-1thensignal_errorconn)(* Escaping *)methodescape_string?pos?lenstr=letpos,len=get_str_pos_len~loc:"escape_string"?pos?lenstrinwrap_conn(funconn->Stub.escape_string_connconnstr~pos~len)methodescape_bytea?pos?lenstr=letpos,len=get_str_pos_len~loc:"escape_bytea"?pos?lenstrinwrap_conn(funconn->Stub.escape_bytea_connconnstr~pos~len)end