package petrol

  1. Overview
  2. Docs

Provides an SQL E-DSL for writing well-typed SQL expressions.

type 'a t = 'a Expr.t

'a t represents an SQL expression that produces a value corresponding to the type 'a.

type 'a expr_list = 'a Expr.expr_list =
  1. | [] : unit expr_list
  2. | :: : ('a t * 'b expr_list) -> ('a * 'b) expr_list

Represents a heterogeneous sequence of SQL expressions.

Note Provided you have opened the Expr module, you can use List syntax to construct such lists:

open Petrol.Sqlite3
    Expr.[i 1; bl false]
(* - : (int * (bool * ())) Expr.expr_list *)
type wrapped_assign = Expr.wrapped_assign

An opaque wrapper that represents an assignment of a value to a particular field in a table.

Constants

The following functions define constant value expressions.

Note For each type, there are two flavours of constant expression: variable and static.

The key difference between the two is in terms of how they are represented in the final SQL query - in particular, variable constant expressions are encoded as holes (?) in the query, to which a constant value is supplied, whereas static constant expressions are encoded directly in the query string.

As Petrol functions cache the construction of SQL queries by their final string representation, you should prefer the dynamic form if you expect the constant value to change frequently - for example, if it is a constant value that you are receiving from elsewhere. Use the static form if you know that the value doesn't change and will always be the same.

val i : int -> int t

i v returns an expression that evaluates to the integer value v.

val f : float -> float t

f v returns an expression that evaluates to the real value v.

val s : string -> string t

s v returns an expression that evaluates to the string value v.

val bl : bool -> bool t

bl v returns an expression that evaluates to the bool value v.

val vl : ty:'a Type.t -> 'a -> 'a t

vl ~ty value returns an expression that evaluates to the value value with type ty.

val i_stat : int -> int t

i_stat v returns a static expression that evaluates to the integer value v.

val f_stat : float -> float t

f_stat v returns a static expression that evaluates to the real value v.

val s_stat : string -> string t

s_stat v returns a static expression that evaluates to the string value v.

val true_ : bool t

true_ represents the SQL constant TRUE.

val false_ : bool t

false_ represents the SQL constant FALSE.

val vl_stat : ty:'a Type.t -> 'a -> 'a t

vl_stat ~ty value returns a static expression that evaluates to the value value with type ty.

Book-keeping: Types, Naming, Nulls

val as_ : 'a t -> name:string -> 'a t * 'a t

as_ exp ~name returns a tuple (exp',exp'_ref) where exp' is the SQL expression exp AS name that names exp as name, and exp'_ref is simply name.

val nullable : 'a t -> 'a option t

nullable e encodes the fact that the expression e may return NULL.

val is_not_null : 'a t -> bool t

is_not_null e constructs an SQL expression that is TRUE iff the expression e is not NULL and FALSE otherwise.

val is_null : 'a t -> bool t

is_null e constructs an SQL expression that is TRUE iff the expression e is NULL and FALSE otherwise.

val coerce : 'a t -> 'b Type.t -> 'b t

coerce expr ty coerces expression expr to the type ty. This coercion is not checked, so make sure you know what you're doing or it could fail at runtime.

val (:=) : 'a t -> 'a t -> wrapped_assign

v := expr returns an SQL expression that can be used with an update or insert clause to change the values in the database.

val unset : 'a t -> wrapped_assign

unset v returns an SQL expression that can be used with an update query to set a field to NULL in the database.

Operators

val (+) : int t -> int t -> int t
val (-) : int t -> int t -> int t
val (*) : int t -> int t -> int t
val (/) : int t -> int t -> int t
val (+.) : float t -> float t -> float t
val (-.) : float t -> float t -> float t
val (*.) : float t -> float t -> float t
val (/.) : float t -> float t -> float t
val (=) : 'a t -> 'a t -> bool t
val (<>) : 'a t -> 'a t -> bool t
val (<=) : 'a t -> 'a t -> bool t
val (<) : 'a t -> 'a t -> bool t
val (>) : 'a t -> 'a t -> bool t
val (>=) : 'a t -> 'a t -> bool t
val (&&) : bool t -> bool t -> bool t
val (||) : bool t -> bool t -> bool t
val not : bool t -> bool t
val exists : ('a, [> `SELECT | `SELECT_CORE ]) query -> bool t
val in_ : 'a t -> ('a * unit, [> `SELECT | `SELECT_CORE ]) query -> bool t
val between : lower:'a t -> upper:'a t -> 'a t -> bool t
val not_between : lower:'a t -> upper:'a t -> 'a t -> bool t
val between_symmetric : lower:'a t -> upper:'a t -> 'a t -> bool t
val not_between_symmetric : lower:'a t -> upper:'a t -> 'a t -> bool t
val is_distinct_from : 'a t -> 'a t -> bool t
val is_not_distinct_from : 'a t -> 'a t -> bool t
val is_true : bool t -> bool t
val is_not_true : bool t -> bool t
val is_false : bool t -> bool t
val is_not_false : bool t -> bool t
val is_unknown : bool t -> bool t
val is_not_unknown : bool t -> bool t

