diff --git a/src/PgQuery.hs b/src/PgQuery.hs index 9526cd24b..37a2cf4c0 100644 --- a/src/PgQuery.hs +++ b/src/PgQuery.hs @@ -140,7 +140,7 @@ update t cols vals = B.Stmt wherePred :: Net.QueryItem -> PStmt wherePred (col, predicate) = - B.Stmt (" " <> cs (pgFmtIdent $ cs col) <> " " <> op <> " " <> + B.Stmt (" " <> pgFmtJsonbPath (cs col) <> " " <> op <> " " <> if opCode `elem` ["is","isnot"] then whiteList value else cs sqlValue) empty True @@ -151,7 +151,6 @@ wherePred (col, predicate) = whiteList val = fromMaybe (cs (pgFmtLit val) <> "::unknown ") (L.find ((==) . T.toLower $ val) ["null","true","false"]) - star c = if c == '*' then '%' else c unknownLiteral = (<> "::unknown ") . pgFmtLit @@ -203,6 +202,31 @@ commaq = B.Stmt ", " empty True andq :: PStmt andq = B.Stmt " and " empty True +data JsonbPath = Identifier T.Text + | SingleArrow JsonbPath JsonbPath + | DoubleArrow JsonbPath JsonbPath + deriving (Show) + +parseJsonbPath :: T.Text -> Maybe JsonbPath +parseJsonbPath p = + case T.splitOn "->>" p of + [a,b] -> + let i:is = T.splitOn "->" a in + Just $ DoubleArrow + (foldl SingleArrow (Identifier i) (map Identifier is)) + (Identifier b) + _ -> Nothing + +pgFmtJsonbPath :: T.Text -> T.Text +pgFmtJsonbPath p = + pgFmtJsonbPath' $ fromMaybe (Identifier p) (parseJsonbPath p) + where + pgFmtJsonbPath' (Identifier i) = pgFmtIdent i + pgFmtJsonbPath' (SingleArrow a b) = + pgFmtJsonbPath' a <> "->" <> pgFmtJsonbPath' b + pgFmtJsonbPath' (DoubleArrow a b) = + pgFmtJsonbPath' a <> "->>" <> pgFmtJsonbPath' b + pgFmtIdent :: T.Text -> T.Text pgFmtIdent x = let escaped = T.replace "\"" "\"\"" (trimNullChars $ cs x) in