on dev help sign in
null_, notNull and values
commitc08a8a6a2433f36fa39ced0aa2901c0213151f5f authorEitan Chatav <eitan.chatav@gmail.com> dateFri 18 May 2018, 14:32 (8 years ago) parent7018d90
7 files changed+72−51
README.md+1−1 squeal-postgresql/exe/Example.hs+1−1 squeal-postgresql/src/Squeal/PostgreSQL.hs+1−1 squeal-postgresql/src/Squeal/PostgreSQL/Definition.hs+20−21 squeal-postgresql/src/Squeal/PostgreSQL/Expression.hs+28−26 squeal-postgresql/src/Squeal/PostgreSQL/Migration.hs+1−1 squeal-postgresql/src/Squeal/PostgreSQL/Query.hs+20
README.md+1−1
@@ -132,7 +132,7 @@ let
132132 createTable #emails
133133 ( serial `As` #id :*
134134 (int & notNull) `As` #user_id :*
135− (text & null') `As` #email :* Nil )
135+ (text & null_) `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+1−1
@@ -52,7 +52,7 @@ setup =
5252 createTable #emails
5353 ( serial `As` #id :*
5454 (int & notNull) `As` #user_id :*
55− (text & null') `As` #email :* Nil )
55+ (text & null_) `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+1−1
@@ -71,7 +71,7 @@
7171-- createTable #emails
7272-- ( serial `As` #id :*
7373-- (int & notNull) `As` #user_id :*
74−-- (text & null') `As` #email :* Nil )
74+-- (text & null_) `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+20−21
@@ -69,8 +69,6 @@ module Squeal.PostgreSQL.Definition
6969 , createTypeEnumFromHaskell
7070 , dropType
7171 , ColumnTypeExpression (..)
72− , notNull
73− , null'
7472 , default_
7573 , serial2
7674 , smallserial
@@ -122,7 +120,7 @@ CREATE statements
122120-- >>> :set -XOverloadedLabels
123121-- >>> :{
124122-- renderDefinition $
125−-- createTable #tab ((int & null') `As` #a :* (real & null') `As` #b :* Nil) Nil
123+-- createTable #tab ((int & null_) `As` #a :* (real & null_) `As` #b :* Nil) Nil
126124-- :}
127125-- "CREATE TABLE \"tab\" (\"a\" int NULL, \"b\" real NULL);"
128126createTable
@@ -149,7 +147,7 @@ createTable tab columns constraints = UnsafeDefinition $
149147-- >>> type Schema = '["tab" ::: 'Table Table]
150148-- >>> :{
151149-- renderDefinition
152−-- (createTableIfNotExists #tab ((int & null') `As` #a :* (real & null') `As` #b :* Nil) Nil :: Definition Schema Schema)
150+-- (createTableIfNotExists #tab ((int & null_) `As` #a :* (real & null_) `As` #b :* Nil) Nil :: Definition Schema Schema)
153151-- :}
154152-- "CREATE TABLE IF NOT EXISTS \"tab\" (\"a\" int NULL, \"b\" real NULL);"
155153createTableIfNotExists
@@ -262,8 +260,8 @@ check _cols condition = UnsafeTableConstraintExpression $
262260-- let
263261-- definition :: Definition '[] Schema
264262-- definition = createTable #tab
265−-- ( (int & null') `As` #a :*
266−-- (int & null') `As` #b :* Nil )
263+-- ( (int & null_) `As` #a :*
264+-- (int & null_) `As` #b :* Nil )
267265-- ( unique (#a :* #b :* Nil) `As` #uq_a_b :* Nil )
268266-- :}
269267--
@@ -343,7 +341,7 @@ primaryKey columns = UnsafeTableConstraintExpression $
343341-- createTable #emails
344342-- ( serial `As` #id :*
345343-- (int & notNull) `As` #user_id :*
346−-- (text & null') `As` #email :* Nil )
344+-- (text & null_) `As` #email :* Nil )
347345-- ( primaryKey #id `As` #pk_emails :*
348346-- foreignKey #user_id #users #id
349347-- OnDeleteCascade OnUpdateCascade `As` #fk_user_id :* Nil )
@@ -373,7 +371,7 @@ primaryKey columns = UnsafeTableConstraintExpression $
373371-- createTable #employees
374372-- ( serial `As` #id :*
375373-- (text & notNull) `As` #name :*
376−-- (integer & null') `As` #employer_id :* Nil )
374+-- (integer & null_) `As` #employer_id :* Nil )
377375-- ( primaryKey #id `As` #employees_pk :*
378376-- foreignKey #employer_id #employees #id
379377-- OnDeleteCascade OnUpdateCascade `As` #employees_employer_fk :* Nil )
@@ -575,7 +573,7 @@ class AddColumn ty where
575573 -- '["tab" ::: 'Table ('[] :=>
576574 -- '[ "col1" ::: 'NoDef :=> 'Null 'PGint4
577575 -- , "col2" ::: 'Def :=> 'Null 'PGtext ])]
578− -- definition = alterTable #tab (addColumn #col2 (text & null' & default_ "foo"))
576+ -- definition = alterTable #tab (addColumn #col2 (text & null_ & default_ "foo"))
579577 -- in renderDefinition definition
580578 -- :}
581579 -- "ALTER TABLE \"tab\" ADD COLUMN \"col2\" text NULL DEFAULT E'foo';"
@@ -587,7 +585,7 @@ class AddColumn ty where
587585 -- '["tab" ::: 'Table ('[] :=>
588586 -- '[ "col1" ::: 'NoDef :=> 'Null 'PGint4
589587 -- , "col2" ::: 'NoDef :=> 'Null 'PGtext ])]
590− -- definition = alterTable #tab (addColumn #col2 (text & null'))
588+ -- definition = alterTable #tab (addColumn #col2 (text & null_))
591589 -- in renderDefinition definition
592590 -- :}
593591 -- "ALTER TABLE \"tab\" ADD COLUMN \"col2\" text NULL;"
@@ -826,17 +824,18 @@ instance (Has alias schema ('Typedef ty))
826824 => IsLabel alias (ColumnTypeExpression schema ('NoDef :=> 'NotNull ty)) where
827825 fromLabel = UnsafeColumnTypeExpression (renderAlias (fromLabel @alias))
828826
829−null'
830− :: TypeExpression ty
831− -> ColumnTypeExpression schema ('NoDef :=> 'Null ty)
832−null' ty = UnsafeColumnTypeExpression $ renderTypeExpression ty <+> "NULL"
833−
834−-- | used in `createTable` commands as a column constraint to ensure
835−-- @NULL@ is not present
836−notNull
837− :: TypeExpression ty
838− -> ColumnTypeExpression schema (def :=> 'NotNull ty)
839−notNull ty = UnsafeColumnTypeExpression $ renderTypeExpression ty <+> "NOT NULL"
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"
840839
841840-- | used in `createTable` commands as a column constraint to give a default
842841default_
squeal-postgresql/src/Squeal/PostgreSQL/Expression.hs+28−26
@@ -32,8 +32,8 @@ module Squeal.PostgreSQL.Expression
3232 Expression (UnsafeExpression, renderExpression)
3333 , HasParameter (param)
3434 -- ** Null
35− , null_
36− , unNull
35+ , HasNull (null_)
36+ , HasNotNull (notNull)
3737 , coalesce
3838 , fromNull
3939 , isNull
@@ -221,26 +221,28 @@ instance
221221 relation ! column = UnsafeExpression $
222222 renderAlias relation <> "." <> renderAlias column
223223
224−-- | analagous to `Nothing`
225−--
226−-- >>> renderExpression $ null_
227−-- "NULL"
228−null_ :: Expression relations grouping params ('Null ty)
229−null_ = UnsafeExpression "NULL"
230−
231−-- | analagous to `Just`
232−--
233−-- >>> renderExpression $ unNull true
234−-- "TRUE"
235−unNull
236− :: Expression relations grouping params ('NotNull ty)
237− -- ^ not @NULL@
238− -> Expression relations grouping params ('Null ty)
239−unNull = UnsafeExpression . renderExpression
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
240242
241243-- | return the leftmost value which is not NULL
242244--
243−-- >>> renderExpression $ coalesce [null_, unNull true] false
245+-- >>> renderExpression $ coalesce [null_, notNull true] false
244246-- "COALESCE(NULL, TRUE, FALSE)"
245247coalesce
246248 :: [Expression relations grouping params ('Null ty)]
@@ -309,7 +311,7 @@ nullIf
309311nullIf x y = UnsafeExpression $ "NULL IF" <+> parenthesized
310312 (renderExpression x <> ", " <> renderExpression y)
311313
312−-- | >>> renderExpression $ array [null_, unNull false, unNull true]
314+-- | >>> renderExpression $ array [null_, notNull false, notNull true]
313315-- "ARRAY[NULL, FALSE, TRUE]"
314316array
315317 :: [Expression relations grouping params ('Null ty)]
@@ -628,7 +630,7 @@ ifThenElse if_ then_ else_ = caseWhenThenElse [(if_,then_)] else_
628630-- | Comparison operations like `.==`, `./=`, `.>`, `.>=`, `.<` and `.<=`
629631-- will produce @NULL@s if one of their arguments is @NULL@.
630632--
631−-- >>> renderExpression $ unNull true .== null_
633+-- >>> renderExpression $ notNull true .== null_
632634-- "(TRUE = NULL)"
633635(.==)
634636 :: Expression relations grouping params (nullity ty) -- ^ lhs
@@ -637,7 +639,7 @@ ifThenElse if_ then_ else_ = caseWhenThenElse [(if_,then_)] else_
637639(.==) = unsafeBinaryOp "="
638640infix 4 .==
639641
640−-- | >>> renderExpression $ unNull true ./= null_
642+-- | >>> renderExpression $ notNull true ./= null_
641643-- "(TRUE <> NULL)"
642644(./=)
643645 :: Expression relations grouping params (nullity ty) -- ^ lhs
@@ -646,7 +648,7 @@ infix 4 .==
646648(./=) = unsafeBinaryOp "<>"
647649infix 4 ./=
648650
649−-- | >>> renderExpression $ unNull true .>= null_
651+-- | >>> renderExpression $ notNull true .>= null_
650652-- "(TRUE >= NULL)"
651653(.>=)
652654 :: Expression relations grouping params (nullity ty) -- ^ lhs
@@ -655,7 +657,7 @@ infix 4 ./=
655657(.>=) = unsafeBinaryOp ">="
656658infix 4 .>=
657659
658−-- | >>> renderExpression $ unNull true .< null_
660+-- | >>> renderExpression $ notNull true .< null_
659661-- "(TRUE < NULL)"
660662(.<)
661663 :: Expression relations grouping params (nullity ty) -- ^ lhs
@@ -664,7 +666,7 @@ infix 4 .>=
664666(.<) = unsafeBinaryOp "<"
665667infix 4 .<
666668
667−-- | >>> renderExpression $ unNull true .<= null_
669+-- | >>> renderExpression $ notNull true .<= null_
668670-- "(TRUE <= NULL)"
669671(.<=)
670672 :: Expression relations grouping params (nullity ty) -- ^ lhs
@@ -673,7 +675,7 @@ infix 4 .<
673675(.<=) = unsafeBinaryOp "<="
674676infix 4 .<=
675677
676−-- | >>> renderExpression $ unNull true .> null_
678+-- | >>> renderExpression $ notNull true .> null_
677679-- "(TRUE > NULL)"
678680(.>)
679681 :: Expression relations grouping params (nullity ty) -- ^ lhs
squeal-postgresql/src/Squeal/PostgreSQL/Migration.hs+1−1
@@ -53,7 +53,7 @@ let
5353 createTable #emails
5454 ( serial `As` #id :*
5555 (int & notNull) `As` #user_id :*
56− (text & null') `As` #email :* Nil )
56+ (text & null_) `As` #email :* Nil )
5757 ( primaryKey #id `As` #pk_emails :*
5858 foreignKey #user_id #users #id
5959 OnDeleteCascade OnUpdateCascade `As` #fk_user_id :* Nil )
squeal-postgresql/src/Squeal/PostgreSQL/Query.hs+20
@@ -35,6 +35,7 @@ module Squeal.PostgreSQL.Query
3535 , selectDistinctStar
3636 , selectDotStar
3737 , selectDistinctDotStar
38+ , values
3839 -- * Table Expressions
3940 , TableExpression (..)
4041 , renderTableExpression
@@ -424,6 +425,25 @@ selectDistinctDotStar rel relations = UnsafeQuery $
424425 "SELECT DISTINCT" <+> renderAlias rel <> ".*"
425426 <+> renderTableExpression relations
426427
428+values
429+ :: SListI cols
430+ => NP (Aliased (Expression '[] 'Ungrouped params)) cols
431+ -> [NP (Aliased (Expression '[] 'Ungrouped params)) cols]
432+ -> Query schema params cols
433+values rw rws = UnsafeQuery $ "SELECT * FROM"
434+ <+> parenthesized (
435+ "VALUES"
436+ <+> commaSeparated
437+ ( parenthesized
438+ . renderCommaSeparated renderValuePart <$> rw:rws )
439+ ) <+> "AS t"
440+ <+> parenthesized (renderCommaSeparated renderAliasPart rw)
441+ where
442+ renderAliasPart, renderValuePart
443+ :: Aliased (Expression '[] 'Ungrouped params) ty -> ByteString
444+ renderAliasPart (_ `As` name) = renderAlias name
445+ renderValuePart (value `As` _) = renderExpression value
446+
427447{-----------------------------------------
428448Table Expressions
429449-----------------------------------------}