on f3daaff help sign in
rel8/src/Rel8/Type/Array1D.hs
136 lines 3.8 kB history raw
1{-# language DataKinds #-}
2{-# language DeriveTraversable #-}
3{-# language DerivingStrategies #-}
4{-# language GeneralizedNewtypeDeriving #-}
5{-# language NamedFieldPuns #-}
6{-# language StandaloneKindSignatures #-}
7{-# language TypeFamilies #-}
8{-# language UndecidableInstances #-}
10{-# options_ghc -fno-warn-redundant-constraints #-}
12module Rel8.Type.Array1D
13 ( Array1D( Array1D )
14 , getArray1D
15 )
16where
18-- aeson
19import Data.Aeson
20 ( ToJSON
21 , ToJSON1
22 , ToJSONKey
23 , FromJSON
24 , FromJSON1
25 , FromJSONKey
26 )
28-- base
29import Control.Applicative ( Alternative, (<|>) )
30import Control.Monad ( MonadPlus )
31import Data.Functor.Classes ( Eq1, Ord1, Read1, Show1 )
32import Data.Kind ( Type )
33import GHC.Exts ( IsList )
34import Prelude hiding ( null )
36-- hasql
37import qualified Hasql.Decoders as Hasql
39-- opaleye
40import qualified Opaleye.Internal.HaskellDB.PrimQuery as Opaleye
42-- rel8
43import Rel8.Expr.Opaleye ( zipPrimExprsWith )
44import Rel8.Expr.Serialize ( litExpr )
45import Rel8.Schema.Nullability
46 ( Unnullify
47 , Nullability( Nullable, NonNullable )
48 , Sql, nullabilization
49 )
50import Rel8.Type ( DBType, typeInformation )
51import Rel8.Type.Eq ( DBEq )
52import Rel8.Type.Information ( TypeInformation(..) )
53import Rel8.Type.Monoid ( DBMonoid, memptyExpr )
54import Rel8.Type.Ord ( DBMax, DBMin, DBOrd )
55import Rel8.Type.Semigroup ( DBSemigroup, (<>.) )
57-- semigroupoids
58import Data.Functor.Alt ( Alt, (<!>) )
59import Data.Functor.Apply ( Apply )
60import Data.Functor.Bind ( Bind )
61import Data.Functor.Plus ( Plus )
63-- semialign
64import Data.Align ( Align )
65import Data.Semialign ( Semialign )
66import Data.Zip ( Repeat, Unzip, Zip )
69newtype Array1D a = Array1D [a]
70 deriving stock Traversable
71 deriving newtype
72 ( Eq, Ord, Read, Show, Semigroup, Monoid, IsList
73 , Functor, Foldable
74 , Eq1, Ord1, Read1, Show1
75 , FromJSON1, ToJSON1, FromJSON, FromJSONKey, ToJSON, ToJSONKey
76 , Apply, Applicative, Alternative, Plus, Bind, Monad, MonadPlus
77 , Align, Semialign, Repeat, Unzip, Zip
78 )
81instance Alt Array1D where
82 (<!>) = (<|>)
85getArray1D :: Array1D a -> [a]
86getArray1D (Array1D a) = a
89type IsArray1D :: Type -> Bool
90type family IsArray1D a where
91 IsArray1D (Array1D _) = 'True
92 IsArray1D _ = 'False
95array1DTypeInformation :: IsArray1D db ~ 'False
96 => Nullability db a
97 -> TypeInformation db
98 -> TypeInformation (Array1D a)
99array1DTypeInformation nullability info =
100 case info of
101 TypeInformation{ encode, decode, typeName, out } -> TypeInformation
102 { decode = case nullability of
103 Nullable -> Array1D <$> Hasql.listArray (Hasql.nullable (out <$> decode))
104 NonNullable -> Array1D <$> Hasql.listArray (Hasql.nonNullable (out <$> decode))
105 , encode = case nullability of
106 Nullable -> Opaleye.ArrayExpr . fmap (maybe null encode) . getArray1D
107 NonNullable -> Opaleye.ArrayExpr . fmap encode . getArray1D
108 , typeName = typeName <> "[]"
109 , out = id
110 }
111 where
112 null = Opaleye.ConstExpr Opaleye.NullLit
115instance (Sql DBType a, IsArray1D (Unnullify a) ~ 'False) => DBType (Array1D a) where
116 typeInformation = array1DTypeInformation nullabilization typeInformation
119instance (Sql DBEq a, IsArray1D (Unnullify a) ~ 'False) => DBEq (Array1D a)
122instance (Sql DBOrd a, IsArray1D (Unnullify a) ~ 'False) => DBOrd (Array1D a)
125instance (Sql DBMax a, IsArray1D (Unnullify a) ~ 'False) => DBMax (Array1D a)
128instance (Sql DBMin a, IsArray1D (Unnullify a) ~ 'False) => DBMin (Array1D a)
131instance (Sql DBType a, IsArray1D (Unnullify a) ~ 'False) => DBSemigroup (Array1D a) where
132 (<>.) = zipPrimExprsWith (Opaleye.BinExpr (Opaleye.:||))
135instance (Sql DBType a, IsArray1D (Unnullify a) ~ 'False) => DBMonoid (Array1D a) where
136 memptyExpr = litExpr mempty