squeal-postgresql 0.5.0.0 → 0.5.1.0
raw patch · 7 files changed
+42/−18 lines, 7 filesdep ~generics-sop
Dependency ranges changed: generics-sop
Files
- squeal-postgresql.cabal +2/−2
- src/Squeal/PostgreSQL/Expression/Aggregate.hs +18/−3
- src/Squeal/PostgreSQL/Expression/Type.hs +12/−0
- src/Squeal/PostgreSQL/Manipulation.hs +1/−2
- src/Squeal/PostgreSQL/PG.hs +0/−1
- src/Squeal/PostgreSQL/Query.hs +8/−9
- test/Specs/Specs.hs +1/−1
squeal-postgresql.cabal view
@@ -1,5 +1,5 @@ name: squeal-postgresql-version: 0.5.0.0+version: 0.5.1.0 synopsis: Squeal PostgreSQL Library description: Squeal is a type-safe embedding of PostgreSQL in Haskell homepage: https://github.com/morphismtech/squeal@@ -62,7 +62,7 @@ , bytestring >= 0.10.8.2 , bytestring-strict-builder >= 0.4.5 , deepseq >= 1.4.3.0- , generics-sop >= 0.3.2.0+ , generics-sop >= 0.3.2.0 && < 0.5.0.0 , mmorph >= 1.1.1 , mtl >= 2.2.2 , network-ip >= 0.3.0.2
src/Squeal/PostgreSQL/Expression/Aggregate.hs view
@@ -26,6 +26,7 @@ module Squeal.PostgreSQL.Expression.Aggregate ( Aggregate (..) , Distinction (..)+ , PGSum , PGAvg ) where @@ -85,9 +86,8 @@ -- :} -- sum(DISTINCT "col") sum_- :: ty `In` PGNum- => expr1 (null ty)- -> aggr ('Null ty)+ :: expr1 (null ty)+ -> aggr ('Null (PGSum ty)) -- | input values, including nulls, concatenated into an array arrayAgg@@ -433,6 +433,21 @@ -> Distinction (NP (Expression outer commons 'Ungrouped schemas params from)) xs -> Expression outer commons ('Grouped bys) schemas params from y unsafeAggregateN fun xs = UnsafeExpression $ fun <> parenthesized (renderSQL xs)++-- | A type family that calculates `PGSum``PGType` of+-- a given argument `PGType`.+type family PGSum ty where+ PGSum 'PGint2 = 'PGint8+ PGSum 'PGint4 = 'PGint8+ PGSum 'PGint8 = 'PGnumeric+ PGSum 'PGfloat4 = 'PGfloat4+ PGSum 'PGfloat8 = 'PGfloat8+ PGSum 'PGnumeric = 'PGnumeric+ PGSum 'PGinterval = 'PGinterval+ PGSum 'PGmoney = 'PGmoney+ PGSum pg = TypeError+ ( 'Text "Squeal type error: Cannot sum with argument type "+ ':<>: 'ShowType pg ) -- | A type family that calculates `PGAvg` type of a `PGType`. type family PGAvg ty where
src/Squeal/PostgreSQL/Expression/Type.hs view
@@ -29,6 +29,7 @@ ( TypeExpression (..) , cast , astype+ , inferredtype , PGTyped (..) , typedef , typetable@@ -112,6 +113,17 @@ -- ^ value -> Expression outer commons grp schemas params from ty astype = cast++-- | `inferredtype` will add a type annotation to an `Expression`+-- which can be useful for fixing the storage type of a value.+--+-- >>> printSQL (inferredtype true)+-- (TRUE :: bool)+inferredtype+ :: PGTyped schemas ty+ => Expression outer common grp schemas params from ty+ -> Expression outer common grp schemas params from ty+inferredtype = astype pgtype {----------------------------------------- type expressions
src/Squeal/PostgreSQL/Manipulation.hs view
@@ -113,8 +113,7 @@ Generally, @parameters@ will be a Haskell tuple or record whose entries may be referenced using positional `Squeal.PostgreSQL.Expression.Parameter.parameter`s and @row@ will be a-Haskell record or a generalized record using tuples of `P` types and normal Haskell-records, whose entries will be targeted using overloaded labels.+Haskell record, whose entries will be targeted using overloaded labels. >>> :set -XDeriveAnyClass -XDerivingStrategies >>> :{
src/Squeal/PostgreSQL/PG.hs view
@@ -45,7 +45,6 @@ , Enumerated (..) , VarArray (..) , FixArray (..)- , SOP.P (..) -- * Type families , LabelsPG , DimPG
src/Squeal/PostgreSQL/Query.hs view
@@ -140,8 +140,7 @@ Generally, @parameters@ will be a Haskell tuple or record whose entries may be referenced using positional `Squeal.PostgreSQL.Expression.Parameter.parameter`s and @row@ will be a-Haskell record or a generalized record using tuples of `P` types and normal Haskell-records, whose entries will be targeted using overloaded labels.+Haskell record, whose entries will be targeted using overloaded labels. Let's see some examples of queries. @@ -212,15 +211,15 @@ >>> :{ let- query :: Query_ (Public Schema) () (Row Int32 Int32)+ query :: Query_ (Public Schema) () (Row Int64 Int32) query = select_ ((fromNull 0 (sum_ (All #col2))) `as` #col1 :* #col1 `as` #col2) ( from (table (#tab `as` #table1)) & groupBy #col1- & having (#col1 + (fromNull 0 (sum_ (Distinct #col2))) .> 1) )+ & having (sum_ (Distinct #col2) .> 1) ) in printSQL query :}-SELECT COALESCE(sum(ALL "col2"), 0) AS "col1", "col1" AS "col2" FROM "tab" AS "table1" GROUP BY "col1" HAVING (("col1" + COALESCE(sum(DISTINCT "col2"), 0)) > 1)+SELECT COALESCE(sum(ALL "col2"), 0) AS "col1", "col1" AS "col2" FROM "tab" AS "table1" GROUP BY "col1" HAVING (sum(DISTINCT "col2") > 1) sorted query: @@ -1023,19 +1022,19 @@ {- | >>> import Data.Monoid (Sum (..))->>> import Data.Int (Int32)+>>> import Data.Int (Int64) >>> :{ let- query :: Query_ schema () (Sum Int32)+ query :: Query_ schema () (Sum Int64) query = withRecursive- ( values_ (1 `as` #n)+ ( values_ ((1 & astype int) `as` #n) `unionAll` select_ ((#n + 1) `as` #n) (from (common #t) & where_ (#n .< 100)) `as` #t ) ( select_ (fromNull 0 (sum_ (All #n)) `as` #getSum) (from (common #t) & groupBy Nil)) in printSQL query :}-WITH RECURSIVE "t" AS ((SELECT * FROM (VALUES (1)) AS t ("n")) UNION ALL (SELECT ("n" + 1) AS "n" FROM "t" AS "t" WHERE ("n" < 100))) SELECT COALESCE(sum(ALL "n"), 0) AS "getSum" FROM "t" AS "t"+WITH RECURSIVE "t" AS ((SELECT * FROM (VALUES ((1 :: int))) AS t ("n")) UNION ALL (SELECT ("n" + 1) AS "n" FROM "t" AS "t" WHERE ("n" < 100))) SELECT COALESCE(sum(ALL "n"), 0) AS "getSum" FROM "t" AS "t" -} withRecursive :: Aliased (Query outer (recursive ': commons) schemas params) recursive
test/Specs/Specs.hs view
@@ -1,4 +1,4 @@-module Specs where+module Main where import Test.Hspec import qualified ExceptionHandling