1{-# language DataKinds #-}
2{-# language FlexibleContexts #-}
4{-# language LambdaCase #-}
5{-# language MultiParamTypeClasses #-}
6{-# language StandaloneKindSignatures #-}
7{-# language TypeFamilies #-}
8{-# language UndecidableInstances #-}
10module Rel8.Schema.Field
12 , HEither, HList, HMaybe, HNonEmpty, HThese
14 , AHEither(..), AHList(..), AHMaybe(..), AHNonEmpty(..), AHThese(..)
19import Control.Applicative ( liftA2 )
20import Data.Bifunctor ( Bifunctor, bimap )
21import Data.Kind ( Type )
22import Data.List.NonEmpty ( NonEmpty )
26import Rel8.Aggregate ( Aggregate, Col(..) )
27import Rel8.Expr ( Expr, Col(..) )
28import Rel8.Kind.Context ( SContext(..), Reifiable( contextSing ) )
29import Rel8.Kind.Necessity
30 ( Necessity( Required, Optional )
31 , SNecessity( SRequired, SOptional )
32 , KnownNecessity, necessitySing
34import Rel8.Schema.Context ( Col(..) )
35import Rel8.Schema.HTable.Either ( HEitherTable )
36import Rel8.Schema.HTable.List ( HListTable )
37import Rel8.Schema.HTable.Maybe ( HMaybeTable )
38import Rel8.Schema.HTable.NonEmpty ( HNonEmptyTable )
39import Rel8.Schema.HTable.These ( HTheseTable )
40import Rel8.Schema.HTable.Identity ( HIdentity( HIdentity ) )
41import Rel8.Schema.Insert ( Insert, Col(..) )
42import qualified Rel8.Schema.Kind as K
43import Rel8.Schema.Name ( Name(..), Col(..) )
44import Rel8.Schema.Null ( Sql )
45import Rel8.Schema.Reify ( Reify, Col(..), hreify, hunreify )
46import Rel8.Schema.Result ( Result )
47import Rel8.Schema.Spec ( Spec( Spec ) )
49 ( Table, Columns, Congruent, Context, fromColumns, toColumns
50 , Unreify, reify, unreify
52import Rel8.Table.Either ( EitherTable )
53import Rel8.Table.List ( ListTable( ListTable ) )
54import Rel8.Table.Maybe ( MaybeTable )
55import Rel8.Table.NonEmpty ( NonEmptyTable( NonEmptyTable ) )
56import Rel8.Table.Recontextualize ( Recontextualize )
57import Rel8.Table.These ( TheseTable )
58import Rel8.Table.Unreify ( Unreifiable )
59import Rel8.Type ( DBType )
62import Data.These ( These )
65type Field :: K.Context -> Necessity -> Type -> Type
66type family Field context necessity a where
67 Field (Reify context) necessity a = AField context necessity a
68 Field Aggregate _necessity a = Aggregate (Expr a)
69 Field Expr _necessity a = Expr a
70 Field Insert 'Required a = Expr a
71 Field Insert 'Optional a = Maybe (Expr a)
72 Field Name _necessity a = Name a
73 Field Result _necessity a = a
76type HEither :: K.Context -> Type -> Type -> Type
77type family HEither context where
78 HEither (Reify context) = AHEither context
79 HEither Aggregate = EitherTable
80 HEither Expr = EitherTable
81 HEither Insert = EitherTable
82 HEither Name = EitherTable
83 HEither Result = Either
86type HList :: K.Context -> Type -> Type
87type family HList context where
88 HList (Reify context) = AHList context
89 HList Aggregate = ListTable
90 HList Expr = ListTable
91 HList Insert = ListTable
92 HList Name = ListTable
96type HMaybe :: K.Context -> Type -> Type
97type family HMaybe context where
98 HMaybe (Reify context) = AHMaybe context
99 HMaybe Aggregate = MaybeTable
100 HMaybe Expr = MaybeTable
101 HMaybe Insert = MaybeTable
102 HMaybe Name = MaybeTable
103 HMaybe Result = Maybe
106type HNonEmpty :: K.Context -> Type -> Type
107type family HNonEmpty context where
108 HNonEmpty (Reify context) = AHNonEmpty context
109 HNonEmpty Aggregate = NonEmptyTable
110 HNonEmpty Expr = NonEmptyTable
111 HNonEmpty Insert = NonEmptyTable
112 HNonEmpty Name = NonEmptyTable
113 HNonEmpty Result = NonEmpty
116type HThese :: K.Context -> Type -> Type -> Type
117type family HThese context where
118 HThese (Reify context) = AHThese context
119 HThese Aggregate = TheseTable
120 HThese Expr = TheseTable
121 HThese Insert = TheseTable
122 HThese Name = TheseTable
123 HThese Result = These
126type AField :: K.Context -> Necessity -> Type -> Type
127newtype AField context necessity a = AField (Field context necessity a)
130instance (Reifiable context, KnownNecessity necessity, Sql DBType a) =>
131 Table (Reify context) (AField context necessity a)
133 type Context (AField context necessity a) = Reify context
134 type Columns (AField context necessity a) = HIdentity ('Spec '[""] necessity a)
135 type Unreify (AField context necessity a) = Field context necessity a
137 fromColumns (HIdentity (Reify a)) = sfromColumn contextSing a
138 toColumns = HIdentity . Reify . stoColumn contextSing necessitySing
140 unreify _ (AField a) = a
144 ( Reifiable context, Reifiable context'
145 , KnownNecessity necessity, Sql DBType a
150 (AField context necessity a)
151 (AField context' necessity a)
154type AHEither :: K.Context -> Type -> Type -> Type
155newtype AHEither context a b = AHEither (HEither context a b)
158instance Reifiable context => Bifunctor (AHEither context) where
159 bimap = sbimapEither contextSing
162instance Reifiable context => Functor (AHEither context a) where
166instance (Reifiable context, Table (Reify context) a, Table (Reify context) b)
167 => Table (Reify context) (AHEither context a b)
169 type Context (AHEither context a b) = Reify context
170 type Columns (AHEither context a b) = HEitherTable (Columns a) (Columns b)
171 type Unreify (AHEither context a b) = HEither context (Unreify a) (Unreify b)
173 fromColumns = sfromColumnsEither contextSing
174 toColumns = stoColumnsEither contextSing
175 reify proof = liftA2 bimap reify reify proof . AHEither
176 unreify proof = (\(AHEither a) -> a) . liftA2 bimap unreify unreify proof
180 ( Reifiable context, Reifiable context'
181 , Recontextualize (Reify context) (Reify context') a a'
182 , Recontextualize (Reify context) (Reify context') b b'
187 (AHEither context a b)
188 (AHEither context' a' b')
191type AHList :: K.Context -> Type -> Type
192newtype AHList context a = AHList (HList context a)
197 , Table (Reify context) a
198 , Unreifiable (Reify context) a
200 => Table (Reify context) (AHList context a)
202 type Context (AHList context a) = Reify context
203 type Columns (AHList context a) = HListTable (Columns a)
204 type Unreify (AHList context a) = HList context (Unreify a)
206 fromColumns = sfromColumnsList contextSing
207 toColumns = stoColumnsList contextSing
209 smapList contextSing (reify proof) hreify .
213 smapList contextSing (unreify proof) hunreify
217 ( Reifiable context, Reifiable context'
218 , Unreifiable (Reify context) a, Unreifiable (Reify context') a'
219 , Recontextualize (Reify context) (Reify context') a a'
228type AHMaybe :: K.Context -> Type -> Type
229newtype AHMaybe context a = AHMaybe (HMaybe context a)
232instance Reifiable context => Functor (AHMaybe context) where
233 fmap = smapMaybe contextSing
236instance (Reifiable context, Table (Reify context) a) =>
237 Table (Reify context) (AHMaybe context a)
239 type Context (AHMaybe context a) = Reify context
240 type Columns (AHMaybe context a) = HMaybeTable (Columns a)
241 type Unreify (AHMaybe context a) = HMaybe context (Unreify a)
243 fromColumns = sfromColumnsMaybe contextSing
244 toColumns = stoColumnsMaybe contextSing
245 reify proof = fmap fmap reify proof . AHMaybe
246 unreify proof = (\(AHMaybe a) -> a) . fmap fmap unreify proof
250 ( Reifiable context, Reifiable context'
251 , Recontextualize (Reify context) (Reify context') a a'
257 (AHMaybe context' a')
260type AHNonEmpty :: K.Context -> Type -> Type
261newtype AHNonEmpty context a = AHNonEmpty (HNonEmpty context a)
266 , Table (Reify context) a
267 , Unreifiable (Reify context) a
269 => Table (Reify context) (AHNonEmpty context a)
271 type Context (AHNonEmpty context a) = Reify context
272 type Columns (AHNonEmpty context a) = HNonEmptyTable (Columns a)
273 type Unreify (AHNonEmpty context a) = HNonEmpty context (Unreify a)
275 fromColumns = sfromColumnsNonEmpty contextSing
276 toColumns = stoColumnsNonEmpty contextSing
278 smapNonEmpty contextSing (reify proof) hreify .
281 (\(AHNonEmpty a) -> a) .
282 smapNonEmpty contextSing (unreify proof) hunreify
286 ( Reifiable context, Reifiable context'
287 , Unreifiable (Reify context) a, Unreifiable (Reify context') a'
288 , Recontextualize (Reify context) (Reify context') a a'
293 (AHNonEmpty context a)
294 (AHNonEmpty context' a')
297type AHThese :: K.Context -> Type -> Type -> Type
298newtype AHThese context a b = AHThese (HThese context a b)
301instance Reifiable context => Bifunctor (AHThese context) where
302 bimap = sbimapThese contextSing
305instance Reifiable context => Functor (AHThese context a) where
309instance (Reifiable context, Table (Reify context) a, Table (Reify context) b)
310 => Table (Reify context) (AHThese context a b)
312 type Context (AHThese context a b) = Reify context
313 type Columns (AHThese context a b) = HTheseTable (Columns a) (Columns b)
314 type Unreify (AHThese context a b) = HThese context (Unreify a) (Unreify b)
316 fromColumns = sfromColumnsThese contextSing
317 toColumns = stoColumnsThese contextSing
318 reify proof = liftA2 bimap reify reify proof . AHThese
319 unreify proof = (\(AHThese a) -> a) . liftA2 bimap unreify unreify proof
323 ( Reifiable context, Reifiable context'
324 , Recontextualize (Reify context) (Reify context') a a'
325 , Recontextualize (Reify context) (Reify context') b b'
330 (AHThese context a b)
331 (AHThese context' a' b')
336 -> Col context ('Spec labels necessity a)
337 -> AField context necessity a
339 SAggregate -> \(Aggregation a) -> AField a
340 SExpr -> \(DB a) -> AField a
341 SResult -> \(Result a) -> AField a
343 RequiredInsert a -> AField a
344 OptionalInsert a -> AField a
345 SName -> \(NameCol a) -> AField (Name a)
346 SReify context -> \(Reify a) -> AField (sfromColumn context a)
351 -> SNecessity necessity
352 -> AField context necessity a
353 -> Col context ('Spec labels necessity a)
355 SAggregate -> \_ (AField a) -> Aggregation a
356 SExpr -> \_ (AField a) -> DB a
357 SResult -> \_ (AField a) -> Result a
359 SRequired -> \(AField a) -> RequiredInsert a
360 SOptional -> \(AField a) -> OptionalInsert a
361 SName -> \_ (AField (Name a)) -> NameCol a
363 \necessity (AField a) -> Reify (stoColumn context necessity a)
370 -> AHEither context a b
371 -> AHEither context c d
373 SAggregate -> \f g (AHEither a) -> AHEither (bimap f g a)
374 SExpr -> \f g (AHEither a) -> AHEither (bimap f g a)
375 SResult -> \f g (AHEither a) -> AHEither (bimap f g a)
376 SInsert -> \f g (AHEither a) -> AHEither (bimap f g a)
377 SName -> \f g (AHEither a) -> AHEither (bimap f g a)
378 SReify context -> \f g (AHEither a) -> AHEither (sbimapEither context f g a)
381sfromColumnsEither :: (Table (Reify context) a, Table (Reify context) b)
383 -> HEitherTable (Columns a) (Columns b) (Col (Reify context))
384 -> AHEither context a b
385sfromColumnsEither = \case
388 bimap (fromColumns . hreify) (fromColumns . hreify) .
393 bimap (fromColumns . hreify) (fromColumns . hreify) .
398 bimap (fromColumns . hreify) (fromColumns . hreify) .
403 bimap (fromColumns . hreify) (fromColumns . hreify) .
408 bimap (fromColumns . hreify) (fromColumns . hreify) .
413 sbimapEither context (fromColumns . hreify) (fromColumns . hreify) .
414 sfromColumnsEither context .
418stoColumnsEither :: (Table (Reify context) a, Table (Reify context) b)
420 -> AHEither context a b
421 -> HEitherTable (Columns a) (Columns b) (Col (Reify context))
422stoColumnsEither = \case
426 bimap (hunreify . toColumns) (hunreify . toColumns) .
431 bimap (hunreify . toColumns) (hunreify . toColumns) .
436 bimap (hunreify . toColumns) (hunreify . toColumns) .
441 bimap (hunreify . toColumns) (hunreify . toColumns) .
446 bimap (hunreify . toColumns) (hunreify . toColumns) .
450 stoColumnsEither context .
451 sbimapEither context (hunreify . toColumns) (hunreify . toColumns) .
455smapList :: Congruent a b
458 -> (HListTable (Columns a) (Col (Context a)) -> HListTable (Columns b) (Col (Context b)))
462 SAggregate -> \_ f (AHList (ListTable a)) -> AHList (ListTable (f a))
463 SExpr -> \_ f (AHList (ListTable a)) -> AHList (ListTable (f a))
464 SResult -> \f _ (AHList as) -> AHList (fmap f as)
465 SInsert -> \_ f (AHList (ListTable a)) -> AHList (ListTable (f a))
466 SName -> \_ f (AHList (ListTable a)) -> AHList (ListTable (f a))
467 SReify context -> \f g (AHList as) -> AHList (smapList context f g as)
470sfromColumnsList :: Table (Reify context) a
472 -> HListTable (Columns a) (Col (Reify context))
474sfromColumnsList = \case
475 SAggregate -> AHList . ListTable
476 SExpr -> AHList . ListTable
477 SResult -> AHList . fmap (fromColumns . hreify) . fromColumns . hunreify
478 SInsert -> AHList . ListTable
479 SName -> AHList . ListTable
482 smapList context (fromColumns . hreify) hreify .
483 sfromColumnsList context .
487stoColumnsList :: Table (Reify context) a
490 -> HListTable (Columns a) (Col (Reify context))
491stoColumnsList = \case
492 SAggregate -> \(AHList (ListTable a)) -> a
493 SExpr -> \(AHList (ListTable a)) -> a
495 hreify . toColumns . fmap (hunreify . toColumns) . (\(AHList a) -> a)
496 SInsert -> \(AHList (ListTable a)) -> a
497 SName -> \(AHList (ListTable a)) -> a
500 stoColumnsList context .
501 smapList context (hunreify . toColumns) hunreify .
511 SAggregate -> \f (AHMaybe a) -> AHMaybe (fmap f a)
512 SExpr -> \f (AHMaybe a) -> AHMaybe (fmap f a)
513 SResult -> \f (AHMaybe a) -> AHMaybe (fmap f a)
514 SInsert -> \f (AHMaybe a) -> AHMaybe (fmap f a)
515 SName -> \f (AHMaybe a) -> AHMaybe (fmap f a)
516 SReify context -> \f (AHMaybe a) -> AHMaybe (smapMaybe context f a)
519sfromColumnsMaybe :: Table (Reify context) a
521 -> HMaybeTable (Columns a) (Col (Reify context))
523sfromColumnsMaybe = \case
524 SAggregate -> AHMaybe . fmap (fromColumns . hreify) . fromColumns . hunreify
525 SExpr -> AHMaybe . fmap (fromColumns . hreify) . fromColumns . hunreify
526 SResult -> AHMaybe . fmap (fromColumns . hreify) . fromColumns . hunreify
527 SInsert -> AHMaybe . fmap (fromColumns . hreify) . fromColumns . hunreify
528 SName -> AHMaybe . fmap (fromColumns . hreify) . fromColumns . hunreify
531 smapMaybe context (fromColumns . hreify) .
532 sfromColumnsMaybe context .
536stoColumnsMaybe :: Table (Reify context) a
539 -> HMaybeTable (Columns a) (Col (Reify context))
540stoColumnsMaybe = \case
542 hreify . toColumns . fmap (hunreify . toColumns) . (\(AHMaybe a) -> a)
544 hreify . toColumns . fmap (hunreify . toColumns) . (\(AHMaybe a) -> a)
546 hreify . toColumns . fmap (hunreify . toColumns) . (\(AHMaybe a) -> a)
548 hreify . toColumns . fmap (hunreify . toColumns) . (\(AHMaybe a) -> a)
550 hreify . toColumns . fmap (hunreify . toColumns) . (\(AHMaybe a) -> a)
553 stoColumnsMaybe context .
554 smapMaybe context (hunreify . toColumns) .
558smapNonEmpty :: Congruent a b
561 -> (HNonEmptyTable (Columns a) (Col (Context a)) -> HNonEmptyTable (Columns b) (Col (Context b)))
562 -> AHNonEmpty context a
563 -> AHNonEmpty context b
565 SAggregate -> \_ f (AHNonEmpty (NonEmptyTable a)) -> AHNonEmpty (NonEmptyTable (f a))
566 SExpr -> \_ f (AHNonEmpty (NonEmptyTable a)) -> AHNonEmpty (NonEmptyTable (f a))
567 SResult -> \f _ (AHNonEmpty as) -> AHNonEmpty (fmap f as)
568 SInsert -> \_ f (AHNonEmpty (NonEmptyTable a)) -> AHNonEmpty (NonEmptyTable (f a))
569 SName -> \_ f (AHNonEmpty (NonEmptyTable a)) -> AHNonEmpty (NonEmptyTable (f a))
570 SReify context -> \f g (AHNonEmpty as) -> AHNonEmpty (smapNonEmpty context f g as)
573sfromColumnsNonEmpty :: Table (Reify context) a
575 -> HNonEmptyTable (Columns a) (Col (Reify context))
576 -> AHNonEmpty context a
577sfromColumnsNonEmpty = \case
578 SAggregate -> AHNonEmpty . NonEmptyTable
579 SExpr -> AHNonEmpty . NonEmptyTable
581 AHNonEmpty . fmap (fromColumns . hreify) . fromColumns . hunreify
582 SInsert -> AHNonEmpty . NonEmptyTable
583 SName -> AHNonEmpty . NonEmptyTable
586 smapNonEmpty context (fromColumns . hreify) hreify .
587 sfromColumnsNonEmpty context .
591stoColumnsNonEmpty :: Table (Reify context) a
593 -> AHNonEmpty context a
594 -> HNonEmptyTable (Columns a) (Col (Reify context))
595stoColumnsNonEmpty = \case
596 SAggregate -> \(AHNonEmpty (NonEmptyTable a)) -> a
597 SExpr -> \(AHNonEmpty (NonEmptyTable a)) -> a
599 hreify . toColumns . fmap (hunreify . toColumns) . (\(AHNonEmpty a) -> a)
600 SInsert -> \(AHNonEmpty (NonEmptyTable a)) -> a
601 SName -> \(AHNonEmpty (NonEmptyTable a)) -> a
604 stoColumnsNonEmpty context .
605 smapNonEmpty context (hunreify . toColumns) hunreify .
606 (\(AHNonEmpty a) -> a)
613 -> AHThese context a b
614 -> AHThese context c d
616 SAggregate -> \f g (AHThese a) -> AHThese (bimap f g a)
617 SExpr -> \f g (AHThese a) -> AHThese (bimap f g a)
618 SResult -> \f g (AHThese a) -> AHThese (bimap f g a)
619 SInsert -> \f g (AHThese a) -> AHThese (bimap f g a)
620 SName -> \f g (AHThese a) -> AHThese (bimap f g a)
621 SReify context -> \f g (AHThese a) -> AHThese (sbimapThese context f g a)
624sfromColumnsThese :: (Table (Reify context) a, Table (Reify context) b)
626 -> HTheseTable (Columns a) (Columns b) (Col (Reify context))
627 -> AHThese context a b
628sfromColumnsThese = \case
631 bimap (fromColumns . hreify) (fromColumns . hreify) .
636 bimap (fromColumns . hreify) (fromColumns . hreify) .
641 bimap (fromColumns . hreify) (fromColumns . hreify) .
646 bimap (fromColumns . hreify) (fromColumns . hreify) .
651 bimap (fromColumns . hreify) (fromColumns . hreify) .
656 sbimapThese context (fromColumns . hreify) (fromColumns . hreify) .
657 sfromColumnsThese context .
661stoColumnsThese :: (Table (Reify context) a, Table (Reify context) b)
663 -> AHThese context a b
664 -> HTheseTable (Columns a) (Columns b) (Col (Reify context))
665stoColumnsThese = \case
669 bimap (hunreify . toColumns) (hunreify . toColumns) .
674 bimap (hunreify . toColumns) (hunreify . toColumns) .
679 bimap (hunreify . toColumns) (hunreify . toColumns) .
684 bimap (hunreify . toColumns) (hunreify . toColumns) .
689 bimap (hunreify . toColumns) (hunreify . toColumns) .
693 stoColumnsThese context .
694 sbimapThese context (hunreify . toColumns) (hunreify . toColumns) .
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