@@ -23,15 +23,16 @@ Type expressions.
23 , ScopedTypeVariables
23 , ScopedTypeVariables
24 , TypeApplications
24 , TypeApplications
25 , TypeOperators
25 , TypeOperators
26+ , UndecidableInstances
28module Squeal.PostgreSQL.Expression.Type
29module Squeal.PostgreSQL.Expression.Type
29− ( TypeExpression (..)
30+ ( -- * type casting
32 , inferredtype
33 , inferredtype
33− , PGTyped (..)
34+ -- * type expressions
34− , FieldTyped (..)
35+ , TypeExpression (..)
36 , typetable
37 , typetable
37 , typeview
38 , typeview
@@ -78,6 +79,22 @@ module Squeal.PostgreSQL.Expression.Type
79 , tstzrange
80 , tstzrange
80 , daterange
81 , daterange
82+ -- * column type definitions
83+ , ColumnTypeExpression (..)
96+ , PGNullityTyped (..)
83import Control.DeepSeq
100import Control.DeepSeq
@@ -91,6 +108,7 @@ import qualified Generics.SOP as SOP
92import Squeal.PostgreSQL.Alias
109import Squeal.PostgreSQL.Alias
93import Squeal.PostgreSQL.Expression
110import Squeal.PostgreSQL.Expression
111+import Squeal.PostgreSQL.PG
94import Squeal.PostgreSQL.Render
112import Squeal.PostgreSQL.Render
95import Squeal.PostgreSQL.Schema
113import Squeal.PostgreSQL.Schema
@@ -132,6 +150,7 @@ astype = cast
132inferredtype
150inferredtype
133 :: PGTyped schemas ty
151 :: PGTyped schemas ty
134 => Expression outer common grp schemas params from ty
152 => Expression outer common grp schemas params from ty
135 -> Expression outer common grp schemas params from ty
154 -> Expression outer common grp schemas params from ty
136inferredtype = astype pgtype
155inferredtype = astype pgtype
@@ -151,6 +170,7 @@ instance RenderSQL (TypeExpression schemas ty) where
152 :: (Has sch schemas schema, Has td schema ('Typedef ty))
171 :: (Has sch schemas schema, Has td schema ('Typedef ty))
153 => QualifiedAlias sch td
172 => QualifiedAlias sch td
154 -> TypeExpression schemas (null ty)
174 -> TypeExpression schemas (null ty)
155typedef = UnsafeTypeExpression . renderSQL
175typedef = UnsafeTypeExpression . renderSQL
@@ -159,6 +179,7 @@ typedef = UnsafeTypeExpression . renderSQL
160 :: (Has sch schemas schema, Has tab schema ('Table table))
180 :: (Has sch schemas schema, Has tab schema ('Table table))
161 => QualifiedAlias sch tab
181 => QualifiedAlias sch tab
162 -> TypeExpression schemas (null ('PGcomposite (TableToRow table)))
183 -> TypeExpression schemas (null ('PGcomposite (TableToRow table)))
163typetable = UnsafeTypeExpression . renderSQL
184typetable = UnsafeTypeExpression . renderSQL
@@ -167,6 +188,7 @@ typetable = UnsafeTypeExpression . renderSQL
168 :: (Has sch schemas schema, Has vw schema ('View view))
189 :: (Has sch schemas schema, Has vw schema ('View view))
169 => QualifiedAlias sch vw
190 => QualifiedAlias sch vw
170 -> TypeExpression schemas (null ('PGcomposite view))
192 -> TypeExpression schemas (null ('PGcomposite view))
171typeview = UnsafeTypeExpression . renderSQL
193typeview = UnsafeTypeExpression . renderSQL
@@ -353,3 +375,72 @@ class FieldTyped schemas ty where
353instance (KnownSymbol alias, PGTyped schemas ty)
375instance (KnownSymbol alias, PGTyped schemas ty)
354 => FieldTyped schemas (alias ::: ty) where
376 => FieldTyped schemas (alias ::: ty) where
355 fieldtype = pgtype `As` Alias
377 fieldtype = pgtype `As` Alias
379+-- | `ColumnTypeExpression`s are used in `createTable` commands.
380+newtype ColumnTypeExpression (schemas :: SchemasType) (ty :: ColumnType)
381+ = UnsafeColumnTypeExpression { renderColumnTypeExpression :: ByteString }
382+ deriving (GHC.Generic,Show,Eq,Ord,NFData)
383+instance RenderSQL (ColumnTypeExpression schemas ty) where
384+ renderSQL = renderColumnTypeExpression
386+-- | used in `createTable` commands as a column constraint to note that
387+-- @NULL@ may be present in a column
389+ :: TypeExpression schemas (nullity ty)
390+ -> ColumnTypeExpression schemas ('NoDef :=> 'Null ty)
391+nullable ty = UnsafeColumnTypeExpression $ renderSQL ty <+> "NULL"
393+-- | used in `createTable` commands as a column constraint to ensure
394+-- @NULL@ is not present in a column
396+ :: TypeExpression schemas (nullity ty)
397+ -> ColumnTypeExpression schemas ('NoDef :=> 'NotNull ty)
398+notNullable ty = UnsafeColumnTypeExpression $ renderSQL ty <+> "NOT NULL"
400+-- | used in `createTable` commands as a column constraint to give a default
402+ :: Expression '[] '[] 'Ungrouped schemas '[] '[] ty
403+ -> ColumnTypeExpression schemas ('NoDef :=> ty)
404+ -> ColumnTypeExpression schemas ('Def :=> ty)
405+default_ x ty = UnsafeColumnTypeExpression $
406+ renderSQL ty <+> "DEFAULT" <+> renderExpression x
408+-- | not a true type, but merely a notational convenience for creating
409+-- unique identifier columns with type `PGint2`
411+ :: ColumnTypeExpression schemas ('Def :=> 'NotNull 'PGint2)
412+serial2 = UnsafeColumnTypeExpression "serial2"
413+smallserial = UnsafeColumnTypeExpression "smallserial"
414+-- | not a true type, but merely a notational convenience for creating
415+-- unique identifier columns with type `PGint4`
417+ :: ColumnTypeExpression schemas ('Def :=> 'NotNull 'PGint4)
418+serial4 = UnsafeColumnTypeExpression "serial4"
419+serial = UnsafeColumnTypeExpression "serial"
420+-- | not a true type, but merely a notational convenience for creating
421+-- unique identifier columns with type `PGint8`
423+ :: ColumnTypeExpression schemas ('Def :=> 'NotNull 'PGint8)
424+serial8 = UnsafeColumnTypeExpression "serial8"
425+bigserial = UnsafeColumnTypeExpression "bigserial"
427+-- | Like @PGTyped@ but also accounts for nullity.
428+class PGNullityTyped schemas (nullty :: NullityType) where
429+ pgNullityType :: ColumnTypeExpression schemas ('NoDef :=> nullty)
431+instance PGTyped schemas ('Null ty) => PGNullityTyped schemas ('Null ty) where
432+ pgNullityType = nullable (pgtype @_ @('Null ty))
434+instance PGTyped schemas ('NotNull ty) => PGNullityTyped schemas ('NotNull ty) where
435+ pgNullityType = notNullable (pgtype @_ @('NotNull ty))
437+-- | Allow you to specify pg column types in relation to haskell types.
438+-- >>> printSQL $ hask @(Maybe String)
441+-- >>> printSQL $ hask @Double
444+ :: forall hask schemas. PGNullityTyped schemas (NullPG hask)
445+ => ColumnTypeExpression schemas ('NoDef :=> NullPG hask)