aboutsummaryrefslogtreecommitdiffstats
path: root/lib/qualified_name.ml
diff options
context:
space:
mode:
Diffstat (limited to 'lib/qualified_name.ml')
-rw-r--r--lib/qualified_name.ml31
1 files changed, 31 insertions, 0 deletions
diff --git a/lib/qualified_name.ml b/lib/qualified_name.ml
new file mode 100644
index 0000000..b6d01bb
--- /dev/null
+++ b/lib/qualified_name.ml
@@ -0,0 +1,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