GaBuZoMeu/Registry/Utils/SQLite.hs

72 lines
3.9 KiB
Haskell
Raw Permalink Normal View History

2023-06-09 09:05:48 +02:00
module Registry.Utils.SQLite where
import Registry.Grammar
import Registry.Types
escape :: String -> String
escape = concatMap fixChar
where
fixChar '\'' = "''"
fixChar c = [c]
maybeToSQLite Nothing = "NULL"
2023-09-30 16:35:07 +02:00
maybeToSQLite (Just s) = "'" ++ (escape s) ++ "'"
2024-06-18 16:42:17 +02:00
maybeListToSQLite Nothing = "NULL"
maybeListToSQLite (Just s) = "'" ++ (foldr (++) "" (map escape s)) ++ "'"
2023-06-09 09:05:48 +02:00
-- TODO: use "SELECT last_insert_rowid();" is probably cleaner than
-- "SELECT max(oid)"
onedescrToSQLite "language" i s = "INSERT INTO Descriptions (description) " ++
" VALUES ('" ++ (escape s) ++ "');\n" ++
"INSERT INTO Descriptions_languages (description, lang) " ++
" SELECT max(oid), '" ++ i ++ "' FROM Descriptions;\n"
onedescrToSQLite "script" i s = "INSERT INTO Descriptions (description) " ++
" VALUES ('" ++ (escape s) ++ "');\n" ++
"INSERT INTO Descriptions_scripts (description, script) " ++
" SELECT max(oid), '" ++ i ++ "' FROM Descriptions;\n"
onedescrToSQLite "region" i s = "INSERT INTO Descriptions (description) " ++
" VALUES ('" ++ (escape s) ++ "');\n" ++
"INSERT INTO Descriptions_regions (description, region) " ++
" SELECT max(oid), '" ++ i ++ "' FROM Descriptions;\n"
onedescrToSQLite "variant" i s = "INSERT INTO Descriptions (description) " ++
" VALUES ('" ++ (escape s) ++ "');\n" ++
"INSERT INTO Descriptions_variants (description, variant) " ++
" SELECT max(oid), '" ++ i ++ "' FROM Descriptions;\n"
descrToSQLite recordtype a i = concat (map (onedescrToSQLite recordtype i) a)
toSQLite (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) ++ "', " ++
(maybeToSQLite (lang'script l)) ++ ", " ++
2023-09-30 16:35:07 +02:00
(maybeToSQLite (lang'preferredValue l)) ++ ", " ++
2023-09-30 17:31:16 +02:00
(maybeToSQLite (lang'deprecated l)) ++ ", " ++
(maybeListToSQLite (lang'comment l)) ++ ", " ++
2023-06-09 09:05:48 +02:00
"'" ++ (lang'added l) ++ "');\n" ++
(descrToSQLite "language" (lang'descr l) (lang'subtag l))
2023-09-30 17:31:16 +02:00
toSQLite (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
(maybeToSQLite (script'deprecated s)) ++ ", " ++
(maybeListToSQLite (script'comment s)) ++ ", " ++
2023-06-09 09:05:48 +02:00
"'" ++ (script'added s) ++ "');\n" ++
(descrToSQLite "script" (script'descr s) (script'subtag s))
2023-09-30 17:31:16 +02:00
toSQLite (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
(maybeToSQLite (region'deprecated r)) ++ ", " ++
(maybeListToSQLite (region'comment r)) ++ ", " ++
2023-06-09 09:05:48 +02:00
"'" ++ (region'added r) ++ "');\n" ++
(descrToSQLite "region" (region'descr r) (region'subtag r))
2023-09-30 17:31:16 +02:00
toSQLite (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
(maybeToSQLite (variant'deprecated v)) ++ ", " ++
(maybeListToSQLite (variant'comment v)) ++ ", " ++
2023-06-09 09:05:48 +02:00
"'" ++ (variant'added v) ++ "');\n" ++
(descrToSQLite "variant" (variant'descr v) (variant'subtag v))