package petrol

  1. Overview
  2. Docs

Module Postgres.ExprSource

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

Sourcetype 'a t = 'a Expr.t

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

Sourcetype '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 *)
Sourcetype 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.

Sourceval i : int -> int t

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

Sourceval f : float -> float t

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

Sourceval s : string -> string t

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

Sourceval bl : bool -> bool t

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

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

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

Sourceval i_stat : int -> int t

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

Sourceval f_stat : float -> float t

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

Sourceval s_stat : string -> string t

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

Sourceval true_ : bool t

true_ represents the SQL constant TRUE.

Sourceval false_ : bool t

false_ represents the SQL constant FALSE.

Sourceval 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

Sourceval 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.

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

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

Sourceval 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.

Sourceval 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.

Sourceval 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.

Sourceval (:=) : '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.

Sourceval 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

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

Arithmetic Functions

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

String functiosn

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

Aggregate Functions

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

Innovation. Community. Security.