From 71dab115c9be7670878545671454060a1f5c758d Mon Sep 17 00:00:00 2001 From: Diogo Biazus Date: Wed, 2 Dec 2015 15:23:10 -0500 Subject: [PATCH] Uses qualified column name in order by clause to allow computed columns --- src/PostgREST/QueryBuilder.hs | 24 +++++++++++------------- test/Feature/QuerySpec.hs | 6 +++++- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/src/PostgREST/QueryBuilder.hs b/src/PostgREST/QueryBuilder.hs index 9b9481ea9..b819c2562 100644 --- a/src/PostgREST/QueryBuilder.hs +++ b/src/PostgREST/QueryBuilder.hs @@ -200,6 +200,17 @@ requestToQuery schema (DbRead (Node (Select colSelects tbls conditions ord, (mai ("WHERE " <> intercalate " AND " ( map (pgFmtCondition qi ) conditions )) `emptyOnNull` conditions, orderF (fromMaybe [] ord) ] + orderF ts = + if null ts + then "" + else "ORDER BY " <> clause + where + clause = intercalate "," (map queryTerm ts) + queryTerm :: OrderTerm -> Text + queryTerm t = " " + <> cs (pgFmtColumn qi $ otTerm t) <> " " + <> (cs.show) (otDirection t) <> " " + <> maybe "" (cs.show) (otNullOrder t) <> " " (withs, selects) = foldr getQueryParts ([],[]) forest getQueryParts :: Tree ReadNode -> ([(SqlFragment, Text)], [SqlFragment]) -> ([(SqlFragment,Text)], [SqlFragment]) getQueryParts (Node n@(_, (table, Just (Relation {relType=Child}))) forst) (w,s) = (w,sel:s) @@ -337,19 +348,6 @@ getJoinConditions (Relation t cols ft fcs typ lt lc1 lc2) = emptyOnNull :: Text -> [a] -> Text emptyOnNull val x = if null x then "" else val -orderF :: [OrderTerm] -> SqlFragment -orderF ts = - if null ts - then "" - else "ORDER BY " <> clause - where - clause = intercalate "," (map queryTerm ts) - queryTerm :: OrderTerm -> Text - queryTerm t = " " - <> cs (pgFmtIdent $ otTerm t) <> " " - <> (cs.show) (otDirection t) <> " " - <> maybe "" (cs.show) (otNullOrder t) <> " " - insertableValue :: JSON.Value -> SqlFragment insertableValue JSON.Null = "null" insertableValue v = (<> "::unknown") . pgFmtLit $ unquoted v diff --git a/test/Feature/QuerySpec.hs b/test/Feature/QuerySpec.hs index f4bca6c20..f238af540 100644 --- a/test/Feature/QuerySpec.hs +++ b/test/Feature/QuerySpec.hs @@ -130,7 +130,11 @@ spec = [json| [{"text_search_vector":"'baz':1 'qux':2"}] |] it "matches with computed column" $ - get "/items?always_true=eq.true" `shouldRespondWith` + get "/items?always_true=eq.true&order=id.asc" `shouldRespondWith` + [json| [{"id":1},{"id":2},{"id":3},{"id":4},{"id":5},{"id":6},{"id":7},{"id":8},{"id":9},{"id":10},{"id":11},{"id":12},{"id":13},{"id":14},{"id":15}] |] + + it "order by computed column" $ + get "/items?order=always_true.asc,id.asc" `shouldRespondWith` [json| [{"id":1},{"id":2},{"id":3},{"id":4},{"id":5},{"id":6},{"id":7},{"id":8},{"id":9},{"id":10},{"id":11},{"id":12},{"id":13},{"id":14},{"id":15}] |] it "matches filtering nested items" $