module Opaleye.Join where
import qualified Opaleye.Internal.Unpackspec as U
import qualified Opaleye.Internal.Join as J
import qualified Opaleye.Internal.Tag as T
import qualified Opaleye.Internal.PrimQuery as PQ
import Opaleye.QueryArr (Query)
import qualified Opaleye.Internal.QueryArr as Q
import Opaleye.Internal.Column (Column(Column))
import qualified Opaleye.PGTypes as T
import qualified Data.Profunctor.Product.Default as D
leftJoin :: (D.Default U.Unpackspec columnsA columnsA,
D.Default U.Unpackspec columnsB columnsB,
D.Default J.NullMaker columnsB nullableColumnsB) =>
Query columnsA -> Query columnsB
-> ((columnsA, columnsB) -> Column T.PGBool)
-> Query (columnsA, nullableColumnsB)
leftJoin = leftJoinExplicit D.def D.def D.def
leftJoinExplicit :: U.Unpackspec columnsA columnsA
-> U.Unpackspec columnsB columnsB
-> J.NullMaker columnsB nullableColumnsB
-> Query columnsA -> Query columnsB
-> ((columnsA, columnsB) -> Column T.PGBool)
-> Query (columnsA, nullableColumnsB)
leftJoinExplicit _ _ nullmaker qA qB cond = Q.simpleQueryArr q where
q ((), startTag) = ((columnsA, nullableColumnsB), primQueryR, T.next endTag)
where (columnsA, primQueryA, midTag) = Q.runSimpleQueryArr qA ((), startTag)
(columnsB, primQueryB, endTag) = Q.runSimpleQueryArr qB ((), midTag)
nullableColumnsB = J.toNullable nullmaker columnsB
Column cond' = cond (columnsA, columnsB)
primQueryR = PQ.Join PQ.LeftJoin cond' primQueryA primQueryB