package fun-sql

  1. Overview
  2. Docs
Legend:
Library
Module
Module type
Parameter
Class
Class type
type db
type arg
type _ ret
val sql : ('a, Format.formatter, unit, string) format4 -> 'a

Helper to construct SQL query strings using placeholders.

exception Bad_migration of string
val migrate : db -> string -> unit

migrate db dir applies the SQL migration scripts in dir on the given database db, keeping track of those that have already been applied.

To apply the migrations in the correct order, the migration scripts must be given filenames that are sorted in lexicographical order of the desired migration order, e.g. 0000_0001_init.sql will be applied before 0000_0002_sec.sql, and so on.

Note that this uses exec_script internally, which means the migration scripts must not have trailing semicolons either.

Any files with extensions other than .sql are ignored.

  • raises Bad_migration

    an error occurs during applying the migrations.

val transaction : db -> (unit -> 'r) -> 'r

transaction db f runs f () inside a transaction in the db. If the operation succeeds, it commits the transaction and returns its result. If it fails with an exception, it rolls back the transaction and re-raises the exception.

Helpers to deal with resultset sequences

exception More_than_one

Thrown if we are expecting at most one result but get more.

val only : 'a Seq.t -> 'a

only seq is the first and only element of seq. This is a convenience function because all queries return seqs but sometimes we want only a single item, otherwise it should be an error.

Use this in preference to calculating the length of the seq, which would force the entire data structure.

val optional : 'a Seq.t -> 'a option

optional seq is Some a if a is the first and only element of seq, or None if seq is empty.

OCaml

Innovation. Community. Security.