Safe Haskell | Safe-Inferred |
---|---|
Language | GHC2021 |
Database.Persist.Sql.Lifted.Expression.SubSelect
Synopsis
- subSelect :: (PersistField a, NullableFieldProjection a a') => SqlQuery (SqlExpr (Value a)) -> SqlExpr (Value (Maybe a'))
- subSelectMaybe :: PersistField a => SqlQuery (SqlExpr (Value (Maybe a))) -> SqlExpr (Value (Maybe a))
- subSelectCount :: (Num a, PersistField a) => SqlQuery ignored -> SqlExpr (Value a)
- subSelectForeign :: (BackendCompatible SqlBackend (PersistEntityBackend val1), PersistEntity val1, PersistEntity val2, PersistField a) => SqlExpr (Entity val2) -> EntityField val2 (Key val1) -> (SqlExpr (Entity val1) -> SqlExpr (Value a)) -> SqlExpr (Value a)
- subSelectList :: PersistField a => SqlQuery (SqlExpr (Value a)) -> SqlExpr (ValueList a)
- subSelectUnsafe :: PersistField a => SqlQuery (SqlExpr (Value a)) -> SqlExpr (Value a)
Documentation
subSelect :: (PersistField a, NullableFieldProjection a a') => SqlQuery (SqlExpr (Value a)) -> SqlExpr (Value (Maybe a')) #
Execute a subquery SELECT
in a SqlExpr
. The query passed to this
function will only return a single result - it has a LIMIT 1
passed in to
the query to make it safe, and the return type is Maybe
to indicate that
the subquery might result in 0 rows.
If you find yourself writing
, then consider using
joinV
. subSelect
subSelectMaybe
.
If you're performing a countRows
, then you can use subSelectCount
which
is safe.
If you know that the subquery will always return exactly one row (eg
a foreign key constraint guarantees that you'll get exactly one row), then
consider subSelectUnsafe
, along with a comment explaining why it is safe.
Since: esqueleto-3.2.0
subSelectMaybe :: PersistField a => SqlQuery (SqlExpr (Value (Maybe a))) -> SqlExpr (Value (Maybe a)) #
Execute a subquery SELECT
in a SqlExpr
. This function is a shorthand
for the common
idiom, where you are calling
joinV
. subSelect
subSelect
on an expression that would be Maybe
already.
As an example, you would use this function when calling sum_
or max_
,
which have Maybe
in the result type (for a 0 row query).
Since: esqueleto-3.2.0
subSelectCount :: (Num a, PersistField a) => SqlQuery ignored -> SqlExpr (Value a) #
Performs a COUNT
of the given query in a subSelect
manner. This is
always guaranteed to return a result value, and is completely safe.
Since: esqueleto-3.2.0
Arguments
:: (BackendCompatible SqlBackend (PersistEntityBackend val1), PersistEntity val1, PersistEntity val2, PersistField a) | |
=> SqlExpr (Entity val2) | An expression representing the table you have access to now. |
-> EntityField val2 (Key val1) | The foreign key field on the table. |
-> (SqlExpr (Entity val1) -> SqlExpr (Value a)) | A function to extract a value from the foreign reference table. |
-> SqlExpr (Value a) |
Performs a sub-select using the given foreign key on the entity. This is useful to extract values that are known to be present by the database schema.
As an example, consider the following persistent definition:
User profile ProfileId Profile name Text
The following query will return the name of the user.
getUserWithName =select
$from
$ user ->pure
(user,subSelectForeign
user UserProfile (^. ProfileName)
Since: esqueleto-3.2.0
subSelectList :: PersistField a => SqlQuery (SqlExpr (Value a)) -> SqlExpr (ValueList a) #
Execute a subquery SELECT
in a SqlExpr
that returns a list. This is an
alias for subList_select
and is provided for symmetry with the other safe
subselect functions.
Since: esqueleto-3.2.0
subSelectUnsafe :: PersistField a => SqlQuery (SqlExpr (Value a)) -> SqlExpr (Value a) #
Execute a subquery SELECT
in a SqlExpr
. This function is unsafe,
because it can throw runtime exceptions in two cases:
- If the query passed has 0 result rows, then it will return a
NULL
value. Thepersistent
parsing operations will fail on an unexpectedNULL
. - If the query passed returns more than one row, then the SQL engine will fail with an error like "More than one row returned by a subquery used as an expression".
This function is safe if you guarantee that exactly one row will be returned,
or if the result already has a Maybe
type for some reason.
For variants with the safety encoded already, see subSelect
and
subSelectMaybe
. For the most common safe use of this, see subSelectCount
.
Since: esqueleto-3.2.0