on dev help sign in
0.5.1.0
commitb82ac4f6d19a49935149ef7ced9dcf639b7e8c28 authorEitan Chatav <eitan@Eitans-MacBook-Pro.local> dateTue 18 Jun 2019, 14:12 (7 years ago) parent7622208
8 files changed+45−21
squeal-postgresql/squeal-postgresql.cabal+4−4 squeal-postgresql/src/Squeal/PostgreSQL/Expression/Aggregate.hs+18−3 squeal-postgresql/src/Squeal/PostgreSQL/Expression/Type.hs+12 squeal-postgresql/src/Squeal/PostgreSQL/Manipulation.hs+1−2 squeal-postgresql/src/Squeal/PostgreSQL/PG.hs−1 squeal-postgresql/src/Squeal/PostgreSQL/Query.hs+8−9 squeal-postgresql/test/Specs/Specs.hs+1−1 stack.yaml+1−1
squeal-postgresql/squeal-postgresql.cabal+4−4
@@ -1,5 +1,5 @@
11name: squeal-postgresql
2−version: 0.5.0.0
2+version: 0.5.1.0
33synopsis: Squeal PostgreSQL Library
44description: Squeal is a type-safe embedding of PostgreSQL in Haskell
55homepage: https://github.com/morphismtech/squeal
@@ -54,7 +54,7 @@ library
5454 Squeal.PostgreSQL.Schema
5555 Squeal.PostgreSQL.Transaction
5656 default-language: Haskell2010
57− ghc-options: -Wall -Werror
57+ ghc-options: -Wall
5858 build-depends:
5959 aeson >= 1.2.4.0
6060 , base >= 4.11.1.0 && < 5.0
@@ -62,7 +62,7 @@ library
6262 , bytestring >= 0.10.8.2
6363 , bytestring-strict-builder >= 0.4.5
6464 , deepseq >= 1.4.3.0
65− , generics-sop >= 0.3.2.0
65+ , generics-sop >= 0.3.2.0 && < 0.5.0.0
6666 , mmorph >= 1.1.1
6767 , mtl >= 2.2.2
6868 , network-ip >= 0.3.0.2
@@ -93,7 +93,7 @@ test-suite squeal-postgresql-specs
9393 default-language: Haskell2010
9494 type: exitcode-stdio-1.0
9595 hs-source-dirs: test/Specs
96− ghc-options: -Wall -main-is Specs
96+ ghc-options: -Wall
9797 main-is: Specs.hs
9898 other-modules: ExceptionHandling
9999 build-depends:
squeal-postgresql/src/Squeal/PostgreSQL/Expression/Aggregate.hs+18−3
@@ -26,6 +26,7 @@ Aggregate functions
2626module Squeal.PostgreSQL.Expression.Aggregate
2727 ( Aggregate (..)
2828 , Distinction (..)
29+ , PGSum
2930 , PGAvg
3031 ) where
3132
@@ -85,9 +86,8 @@ class Aggregate expr1 exprN aggr
8586 -- :}
8687 -- sum(DISTINCT "col")
8788 sum_
88− :: ty `In` PGNum
89− => expr1 (null ty)
90− -> aggr ('Null ty)
89+ :: expr1 (null ty)
90+ -> aggr ('Null (PGSum ty))
9191
9292 -- | input values, including nulls, concatenated into an array
9393 arrayAgg
@@ -434,6 +434,21 @@ unsafeAggregateN
434434 -> Expression outer commons ('Grouped bys) schemas params from y
435435unsafeAggregateN fun xs = UnsafeExpression $ fun <> parenthesized (renderSQL xs)
436436
437+-- | A type family that calculates `PGSum``PGType` of
438+-- a given argument `PGType`.
439+type family PGSum ty where
440+ PGSum 'PGint2 = 'PGint8
441+ PGSum 'PGint4 = 'PGint8
442+ PGSum 'PGint8 = 'PGnumeric
443+ PGSum 'PGfloat4 = 'PGfloat4
444+ PGSum 'PGfloat8 = 'PGfloat8
445+ PGSum 'PGnumeric = 'PGnumeric
446+ PGSum 'PGinterval = 'PGinterval
447+ PGSum 'PGmoney = 'PGmoney
448+ PGSum pg = TypeError
449+ ( 'Text "Squeal type error: Cannot sum with argument type "
450+ ':<>: 'ShowType pg )
451+
437452-- | A type family that calculates `PGAvg` type of a `PGType`.
438453type family PGAvg ty where
439454 PGAvg 'PGint2 = 'PGnumeric
squeal-postgresql/src/Squeal/PostgreSQL/Expression/Type.hs+12
@@ -29,6 +29,7 @@ module Squeal.PostgreSQL.Expression.Type
2929 ( TypeExpression (..)
3030 , cast
3131 , astype
32+ , inferredtype
3233 , PGTyped (..)
3334 , typedef
3435 , typetable
@@ -113,6 +114,17 @@ astype
113114 -> Expression outer commons grp schemas params from ty
114115astype = cast
115116
117+-- | `inferredtype` will add a type annotation to an `Expression`
118+-- which can be useful for fixing the storage type of a value.
119+--
120+-- >>> printSQL (inferredtype true)
121+-- (TRUE :: bool)
122+inferredtype
123+ :: PGTyped schemas ty
124+ => Expression outer common grp schemas params from ty
125+ -> Expression outer common grp schemas params from ty
126+inferredtype = astype pgtype
127+
116128{-----------------------------------------
117129type expressions
118130-----------------------------------------}
squeal-postgresql/src/Squeal/PostgreSQL/Manipulation.hs+1−2
@@ -113,8 +113,7 @@ using `Squeal.PostgreSQL.PQ.manipulate`.
113113Generally, @parameters@ will be a Haskell tuple or record whose entries
114114may be referenced using positional
115115`Squeal.PostgreSQL.Expression.Parameter.parameter`s and @row@ will be a
116−Haskell record or a generalized record using tuples of `P` types and normal Haskell
117−records, whose entries will be targeted using overloaded labels.
116+Haskell record, whose entries will be targeted using overloaded labels.
118117
119118>>> :set -XDeriveAnyClass -XDerivingStrategies
120119>>> :{
squeal-postgresql/src/Squeal/PostgreSQL/PG.hs−1
@@ -45,7 +45,6 @@ module Squeal.PostgreSQL.PG
4545 , Enumerated (..)
4646 , VarArray (..)
4747 , FixArray (..)
48− , SOP.P (..)
4948 -- * Type families
5049 , LabelsPG
5150 , DimPG
squeal-postgresql/src/Squeal/PostgreSQL/Query.hs+8−9
@@ -140,8 +140,7 @@ using `Squeal.PostgreSQL.PQ.runQuery`.
140140Generally, @parameters@ will be a Haskell tuple or record whose entries
141141may be referenced using positional
142142`Squeal.PostgreSQL.Expression.Parameter.parameter`s and @row@ will be a
143−Haskell record or a generalized record using tuples of `P` types and normal Haskell
144−records, whose entries will be targeted using overloaded labels.
143+Haskell record, whose entries will be targeted using overloaded labels.
145144
146145Let's see some examples of queries.
147146
@@ -212,15 +211,15 @@ aggregation query:
212211
213212>>> :{
214213let
215− query :: Query_ (Public Schema) () (Row Int32 Int32)
214+ query :: Query_ (Public Schema) () (Row Int64 Int32)
216215 query =
217216 select_ ((fromNull 0 (sum_ (All #col2))) `as` #col1 :* #col1 `as` #col2)
218217 ( from (table (#tab `as` #table1))
219218 & groupBy #col1
220− & having (#col1 + (fromNull 0 (sum_ (Distinct #col2))) .> 1) )
219+ & having (sum_ (Distinct #col2) .> 1) )
221220in printSQL query
222221:}
223−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)
222+SELECT COALESCE(sum(ALL "col2"), 0) AS "col1", "col1" AS "col2" FROM "tab" AS "table1" GROUP BY "col1" HAVING (sum(DISTINCT "col2") > 1)
224223
225224sorted query:
226225
@@ -1023,19 +1022,19 @@ instance With (Query outer) where
10231022
10241023{- |
10251024>>> import Data.Monoid (Sum (..))
1026−>>> import Data.Int (Int32)
1025+>>> import Data.Int (Int64)
10271026>>> :{
10281027 let
1029− query :: Query_ schema () (Sum Int32)
1028+ query :: Query_ schema () (Sum Int64)
10301029 query = withRecursive
1031− ( values_ (1 `as` #n)
1030+ ( values_ ((1 & astype int) `as` #n)
10321031 `unionAll`
10331032 select_ ((#n + 1) `as` #n)
10341033 (from (common #t) & where_ (#n .< 100)) `as` #t )
10351034 ( select_ (fromNull 0 (sum_ (All #n)) `as` #getSum) (from (common #t) & groupBy Nil))
10361035 in printSQL query
10371036:}
1038−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"
1037+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"
10391038-}
10401039withRecursive
10411040 :: Aliased (Query outer (recursive ': commons) schemas params) recursive
squeal-postgresql/test/Specs/Specs.hs+1−1
@@ -1,4 +1,4 @@
1−module Specs where
1+module Main where
22
33import Test.Hspec
44import qualified ExceptionHandling
stack.yaml+1−1
@@ -1,4 +1,4 @@
1−resolver: lts-13.8
1+resolver: lts-13.25
22packages:
33- squeal-postgresql
44