on 28aaa76 help sign in
rel8/src/Rel8/Schema/Name.hs
88 lines 2.7 kB history raw
1{-# language DataKinds #-}
2{-# language DerivingStrategies #-}
3{-# language FlexibleContexts #-}
4{-# language FlexibleInstances #-}
5{-# language GeneralizedNewtypeDeriving #-}
6{-# language MultiParamTypeClasses #-}
7{-# language RankNTypes #-}
8{-# language ScopedTypeVariables #-}
9{-# language StandaloneKindSignatures #-}
10{-# language TypeApplications #-}
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 )
26import Prelude
28-- rel8
29import Rel8.Expr ( Expr )
30import Rel8.Opaque ( Opaque )
31import Rel8.Schema.Context ( Interpretation, Col )
32import Rel8.Schema.Context.Label ( Labelable, labeler, unlabeler )
33import Rel8.Schema.HTable.Type ( HType( HType ) )
34import qualified Rel8.Schema.Kind as K
35import Rel8.Schema.Nullability ( Sql )
36import Rel8.Table ( Table, Columns, Context, fromColumns, toColumns )
37import Rel8.Table.Recontextualize ( Recontextualize )
38import Rel8.Type ( DBType )
41-- | A @Name@ is the name of a column, as it would be defined in a table's
42-- schema definition. You can construct names by using the @OverloadedStrings@
43-- extension and writing string literals. This is typically done when providing
44-- a 'TableSchema' value.
45type Name :: K.Context
46newtype Name a = Name String
47 deriving stock Show
48 deriving newtype (IsString, Monoid, Semigroup)
51instance Sql DBType a => Table Name (Name a) where
52 type Columns (Name a) = HType a
53 type Context (Name a) = Name
55 toColumns (Name a) = HType (NameCol a)
56 fromColumns (HType (NameCol a)) = Name a
59instance Sql DBType a => Recontextualize Expr Name (Expr a) (Name a)
62instance Sql DBType a => Recontextualize Identity Name (Identity a) (Name a)
65instance Sql DBType a => Recontextualize Name Expr (Name a) (Expr a)
68instance Sql DBType a => Recontextualize Name Identity (Name a) (Identity a)
71instance Sql DBType a => Recontextualize Name Name (Name a) (Name a)
74instance Interpretation Name where
75 newtype Col Name _spec = NameCol String
78instance Labelable Name where
79 labeler (NameCol a) = NameCol a
80 unlabeler (NameCol a) = NameCol a
83-- | @Selects a b@ means that @a@ is a schema (i.e., a 'Table' of 'Name's) for
84-- the 'Expr' columns in @b@.
85type Selects :: Type -> Type -> Constraint
86class Recontextualize Name Expr names exprs => Selects names exprs
87instance Recontextualize Name Expr names exprs => Selects names exprs
88instance {-# OVERLAPPING #-} Selects (Opaque Name Opaque) (Opaque Expr Opaque)