diff options
Diffstat (limited to 'lib/query/create.ml')
| -rw-r--r-- | lib/query/create.ml | 125 |
1 files changed, 87 insertions, 38 deletions
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 _ -> |
