on dev help sign in
slay the Nil
commit17fbe57ec21c44290181aaa949aed7f74c58deac authorEitan Chatav <eitan.chatav@gmail.com> dateMon 2 Jul 2018, 16:21 (8 years ago) parent6a45249
3 files changed+44−21
squeal-postgresql/src/Squeal/PostgreSQL/Definition.hs+34−9 squeal-postgresql/src/Squeal/PostgreSQL/Manipulation.hs+1−1 squeal-postgresql/src/Squeal/PostgreSQL/Schema.hs+9−11
squeal-postgresql/src/Squeal/PostgreSQL/Definition.hs+34−9
@@ -129,8 +129,17 @@ CREATE statements
129129
130130>>> :set -XOverloadedLabels
131131>>> :{
132−printSQL $
133− createTable #tab ((int & nullable) `As` #a :* (real & nullable) `As` #b :* Nil) Nil
132+type Table = '[] :=>
133+ '[ "a" ::: 'NoDef :=> 'Null 'PGint4
134+ , "b" ::: 'NoDef :=> 'Null 'PGfloat4 ]
135+:}
136+
137+>>> :{
138+let
139+ setup :: Definition '[] '["tab" ::: 'Table Table]
140+ setup = createTable #tab
141+ (nullable int `as` #a :* nullable real `as` #b) Nil
142+in printSQL setup
134143:}
135144CREATE TABLE "tab" ("a" int NULL, "b" real NULL);
136145-}
@@ -155,12 +164,19 @@ Instead, the schema already has the table so if the table did not yet exist, the
155164the `Category` of `Definition`s.
156165
157166>>> :set -XOverloadedLabels -XTypeApplications
158−>>> type Table = '[] :=> '["a" ::: 'NoDef :=> 'Null 'PGint4, "b" ::: 'NoDef :=> 'Null 'PGfloat4]
167+>>> :{
168+type Table = '[] :=>
169+ '[ "a" ::: 'NoDef :=> 'Null 'PGint4
170+ , "b" ::: 'NoDef :=> 'Null 'PGfloat4 ]
171+:}
172+
159173>>> type Schema = '["tab" ::: 'Table Table]
174+
160175>>> :{
161176let
162177 setup :: Definition Schema Schema
163− setup = createTableIfNotExists #tab ((int & nullable) `As` #a :* (real & nullable) `As` #b :* Nil) Nil
178+ setup = createTableIfNotExists #tab
179+ (nullable int `as` #a :* nullable real `as` #b) Nil
164180in printSQL setup
165181:}
166182CREATE TABLE IF NOT EXISTS "tab" ("a" int NULL, "b" real NULL);
@@ -850,11 +866,20 @@ createTypeEnumFrom
850866createTypeEnumFrom enum = createTypeEnum enum
851867 (SOP.hpure label :: NP PGlabel (LabelsFrom hask))
852868
853−-- | `createTypeComposite` creates a composite type. The composite type is
854−-- specified by a list of attribute names and data types.
855−--
856−-- >>> printSQL $ createTypeComposite #complex (float8 `As` #real :* float8 `As` #imaginary :* Nil)
857−-- CREATE TYPE "complex" AS ("real" float8, "imaginary" float8);
869+{- | `createTypeComposite` creates a composite type. The composite type is
870+specified by a list of attribute names and data types.
871+
872+>>> type PGcomplex = 'PGcomposite '["real" ::: 'PGfloat8, "imaginary" ::: 'PGfloat8]
873+
874+>>> :{
875+let
876+ setup :: Definition '[] '["complex" ::: 'Typedef PGcomplex]
877+ setup = createTypeComposite #complex
878+ (float8 `as` #real :* float8 `as` #imaginary)
879+in printSQL setup
880+:}
881+CREATE TYPE "complex" AS ("real" float8, "imaginary" float8);
882+-}
858883createTypeComposite
859884 :: (KnownSymbol ty, SOP.SListI fields)
860885 => Alias ty
squeal-postgresql/src/Squeal/PostgreSQL/Manipulation.hs+1−1
@@ -484,7 +484,7 @@ WITH statements
484484-- let
485485-- manipulation :: Manipulation '["products" ::: 'Table ProductsTable, "products_deleted" ::: 'Table ProductsTable] '[ 'NotNull 'PGdate] '[]
486486-- manipulation = with
487−-- (deleteFrom #products (#date .< param @1) ReturningStar `As` #deleted_rows :* Nil)
487+-- (deleteFrom #products (#date .< param @1) ReturningStar `as` #deleted_rows)
488488-- (insertQuery_ #products_deleted (selectStar (from (view (#deleted_rows `as` #t)))))
489489-- in printSQL manipulation
490490-- :}
squeal-postgresql/src/Squeal/PostgreSQL/Schema.hs+9−11
@@ -374,7 +374,7 @@ renderAlias :: KnownSymbol alias => Alias alias -> ByteString
374374renderAlias = doubleQuoted . fromString . symbolVal
375375
376376-- | >>> import Generics.SOP (NP(..))
377−-- >>> renderAliases (#jimbob :* #kandi :* Nil)
377+-- >>> renderAliases (#jimbob :* #kandi)
378378-- ["\"jimbob\"","\"kandi\""]
379379renderAliases
380380 :: All KnownSymbol aliases => NP Alias aliases -> [ByteString]
@@ -402,21 +402,19 @@ instance (alias0 ~ alias1, alias0 ~ alias2, KnownSymbol alias2)
402402 => IsLabel alias0 (Aliased Alias (alias1 ::: alias2)) where
403403 fromLabel = fromLabel @alias2 `As` fromLabel @alias1
404404
405−class KnownSymbol alias => Aliasable expression alias aliased
405+class KnownSymbol alias => Aliasable alias expression aliased
406406 | aliased -> expression
407407 , aliased -> alias
408− where
409− as :: expression -> Alias alias -> aliased
408+ where as :: expression -> Alias alias -> aliased
410409
411−instance KnownSymbol alias => Aliasable
410+instance (alias ~ alias1, KnownSymbol alias) => Aliasable alias
412411 (expression ty)
413− alias
414− (Aliased expression (alias ::: ty))
415− where as = As
416−instance KnownSymbol alias => Aliasable
412+ (Aliased expression (alias1 ::: ty))
413+ where
414+ as = As
415+instance (KnownSymbol alias, tys ~ '[alias ::: ty]) => Aliasable alias
417416 (expression ty)
418− alias
419− (NP (Aliased expression) '[alias ::: ty])
417+ (NP (Aliased expression) tys)
420418 where
421419 expression `as` alias = expression `As` alias :* Nil
422420