module IHP.Job.Dashboard.Utils where

import IHP.Prelude
import IHP.ModelSupport
import qualified Database.PostgreSQL.Simple as PG
import qualified Database.PostgreSQL.Simple.Types as PG

numberOfPagesForTable :: (?modelContext::ModelContext) => Text -> Int -> IO Int
numberOfPagesForTable :: (?modelContext::ModelContext) => Text -> Int -> IO Int
numberOfPagesForTable Text
table Int
pageSize = do
    Int
totalRecords <- (?modelContext::ModelContext) => Text -> IO Int
totalRecordsForTable Text
table
    forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ case Int
totalRecords forall a. Integral a => a -> a -> (a, a)
`quotRem` Int
pageSize of
        (Int
pages, Int
0) -> Int
pages
        (Int
pages, Int
_) -> Int
pages forall a. Num a => a -> a -> a
+ Int
1

totalRecordsForTable :: (?modelContext :: ModelContext) => Text -> IO Int
totalRecordsForTable :: (?modelContext::ModelContext) => Text -> IO Int
totalRecordsForTable Text
table = forall q value.
(?modelContext::ModelContext, ToRow q, FromField value) =>
Query -> q -> IO value
sqlQueryScalar (ByteString -> Query
PG.Query forall a b. (a -> b) -> a -> b
$ forall a b. ConvertibleStrings a b => a -> b
cs forall a b. (a -> b) -> a -> b
$ Text
"SELECT COUNT(*) FROM " forall a. Semigroup a => a -> a -> a
<> Text
table) ()