on db148ff help sign in
rel8/src/Rel8/Schema/Name.hs
97 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( NameCol )
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.Type ( HType( HType ) )
33import Rel8.Schema.Null ( Sql )
34import Rel8.Schema.Reify ( notReify )
35import Rel8.Schema.Result ( Result )
36import Rel8.Table
37 ( Table, Columns, Context, fromColumns, toColumns, reify, unreify
38 )
39import Rel8.Table.Recontextualize ( Recontextualize )
40import Rel8.Type ( DBType )
43-- | A @Name@ is the name of a column, as it would be defined in a table's
44-- schema definition. You can construct names by using the @OverloadedStrings@
45-- extension and writing string literals. This is typically done when providing
46-- a 'TableSchema' value.
47type Name :: k -> Type
48data Name a where
49 Name :: k ~ Type => !String -> Name (a :: k)
52deriving stock instance Show (Name a)
55instance k ~ Type => IsString (Name (a :: k)) where
56 fromString = Name
59instance Sql DBType a => Table Name (Name a) where
60 type Columns (Name a) = HType a
61 type Context (Name a) = Name
63 toColumns (Name a) = HType (NameCol a)
64 fromColumns (HType (NameCol a)) = Name a
65 reify = notReify
66 unreify = notReify
69instance Sql DBType a => Recontextualize Expr Name (Expr a) (Name a)
72instance Sql DBType a => Recontextualize Result Name (Identity a) (Name a)
75instance Sql DBType a => Recontextualize Name Expr (Name a) (Expr a)
78instance Sql DBType a => Recontextualize Name Result (Name a) (Identity a)
81instance Sql DBType a => Recontextualize Name Name (Name a) (Name a)
84instance Interpretation Name where
85 newtype Col Name _spec = NameCol String
88instance Labelable Name where
89 labeler (NameCol a) = NameCol a
90 unlabeler (NameCol a) = NameCol a
93-- | @Selects a b@ means that @a@ is a schema (i.e., a 'Table' of 'Name's) for
94-- the 'Expr' columns in @b@.
95type Selects :: Type -> Type -> Constraint
96class Recontextualize Name Expr names exprs => Selects names exprs
97instance Recontextualize Name Expr names exprs => Selects names exprs