From 32b97ef07654a3237d815109247b9f9a14ed82eb Mon Sep 17 00:00:00 2001 From: Diogo Biazus Date: Mon, 17 Aug 2015 22:04:05 -0400 Subject: [PATCH] Adds not as a keyword that can optionally be prepended to any operator in a parameter value --- src/PostgREST/PgQuery.hs | 9 ++++++--- test/Feature/QuerySpec.hs | 13 ++++++++++++- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/PostgREST/PgQuery.hs b/src/PostgREST/PgQuery.hs index 741cf94a3..b82d78015 100644 --- a/src/PostgREST/PgQuery.hs +++ b/src/PostgREST/PgQuery.hs @@ -182,14 +182,17 @@ callProc qi params = do wherePred :: QualifiedIdentifier -> Net.QueryItem -> PStmt wherePred table (col, predicate) = - B.Stmt (" " <> pgFmtJsonbPath table (cs col) <> " " <> op <> " " <> + B.Stmt (notOp <> " " <> pgFmtJsonbPath table (cs col) <> " " <> op <> " " <> if opCode `elem` ["is","isnot"] then whiteList value else cs sqlValue) empty True where - opCode:rest = T.split (=='.') $ cs $ fromMaybe "." predicate - value = T.intercalate "." rest + headPredicate:rest = T.split (=='.') $ cs $ fromMaybe "." predicate + hasNot caseTrue caseFalse = if headPredicate == "not" then caseTrue else caseFalse + opCode = hasNot (head rest) headPredicate + notOp = hasNot headPredicate "" + value = hasNot (T.intercalate "." $ tail rest) (T.intercalate "." rest) whiteList val = fromMaybe (cs (pgFmtLit val) <> "::unknown ") (L.find ((==) . T.toLower $ val) ["null","true","false"]) diff --git a/test/Feature/QuerySpec.hs b/test/Feature/QuerySpec.hs index 6256e195a..024ae307e 100644 --- a/test/Feature/QuerySpec.hs +++ b/test/Feature/QuerySpec.hs @@ -53,6 +53,14 @@ spec = , matchHeaders = ["Content-Range" <:> "0-2/3"] } + it "matches items NOT IN using not operator" $ + get "/items?id=not.in.2,4,6,7,8,9,10,11,12,13,14,15" + `shouldRespondWith` ResponseMatcher { + matchBody = Just [json| [{"id":1},{"id":3},{"id":5}] |] + , matchStatus = 200 + , matchHeaders = ["Content-Range" <:> "0-2/3"] + } + it "matches nulls in varchar and numeric fields alike" $ do get "/no_pk?a=is.null" `shouldRespondWith` [json| [{"a": null, "b": null}] |] @@ -166,12 +174,15 @@ spec = respHeaders `shouldSatisfy` matchHeader "Content-Location" "/simple_pk" - describe "jsonb" $ + describe "jsonb" $ do it "can filter by properties inside json column" $ do get "/json?data->foo->>bar=eq.baz" `shouldRespondWith` [json| [{"data": {"foo": {"bar": "baz"}}}] |] get "/json?data->foo->>bar=eq.fake" `shouldRespondWith` [json| [] |] + it "can filter by properties inside json column using not" $ + get "/json?data->foo->>bar=not.eq.baz" `shouldRespondWith` + [json| [] |] describe "remote procedure call" $ do context "a proc that returns a set" . before_ (clearTable "items" >> createItems 10) .