@@ -69,6 +69,8 @@ module Squeal.PostgreSQL.Definition
69 , createTypeEnumFromHaskell
69 , createTypeEnumFromHaskell
70 , dropType
70 , dropType
71 , ColumnTypeExpression (..)
71 , ColumnTypeExpression (..)
72 , default_
74 , default_
74 , smallserial
76 , smallserial
@@ -120,7 +122,7 @@ CREATE statements
120-- >>> :set -XOverloadedLabels
122-- >>> :set -XOverloadedLabels
122-- renderDefinition $
124-- renderDefinition $
123−-- createTable #tab ((int & null_) `As` #a :* (real & null_) `As` #b :* Nil) Nil
125+-- createTable #tab ((int & hasNull) `As` #a :* (real & hasNull) `As` #b :* Nil) Nil
125-- "CREATE TABLE \"tab\" (\"a\" int NULL, \"b\" real NULL);"
127-- "CREATE TABLE \"tab\" (\"a\" int NULL, \"b\" real NULL);"
126createTable
128createTable
@@ -147,7 +149,7 @@ createTable tab columns constraints = UnsafeDefinition $
147-- >>> type Schema = '["tab" ::: 'Table Table]
149-- >>> type Schema = '["tab" ::: 'Table Table]
149-- renderDefinition
151-- renderDefinition
150−-- (createTableIfNotExists #tab ((int & null_) `As` #a :* (real & null_) `As` #b :* Nil) Nil :: Definition Schema Schema)
152+-- (createTableIfNotExists #tab ((int & hasNull) `As` #a :* (real & hasNull) `As` #b :* Nil) Nil :: Definition Schema Schema)
152-- "CREATE TABLE IF NOT EXISTS \"tab\" (\"a\" int NULL, \"b\" real NULL);"
154-- "CREATE TABLE IF NOT EXISTS \"tab\" (\"a\" int NULL, \"b\" real NULL);"
153createTableIfNotExists
155createTableIfNotExists
@@ -229,8 +231,8 @@ newtype TableConstraintExpression
230-- definition :: Definition '[] Schema
232-- definition :: Definition '[] Schema
231-- definition = createTable #tab
233-- definition = createTable #tab
232−-- ( (int & notNull) `As` #a :*
234+-- ( (int & hasNotNull) `As` #a :*
233−-- (int & notNull) `As` #b :* Nil )
235+-- (int & hasNotNull) `As` #b :* Nil )
234-- ( check (#a :* #b :* Nil) (#a .> #b) `As` #inequality :* Nil )
236-- ( check (#a :* #b :* Nil) (#a .> #b) `As` #inequality :* Nil )
@@ -260,8 +262,8 @@ check _cols condition = UnsafeTableConstraintExpression $
261-- definition :: Definition '[] Schema
263-- definition :: Definition '[] Schema
262-- definition = createTable #tab
264-- definition = createTable #tab
263−-- ( (int & null_) `As` #a :*
265+-- ( (int & hasNull) `As` #a :*
264−-- (int & null_) `As` #b :* Nil )
266+-- (int & hasNull) `As` #b :* Nil )
265-- ( unique (#a :* #b :* Nil) `As` #uq_a_b :* Nil )
267-- ( unique (#a :* #b :* Nil) `As` #uq_a_b :* Nil )
@@ -292,7 +294,7 @@ unique columns = UnsafeTableConstraintExpression $
292-- definition :: Definition '[] Schema
294-- definition :: Definition '[] Schema
293-- definition = createTable #tab
295-- definition = createTable #tab
294-- ( serial `As` #id :*
296-- ( serial `As` #id :*
295−-- (text & notNull) `As` #name :* Nil )
297+-- (text & hasNotNull) `As` #name :* Nil )
296-- ( primaryKey #id `As` #pk_id :* Nil )
298-- ( primaryKey #id `As` #pk_id :* Nil )
@@ -336,12 +338,12 @@ primaryKey columns = UnsafeTableConstraintExpression $
336-- setup =
338-- setup =
337-- createTable #users
339-- createTable #users
338-- ( serial `As` #id :*
340-- ( serial `As` #id :*
339−-- (text & notNull) `As` #name :* Nil )
341+-- (text & hasNotNull) `As` #name :* Nil )
340-- ( primaryKey #id `As` #pk_users :* Nil ) >>>
342-- ( primaryKey #id `As` #pk_users :* Nil ) >>>
341-- createTable #emails
343-- createTable #emails
342-- ( serial `As` #id :*
344-- ( serial `As` #id :*
343−-- (int & notNull) `As` #user_id :*
345+-- (int & hasNotNull) `As` #user_id :*
344−-- (text & null_) `As` #email :* Nil )
346+-- (text & hasNull) `As` #email :* Nil )
345-- ( primaryKey #id `As` #pk_emails :*
347-- ( primaryKey #id `As` #pk_emails :*
346-- foreignKey #user_id #users #id
348-- foreignKey #user_id #users #id
347-- OnDeleteCascade OnUpdateCascade `As` #fk_user_id :* Nil )
349-- OnDeleteCascade OnUpdateCascade `As` #fk_user_id :* Nil )
@@ -370,8 +372,8 @@ primaryKey columns = UnsafeTableConstraintExpression $
370-- setup =
372-- setup =
371-- createTable #employees
373-- createTable #employees
372-- ( serial `As` #id :*
374-- ( serial `As` #id :*
373−-- (text & notNull) `As` #name :*
375+-- (text & hasNotNull) `As` #name :*
374−-- (integer & null_) `As` #employer_id :* Nil )
376+-- (integer & hasNull) `As` #employer_id :* Nil )
375-- ( primaryKey #id `As` #employees_pk :*
377-- ( primaryKey #id `As` #employees_pk :*
376-- foreignKey #employer_id #employees #id
378-- foreignKey #employer_id #employees #id
377-- OnDeleteCascade OnUpdateCascade `As` #employees_employer_fk :* Nil )
379-- OnDeleteCascade OnUpdateCascade `As` #employees_employer_fk :* Nil )
@@ -573,7 +575,7 @@ class AddColumn ty where
573 -- '["tab" ::: 'Table ('[] :=>
575 -- '["tab" ::: 'Table ('[] :=>
574 -- '[ "col1" ::: 'NoDef :=> 'Null 'PGint4
576 -- '[ "col1" ::: 'NoDef :=> 'Null 'PGint4
575 -- , "col2" ::: 'Def :=> 'Null 'PGtext ])]
577 -- , "col2" ::: 'Def :=> 'Null 'PGtext ])]
576− -- definition = alterTable #tab (addColumn #col2 (text & null_ & default_ "foo"))
578+ -- definition = alterTable #tab (addColumn #col2 (text & hasNull & default_ "foo"))
577 -- in renderDefinition definition
579 -- in renderDefinition definition
579 -- "ALTER TABLE \"tab\" ADD COLUMN \"col2\" text NULL DEFAULT E'foo';"
581 -- "ALTER TABLE \"tab\" ADD COLUMN \"col2\" text NULL DEFAULT E'foo';"
@@ -585,7 +587,7 @@ class AddColumn ty where
585 -- '["tab" ::: 'Table ('[] :=>
587 -- '["tab" ::: 'Table ('[] :=>
586 -- '[ "col1" ::: 'NoDef :=> 'Null 'PGint4
588 -- '[ "col1" ::: 'NoDef :=> 'Null 'PGint4
587 -- , "col2" ::: 'NoDef :=> 'Null 'PGtext ])]
589 -- , "col2" ::: 'NoDef :=> 'Null 'PGtext ])]
588− -- definition = alterTable #tab (addColumn #col2 (text & null_))
590+ -- definition = alterTable #tab (addColumn #col2 (text & hasNull))
589 -- in renderDefinition definition
591 -- in renderDefinition definition
591 -- "ALTER TABLE \"tab\" ADD COLUMN \"col2\" text NULL;"
593 -- "ALTER TABLE \"tab\" ADD COLUMN \"col2\" text NULL;"
@@ -744,7 +746,7 @@ dropNotNull = UnsafeAlterColumn $ "DROP NOT NULL"
744-- '["tab" ::: 'Table ('[] :=> '["col" ::: 'NoDef :=> 'NotNull 'PGint4])]
746-- '["tab" ::: 'Table ('[] :=> '["col" ::: 'NoDef :=> 'NotNull 'PGint4])]
745-- '["tab" ::: 'Table ('[] :=> '["col" ::: 'NoDef :=> 'NotNull 'PGnumeric])]
747-- '["tab" ::: 'Table ('[] :=> '["col" ::: 'NoDef :=> 'NotNull 'PGnumeric])]
746-- definition =
748-- definition =
747−-- alterTable #tab (alterColumn #col (alterType (numeric & notNull)))
749+-- alterTable #tab (alterColumn #col (alterType (numeric & hasNotNull)))
748-- in renderDefinition definition
750-- in renderDefinition definition
750-- "ALTER TABLE \"tab\" ALTER COLUMN \"col\" TYPE numeric NOT NULL;"
752-- "ALTER TABLE \"tab\" ALTER COLUMN \"col\" TYPE numeric NOT NULL;"
@@ -824,18 +826,17 @@ instance (Has alias schema ('Typedef ty))
824 => IsLabel alias (ColumnTypeExpression schema ('NoDef :=> 'NotNull ty)) where
826 => IsLabel alias (ColumnTypeExpression schema ('NoDef :=> 'NotNull ty)) where
825 fromLabel = UnsafeColumnTypeExpression (renderAlias (fromLabel @alias))
827 fromLabel = UnsafeColumnTypeExpression (renderAlias (fromLabel @alias))
827−instance HasNull
829+hasNull
828− ( TypeExpression ty
830+ :: TypeExpression ty
829− -> ColumnTypeExpression schema ('NoDef :=> 'Null ty) ) where
831+ -> ColumnTypeExpression schema ('NoDef :=> 'Null ty)
830− null_ ty = UnsafeColumnTypeExpression $ renderTypeExpression ty <+> "NULL"
832+hasNull ty = UnsafeColumnTypeExpression $ renderTypeExpression ty <+> "NULL"
835− ColumnTypeExpression schema (def :=> 'NotNull ty) ) where
836− -- | used in `createTable` commands as a column constraint to ensure
834+-- | used in `createTable` commands as a column constraint to ensure
837− -- @NULL@ is not present
835+-- @NULL@ is not present
837+ :: TypeExpression ty
838+ -> ColumnTypeExpression schema (def :=> 'NotNull ty)
838− notNull ty = UnsafeColumnTypeExpression $ renderTypeExpression ty <+> "NOT NULL"
839+hasNotNull ty = UnsafeColumnTypeExpression $ renderTypeExpression ty <+> "NOT NULL"
840-- | used in `createTable` commands as a column constraint to give a default
841-- | used in `createTable` commands as a column constraint to give a default