aboutsummaryrefslogtreecommitdiffstats
path: root/lib/qualified_name.ml
blob: b6d01bbd15360bd4c423869441734d9f039b620f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
(** Fully qualified relation name: [multigroup:schema:relation].

    Splits on [:], fills defaults for missing parts.
    - ["relation"]             -> { multigroup=""; schema="public"; relation }
    - ["schema:relation"]      -> { multigroup=""; schema; relation }
    - ["mg:schema:relation"]   -> { multigroup=mg; schema; relation }
*)

type t = {
  multigroup : string;
  schema : string;
  relation : string;
}

let default_schema = "public"

let parse s =
  match String.split_on_char ':' s with
  | [ rel ] -> { multigroup = ""; schema = default_schema; relation = rel }
  | [ schema; rel ] -> { multigroup = ""; schema; relation = rel }
  | [ mg; schema; rel ] -> { multigroup = mg; schema; relation = rel }
  | _ -> { multigroup = ""; schema = default_schema; relation = s }

let make ?(multigroup = "") ?(schema = default_schema) relation =
  { multigroup; schema; relation }

let to_string { multigroup; schema; relation } =
  if multigroup = "" then schema ^ ":" ^ relation
  else multigroup ^ ":" ^ schema ^ ":" ^ relation

let to_key { schema; relation; _ } = schema ^ ":" ^ relation