Support IN query param operator

This commit is contained in:
Joe Nelson
2015-03-01 18:30:08 -08:00
parent 9b15071f23
commit 6474a83221
2 changed files with 20 additions and 4 deletions
+12 -4
View File
@@ -138,15 +138,22 @@ update t cols vals = B.Stmt
wherePred :: Net.QueryItem -> PStmt wherePred :: Net.QueryItem -> PStmt
wherePred (col, predicate) = B.Stmt wherePred (col, predicate) = B.Stmt
(" " <> cs (pgFmtIdent $ cs col) <> " " <> op <> " " <> cs (pgFmtLit value) <> "::unknown ") (" " <> cs (pgFmtIdent $ cs col) <> " " <> op <> " " <> cs sqlValue)
empty True empty True
where where
opCode:rest = T.split (=='.') $ cs $ fromMaybe "." predicate opCode:rest = T.split (=='.') $ cs $ fromMaybe "." predicate
unStarredVal = T.intercalate "." rest value = T.intercalate "." rest
star c = if c == '*' then '%' else c star c = if c == '*' then '%' else c
value = if opCode == "like" || opCode == "ilike" unknownLiteral = (<> "::unknown ") . pgFmtLit
then T.map star unStarredVal else unStarredVal
sqlValue = case opCode of
"like" -> unknownLiteral $ T.map star value
"ilike" -> unknownLiteral $ T.map star value
"in" -> "(" <> T.intercalate ", " (map unknownLiteral $ T.split (==',') value) <> ") "
_ -> unknownLiteral value
op = case opCode of op = case opCode of
"eq" -> "=" "eq" -> "="
"gt" -> ">" "gt" -> ">"
@@ -156,6 +163,7 @@ wherePred (col, predicate) = B.Stmt
"neq" -> "<>" "neq" -> "<>"
"like"-> "like" "like"-> "like"
"ilike"-> "ilike" "ilike"-> "ilike"
"in" -> "in"
_ -> "=" _ -> "="
orderParse :: Net.Query -> [OrderTerm] orderParse :: Net.Query -> [OrderTerm]
+8
View File
@@ -38,6 +38,14 @@ spec = beforeAll testSet . afterAll_ (clearTable "items") . around withApp $ do
, matchHeaders = ["Content-Range" <:> "0-0/1"] , matchHeaders = ["Content-Range" <:> "0-0/1"]
} }
it "matches items IN" $
get "/items?id=in.1,3,5"
`shouldRespondWith` ResponseMatcher {
matchBody = Just [json| [{"id":1},{"id":3},{"id":5}] |]
, matchStatus = 200
, matchHeaders = ["Content-Range" <:> "0-2/3"]
}
it "matches with like" $ do it "matches with like" $ do
get "/no_pk?a=like.*yx" `shouldRespondWith` [json| get "/no_pk?a=like.*yx" `shouldRespondWith` [json|
[{"a":"xyyx","b":"u"}]|] [{"a":"xyyx","b":"u"}]|]