This commit is contained in:
parent
aa2ff7b2f0
commit
2f3113f55d
11742 changed files with 1223940 additions and 0 deletions
File diff suppressed because it is too large
Load diff
|
|
@ -0,0 +1,73 @@
|
|||
(* Copyright (C) 2017--2023 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 LGPL-3.0 Linking 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
|
||||
* and the LGPL-3.0 Linking Exception along with this library. If not, see
|
||||
* <http://www.gnu.org/licenses/> and <https://spdx.org>, respectively.
|
||||
*)
|
||||
|
||||
(** PostgreSQL driver for Caqti (bindings).
|
||||
|
||||
This driver is implemented in terms of the postgresql OPAM package which
|
||||
provides bindings for the PostgreSQL C client library.
|
||||
|
||||
It handles URIs of the form
|
||||
{[
|
||||
postgresql://<user>:<password>@<host>:<port>/<rest>
|
||||
]}
|
||||
which are passed verbatim to {!Postgresql.connection}, except as noted
|
||||
below, and URIs of the form [postgresql://<query>] which are first split
|
||||
into [<key> = '<value>'] form.
|
||||
|
||||
In addition to libpq parameters, the following will be interpreted by Caqti
|
||||
and stripped of before passing the URI to libpq:
|
||||
|
||||
- [notice_processing] can be set to [quite] (the default) to suppress
|
||||
notices or to [stderr] to let libpq emit notices to standard error.
|
||||
|
||||
- [use_single_row_mode] can be set to [true] to enable single row mode for
|
||||
queries which may return more than one row. The default is [false]
|
||||
because it is significantly faster. Single row mode is needed if query
|
||||
results may exceed whan can be stored in memory, but consider batching
|
||||
the results with [LIMIT] and [OFFSET] instead for better perfomance.
|
||||
|
||||
The interface provided by this module {e should normally not be used by
|
||||
applications}, but provides access to some PostgreSQL specifics in case they
|
||||
are needed. *)
|
||||
|
||||
(** {1 Error Details}
|
||||
|
||||
The following gives access to diagnostics collected from the PostgreSQL
|
||||
connection and result objects. *)
|
||||
|
||||
type Caqti_error.msg +=
|
||||
| Connect_error_msg of {
|
||||
error: Postgresql.error;
|
||||
(** The exception raised by postgresql-ocaml. *)
|
||||
}
|
||||
(** An exception was raised while attempting to connect to the database. *)
|
||||
|
||||
| Connection_error_msg of {
|
||||
error: Postgresql.error;
|
||||
(** The exception raised by postgresql-ocaml. *)
|
||||
connection_status: Postgresql.connection_status;
|
||||
(** The connection status reported by [PQstatus]. *)
|
||||
}
|
||||
(** An exception was raised while operating on the database connection. *)
|
||||
|
||||
| Result_error_msg of {
|
||||
error_message: string;
|
||||
(** The error message from [PQresultErrorMessage]. *)
|
||||
sqlstate: string;
|
||||
(** The SQLSTATE error field. *)
|
||||
}
|
||||
(** An error was reported on the result from a database operation. *)
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
(rule (copy# ../../shared/postgresql_conv.ml postgresql_conv.ml))
|
||||
|
||||
(library
|
||||
(name caqti_driver_postgresql)
|
||||
(public_name caqti-driver-postgresql)
|
||||
(flags (:standard -alert -caqti_unstable))
|
||||
(library_flags (:standard -linkall))
|
||||
(libraries caqti caqti.platform caqti.platform.unix postgresql))
|
||||
|
||||
(plugin
|
||||
(package caqti-driver-postgresql)
|
||||
(name caqti-driver-postgresql)
|
||||
(libraries caqti-driver-postgresql)
|
||||
(site (caqti plugins)))
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
(test
|
||||
(name test_postgresql)
|
||||
(package caqti-driver-postgresql)
|
||||
(flags (:standard -alert -caqti_unstable))
|
||||
(libraries
|
||||
alcotest
|
||||
caqti
|
||||
caqti.blocking
|
||||
caqti-driver-postgresql
|
||||
testlib
|
||||
testlib_blocking)
|
||||
(deps ../../testsuite/uris.conf)
|
||||
(action
|
||||
(setenv CAQTI_TEST_URIS_FILE ../../testsuite/uris.conf
|
||||
(run %{test}))))
|
||||
|
|
@ -0,0 +1,62 @@
|
|||
(* Copyright (C) 2021--2025 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 LGPL-3.0 Linking 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
|
||||
* and the LGPL-3.0 Linking Exception along with this library. If not, see
|
||||
* <http://www.gnu.org/licenses/> and <https://spdx.org>, respectively.
|
||||
*)
|
||||
|
||||
open Testlib
|
||||
open Testlib_blocking
|
||||
|
||||
let bad_select_req =
|
||||
Caqti_template.Create.(static T.(unit -->! unit) "SELECT not_defined")
|
||||
|
||||
let test_error (module C : Caqti_blocking.CONNECTION) =
|
||||
(match C.find bad_select_req () with
|
||||
| Ok () -> Alcotest.fail "unexpected ok from bad_select"
|
||||
| Error (`Request_failed
|
||||
{msg = Caqti_driver_postgresql.Result_error_msg {sqlstate; _}; _}) ->
|
||||
assert (sqlstate = "42703")
|
||||
| Error err ->
|
||||
Alcotest.failf "unexpected error from bad_select: %a" Caqti_error.pp err)
|
||||
|
||||
let test_cases_on_connection = [
|
||||
"test_error", `Quick, test_error;
|
||||
]
|
||||
|
||||
let mk_test (name, pool) =
|
||||
let pass_conn (name, speed, f) =
|
||||
let f' () =
|
||||
Caqti_blocking.Pool.use (fun c -> Ok (f c)) pool |> function
|
||||
| Ok () -> ()
|
||||
| Error err -> Alcotest.failf "%a" Caqti_error.pp err
|
||||
in
|
||||
(name, speed, f')
|
||||
in
|
||||
let test_cases = List.map pass_conn test_cases_on_connection in
|
||||
(name, test_cases)
|
||||
|
||||
let mk_tests {uris; connect_config = config} =
|
||||
let connect_pool uri =
|
||||
let pool_config = Caqti_pool_config.create ~max_size:1 () in
|
||||
(match Caqti_blocking.connect_pool uri ~pool_config ~config with
|
||||
| Ok pool -> (test_name_of_uri uri, pool)
|
||||
| Error err -> raise (Caqti_error.Exn err))
|
||||
in
|
||||
let is_postgresql uri = Uri.scheme uri = Some "postgresql" in
|
||||
let pools = List.map connect_pool (List.filter is_postgresql uris) in
|
||||
List.map mk_test pools
|
||||
|
||||
let () =
|
||||
Alcotest_cli.run_with_args_dependency "test_postgresql"
|
||||
(common_args ()) mk_tests
|
||||
Loading…
Add table
Add a link
Reference in a new issue