Repodb.Expr
repodb · API reference
type json_path_elem =
| JsonKey of string
| JsonIdx of intType-indexed SQL expressions for filters, projections, updates, and inserts.
An ('a t) is an SQL expression that evaluates to an OCaml value of type 'a. Expressions are used by Query, JSON helpers, and update/insert builders. Constructors preserve enough type information to catch many query mistakes before SQL is generated.
Build expressions from fields and literals:
let active_users =
Query.from User.table
|> Query.where Expr.(column User.active = bool true)
|> Query.where Expr.(column User.age >= int 18)
|> Query.order_by Expr.(lower (column User.email))The infix operators in this module intentionally shadow Stdlib comparison and arithmetic operators. Open Expr locally, as above, instead of at the top of a large module.
to_sql_params renders parameterized SQL fragments and returns the collected Driver.Value.t parameters. to_sql is convenient for DDL tests and generated SQL inspection; prefer parameterized rendering when user input is involved.
JSON functions render backend-specific syntax for PostgreSQL, SQLite, and MySQL. Some operators are not available everywhere; unsupported features raise Error.Unsupported_dialect_feature with a suggested alternative when one exists.
raw and subquery are escape hatches. They are not parameterized and should be limited to trusted SQL snippets, generated SQL, or tests.
type json_path = json_path_elem listtype json_extract_mode =
| AsJson
| AsTexttype 'a t =
| Lit : 'a * 'a Types.t -> 'a t
| Column : ('record, 'a) Field.t -> 'a t
| Null : 'a Types.t -> 'a option t
| Some : 'a t -> 'a option t
| Binop : string * 'a t * 'b t * 'c Types.t -> 'c t
| Unop : string * 'a t * 'b Types.t -> 'b t
| Func : string * wrapped list * 'a Types.t -> 'a t
| Raw : string -> 'a t
| Param : int * 'a Types.t -> 'a t
| Cast : 'a t * 'b Types.t -> 'b t
| Case : (bool t * 'a t) list * 'a t option * 'a Types.t -> 'a t
| Between : 'a t * 'a t * 'a t -> bool t
| InList : 'a t * 'a t list -> bool t
| Subquery : string -> 'a t
| JsonGet : ('r, string) Field.t * json_path * json_extract_mode -> string t
| JsonContains : ('r, string) Field.t * string -> bool t
| JsonContainedBy : ('r, string) Field.t * string -> bool t
| JsonHasKey : ('r, string) Field.t * string -> bool t
| JsonHasPath : ('r, string) Field.t * json_path -> bool t
| JsonHasAnyKeys : ('r, string) Field.t * string list -> bool t
| JsonHasAllKeys : ('r, string) Field.t * string list -> bool t
| JsonArrayLength : ('r, string) Field.t * json_path option -> int t
| JsonTypeof : ('r, string) Field.t * json_path option -> string tand wrapped =
| Wrapped : 'a t -> wrappedval int : int -> int tval int32 : int32 -> int32 tval int64 : int64 -> int64 tval float : float -> float tval string : string -> string tval bool : bool -> bool tval null : 'a Types.t -> 'a option tval some : 'a t -> 'a option tval int_opt : int Option.t -> int option tval int32_opt : int32 Option.t -> int32 option tval int64_opt : int64 Option.t -> int64 option tval float_opt : float Option.t -> float option tval string_opt : string Option.t -> string option tval bool_opt : bool Option.t -> bool option tval column : ('a, 'b) Field.t -> 'b tval raw : string -> 'a tval param : int -> 'a Types.t -> 'a tval cast : 'a t -> 'b Types.t -> 'b tval subquery : string -> 'a tval between : 'a t -> 'a t -> 'a t -> bool tval in_list : 'a t -> 'a t list -> bool tval case : (bool t * 'a t) list -> else_:'a t -> 'a Types.t -> 'a tval case_no_else : (bool t * 'a t) list -> 'a Types.t -> 'a tval coalesce : 'a t list -> 'b Types.t -> 'b tval nullif : 'a t -> 'b t -> 'c Types.t -> 'c option tval greatest : 'a t list -> 'b Types.t -> 'b tval least : 'a t list -> 'b Types.t -> 'b tval count : 'a t -> int64 tval count_distinct : 'a t -> int64 tval count_all : int64 tval sum : 'a t -> int64 tval avg : 'a t -> float tval max_ : 'a t -> 'b Types.t -> 'b tval min_ : 'a t -> 'b Types.t -> 'b tval lower : 'a t -> string tval upper : 'a t -> string tval length : 'a t -> int tval trim : 'a t -> string tval ltrim : 'a t -> string tval rtrim : 'a t -> string tval concat : 'a t list -> string tval concat_ws : string -> 'a t list -> string tval substring : 'a t -> from:int -> for_:int -> string tval replace : 'a t -> from:string -> to_:string -> string tval left : 'a t -> int -> string tval right : 'a t -> int -> string tval now : unit -> Ptime.t tval current_date : Ptime.date tval current_timestamp : Ptime.t tval date_part : string -> 'a t -> float tval extract : string -> 'a t -> float tval abs_ : 'a t -> int tval ceil : 'a t -> float tval floor : 'a t -> float tval round : 'a t -> float tval sqrt : 'a t -> float tval power : 'a t -> 'b t -> float tval random : float tval gen_random_uuid : Uuidm.t ttype wrapped_expr =
| W : 'a t -> wrapped_exprtype 'a expr_list =
| [] : unit expr_list
| :: : 'a t * 'b expr_list -> ('a * 'b) expr_listval expr_list_to_list : 'a. 'a expr_list -> wrapped_expr listval escape_string_buf : Buffer.t -> string -> unitval escape_string : string -> stringval lit_to_sql_buf : 'a. Buffer.t -> 'a -> 'a Types.t -> unitval json_path_to_pg : Buffer.t -> json_path_elem list -> unitval json_path_to_sqlite : Buffer.t -> json_path_elem list -> unitval json_path_to_mysql : Buffer.t -> json_path_elem list -> unittype param_ctx = {
dialect : Driver.dialect;
mutable next_idx : int;
params : Driver.Value.t Queue.t;
}val create_param_ctx : Driver.dialect -> param_ctxval add_param : param_ctx -> Driver.Value.t -> stringval unsupported_json_contains : dialect:Driver.dialect -> 'aval unsupported_json_feature : feature:string -> dialect:Driver.dialect -> 'aval json_path_writer :
Driver.dialect ->
Buffer.t ->
json_path_elem list ->
unitval json_contains_path_sql :
Buffer.t ->
('a, 'b) Field.t ->
string ->
json_path_elem list list ->
unitval emit_json_get :
dialect:Driver.dialect ->
Buffer.t ->
('a, 'b) Field.t ->
json_path_elem list ->
json_extract_mode ->
unitval emit_json_contains :
dialect:Driver.dialect ->
Buffer.t ->
('a, 'b) Field.t ->
string ->
unitval emit_json_contains_params :
dialect:Driver.dialect ->
ctx:param_ctx ->
Buffer.t ->
('a, 'b) Field.t ->
string ->
unitval emit_json_contained_by :
dialect:Driver.dialect ->
Buffer.t ->
('a, 'b) Field.t ->
string ->
unitval emit_json_contained_by_params :
dialect:Driver.dialect ->
ctx:param_ctx ->
Buffer.t ->
('a, 'b) Field.t ->
string ->
unitval emit_json_has_key :
dialect:Driver.dialect ->
Buffer.t ->
('a, 'b) Field.t ->
string ->
unitval emit_json_has_key_params :
dialect:Driver.dialect ->
ctx:param_ctx ->
Buffer.t ->
('a, 'b) Field.t ->
string ->
unitval emit_json_has_path :
dialect:Driver.dialect ->
Buffer.t ->
('a, 'b) Field.t ->
json_path_elem list ->
unitval emit_json_has_keys :
dialect:Driver.dialect ->
Buffer.t ->
('a, 'b) Field.t ->
string ->
string list ->
unitval emit_json_has_keys_params :
dialect:Driver.dialect ->
ctx:param_ctx ->
Buffer.t ->
('a, 'b) Field.t ->
string ->
string list ->
unitval emit_json_array_length :
dialect:Driver.dialect ->
Buffer.t ->
('a, 'b) Field.t ->
json_path_elem list option ->
unitval emit_json_typeof :
dialect:Driver.dialect ->
Buffer.t ->
('a, 'b) Field.t ->
json_path_elem list option ->
unitval to_sql_buf : 'a. dialect:Driver.dialect -> Buffer.t -> 'a t -> unitval to_sql : ?dialect:Driver.dialect -> 'a t -> stringval lit_to_value : 'a. 'a -> 'a Types.t -> Driver.Value.tval to_sql_params_buf : 'a. ctx:param_ctx -> Buffer.t -> 'a t -> unitval to_sql_params :
?dialect:Driver.dialect ->
'a t ->
string * Driver.Value.t arrayval wrapped_to_sql_params : ctx:param_ctx -> Buffer.t -> wrapped_expr -> unitval lit_to_sql : 'a. 'a -> 'a Types.t -> stringval eq : 'a t -> 'b t -> bool tval neq : 'a t -> 'b t -> bool tval lt : 'a t -> 'b t -> bool tval gt : 'a t -> 'b t -> bool tval lte : 'a t -> 'b t -> bool tval gte : 'a t -> 'b t -> bool tval and_ : 'a t -> 'b t -> bool tval or_ : 'a t -> 'b t -> bool tval not_ : 'a t -> bool tval is_null : 'a t -> bool tval is_not_null : 'a t -> bool tval add : 'a t -> 'b t -> int tval sub : 'a t -> 'b t -> int tval mul : 'a t -> 'b t -> int tval div : 'a t -> 'b t -> int tval mod_ : 'a t -> 'b t -> int tval addf : 'a t -> 'b t -> float tval subf : 'a t -> 'b t -> float tval mulf : 'a t -> 'b t -> float tval divf : 'a t -> 'b t -> float tval not_in_list : 'a t -> 'a t list -> bool tval like : 'a t -> string -> bool tval ilike : 'a t -> string -> bool tval similar_to : 'a t -> string -> bool tval regexp : 'a t -> string -> bool tval regexp_i : 'a t -> string -> bool tval json_key : string -> json_path_elemval json_idx : int -> json_path_elemval json_path : string list -> json_path_elem listval json_get :
('a, string) Field.t ->
json_path ->
json_extract_mode ->
string tval json_contains : ('a, string) Field.t -> string -> bool tval json_contained_by : ('a, string) Field.t -> string -> bool tval json_has_key : ('a, string) Field.t -> string -> bool tval json_has_path : ('a, string) Field.t -> json_path -> bool tval json_has_any_keys : ('a, string) Field.t -> string list -> bool tval json_has_all_keys : ('a, string) Field.t -> string list -> bool tval json_array_length : ('a, string) Field.t -> json_path option -> int tval json_typeof : ('a, string) Field.t -> json_path option -> string tval (=) : 'a t -> 'b t -> bool tval (<>) : 'a t -> 'b t -> bool tval (<) : 'a t -> 'b t -> bool tval (>) : 'a t -> 'b t -> bool tval (<=) : 'a t -> 'b t -> bool tval (>=) : 'a t -> 'b t -> bool tval (&&) : 'a t -> 'b t -> bool tval (||) : 'a t -> 'b t -> bool tval (+) : 'a t -> 'b t -> int tval (-) : 'a t -> 'b t -> int tval (*) : 'a t -> 'b t -> int tval (/) : 'a t -> 'b t -> int tval (mod) : 'a t -> 'b t -> int tval (+.) : 'a t -> 'b t -> float tval (-.) : 'a t -> 'b t -> float tval (*.) : 'a t -> 'b t -> float tval (/.) : 'a t -> 'b t -> float t