Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion postgresql-simple-interval.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ common library
attoparsec ^>=0.14.4,
base >=4.13 && <4.22,
bytestring >=0.10.10 && <0.13,
postgresql-simple ^>=0.7,

default-language: Haskell2010
ghc-options:
Expand Down Expand Up @@ -61,7 +62,6 @@ library
import: library
build-depends:
persistent ^>=2.17,
postgresql-simple ^>=0.7,
scientific ^>=0.3.8,
text >=1.2.4 && <1.3 || >=2.0 && <2.2,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ import qualified Database.PostgreSQL.Simple.TypeInfo.Static as Postgres
-- - 'Data.Time.CalendarDiffTime': Does not handle days. Embeds a
-- @NominalDiffTime@. Is not bounded.
-- - 'Data.Time.CalendarDiffDays': Does not handle seconds. Is not bounded.
--
-- WARNING: The PostgreSQL interval parser is broken in versions prior to 15.
-- It is not possible to round trip all intervals through PostgreSQL on those
-- versions. You should upgrade to at least PostgreSQL version 15. For more
-- information, see this patch:
-- <https://git.postgresql.org/gitweb/?p=postgresql.git;a=commitdiff;h=e39f99046>
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

data Interval = MkInterval
{ months :: !Int.Int32,
days :: !Int.Int32,
Expand Down
21 changes: 21 additions & 0 deletions source/test-suite/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import qualified Data.ByteString as ByteString
import qualified Data.ByteString.Builder as Builder
import qualified Data.ByteString.Lazy as LazyByteString
import qualified Data.Int as Int
import qualified Database.PostgreSQL.Simple as Postgres
import qualified Database.PostgreSQL.Simple.Interval.Unstable as I
import qualified Database.PostgreSQL.Simple.ToField as Postgres
import qualified Test.Hspec as H

main :: IO ()
Expand Down Expand Up @@ -143,13 +145,32 @@ spec = H.describe "Database.PostgreSQL.Simple.Interval" $ do
let actual = Attoparsec.parseOnly I.parse input
actual `H.shouldBe` Right interval

H.describe "integration" $ do
Monad.forM_ intervalStyles $ \(style, field) -> do
H.describe ("with style " <> show style) $ do
Monad.forM_ examples $ \example -> do
H.it ("round trips " <> show (field example)) $ do
let interval = exampleInterval example
actual <- Postgres.withConnect Postgres.defaultConnectInfo $ \connection -> do
Postgres.withTransaction connection $ do
Monad.void $ Postgres.execute connection "set local intervalstyle = ?" [style]
Postgres.query connection "select ?" [interval]
actual `H.shouldBe` [Postgres.Only interval]

data IntervalStyle
= Iso8601
| Postgres
| PostgresVerbose
| SqlStandard
deriving (Eq, Show)

instance Postgres.ToField IntervalStyle where
toField style = Postgres.Plain $ case style of
Iso8601 -> "iso_8601"
Postgres -> "postgres"
PostgresVerbose -> "postgres_verbose"
SqlStandard -> "sql_standard"

data Example = MkExample
{ exampleInterval :: I.Interval,
exampleIso8601 :: ByteString.ByteString,
Expand Down
Loading