on dev help sign in
nulls
a646215Eitan Chatav
README.md+3−3
@@ -127,12 +127,12 @@ let
127 setup =
127 setup =
128 createTable #users
128 createTable #users
129 ( serial `As` #id :*
129 ( serial `As` #id :*
130− (text & notNull) `As` #name :* Nil )
130+ (text & hasNotNull) `As` #name :* Nil )
131 ( primaryKey #id `As` #pk_users :* Nil ) >>>
131 ( primaryKey #id `As` #pk_users :* Nil ) >>>
132 createTable #emails
132 createTable #emails
133 ( serial `As` #id :*
133 ( serial `As` #id :*
134− (int & notNull) `As` #user_id :*
134+ (int & hasNotNull) `As` #user_id :*
135− (text & null_) `As` #email :* Nil )
135+ (text & hasNull) `As` #email :* Nil )
136 ( primaryKey #id `As` #pk_emails :*
136 ( primaryKey #id `As` #pk_emails :*
137 foreignKey #user_id #users #id
137 foreignKey #user_id #users #id
138 OnDeleteCascade OnUpdateCascade `As` #fk_user_id :* Nil )
138 OnDeleteCascade OnUpdateCascade `As` #fk_user_id :* Nil )
squeal-postgresql/exe/Example.hs+4−4
@@ -45,14 +45,14 @@ setup :: Definition '[] Schema
45setup =
45setup =
46 createTable #users
46 createTable #users
47 ( serial `As` #id :*
47 ( serial `As` #id :*
48− (text & notNull) `As` #name :*
48+ (text & hasNotNull) `As` #name :*
49− (vararray int2 & notNull) `As` #vec :* Nil )
49+ (vararray int2 & hasNotNull) `As` #vec :* Nil )
50 ( primaryKey #id `As` #pk_users :* Nil )
50 ( primaryKey #id `As` #pk_users :* Nil )
51 >>>
51 >>>
52 createTable #emails
52 createTable #emails
53 ( serial `As` #id :*
53 ( serial `As` #id :*
54− (int & notNull) `As` #user_id :*
54+ (int & hasNotNull) `As` #user_id :*
55− (text & null_) `As` #email :* Nil )
55+ (text & hasNull) `As` #email :* Nil )
56 ( primaryKey #id `As` #pk_emails :*
56 ( primaryKey #id `As` #pk_emails :*
57 foreignKey #user_id #users #id
57 foreignKey #user_id #users #id
58 OnDeleteCascade OnUpdateCascade `As` #fk_user_id :* Nil )
58 OnDeleteCascade OnUpdateCascade `As` #fk_user_id :* Nil )
squeal-postgresql/src/Squeal/PostgreSQL.hs+3−3
@@ -66,12 +66,12 @@
66-- setup =
66-- setup =
67-- createTable #users
67-- createTable #users
68-- ( serial `As` #id :*
68-- ( serial `As` #id :*
69−-- (text & notNull) `As` #name :* Nil )
69+-- (text & hasNotNull) `As` #name :* Nil )
70-- ( primaryKey #id `As` #pk_users :* Nil ) >>>
70-- ( primaryKey #id `As` #pk_users :* Nil ) >>>
71-- createTable #emails
71-- createTable #emails
72-- ( serial `As` #id :*
72-- ( serial `As` #id :*
73−-- (int & notNull) `As` #user_id :*
73+-- (int & hasNotNull) `As` #user_id :*
74−-- (text & null_) `As` #email :* Nil )
74+-- (text & hasNull) `As` #email :* Nil )
75-- ( primaryKey #id `As` #pk_emails :*
75-- ( primaryKey #id `As` #pk_emails :*
76-- foreignKey #user_id #users #id
76-- foreignKey #user_id #users #id
77-- OnDeleteCascade OnUpdateCascade `As` #fk_user_id :* Nil )
77-- OnDeleteCascade OnUpdateCascade `As` #fk_user_id :* Nil )
squeal-postgresql/src/Squeal/PostgreSQL/Definition.hs+28−27
@@ -69,6 +69,8 @@ module Squeal.PostgreSQL.Definition
69 , createTypeEnumFromHaskell
69 , createTypeEnumFromHaskell
70 , dropType
70 , dropType
71 , ColumnTypeExpression (..)
71 , ColumnTypeExpression (..)
72+ , hasNull
73+ , hasNotNull
72 , default_
74 , default_
73 , serial2
75 , serial2
74 , smallserial
76 , smallserial
@@ -120,7 +122,7 @@ CREATE statements
120-- >>> :set -XOverloadedLabels
122-- >>> :set -XOverloadedLabels
121-- >>> :{
123-- >>> :{
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
124-- :}
126-- :}
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]
148-- >>> :{
150-- >>> :{
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)
151-- :}
153-- :}
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
229-- let
231-- let
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 )
235-- :}
237-- :}
236--
238--
@@ -260,8 +262,8 @@ check _cols condition = UnsafeTableConstraintExpression $
260-- let
262-- let
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 )
266-- :}
268-- :}
267--
269--
@@ -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 )
297-- :}
299-- :}
298--
300--
@@ -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
578 -- :}
580 -- :}
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
590 -- :}
592 -- :}
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
749-- :}
751-- :}
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))
826
828
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"
831−
833+
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
834+-- | used in `createTable` commands as a column constraint to ensure
837− -- @NULL@ is not present
835+-- @NULL@ is not present
836+hasNotNull
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"
839
840
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
841default_
842default_
squeal-postgresql/src/Squeal/PostgreSQL/Expression.hs+21−24
@@ -32,12 +32,12 @@ module Squeal.PostgreSQL.Expression
32 Expression (UnsafeExpression, renderExpression)
32 Expression (UnsafeExpression, renderExpression)
33 , HasParameter (param)
33 , HasParameter (param)
34 -- ** Null
34 -- ** Null
35− , HasNull (null_)
35+ , null_
36− , HasNotNull (notNull)
36+ , notNull
37 , coalesce
37 , coalesce
38 , fromNull
38 , fromNull
39 , isNull
39 , isNull
40− , isn'tNull
40+ , isNotNull
41 , matchNull
41 , matchNull
42 , nullIf
42 , nullIf
43 -- ** Arrays, Enums, Composites
43 -- ** Arrays, Enums, Composites
@@ -221,24 +221,21 @@ instance
221 relation ! column = UnsafeExpression $
221 relation ! column = UnsafeExpression $
222 renderAlias relation <> "." <> renderAlias column
222 renderAlias relation <> "." <> renderAlias column
223
223
224−class HasNull expr where null_ :: expr
225−instance HasNull (Expression rels grouping params ('Null ty)) where
226− -- | analagous to `Nothing`
224+-- | analagous to `Nothing`
227− --
225+--
228− -- >>> renderExpression $ null_
226+-- >>> renderExpression $ null_
229− -- "NULL"
227+-- "NULL"
230− null_ = UnsafeExpression "NULL"
231−
232−class HasNotNull expr where notNull :: expr
233−instance HasNotNull
234− ( Expression rels grouping params ('NotNull ty)
228+null_ :: Expression rels grouping params ('Null ty)
235− ->
229+null_ = UnsafeExpression "NULL"
236− Expression rels grouping params ('Null ty) ) where
230+
237− -- | analagous to `Just`
231+-- | analagous to `Just`
238− --
232+--
239− -- >>> renderExpression $ notNull true
233+-- >>> renderExpression $ notNull true
240− -- "TRUE"
234+-- "TRUE"
235+notNull
236+ :: Expression rels grouping params ('NotNull ty)
237+ -> Expression rels grouping params ('Null ty)
241− notNull = UnsafeExpression . renderExpression
238+notNull = UnsafeExpression . renderExpression
242
239
243-- | return the leftmost value which is not NULL
240-- | return the leftmost value which is not NULL
244--
241--
@@ -273,13 +270,13 @@ isNull
273 -> Condition relations grouping params
270 -> Condition relations grouping params
274isNull x = UnsafeExpression $ renderExpression x <+> "IS NULL"
271isNull x = UnsafeExpression $ renderExpression x <+> "IS NULL"
275
272
276−-- | >>> renderExpression $ null_ & isn'tNull
273+-- | >>> renderExpression $ null_ & isNotNull
277-- "NULL IS NOT NULL"
274-- "NULL IS NOT NULL"
278−isn'tNull
275+isNotNull
279 :: Expression relations grouping params ('Null ty)
276 :: Expression relations grouping params ('Null ty)
280 -- ^ possibly @NULL@
277 -- ^ possibly @NULL@
281 -> Condition relations grouping params
278 -> Condition relations grouping params
282−isn'tNull x = UnsafeExpression $ renderExpression x <+> "IS NOT NULL"
279+isNotNull x = UnsafeExpression $ renderExpression x <+> "IS NOT NULL"
283
280
284-- | analagous to `maybe` using @IS NULL@
281-- | analagous to `maybe` using @IS NULL@
285--
282--
squeal-postgresql/src/Squeal/PostgreSQL/Migration.hs+5−5
@@ -37,7 +37,7 @@ let
37 , up = void . define $
37 , up = void . define $
38 createTable #users
38 createTable #users
39 ( serial `As` #id :*
39 ( serial `As` #id :*
40− (text & notNull) `As` #name :* Nil )
40+ (text & hasNotNull) `As` #name :* Nil )
41 ( primaryKey #id `As` #pk_users :* Nil )
41 ( primaryKey #id `As` #pk_users :* Nil )
42 , down = void . define $ dropTable #users
42 , down = void . define $ dropTable #users
43 }
43 }
@@ -52,8 +52,8 @@ let
52 , up = void . define $
52 , up = void . define $
53 createTable #emails
53 createTable #emails
54 ( serial `As` #id :*
54 ( serial `As` #id :*
55− (int & notNull) `As` #user_id :*
55+ (int & hasNotNull) `As` #user_id :*
56− (text & null_) `As` #email :* Nil )
56+ (text & hasNull) `As` #email :* Nil )
57 ( primaryKey #id `As` #pk_emails :*
57 ( primaryKey #id `As` #pk_emails :*
58 foreignKey #user_id #users #id
58 foreignKey #user_id #users #id
59 OnDeleteCascade OnUpdateCascade `As` #fk_user_id :* Nil )
59 OnDeleteCascade OnUpdateCascade `As` #fk_user_id :* Nil )
@@ -285,8 +285,8 @@ createMigrations
285 => Definition schema schema
285 => Definition schema schema
286createMigrations =
286createMigrations =
287 createTableIfNotExists #schema_migrations
287 createTableIfNotExists #schema_migrations
288− ( (text & notNull) `As` #name :*
288+ ( (text & hasNotNull) `As` #name :*
289− (timestampWithTimeZone & notNull & default_ currentTimestamp)
289+ (timestampWithTimeZone & hasNotNull & default_ currentTimestamp)
290 `As` #executed_at :* Nil )
290 `As` #executed_at :* Nil )
291 ( unique (#name :* Nil) `As` #migrations_unique_name :* Nil )
291 ( unique (#name :* Nil) `As` #migrations_unique_name :* Nil )
292
292