2023-06-09 09:05:48 +02:00
|
|
|
module Registry.Utils.PostgreSQL where
|
|
|
|
|
|
|
|
import Registry.Grammar
|
|
|
|
import Registry.Types
|
|
|
|
|
|
|
|
escape :: String -> String
|
|
|
|
escape = concatMap fixChar
|
|
|
|
where
|
|
|
|
fixChar '\'' = "\'\'"
|
|
|
|
fixChar c = [c]
|
|
|
|
|
|
|
|
maybeToPostgreSQL Nothing = "NULL"
|
2023-09-30 16:35:07 +02:00
|
|
|
maybeToPostgreSQL (Just s) = "'" ++ (escape s) ++ "'"
|
2023-06-09 09:05:48 +02:00
|
|
|
|
|
|
|
onedescrToPostgreSQL "language" i s = "INSERT INTO Descriptions (description) " ++
|
|
|
|
" VALUES ('" ++ (escape s) ++ "');\n" ++
|
|
|
|
"INSERT INTO Descriptions_languages (description, lang) " ++
|
|
|
|
" SELECT currval('Descriptions_id_seq'), '" ++ i ++ "';\n"
|
|
|
|
onedescrToPostgreSQL "script" i s = "INSERT INTO Descriptions (description) " ++
|
|
|
|
" VALUES ('" ++ (escape s) ++ "');\n" ++
|
|
|
|
"INSERT INTO Descriptions_scripts (description, script) " ++
|
|
|
|
" SELECT currval('Descriptions_id_seq'), '" ++ i ++ "';\n"
|
|
|
|
onedescrToPostgreSQL "region" i s = "INSERT INTO Descriptions (description) " ++
|
|
|
|
" VALUES ('" ++ (escape s) ++ "');\n" ++
|
|
|
|
"INSERT INTO Descriptions_regions (description, region) " ++
|
|
|
|
" SELECT currval('Descriptions_id_seq'), '" ++ i ++ "';\n"
|
|
|
|
onedescrToPostgreSQL "variant" i s = "INSERT INTO Descriptions (description) " ++
|
|
|
|
" VALUES ('" ++ (escape s) ++ "');\n" ++
|
|
|
|
"INSERT INTO Descriptions_variants (description, variant) " ++
|
|
|
|
" SELECT currval('Descriptions_id_seq'), '" ++ i ++ "';\n"
|
|
|
|
descrToPostgreSQL recordtype a i = concat (map (onedescrToPostgreSQL recordtype i) a)
|
|
|
|
|
|
|
|
toPostgreSQL (Lang l) = "INSERT INTO Languages (code, suppressscript, " ++
|
2023-09-30 17:31:16 +02:00
|
|
|
"preferredvalue, deprecated, comments, added) " ++
|
2023-06-09 09:05:48 +02:00
|
|
|
"VALUES (" ++
|
|
|
|
"'" ++ (lang'subtag l) ++ "', " ++
|
|
|
|
(maybeToPostgreSQL (lang'script l)) ++ ", " ++
|
2023-09-30 16:35:07 +02:00
|
|
|
(maybeToPostgreSQL (lang'preferredValue l)) ++ ", " ++
|
2023-09-30 17:31:16 +02:00
|
|
|
(maybeToPostgreSQL (lang'deprecated l)) ++ ", " ++
|
2023-09-30 16:35:07 +02:00
|
|
|
(maybeToPostgreSQL (lang'comment l)) ++ ", " ++
|
2023-06-09 09:05:48 +02:00
|
|
|
"'" ++ (lang'added l) ++ "');\n" ++
|
|
|
|
(descrToPostgreSQL "language" (lang'descr l) (lang'subtag l))
|
|
|
|
|
2023-09-30 17:31:16 +02:00
|
|
|
toPostgreSQL (Scr s) = "INSERT INTO Scripts (code, deprecated, comments, added) " ++
|
2023-06-09 09:05:48 +02:00
|
|
|
"VALUES (" ++
|
|
|
|
"'" ++ (script'subtag s) ++ "', " ++
|
2023-09-30 17:31:16 +02:00
|
|
|
(maybeToPostgreSQL (script'deprecated s)) ++ ", " ++
|
2023-09-30 16:35:07 +02:00
|
|
|
(maybeToPostgreSQL (script'comment s)) ++ ", " ++
|
2023-06-09 09:05:48 +02:00
|
|
|
"'" ++ (script'added s) ++ "');\n" ++
|
|
|
|
(descrToPostgreSQL "script" (script'descr s) (script'subtag s))
|
|
|
|
|
2023-09-30 17:31:16 +02:00
|
|
|
toPostgreSQL (Reg r) = "INSERT INTO Regions (code, deprecated, comments, added) " ++
|
2023-06-09 09:05:48 +02:00
|
|
|
"VALUES (" ++
|
|
|
|
"'" ++ (region'subtag r) ++ "', " ++
|
2023-09-30 17:31:16 +02:00
|
|
|
(maybeToPostgreSQL (region'deprecated r)) ++ ", " ++
|
2023-09-30 16:35:07 +02:00
|
|
|
(maybeToPostgreSQL (region'comment r)) ++ ", " ++
|
2023-06-09 09:05:48 +02:00
|
|
|
"'" ++ (region'added r) ++ "');\n" ++
|
|
|
|
(descrToPostgreSQL "region" (region'descr r) (region'subtag r))
|
|
|
|
|
2023-09-30 17:31:16 +02:00
|
|
|
toPostgreSQL (Var v) = "INSERT INTO Variants (code, deprecated, comments, added) " ++
|
2023-06-09 09:05:48 +02:00
|
|
|
"VALUES (" ++
|
|
|
|
"'" ++ (variant'subtag v) ++ "', " ++
|
2023-09-30 17:31:16 +02:00
|
|
|
(maybeToPostgreSQL (variant'deprecated v)) ++ ", " ++
|
2023-09-30 16:35:07 +02:00
|
|
|
(maybeToPostgreSQL (variant'comment v)) ++ ", " ++
|
2023-06-09 09:05:48 +02:00
|
|
|
"'" ++ (variant'added v) ++ "');\n" ++
|
|
|
|
(descrToPostgreSQL "variant" (variant'descr v) (variant'subtag v))
|
|
|
|
|