Arithmetic Functions

val ceil : float t -> float t
val floor : float t -> float t
val round : float t -> float t
val trunc : float t -> float t
val ceili : int t -> int t
val floori : int t -> int t
val roundi : int t -> int t
val trunci : int t -> int t
val ceil_gen : ty:'a Type.Numeric.t -> 'a t -> 'a t
val floor_gen : ty:'a Type.Numeric.t -> 'a t -> 'a t
val round_gen : ty:'a Type.Numeric.t -> 'a t -> 'a t
val trunc_gen : ty:'a Type.Numeric.t -> 'a t -> 'a t
val pi : float t
val sqrt : float t -> float t
val degrees : float t -> float t
val radians : float t -> float t
val exp : float t -> float t
val ln : float t -> float t
val log10 : float t -> float t
val log : base:float t -> float t -> float t
val power : float t -> float t -> float t
val poweri : int t -> int t -> int t
val power_gen : ty:'a Type.Numeric.t -> 'a t -> 'a t -> 'a t
val cos : float t -> float t
val cosd : float t -> float t
val acos : float t -> float t
val acosd : float t -> float t
val cosh : float t -> float t
val acosh : float t -> float t
val sin : float t -> float t
val sind : float t -> float t
val asin : float t -> float t
val asind : float t -> float t
val sinh : float t -> float t
val asinh : float t -> float t
val tan : float t -> float t
val tand : float t -> float t
val atan : float t -> float t
val atand : float t -> float t
val atan2 : float t -> float t
val atan2d : float t -> float t
val tanh : float t -> float t
val atanh : float t -> float t
val cot : float t -> float t
val cotd : float t -> float t
val factorial : int t -> int t
val factorial_gen : ty:'a Type.Numeric.integral -> 'a t -> 'a t
val gcd : int t -> int t -> int t
val gcd_gen : ty:'a Type.Numeric.integral -> 'a t -> 'a t -> 'a t
val lcm : int t -> int t -> int t
val lcm_gen : ty:'a Type.Numeric.integral -> 'a t -> 'a t -> 'a t
val abs : int t -> int t
val absf : float t -> float t
val abs_gen : 'a Type.Numeric.t -> 'a t -> 'a t

String functiosn

val concat : string t list -> string t
val concat_ws : sep_by:string t -> string t list -> string t
val like : string t -> pat:string t -> bool t
val lower : string t -> string t
val upper : string t -> string t
val char_length : string t -> int t
val length : string t -> int t
val substring : ?from:int t -> ?for_:int t -> string t -> string t
val replace : from:string t -> to_:string t -> string t -> string t
val reverse : string t -> string t
val starts_with : prefix:string t -> string t -> bool t
val similar_to : pat:string t -> string t -> bool t

Aggregate Functions

val count : ?distinct:bool -> 'a expr_list -> int t
val count_star : int t
val coalesce : 'a t list -> 'a t
val max : ?distinct:bool -> int t -> int t
val maxf : ?distinct:bool -> float t -> float t
val max_gen : ?distinct:bool -> 'a Type.Numeric.t -> 'a t -> 'a t
val min : ?distinct:bool -> int t -> int t
val minf : ?distinct:bool -> float t -> float t
val min_gen : ?distinct:bool -> 'a Type.Numeric.t -> 'a t -> 'a t
val sum : ?distinct:bool -> int t -> int t
val sumf : ?distinct:bool -> float t -> float t
val sum_gen : ?distinct:bool -> 'a Type.Numeric.t -> 'a t -> 'a t
val greatest : int t list -> int t
val greatestf : float t list -> float t
val greatest_gen : ty:'a Type.Numeric.t -> 'a t list -> 'a t
val least : int t list -> int t
val leastf : float t list -> float t
val least_gen : ty:'a Type.Numeric.t -> 'a t list -> 'a t
val random : float t