123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687(* Copyright (C) 2017 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/>.
*)openPrintfletdrivers=Hashtbl.create11letdefine_driverschemep=Hashtbl.adddriversschemepletload_driver_functor~urischeme=(tryOk(Hashtbl.finddriversscheme)with|Not_found->(match!Caqti_connect.dynload_library("caqti-driver-"^scheme^".v1")[@ocaml.warning"-3"]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(System:Caqti1_system_sig.S)=structopenSystemmoduletypeDRIVER=Caqti1_driver_sig.Swithtype'aio:='aSystem.ioletdrivers:(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|Okv1_functor->letmoduleF=(valv1_functor:Caqti1_driver_sig.F)inletmoduleDriver=F(System)inletdriver=(moduleDriver:DRIVER)inHashtbl.adddriversschemedriver;Okdriver|Error_asr->r)))moduletypeCONNECTION=Caqti1_sigs.CONNECTIONwithtype'aio='aSystem.ioletconnecturi:(moduleCONNECTION)System.io=(matchload_driveruriwith|Okdriver->letmoduleDriver=(valdriver)inDriver.connecturi>>=funclient->letmoduleClient=(valclient)inreturn(moduleClient:CONNECTION)|Errorerr->letmsg=Caqti_error.showerrin(matchUri.schemeuriwith|None->fail(Caqti1_plugin.Plugin_missing("?",msg))|Somescheme->fail(Caqti1_plugin.Plugin_missing("caqti-driver-"^scheme^".v1",msg))))modulePool=Caqti1_pool.Make(System)letconnect_pool?max_sizeuri:(moduleCONNECTION)Pool.t=letconnect()=connecturiinletdisconnect(moduleConn:CONNECTION)=Conn.disconnect()inletvalidate(moduleConn:CONNECTION)=Conn.validate()inletcheck(moduleConn:CONNECTION)=Conn.checkinPool.create?max_size~validate~checkconnectdisconnectend