summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/configuration/ast.ml32
-rw-r--r--lib/configuration/ast.mli9
-rw-r--r--lib/configuration/read_conf.ml89
-rw-r--r--lib/configuration/tests/configuration.t55
-rw-r--r--lib/configuration/tests/errors.t103
-rw-r--r--lib/model/data_list.ml1
-rw-r--r--lib/model/data_list.mli18
-rw-r--r--lib/model/model.ml5
-rw-r--r--lib/model/model.mli23
-rw-r--r--lib/model/reference.ml24
-rw-r--r--lib/model/reference.mli10
-rw-r--r--lib/model/typ.ml24
-rw-r--r--lib/query/create.ml125
-rw-r--r--lib/query/tests/create.t40
14 files changed, 414 insertions, 144 deletions
diff --git a/lib/configuration/ast.ml b/lib/configuration/ast.ml
index 6d29d0c..edf129f 100644
--- a/lib/configuration/ast.ml
+++ b/lib/configuration/ast.ml
@@ -5,23 +5,27 @@ let escaped = String.escaped
type setting = {
mutable contents : Model.content Splay.t;
- lists : Model.List.t list;
+ mutable lists : Model.list Splay.t;
}
-let init : unit -> setting = fun () -> { contents = Splay.empty (); lists = [] }
+let init : unit -> setting =
+ fun () -> { contents = Splay.empty (); lists = Splay.empty () }
(** Get the content from the setting. *)
let get_content : setting -> Model.Identifier.t -> Model.content option =
fun settings name -> Splay.find name settings.contents
(** Get the list declared with the given name, if any *)
-let get_list : setting -> Model.Identifier.t -> Model.List.t option =
- fun setting name ->
- List.find_opt setting.lists ~f:(fun l ->
- Model.Identifier.equal l.Model.List.id name)
+let get_list : setting -> Model.Identifier.t -> Model.list option =
+ fun setting name -> Splay.find name setting.lists
-(** All the lists declared in the setting *)
-let lists : setting -> Model.List.t list = fun setting -> setting.lists
+(** All the lists declared in the setting, in a deterministic (identifier)
+ order *)
+let lists : setting -> Model.list list =
+ fun setting ->
+ Splay.fold (fun acc (_, list) -> list :: acc) [] setting.lists
+ |> List.sort ~cmp:(fun a b ->
+ Model.Identifier.compare a.Model.DataList.id b.Model.DataList.id)
exception BrokenLink : ('a Type.Id.t * 'a * Model.Identifier.t) -> exn
@@ -39,8 +43,12 @@ let register_content : setting -> Model.content -> unit =
in
traverse content
-let register_list : setting -> Model.List.t -> setting =
- fun t l -> { t with lists = l :: t.lists }
+let register_list : setting -> Model.list -> setting =
+ fun setting list ->
+ let () =
+ setting.lists <- Splay.add list.Model.DataList.id list setting.lists
+ in
+ setting
(** Force the evaluation of the field target and ensure it points to an existing
content in the application.
@@ -56,8 +64,8 @@ let get_reference_target : Model.field -> Model.content option =
(* References to static lists are leaves of the dependency graph: they
are not followed here. *)
match r.Model.Typ.t with
- | Model.Typ.List _ -> None
- | Model.Typ.Content ref -> (
+ | Model.Reference.List _ -> None
+ | Model.Reference.Content ref -> (
try Some (Model.Reference.get ref)
with BrokenLink _ as e -> raise_notrace e))
diff --git a/lib/configuration/ast.mli b/lib/configuration/ast.mli
index ce5d1b6..b014c9b 100644
--- a/lib/configuration/ast.mli
+++ b/lib/configuration/ast.mli
@@ -5,10 +5,10 @@ val init : unit -> setting
val get_content : setting -> Model.Identifier.t -> Model.content option
(** Get the content from the setting. *)
-val get_list : setting -> Model.Identifier.t -> Model.List.t option
+val get_list : setting -> Model.Identifier.t -> Model.list option
(** Get the list declared with the given name, if any *)
-val lists : setting -> Model.List.t list
+val lists : setting -> Model.list list
(** All the lists declared in the setting *)
val root_contents : setting -> Model.content list
@@ -25,7 +25,10 @@ exception BrokenLink : ('a Type.Id.t * 'a * Model.Identifier.t) -> exn
(** Exception raised when a reference in the configuration targets neither an
existing content nor a declared list *)
-val register_list : setting -> Model.List.t -> setting
+val register_list : setting -> Model.list -> setting
+(** Register a list in the setting, replacing any previous list with the same
+ id. Lists are registered while they are decoded, so that a list can
+ reference the lists declared before it. *)
val check : setting -> (unit, exn) result
(** Check the setting and return the the error as an exception *)
diff --git a/lib/configuration/read_conf.ml b/lib/configuration/read_conf.ml
index c7149ae..14ee0b7 100644
--- a/lib/configuration/read_conf.ml
+++ b/lib/configuration/read_conf.ml
@@ -29,27 +29,41 @@ module Make (S : Decoders.Decode.S) = struct
| None ->
S.fail "Expected an identifier with only the accepted characters"
- type env = { ty : S.value Type.Id.t; settings : Ast.setting }
+ type env = {
+ ty : S.value Type.Id.t;
+ settings : Ast.setting;
+ allow_content : bool;
+ (** Whether a reference may target a content. References inside a list
+ can only target other lists. *)
+ }
(** The decoders below need the setting being built (to resolve the reference
targets) and a type id (to attribute broken links to this parsing pass in
the error messages). *)
(** Build the reference target for a field. Lists are decoded before the
- contents, so a declared list is matched eagerly; a content target is a
- lazy thunk closing over the setting, only forced by [Ast.check] once every
- content has been registered — this is what allows forward and cyclic
- references between contents. The raw value is kept to report broken links.
+ contents and registered as soon as they are decoded, so a declared list
+ is matched eagerly; a content target is a lazy thunk closing over the
+ setting, only forced by [Ast.check] once every content has been
+ registered — this is what allows forward and cyclic references between
+ contents. The raw value is kept to report broken links.
*)
let link (env : env) (value : S.value) (name : Model.Identifier.t) :
- Model.target =
+ Model.target S.decoder =
match Ast.get_list env.settings name with
- | Some list -> Model.Typ.List list
- | None ->
- Model.Typ.Content
- (Model.Reference.v (fun () ->
- match Ast.get_content env.settings name with
- | Some content -> content
- | None -> raise_notrace (Ast.BrokenLink (env.ty, value, name))))
+ | Some list -> S.succeed (Model.Reference.List list)
+ | None -> (
+ if env.allow_content then
+ S.succeed
+ (Model.Reference.Content
+ (Model.Reference.v (fun () ->
+ match Ast.get_content env.settings name with
+ | Some content -> content
+ | None ->
+ raise_notrace (Ast.BrokenLink (env.ty, value, name)))))
+ else
+ S.fail_with
+ (Decoders.Error.make ~context:value
+ "References in a list can only target a list declared before it"))
let field_type (env : env) : Model.field_ex S.decoder =
let text_type : Model.field_ex S.decoder =
@@ -65,7 +79,7 @@ module Make (S : Decoders.Decode.S) = struct
and* multivaluated = S.field_opt_or ~default:false "multivaluated" S.bool
and* constraints = S.field_opt "constraints" dsl in
let value, name = content_t in
- let t = link env value name in
+ let* t = link env value name in
match multivaluated with
| true -> S.succeed (Model.Field.T (MultiReference { t; constraints }))
| false -> S.succeed (Model.Field.T (MonoReference { t; constraints }))
@@ -108,35 +122,36 @@ module Make (S : Decoders.Decode.S) = struct
in
S.succeed (Model.Content.v id sections subcontents))
- let list_source : Model.List.source S.decoder =
- (* The source is either a plain kind string ("csv"), or an object with a
- "type" and an optional "file" path. *)
- let as_object =
- let* kind = S.field "type" S.string
- and* file = S.field_opt "file" S.string in
- S.succeed Model.List.{ kind; file }
- in
- let as_string =
- S.map (fun kind -> Model.List.{ kind; file = None }) S.string
- in
- S.one_of [ ("object", as_object); ("string", as_string) ]
-
- let list : Model.List.t S.decoder =
+ let list (env : env) : Model.list S.decoder =
+ (* The list has its own fields, declared directly under "field" — unlike
+ contents, there is no section grouping them. Only the id and the fields
+ are part of the model: where the entries come from is the concern of the
+ data provider, not of the configuration model. *)
let* id = S.field "id" identifier
- and* source = S.field "source" list_source in
- S.succeed Model.List.{ id; source }
+ and* fields = S.field_opt_or ~default:[] "field" (S.list (field env)) in
+ S.succeed Model.DataList.{ id; fields }
let t : Ast.setting S.decoder =
(* Register a type id to attribute broken links to this parsing pass *)
let ty : S.value Type.Id.t = Type.Id.make () in
- (* Decode the lists first, then the contents: the reference targets are
- lazy thunks closing over [settings], forced only by [Ast.check] once
- every content has been registered. *)
- let* lists = S.field_opt_or ~default:[] "list" (S.list list) in
- let settings =
- List.fold_left lists ~init:(Ast.init ()) ~f:Ast.register_list
+ let settings = Ast.init () in
+ (* Decode the lists first, registering each one as soon as it is decoded so
+ that a list can reference the lists declared before it. References in a
+ list can only target lists ([allow_content = false]). *)
+ let list_env = { ty; settings; allow_content = false } in
+ let* _lists =
+ S.field_opt_or ~default:[] "list"
+ (S.list
+ (S.and_then
+ (fun list ->
+ let _ = Ast.register_list settings list in
+ S.succeed list)
+ (list list_env)))
in
- let env = { ty; settings } in
+ (* Then the contents: the reference targets are lazy thunks closing over
+ [settings], forced only by [Ast.check] once every content has been
+ registered. *)
+ let env = { ty; settings; allow_content = true } in
let* contents = S.field "content" (S.list (content env)) in
let () = List.iter contents ~f:(Ast.register_content settings) in
diff --git a/lib/configuration/tests/configuration.t b/lib/configuration/tests/configuration.t
index 880e690..5d14bbb 100644
--- a/lib/configuration/tests/configuration.t
+++ b/lib/configuration/tests/configuration.t
@@ -76,20 +76,17 @@ Cycles between elements are accepted when they are not mandatory to each other
-Lists are declared in the "list" key, with a source given either as a plain
-kind string or as an object with a "type" and an optional "file". A reference
-field can target a list, like a content:
+Lists are declared in the "list" key. A reference field can target a list,
+like a content:
$ tests --configuration <<EOF
> {
> "list": [
> {
- > "id": "user_type_list",
- > "source": "csv"
+ > "id": "user_type_list"
> },
> {
- > "id": "status_list",
- > "source": { "type": "csv", "file": "status.csv" }
+ > "id": "status_list"
> }
> ],
> "content": [
@@ -119,3 +116,47 @@ field can target a list, like a content:
> }
> EOF
+
+Lists can declare their own fields, directly under the "field" key (there is
+no section in a list). A reference field in a list can target another list:
+
+ $ tests --configuration <<EOF
+ > {
+ > "list": [
+ > {
+ > "id": "country_list",
+ > "field": [
+ > { "name": "name", "label": {}, "type": "text" },
+ > { "name": "code", "label": {}, "type": "text" }
+ > ]
+ > },
+ > {
+ > "id": "user_type_list",
+ > "field": [
+ > {
+ > "name": "country",
+ > "label": {},
+ > "type": { "content": "country_list" }
+ > }
+ > ]
+ > }
+ > ],
+ > "content": [
+ > {
+ > "id": "entity",
+ > "section": [
+ > {
+ > "field": [
+ > {
+ > "name": "user_type",
+ > "label": {},
+ > "type": { "content": "user_type_list" }
+ > }
+ > ]
+ > }
+ > ]
+ > }
+ > ]
+ > }
+ > EOF
+
diff --git a/lib/configuration/tests/errors.t b/lib/configuration/tests/errors.t
index 99ad465..b23f237 100644
--- a/lib/configuration/tests/errors.t
+++ b/lib/configuration/tests/errors.t
@@ -222,8 +222,7 @@ A list ID follows the same rules as a content ID:
> {
> "list": [
> {
- > "id": "foo.bar",
- > "source": "csv"
+ > "id": "foo.bar"
> }
> ],
> "content": []
@@ -235,3 +234,103 @@ A list ID follows the same rules as a content ID:
in field "id":
Expected an identifier with only the accepted characters, but got
"foo.bar"
+
+A reference field in a list can only target another list, not a content:
+
+ $ tests --configuration <<EOF
+ > {
+ > "list": [
+ > {
+ > "id": "user_type_list",
+ > "field": [
+ > {
+ > "name": "entity",
+ > "label": {},
+ > "type": { "content": "entity" }
+ > }
+ > ]
+ > }
+ > ],
+ > "content": [ { "id": "entity" } ]
+ > }
+ > EOF
+ in field "list":
+ while decoding a list:
+ element 0:
+ in field "field":
+ while decoding a list:
+ element 0:
+ in field "type":
+ I tried the following decoders but they all failed:
+ "text" decoder:
+ Expected a string, but got { "content": "entity" }
+ "content" decoder:
+ References in a list can only target a list declared before it, but got
+ { "content": "entity" }
+
+A list cannot reference itself:
+
+ $ tests --configuration <<EOF
+ > {
+ > "list": [
+ > {
+ > "id": "user_type_list",
+ > "field": [
+ > {
+ > "name": "parent",
+ > "label": {},
+ > "type": { "content": "user_type_list" }
+ > }
+ > ]
+ > }
+ > ],
+ > "content": []
+ > }
+ > EOF
+ in field "list":
+ while decoding a list:
+ element 0:
+ in field "field":
+ while decoding a list:
+ element 0:
+ in field "type":
+ I tried the following decoders but they all failed:
+ "text" decoder:
+ Expected a string, but got { "content": "user_type_list" }
+ "content" decoder:
+ References in a list can only target a list declared before it, but got
+ { "content": "user_type_list" }
+
+A list cannot reference a list declared after it:
+
+ $ tests --configuration <<EOF
+ > {
+ > "list": [
+ > {
+ > "id": "first_list",
+ > "field": [
+ > {
+ > "name": "other",
+ > "label": {},
+ > "type": { "content": "second_list" }
+ > }
+ > ]
+ > },
+ > { "id": "second_list" }
+ > ],
+ > "content": []
+ > }
+ > EOF
+ in field "list":
+ while decoding a list:
+ element 0:
+ in field "field":
+ while decoding a list:
+ element 0:
+ in field "type":
+ I tried the following decoders but they all failed:
+ "text" decoder:
+ Expected a string, but got { "content": "second_list" }
+ "content" decoder:
+ References in a list can only target a list declared before it, but got
+ { "content": "second_list" }
diff --git a/lib/model/data_list.ml b/lib/model/data_list.ml
new file mode 100644
index 0000000..11fb0b4
--- /dev/null
+++ b/lib/model/data_list.ml
@@ -0,0 +1 @@
+type 'a t = { id : Identifier.t; fields : 'a Field.t list }
diff --git a/lib/model/data_list.mli b/lib/model/data_list.mli
new file mode 100644
index 0000000..e74bf23
--- /dev/null
+++ b/lib/model/data_list.mli
@@ -0,0 +1,18 @@
+(** A static list declared in the configuration.
+
+ Unlike contents, lists are not editable by the user: their entries are
+ loaded by the data provider (from a file or any other source, which is not
+ part of the model) and they can only be referenced from fields.
+
+ Lists have their own fields, declared directly under the ["field"] key of
+ the list — unlike contents, there is no section grouping them. The fields
+ use the same {!Field.t} as the content fields; the only restriction is that
+ a reference field in a list can only target another list, declared before it
+ (a list cannot reference itself or a later list). *)
+
+type 'a t = {
+ id : Identifier.t;
+ (** Name of the list; usable as the target of a reference field, and as
+ the name of its dedicated table in the database *)
+ fields : 'a Field.t list; (** The fields of each entry of the list *)
+}
diff --git a/lib/model/model.ml b/lib/model/model.ml
index 25179ff..20914b1 100644
--- a/lib/model/model.ml
+++ b/lib/model/model.ml
@@ -6,15 +6,16 @@ module Identifier = Identifier
module Dsl = Dsl
module Typ = Typ
module Reference = Reference
-module List = List
+module DataList = Data_list
module Field = Field
module Section = Section
module Content = Content
(** The target of a reference field, resolved: a lazy pointer to a content, or
a static list. *)
-type target = Reference.t Typ.target
+type target = Reference.target
+type list = target Data_list.t
type content = target Content.t
type section = target Section.t
type field = target Field.t
diff --git a/lib/model/model.mli b/lib/model/model.mli
index 8c097f3..e81a844 100644
--- a/lib/model/model.mli
+++ b/lib/model/model.mli
@@ -7,7 +7,7 @@
list of {!section}s, and optionally sub-contents (a content with a parent is
a subcontent). Each section groups a list of {!field}s. A field has a name,
a label translated in several languages, a {!typ} — text, integer, or a
- reference to another content or to a static {!List.t} (single or
+ reference to another content or to a static {!DataList.t} (single or
multivalued) — and optional validation rules expressed in a small expression
language (see {!Dsl}).
@@ -41,8 +41,8 @@
- {!Content}, {!Section}, {!Field} — the structure of the model.
- {!Typ} — the field types, and the values carried by fields.
- {!Reference} — a link from a reference field to its target content.
- - {!List} — a static list declared in the configuration: referenceable, but
- not editable by the user.
+ - {!DataList} — a static list declared in the configuration: referenceable,
+ but not editable by the user.
- {!Dsl} — the expression language used in validation rules and display
criteria.
@@ -72,9 +72,13 @@ module Content = Content
{!Content.parent} / {!Content.root}; iterate over all its fields with
{!Content.fields}. *)
-type target = Reference.t Typ.target
+type target = Reference.target
(** The target of a reference field, resolved: either a lazy pointer to a
- content ({!Typ.Content}) or a static {!List.t} ({!Typ.List}). *)
+ content ({!Reference.Content}) or a static {!DataList.t}
+ ({!Reference.List}). *)
+
+type list = target Data_list.t
+(** A static list, resolved: its fields are typed with {!target} *)
type content = target Content.t
(** A content : the element produced by the configuration loader and manipulated
@@ -114,10 +118,11 @@ module Reference = Reference
type reference = target Typ.reference
(** A reference field target: a {!target}, plus optional constraints *)
-module List = List
-(** A static list declared in the configuration: its entries are loaded from a
- source (e.g. a CSV file) and the list can be referenced from fields, but it
- is not editable by the user. *)
+module DataList = Data_list
+(** A static list declared in the configuration: its entries are loaded by the
+ data provider and the list can be referenced from fields, but it is not
+ editable by the user. Lists have their own fields (no sections); a reference
+ field in a list can only target another list. *)
module Section = Section
(** A group of fields displayed together, with a per-language [label] and an
diff --git a/lib/model/reference.ml b/lib/model/reference.ml
index 67bbcaf..bdacf54 100644
--- a/lib/model/reference.ml
+++ b/lib/model/reference.ml
@@ -1,11 +1,17 @@
-(** A lazy pointer to a resolved content. The payload is
- [t Typ.target Content.t] — i.e. {!Model.content} — and is only forced once
- every content has been registered (see [Configuration.Ast.check]), which
- is what allows cyclic references between contents. *)
-type t = T : t Typ.target Content.t Lazy.t -> t [@@unboxed]
+(** A lazy pointer to a resolved content. The payload is [target Content.t] —
+ i.e. {!Model.content} — and is only forced once every content has been
+ registered (see [Configuration.Ast.check]), which is what allows cyclic
+ references between contents. *)
+type t = T : target Content.t Lazy.t -> t [@@unboxed]
-let v : (unit -> t Typ.target Content.t) -> t =
- fun f -> T (lazy (f ()))
+(** The target of a reference field, resolved: either a lazy pointer to a
+ content, or a static list. The list case carries the list itself, whose own
+ fields are typed with this very [target] — references inside a list can
+ only target other lists (enforced by the configuration reader). *)
+and target =
+ | Content of t
+ | List of target Data_list.t
-let get : t -> t Typ.target Content.t = fun (T target) ->
- Lazy.force target
+let v : (unit -> target Content.t) -> t = fun f -> T (lazy (f ()))
+
+let get : t -> target Content.t = fun (T target) -> Lazy.force target
diff --git a/lib/model/reference.mli b/lib/model/reference.mli
index 63acf6c..2f9659b 100644
--- a/lib/model/reference.mli
+++ b/lib/model/reference.mli
@@ -1,4 +1,10 @@
type t
-val v : (unit -> t Typ.target Content.t) -> t
-val get : t -> t Typ.target Content.t
+and target =
+ | Content of t
+ | List of target Data_list.t
+ (** The target of a reference field: a content (through the lazy pointer)
+ or a static list *)
+
+val v : (unit -> target Content.t) -> t
+val get : t -> target Content.t
diff --git a/lib/model/typ.ml b/lib/model/typ.ml
index 9427b86..6168c4c 100644
--- a/lib/model/typ.ml
+++ b/lib/model/typ.ml
@@ -1,33 +1,19 @@
-(** The target of a reference field: either a content or a static list.
-
- Only the content side is parameterized: this module cannot depend on
- [Reference] (module cycle through [Content] and [Field]), whereas [List]
- only depends on [Identifier] and can be fixed here. At the resolved level
- (see {!Model.target}) ['c = Reference.t]. *)
-type 'c target =
- | Content of 'c
- | List of List.t
-
type 'a reference = {
t : 'a;
- (** The target of the reference: a {!target} at the resolved level. *)
+ (** The target of the reference: at the resolved level a
+ {!Reference.target} (aliased as {!Model.target}). *)
constraints : string list Dsl.t option;
(** Optionally restrict the values allowed for this field, as a DSL
expression read from the optional ["constraints"] key of the
configuration. Currently carried through to the consumers (e.g. to
filter the proposed values); [None] means no restriction. *)
}
-(** A reference from a field to a content.
-
- The target [t] is a {!Model.Reference.t}: a lazy pointer to the content,
- created while the configuration is parsed (see [Configuration.Read_conf])
- and resolved once all the contents are registered. The laziness is what
- allows contents to reference each other cyclically.
+(** A reference from a field to a target (a content or a static list).
The type stays parameterized over ['a] only to break the module dependency
cycle ([Reference] depends on [Content], which depends on [Field]/[Typ]); in
- practice ['a] is always [Model.Reference.t]. Use the aliases
- [Model.reference], [Model.content], [Model.field], ... at the use sites. *)
+ practice ['a] is always {!Model.target}. Use the aliases [Model.reference],
+ [Model.content], [Model.field], ... at the use sites. *)
(** Each of the field types available in the application.
diff --git a/lib/query/create.ml b/lib/query/create.ml
index 8dcd10d..49047a7 100644
--- a/lib/query/create.ml
+++ b/lib/query/create.ml
@@ -50,37 +50,14 @@ let get_foreign_statement :
^ Model.Identifier.quote Model.Identifier.id
^ ")"
-(** Create the dedicated table of a static list.
-
- References to a list point to the [id] of this table, exactly like
- references to a content. The rows themselves come from the list source and
- are not created here. *)
-let create_list_table : Caqti_lwt.connection -> Model.List.t -> unit Lwt.t =
- fun db list ->
- let module Db = (val db) in
- let buff = Buffer.create 64 in
- let format = Format.formatter_of_buffer buff in
- let () =
- Format.fprintf format "CREATE TABLE %s@; @[@[<v>( %a@] )@]"
- (Model.Identifier.quote list.Model.List.id)
- Format.pp_print_string "\"id\" INTEGER PRIMARY KEY"
- in
- Format.pp_print_flush format ();
- let request =
- let open Caqti.Templater in
- direct T.(unit -->. unit) (Buffer.contents buff)
- in
- let* result = Db.exec request () in
- Caqti_lwt.or_fail result
-
(** Create the dedicated table required for multivaluated fields *)
let create_external_reference_table :
Caqti_lwt.connection ->
- Model.content ->
+ Model.Identifier.t ->
foreign list ->
Model.field ->
foreign list Lwt.t =
- fun db content acc field ->
+ fun db parent_table acc field ->
match Model.Field.type_ field with
| Model.Field.(T Integer)
| Model.Field.(T Text)
@@ -93,17 +70,16 @@ let create_external_reference_table :
dedicated table of the target list *)
let target_table =
match reference.Model.Typ.t with
- | Model.Typ.Content ref ->
+ | Model.Reference.Content ref ->
Model.Content.id (Model.Reference.get ref)
- | Model.Typ.List list -> list.Model.List.id
+ | Model.Reference.List list -> list.Model.DataList.id
in
let fields =
[
"\"id\" INTEGER PRIMARY KEY";
"\"parent\" INTEGER NOT NULL";
"\"target\" INTEGER NOT NULL";
- get_foreign_statement Model.Identifier.parent
- (Model.Content.id content);
+ get_foreign_statement Model.Identifier.parent parent_table;
(* Table constraints must come after the column definitions *)
get_foreign_statement Model.Identifier.target target_table;
]
@@ -111,8 +87,7 @@ let create_external_reference_table :
let () =
Format.fprintf format "CREATE TABLE %s@; @[@[<v>( %a@] )@]"
(Model.Identifier.quote
- (Model.Identifier.dot (Model.Content.id content)
- (Model.Field.name field)))
+ (Model.Identifier.dot parent_table (Model.Field.name field)))
(Format.pp_print_list
~pp_sep:(fun f () ->
Format.pp_print_break f 0 0;
@@ -131,6 +106,78 @@ let create_external_reference_table :
let* () = Caqti_lwt.or_fail result in
Lwt.return acc
+(** Create the dedicated table of a static list, with one column per field.
+
+ References to a list point to the [id] of this table, exactly like
+ references to a content. The rows themselves come from the list source and
+ are not created here. *)
+let create_list_table :
+ Caqti_lwt.connection ->
+ foreign list * Model.Identifier.t list ->
+ Model.list ->
+ (foreign list * Model.Identifier.t list) Lwt.t =
+ fun db (alter_queries, created) list ->
+ let module Identifier = Model.Identifier in
+ let module Db = (val db) in
+ let list_fields = list.Model.DataList.fields in
+ (* Foreign keys toward the lists targeted by reference fields. A not yet
+ created target goes to the ALTER TABLE list, processed once every table
+ exists. *)
+ let foreign, alter_queries =
+ List.fold_left list_fields ~init:([], alter_queries)
+ ~f:(fun (foreign, alter_queries) field ->
+ match Model.Field.type_ field with
+ | T (MonoReference reference) -> (
+ let column = Model.Field.name field in
+ match reference.Model.Typ.t with
+ | Model.Reference.List target ->
+ let dst = target.Model.DataList.id in
+ if List.mem ~set:created dst then
+ (get_foreign_statement column dst :: foreign, alter_queries)
+ else
+ ( foreign,
+ {
+ src_table = list.Model.DataList.id;
+ src_column = column;
+ dst_table = dst;
+ dst_column = Identifier.id;
+ }
+ :: alter_queries )
+ | Model.Reference.Content _ -> (foreign, alter_queries))
+ | _ -> (foreign, alter_queries))
+ in
+ let fields =
+ "\"id\" INTEGER PRIMARY KEY"
+ :: List.fold_left list_fields ~init:foreign ~f:(fun acc field ->
+ match get_field_query field with None -> acc | Some q -> q :: acc)
+ in
+ let buff = Buffer.create 128 in
+ let format = Format.formatter_of_buffer buff in
+ let () =
+ Format.fprintf format "CREATE TABLE %s@; @[@[<v>( %a@] )@]"
+ (Identifier.quote list.Model.DataList.id)
+ (Format.pp_print_list
+ ~pp_sep:(fun f () ->
+ Format.pp_print_break f 0 0;
+ Format.pp_print_string f ", ")
+ Format.pp_print_string)
+ fields
+ in
+ Format.pp_print_flush format ();
+ let request =
+ let open Caqti.Templater in
+ direct T.(unit -->. unit) (Buffer.contents buff)
+ in
+ let* result = Db.exec request () in
+ let* () = Caqti_lwt.or_fail result in
+ (* Create the externals tables holding the multivaluated fields *)
+ let* alter_queries =
+ Lwt_list.fold_left_s
+ (create_external_reference_table db list.Model.DataList.id)
+ alter_queries list_fields
+ in
+ Lwt.return (alter_queries, list.Model.DataList.id :: created)
+
(** Generate the create table for a content *)
let rec create_table :
Caqti_lwt.connection ->
@@ -171,14 +218,14 @@ let rec create_table :
| T (MonoReference reference) -> (
let column = Model.Field.name field in
match reference.Model.Typ.t with
- | Model.Typ.List list ->
+ | Model.Reference.List list ->
(* The list tables are created before the content tables, so
the constraint can be declared directly *)
let foreign =
- get_foreign_statement column list.Model.List.id :: foreign
+ get_foreign_statement column list.Model.DataList.id :: foreign
in
(foreign, alter_queries)
- | Model.Typ.Content ref -> (
+ | Model.Reference.Content ref -> (
let dst = Model.Reference.get ref in
match List.mem ~set:created dst with
| true ->
@@ -241,7 +288,7 @@ let rec create_table :
(* Create the externals tables holding the multivaluated fields *)
let* acc =
Lwt_list.fold_left_s
- (create_external_reference_table db content)
+ (create_external_reference_table db (Model.Content.id content))
alter_queries content_fields
in
let created = content :: created in
@@ -285,8 +332,10 @@ let create : Caqti_lwt.connection -> Configuration.Ast.setting -> unit Lwt.t =
let module Db = (val db) in
(* The list tables are created first, so that the content tables can declare
their foreign keys to the lists directly *)
- let* () =
- Lwt_list.iter_s (create_list_table db) (Configuration.Ast.lists conf)
+ let* foreign, _ =
+ Lwt_list.fold_left_s
+ (fun acc list -> create_list_table db acc list)
+ ([], []) (Configuration.Ast.lists conf)
in
let contents = Configuration.Ast.root_contents conf in
let* foreign, _ =
@@ -298,7 +347,7 @@ let create : Caqti_lwt.connection -> Configuration.Ast.setting -> unit Lwt.t =
parent *)
Lwt.return acc
| None -> create_table db acc content)
- ([], []) contents
+ (foreign, []) contents
in
match Db.dialect with
| Caqti.Template.Dialect.Sqlite _ ->
diff --git a/lib/query/tests/create.t b/lib/query/tests/create.t
index 46e3940..7f41ad0 100644
--- a/lib/query/tests/create.t
+++ b/lib/query/tests/create.t
@@ -156,8 +156,7 @@ to a list is an integer column constrained by a foreign key to the list table:
> {
> "list": [
> {
- > "id": "user_type_list",
- > "source": "csv"
+ > "id": "user_type_list"
> }
> ],
> "content": [
@@ -192,8 +191,7 @@ target column referencing the list table:
> {
> "list": [
> {
- > "id": "user_type_list",
- > "source": "csv"
+ > "id": "user_type_list"
> }
> ],
> "content": [
@@ -223,3 +221,37 @@ target column referencing the list table:
, "target" INTEGER NOT NULL
, FOREIGN KEY("parent") REFERENCES "entity"("id")
, FOREIGN KEY("target") REFERENCES "user_type_list"("id") )
+
+A list with fields gets one column per field. A reference field in the list is
+a foreign key to the targeted list table:
+
+ $ tests --schema "country_list" "user_type_list" <<EOF
+ > {
+ > "list": [
+ > {
+ > "id": "country_list",
+ > "field": [
+ > { "name": "name", "label": {}, "type": "text" }
+ > ]
+ > },
+ > {
+ > "id": "user_type_list",
+ > "field": [
+ > {
+ > "name": "country",
+ > "label": {},
+ > "type": { "content": "country_list" }
+ > }
+ > ]
+ > }
+ > ],
+ > "content": [ { "id": "entity" } ]
+ > }
+ > EOF
+ CREATE TABLE "country_list"
+ ( "id" INTEGER PRIMARY KEY
+ , "name" VARCHAR )
+ CREATE TABLE "user_type_list"
+ ( "id" INTEGER PRIMARY KEY
+ , "country" INTEGER
+ , FOREIGN KEY("country") REFERENCES "country_list"("id") )