1{-# language DisambiguateRecordFields #-}
2{-# language FlexibleContexts #-}
3{-# language NamedFieldPuns #-}
4{-# language TypeFamilies #-}
5{-# language ViewPatterns #-}
7module Rel8.Table.Opaleye
19import Prelude hiding ( undefined )
22import qualified Opaleye.Internal.Aggregate as Opaleye
23import qualified Opaleye.Internal.Binary as Opaleye
24import qualified Opaleye.Internal.Distinct as Opaleye
25import qualified Opaleye.Internal.HaskellDB.PrimQuery as Opaleye
26import qualified Opaleye.Internal.PackMap as Opaleye
27import qualified Opaleye.Internal.Unpackspec as Opaleye
28import qualified Opaleye.Internal.Values as Opaleye
29import qualified Opaleye.Internal.Table as Opaleye
32import Data.Profunctor ( dimap, lmap )
35import Rel8.Aggregate ( Aggregate( Aggregate ) )
36import Rel8.Expr ( Expr, Col(..) )
37import Rel8.Expr.Opaleye
39 , fromPrimExpr, toPrimExpr
41 , fromColumn, toColumn
43import Rel8.Kind.Necessity ( SNecessity( SRequired, SOptional ) )
44import Rel8.Schema.HTable ( htabulateA, hfield, htraverse, hspecs )
45import Rel8.Schema.Insert ( Insert, Inserts, Col(..) )
46import Rel8.Schema.Name ( Name, Selects, Col(..) )
47import Rel8.Schema.Spec ( SSpec(..) )
48import Rel8.Schema.Table ( TableSchema(..) )
49import Rel8.Table ( Table, fromColumns, toColumns )
50import Rel8.Table.Undefined ( undefined )
53import Data.Functor.Apply ( WrappedApplicative(..) )
56aggregator :: Opaleye.Aggregator (Aggregate exprs) exprs
57aggregator = Opaleye.Aggregator $ Opaleye.PackMap $
58 \f (Aggregate (Opaleye.Aggregator (Opaleye.PackMap inner))) -> inner f ()
61binaryspec :: Table Expr a => Opaleye.Binaryspec a a
62binaryspec = Opaleye.Binaryspec $ Opaleye.PackMap $ \f (as, bs) ->
63 fmap fromColumns $ unwrapApplicative $ htabulateA $ \field ->
65 case (hfield (toColumns as) field, hfield (toColumns bs) field) of
66 (DB a, DB b) -> DB . fromPrimExpr <$> f (toPrimExpr a, toPrimExpr b)
69distinctspec :: Table Expr a => Opaleye.Distinctspec a a
71 Opaleye.Distinctspec $ Opaleye.Aggregator $ Opaleye.PackMap $ \f ->
76 WrapApplicative $ DB . fromPrimExpr <$> f (Nothing, toPrimExpr a)) .
80table ::(Selects names exprs, Inserts exprs inserts)
81 => TableSchema names -> Opaleye.Table inserts exprs
82table (TableSchema name schema columns) =
83 Opaleye.Table qualified (tableFields columns)
85 qualified = case schema of
87 Just qualifier -> qualifier <> "." <> name
90tableFields :: (Selects names exprs, Inserts exprs inserts)
91 => names -> Opaleye.TableFields inserts exprs
92tableFields (toColumns -> names) = dimap toColumns fromColumns $
93 unwrapApplicative $ htabulateA $ \field -> WrapApplicative $
94 case hfield hspecs field of
95 specs -> case hfield names field of
96 name -> lmap (`hfield` field) (go specs name)
98 go :: SSpec spec -> Col Name spec -> Opaleye.TableFields (Col Insert spec) (Col Expr spec)
99 go SSpec {necessity, info} (NameCol name) = case necessity of
101 lmap (\(RequiredInsert a) -> toColumn $ toPrimExpr a) $
102 DB . scastExpr info . fromPrimExpr . fromColumn <$>
103 Opaleye.requiredTableField name
105 lmap (\(OptionalInsert ma) -> toColumn . toPrimExpr <$> ma) $
106 DB . scastExpr info . fromPrimExpr . fromColumn <$>
107 Opaleye.optionalTableField name
110unpackspec :: Table Expr a => Opaleye.Unpackspec a a
111unpackspec = Opaleye.Unpackspec $ Opaleye.PackMap $ \f ->
114 htraverse (\(DB a) -> WrapApplicative $ DB <$> traversePrimExpr f a) .
116{-# INLINABLE unpackspec #-}
119valuesspec :: Table Expr a => Opaleye.ValuesspecSafe a a
120valuesspec = Opaleye.ValuesspecSafe (toPackMap undefined) unpackspec
123toPackMap :: Table Expr a
124 => a -> Opaleye.PackMap Opaleye.PrimExpr Opaleye.PrimExpr () a
125toPackMap as = Opaleye.PackMap $ \f () ->
128 htraverse (\(DB a) -> WrapApplicative $ DB <$> traversePrimExpr f a) $
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list