123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109(* Copyright (C) 2014--2019 Petter A. Urkedal <paurkedal@gmail.com>
*
* 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 3 of the License, or (at your
* option) any later version, with the OCaml static compilation exception.
*
* 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, see <http://www.gnu.org/licenses/>.
*)openPrintfletdynload_library=ref@@funlib->Error(sprintf"Neither %s nor the dynamic linker is linked into the \
application."lib)letdefine_loaderf=dynload_library:=fletdrivers=Hashtbl.create11letdefine_unix_driverschemep=Hashtbl.adddriversschemepletscheme_driver_name=function|"postgres"|"postgresql"->"caqti-driver-postgresql"|s->"caqti-driver-"^sletload_driver_functor~urischeme=(tryOk(Hashtbl.finddriversscheme)with|Not_found->(match!dynload_library(scheme_driver_namescheme)with|Ok()->(tryOk(Hashtbl.finddriversscheme)with|Not_found->letmsg=sprintf"The driver for %s did not register itself \
after apparently loading."schemeinError(Caqti_error.load_failed~uri(Caqti_error.Msgmsg)))|Errormsg->Error(Caqti_error.load_failed~uri(Caqti_error.Msgmsg))))moduleMake_unix(System:Caqti_driver_sig.System_unix)=structopenSystemmoduletypeDRIVER=Caqti_driver_sig.Swithtype'afuture:='aSystem.futureandtype('a,'err)stream:=('a,'err)System.Stream.tletdrivers:(string,(moduleDRIVER))Hashtbl.t=Hashtbl.create11letload_driveruri=(matchUri.schemeuriwith|None->letmsg="Missing URI scheme."inError(Caqti_error.load_rejected~uri(Caqti_error.Msgmsg))|Somescheme->(tryOk(Hashtbl.finddriversscheme)with|Not_found->(matchload_driver_functor~urischemewith|Okmake_driver->letmoduleMake_driver=(valmake_driver:Caqti_driver_sig.Of_system_unix)inletmoduleDriver=Make_driver(System)inletdriver=(moduleDriver:DRIVER)inHashtbl.adddriversschemedriver;Okdriver|Error_asr->r)))moduletypeCONNECTION_BASE=Caqti_connection_sig.Basewithtype'afuture:='aSystem.futureandtype('a,'err)stream:=('a,'err)System.Stream.tmoduletypeCONNECTION=Caqti_connection_sig.Swithtype'afuture:='aSystem.futureandtype('a,'err)stream:=('a,'err)System.Stream.ttypeconnection=(moduleCONNECTION)letconnecturi:((moduleCONNECTION),_)resultfuture=(matchload_driveruriwith|Okdriver->letmoduleDriver=(valdriver)inDriver.connecturi|Errorerr->return(Errorerr))modulePool=Caqti_pool.Make(System)moduleStream=System.Streamletconnect_pool?max_sizeuri=(matchload_driveruriwith|Okdriver->letmoduleDriver=(valdriver)inletconnect()=Driver.connecturiinletdisconnect(moduleDb:CONNECTION)=Db.disconnect()inletvalidate(moduleDb:CONNECTION)=Db.validate()inletcheck(moduleDb:CONNECTION)=Db.checkinletdi=Driver.driver_infoinletmax_size=ifnot(Caqti_driver_info.(can_concurdi&&can_pooldi))thenSome1elsemax_sizeinOk(Pool.create?max_size~validate~checkconnectdisconnect)|Errorerr->Errorerr)end