module Database.Beam.Query.Relationships
(
ManyToMany, ManyToManyThrough
, manyToMany_, manyToManyPassthrough_
, OneToMany, OneToManyOptional
, oneToMany_, oneToManyOptional_
, OneToOne, OneToMaybe
, oneToOne_, oneToMaybe_ ) where
import Database.Beam.Query.Combinators
import Database.Beam.Query.Operator
import Database.Beam.Query.Internal
import Database.Beam.Query.Ord
import Database.Beam.Schema
import Database.Beam.Backend.SQL
type OneToOne be db s one many = OneToMany be db s one many
type OneToMany be db s one many =
( BeamSqlBackend be, BeamSqlBackendCanSerialize be Bool ) =>
one (QExpr be s) -> Q be db s (many (QExpr be s))
type OneToMaybe be db s tbl rel = OneToManyOptional be db s tbl rel
type OneToManyOptional be db s tbl rel =
( BeamSqlBackend be, BeamSqlBackendCanSerialize be Bool
, BeamSqlBackendCanSerialize be SqlNull ) =>
tbl (QExpr be s) -> Q be db s (rel (Nullable (QExpr be s)))
oneToMany_, oneToOne_
:: ( Database be db, BeamSqlBackend be
, HasTableEquality be (PrimaryKey tbl)
, Table tbl, Table rel )
=> DatabaseEntity be db (TableEntity rel)
-> (rel (QExpr be s) -> PrimaryKey tbl (QExpr be s))
-> tbl (QExpr be s)
-> Q be db s (rel (QExpr be s))
oneToMany_ :: DatabaseEntity be db (TableEntity rel)
-> (rel (QExpr be s) -> PrimaryKey tbl (QExpr be s))
-> tbl (QExpr be s)
-> Q be db s (rel (QExpr be s))
oneToMany_ DatabaseEntity be db (TableEntity rel)
rel rel (QExpr be s) -> PrimaryKey tbl (QExpr be s)
getKey tbl (QExpr be s)
tbl =
DatabaseEntity be db (TableEntity rel)
-> (rel (QExpr be s) -> QExpr be s Bool)
-> Q be db s (rel (QExpr be s))
forall be (db :: (* -> *) -> *) (table :: (* -> *) -> *) s.
(Database be db, Table table, BeamSqlBackend be) =>
DatabaseEntity be db (TableEntity table)
-> (table (QExpr be s) -> QExpr be s Bool)
-> Q be db s (table (QExpr be s))
join_ DatabaseEntity be db (TableEntity rel)
rel (\rel (QExpr be s)
rel' -> rel (QExpr be s) -> PrimaryKey tbl (QExpr be s)
getKey rel (QExpr be s)
rel' PrimaryKey tbl (QExpr be s)
-> PrimaryKey tbl (QExpr be s) -> QExpr be s Bool
forall (expr :: * -> *) a. SqlEq expr a => a -> a -> expr Bool
==. tbl (QExpr be s) -> PrimaryKey tbl (QExpr be s)
forall (t :: (* -> *) -> *) (f :: * -> *).
Table t =>
t f -> PrimaryKey t f
pk tbl (QExpr be s)
tbl)
oneToOne_ :: DatabaseEntity be db (TableEntity rel)
-> (rel (QExpr be s) -> PrimaryKey tbl (QExpr be s))
-> tbl (QExpr be s)
-> Q be db s (rel (QExpr be s))
oneToOne_ = DatabaseEntity be db (TableEntity rel)
-> (rel (QExpr be s) -> PrimaryKey tbl (QExpr be s))
-> tbl (QExpr be s)
-> Q be db s (rel (QExpr be s))
forall be (db :: (* -> *) -> *) (tbl :: (* -> *) -> *)
(rel :: (* -> *) -> *) s.
(Database be db, BeamSqlBackend be,
HasTableEquality be (PrimaryKey tbl), Table tbl, Table rel) =>
DatabaseEntity be db (TableEntity rel)
-> (rel (QExpr be s) -> PrimaryKey tbl (QExpr be s))
-> tbl (QExpr be s)
-> Q be db s (rel (QExpr be s))
oneToMany_
oneToManyOptional_, oneToMaybe_
:: ( BeamSqlBackend be, Database be db
, Table tbl, Table rel
, HasTableEqualityNullable be (PrimaryKey tbl) )
=> DatabaseEntity be db (TableEntity rel)
-> (rel (QExpr be s) -> PrimaryKey tbl (Nullable (QExpr be s)))
-> tbl (QExpr be s)
-> Q be db s (rel (Nullable (QExpr be s)))
oneToManyOptional_ :: DatabaseEntity be db (TableEntity rel)
-> (rel (QExpr be s) -> PrimaryKey tbl (Nullable (QExpr be s)))
-> tbl (QExpr be s)
-> Q be db s (rel (Nullable (QExpr be s)))
oneToManyOptional_ DatabaseEntity be db (TableEntity rel)
rel rel (QExpr be s) -> PrimaryKey tbl (Nullable (QExpr be s))
getKey tbl (QExpr be s)
tbl =
Q be db (QNested s) (rel (QExpr be (QNested s)))
-> (WithRewrittenThread (QNested s) s (rel (QExpr be (QNested s)))
-> QExpr be s Bool)
-> Q be
db
s
(Retag
Nullable
(WithRewrittenThread (QNested s) s (rel (QExpr be (QNested s)))))
forall s r be (db :: (* -> *) -> *).
(BeamSqlBackend be, Projectible be r,
ThreadRewritable (QNested s) r,
Retaggable (QExpr be s) (WithRewrittenThread (QNested s) s r)) =>
Q be db (QNested s) r
-> (WithRewrittenThread (QNested s) s r -> QExpr be s Bool)
-> Q be db s (Retag Nullable (WithRewrittenThread (QNested s) s r))
leftJoin_ (DatabaseEntity be db (TableEntity rel)
-> Q be db (QNested s) (rel (QExpr be (QNested s)))
forall be (db :: (* -> *) -> *) (table :: (* -> *) -> *) s.
(Database be db, BeamSqlBackend be) =>
DatabaseEntity be db (TableEntity table)
-> Q be db s (table (QExpr be s))
all_ DatabaseEntity be db (TableEntity rel)
rel) (\WithRewrittenThread (QNested s) s (rel (QExpr be (QNested s)))
rel' -> rel (QExpr be s) -> PrimaryKey tbl (Nullable (QExpr be s))
getKey rel (QExpr be s)
WithRewrittenThread (QNested s) s (rel (QExpr be (QNested s)))
rel' PrimaryKey tbl (Nullable (QExpr be s))
-> PrimaryKey tbl (Nullable (QExpr be s)) -> QExpr be s Bool
forall (expr :: * -> *) a. SqlEq expr a => a -> a -> expr Bool
==. PrimaryKey tbl (QExpr be s)
-> PrimaryKey tbl (Nullable (QExpr be s))
forall a b. SqlJustable a b => a -> b
just_ (tbl (QExpr be s) -> PrimaryKey tbl (QExpr be s)
forall (t :: (* -> *) -> *) (f :: * -> *).
Table t =>
t f -> PrimaryKey t f
pk tbl (QExpr be s)
tbl))
oneToMaybe_ :: DatabaseEntity be db (TableEntity rel)
-> (rel (QExpr be s) -> PrimaryKey tbl (Nullable (QExpr be s)))
-> tbl (QExpr be s)
-> Q be db s (rel (Nullable (QExpr be s)))
oneToMaybe_ = DatabaseEntity be db (TableEntity rel)
-> (rel (QExpr be s) -> PrimaryKey tbl (Nullable (QExpr be s)))
-> tbl (QExpr be s)
-> Q be db s (rel (Nullable (QExpr be s)))
forall be (db :: (* -> *) -> *) (tbl :: (* -> *) -> *)
(rel :: (* -> *) -> *) s.
(BeamSqlBackend be, Database be db, Table tbl, Table rel,
HasTableEqualityNullable be (PrimaryKey tbl)) =>
DatabaseEntity be db (TableEntity rel)
-> (rel (QExpr be s) -> PrimaryKey tbl (Nullable (QExpr be s)))
-> tbl (QExpr be s)
-> Q be db s (rel (Nullable (QExpr be s)))
oneToManyOptional_
type ManyToMany be db left right =
forall s.
( BeamSqlBackend be
, SqlEq (QExpr be s) (PrimaryKey left (QExpr be s))
, SqlEq (QExpr be s) (PrimaryKey right (QExpr be s)) ) =>
Q be db s (left (QExpr be s)) -> Q be db s (right (QExpr be s)) ->
Q be db s (left (QExpr be s), right (QExpr be s))
type ManyToManyThrough be db through left right =
forall s.
( BeamSqlBackend be
, SqlEq (QExpr be s) (PrimaryKey left (QExpr be s))
, SqlEq (QExpr be s) (PrimaryKey right (QExpr be s)) ) =>
Q be db s (left (QExpr be s)) -> Q be db s (right (QExpr be s)) ->
Q be db s ( through (QExpr be s), left (QExpr be s), right (QExpr be s) )
manyToMany_
:: ( Database be db
, Table joinThrough, Table left, Table right
, BeamSqlBackend be
, SqlEq (QExpr be s) (PrimaryKey left (QExpr be s))
, SqlEq (QExpr be s) (PrimaryKey right (QExpr be s)) )
=> DatabaseEntity be db (TableEntity joinThrough)
-> (joinThrough (QExpr be s) -> PrimaryKey left (QExpr be s))
-> (joinThrough (QExpr be s) -> PrimaryKey right (QExpr be s))
-> Q be db s (left (QExpr be s)) -> Q be db s (right (QExpr be s))
-> Q be db s (left (QExpr be s), right (QExpr be s))
manyToMany_ :: DatabaseEntity be db (TableEntity joinThrough)
-> (joinThrough (QExpr be s) -> PrimaryKey left (QExpr be s))
-> (joinThrough (QExpr be s) -> PrimaryKey right (QExpr be s))
-> Q be db s (left (QExpr be s))
-> Q be db s (right (QExpr be s))
-> Q be db s (left (QExpr be s), right (QExpr be s))
manyToMany_ DatabaseEntity be db (TableEntity joinThrough)
joinTbl joinThrough (QExpr be s) -> PrimaryKey left (QExpr be s)
leftKey joinThrough (QExpr be s) -> PrimaryKey right (QExpr be s)
rightKey Q be db s (left (QExpr be s))
left Q be db s (right (QExpr be s))
right = ((joinThrough (QExpr be s), left (QExpr be s), right (QExpr be s))
-> (left (QExpr be s), right (QExpr be s)))
-> Q be
db
s
(joinThrough (QExpr be s), left (QExpr be s), right (QExpr be s))
-> Q be db s (left (QExpr be s), right (QExpr be s))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\(joinThrough (QExpr be s)
_, left (QExpr be s)
l, right (QExpr be s)
r) -> (left (QExpr be s)
l, right (QExpr be s)
r)) (Q be
db
s
(joinThrough (QExpr be s), left (QExpr be s), right (QExpr be s))
-> Q be db s (left (QExpr be s), right (QExpr be s)))
-> Q be
db
s
(joinThrough (QExpr be s), left (QExpr be s), right (QExpr be s))
-> Q be db s (left (QExpr be s), right (QExpr be s))
forall a b. (a -> b) -> a -> b
$
DatabaseEntity be db (TableEntity joinThrough)
-> (joinThrough (QExpr be s) -> PrimaryKey left (QExpr be s))
-> (joinThrough (QExpr be s) -> PrimaryKey right (QExpr be s))
-> Q be db s (left (QExpr be s))
-> Q be db s (right (QExpr be s))
-> Q be
db
s
(joinThrough (QExpr be s), left (QExpr be s), right (QExpr be s))
forall be (db :: (* -> *) -> *) (joinThrough :: (* -> *) -> *)
(left :: (* -> *) -> *) (right :: (* -> *) -> *) s.
(Database be db, Table joinThrough, Table left, Table right,
BeamSqlBackend be,
SqlEq (QExpr be s) (PrimaryKey left (QExpr be s)),
SqlEq (QExpr be s) (PrimaryKey right (QExpr be s))) =>
DatabaseEntity be db (TableEntity joinThrough)
-> (joinThrough (QExpr be s) -> PrimaryKey left (QExpr be s))
-> (joinThrough (QExpr be s) -> PrimaryKey right (QExpr be s))
-> Q be db s (left (QExpr be s))
-> Q be db s (right (QExpr be s))
-> Q be
db
s
(joinThrough (QExpr be s), left (QExpr be s), right (QExpr be s))
manyToManyPassthrough_ DatabaseEntity be db (TableEntity joinThrough)
joinTbl joinThrough (QExpr be s) -> PrimaryKey left (QExpr be s)
leftKey joinThrough (QExpr be s) -> PrimaryKey right (QExpr be s)
rightKey Q be db s (left (QExpr be s))
left Q be db s (right (QExpr be s))
right
manyToManyPassthrough_
:: ( Database be db
, Table joinThrough, Table left, Table right
, BeamSqlBackend be
, SqlEq (QExpr be s) (PrimaryKey left (QExpr be s))
, SqlEq (QExpr be s) (PrimaryKey right (QExpr be s)) )
=> DatabaseEntity be db (TableEntity joinThrough)
-> (joinThrough (QExpr be s) -> PrimaryKey left (QExpr be s))
-> (joinThrough (QExpr be s) -> PrimaryKey right (QExpr be s))
-> Q be db s (left (QExpr be s))
-> Q be db s (right (QExpr be s))
-> Q be db s ( joinThrough (QExpr be s)
, left (QExpr be s)
, right (QExpr be s))
manyToManyPassthrough_ :: DatabaseEntity be db (TableEntity joinThrough)
-> (joinThrough (QExpr be s) -> PrimaryKey left (QExpr be s))
-> (joinThrough (QExpr be s) -> PrimaryKey right (QExpr be s))
-> Q be db s (left (QExpr be s))
-> Q be db s (right (QExpr be s))
-> Q be
db
s
(joinThrough (QExpr be s), left (QExpr be s), right (QExpr be s))
manyToManyPassthrough_ DatabaseEntity be db (TableEntity joinThrough)
joinTbl joinThrough (QExpr be s) -> PrimaryKey left (QExpr be s)
leftKey joinThrough (QExpr be s) -> PrimaryKey right (QExpr be s)
rightKey Q be db s (left (QExpr be s))
left Q be db s (right (QExpr be s))
right =
do left (QExpr be s)
left_ <- Q be db s (left (QExpr be s))
left
right (QExpr be s)
right_ <- Q be db s (right (QExpr be s))
right
joinThrough (QExpr be s)
joinTbl_ <- DatabaseEntity be db (TableEntity joinThrough)
-> (joinThrough (QExpr be s) -> QExpr be s Bool)
-> Q be db s (joinThrough (QExpr be s))
forall be (db :: (* -> *) -> *) (table :: (* -> *) -> *) s.
(Database be db, Table table, BeamSqlBackend be) =>
DatabaseEntity be db (TableEntity table)
-> (table (QExpr be s) -> QExpr be s Bool)
-> Q be db s (table (QExpr be s))
join_ DatabaseEntity be db (TableEntity joinThrough)
joinTbl (\joinThrough (QExpr be s)
joinTbl_ -> joinThrough (QExpr be s) -> PrimaryKey left (QExpr be s)
leftKey joinThrough (QExpr be s)
joinTbl_ PrimaryKey left (QExpr be s)
-> PrimaryKey left (QExpr be s) -> QExpr be s Bool
forall (expr :: * -> *) a. SqlEq expr a => a -> a -> expr Bool
==. left (QExpr be s) -> PrimaryKey left (QExpr be s)
forall (table :: (* -> *) -> *) (column :: * -> *).
Table table =>
table column -> PrimaryKey table column
primaryKey left (QExpr be s)
left_ QExpr be s Bool -> QExpr be s Bool -> QExpr be s Bool
forall be context s.
BeamSqlBackend be =>
QGenExpr context be s Bool
-> QGenExpr context be s Bool -> QGenExpr context be s Bool
&&.
joinThrough (QExpr be s) -> PrimaryKey right (QExpr be s)
rightKey joinThrough (QExpr be s)
joinTbl_ PrimaryKey right (QExpr be s)
-> PrimaryKey right (QExpr be s) -> QExpr be s Bool
forall (expr :: * -> *) a. SqlEq expr a => a -> a -> expr Bool
==. right (QExpr be s) -> PrimaryKey right (QExpr be s)
forall (table :: (* -> *) -> *) (column :: * -> *).
Table table =>
table column -> PrimaryKey table column
primaryKey right (QExpr be s)
right_)
(joinThrough (QExpr be s), left (QExpr be s), right (QExpr be s))
-> Q be
db
s
(joinThrough (QExpr be s), left (QExpr be s), right (QExpr be s))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (joinThrough (QExpr be s)
joinTbl_, left (QExpr be s)
left_, right (QExpr be s)
right_)