summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--bin/mainlite.ml26
-rw-r--r--litedb/query.ml16
2 files changed, 29 insertions, 13 deletions
diff --git a/bin/mainlite.ml b/bin/mainlite.ml
index f288891..400f113 100644
--- a/bin/mainlite.ml
+++ b/bin/mainlite.ml
@@ -32,19 +32,19 @@ let () =
Uri.make ~scheme:"sqlite3" ~path:"/home/y/code/ocaml/combattant/bulkdata/blog.db" ()
in
(* Create connection pool with initialization function *)
- (* let connect_pool ~sw ~stdenv uri =
- Caqti_eio_unix.connect_pool
- ~sw
- ~stdenv
- ~post_connect:(fun conn ->
- (* Initialize each connection with SQLite performance pragmas *)
- match Query.init_connection conn with
- | Ok () -> Ok ()
- | Error err -> Error err)
- uri
- in
- match connect_pool ~sw ~stdenv:(env :> Caqti_eio.stdenv) db_uri with *)
- match Caqti_eio_unix.connect_pool ~sw ~stdenv:(env :> Caqti_eio.stdenv) db_uri with
+ let connect_pool ~sw ~stdenv uri =
+ Caqti_eio_unix.connect_pool
+ ~sw
+ ~stdenv
+ ~post_connect:(fun conn ->
+ (* Initialize each connection with SQLite performance pragmas *)
+ match Litedb.Query.init_connection conn with
+ | Ok () -> Ok ()
+ | Error err -> Error err)
+ uri
+ in
+ match connect_pool ~sw ~stdenv:(env :> Caqti_eio.stdenv) db_uri with
+ (* match Caqti_eio_unix.connect_pool ~sw ~stdenv:(env :> Caqti_eio.stdenv) db_uri with *)
| Ok pool ->
let server = Server.create ~config (request_handler ~db_pool:pool) in
ignore @@ Server.Command.start ~sw env server
diff --git a/litedb/query.ml b/litedb/query.ml
index 61616b4..37530db 100644
--- a/litedb/query.ml
+++ b/litedb/query.ml
@@ -103,3 +103,19 @@ let sqlite_pragmas = [
"PRAGMA synchronous = NORMAL";
"PRAGMA mmap_size = 30000000000";
]
+
+(* Example of how to execute raw SQL with Caqti *)
+let init_connection conn =
+ let module C = (val conn : Rapper_helper.CONNECTION) in
+ (* Create a request: unit input -> unit output using Caqti infix operators *)
+ let pragma_req sql =
+ let open Caqti_request.Infix in
+ let open Caqti_type in
+ (unit ->. unit) sql
+ in
+ (* Execute each pragma *)
+ List.fold_left (fun acc sql ->
+ match acc with
+ | Error e -> Error e
+ | Ok () -> C.exec (pragma_req sql) ()
+ ) (Ok ()) sqlite_pragmas