package petrol

  1. Overview
  2. Docs

Provides an E-DSL for specifying SQL queries in OCaml.

type ('ret_ty, 'query_kind) t = ('ret_ty, 'query_kind) query

('ret_ty, 'query_tag) t represents an SQL query that returns values of type 'ret_ty and is a SQL query of kind 'query_kind.

val pp : Stdlib.Format.formatter -> ('ret_ty, 'query_kind) t -> unit

pp fmt q prints the query q in a form that can be parsed by an SQL engine.

type join_op =
  1. | LEFT
    (*

    LEFT join -- keep rows from the left table where the right column is NULL

    *)
  2. | RIGHT
    (*

    RIGHT join -- keep rows from the right table where the right column is NULL

    *)
  3. | INNER
    (*

    INNER -- only keep rows for which both the left and right of the join are present.

    *)

Defines the type of join to be used to combine two tables

type ('a, 'c) where_fun = bool Expr.t -> ('c, 'a) t -> ('c, 'a) t constraint 'a = [< `DELETE | `SELECT | `SELECT_CORE | `UPDATE ]

('a,'c) where_fun defines the type of an SQL function that corresponds to SQL's WHERE clause.

type ('a, 'b, 'c) group_by_fun = 'b Expr.expr_list -> ('c, 'a) t -> ('c, 'a) t constraint 'a = [< `SELECT | `SELECT_CORE ]

('a,'b,'c) group_by_fun defines the type of an SQL function that corresponds to SQL's GROUP BY clause.

type ('a, 'c) having_fun = bool Expr.t -> ('c, 'a) t -> ('c, 'a) t constraint 'a = [< `SELECT | `SELECT_CORE ]

('a,'b,'c) having_fun defines the type of an SQL function that corresponds to SQL's HAVING clause.

type ('a, 'b, 'd, 'c) join_fun = ?op:join_op -> on:bool Expr.t -> ('b, 'd) t -> ('c, 'a) t -> ('c, 'a) t constraint 'a = [< `SELECT_CORE ] constraint 'd = [< `SELECT_CORE | `SELECT ]

('a,'b,'c,'d) join_fun defines the type of an SQL function that corresponds to SQL's JOIN clause.

type ('a, 'b, 'c) on_err_fun = 'b -> ('c, 'a) t -> ('c, 'a) t constraint 'a = [> `INSERT | `UPDATE ] constraint 'b = [< `ABORT | `FAIL | `IGNORE | `REPLACE | `ROLLBACK ]

('a,'b,'c) having_fun defines the type of an SQL function that corresponds to SQL's HAVING clause ON ERR.

val select : 'a Expr.expr_list -> from:table_name -> ('a, [> `SELECT_CORE ]) t

select fields ~from corresponds to the SQL SELECT {fields} FROM {from}.

val update : table:table_name -> set:Expr.wrapped_assign list -> (unit, [> `UPDATE ]) t

update ~table ~set corresponds to the SQL UPDATE {set} FROM {table}.

val insert : table:table_name -> values:Expr.wrapped_assign list -> (unit, [> `INSERT ]) t

insert ~table ~values corresponds to the SQL INSERT {values} INTO {table}.

val delete : from:table_name -> (unit, [> `DELETE ]) t

delete ~from corresponds to the SQL DELETE FROM {from}.

val where : ([< `DELETE | `SELECT | `SELECT_CORE | `UPDATE ], 'c) where_fun

where by expr corresponds to the SQL {expr} WHERE {by}.

val group_by : ([< `SELECT | `SELECT_CORE ], 'b, 'c) group_by_fun

group_by fields expr corresponds to the SQL {expr} GROUP BY {fields}.

val having : ([< `SELECT | `SELECT_CORE ], 'c) having_fun

having fields expr corresponds to the SQL {expr} HAVING {fields}.

val join : ([ `SELECT_CORE ], 'b, [< `SELECT_CORE | `SELECT ], 'c) join_fun

join ?op ~on oexpr expr corresponds to the SQL {expr} {op} JOIN {oexpr} ON {expr}.

The ordering of the last two arguments has been chosen to allow easily piping this with another SQL query.

val on_err : [ `ABORT | `FAIL | `IGNORE | `REPLACE | `ROLLBACK ] -> (unit, 'a) t -> (unit, 'a) t

on_err err expr corresponds to the SQL {expr} ON ERR {err}.

val limit : int Expr.t -> ('a, [< `SELECT | `SELECT_CORE ]) t -> ('a, [> `SELECT ]) t

limit count expr corresponds to the SQL {expr} LIMIT {count}.

val offset : int Expr.t -> ('a, [< `SELECT | `SELECT_CORE ]) t -> ('a, [> `SELECT ]) t

offset count expr corresponds to the SQL {expr} OFFSET {fields}.

val order_by : ?direction:[ `ASC | `DESC ] -> 'a Expr.t -> ('b, [< `SELECT | `SELECT_CORE ]) t -> ('b, [> `SELECT ]) t

order_by ?direction fields expr corresponds to the SQL {expr} ORDER BY {direction} {fields}.

val order_by_ : ?direction:[ `ASC | `DESC ] -> 'a Expr.expr_list -> ('b, [< `SELECT | `SELECT_CORE ]) t -> ('b, [> `SELECT ]) t

order_by_ ?direction fields expr corresponds to the SQL {expr} ORDER BY {direction} {fields}. (In contrast to Petrol.Query.order_by, this function allows passing a list of elements to be ordered by)

OCaml

Innovation. Community. Security.