|
| 1 | +namespace BioFSharp.INSDC.SQLite.Internal |
| 2 | + |
| 3 | +open Microsoft.Data.Sqlite |
| 4 | +open BioFSharp.FileFormats.INSDC |
| 5 | + |
| 6 | +/// Identity of the row family that holds an `Identifier` composite — five |
| 7 | +/// entity tables (`{entity}_identifiers`) and three reference-nested tables |
| 8 | +/// (`{owner}_{ref}_identifiers`) all share the same column shape, so the read |
| 9 | +/// and write paths are parameterized over the table to avoid eight near- |
| 10 | +/// duplicate modules. |
| 11 | +type IdentifierOwner = { |
| 12 | + /// Fully-qualified SQLite table name, e.g. `"bioproject_identifiers"`. |
| 13 | + Table: string |
| 14 | + /// FK column referencing the owning entity, e.g. `"bioproject_accession"`. |
| 15 | + AccessionColumn: string |
| 16 | + /// Accession value of the owning row that all written identifiers belong to. |
| 17 | + Accession: string |
| 18 | +} |
| 19 | + |
| 20 | +/// Persists / hydrates the `Identifier` composite (PrimaryId, SecondaryId[], |
| 21 | +/// ExternalId[], SubmitterId, Uuid[]) against a unified per-owner identifier |
| 22 | +/// table. The DB shape is one row per identifier kind+ordinal pair; the kind |
| 23 | +/// discriminator restores which slot in the F# composite a row populates. |
| 24 | +module Identifiers = |
| 25 | + |
| 26 | + [<Literal>] |
| 27 | + let private kindPrimary = "PRIMARY" |
| 28 | + |
| 29 | + [<Literal>] |
| 30 | + let private kindSecondary = "SECONDARY" |
| 31 | + |
| 32 | + [<Literal>] |
| 33 | + let private kindExternal = "EXTERNAL" |
| 34 | + |
| 35 | + [<Literal>] |
| 36 | + let private kindSubmitter = "SUBMITTER" |
| 37 | + |
| 38 | + [<Literal>] |
| 39 | + let private kindUuid = "UUID" |
| 40 | + |
| 41 | + let private insertRow |
| 42 | + (connection: SqliteConnection) |
| 43 | + (owner: IdentifierOwner) |
| 44 | + (kind: string) |
| 45 | + (ordinal: int) |
| 46 | + (value: string) |
| 47 | + (label: string) |
| 48 | + (ns: string) |
| 49 | + : unit = |
| 50 | + let sql = |
| 51 | + sprintf |
| 52 | + "INSERT INTO %s (%s, kind, ordinal, value, label, namespace) VALUES (@acc, @kind, @ordinal, @value, @label, @namespace);" |
| 53 | + owner.Table |
| 54 | + owner.AccessionColumn |
| 55 | + Sql.execNonQuery |
| 56 | + connection |
| 57 | + sql |
| 58 | + [ |
| 59 | + "@acc", box owner.Accession |
| 60 | + "@kind", box kind |
| 61 | + "@ordinal", box ordinal |
| 62 | + "@value", box value |
| 63 | + "@label", box label |
| 64 | + "@namespace", box ns |
| 65 | + ] |
| 66 | + |> ignore |
| 67 | + |
| 68 | + /// Writes every populated identifier on `identifiers` into the owner's |
| 69 | + /// identifier table. Null `identifiers` is treated as "nothing to write", |
| 70 | + /// matching the XSD where the whole IDENTIFIERS element is optional. |
| 71 | + let write (connection: SqliteConnection) (owner: IdentifierOwner) (identifiers: Identifier) : unit = |
| 72 | + if isNull identifiers then () else |
| 73 | + |
| 74 | + match identifiers.PrimaryId with |
| 75 | + | null -> () |
| 76 | + | n -> insertRow connection owner kindPrimary 0 n.Value n.Label null |
| 77 | + |
| 78 | + if not (isNull identifiers.SecondaryId) then |
| 79 | + identifiers.SecondaryId |
| 80 | + |> Seq.iteri (fun i n -> |
| 81 | + insertRow connection owner kindSecondary i n.Value n.Label null) |
| 82 | + |
| 83 | + if not (isNull identifiers.ExternalId) then |
| 84 | + identifiers.ExternalId |
| 85 | + |> Seq.iteri (fun i qn -> |
| 86 | + insertRow connection owner kindExternal i qn.Value qn.Label qn.Namespace) |
| 87 | + |
| 88 | + match identifiers.SubmitterId with |
| 89 | + | null -> () |
| 90 | + | qn -> insertRow connection owner kindSubmitter 0 qn.Value qn.Label qn.Namespace |
| 91 | + |
| 92 | + if not (isNull identifiers.Uuid) then |
| 93 | + identifiers.Uuid |
| 94 | + |> Seq.iteri (fun i n -> |
| 95 | + insertRow connection owner kindUuid i n.Value n.Label null) |
| 96 | + |
| 97 | + /// Reads every row in the owner's identifier table and reassembles the |
| 98 | + /// F# `Identifier` composite. Returns `null` when no rows exist — the |
| 99 | + /// generated INSDC types keep the IDENTIFIERS element as an optional |
| 100 | + /// reference, and CLR-`null` is what the XML serializer round-trips to. |
| 101 | + let read (connection: SqliteConnection) (owner: IdentifierOwner) : Identifier = |
| 102 | + let sql = |
| 103 | + sprintf |
| 104 | + "SELECT kind, ordinal, value, label, namespace FROM %s WHERE %s = @acc ORDER BY kind, ordinal;" |
| 105 | + owner.Table |
| 106 | + owner.AccessionColumn |
| 107 | + let rows = |
| 108 | + Sql.queryAll |
| 109 | + connection |
| 110 | + sql |
| 111 | + [ "@acc", box owner.Accession ] |
| 112 | + (fun reader -> |
| 113 | + let kind = reader.GetString(0) |
| 114 | + let value = reader.GetString(2) |
| 115 | + let label = Sql.readStringOrNull reader 3 |
| 116 | + let ns = Sql.readStringOrNull reader 4 |
| 117 | + kind, value, label, ns) |
| 118 | + |
| 119 | + if List.isEmpty rows then null else |
| 120 | + |
| 121 | + let identifiers = Identifier() |
| 122 | + for (kind, value, label, ns) in rows do |
| 123 | + match kind with |
| 124 | + | k when k = kindPrimary -> |
| 125 | + identifiers.PrimaryId <- Name(Value = value, Label = label) |
| 126 | + | k when k = kindSecondary -> |
| 127 | + identifiers.SecondaryId.Add(Name(Value = value, Label = label)) |
| 128 | + | k when k = kindExternal -> |
| 129 | + identifiers.ExternalId.Add(QualifiedName(Value = value, Label = label, Namespace = ns)) |
| 130 | + | k when k = kindSubmitter -> |
| 131 | + identifiers.SubmitterId <- QualifiedName(Value = value, Label = label, Namespace = ns) |
| 132 | + | k when k = kindUuid -> |
| 133 | + identifiers.Uuid.Add(Name(Value = value, Label = label)) |
| 134 | + | other -> |
| 135 | + failwithf "Unexpected identifier kind '%s' in %s for %s='%s'" other owner.Table owner.AccessionColumn owner.Accession |
| 136 | + identifiers |
0 commit comments