Accept range headers

TODO: response headers and proper codes
This commit is contained in:
Joe Nelson
2014-08-02 15:27:45 -07:00
parent 6fa5cc2683
commit ce361643fa
3 changed files with 51 additions and 17 deletions
+10 -5
View File
@@ -7,6 +7,7 @@ import Data.Maybe (fromMaybe)
import Data.List (intercalate)
import Data.Monoid ((<>))
import qualified RangeQuery as R
import qualified Data.Text as T
import qualified Data.ByteString.Lazy as BL
import qualified Data.ByteString.Char8 as BS
@@ -16,20 +17,24 @@ import Database.HDBC.PostgreSQL
import Network.HTTP.Types.URI
selectWhere :: T.Text -> T.Text -> Query -> Connection -> IO BL.ByteString
selectWhere ver table qq conn = do
selectWhere :: T.Text -> T.Text -> Query -> Maybe R.NonnegRange -> Connection -> IO BL.ByteString
selectWhere ver table qq range conn = do
s <- selectSql
w <- whereClause conn qq
r <- quickQuery conn (BS.unpack $ s <> w) []
return $ case r of
[[json]] -> fromSql json
_ -> "" :: BL.ByteString
where
limit = fromMaybe "ALL" $ show <$> (R.limit =<< range)
offset = fromMaybe 0 (R.offset <$> range)
selectSql = pgFormat conn
"select array_to_json(array_agg(row_to_json(t)))\
\ from (select * from %I.%I) t"
[toSql ver, toSql table]
\ from (select * from %I.%I LIMIT %s OFFSET %s) t"
[toSql ver, toSql table, toSql limit, toSql offset]
whereClause :: Connection -> Query -> IO BS.ByteString
whereClause _ [] = return ""
@@ -41,7 +46,7 @@ whereClause conn qs =
clause = BS.intercalate " and " <$> preds
preds :: IO [BS.ByteString]
preds = sequence $ map (wherePred conn) qs
preds = mapM (wherePred conn) qs
wherePred :: Connection -> QueryItem -> IO BS.ByteString