From e1bf7686f408b52a870d4ed4f3c565dbbcc3954f Mon Sep 17 00:00:00 2001 From: Sébastien Dailly Date: Thu, 19 Nov 2026 09:57:49 +0100 Subject: Management of lists in the settings --- lib/configuration/ast.ml | 29 ++++++--- lib/configuration/ast.mli | 16 ++--- lib/configuration/read_conf.ml | 59 +++++++++-------- lib/configuration/tests/configuration.t | 44 +++++++++++++ lib/configuration/tests/errors.t | 20 ++++++ lib/model/model.ml | 24 ++++--- lib/model/model.mli | 66 +++++++++++-------- lib/model/reference.ml | 13 +++- lib/model/reference.mli | 4 +- lib/model/typ.ml | 13 +++- lib/query/create.ml | 108 ++++++++++++++++++++++++-------- lib/query/tests/create.t | 74 ++++++++++++++++++++++ lib/query/update.ml | 4 +- 13 files changed, 357 insertions(+), 117 deletions(-) (limited to 'lib') diff --git a/lib/configuration/ast.ml b/lib/configuration/ast.ml index 607031e..6d29d0c 100644 --- a/lib/configuration/ast.ml +++ b/lib/configuration/ast.ml @@ -3,8 +3,10 @@ module Splay = Splay.Make (Model.Identifier) let escaped = String.escaped -type list' = { id : string; source : string } -type setting = { mutable contents : Model.content Splay.t; lists : list' list } +type setting = { + mutable contents : Model.content Splay.t; + lists : Model.List.t list; +} let init : unit -> setting = fun () -> { contents = Splay.empty (); lists = [] } @@ -12,14 +14,16 @@ let init : unit -> setting = fun () -> { contents = Splay.empty (); lists = [] } let get_content : setting -> Model.Identifier.t -> Model.content option = fun settings name -> Splay.find name settings.contents -(** Check whether a list with the given name is declared in the setting *) -let has_list : setting -> Model.Identifier.t -> bool = +(** 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.exists setting.lists ~f:(fun { id; _ } -> - String.equal id (Model.Identifier.to_string name)) + List.find_opt setting.lists ~f:(fun l -> + Model.Identifier.equal l.Model.List.id name) + +(** All the lists declared in the setting *) +let lists : setting -> Model.List.t list = fun setting -> setting.lists exception BrokenLink : ('a Type.Id.t * 'a * Model.Identifier.t) -> exn -exception NotImplemented : ('a Type.Id.t * 'a * Model.Identifier.t) -> exn (** Register a content and all the associated subcontent in the top level, giving the possibility to fetch any content or subcontent by its name @@ -35,7 +39,7 @@ let register_content : setting -> Model.content -> unit = in traverse content -let register_list : setting -> list' -> setting = +let register_list : setting -> Model.List.t -> setting = fun t l -> { t with lists = l :: t.lists } (** Force the evaluation of the field target and ensure it points to an existing @@ -49,8 +53,13 @@ let get_reference_target : Model.field -> Model.content option = | Text -> None | Integer -> None | MonoReference r | MultiReference r -> ( - try Some (Model.Reference.get r.Model.Typ.t) - with BrokenLink _ as e -> raise_notrace e) + (* 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 -> ( + try Some (Model.Reference.get ref) + with BrokenLink _ as e -> raise_notrace e)) (** Extract the other contents pointed by a given content, and the field used for it. *) diff --git a/lib/configuration/ast.mli b/lib/configuration/ast.mli index 298e2d5..ce5d1b6 100644 --- a/lib/configuration/ast.mli +++ b/lib/configuration/ast.mli @@ -1,13 +1,15 @@ type setting -type list' = { id : string; source : string } val init : unit -> setting val get_content : setting -> Model.Identifier.t -> Model.content option (** Get the content from the setting. *) -val has_list : setting -> Model.Identifier.t -> bool -(** Check whether a list with the given name is declared in the setting *) +val get_list : setting -> Model.Identifier.t -> Model.List.t option +(** Get the list declared with the given name, if any *) + +val lists : setting -> Model.List.t list +(** All the lists declared in the setting *) val root_contents : setting -> Model.content list (** Extract all the content in the topological order. @@ -20,12 +22,10 @@ val register_content : setting -> Model.content -> unit without traversing the whole tree each time. *) exception BrokenLink : ('a Type.Id.t * 'a * Model.Identifier.t) -> exn -(** Exception raised when a reference in the configuration targets a - non-existing content *) - -exception NotImplemented : ('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 -> list' -> setting +val register_list : setting -> Model.List.t -> setting 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 bcd8f8a..c7149ae 100644 --- a/lib/configuration/read_conf.ml +++ b/lib/configuration/read_conf.ml @@ -34,19 +34,22 @@ module Make (S : Decoders.Decode.S) = struct targets) and a type id (to attribute broken links to this parsing pass in the error messages). *) - (** Build the reference target for a field: a lazy thunk closing over the - setting. The thunk is 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. *) + (** 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. + *) let link (env : env) (value : S.value) (name : Model.Identifier.t) : - Model.Reference.t = - Model.Reference.v (fun () -> - match Ast.get_content env.settings name with - | Some content -> content - | None -> - if Ast.has_list env.settings name then - raise_notrace (Ast.NotImplemented (env.ty, value, name)) - else raise_notrace (Ast.BrokenLink (env.ty, value, name))) + Model.target = + 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)))) let field_type (env : env) : Model.field_ex S.decoder = let text_type : Model.field_ex S.decoder = @@ -105,9 +108,23 @@ module Make (S : Decoders.Decode.S) = struct in S.succeed (Model.Content.v id sections subcontents)) - let list' : Ast.list' S.decoder = - let* id = S.field "id" S.string and* source = S.field "source" S.string in - S.succeed Ast.{ id; source } + 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* id = S.field "id" identifier + and* source = S.field "source" list_source in + S.succeed Model.List.{ id; source } let t : Ast.setting S.decoder = (* Register a type id to attribute broken links to this parsing pass *) @@ -115,7 +132,7 @@ module Make (S : Decoders.Decode.S) = struct (* 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* lists = S.field_opt_or ~default:[] "list" (S.list list) in let settings = List.fold_left lists ~init:(Ast.init ()) ~f:Ast.register_list in @@ -136,16 +153,6 @@ module Make (S : Decoders.Decode.S) = struct ^ "\". Expected existing list or content" in S.fail_with (Decoders.Error.make ~context message)) - | Error (Ast.NotImplemented (ty', context, name) as e) -> ( - match Type.Id.provably_equal ty ty' with - | None -> raise e - | Some Type.Equal -> - let message = - "Unsupported target \"" - ^ Model.Identifier.to_string name - ^ "\". Lists as target are not implemented yet" - in - S.fail_with (Decoders.Error.make ~context message)) | Error (Graph_tree.Cycle contents) -> let content_name = List.map contents ~f:(fun c -> diff --git a/lib/configuration/tests/configuration.t b/lib/configuration/tests/configuration.t index e44b91a..880e690 100644 --- a/lib/configuration/tests/configuration.t +++ b/lib/configuration/tests/configuration.t @@ -75,3 +75,47 @@ 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: + + $ tests --configuration < { + > "list": [ + > { + > "id": "user_type_list", + > "source": "csv" + > }, + > { + > "id": "status_list", + > "source": { "type": "csv", "file": "status.csv" } + > } + > ], + > "content": [ + > { + > "id": "entity", + > "section": [ + > { + > "field": [ + > { + > "name": "user_type", + > "label": {}, + > "type": { "content": "user_type_list" } + > }, + > { + > "name": "statuses", + > "label": {}, + > "type": { + > "content": "status_list", + > "multivaluated": true + > } + > } + > ] + > } + > ] + > } + > ] + > } + > EOF + diff --git a/lib/configuration/tests/errors.t b/lib/configuration/tests/errors.t index bad5b3b..99ad465 100644 --- a/lib/configuration/tests/errors.t +++ b/lib/configuration/tests/errors.t @@ -215,3 +215,23 @@ Reference targets are validated the same way: in field "content": Expected an identifier with only the accepted characters, but got "bad\"target" + +A list ID follows the same rules as a content ID: + + $ tests --configuration < { + > "list": [ + > { + > "id": "foo.bar", + > "source": "csv" + > } + > ], + > "content": [] + > } + > EOF + in field "list": + while decoding a list: + element 0: + in field "id": + Expected an identifier with only the accepted characters, but got + "foo.bar" diff --git a/lib/model/model.ml b/lib/model/model.ml index ec28b56..25179ff 100644 --- a/lib/model/model.ml +++ b/lib/model/model.ml @@ -1,20 +1,24 @@ (* Entry point of the [model] library: re-export the submodules, and expose - the aliases for the model types with the references resolved to - [Reference.t] at the top level, so that use sites can write [Model.content] - instead of [Model.content]. *) + the resolved types at the top level, so that use sites can write + [Model.content] instead of spelling out the type parameters. *) module Identifier = Identifier module Dsl = Dsl module Typ = Typ module Reference = Reference +module List = List module Field = Field module Section = Section module Content = Content -type content = Reference.t Content.t -type section = Reference.t Section.t -type field = Reference.t Field.t -type field_ex = Reference.t Field.field_ex -type value = Reference.t Typ.value -type reference = Reference.t Typ.reference -type 'db typ = ('db, Reference.t) Typ.t +(** The target of a reference field, resolved: a lazy pointer to a content, or + a static list. *) +type target = Reference.t Typ.target + +type content = target Content.t +type section = target Section.t +type field = target Field.t +type field_ex = target Field.field_ex +type reference = target Typ.reference +type value = target Typ.value +type 'db typ = ('db, target) Typ.t diff --git a/lib/model/model.mli b/lib/model/model.mli index afc96f3..8c097f3 100644 --- a/lib/model/model.mli +++ b/lib/model/model.mli @@ -7,8 +7,9 @@ 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 (single or multivalued) — and optional - validation rules expressed in a small expression language (see {!Dsl}). + reference to another content or to a static {!List.t} (single or + multivalued) — and optional validation rules expressed in a small expression + language (see {!Dsl}). For example, this configuration: @@ -40,32 +41,42 @@ - {!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. - {!Dsl} — the expression language used in validation rules and display criteria. {2:aliases Resolved types} The aliases at the end of this file are the model types with references - resolved to {!Reference.t}. They are the types used everywhere outside this + resolved to {!target}. They are the types used everywhere outside this package: {!content} is what the configuration loader produces and what the query layer and the HTML renderer operate on. *) module Identifier = Identifier -(** Names of contents and fields. +(** Names of contents, fields and lists. The type is abstract: the only way to build one is {!Identifier.of_string}, which accepts a non-empty sequence of letters, digits, underscores and hyphens (rejecting in particular the double quote and the dot), so an identifier is always safe to embed in a quoted SQL identifier - ({!Identifier.quote}). *) + ({!Identifier.quote}). The fixed columns of the generated schema are + predefined: {!Identifier.id}, {!Identifier.parent}, {!Identifier.target}. + {!Identifier.dot} builds the composite name of the dedicated table of a + multivalued field, e.g. ["entity.user_type"]. *) module Content = Content -(** A content: the unit of the model. It has an {!Identifier.t} [id], a list of - {!Section.t}s, a list of sub-contents, and a weak link to its parent. Build - one with {!Content.v}; navigate with {!Content.parent} / {!Content.root}; - iterate over all its fields with {!Content.fields}. *) +(** A content: the unit of the model, editable by the user. It has an + {!Identifier.t} [id], a list of {!Section.t}s, a list of sub-contents, and a + weak link to its parent. Build one with {!Content.v}; navigate with + {!Content.parent} / {!Content.root}; iterate over all its fields with + {!Content.fields}. *) -type content = Reference.t Content.t +type target = Reference.t Typ.target +(** The target of a reference field, resolved: either a lazy pointer to a + content ({!Typ.Content}) or a static {!List.t} ({!Typ.List}). *) + +type content = target Content.t (** A content : the element produced by the configuration loader and manipulated by the query and rendering layers. *) @@ -75,18 +86,19 @@ module Typ = Typ A field type is one of: - {!Typ.Text} — a string, stored as a nullable column; - {!Typ.Integer} — an integer, stored as a nullable column; - - {!Typ.MonoReference} — a reference to a single content, stored as a - nullable foreign key; - - {!Typ.MultiReference} — a reference to several contents of the same kind, + - {!Typ.MonoReference} — a reference to a single target ({!target}), stored + as a nullable foreign key for a content, or as a plain column for a static + list; + - {!Typ.MultiReference} — a reference to several targets of the same kind, stored in a dedicated table ["content.field"]. A {!Typ.reference} record carries the target of a reference field (a - {!Reference.t}) plus optional [constraints] restricting the allowed values. - A {!Typ.value} is the data provided for a field when inserting/updating: + {!target}) plus optional [constraints] restricting the allowed values. A + {!Typ.value} is the data provided for a field when inserting/updating: {!Typ.None} (leave as is), {!Typ.V} (a single value) or {!Typ.Seq} (a sequence of references, for multivalued fields). *) -type 'db typ = ('db, Reference.t) Typ.t +type 'db typ = ('db, target) Typ.t (** A field type descriptor ; ['db] is the database representation of the field (e.g. [string option] for a text field, [int64] for a multivalued reference) *) @@ -97,24 +109,24 @@ module Reference = Reference The target is a lazy pointer ({!Reference.v} / {!Reference.get}): it is created while the configuration is parsed, before the target content necessarily exists, and resolved once all contents are registered. This is - what allows contents to reference each other, including cyclically. + what allows contents to reference each other, including cyclically. *) - This module also defines the aliases for the resolved model types - ({!Reference.content}, {!Reference.field}, ...), re-exported at the top - level of this package as {!content}, {!field}, ... *) +type reference = target Typ.reference +(** A reference field target: a {!target}, plus optional constraints *) -type reference = Reference.t Typ.reference -(** A reference field target: a lazy pointer to a {!content}, 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 Section = Section (** A group of fields displayed together, with a per-language [label] and an optional [criteria] controlling when the whole section is displayed. *) -type section = Reference.t Section.t +type section = target Section.t (** A section of {!content} *) -type value = Reference.t Typ.value +type value = target Typ.value (** A value provided for a field when inserting/updating data *) module Field = Field @@ -126,10 +138,10 @@ module Field = Field per-language error message) and a [criteria] controlling when the field is displayed. Build one with {!Field.create}. *) -type field = Reference.t Field.t +type field = target Field.t (** A field of {!content} *) -type field_ex = Reference.t Field.field_ex +type field_ex = target Field.field_ex (** The type descriptor of {!field} *) module Dsl = Dsl diff --git a/lib/model/reference.ml b/lib/model/reference.ml index 496eb9d..67bbcaf 100644 --- a/lib/model/reference.ml +++ b/lib/model/reference.ml @@ -1,4 +1,11 @@ -type t = T : t Content.t Lazy.t -> t [@@unboxed] +(** 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] -let v : (unit -> t Content.t) -> t = fun f -> T (lazy (f ())) -let get : t -> t Content.t = fun (T target) -> Lazy.force target +let v : (unit -> t Typ.target Content.t) -> t = + fun f -> T (lazy (f ())) + +let get : t -> t Typ.target Content.t = fun (T target) -> + Lazy.force target diff --git a/lib/model/reference.mli b/lib/model/reference.mli index 7a9d135..63acf6c 100644 --- a/lib/model/reference.mli +++ b/lib/model/reference.mli @@ -1,4 +1,4 @@ type t -val v : (unit -> t Content.t) -> t -val get : t -> t Content.t +val v : (unit -> t Typ.target Content.t) -> t +val get : t -> t Typ.target Content.t diff --git a/lib/model/typ.ml b/lib/model/typ.ml index 0e94378..9427b86 100644 --- a/lib/model/typ.ml +++ b/lib/model/typ.ml @@ -1,7 +1,16 @@ +(** 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 [Model.Reference.t], a lazy pointer to - the content. *) + (** The target of the reference: a {!target} at the resolved level. *) 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 diff --git a/lib/query/create.ml b/lib/query/create.ml index a40e722..8dcd10d 100644 --- a/lib/query/create.ml +++ b/lib/query/create.ml @@ -12,7 +12,10 @@ let get_field_query : _ Model.Field.t -> string option = match Model.Field.type_ field with | T (MultiReference _) -> None | T Integer -> Some "INTEGER" - | T (MonoReference _) -> Some "INTEGER" + | T (MonoReference _) -> + (* References are stored as the id of the target row, in the content + table or in the dedicated list table *) + Some "INTEGER" | T Text -> Some "VARCHAR" in Option.map @@ -36,16 +39,40 @@ ADD FOREIGN KEY (best_a_id) REFERENCES answer(a_id); *) -let get_foreign_statement : Model.Identifier.t -> Model.content -> string = - fun property content -> +let get_foreign_statement : + Model.Identifier.t -> Model.Identifier.t -> string = + fun property dst_table -> "FOREIGN KEY(" ^ Model.Identifier.quote property ^ ") REFERENCES " - ^ Model.Identifier.quote (Model.Content.id content) + ^ Model.Identifier.quote dst_table ^ "(" ^ 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@; @[@[( %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 -> @@ -62,14 +89,23 @@ let create_external_reference_table : | Model.Field.(T (MultiReference reference)) -> let buff = Buffer.create 16 in let format = Format.formatter_of_buffer buff in - let target = Model.Reference.get reference.Model.Typ.t in + (* The target is the id of a row in the target content table, or in the + dedicated table of the target list *) + let target_table = + match reference.Model.Typ.t with + | Model.Typ.Content ref -> + Model.Content.id (Model.Reference.get ref) + | Model.Typ.List list -> list.Model.List.id + in let fields = [ "\"id\" INTEGER PRIMARY KEY"; "\"parent\" INTEGER NOT NULL"; "\"target\" INTEGER NOT NULL"; - get_foreign_statement Model.Identifier.parent content; - get_foreign_statement Model.Identifier.target target; + get_foreign_statement Model.Identifier.parent + (Model.Content.id content); + (* Table constraints must come after the column definitions *) + get_foreign_statement Model.Identifier.target target_table; ] in let () = @@ -124,7 +160,8 @@ let rec create_table : the foreign key constraint *) let init_foreign = match Model.Content.parent content with - | Some parent -> [ get_foreign_statement Identifier.parent parent ] + | Some parent -> + [ get_foreign_statement Identifier.parent (Model.Content.id parent) ] | None -> [] in let foreign, alter_queries = @@ -132,28 +169,40 @@ let rec create_table : ~f:(fun (foreign, alter_queries) field -> match Model.Field.type_ field with | T (MonoReference reference) -> ( - let dst = Model.Reference.get reference.Model.Typ.t in let column = Model.Field.name field in - match List.mem ~set:created dst with - | true -> - let foreign = get_foreign_statement column dst :: foreign in + match reference.Model.Typ.t with + | Model.Typ.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 + in (foreign, alter_queries) - | false -> - (* If the table does not exists yet, add the constraint in the - ALTER TABLE list. + | Model.Typ.Content ref -> ( + let dst = Model.Reference.get ref in + match List.mem ~set:created dst with + | true -> + let foreign = + get_foreign_statement column (Model.Content.id dst) + :: foreign + in + (foreign, alter_queries) + | false -> + (* If the table does not exists yet, add the constraint in the + ALTER TABLE list. - This list will be processed later, once all the table are - created *) - let alter_queries = - { - src_table = Model.Content.id content; - src_column = column; - dst_table = Model.Content.id dst; - dst_column = Model.Identifier.id; - } - :: alter_queries - in - (foreign, alter_queries)) + This list will be processed later, once all the table are + created *) + let alter_queries = + { + src_table = Model.Content.id content; + src_column = column; + dst_table = Model.Content.id dst; + dst_column = Model.Identifier.id; + } + :: alter_queries + in + (foreign, alter_queries))) | _ -> (foreign, alter_queries)) in @@ -234,6 +283,11 @@ let add_foreign_statement : Caqti_lwt.connection -> foreign list -> unit Lwt.t = let create : Caqti_lwt.connection -> Configuration.Ast.setting -> unit Lwt.t = fun db conf -> 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) + in let contents = Configuration.Ast.root_contents conf in let* foreign, _ = Lwt_list.fold_left_s diff --git a/lib/query/tests/create.t b/lib/query/tests/create.t index 38a4630..46e3940 100644 --- a/lib/query/tests/create.t +++ b/lib/query/tests/create.t @@ -149,3 +149,77 @@ table but a dedicated table is created: , FOREIGN KEY("parent") REFERENCES "entity"("id") , FOREIGN KEY("target") REFERENCES "user_type_list"("id") ) +A declared list gets its own table, holding an id. A reference field pointing +to a list is an integer column constrained by a foreign key to the list table: + + $ tests --schema "user_type_list" "entity" < { + > "list": [ + > { + > "id": "user_type_list", + > "source": "csv" + > } + > ], + > "content": [ + > { + > "id": "entity", + > "section": [ + > { + > "field": [ + > { + > "name": "user_type", + > "label": {}, + > "type": { "content": "user_type_list" } + > } + > ] + > } + > ] + > } + > ] + > } + > EOF + CREATE TABLE "user_type_list" + ( "id" INTEGER PRIMARY KEY ) + CREATE TABLE "entity" + ( "id" INTEGER PRIMARY KEY + , "user_type" INTEGER + , FOREIGN KEY("user_type") REFERENCES "user_type_list"("id") ) + +A multivaluated reference to a list gets the usual dedicated table, with the +target column referencing the list table: + + $ tests --schema "entity.user_type" < { + > "list": [ + > { + > "id": "user_type_list", + > "source": "csv" + > } + > ], + > "content": [ + > { + > "id": "entity", + > "section": [ + > { + > "field": [ + > { + > "name": "user_type", + > "label": {}, + > "type": { + > "content": "user_type_list", + > "multivaluated": true + > } + > } + > ] + > } + > ] + > } + > ] + > } + > EOF + CREATE TABLE "entity.user_type" + ( "id" INTEGER PRIMARY KEY + , "parent" INTEGER NOT NULL + , "target" INTEGER NOT NULL + , FOREIGN KEY("parent") REFERENCES "entity"("id") + , FOREIGN KEY("target") REFERENCES "user_type_list"("id") ) diff --git a/lib/query/update.ml b/lib/query/update.ml index 2475d40..3c0941c 100644 --- a/lib/query/update.ml +++ b/lib/query/update.ml @@ -60,7 +60,7 @@ module ReferenceListEncoder = Encoder.Map (struct include Encoder.DefaultEncoder type 'a t = 'a - type env = (int64, Model.Reference.t) Model.Typ.t * Int64.t Seq.t ref + type env = (int64, Model.target) Model.Typ.t * Int64.t Seq.t ref let f : type a. env -> a Intf.data -> a t = fun (t, env) d -> @@ -78,7 +78,7 @@ module ReferencesSet = Set.Make (Int64) let update_mulivaluated_field : Caqti_lwt.connection -> Model.content -> - (Model.Identifier.t * (int64, Model.Reference.t) Model.Typ.t * int64 Seq.t) + (Model.Identifier.t * (int64, Model.target) Model.Typ.t * int64 Seq.t) list -> int64 -> unit Lwt.t = -- cgit v1.2.3