summaryrefslogtreecommitdiff
path: root/lib/model/identifier.ml
diff options
context:
space:
mode:
Diffstat (limited to 'lib/model/identifier.ml')
-rw-r--r--lib/model/identifier.ml35
1 files changed, 35 insertions, 0 deletions
diff --git a/lib/model/identifier.ml b/lib/model/identifier.ml
new file mode 100644
index 0000000..f1a981e
--- /dev/null
+++ b/lib/model/identifier.ml
@@ -0,0 +1,35 @@
+type t = string
+
+exception Invalid of string
+
+let is_valid : string -> bool =
+ fun id ->
+ id <> ""
+ && String.for_all
+ (fun c ->
+ (c >= 'a' && c <= 'z')
+ || (c >= 'A' && c <= 'Z')
+ || (c >= '0' && c <= '9')
+ || c = '_' || c = '-')
+ id
+
+let of_string_opt : string -> t option =
+ fun id -> if is_valid id then Some id else None
+
+let of_string : string -> t =
+ fun id ->
+ match of_string_opt id with Some id -> id | None -> raise (Invalid id)
+
+let to_string : t -> string = fun id -> id
+
+(** The fixed columns of the generated schema, predefined so that the SQL
+ generation can refer to them directly. *)
+let id : t = of_string "id"
+let parent : t = of_string "parent"
+let target : t = of_string "target"
+let quote : t -> string = fun id -> "\"" ^ id ^ "\""
+let dot : t -> t -> t = fun a b -> a ^ "." ^ b
+let equal : t -> t -> bool = String.equal
+let compare : t -> t -> int = String.compare
+let hash : t -> int = Hashtbl.hash
+let pp : Format.formatter -> t -> unit = Format.pp_print_string