diff options
Diffstat (limited to 'lib/query')
| -rw-r--r-- | lib/query/create.ml | 108 | ||||
| -rw-r--r-- | lib/query/tests/create.t | 74 | ||||
| -rw-r--r-- | lib/query/update.ml | 4 |
3 files changed, 157 insertions, 29 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 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" <<EOF + > { + > "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" <<EOF + > { + > "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 = |
