Something went wrong. Try again.
SQL frontend over the catalog engine
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343(*--------------------------------------------------------------------------- Copyright (c) 2026 Thomas Gazagnaire. All rights reserved. SPDX-License-Identifier: ISC ---------------------------------------------------------------------------*)
(** SQLite's JSONB: the binary JSON format sqlite3 stores and the json1 functions read ({{:https://sqlite.org/jsonb.html}jsonb.html}).
A JSONB document is one element: a header holding its type and the size of its payload, then the payload, the text of a number or a string as it was written, or the elements of an array or an object. This module reads and writes those bytes as sqlite/src/json.c 3.54.1 does, so every byte it writes is the one sqlite3 writes.
{!Value} is the format's data model, {!Codec} maps elements to OCaml values, and {!Cursor} searches and edits a document in place with SQLite's paths. The functions below read and write JSONB bytes; the json1 functions over them read and write JSON text.
Errors raise or answer {!Catalog.Error.t}, in sqlite3's words. *)
module Sort : sig (** The element types (jsonb.html, section 3). *)
type t = Jsonb_sort.t = | Null (** NULL, type 0. *) | True (** TRUE, type 1. *) | False (** FALSE, type 2. *) | Int (** INT, type 3: RFC 8259 integer text. *) | Int5 (** INT5, type 4: JSON5 integer text. *) | Float (** FLOAT, type 5: RFC 8259 real text. *) | Float5 (** FLOAT5, type 6: JSON5 real text. *) | Text (** TEXT, type 7: a string with no escape. *) | Textj (** TEXTJ, type 8: a string with RFC 8259 escapes. *) | Text5 (** TEXT5, type 9: a string with JSON5 escapes. *) | Textraw (** TEXTRAW, type 10: a string to escape on output. *) | Array (** ARRAY, type 11. *) | Object (** OBJECT, type 12. *)
val of_code : int -> t option (** [of_code c] is the type whose code is [c], [None] for 13 to 15, which are reserved, and any other. *)
val to_code : t -> int (** [to_code s] is the code of [s], the low four bits of its header. *)
val to_string : t -> string (** [to_string s] is the lowercase name jsonb.html gives [s]. *)
val equal : t -> t -> bool (** [equal a b] is [true] iff [a] and [b] are the same type. *)
val pp : t Fmt.t (** [pp] formats a type as {!to_string} does. *)end
module Error : sig (** Errors: {!Catalog.Error.Runtime} in sqlite3's words. Each has a builder and a [fail_] twin that raises it. *)
type t = Catalog.Error.t (** The type for errors. *)
val pp : t Fmt.t (** [pp] is {!Catalog.Error.pp}. *)
val malformed : t (** [malformed] is ["malformed JSON"]. *)
val fail_malformed : unit -> 'a (** [fail_malformed ()] raises {!malformed}. *)
val too_deep : t (** [too_deep] is ["JSON nested too deep"]. *)
val fail_too_deep : unit -> 'a (** [fail_too_deep ()] raises {!too_deep}. *)
(** The type for the ways a path fails. *) type path = Jsonb_error.path = | Bad_path (** The path is not one SQLite's syntax spells. *) | Not_array_element (** An array insertion names something that is not an array element. *) | Path_too_deep (** The path goes past 1000 levels. *) | Malformed (** The document is malformed where the path goes. *)
val path : path -> string -> t (** [path p text] is the error for the path [text] failing as [p]: ["bad JSON path: 'p'"], ["not an array element: 'p'"], ["JSON path too deep"] or ["malformed JSON"]. *)
val fail_path : path -> string -> 'a (** [fail_path p text] raises [path p text]. *)
val sort : exp:string -> fnd:Sort.t option -> t (** [sort ~exp ~fnd] is the error for a codec of kind [exp] finding an element of type [fnd], [None] for a reserved type. *)
val fail_sort : exp:string -> fnd:Sort.t option -> 'a (** [fail_sort ~exp ~fnd] raises [sort ~exp ~fnd]. *)
val no_encoder : string -> t (** [no_encoder kind] is the error for writing with a codec of [kind] that only reads. *)
val fail_no_encoder : string -> 'a (** [fail_no_encoder kind] raises [no_encoder kind]. *)
val no_decoder : string -> t (** [no_decoder kind] is the error for reading with a codec of [kind] that only writes. *)
val fail_no_decoder : string -> 'a (** [fail_no_decoder kind] raises [no_decoder kind]. *)end
module Value : sig (** The format's data model. A value keeps what an element is, not only what it means: the text of a number as it was written, so an integer past 2{^ 53} and [1.50] stay exact, and the escapes a string was written with. *)
(** The type for JSONB values, one constructor per element type. *) type t = Jsonb_value.t = | Null | True | False | Int of string (** RFC 8259 integer text, such as ["-12"]. *) | Int5 of string (** JSON5 integer text, such as ["0x1F"]. *) | Float of string (** RFC 8259 real text, such as ["1.50"]. *) | Float5 of string (** JSON5 real text, such as [".5"]. *) | Text of string (** A string with no escape. *) | Textj of string (** A string with its RFC 8259 escapes, unread. *) | Text5 of string (** A string with its JSON5 escapes, unread. *) | Textraw of string (** A string to escape on output. *) | Array of t list | Object of (t * t) list (** Labels and values in order; a label is a text element. *)
val sort : t -> Sort.t (** [sort v] is the element type of [v]. *)
val equal : t -> t -> bool (** [equal a b] is [true] iff [a] and [b] are the same elements, types and payloads: [Int "1"] and [Int5 "0x1"] differ. *)
val pp : t Fmt.t (** [pp] formats a value as JSON-like text. *)end
type t = Value.t(** The type for JSONB values. *)
val pp : t Fmt.t(** [pp] is {!Value.pp}. *)
module Codec : sig (** Codecs between JSONB elements and OCaml values: {e finally tagged} combinators over the element types. A codec reads the bytes in place, the element it names and nothing else. *)
type 'a t = 'a Jsonb_codec.t (** The type for codecs of OCaml values of type ['a]. *)
val null : 'a -> 'a t (** [null v] reads a NULL as [v] and writes NULL. *)
val bool : bool t (** [bool] reads TRUE and FALSE. *)
val int : string t (** [int] reads an INT or an INT5 as its text, and writes INT5 for hexadecimal text, INT otherwise. *)
val float : string t (** [float] reads a FLOAT or a FLOAT5 as its text, and writes FLOAT. *)
val text : string t (** [text] reads any text element as its string, the escapes of a TEXTJ or a TEXT5 read, and writes TEXTRAW. *)
val element : string t (** [element] reads an element as its own JSONB bytes, header included, and writes such bytes as they are. *)
val ignore : unit t (** [ignore] reads any element, and has no encoder. *)
val array : 'a t -> 'a list t (** [array c] reads an ARRAY, each element with [c]. *)
val members : 'a t -> (string * 'a) list t (** [members c] reads an OBJECT as its labels, read as {!text} reads them, and its values read with [c]. It writes each label TEXTRAW. *)
val map : ?kind:string -> ?dec:('a -> 'b) -> ?enc:('b -> 'a) -> 'a t -> 'b t (** [map ~kind ~dec ~enc c] reads with [c] then [dec], and writes [enc] then with [c]. A missing [dec] or [enc] fails, naming [kind]. *)
val any : ?kind:string -> ?null:'a t -> ?bool:'a t -> ?int:'a t -> ?float:'a t -> ?text:'a t -> ?array:'a t -> ?object':'a t -> ?enc:('a -> 'a t) -> unit -> 'a t (** [any ()] reads each element type with the codec given for it: [int] for INT and INT5, [float] for FLOAT and FLOAT5, [text] for the four text types. It writes with the codec [enc] picks. *)
val json : Json.t t (** [json] reads an element as nox-json's value, a number as its RFC 8259 text, an INT5 or a FLOAT5 as json() writes it, kept as the number's lexeme, and a string with its escapes read. It writes such a value, a number as the lexeme it was read from or as sqlite3 writes an integer or a real, NaN as NULL, and a string TEXTRAW. *)
(** The identity codec. *) module Value : sig val t : Value.t t (** [t] reads an element as its {!Value.t}, and writes one. *) endend
type 'a codec = 'a Codec.t(** The type for codecs. *)
(** {1:bytes JSONB bytes} *)
val sniff : string -> bool(** [sniff s] is [true] iff a json1 function reads the BLOB [s] as JSONB (jsonArgIsJsonb): its outer element is sized to [s] and, for a payload of seven bytes or fewer, which JSON text cast to a BLOB can mimic, every element is well formed. *)
val of_string : 'a codec -> string -> ('a, Error.t) result(** [of_string c s] reads the JSONB bytes [s] with [c], once [s] is one well formed element (jsonbValidityCheck). *)
val of_string_exn : 'a codec -> string -> 'a(** [of_string_exn] is {!of_string} raising {!Catalog.Error}. *)
val to_string : 'a codec -> 'a -> string(** [to_string c v] is the JSONB bytes of [v], each header in its shortest form.*)
val to_writer : 'a codec -> 'a -> Buffer.t -> unit(** [to_writer c v b] adds the JSONB bytes of [v] to [b]. *)
val decode : 'a codec -> t -> ('a, Error.t) result(** [decode c v] reads the value [v] with [c]. *)
val decode_exn : 'a codec -> t -> 'a(** [decode_exn] is {!decode} raising {!Catalog.Error}. *)
val encode : 'a codec -> 'a -> t(** [encode c v] is [v] as a value. *)
(** {1:cursor Cursors} *)
module Cursor : sig (** A cursor on a JSONB document: the bytes, held for reading and editing in place, and a focus on one element.
Paths are SQLite's ({{:https://sqlite.org/json1.html#path_arguments} json1.html}): [$] the root, [.key] or [."key"] a member, [[N]] the element at index [N], [[#]] the end of an array and [[#-N]] the [N]th element from it. A path is read as sqlite3 reads it, as far as the document goes. Searching reads the bytes and builds no value; an edit moves the bytes after it and fixes the headers around it. *)
type t (** The type for cursors. *)
val of_string : string -> t (** [of_string s] is a cursor on a copy of the JSONB bytes [s], focused on the root. [s] is not checked: a malformed document fails where it is read. *)
val to_string : t -> string (** [to_string c] is the bytes of [c]'s document, its edits made. *)
val root : t -> unit (** [root c] focuses [c] on the root. *)
val seek : string -> t -> (bool, Error.path) result (** [seek path c] focuses [c] on the element [path] names from the root: [Ok true] if there is one, [Ok false], the focus unchanged, if there is none, and [Error p] if [path] fails as [p]. The focus moves in place, so a search allocates nothing. *)
val to_path : t -> string (** [to_path c] is the path that focused [c], ["$"] at the root. *)
val pp : t Fmt.t (** [pp] formats a cursor as {!to_path} does. *)
val sort : t -> Sort.t option (** [sort c] is the type of the focus, [None] for a reserved type. *)
val get : 'a codec -> t -> 'a (** [get codec c] is the focus read with [codec].
@raise Catalog.Error if the focus is malformed or not what [codec] reads. *)
val length : t -> int (** [length c] is the count of elements of the focus if it is an array, 0 otherwise. *)
(** {1:edit Edits}
An edit writes the value through its codec at the path, as the json1 function of its name does, and leaves the focus at the root. It is [Ok ()] whether or not it wrote anything, and [Error p] if the path fails as [p]. Where the edit inserts, a path that names nothing creates the objects and arrays it goes through. *)
val insert : string -> 'a codec -> 'a -> t -> (unit, Error.path) result (** [insert path codec v c] writes [v] where [path] names nothing (json_insert). *)
val replace : string -> 'a codec -> 'a -> t -> (unit, Error.path) result (** [replace path codec v c] writes [v] over what [path] names (json_replace). *)
val set : string -> 'a codec -> 'a -> t -> (unit, Error.path) result (** [set path codec v c] writes [v] at [path] either way (json_set). *)
val array_insert : string -> 'a codec -> 'a -> t -> (unit, Error.path) result (** [array_insert path codec v c] inserts [v] into an array before the element [path] names, or at its end for [[#]] (json_array_insert). *)
val remove : string -> t -> (unit, Error.path) result (** [remove path c] removes what [path] names (json_remove); [$] empties the document. *)end