@@ -1,695 +0,0 @@
1−{-# language DataKinds #-}
2−{-# language FlexibleContexts #-}
4−{-# language LambdaCase #-}
5−{-# language MultiParamTypeClasses #-}
6−{-# language RankNTypes #-}
7−{-# language StandaloneKindSignatures #-}
8−{-# language TypeFamilies #-}
9−{-# language UndecidableInstances #-}
11−module Rel8.Schema.Field
13− , HEither, HList, HMaybe, HNonEmpty, HThese
15− , AHEither(..), AHList(..), AHMaybe(..), AHNonEmpty(..), AHThese(..)
20−import Control.Applicative ( liftA2 )
21−import Data.Bifunctor ( Bifunctor, bimap )
22−import Data.Kind ( Type )
23−import Data.List.NonEmpty ( NonEmpty )
27−import Rel8.Aggregate ( Aggregate, Col(..) )
28−import Rel8.Expr ( Expr, Col(..) )
29−import Rel8.Kind.Context ( SContext(..), Reifiable( contextSing ) )
30−import Rel8.Kind.Necessity
31− ( Necessity( Required, Optional )
32− , SNecessity( SRequired, SOptional )
33− , KnownNecessity, necessitySing
35−import Rel8.Schema.HTable.Either ( HEitherTable )
36−import Rel8.Schema.HTable.List ( HListTable )
37−import Rel8.Schema.HTable.Maybe ( HMaybeTable )
38−import Rel8.Schema.HTable.NonEmpty ( HNonEmptyTable )
39−import Rel8.Schema.HTable.These ( HTheseTable )
40−import Rel8.Schema.HTable.Identity ( HIdentity( HIdentity ) )
41−import Rel8.Schema.Insert ( Insert, Col(..) )
42−import qualified Rel8.Schema.Kind as K
43−import Rel8.Schema.Name ( Name(..), Col(..) )
44−import Rel8.Schema.Null ( Sql )
45−import Rel8.Schema.Reify ( Reify, Col(..), hreify, hunreify )
46−import Rel8.Schema.Result ( Col( Result ), Result )
47−import Rel8.Schema.Spec ( Spec( Spec ) )
49− ( Table, Columns, Congruent, Context, fromColumns, toColumns
50− , Unreify, reify, unreify
52−import Rel8.Table.Either ( EitherTable )
53−import Rel8.Table.List ( ListTable( ListTable ) )
54−import Rel8.Table.Maybe ( MaybeTable )
55−import Rel8.Table.NonEmpty ( NonEmptyTable( NonEmptyTable ) )
56−import Rel8.Table.Recontextualize ( Recontextualize )
57−import Rel8.Table.These ( TheseTable )
58−import Rel8.Table.Unreify ( Unreifiable )
59−import Rel8.Type ( DBType )
62−import Data.These ( These )
65−type Field :: K.Context -> Necessity -> Type -> Type
66−type 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
76−type HEither :: K.Context -> Type -> Type -> Type
77−type 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
86−type HList :: K.Context -> Type -> Type
87−type 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
96−type HMaybe :: K.Context -> Type -> Type
97−type 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
106−type HNonEmpty :: K.Context -> Type -> Type
107−type 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
116−type HThese :: K.Context -> Type -> Type -> Type
117−type 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
126−type AField :: K.Context -> Necessity -> Type -> Type
127−newtype AField context necessity a = AField (Field context necessity a)
130−instance (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)
154−type AHEither :: K.Context -> Type -> Type -> Type
155−newtype AHEither context a b = AHEither (HEither context a b)
158−instance Reifiable context => Bifunctor (AHEither context) where
159− bimap = sbimapEither contextSing
162−instance Reifiable context => Functor (AHEither context a) where
166−instance (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')
191−type AHList :: K.Context -> Type -> Type
192−newtype 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 .
212− (\(AHList a) -> a) .
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'
225− (AHList context' a')
228−type AHMaybe :: K.Context -> Type -> Type
229−newtype AHMaybe context a = AHMaybe (HMaybe context a)
232−instance Reifiable context => Functor (AHMaybe context) where
233− fmap = smapMaybe contextSing
236−instance (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')
260−type AHNonEmpty :: K.Context -> Type -> Type
261−newtype 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')
297−type AHThese :: K.Context -> Type -> Type -> Type
298−newtype AHThese context a b = AHThese (HThese context a b)
301−instance Reifiable context => Bifunctor (AHThese context) where
302− bimap = sbimapThese contextSing
305−instance Reifiable context => Functor (AHThese context a) where
309−instance (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)
381−sfromColumnsEither :: (Table (Reify context) a, Table (Reify context) b)
383− -> HEitherTable (Columns a) (Columns b) (Col (Reify context))
384− -> AHEither context a b
385−sfromColumnsEither = \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 .
418−stoColumnsEither :: (Table (Reify context) a, Table (Reify context) b)
420− -> AHEither context a b
421− -> HEitherTable (Columns a) (Columns b) (Col (Reify context))
422−stoColumnsEither = \case
426− bimap (hunreify . toColumns) (hunreify . toColumns) .
427− (\(AHEither a) -> a)
431− bimap (hunreify . toColumns) (hunreify . toColumns) .
432− (\(AHEither a) -> a)
436− bimap (hunreify . toColumns) (hunreify . toColumns) .
437− (\(AHEither a) -> a)
441− bimap (hunreify . toColumns) (hunreify . toColumns) .
442− (\(AHEither a) -> a)
446− bimap (hunreify . toColumns) (hunreify . toColumns) .
447− (\(AHEither a) -> a)
450− stoColumnsEither context .
451− sbimapEither context (hunreify . toColumns) (hunreify . toColumns) .
452− (\(AHEither a) -> a)
455−smapList :: 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)
470−sfromColumnsList :: Table (Reify context) a
472− -> HListTable (Columns a) (Col (Reify context))
474−sfromColumnsList = \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 .
487−stoColumnsList :: Table (Reify context) a
490− -> HListTable (Columns a) (Col (Reify context))
491−stoColumnsList = \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 .
508− -> AHMaybe context a
509− -> AHMaybe context b
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)
519−sfromColumnsMaybe :: Table (Reify context) a
521− -> HMaybeTable (Columns a) (Col (Reify context))
522− -> AHMaybe context a
523−sfromColumnsMaybe = \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 .
536−stoColumnsMaybe :: Table (Reify context) a
538− -> AHMaybe context a
539− -> HMaybeTable (Columns a) (Col (Reify context))
540−stoColumnsMaybe = \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) .
558−smapNonEmpty :: 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)
573−sfromColumnsNonEmpty :: Table (Reify context) a
575− -> HNonEmptyTable (Columns a) (Col (Reify context))
576− -> AHNonEmpty context a
577−sfromColumnsNonEmpty = \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 .
591−stoColumnsNonEmpty :: Table (Reify context) a
593− -> AHNonEmpty context a
594− -> HNonEmptyTable (Columns a) (Col (Reify context))
595−stoColumnsNonEmpty = \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)
624−sfromColumnsThese :: (Table (Reify context) a, Table (Reify context) b)
626− -> HTheseTable (Columns a) (Columns b) (Col (Reify context))
627− -> AHThese context a b
628−sfromColumnsThese = \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 .
661−stoColumnsThese :: (Table (Reify context) a, Table (Reify context) b)
663− -> AHThese context a b
664− -> HTheseTable (Columns a) (Columns b) (Col (Reify context))
665−stoColumnsThese = \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) .