Merge pull request #400 from diogob/order_by_computed_column

Order by computed column
This commit is contained in:
Joe Nelson
2015-12-02 19:19:02 -08:00
5 changed files with 29 additions and 14 deletions
+3
View File
@@ -9,6 +9,9 @@ This project adheres to [Semantic Versioning](http://semver.org/).
- Miscalculation of time used for expiring tokens - @calebmer
- Remove bcrypt dependency to fix Windows build - @begriffs
### Added
- Allow order by computed columns - @diogob
## [0.3.0.1] - 2015-11-27
### Fixed
+5
View File
@@ -171,6 +171,11 @@ If you care where nulls are sorted, add `nullsfirst` or `nullslast`:
GET /people?order=age.nullsfirst
```
You can also use [computed
columns](http://www.postgresql.org/docs/current/interactive/xfunc-sql.html#XFUNC-SQL-COMPOSITE-FUNCTIONS)
to order the results, even though the computed
columns will not appear in the output.
### Limiting and Pagination
#### Pagination by Limit-Offset
+11 -13
View File
@@ -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
+5 -1
View File
@@ -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=anti_id.desc" `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" $
+5
View File
@@ -462,6 +462,11 @@ CREATE FUNCTION public.always_true(test.items) RETURNS boolean
ALTER FUNCTION public.always_true(test.items) OWNER TO postgrest_test;
CREATE FUNCTION public.anti_id(test.items) RETURNS bigint
LANGUAGE sql STABLE
AS $$ SELECT $1.id * -1 $$;
ALTER FUNCTION public.anti_id(test.items) OWNER TO postgrest_test;
ALTER TABLE ONLY authors_only