summaryrefslogtreecommitdiff
path: root/lib/query
diff options
context:
space:
mode:
Diffstat (limited to 'lib/query')
-rw-r--r--lib/query/create.ml125
-rw-r--r--lib/query/tests/create.t40
2 files changed, 123 insertions, 42 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 _ ->
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") )