diff options
| author | Sébastien Dailly <sebastien@dailly.me> | 2026-11-19 09:57:49 +0100 |
|---|---|---|
| committer | Sébastien Dailly <sebastien@dailly.me> | 2026-11-19 09:57:49 +0100 |
| commit | e1bf7686f408b52a870d4ed4f3c565dbbcc3954f (patch) | |
| tree | ee7e156032cc0c43ca10879051e64c3dfeab1515 /lib/query/create.ml | |
| parent | a9628fda0fc027c54b5ed5313a0c41731d79e41f (diff) | |
Management of lists in the settings
Diffstat (limited to 'lib/query/create.ml')
| -rw-r--r-- | lib/query/create.ml | 108 |
1 files changed, 81 insertions, 27 deletions
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@; @[@[<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 -> @@ -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 |
