on 29cdfdb help sign in
rel8/src/Rel8/Schema/Name.hs
77 lines 2.2 kB history raw
1{-# language DataKinds #-}
2{-# language DerivingStrategies #-}
3{-# language FlexibleContexts #-}
4{-# language FlexibleInstances #-}
5{-# language GADTs #-}
6{-# language GeneralizedNewtypeDeriving #-}
7{-# language MultiParamTypeClasses #-}
8{-# language RankNTypes #-}
9{-# language StandaloneKindSignatures #-}
10{-# language TypeFamilies #-}
11{-# language UndecidableInstances #-}
13module Rel8.Schema.Name
14 ( Name(..)
15 , Selects
16 , ppColumn
17 )
18where
20-- base
21import Data.Functor.Identity ( Identity( Identity ) )
22import Data.Kind ( Constraint, Type )
23import Data.String ( IsString )
24import Prelude
26-- opaleye
27import qualified Opaleye.Internal.HaskellDB.Sql as Opaleye
28import qualified Opaleye.Internal.HaskellDB.Sql.Print as Opaleye
30-- pretty
31import Text.PrettyPrint ( Doc )
33-- rel8
34import Rel8.Expr ( Expr )
35import qualified Rel8.Schema.Kind as K
36import Rel8.Schema.HTable.Identity ( HIdentity( HIdentity ) )
37import Rel8.Schema.Null ( Sql )
38import Rel8.Table
39 ( Table, Columns, Context, fromColumns, toColumns
40 , FromExprs, fromResult, toResult
41 , Transpose
42 )
43import Rel8.Table.Transpose ( Transposes )
44import Rel8.Type ( DBType )
47-- | A @Name@ is the name of a column, as it would be defined in a table's
48-- schema definition. You can construct names by using the @OverloadedStrings@
49-- extension and writing string literals. This is typically done when providing
50-- a 'TableSchema' value.
51type Name :: K.Context
52newtype Name a = Name String
53 deriving stock Show
54 deriving newtype IsString
57instance Sql DBType a => Table Name (Name a) where
58 type Columns (Name a) = HIdentity a
59 type Context (Name a) = Name
60 type FromExprs (Name a) = a
61 type Transpose to (Name a) = to a
63 toColumns a = HIdentity a
64 fromColumns (HIdentity a) = a
65 toResult a = HIdentity (Identity a)
66 fromResult (HIdentity (Identity a)) = a
69-- | @Selects a b@ means that @a@ is a schema (i.e., a 'Table' of 'Name's) for
70-- the 'Expr' columns in @b@.
71type Selects :: Type -> Type -> Constraint
72class Transposes Name Expr names exprs => Selects names exprs
73instance Transposes Name Expr names exprs => Selects names exprs
76ppColumn :: String -> Doc
77ppColumn = Opaleye.ppSqlExpr . Opaleye.ColumnSqlExpr . Opaleye.SqlColumn