@@ -69,6 +69,8 @@ module Squeal.PostgreSQL.Definition
6969 , createTypeEnumFromHaskell
7070 , dropType
7171 , ColumnTypeExpression (..)
72+ , hasNull
73+ , hasNotNull
7274 , default_
7375 , serial2
7476 , smallserial
@@ -120,7 +122,7 @@ CREATE statements
120122-- >>> :set -XOverloadedLabels
121123-- >>> :{
122124-- 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
124126-- :}
125127-- "CREATE TABLE \"tab\" (\"a\" int NULL, \"b\" real NULL);"
126128createTable
@@ -147,7 +149,7 @@ createTable tab columns constraints = UnsafeDefinition $
147149-- >>> type Schema = '["tab" ::: 'Table Table]
148150-- >>> :{
149151-- 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)
151153-- :}
152154-- "CREATE TABLE IF NOT EXISTS \"tab\" (\"a\" int NULL, \"b\" real NULL);"
153155createTableIfNotExists
@@ -229,8 +231,8 @@ newtype TableConstraintExpression
229231-- let
230232-- definition :: Definition '[] Schema
231233-- definition = createTable #tab
232−-- ( (int & notNull) `As` #a :*
233−-- (int & notNull) `As` #b :* Nil )
234+-- ( (int & hasNotNull) `As` #a :*
235+-- (int & hasNotNull) `As` #b :* Nil )
234236-- ( check (#a :* #b :* Nil) (#a .> #b) `As` #inequality :* Nil )
235237-- :}
236238--
@@ -260,8 +262,8 @@ check _cols condition = UnsafeTableConstraintExpression $
260262-- let
261263-- definition :: Definition '[] Schema
262264-- definition = createTable #tab
263−-- ( (int & null_) `As` #a :*
264−-- (int & null_) `As` #b :* Nil )
265+-- ( (int & hasNull) `As` #a :*
266+-- (int & hasNull) `As` #b :* Nil )
265267-- ( unique (#a :* #b :* Nil) `As` #uq_a_b :* Nil )
266268-- :}
267269--
@@ -292,7 +294,7 @@ unique columns = UnsafeTableConstraintExpression $
292294-- definition :: Definition '[] Schema
293295-- definition = createTable #tab
294296-- ( serial `As` #id :*
295−-- (text & notNull) `As` #name :* Nil )
297+-- (text & hasNotNull) `As` #name :* Nil )
296298-- ( primaryKey #id `As` #pk_id :* Nil )
297299-- :}
298300--
@@ -336,12 +338,12 @@ primaryKey columns = UnsafeTableConstraintExpression $
336338-- setup =
337339-- createTable #users
338340-- ( serial `As` #id :*
339−-- (text & notNull) `As` #name :* Nil )
341+-- (text & hasNotNull) `As` #name :* Nil )
340342-- ( primaryKey #id `As` #pk_users :* Nil ) >>>
341343-- createTable #emails
342344-- ( serial `As` #id :*
343−-- (int & notNull) `As` #user_id :*
344−-- (text & null_) `As` #email :* Nil )
345+-- (int & hasNotNull) `As` #user_id :*
346+-- (text & hasNull) `As` #email :* Nil )
345347-- ( primaryKey #id `As` #pk_emails :*
346348-- foreignKey #user_id #users #id
347349-- OnDeleteCascade OnUpdateCascade `As` #fk_user_id :* Nil )
@@ -370,8 +372,8 @@ primaryKey columns = UnsafeTableConstraintExpression $
370372-- setup =
371373-- createTable #employees
372374-- ( serial `As` #id :*
373−-- (text & notNull) `As` #name :*
374−-- (integer & null_) `As` #employer_id :* Nil )
375+-- (text & hasNotNull) `As` #name :*
376+-- (integer & hasNull) `As` #employer_id :* Nil )
375377-- ( primaryKey #id `As` #employees_pk :*
376378-- foreignKey #employer_id #employees #id
377379-- OnDeleteCascade OnUpdateCascade `As` #employees_employer_fk :* Nil )
@@ -573,7 +575,7 @@ class AddColumn ty where
573575 -- '["tab" ::: 'Table ('[] :=>
574576 -- '[ "col1" ::: 'NoDef :=> 'Null 'PGint4
575577 -- , "col2" ::: 'Def :=> 'Null 'PGtext ])]
576− -- definition = alterTable #tab (addColumn #col2 (text & null_ & default_ "foo"))
578+ -- definition = alterTable #tab (addColumn #col2 (text & hasNull & default_ "foo"))
577579 -- in renderDefinition definition
578580 -- :}
579581 -- "ALTER TABLE \"tab\" ADD COLUMN \"col2\" text NULL DEFAULT E'foo';"
@@ -585,7 +587,7 @@ class AddColumn ty where
585587 -- '["tab" ::: 'Table ('[] :=>
586588 -- '[ "col1" ::: 'NoDef :=> 'Null 'PGint4
587589 -- , "col2" ::: 'NoDef :=> 'Null 'PGtext ])]
588− -- definition = alterTable #tab (addColumn #col2 (text & null_))
590+ -- definition = alterTable #tab (addColumn #col2 (text & hasNull))
589591 -- in renderDefinition definition
590592 -- :}
591593 -- "ALTER TABLE \"tab\" ADD COLUMN \"col2\" text NULL;"
@@ -744,7 +746,7 @@ dropNotNull = UnsafeAlterColumn $ "DROP NOT NULL"
744746-- '["tab" ::: 'Table ('[] :=> '["col" ::: 'NoDef :=> 'NotNull 'PGint4])]
745747-- '["tab" ::: 'Table ('[] :=> '["col" ::: 'NoDef :=> 'NotNull 'PGnumeric])]
746748-- definition =
747−-- alterTable #tab (alterColumn #col (alterType (numeric & notNull)))
749+-- alterTable #tab (alterColumn #col (alterType (numeric & hasNotNull)))
748750-- in renderDefinition definition
749751-- :}
750752-- "ALTER TABLE \"tab\" ALTER COLUMN \"col\" TYPE numeric NOT NULL;"
@@ -824,18 +826,17 @@ instance (Has alias schema ('Typedef ty))
824826 => IsLabel alias (ColumnTypeExpression schema ('NoDef :=> 'NotNull ty)) where
825827 fromLabel = UnsafeColumnTypeExpression (renderAlias (fromLabel @alias))
826828
827−instance HasNull
828− ( TypeExpression ty
829− -> ColumnTypeExpression schema ('NoDef :=> 'Null ty) ) where
830− null_ ty = UnsafeColumnTypeExpression $ renderTypeExpression ty <+> "NULL"
831−
832−instance HasNotNull
833− ( TypeExpression ty
834− ->
835− ColumnTypeExpression schema (def :=> 'NotNull ty) ) where
836− -- | used in `createTable` commands as a column constraint to ensure
837− -- @NULL@ is not present
838− notNull ty = UnsafeColumnTypeExpression $ renderTypeExpression ty <+> "NOT NULL"
829+hasNull
830+ :: TypeExpression ty
831+ -> ColumnTypeExpression schema ('NoDef :=> 'Null ty)
832+hasNull ty = UnsafeColumnTypeExpression $ renderTypeExpression ty <+> "NULL"
833+
834+-- | used in `createTable` commands as a column constraint to ensure
835+-- @NULL@ is not present
836+hasNotNull
837+ :: TypeExpression ty
838+ -> ColumnTypeExpression schema (def :=> 'NotNull ty)
839+hasNotNull ty = UnsafeColumnTypeExpression $ renderTypeExpression ty <+> "NOT NULL"
839840
840841-- | used in `createTable` commands as a column constraint to give a default
841842default_