2018-12-13 10:26:15 +03:00
|
|
|
module Hasura.RQL.DML.Count
|
|
|
|
( CountQueryP1(..)
|
|
|
|
, validateCountQWith
|
|
|
|
, validateCountQ
|
|
|
|
, runCount
|
|
|
|
, countQToTx
|
|
|
|
) where
|
2018-06-27 16:11:32 +03:00
|
|
|
|
2020-08-27 19:36:39 +03:00
|
|
|
import Hasura.Prelude
|
|
|
|
|
2020-11-02 14:50:40 +03:00
|
|
|
import qualified Data.ByteString.Builder as BB
|
|
|
|
import qualified Data.Sequence as DS
|
2020-10-27 16:53:49 +03:00
|
|
|
|
2020-12-28 15:56:00 +03:00
|
|
|
import Control.Monad.Trans.Control (MonadBaseControl)
|
2018-06-27 16:11:32 +03:00
|
|
|
import Data.Aeson
|
|
|
|
|
2020-11-02 14:50:40 +03:00
|
|
|
import qualified Database.PG.Query as Q
|
|
|
|
import qualified Hasura.Backends.Postgres.SQL.DML as S
|
2020-12-28 15:56:00 +03:00
|
|
|
import qualified Hasura.Tracing as Tracing
|
2018-06-27 16:11:32 +03:00
|
|
|
|
2020-10-27 16:53:49 +03:00
|
|
|
import Hasura.Backends.Postgres.SQL.Types
|
2020-11-02 14:50:40 +03:00
|
|
|
import Hasura.Backends.Postgres.Translate.BoolExp
|
2021-05-11 18:18:31 +03:00
|
|
|
import Hasura.Base.Error
|
2019-03-18 19:22:21 +03:00
|
|
|
import Hasura.EncJSON
|
2018-06-27 16:11:32 +03:00
|
|
|
import Hasura.RQL.DML.Internal
|
2020-11-12 12:25:48 +03:00
|
|
|
import Hasura.RQL.DML.Types
|
|
|
|
import Hasura.RQL.IR.BoolExp
|
2018-06-27 16:11:32 +03:00
|
|
|
import Hasura.RQL.Types
|
2020-12-28 15:56:00 +03:00
|
|
|
import Hasura.RQL.Types.Run
|
2018-06-27 16:11:32 +03:00
|
|
|
import Hasura.SQL.Types
|
2021-01-09 02:09:15 +03:00
|
|
|
import Hasura.Session
|
2018-06-27 16:11:32 +03:00
|
|
|
|
|
|
|
|
|
|
|
data CountQueryP1
|
|
|
|
= CountQueryP1
|
|
|
|
{ cqp1Table :: !QualifiedTable
|
2021-04-22 00:44:37 +03:00
|
|
|
, cqp1Where :: !(AnnBoolExpSQL ('Postgres 'Vanilla), Maybe (AnnBoolExpSQL ('Postgres 'Vanilla)))
|
2018-06-27 16:11:32 +03:00
|
|
|
, cqp1Distinct :: !(Maybe [PGCol])
|
2020-10-22 23:42:27 +03:00
|
|
|
} deriving (Eq)
|
2018-06-27 16:11:32 +03:00
|
|
|
|
|
|
|
mkSQLCount
|
|
|
|
:: CountQueryP1 -> S.Select
|
|
|
|
mkSQLCount (CountQueryP1 tn (permFltr, mWc) mDistCols) =
|
|
|
|
S.mkSelect
|
2018-11-14 15:59:59 +03:00
|
|
|
{ S.selExtr = [S.Extractor S.countStar Nothing]
|
2018-06-27 16:11:32 +03:00
|
|
|
, S.selFrom = Just $ S.FromExp
|
|
|
|
[S.mkSelFromExp False innerSel $ TableName "r"]
|
|
|
|
}
|
|
|
|
where
|
|
|
|
|
|
|
|
finalWC =
|
2018-11-16 15:40:23 +03:00
|
|
|
toSQLBoolExp (S.QualTable tn) $
|
|
|
|
maybe permFltr (andAnnBoolExps permFltr) mWc
|
2018-06-27 16:11:32 +03:00
|
|
|
|
|
|
|
innerSel = partSel
|
|
|
|
{ S.selFrom = Just $ S.mkSimpleFromExp tn
|
|
|
|
, S.selWhere = S.WhereFrag <$> Just finalWC
|
|
|
|
}
|
|
|
|
|
|
|
|
partSel = case mDistCols of
|
|
|
|
Just distCols ->
|
|
|
|
let extrs = flip map distCols $ \c -> S.Extractor (S.mkSIdenExp c) Nothing
|
|
|
|
in S.mkSelect
|
|
|
|
{ S.selDistinct = Just S.DistinctSimple
|
|
|
|
, S.selExtr = extrs
|
|
|
|
}
|
|
|
|
Nothing -> S.mkSelect
|
2020-01-16 07:53:28 +03:00
|
|
|
{ S.selExtr = [S.Extractor (S.SEStar Nothing) Nothing] }
|
2018-06-27 16:11:32 +03:00
|
|
|
|
|
|
|
-- SELECT count(*) FROM (SELECT DISTINCT c1, .. cn FROM .. WHERE ..) r;
|
|
|
|
-- SELECT count(*) FROM (SELECT * FROM .. WHERE ..) r;
|
2018-12-13 10:26:15 +03:00
|
|
|
validateCountQWith
|
2021-04-22 00:44:37 +03:00
|
|
|
:: (UserInfoM m, QErrM m, TableInfoRM ('Postgres 'Vanilla) m)
|
|
|
|
=> SessVarBldr ('Postgres 'Vanilla) m
|
|
|
|
-> (ColumnType ('Postgres 'Vanilla) -> Value -> m S.SQLExp)
|
2018-06-27 16:11:32 +03:00
|
|
|
-> CountQuery
|
|
|
|
-> m CountQueryP1
|
2021-01-07 12:04:22 +03:00
|
|
|
validateCountQWith sessVarBldr prepValBldr (CountQuery qt _ mDistCols mWhere) = do
|
2020-12-28 15:56:00 +03:00
|
|
|
tableInfo <- askTabInfoSource qt
|
2018-06-27 16:11:32 +03:00
|
|
|
|
|
|
|
-- Check if select is allowed
|
|
|
|
selPerm <- modifyErr (<> selNecessaryMsg) $
|
|
|
|
askSelPermInfo tableInfo
|
|
|
|
|
2019-11-20 21:21:30 +03:00
|
|
|
let colInfoMap = _tciFieldInfoMap $ _tiCoreInfo tableInfo
|
2018-06-27 16:11:32 +03:00
|
|
|
|
|
|
|
forM_ mDistCols $ \distCols -> do
|
|
|
|
let distColAsrns = [ checkSelOnCol selPerm
|
|
|
|
, assertPGCol colInfoMap relInDistColsErr]
|
|
|
|
withPathK "distinct" $ verifyAsrns distColAsrns distCols
|
|
|
|
|
|
|
|
-- convert the where clause
|
|
|
|
annSQLBoolExp <- forM mWhere $ \be ->
|
|
|
|
withPathK "where" $
|
2021-04-19 15:16:10 +03:00
|
|
|
convBoolExp colInfoMap selPerm be sessVarBldr qt (valueParserWithCollectableType prepValBldr)
|
2019-04-17 12:48:41 +03:00
|
|
|
|
|
|
|
resolvedSelFltr <- convAnnBoolExpPartialSQL sessVarBldr $
|
|
|
|
spiFilter selPerm
|
2018-06-27 16:11:32 +03:00
|
|
|
|
|
|
|
return $ CountQueryP1
|
|
|
|
qt
|
2019-04-17 12:48:41 +03:00
|
|
|
(resolvedSelFltr, annSQLBoolExp)
|
2018-06-27 16:11:32 +03:00
|
|
|
mDistCols
|
|
|
|
where
|
|
|
|
selNecessaryMsg =
|
|
|
|
"; \"count\" is only allowed if the role "
|
|
|
|
<> "has \"select\" permissions on the table"
|
|
|
|
relInDistColsErr =
|
|
|
|
"Relationships can't be used in \"distinct\"."
|
|
|
|
|
2018-12-13 10:26:15 +03:00
|
|
|
validateCountQ
|
2019-11-20 21:21:30 +03:00
|
|
|
:: (QErrM m, UserInfoM m, CacheRM m)
|
2021-01-07 12:04:22 +03:00
|
|
|
=> CountQuery -> m (CountQueryP1, DS.Seq Q.PrepArg)
|
|
|
|
validateCountQ query = do
|
|
|
|
let source = cqSource query
|
2021-04-22 00:44:37 +03:00
|
|
|
tableCache :: TableCache ('Postgres 'Vanilla) <- askTableCache source
|
2020-12-28 15:56:00 +03:00
|
|
|
flip runTableCacheRT (source, tableCache) $ runDMLP1T $
|
|
|
|
validateCountQWith sessVarFromCurrentSetting binRHSBuilder query
|
2018-12-13 10:26:15 +03:00
|
|
|
|
|
|
|
countQToTx
|
|
|
|
:: (QErrM m, MonadTx m)
|
2019-03-18 19:22:21 +03:00
|
|
|
=> (CountQueryP1, DS.Seq Q.PrepArg) -> m EncJSON
|
2018-12-13 10:26:15 +03:00
|
|
|
countQToTx (u, p) = do
|
2018-11-16 15:40:23 +03:00
|
|
|
qRes <- liftTx $ Q.rawQE dmlTxErrorHandler
|
|
|
|
(Q.fromBuilder countSQL) (toList p) True
|
2019-03-18 19:22:21 +03:00
|
|
|
return $ encJFromBuilder $ encodeCount qRes
|
2018-06-27 16:11:32 +03:00
|
|
|
where
|
|
|
|
countSQL = toSQL $ mkSQLCount u
|
|
|
|
encodeCount (Q.SingleRow (Identity c)) =
|
|
|
|
BB.byteString "{\"count\":" <> BB.intDec c <> BB.char7 '}'
|
|
|
|
|
2018-12-13 10:26:15 +03:00
|
|
|
runCount
|
2020-12-28 15:56:00 +03:00
|
|
|
:: ( QErrM m, UserInfoM m, CacheRM m
|
|
|
|
, MonadIO m, MonadBaseControl IO m
|
2021-03-02 07:26:31 +03:00
|
|
|
, Tracing.MonadTrace m, MetadataM m
|
2020-12-28 15:56:00 +03:00
|
|
|
)
|
2021-01-07 12:04:22 +03:00
|
|
|
=> CountQuery -> m EncJSON
|
|
|
|
runCount q = do
|
2021-04-22 00:44:37 +03:00
|
|
|
sourceConfig <- askSourceConfig @('Postgres 'Vanilla) (cqSource q)
|
2021-01-07 12:04:22 +03:00
|
|
|
validateCountQ q >>= runQueryLazyTx (_pscExecCtx sourceConfig) Q.ReadOnly . countQToTx
|