on dev help sign in
slay the Nil
17fbe57Eitan Chatav
squeal-postgresql/src/Squeal/PostgreSQL/Definition.hs+34−9
@@ -129,8 +129,17 @@ CREATE statements
129
129
130>>> :set -XOverloadedLabels
130>>> :set -XOverloadedLabels
131>>> :{
131>>> :{
132−printSQL $
132+type Table = '[] :=>
133+ '[ "a" ::: 'NoDef :=> 'Null 'PGint4
134+ , "b" ::: 'NoDef :=> 'Null 'PGfloat4 ]
135+:}
136+
137+>>> :{
138+let
139+ setup :: Definition '[] '["tab" ::: 'Table Table]
133− createTable #tab ((int & nullable) `As` #a :* (real & nullable) `As` #b :* Nil) Nil
140+ setup = createTable #tab
141+ (nullable int `as` #a :* nullable real `as` #b) Nil
142+in printSQL setup
134:}
143:}
135CREATE TABLE "tab" ("a" int NULL, "b" real NULL);
144CREATE TABLE "tab" ("a" int NULL, "b" real NULL);
136-}
145-}
@@ -155,12 +164,19 @@ Instead, the schema already has the table so if the table did not yet exist, the
155the `Category` of `Definition`s.
164the `Category` of `Definition`s.
156
165
157>>> :set -XOverloadedLabels -XTypeApplications
166>>> :set -XOverloadedLabels -XTypeApplications
167+>>> :{
168+type Table = '[] :=>
169+ '[ "a" ::: 'NoDef :=> 'Null 'PGint4
158−>>> type Table = '[] :=> '["a" ::: 'NoDef :=> 'Null 'PGint4, "b" ::: 'NoDef :=> 'Null 'PGfloat4]
170+ , "b" ::: 'NoDef :=> 'Null 'PGfloat4 ]
171+:}
172+
159>>> type Schema = '["tab" ::: 'Table Table]
173>>> type Schema = '["tab" ::: 'Table Table]
174+
160>>> :{
175>>> :{
161let
176let
162 setup :: Definition Schema Schema
177 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
164in printSQL setup
180in printSQL setup
165:}
181:}
166CREATE TABLE IF NOT EXISTS "tab" ("a" int NULL, "b" real NULL);
182CREATE TABLE IF NOT EXISTS "tab" ("a" int NULL, "b" real NULL);
@@ -850,11 +866,20 @@ createTypeEnumFrom
850createTypeEnumFrom enum = createTypeEnum enum
866createTypeEnumFrom enum = createTypeEnum enum
851 (SOP.hpure label :: NP PGlabel (LabelsFrom hask))
867 (SOP.hpure label :: NP PGlabel (LabelsFrom hask))
852
868
853−-- | `createTypeComposite` creates a composite type. The composite type is
869+{- | `createTypeComposite` creates a composite type. The composite type is
854−-- specified by a list of attribute names and data types.
870+specified by a list of attribute names and data types.
855−--
871+
856−-- >>> printSQL $ createTypeComposite #complex (float8 `As` #real :* float8 `As` #imaginary :* Nil)
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+:}
857−-- CREATE TYPE "complex" AS ("real" float8, "imaginary" float8);
881+CREATE TYPE "complex" AS ("real" float8, "imaginary" float8);
882+-}
858createTypeComposite
883createTypeComposite
859 :: (KnownSymbol ty, SOP.SListI fields)
884 :: (KnownSymbol ty, SOP.SListI fields)
860 => Alias ty
885 => Alias ty
squeal-postgresql/src/Squeal/PostgreSQL/Manipulation.hs+1−1
@@ -484,7 +484,7 @@ WITH statements
484-- let
484-- let
485-- manipulation :: Manipulation '["products" ::: 'Table ProductsTable, "products_deleted" ::: 'Table ProductsTable] '[ 'NotNull 'PGdate] '[]
485-- manipulation :: Manipulation '["products" ::: 'Table ProductsTable, "products_deleted" ::: 'Table ProductsTable] '[ 'NotNull 'PGdate] '[]
486-- manipulation = with
486-- manipulation = with
487−-- (deleteFrom #products (#date .< param @1) ReturningStar `As` #deleted_rows :* Nil)
487+-- (deleteFrom #products (#date .< param @1) ReturningStar `as` #deleted_rows)
488-- (insertQuery_ #products_deleted (selectStar (from (view (#deleted_rows `as` #t)))))
488-- (insertQuery_ #products_deleted (selectStar (from (view (#deleted_rows `as` #t)))))
489-- in printSQL manipulation
489-- in printSQL manipulation
490-- :}
490-- :}
squeal-postgresql/src/Squeal/PostgreSQL/Schema.hs+9−11
@@ -374,7 +374,7 @@ renderAlias :: KnownSymbol alias => Alias alias -> ByteString
374renderAlias = doubleQuoted . fromString . symbolVal
374renderAlias = doubleQuoted . fromString . symbolVal
375
375
376-- | >>> import Generics.SOP (NP(..))
376-- | >>> import Generics.SOP (NP(..))
377−-- >>> renderAliases (#jimbob :* #kandi :* Nil)
377+-- >>> renderAliases (#jimbob :* #kandi)
378-- ["\"jimbob\"","\"kandi\""]
378-- ["\"jimbob\"","\"kandi\""]
379renderAliases
379renderAliases
380 :: All KnownSymbol aliases => NP Alias aliases -> [ByteString]
380 :: All KnownSymbol aliases => NP Alias aliases -> [ByteString]
@@ -402,21 +402,19 @@ instance (alias0 ~ alias1, alias0 ~ alias2, KnownSymbol alias2)
402 => IsLabel alias0 (Aliased Alias (alias1 ::: alias2)) where
402 => IsLabel alias0 (Aliased Alias (alias1 ::: alias2)) where
403 fromLabel = fromLabel @alias2 `As` fromLabel @alias1
403 fromLabel = fromLabel @alias2 `As` fromLabel @alias1
404
404
405−class KnownSymbol alias => Aliasable expression alias aliased
405+class KnownSymbol alias => Aliasable alias expression aliased
406 | aliased -> expression
406 | aliased -> expression
407 , aliased -> alias
407 , aliased -> alias
408− where
409− as :: expression -> Alias alias -> aliased
408+ where as :: expression -> Alias alias -> aliased
410
409
411−instance KnownSymbol alias => Aliasable
410+instance (alias ~ alias1, KnownSymbol alias) => Aliasable alias
412 (expression ty)
411 (expression ty)
413− alias
414− (Aliased expression (alias ::: ty))
412+ (Aliased expression (alias1 ::: ty))
415− where as = As
413+ where
414+ as = As
416−instance KnownSymbol alias => Aliasable
415+instance (KnownSymbol alias, tys ~ '[alias ::: ty]) => Aliasable alias
417 (expression ty)
416 (expression ty)
418− alias
419− (NP (Aliased expression) '[alias ::: ty])
417+ (NP (Aliased expression) tys)
420 where
418 where
421 expression `as` alias = expression `As` alias :* Nil
419 expression `as` alias = expression `As` alias :* Nil
422
420