on 2ba99ea help sign in
rel8/src/Rel8/Schema/Name.hs
99 lines 2.7 kB history raw
1{-# language DataKinds #-}
2{-# language DerivingStrategies #-}
3{-# language FlexibleContexts #-}
4{-# language FlexibleInstances #-}
5{-# language GADTs #-}
6{-# language MultiParamTypeClasses #-}
7{-# language PolyKinds #-}
8{-# language RankNTypes #-}
9{-# language StandaloneDeriving #-}
10{-# language StandaloneKindSignatures #-}
11{-# language TypeFamilies #-}
12{-# language UndecidableInstances #-}
13{-# language UndecidableSuperClasses #-}
15module Rel8.Schema.Name
16 ( Name(..)
17 , Col( N, unN )
18 , Selects
19 )
20where
22-- base
23import Data.Functor.Identity ( Identity )
24import Data.Kind ( Constraint, Type )
25import Data.String ( IsString, fromString )
26import Prelude
28-- rel8
29import Rel8.Expr ( Expr )
30import Rel8.Schema.Context ( Interpretation, Col )
31import Rel8.Schema.Context.Label ( Labelable, labeler, unlabeler )
32import Rel8.Schema.HTable.Identity ( HIdentity( HType ), HType )
33import Rel8.Schema.Null ( Sql )
34import Rel8.Schema.Reify ( notReify )
35import Rel8.Schema.Result ( Result )
36import Rel8.Schema.Spec ( Spec( Spec ) )
37import Rel8.Table
38 ( Table, Columns, Context, fromColumns, toColumns, reify, unreify
39 )
40import Rel8.Table.Recontextualize ( Recontextualize )
41import Rel8.Type ( DBType )
44-- | A @Name@ is the name of a column, as it would be defined in a table's
45-- schema definition. You can construct names by using the @OverloadedStrings@
46-- extension and writing string literals. This is typically done when providing
47-- a 'TableSchema' value.
48type Name :: k -> Type
49data Name a where
50 Name :: k ~ Type => !String -> Name (a :: k)
53deriving stock instance Show (Name a)
56instance k ~ Type => IsString (Name (a :: k)) where
57 fromString = Name
60instance Sql DBType a => Table Name (Name a) where
61 type Columns (Name a) = HType a
62 type Context (Name a) = Name
64 toColumns a = HType (N a)
65 fromColumns (HType (N a)) = a
66 reify = notReify
67 unreify = notReify
70instance Sql DBType a => Recontextualize Expr Name (Expr a) (Name a)
73instance Sql DBType a => Recontextualize Result Name (Identity a) (Name a)
76instance Sql DBType a => Recontextualize Name Expr (Name a) (Expr a)
79instance Sql DBType a => Recontextualize Name Result (Name a) (Identity a)
82instance Sql DBType a => Recontextualize Name Name (Name a) (Name a)
85instance Interpretation Name where
86 data Col Name _spec where
87 N :: {unN :: !(Name a)} -> Col Name ('Spec labels a)
90instance Labelable Name where
91 labeler (N a) = N a
92 unlabeler (N a) = N a
95-- | @Selects a b@ means that @a@ is a schema (i.e., a 'Table' of 'Name's) for
96-- the 'Expr' columns in @b@.
97type Selects :: Type -> Type -> Constraint
98class Recontextualize Name Expr names exprs => Selects names exprs
99instance Recontextualize Name Expr names exprs => Selects names exprs