on dev help sign in
nulls
commita646215c894d06120fee35ede915fad5fa0bf5c2 authorEitan Chatav <eitan.chatav@gmail.com> dateFri 18 May 2018, 21:11 (8 years ago) parent0df15ea
6 files changed+64−66
README.md+3−3 squeal-postgresql/exe/Example.hs+4−4 squeal-postgresql/src/Squeal/PostgreSQL.hs+3−3 squeal-postgresql/src/Squeal/PostgreSQL/Definition.hs+28−27 squeal-postgresql/src/Squeal/PostgreSQL/Expression.hs+21−24 squeal-postgresql/src/Squeal/PostgreSQL/Migration.hs+5−5
README.md+3−3
@@ -127,12 +127,12 @@ let
127127 setup =
128128 createTable #users
129129 ( serial `As` #id :*
130− (text & notNull) `As` #name :* Nil )
130+ (text & hasNotNull) `As` #name :* Nil )
131131 ( primaryKey #id `As` #pk_users :* Nil ) >>>
132132 createTable #emails
133133 ( serial `As` #id :*
134− (int & notNull) `As` #user_id :*
135− (text & null_) `As` #email :* Nil )
134+ (int & hasNotNull) `As` #user_id :*
135+ (text & hasNull) `As` #email :* Nil )
136136 ( primaryKey #id `As` #pk_emails :*
137137 foreignKey #user_id #users #id
138138 OnDeleteCascade OnUpdateCascade `As` #fk_user_id :* Nil )
squeal-postgresql/exe/Example.hs+4−4
@@ -45,14 +45,14 @@ setup :: Definition '[] Schema
4545setup =
4646 createTable #users
4747 ( serial `As` #id :*
48− (text & notNull) `As` #name :*
49− (vararray int2 & notNull) `As` #vec :* Nil )
48+ (text & hasNotNull) `As` #name :*
49+ (vararray int2 & hasNotNull) `As` #vec :* Nil )
5050 ( primaryKey #id `As` #pk_users :* Nil )
5151 >>>
5252 createTable #emails
5353 ( serial `As` #id :*
54− (int & notNull) `As` #user_id :*
55− (text & null_) `As` #email :* Nil )
54+ (int & hasNotNull) `As` #user_id :*
55+ (text & hasNull) `As` #email :* Nil )
5656 ( primaryKey #id `As` #pk_emails :*
5757 foreignKey #user_id #users #id
5858 OnDeleteCascade OnUpdateCascade `As` #fk_user_id :* Nil )
squeal-postgresql/src/Squeal/PostgreSQL.hs+3−3
@@ -66,12 +66,12 @@
6666-- setup =
6767-- createTable #users
6868-- ( serial `As` #id :*
69−-- (text & notNull) `As` #name :* Nil )
69+-- (text & hasNotNull) `As` #name :* Nil )
7070-- ( primaryKey #id `As` #pk_users :* Nil ) >>>
7171-- createTable #emails
7272-- ( serial `As` #id :*
73−-- (int & notNull) `As` #user_id :*
74−-- (text & null_) `As` #email :* Nil )
73+-- (int & hasNotNull) `As` #user_id :*
74+-- (text & hasNull) `As` #email :* Nil )
7575-- ( primaryKey #id `As` #pk_emails :*
7676-- foreignKey #user_id #users #id
7777-- OnDeleteCascade OnUpdateCascade `As` #fk_user_id :* Nil )
squeal-postgresql/src/Squeal/PostgreSQL/Definition.hs+28−27
@@ -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_
squeal-postgresql/src/Squeal/PostgreSQL/Expression.hs+21−24
@@ -32,12 +32,12 @@ module Squeal.PostgreSQL.Expression
3232 Expression (UnsafeExpression, renderExpression)
3333 , HasParameter (param)
3434 -- ** Null
35− , HasNull (null_)
36− , HasNotNull (notNull)
35+ , null_
36+ , notNull
3737 , coalesce
3838 , fromNull
3939 , isNull
40− , isn'tNull
40+ , isNotNull
4141 , matchNull
4242 , nullIf
4343 -- ** Arrays, Enums, Composites
@@ -221,24 +221,21 @@ instance
221221 relation ! column = UnsafeExpression $
222222 renderAlias relation <> "." <> renderAlias column
223223
224−class HasNull expr where null_ :: expr
225−instance HasNull (Expression rels grouping params ('Null ty)) where
226− -- | analagous to `Nothing`
227− --
228− -- >>> renderExpression $ null_
229− -- "NULL"
230− null_ = UnsafeExpression "NULL"
231−
232−class HasNotNull expr where notNull :: expr
233−instance HasNotNull
234− ( Expression rels grouping params ('NotNull ty)
235− ->
236− Expression rels grouping params ('Null ty) ) where
237− -- | analagous to `Just`
238− --
239− -- >>> renderExpression $ notNull true
240− -- "TRUE"
241− notNull = UnsafeExpression . renderExpression
224+-- | analagous to `Nothing`
225+--
226+-- >>> renderExpression $ null_
227+-- "NULL"
228+null_ :: Expression rels grouping params ('Null ty)
229+null_ = UnsafeExpression "NULL"
230+
231+-- | analagous to `Just`
232+--
233+-- >>> renderExpression $ notNull true
234+-- "TRUE"
235+notNull
236+ :: Expression rels grouping params ('NotNull ty)
237+ -> Expression rels grouping params ('Null ty)
238+notNull = UnsafeExpression . renderExpression
242239
243240-- | return the leftmost value which is not NULL
244241--
@@ -273,13 +270,13 @@ isNull
273270 -> Condition relations grouping params
274271isNull x = UnsafeExpression $ renderExpression x <+> "IS NULL"
275272
276−-- | >>> renderExpression $ null_ & isn'tNull
273+-- | >>> renderExpression $ null_ & isNotNull
277274-- "NULL IS NOT NULL"
278−isn'tNull
275+isNotNull
279276 :: Expression relations grouping params ('Null ty)
280277 -- ^ possibly @NULL@
281278 -> Condition relations grouping params
282−isn'tNull x = UnsafeExpression $ renderExpression x <+> "IS NOT NULL"
279+isNotNull x = UnsafeExpression $ renderExpression x <+> "IS NOT NULL"
283280
284281-- | analagous to `maybe` using @IS NULL@
285282--
squeal-postgresql/src/Squeal/PostgreSQL/Migration.hs+5−5
@@ -37,7 +37,7 @@ let
3737 , up = void . define $
3838 createTable #users
3939 ( serial `As` #id :*
40− (text & notNull) `As` #name :* Nil )
40+ (text & hasNotNull) `As` #name :* Nil )
4141 ( primaryKey #id `As` #pk_users :* Nil )
4242 , down = void . define $ dropTable #users
4343 }
@@ -52,8 +52,8 @@ let
5252 , up = void . define $
5353 createTable #emails
5454 ( serial `As` #id :*
55− (int & notNull) `As` #user_id :*
56− (text & null_) `As` #email :* Nil )
55+ (int & hasNotNull) `As` #user_id :*
56+ (text & hasNull) `As` #email :* Nil )
5757 ( primaryKey #id `As` #pk_emails :*
5858 foreignKey #user_id #users #id
5959 OnDeleteCascade OnUpdateCascade `As` #fk_user_id :* Nil )
@@ -285,8 +285,8 @@ createMigrations
285285 => Definition schema schema
286286createMigrations =
287287 createTableIfNotExists #schema_migrations
288− ( (text & notNull) `As` #name :*
289− (timestampWithTimeZone & notNull & default_ currentTimestamp)
288+ ( (text & hasNotNull) `As` #name :*
289+ (timestampWithTimeZone & hasNotNull & default_ currentTimestamp)
290290 `As` #executed_at :* Nil )
291291 ( unique (#name :* Nil) `As` #migrations_unique_name :* Nil )
292292