Copyright | (c) Eitan Chatav 2017 |
---|---|
Maintainer | eitan@morphism.tech |
Stability | experimental |
Safe Haskell | None |
Language | Haskell2010 |
This module provides a type-level DSL for kinds of Postgres types, tables, schema, constraints, aliases, enumerated labels, and groupings. It also defines useful type families to operate on these. Finally, it defines an embedding of Haskell types into Postgres types.
Synopsis
- data PGType
- = PGbool
- | PGint2
- | PGint4
- | PGint8
- | PGnumeric
- | PGfloat4
- | PGfloat8
- | PGchar Nat
- | PGvarchar Nat
- | PGtext
- | PGbytea
- | PGtimestamp
- | PGtimestamptz
- | PGdate
- | PGtime
- | PGtimetz
- | PGinterval
- | PGuuid
- | PGinet
- | PGjson
- | PGjsonb
- | PGvararray NullityType
- | PGfixarray Nat NullityType
- | PGenum [Symbol]
- | PGcomposite RowType
- | UnsafePGType Symbol
- data NullityType
- type RowType = [(Symbol, NullityType)]
- type FromType = [(Symbol, RowType)]
- type family PG (hask :: Type) :: PGType
- type family NullPG (hask :: Type) :: NullityType where ...
- type family TuplePG (hask :: Type) :: [NullityType] where ...
- type family RowPG (hask :: Type) :: RowType where ...
- newtype Json hask = Json {
- getJson :: hask
- newtype Jsonb hask = Jsonb {
- getJsonb :: hask
- newtype Composite record = Composite {
- getComposite :: record
- newtype Enumerated enum = Enumerated {
- getEnumerated :: enum
- type ColumnType = (ColumnConstraint, NullityType)
- type ColumnsType = [(Symbol, ColumnType)]
- type TableType = (TableConstraints, ColumnsType)
- data SchemumType
- type SchemaType = [(Symbol, SchemumType)]
- type (:=>) constraint ty = '(constraint, ty)
- data ColumnConstraint
- data TableConstraint
- = Check [Symbol]
- | Unique [Symbol]
- | PrimaryKey [Symbol]
- | ForeignKey [Symbol] Symbol [Symbol]
- type TableConstraints = [(Symbol, TableConstraint)]
- type family Uniquely (key :: [Symbol]) (constraints :: TableConstraints) :: Constraint where ...
- type (:::) (alias :: Symbol) ty = '(alias, ty)
- data Alias (alias :: Symbol) = Alias
- renderAlias :: KnownSymbol alias => Alias alias -> ByteString
- renderAliases :: All KnownSymbol aliases => NP Alias aliases -> [ByteString]
- data Aliased expression aliased where
- As :: KnownSymbol alias => expression ty -> Alias alias -> Aliased expression (alias ::: ty)
- class KnownSymbol alias => Aliasable alias expression aliased | aliased -> expression, aliased -> alias where
- renderAliasedAs :: (forall ty. expression ty -> ByteString) -> Aliased expression aliased -> ByteString
- class KnownSymbol alias => Has (alias :: Symbol) (fields :: [(Symbol, kind)]) (field :: kind) | alias fields -> field
- type HasUnique alias fields field = fields ~ '[alias ::: field]
- class All KnownSymbol aliases => HasAll (aliases :: [Symbol]) (fields :: [(Symbol, kind)]) (subfields :: [(Symbol, kind)]) | aliases fields -> subfields
- class IsLabel (x :: Symbol) a where
- class IsQualified table column expression where
- renderAliasString :: KnownSymbol alias => Alias alias -> ByteString
- class IsPGlabel (label :: Symbol) expr where
- data PGlabel (label :: Symbol) = PGlabel
- renderLabel :: KnownSymbol label => proxy label -> ByteString
- renderLabels :: All KnownSymbol labels => NP PGlabel labels -> [ByteString]
- type family LabelsPG (hask :: Type) :: [ConstructorName] where ...
- data Grouping
- class (KnownSymbol table, KnownSymbol column) => GroupedBy table column bys
- data AlignedList p x0 x1 where
- Done :: AlignedList p x x
- (:>>) :: p x0 x1 -> AlignedList p x1 x2 -> AlignedList p x0 x2
- single :: p x0 x1 -> AlignedList p x0 x1
- type family Create alias x xs where ...
- type family Drop alias xs where ...
- type family Alter alias x xs where ...
- type family Rename alias0 alias1 xs where ...
- type family DropIfConstraintsInvolve column constraints where ...
- type family Join xs ys where ...
- type family Elem x xs where ...
- type family In x xs :: Constraint where ...
- type family Length (xs :: [k]) :: Nat where ...
- class HasOid (ty :: PGType) where
- type PGNum = '[PGint2, PGint4, PGint8, PGnumeric, PGfloat4, PGfloat8]
- type PGIntegral = '[PGint2, PGint4, PGint8]
- type PGFloating = '[PGfloat4, PGfloat8, PGnumeric]
- type family PGTypeOf (ty :: NullityType) :: PGType where ...
- type family PGArrayOf name arr ty :: Constraint where ...
- type family PGArray name arr :: Constraint where ...
- type PGTextArray name arr = PGArrayOf name arr (NotNull PGtext)
- type PGJsonType = '[PGjson, PGjsonb]
- type PGJsonKey = '[PGint2, PGint4, PGtext]
- class SamePGType (ty0 :: (Symbol, ColumnType)) (ty1 :: (Symbol, ColumnType))
- type family AllNotNull (columns :: ColumnsType) :: Constraint where ...
- type family NotAllNull (columns :: ColumnsType) :: Constraint where ...
- type family NullifyType (ty :: NullityType) :: NullityType where ...
- type family NullifyRow (columns :: RowType) :: RowType where ...
- type family NullifyFrom (tables :: FromType) :: FromType where ...
- type family TableToColumns (table :: TableType) :: ColumnsType where ...
- type family TableToRow (table :: TableType) :: RowType where ...
Postgres Types
PGType
is the promoted datakind of PostgreSQL types.
>>>
import Squeal.PostgreSQL.Schema
>>>
:kind 'PGbool
'PGbool :: PGType
PGbool | logical Boolean (true/false) |
PGint2 | signed two-byte integer |
PGint4 | signed four-byte integer |
PGint8 | signed eight-byte integer |
PGnumeric | arbitrary precision numeric type |
PGfloat4 | single precision floating-point number (4 bytes) |
PGfloat8 | double precision floating-point number (8 bytes) |
PGchar Nat | fixed-length character string |
PGvarchar Nat | variable-length character string |
PGtext | variable-length character string |
PGbytea | binary data ("byte array") |
PGtimestamp | date and time (no time zone) |
PGtimestamptz | date and time, including time zone |
PGdate | calendar date (year, month, day) |
PGtime | time of day (no time zone) |
PGtimetz | time of day, including time zone |
PGinterval | time span |
PGuuid | universally unique identifier |
PGinet | IPv4 or IPv6 host address |
PGjson | textual JSON data |
PGjsonb | binary JSON data, decomposed |
PGvararray NullityType | variable length array |
PGfixarray Nat NullityType | fixed length array |
PGenum [Symbol] | enumerated (enum) types are data types that comprise a static, ordered set of values. |
PGcomposite RowType | a composite type represents the structure of a row or record; it is essentially just a list of field names and their data types. |
UnsafePGType Symbol | an escape hatch for unsupported PostgreSQL types |
Instances
data NullityType Source #
NullityType
encodes the potential presence or definite absence of a
NULL
allowing operations which are sensitive to such to be well typed.
>>>
:kind 'Null 'PGint4
'Null 'PGint4 :: NullityType>>>
:kind 'NotNull ('PGvarchar 50)
'NotNull ('PGvarchar 50) :: NullityType
Instances
(Has table from columns, Has column columns ty, GroupedBy table column bys) => IsQualified table column (NP (Aliased (Expression schema from (Grouped bys) params)) ((column ::: ty) ': ([] :: [(Symbol, NullityType)]))) Source # | |
(Has table from columns, Has column columns ty, GroupedBy table column bys) => IsQualified table column (Aliased (Expression schema from (Grouped bys) params) (column ::: ty)) Source # | |
Defined in Squeal.PostgreSQL.Expression | |
(Has table from columns, Has column columns ty) => IsQualified table column (NP (Aliased (Expression schema from Ungrouped params)) ((column ::: ty) ': ([] :: [(Symbol, NullityType)]))) Source # | |
(Has table from columns, Has column columns ty) => IsQualified table column (Aliased (Expression schema from Ungrouped params) (column ::: ty)) Source # | |
Defined in Squeal.PostgreSQL.Expression | |
(KnownNat n, HasParameter (n - 1) schema params ty) => HasParameter n schema (ty' ': params) ty Source # | |
Defined in Squeal.PostgreSQL.Expression parameter :: TypeExpression schema ty -> Expression schema from grouping (ty' ': params) ty Source # | |
HasParameter 1 schema (ty1 ': tys) ty1 Source # | |
Defined in Squeal.PostgreSQL.Expression parameter :: TypeExpression schema ty1 -> Expression schema from grouping (ty1 ': tys) ty1 Source # | |
(HasUnique table from columns, Has column columns ty, GroupedBy table column bys) => IsLabel column (NP (Aliased (Expression schema from (Grouped bys) params)) ((column ::: ty) ': ([] :: [(Symbol, NullityType)]))) # | |
Defined in Squeal.PostgreSQL.Expression | |
(HasUnique table from columns, Has column columns ty, GroupedBy table column bys) => IsLabel column (Aliased (Expression schema from (Grouped bys) params) (column ::: ty)) # | |
Defined in Squeal.PostgreSQL.Expression | |
(HasUnique table from columns, Has column columns ty) => IsLabel column (NP (Aliased (Expression schema from Ungrouped params)) ((column ::: ty) ': ([] :: [(Symbol, NullityType)]))) # | |
Defined in Squeal.PostgreSQL.Expression | |
(HasUnique table from columns, Has column columns ty) => IsLabel column (Aliased (Expression schema from Ungrouped params) (column ::: ty)) # | |
Defined in Squeal.PostgreSQL.Expression | |
ty0 ~ ty1 => SamePGType (alias0 ::: (def0 :=> nullity0 ty0)) (alias1 ::: (def1 :=> nullity1 ty1)) Source # | |
Defined in Squeal.PostgreSQL.Schema | |
AddColumn (Def :=> ty) Source # | |
Defined in Squeal.PostgreSQL.Definition | |
AddColumn (NoDef :=> Null ty) Source # | |
Defined in Squeal.PostgreSQL.Definition |
type RowType = [(Symbol, NullityType)] Source #
A RowType
is a row of NullityType
. They correspond to Haskell
record types by means of RowPG
and are used in many places.
>>>
:{
type family PersonRow :: RowType where PersonRow = '[ "name" ::: 'NotNull 'PGtext , "age" ::: 'NotNull 'PGint4 , "dateOfBirth" ::: 'Null 'PGdate ] :}
type FromType = [(Symbol, RowType)] Source #
FromType
is a row of RowType
s. It can be thought of as
a product, or horizontal gluing and is used in FromClause
s
and TableExpression
s.
Haskell to Postgres Types
type family PG (hask :: Type) :: PGType Source #
The PG
type family embeds a subset of Haskell types
as Postgres types. As an open type family, PG
is extensible.
>>>
:kind! PG LocalTime
PG LocalTime :: PGType = 'PGtimestamp
>>>
newtype MyDouble = My Double
>>>
type instance PG MyDouble = 'PGfloat8
Instances
type family NullPG (hask :: Type) :: NullityType where ... Source #
NullPG
turns a Haskell type into a NullityType
.
>>>
:kind! NullPG Double
NullPG Double :: NullityType = 'NotNull 'PGfloat8>>>
:kind! NullPG (Maybe Double)
NullPG (Maybe Double) :: NullityType = 'Null 'PGfloat8
type family TuplePG (hask :: Type) :: [NullityType] where ... Source #
TuplePG
turns a Haskell tuple type (including record types) into
the corresponding list of NullityType
s.
>>>
:kind! TuplePG (Double, Maybe Char)
TuplePG (Double, Maybe Char) :: [NullityType] = '['NotNull 'PGfloat8, 'Null ('PGchar 1)]
type family RowPG (hask :: Type) :: RowType where ... Source #
RowPG
turns a Haskell record type into a RowType
.
>>>
data Person = Person { name :: Text, age :: Int32 } deriving GHC.Generic
>>>
instance Generic Person
>>>
instance HasDatatypeInfo Person
>>>
:kind! RowPG Person
RowPG Person :: [(Symbol, NullityType)] = '["name" ::: 'NotNull 'PGtext, "age" ::: 'NotNull 'PGint4]
RowPG hask = RowOf (RecordCodeOf hask) |
Instances
FromJSON x => FromValue PGjson (Json x) Source # | |
Eq hask => Eq (Json hask) Source # | |
Ord hask => Ord (Json hask) Source # | |
Defined in Squeal.PostgreSQL.Schema | |
Read hask => Read (Json hask) Source # | |
Show hask => Show (Json hask) Source # | |
Generic (Json hask) Source # | |
ToJSON x => ToParam (Json x) PGjson Source # | |
type Rep (Json hask) Source # | |
Defined in Squeal.PostgreSQL.Schema | |
type PG (Json hask) Source # | |
Defined in Squeal.PostgreSQL.Schema |
The Jsonb
newtype is an indication that the Haskell
type it's applied to should be stored as PGjsonb
.
Instances
FromJSON x => FromValue PGjsonb (Jsonb x) Source # | |
Eq hask => Eq (Jsonb hask) Source # | |
Ord hask => Ord (Jsonb hask) Source # | |
Read hask => Read (Jsonb hask) Source # | |
Show hask => Show (Jsonb hask) Source # | |
Generic (Jsonb hask) Source # | |
ToJSON x => ToParam (Jsonb x) PGjsonb Source # | |
type Rep (Jsonb hask) Source # | |
Defined in Squeal.PostgreSQL.Schema | |
type PG (Jsonb hask) Source # | |
Defined in Squeal.PostgreSQL.Schema |
newtype Composite record Source #
The Composite
newtype is an indication that the Haskell
type it's applied to should be stored as a PGcomposite
.
Composite | |
|
Instances
newtype Enumerated enum Source #
The Enumerated
newtype is an indication that the Haskell
type it's applied to should be stored as PGenum
.
Enumerated | |
|
Instances
Schema Types
type ColumnType = (ColumnConstraint, NullityType) Source #
ColumnType
encodes the allowance of DEFAULT
and NULL
and the
base PGType
for a column.
>>>
:set -XTypeFamilies -XTypeInType
>>>
import GHC.TypeLits
>>>
type family IdColumn :: ColumnType where IdColumn = 'Def :=> 'NotNull 'PGint4
>>>
type family EmailColumn :: ColumnType where EmailColumn = 'NoDef :=> 'Null 'PGtext
type ColumnsType = [(Symbol, ColumnType)] Source #
ColumnsType
is a row of ColumnType
s.
>>>
:{
type family UsersColumns :: ColumnsType where UsersColumns = '[ "name" ::: 'NoDef :=> 'NotNull 'PGtext , "id" ::: 'Def :=> 'NotNull 'PGint4 ] :}
type TableType = (TableConstraints, ColumnsType) Source #
TableType
encodes a row of constraints on a table as well as the types
of its columns.
>>>
:{
type family UsersTable :: TableType where UsersTable = '[ "pk_users" ::: 'PrimaryKey '["id"] ] :=> '[ "id" ::: 'Def :=> 'NotNull 'PGint4 , "name" ::: 'NoDef :=> 'NotNull 'PGtext ] :}
data SchemumType Source #
A SchemumType
is a user-defined type, either a Table
,
View
or Typedef
.
Instances
Category Definition # | |
Defined in Squeal.PostgreSQL.Definition id :: Definition a a # (.) :: Definition b c -> Definition a b -> Definition a c # | |
(KnownSymbol alias, schema1 ~ ((alias ::: View cte) ': schema)) => Aliasable alias (statement schema params cte) (AlignedList (CommonTableExpression statement params) schema schema1) Source # | |
Defined in Squeal.PostgreSQL.Query as :: statement schema params cte -> Alias alias -> AlignedList (CommonTableExpression statement params) schema schema1 Source # |
type SchemaType = [(Symbol, SchemumType)] Source #
The schema of a database consists of a list of aliased,
user-defined SchemumType
s.
>>>
:{
type family Schema :: SchemaType where Schema = '[ "users" ::: 'Table ( '[ "pk_users" ::: 'PrimaryKey '["id"] ] :=> '[ "id" ::: 'Def :=> 'NotNull 'PGint4 , "name" ::: 'NoDef :=> 'NotNull 'PGtext ]) , "emails" ::: 'Table ( '[ "pk_emails" ::: 'PrimaryKey '["id"] , "fk_user_id" ::: 'ForeignKey '["user_id"] "users" '["id"] ] :=> '[ "id" ::: 'Def :=> 'NotNull 'PGint4 , "user_id" ::: 'NoDef :=> 'NotNull 'PGint4 , "email" ::: 'NoDef :=> 'Null 'PGtext ]) ] :}
Constraints
type (:=>) constraint ty = '(constraint, ty) infixr 7 Source #
The constraint operator, :=>
is a type level pair
between a "constraint" and some type, for use in pairing
a ColumnConstraint
with a NullityType
to produce a ColumnType
or a TableConstraints
and a ColumnsType
to produce a TableType
.
data ColumnConstraint Source #
ColumnConstraint
encodes the availability of DEFAULT
for inserts and updates.
A column can be assigned a default value.
A data Manipulation
command can also
request explicitly that a column be set to its default value,
without having to know what that value is.
Instances
ty0 ~ ty1 => SamePGType (alias0 ::: (def0 :=> nullity0 ty0)) (alias1 ::: (def1 :=> nullity1 ty1)) Source # | |
Defined in Squeal.PostgreSQL.Schema | |
AddColumn (Def :=> ty) Source # | |
Defined in Squeal.PostgreSQL.Definition | |
AddColumn (NoDef :=> Null ty) Source # | |
Defined in Squeal.PostgreSQL.Definition |
data TableConstraint Source #
TableConstraint
encodes various forms of data constraints
of columns in a table.
TableConstraint
s give you as much control over the data in your tables
as you wish. If a user attempts to store data in a column that would
violate a constraint, an error is raised. This applies
even if the value came from the default value definition.
Check [Symbol] | |
Unique [Symbol] | |
PrimaryKey [Symbol] | |
ForeignKey [Symbol] Symbol [Symbol] |
type TableConstraints = [(Symbol, TableConstraint)] Source #
A TableConstraints
is a row of TableConstraint
s.
type family Uniquely (key :: [Symbol]) (constraints :: TableConstraints) :: Constraint where ... Source #
A ForeignKey
must reference columns that either are
a PrimaryKey
or form a Unique
constraint.
Aliases
data Alias (alias :: Symbol) Source #
Alias
es are proxies for a type level string or Symbol
and have an IsLabel
instance so that with -XOverloadedLabels
>>>
:set -XOverloadedLabels
>>>
#foobar :: Alias "foobar"
Alias
Instances
IsQualified table column (Alias table, Alias column) Source # | |
alias1 ~ alias2 => IsLabel alias1 (Alias alias2) Source # | |
Defined in Squeal.PostgreSQL.Schema | |
(alias0 ~ alias1, alias0 ~ alias2, KnownSymbol alias2) => IsLabel alias0 (Aliased Alias (alias1 ::: alias2)) Source # | |
aliases ~ (alias ': ([] :: [Symbol])) => IsLabel alias (NP Alias aliases) Source # | |
Defined in Squeal.PostgreSQL.Schema | |
Eq (Alias alias) Source # | |
Ord (Alias alias) Source # | |
Defined in Squeal.PostgreSQL.Schema | |
Show (Alias alias) Source # | |
Generic (Alias alias) Source # | |
NFData (Alias alias) Source # | |
Defined in Squeal.PostgreSQL.Schema | |
KnownSymbol alias => RenderSQL (Alias alias) Source # | |
Defined in Squeal.PostgreSQL.Schema renderSQL :: Alias alias -> ByteString Source # | |
type Rep (Alias alias) Source # | |
renderAlias :: KnownSymbol alias => Alias alias -> ByteString Source #
>>>
renderAlias #jimbob
"\"jimbob\""
renderAliases :: All KnownSymbol aliases => NP Alias aliases -> [ByteString] Source #
>>>
import Generics.SOP (NP(..))
>>>
renderAliases (#jimbob :* #kandi)
["\"jimbob\"","\"kandi\""]
data Aliased expression aliased where Source #
The As
operator is used to name an expression. As
is like a demoted
version of :::
.
>>>
Just "hello" `As` #hi :: Aliased Maybe ("hi" ::: String)
As (Just "hello") Alias
As :: KnownSymbol alias => expression ty -> Alias alias -> Aliased expression (alias ::: ty) |
Instances
(Has table from columns, Has column columns ty, GroupedBy table column bys) => IsQualified table column (NP (Aliased (Expression schema from (Grouped bys) params)) ((column ::: ty) ': ([] :: [(Symbol, NullityType)]))) Source # | |
(Has table from columns, Has column columns ty, GroupedBy table column bys) => IsQualified table column (Aliased (Expression schema from (Grouped bys) params) (column ::: ty)) Source # | |
Defined in Squeal.PostgreSQL.Expression | |
(Has table from columns, Has column columns ty) => IsQualified table column (NP (Aliased (Expression schema from Ungrouped params)) ((column ::: ty) ': ([] :: [(Symbol, NullityType)]))) Source # | |
(Has table from columns, Has column columns ty) => IsQualified table column (Aliased (Expression schema from Ungrouped params) (column ::: ty)) Source # | |
Defined in Squeal.PostgreSQL.Expression | |
(KnownSymbol alias, tys ~ ((alias ::: ty) ': ([] :: [(Symbol, k)]))) => Aliasable alias (expression ty) (NP (Aliased expression) tys) Source # | |
(KnownSymbol alias, alias ~ alias1) => Aliasable alias (expression ty) (Aliased expression (alias1 ::: ty)) Source # | |
(alias0 ~ alias1, alias0 ~ alias2, KnownSymbol alias2) => IsLabel alias0 (Aliased Alias (alias1 ::: alias2)) Source # | |
(HasUnique table from columns, Has column columns ty, GroupedBy table column bys) => IsLabel column (NP (Aliased (Expression schema from (Grouped bys) params)) ((column ::: ty) ': ([] :: [(Symbol, NullityType)]))) # | |
Defined in Squeal.PostgreSQL.Expression | |
(HasUnique table from columns, Has column columns ty, GroupedBy table column bys) => IsLabel column (Aliased (Expression schema from (Grouped bys) params) (column ::: ty)) # | |
Defined in Squeal.PostgreSQL.Expression | |
(HasUnique table from columns, Has column columns ty) => IsLabel column (NP (Aliased (Expression schema from Ungrouped params)) ((column ::: ty) ': ([] :: [(Symbol, NullityType)]))) # | |
Defined in Squeal.PostgreSQL.Expression | |
(HasUnique table from columns, Has column columns ty) => IsLabel column (Aliased (Expression schema from Ungrouped params) (column ::: ty)) # | |
Defined in Squeal.PostgreSQL.Expression | |
Eq (expression ty) => Eq (Aliased expression (alias ::: ty)) Source # | |
Ord (expression ty) => Ord (Aliased expression (alias ::: ty)) Source # | |
Defined in Squeal.PostgreSQL.Schema compare :: Aliased expression (alias ::: ty) -> Aliased expression (alias ::: ty) -> Ordering # (<) :: Aliased expression (alias ::: ty) -> Aliased expression (alias ::: ty) -> Bool # (<=) :: Aliased expression (alias ::: ty) -> Aliased expression (alias ::: ty) -> Bool # (>) :: Aliased expression (alias ::: ty) -> Aliased expression (alias ::: ty) -> Bool # (>=) :: Aliased expression (alias ::: ty) -> Aliased expression (alias ::: ty) -> Bool # max :: Aliased expression (alias ::: ty) -> Aliased expression (alias ::: ty) -> Aliased expression (alias ::: ty) # min :: Aliased expression (alias ::: ty) -> Aliased expression (alias ::: ty) -> Aliased expression (alias ::: ty) # | |
Show (expression ty) => Show (Aliased expression (alias ::: ty)) Source # | |
class KnownSymbol alias => Aliasable alias expression aliased | aliased -> expression, aliased -> alias where Source #
Instances
(KnownSymbol alias, tys ~ ((alias ::: ty) ': ([] :: [(Symbol, k)]))) => Aliasable alias (expression ty) (NP (Aliased expression) tys) Source # | |
(KnownSymbol alias, alias ~ alias1) => Aliasable alias (expression ty) (Aliased expression (alias1 ::: ty)) Source # | |
(KnownSymbol alias, schema1 ~ ((alias ::: View cte) ': schema)) => Aliasable alias (statement schema params cte) (AlignedList (CommonTableExpression statement params) schema schema1) Source # | |
Defined in Squeal.PostgreSQL.Query as :: statement schema params cte -> Alias alias -> AlignedList (CommonTableExpression statement params) schema schema1 Source # | |
(KnownSymbol alias, schema1 ~ ((alias ::: View cte) ': schema)) => Aliasable alias (statement schema params cte) (CommonTableExpression statement params schema schema1) Source # | |
Defined in Squeal.PostgreSQL.Query as :: statement schema params cte -> Alias alias -> CommonTableExpression statement params schema schema1 Source # |
renderAliasedAs :: (forall ty. expression ty -> ByteString) -> Aliased expression aliased -> ByteString Source #
>>>
let renderMaybe = fromString . maybe "Nothing" (const "Just")
>>>
renderAliasedAs renderMaybe (Just (3::Int) `As` #an_int)
"Just AS \"an_int\""
class KnownSymbol alias => Has (alias :: Symbol) (fields :: [(Symbol, kind)]) (field :: kind) | alias fields -> field Source #
Has alias fields field
is a constraint that proves that
fields
has a field of alias ::: field
, inferring field
from alias
and fields
.
Instances
(KnownSymbol alias, Has alias fields field) => Has alias (field' ': fields :: [(Symbol, kind)]) (field :: kind) Source # | |
Defined in Squeal.PostgreSQL.Schema | |
KnownSymbol alias => Has alias ((alias ::: field) ': fields :: [(Symbol, kind)]) (field :: kind) Source # | |
Defined in Squeal.PostgreSQL.Schema |
type HasUnique alias fields field = fields ~ '[alias ::: field] Source #
HasUnique alias fields field
is a constraint that proves that
fields
is a singleton of alias ::: field
.
class All KnownSymbol aliases => HasAll (aliases :: [Symbol]) (fields :: [(Symbol, kind)]) (subfields :: [(Symbol, kind)]) | aliases fields -> subfields Source #
Instances
HasAll ([] :: [Symbol]) (fields :: [(Symbol, kind)]) ([] :: [(Symbol, kind)]) Source # | |
Defined in Squeal.PostgreSQL.Schema | |
(Has alias fields field, HasAll aliases fields subfields) => HasAll (alias ': aliases) (fields :: [(Symbol, k)]) ((alias ::: field) ': subfields :: [(Symbol, k)]) Source # | |
Defined in Squeal.PostgreSQL.Schema |
class IsLabel (x :: Symbol) a where #
Instances
alias1 ~ alias2 => IsLabel alias1 (Alias alias2) # | |
Defined in Squeal.PostgreSQL.Schema | |
(HasUnique rel rels cols, Has col cols ty, by ~ (,) rel col) => IsLabel col (By rels by) # | |
Defined in Squeal.PostgreSQL.Query | |
(alias0 ~ alias1, alias0 ~ alias2, KnownSymbol alias2) => IsLabel alias0 (Aliased Alias (alias1 ::: alias2)) # | |
aliases ~ (alias ': ([] :: [Symbol])) => IsLabel alias (NP Alias aliases) # | |
Defined in Squeal.PostgreSQL.Schema | |
(HasUnique table from columns, Has column columns ty, GroupedBy table column bys) => IsLabel column (NP (Aliased (Expression schema from (Grouped bys) params)) ((column ::: ty) ': ([] :: [(Symbol, NullityType)]))) # | |
Defined in Squeal.PostgreSQL.Expression | |
(HasUnique table from columns, Has column columns ty, GroupedBy table column bys) => IsLabel column (Aliased (Expression schema from (Grouped bys) params) (column ::: ty)) # | |
Defined in Squeal.PostgreSQL.Expression | |
(HasUnique table from columns, Has column columns ty) => IsLabel column (NP (Aliased (Expression schema from Ungrouped params)) ((column ::: ty) ': ([] :: [(Symbol, NullityType)]))) # | |
Defined in Squeal.PostgreSQL.Expression | |
(HasUnique table from columns, Has column columns ty) => IsLabel column (Aliased (Expression schema from Ungrouped params) (column ::: ty)) # | |
Defined in Squeal.PostgreSQL.Expression | |
(HasUnique rel rels cols, Has col cols ty, bys ~ ((,) rel col ': ([] :: [(Symbol, Symbol)]))) => IsLabel col (NP (By rels) bys) # | |
Defined in Squeal.PostgreSQL.Query | |
(HasUnique table from columns, Has column columns ty, GroupedBy table column bys) => IsLabel column (Expression schema from (Grouped bys) params ty) # | |
Defined in Squeal.PostgreSQL.Expression fromLabel :: Expression schema from (Grouped bys) params ty # | |
(HasUnique table from columns, Has column columns ty) => IsLabel column (Expression schema from Ungrouped params ty) # | |
Defined in Squeal.PostgreSQL.Expression fromLabel :: Expression schema from Ungrouped params ty # |
class IsQualified table column expression where Source #
Analagous to IsLabel
, the constraint
IsQualified
defines !
for a column alias qualified
by a table alias.
Instances
IsQualified table column (Alias table, Alias column) Source # | |
(Has rel rels cols, Has col cols ty, by ~ (,) rel col) => IsQualified rel col (By rels by) Source # | |
(Has table from columns, Has column columns ty, GroupedBy table column bys) => IsQualified table column (NP (Aliased (Expression schema from (Grouped bys) params)) ((column ::: ty) ': ([] :: [(Symbol, NullityType)]))) Source # | |
(Has table from columns, Has column columns ty, GroupedBy table column bys) => IsQualified table column (Aliased (Expression schema from (Grouped bys) params) (column ::: ty)) Source # | |
Defined in Squeal.PostgreSQL.Expression | |
(Has table from columns, Has column columns ty) => IsQualified table column (NP (Aliased (Expression schema from Ungrouped params)) ((column ::: ty) ': ([] :: [(Symbol, NullityType)]))) Source # | |
(Has table from columns, Has column columns ty) => IsQualified table column (Aliased (Expression schema from Ungrouped params) (column ::: ty)) Source # | |
Defined in Squeal.PostgreSQL.Expression | |
(Has rel rels cols, Has col cols ty, bys ~ ((,) rel col ': ([] :: [(Symbol, Symbol)]))) => IsQualified rel col (NP (By rels) bys) Source # | |
(Has table from columns, Has column columns ty, GroupedBy table column bys) => IsQualified table column (Expression schema from (Grouped bys) params ty) Source # | |
Defined in Squeal.PostgreSQL.Expression | |
(Has table from columns, Has column columns ty) => IsQualified table column (Expression schema from Ungrouped params ty) Source # | |
Defined in Squeal.PostgreSQL.Expression |
renderAliasString :: KnownSymbol alias => Alias alias -> ByteString Source #
>>>
renderAliasString #ohmahgerd
"'ohmahgerd'"
Enumerated Labels
class IsPGlabel (label :: Symbol) expr where Source #
IsPGlabel
looks very much like the IsLabel
class. Whereas
the overloaded label, fromLabel
is used for column references,
label
s are used for enum terms. A label
is called with
type application like `label @"beef"`.
Instances
label ~ label1 => IsPGlabel label (PGlabel label1) Source # | |
Defined in Squeal.PostgreSQL.Schema | |
labels ~ (label ': ([] :: [Symbol])) => IsPGlabel label (NP PGlabel labels) Source # | |
(KnownSymbol label, In label labels) => IsPGlabel label (Expression schema from grouping params (nullity (PGenum labels))) Source # | |
Defined in Squeal.PostgreSQL.Expression label :: Expression schema from grouping params (nullity (PGenum labels)) Source # |
renderLabel :: KnownSymbol label => proxy label -> ByteString Source #
Renders a label
renderLabels :: All KnownSymbol labels => NP PGlabel labels -> [ByteString] Source #
Renders a list of labels
type family LabelsPG (hask :: Type) :: [ConstructorName] where ... Source #
The LabelsPG
type family calculates the constructors of a
Haskell enum type.
>>>
data Schwarma = Beef | Lamb | Chicken deriving GHC.Generic
>>>
instance Generic Schwarma
>>>
instance HasDatatypeInfo Schwarma
>>>
:kind! LabelsPG Schwarma
LabelsPG Schwarma :: [Type.ConstructorName] = '["Beef", "Lamb", "Chicken"]
LabelsPG hask = ConstructorNamesOf (ConstructorsOf (DatatypeInfoOf hask)) |
Grouping
class (KnownSymbol table, KnownSymbol column) => GroupedBy table column bys Source #
A GroupedBy
constraint indicates that a table qualified column is
a member of the auxiliary namespace created by GROUP BY
clauses and thus,
may be called in an output Expression
without aggregating.
Instances
(KnownSymbol table, KnownSymbol column) => GroupedBy table column ((,) table column ': bys :: [(Symbol, Symbol)]) Source # | |
Defined in Squeal.PostgreSQL.Schema | |
(KnownSymbol table, KnownSymbol column, GroupedBy table column bys) => GroupedBy table column (tabcol ': bys :: [a]) Source # | |
Defined in Squeal.PostgreSQL.Schema |
Aligned lists
data AlignedList p x0 x1 where Source #
An AlignedList
is a type-aligned list or free category.
Done :: AlignedList p x x | |
(:>>) :: p x0 x1 -> AlignedList p x1 x2 -> AlignedList p x0 x2 infixr 7 |
Instances
Category (AlignedList p :: k -> k -> *) Source # | |
Defined in Squeal.PostgreSQL.Schema id :: AlignedList p a a # (.) :: AlignedList p b c -> AlignedList p a b -> AlignedList p a c # | |
(KnownSymbol alias, schema1 ~ ((alias ::: View cte) ': schema)) => Aliasable alias (statement schema params cte) (AlignedList (CommonTableExpression statement params) schema schema1) Source # | |
Defined in Squeal.PostgreSQL.Query as :: statement schema params cte -> Alias alias -> AlignedList (CommonTableExpression statement params) schema schema1 Source # |
single :: p x0 x1 -> AlignedList p x0 x1 Source #
A single
step.
Data Definitions
type family Create alias x xs where ... Source #
Create alias x xs
adds alias ::: x
to the end of xs
and is used in
createTable
statements and in ALTER TABLE
addColumn
.
type family Drop alias xs where ... Source #
Drop alias xs
removes the type associated with alias
in xs
and is used in dropTable
statements
and in ALTER TABLE
dropColumn
statements.
type family Alter alias x xs where ... Source #
Alter alias x xs
replaces the type associated with an alias
in xs
with the type x
and is used in alterTable
and alterColumn
.
type family Rename alias0 alias1 xs where ... Source #
Rename alias0 alias1 xs
replaces the alias alias0
by alias1
in xs
and is used in alterTableRename
and
renameColumn
.
type family DropIfConstraintsInvolve column constraints where ... Source #
Drop all TableConstraint
s that involve a column
DropIfConstraintsInvolve column '[] = '[] | |
DropIfConstraintsInvolve column ((alias ::: constraint) ': constraints) = If (ConstraintInvolves column constraint) (DropIfConstraintsInvolve column constraints) ((alias ::: constraint) ': DropIfConstraintsInvolve column constraints) |
Lists
type family Join xs ys where ... Source #
Join
is simply promoted ++
and is used in JOIN
s in
FromClause
s.
type family In x xs :: Constraint where ... Source #
In x xs
is a constraint that proves that x
is in xs
.
type family Length (xs :: [k]) :: Nat where ... Source #
Calculate the Length
of a type level list
>>>
:kind! Length '[Char,String,Bool,Double]
Length '[Char,String,Bool,Double] :: Nat = 4
Type Classifications
class HasOid (ty :: PGType) where Source #
The object identifier of a PGType
.
>>>
:set -XTypeApplications
>>>
oid @'PGbool
16
Instances
type PGNum = '[PGint2, PGint4, PGint8, PGnumeric, PGfloat4, PGfloat8] Source #
Numeric Postgres types.
type family PGTypeOf (ty :: NullityType) :: PGType where ... Source #
PGTypeOf
forgets about NULL
and any column constraints.
PGTypeOf (nullity pg) = pg |
type family PGArrayOf name arr ty :: Constraint where ... Source #
Ensure a type is a valid array type with a specific element type.
PGArrayOf name (PGvararray x) ty = x ~ ty | |
PGArrayOf name (PGfixarray n x) ty = x ~ ty | |
PGArrayOf name val ty = TypeError ((((Text name :<>: Text "Unsatisfied PGArrayOf constraint. Expected either: ") :$$: (Text " \8226 " :<>: ErrPGvararrayOf (ShowType ty))) :$$: (Text " \8226 " :<>: ErrPGfixarrayOf (ShowType ty))) :$$: (Text "But got: " :<>: ShowType val)) |
type family PGArray name arr :: Constraint where ... Source #
Ensure a type is a valid array type.
PGArray name (PGvararray x) = () | |
PGArray name (PGfixarray n x) = () | |
PGArray name val = TypeError ((((Text name :<>: Text ": Unsatisfied PGArray constraint. Expected either: ") :$$: (Text " \8226 " :<>: ErrPGvararrayOf (Placeholder PGType))) :$$: (Text " \8226 " :<>: ErrPGfixarrayOf (Placeholder PGType))) :$$: (Text "But got: " :<>: ShowType val)) |
type PGTextArray name arr = PGArrayOf name arr (NotNull PGtext) Source #
Ensure a type is a valid array type whose elements are text.
type PGJsonType = '[PGjson, PGjsonb] Source #
Is a type a valid JSON type?
class SamePGType (ty0 :: (Symbol, ColumnType)) (ty1 :: (Symbol, ColumnType)) Source #
Equality constraint on the underlying PGType
of two columns.
Instances
ty0 ~ ty1 => SamePGType (alias0 ::: (def0 :=> nullity0 ty0)) (alias1 ::: (def1 :=> nullity1 ty1)) Source # | |
Defined in Squeal.PostgreSQL.Schema |
type family AllNotNull (columns :: ColumnsType) :: Constraint where ... Source #
AllNotNull
is a constraint that proves a ColumnsType
has no NULL
s.
AllNotNull '[] = () | |
AllNotNull ((column ::: (def :=> NotNull ty)) ': columns) = AllNotNull columns |
type family NotAllNull (columns :: ColumnsType) :: Constraint where ... Source #
NotAllNull
is a constraint that proves a ColumnsType
has some
NOT NULL
.
NotAllNull ((column ::: (def :=> NotNull ty)) ': columns) = () | |
NotAllNull ((column ::: (def :=> Null ty)) ': columns) = NotAllNull columns |
Nullifications
type family NullifyType (ty :: NullityType) :: NullityType where ... Source #
NullifyType
is an idempotent that nullifies a NullityType
.
NullifyType (Null ty) = Null ty | |
NullifyType (NotNull ty) = Null ty |
type family NullifyRow (columns :: RowType) :: RowType where ... Source #
NullifyRow
is an idempotent that nullifies a RowType
.
NullifyRow '[] = '[] | |
NullifyRow ((column ::: ty) ': columns) = (column ::: NullifyType ty) ': NullifyRow columns |
type family NullifyFrom (tables :: FromType) :: FromType where ... Source #
NullifyFrom
is an idempotent that nullifies a FromType
used to nullify the left or right hand side of an outer join
in a FromClause
.
NullifyFrom '[] = '[] | |
NullifyFrom ((table ::: columns) ': tables) = (table ::: NullifyRow columns) ': NullifyFrom tables |
Table Conversions
type family TableToColumns (table :: TableType) :: ColumnsType where ... Source #
TableToColumns
removes table constraints.
TableToColumns (constraints :=> columns) = columns |
type family TableToRow (table :: TableType) :: RowType where ... Source #
Convert a table to a row type.
TableToRow tab = ColumnsToRow (TableToColumns tab) |