Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor pagination SQL queries to use generic paramaters #2021

Merged
merged 2 commits into from
Dec 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 12 additions & 18 deletions IHP/Pagination/ControllerFunctions.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,12 @@ module IHP.Pagination.ControllerFunctions
import IHP.Prelude
import IHP.Controller.Context
import IHP.Controller.Param ( paramOrDefault, paramOrNothing )

import IHP.Pagination.Types
( Options(..), Pagination(..) )

import IHP.QueryBuilder
( HasQueryBuilder, filterWhereILike, limit, offset )
import IHP.Pagination.Types ( Options(..), Pagination(..) )
import IHP.QueryBuilder ( HasQueryBuilder, filterWhereILike, limit, offset )
import IHP.Fetch (fetchCount)

import IHP.ModelSupport (GetModelByTableName, sqlQuery, sqlQueryScalar, Table)

import Database.PostgreSQL.Simple.ToField (toField, Action)
import Database.PostgreSQL.Simple.Types (Query(Query))
import Database.PostgreSQL.Simple (FromRow, ToRow, Query(..), Only(Only), (:.)(..))

-- | Paginate a query, with the following default options:
--
Expand Down Expand Up @@ -179,12 +173,12 @@ defaultPaginationOptions =
--
-- *AutoRefresh:* When using 'paginatedSqlQuery' with AutoRefresh, you need to use 'trackTableRead' to let AutoRefresh know that you have accessed a certain table. Otherwise AutoRefresh will not watch table of your custom sql query.
paginatedSqlQuery
:: forall model
. ( FromRow model
:: ( FromRow model
, ToRow parameters
, ?context :: ControllerContext
, ?modelContext :: ModelContext
)
=> ByteString -> [Action] -> IO ([model], Pagination)
=> Query -> parameters -> IO ([model], Pagination)
paginatedSqlQuery = paginatedSqlQueryWithOptions defaultPaginationOptions

-- | Runs a raw sql query and adds pagination to it.
Expand All @@ -202,14 +196,14 @@ paginatedSqlQuery = paginatedSqlQueryWithOptions defaultPaginationOptions
--
-- *AutoRefresh:* When using 'paginatedSqlQuery' with AutoRefresh, you need to use 'trackTableRead' to let AutoRefresh know that you have accessed a certain table. Otherwise AutoRefresh will not watch table of your custom sql query.
paginatedSqlQueryWithOptions
:: forall model
. ( FromRow model
:: ( FromRow model
, ToRow parameters
, ?context :: ControllerContext
, ?modelContext :: ModelContext
)
=> Options -> ByteString -> [Action] -> IO ([model], Pagination)
=> Options -> Query -> parameters -> IO ([model], Pagination)
paginatedSqlQueryWithOptions options sql placeholders = do
count :: Int <- sqlQueryScalar (Query $ "SELECT count(subquery.*) FROM (" <> sql <> ") as subquery") placeholders
count :: Int <- sqlQueryScalar ("SELECT count(subquery.*) FROM (" <> sql <> ") as subquery") placeholders

let pageSize = pageSize' options
pagination = Pagination
Expand All @@ -220,8 +214,8 @@ paginatedSqlQueryWithOptions options sql placeholders = do
}

results :: [model] <- sqlQuery
(Query $ "SELECT subquery.* FROM (" <> sql <> ") as subquery LIMIT ? OFFSET ?")
(placeholders ++ map toField [pageSize, offset' pageSize page])
("SELECT subquery.* FROM (" <> sql <> ") as subquery LIMIT ? OFFSET ?")
(placeholders :. Only pageSize :. Only (offset' pageSize page))

pure (results, pagination)

Expand Down
Loading