Merge pull request #276 from ruslantalpa/master

Support for &select=col1,col2,col3 as suggested in issue #227
This commit is contained in:
Joe Nelson
2015-09-06 15:24:47 -07:00
6 changed files with 114 additions and 11 deletions
+3 -3
View File
@@ -64,7 +64,7 @@ app conf reqBody req =
else do
let qt = qualify table
from = fromMaybe 0 $ rangeOffset <$> range
select = B.Stmt "select " V.empty True <>
query = B.Stmt "select " V.empty True <>
parentheticT (
whereT qt qq $ countRows qt
) <> commaq <> (
@@ -72,9 +72,9 @@ app conf reqBody req =
. limitT range
. orderT (orderParse qq)
. whereT qt qq
$ selectStar qt
$ select qt qq
)
row <- H.maybeEx select
row <- H.maybeEx query
let (tableTotal, queryTotal, body) =
fromMaybe (0, 0, Just "" :: Maybe Text) row
to = from+queryTotal-1
+24 -1
View File
@@ -58,7 +58,7 @@ whereT table params q =
then q
else q <> B.Stmt " where " empty True <> conjunction
where
cols = [ col | col <- params, fst col `notElem` ["order"] ]
cols = [ col | col <- params, fst col `notElem` ["order","select"] ]
wherePredTable = wherePred table
conjunction = mconcat $ L.intersperse andq (map wherePredTable cols)
@@ -129,6 +129,29 @@ asJsonRow s = s { B.stmtTemplate = "row_to_json(t) from (" <> B.stmtTemplate s <
selectStar :: QualifiedIdentifier -> PStmt
selectStar t = B.Stmt ("select * from " <> fromQi t) empty True
select :: QualifiedIdentifier -> Net.Query -> PStmt
select table params =
if L.null cols
then selectStar table
else B.Stmt "select " empty True <> conjunction <> B.Stmt (" from " <> fromQi table ) empty True
where
selectTermTable = selectTerm table
conjunction = mconcat $ L.intersperse commaq (map selectTermTable cols)
columnsParam = fromMaybe "" $ join (lookup "select" params)
cols = filter ((>0) . T.length) $ map T.strip $ T.split (==',') $ cs columnsParam
selectTerm :: QualifiedIdentifier -> T.Text -> PStmt
selectTerm table col =
case T.splitOn "::" col of
[colName,castTo] -> B.Stmt ("CAST (" <> pgFmtJsonbPath table (cs colName) <> " AS " <> castToSafe <> " )" <> asT (jsonbPath colName)) empty True
where castToSafe = T.filter ( `elem` ['a'..'z'] ) castTo
_-> B.Stmt (pgFmtJsonbPath table (cs col) <> asT (jsonbPath col)) empty True
where
jsonbPath :: T.Text -> Maybe JsonbPath
jsonbPath c = parseJsonbPath $ cs c
asT (Just (DoubleArrow _ (KeyIdentifier key))) = " AS " <> pgFmtIdent key
asT _ = ""
returningStarT :: StatementT
returningStarT s = s { B.stmtTemplate = B.stmtTemplate s <> " RETURNING *" }