Safe Haskell | None |
---|---|
Language | Haskell2010 |
Postgres Execute Prepare
Deals with translating (session) variables to SQL expressions. Uses a state monad to keep track of things like variables and generating fresh variable names.
See Execute
.
Synopsis
- type PlanVariables = HashMap Name Int
- type PrepArgMap = IntMap (PrepArg, PGScalarValue)
- data PlanningSt = PlanningSt {}
- initPlanningSt :: PlanningSt
- prepareWithPlan :: (MonadState PlanningSt m, MonadError QErr m) => UserInfo -> UnpreparedValue ('Postgres pgKind) -> m SQLExp
- prepareWithoutPlan :: MonadError QErr m => UserInfo -> UnpreparedValue ('Postgres pgKind) -> m SQLExp
- withUserVars :: SessionVariables -> PrepArgMap -> PrepArgMap
- getVarArgNum :: MonadState PlanningSt m => Name -> m Int
- addPrepArg :: MonadState PlanningSt m => Int -> (PrepArg, PGScalarValue) -> m ()
- getNextArgNum :: MonadState PlanningSt m => m Int
Documentation
type PlanVariables = HashMap Name Int Source #
type PrepArgMap = IntMap (PrepArg, PGScalarValue) Source #
The value is (Q.PrepArg, PGScalarValue) because we want to log the human-readable value of the prepared argument and not the binary encoding in PG format
data PlanningSt Source #
Instances
Eq PlanningSt Source # | |
Defined in Hasura.Backends.Postgres.Execute.Prepare (==) :: PlanningSt -> PlanningSt -> Bool # (/=) :: PlanningSt -> PlanningSt -> Bool # | |
Show PlanningSt Source # | |
Defined in Hasura.Backends.Postgres.Execute.Prepare showsPrec :: Int -> PlanningSt -> ShowS # show :: PlanningSt -> String # showList :: [PlanningSt] -> ShowS # |
prepareWithPlan :: (MonadState PlanningSt m, MonadError QErr m) => UserInfo -> UnpreparedValue ('Postgres pgKind) -> m SQLExp Source #
If we're preparing a value with planning state, we favour referring to
values by their prepared argument index. If the value refers to a session
value, we look for it in prepared value (1) and access the particular keys
using the JSONB ->>
accessor.
prepareWithoutPlan :: MonadError QErr m => UserInfo -> UnpreparedValue ('Postgres pgKind) -> m SQLExp Source #
If we're not using a prepared statement, substitution is pretty naïve:
we resolve session variable names, ignore parameter names, and substitute
into the SQLExp
.
withUserVars :: SessionVariables -> PrepArgMap -> PrepArgMap Source #
The map of user session variables is always given the number (1) as its
variable argument number (see getVarArgNum
). If we want to refer to a
particular variable in this map, we use JSONB functions to interrogate
variable (1).
getVarArgNum :: MonadState PlanningSt m => Name -> m Int Source #
In prepared statements, we refer to variables by a number, not their name. If the statement already refers to a variable, then we'll already have a number for it, and so we just return that. Otherwise, we produce a new number, and that will refer to the variable from now on.
addPrepArg :: MonadState PlanningSt m => Int -> (PrepArg, PGScalarValue) -> m () Source #
Add a prepared argument to the prepared argument map. These are keyed by
the variable argument numbers, which can be computed using getVarArgNum
.
getNextArgNum :: MonadState PlanningSt m => m Int Source #
Get _psArgNumber
from inside the PlanningSt
and increment it for the
next operation. Think of this as a pure analogue to newUnique
.