Reputation: 6796
I have some broken Haskell code and have never even heard of Haskell before now.
The code is generating a DB query string and is failing to properly quote the identifiers.
So I simply want to put quotes around the identifiers.
Here is the existing code:
(this part in a separate file):
data PgArg = PgArg {
pgaName :: Text
, pgaType :: Text
, pgaReq :: Bool
} deriving (Show, Eq, Ord)
(then the broken part in another file):
unwords [
"_args_record AS (",
"SELECT * FROM " <> (if isObject then "json_to_record" else "json_to_recordset") <> "($1)",
"AS _(" <> intercalate ", " ((\a -> pgaName a <> " " <> pgaType a) <$> pgArgs) <> ")",
")"]
I tried a few hacks, ultimately winding up with:
unwords [
"_args_record AS (",
"SELECT * FROM " <> (if isObject then "json_to_record" else "json_to_recordset") <> "($1)",
"AS _(" <> intercalate ", " ((\a -> (["\""] ++ (pgaName a) ++ ["\""]) <> " " <> pgaType a) <$> pgArgs) <> ")",
")"]
So basically I just changed:
pgaName a
To:
(["\""] ++ (pgaName a) ++ ["\""])
-- Anyway it fails with a type mismatch
error:
* Couldn't match expected type `[Char]' with actual type `Text'
* In the first argument of `(++)', namely `(pgaName a)'
In the second argument of `(++)', namely `(pgaName a) ++ ["\""]'
In the first argument of `(<>)', namely
`(["\""] ++ (pgaName a) ++ ["\""])'
Any ideas how to fix this correctly?
Upvotes: 0
Views: 400
Reputation: 34391
You faced the problem because (++)
operates on lists ([]
), and while String
s are just [Char]
, Text
is not.
Fortunately, there is generic append operator coming from Monoid
class called (<>)
. So, as @4castle said, just use "\"" <> pgaName a <> "\""
to concatenate Text
s.
Upvotes: 3