WIP: working on querying and ordering with jsonb paths

Affects #118
This commit is contained in:
Joe Nelson
2015-03-30 00:51:52 -07:00
parent a1822a8e08
commit ca1c524ede
+26 -2
View File
@@ -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