package caqti
Install
dune-project
Dependency
Authors
Maintainers
Sources
sha256=3ceea06ba0e8e8bcab0386b5817b68cb30fce457eaa25ada0182891d66f6a0b9
sha512=e01f546a45b04cafd5fa262e9228e5a1aabd5e0942059c073a4181c928450baf3d990cc59ffb1a33dff445b46a0e218224a23dca96e8d24cdd0ab793d08e4538
doc/caqti.classic/Caqti_query/index.html
Module Caqti_querySource
Intermediate query string representation.
This module provides a common representation of database query strings. This module can be used directly to construct queries dynamically, or indirectly via the parser, as may be more convenient when the query string is known at compile-time. In the latter case, the input string is typically very similar to the output string. In either case the intermediate representation serve to unify the syntax across database systems and to provide additional functionality.
When using this module directly, it provides:
- flexible, pure, and efficient construction (
S,L), - uniform index-based parameter references (
P), - expansion of fragments provided by an environment function (
E), and - safe embedding of values in queries (
V,Q).
Construction
type t = | L of string| V : 'a Caqti_type.Field.t * 'a -> t(*
*)V (t, v)translates to a parameter of typetbound to the valuev. That is, the query string will contain a parameter reference which does not conflict with anyPnodes and bindvto the corresponding parameter each time the query is executed. This allows taking advantage of driver-dependent serialization and escaping mechanisms to safely send values to the database server.| Q of string(*
*)Q scorresponds to a quoted string literal. This is passed as part of the query string if a suitable quoting function is available in the client library, otherwise it is equivalent toV(Caqti_type.Field.t.String, s).| P of int(*
*)P irefers to parameter numberi, counting from 0, so that e.g.P 0translates to"$1"for PostgreSQL and"?1"for SQLite3.| E of string(*
*)E namewill be replaced by the fragment returned by an environment lookup function, as passed directly toexpandor indirectly through the?envargument found in higher-level functions. An error will be issued for any remainingE-nodes in the final translation to a query string.| S of t list(*
*)S fragsis the concatenation offrags. Apart from combining different kinds of nodes, this constructor can be nested according to the flow of the generating code.| Annot of Caqti.Template.Query.Private.Annot.t * t(*This constructor is private.
*)
t is an intermediate representation of a query string to be send to a database, possibly combined with some hidden parameters used to safely embed values. Apart from embedding values, this representation provides indexed parameter references, independent of the target database system. For databases which use linear parameter references (like ? for MariaDB), the driver will reshuffle, elide, and duplicate parameters as needed.
Please note that additional constructors may be added to this type across minor releases.
concat sep frags is frags interfixed with sep if frags is non-empty, and the empty string if frags is empty.
Embedding Values
The following are shortcuts for combining V with some of the field types. The values will be passed as hidden parameters.
val bool : bool -> tval int : int -> tval float : float -> tval string : string -> tval octets : string -> tval ptime_span : Ptime.span -> tval const_fields : 'a Caqti_type.t -> 'a -> t listconst_fields t x returns a list of fragments corresponding to the single-field projections of the value x as described by the type descriptor t. Each element of the returned list will be either a V-fragment containing the projected value, or the L["NULL"] fragment if the projection is None.
The result can be turned into a comma-separated list with concat, except values of unitary types, i.e. types having no fields, may require special care.
Normalization and Equality
val hash : t -> intA hash function compatible with equal. The hash function may change across minor versions and may depend on architecture.
Parsing, Expansion, and Printing
val pp : Format.formatter -> t -> unitpp ppf q prints a human-readable representation of q on ppf. The printed string is not suitable for sending to an SQL database; doing so may lead to an SQL injection vulnerability.
val show : t -> stringshow q is the same human-readable representation of q as printed by pp. The returned string is not suitable for sending to an SQL database; doing so may lead to an SQL injection vulnerability.
A description of the error caused during expand if the environment lookup function returns an invalid result or raises Not_found for a variable when the expansion is final.
Prints an informative error.
exception Expand_error of expand_errorThe exception raised by expand when there are issues expanding an environment variable using the provided callback.
expand f q replaces each occurrence of E v some some v with f v or leaves it unchanged where f v raises Not_found. The Not_found exception will not escape this call.
val angstrom_parser : t Angstrom.tMatches a single expression terminated by the end of input or a semicolon lookahead. The accepted languages is described in The Syntax of Query Templates.
val angstrom_parser_with_semicolon : t Angstrom.tA variant of angstrom_parser which accepts unquoted semicolons as part of the single statement, as is valid in some cases like in SQLite3 trigger definitions. This is the parser used by Caqti_request, where it's assumed that the input is a single SQL statement.
val angstrom_list_parser : t list Angstrom.tMatches a sequence of statements while ignoring surrounding white space and end-of-line comments starting with "--". This parser can be used to load schema files with support for environment expansions, like substituting the name of the database schema.
Parses a single expression using angstrom_parser_with_semicolon. The error indicates the byte position of the input string where the parse failure occurred in addition to an error message. See The Syntax of Query Templates for how the input string is interpreted.