From f4e0c12cba5e35cc6ec8fab9c2e714a3fad66d54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Crist=C3=B3v=C3=A3o?= Date: Thu, 12 Feb 2015 11:31:56 +0000 Subject: [PATCH 1/2] IS/IS NOT null, true, false --- src/PgQuery.hs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/PgQuery.hs b/src/PgQuery.hs index bcec5c114..38a230156 100644 --- a/src/PgQuery.hs +++ b/src/PgQuery.hs @@ -139,13 +139,18 @@ update t cols vals = B.Stmt empty True wherePred :: Net.QueryItem -> PStmt -wherePred (col, predicate) = B.Stmt - (" " <> cs (pgFmtIdent $ cs col) <> " " <> op <> " " <> cs sqlValue) - empty True +wherePred (col, predicate) = + B.Stmt (" " <> cs (pgFmtIdent $ 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 + 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 @@ -166,6 +171,8 @@ wherePred (col, predicate) = B.Stmt "like"-> "like" "ilike"-> "ilike" "in" -> "in" + "is" -> "is" + "isnot" -> "is not" _ -> "=" orderParse :: Net.Query -> [OrderTerm] From f7ff08edf763ca665efdbf6c2acb5bc0a2ed6c5c Mon Sep 17 00:00:00 2001 From: Joe Nelson Date: Sun, 15 Mar 2015 15:13:15 -0700 Subject: [PATCH 2/2] Test is.null matcher --- CHANGELOG.md | 3 ++- test/Feature/QuerySpec.hs | 4 ++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 22062fcae..9f92b46d0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,8 @@ This project adheres to [Semantic Versioning](http://semver.org/). ## Unreleased ### Added -- Option to specify nulls first or last, eg /people?order=age.desc.nullsfirst +- Option to specify nulls first or last, eg `/people?order=age.desc.nullsfirst` +- Filter nulls, `?col=is.null` and `?col=isnot.null` ## [0.2.7.0] - 2015-03-03 ### Added diff --git a/test/Feature/QuerySpec.hs b/test/Feature/QuerySpec.hs index 2029be092..2bfcbaa78 100644 --- a/test/Feature/QuerySpec.hs +++ b/test/Feature/QuerySpec.hs @@ -34,6 +34,10 @@ spec = , matchHeaders = ["Content-Range" <:> "0-2/3"] } + it "matches nulls" $ + get "/no_pk?a=is.null" `shouldRespondWith` + [json| [{"a": null, "b": null}] |] + it "matches with like" $ do get "/simple_pk?k=like.*yx" `shouldRespondWith` "[{\"k\":\"xyyx\",\"extra\":\"u\"}]"