Compare commits
No commits in common. "main" and "1.1.0.0" have entirely different histories.
@ -5,7 +5,7 @@
|
||||
module Database.PostgreSQL.Opium
|
||||
-- * Connection Management
|
||||
( Connection
|
||||
, ConnectionError (..)
|
||||
, ConnectionError
|
||||
, connect
|
||||
, close
|
||||
-- * Queries
|
||||
@ -41,7 +41,7 @@ import Database.PostgreSQL.LibPQ (Result)
|
||||
import qualified Data.Text.Encoding as Encoding
|
||||
import qualified Database.PostgreSQL.LibPQ as LibPQ
|
||||
|
||||
import Database.PostgreSQL.Opium.Connection (Connection, ConnectionError (..), connect, close, withRawConnection)
|
||||
import Database.PostgreSQL.Opium.Connection (Connection, ConnectionError, connect, close, withRawConnection)
|
||||
import Database.PostgreSQL.Opium.Error (Error (..), ErrorPosition (..))
|
||||
import Database.PostgreSQL.Opium.FromField (FromField (..), RawField (..))
|
||||
import Database.PostgreSQL.Opium.FromRow (FromRow (..), ColumnTable)
|
||||
@ -59,11 +59,11 @@ instance RowContainer Maybe where
|
||||
extract result nRows columnTable
|
||||
| nRows == 0 = pure Nothing
|
||||
| nRows == 1 = Just <$> ExceptT (fromRow result columnTable 0)
|
||||
| otherwise = throwE $ ErrorMoreThanOneRow $ fromEnum nRows
|
||||
| otherwise = throwE ErrorMoreThanOneRow
|
||||
|
||||
instance RowContainer Identity where
|
||||
extract result nRows columnTable = do
|
||||
unless (nRows == 1) $ throwE $ ErrorNotExactlyOneRow $ fromEnum nRows
|
||||
unless (nRows == 1) $ throwE ErrorNotExactlyOneRow
|
||||
Identity <$> ExceptT (fromRow result columnTable 0)
|
||||
|
||||
-- The order of the type parameters is important, because it is more common to use type applications for providing the row type and row container type.
|
||||
|
@ -4,7 +4,7 @@
|
||||
|
||||
module Database.PostgreSQL.Opium.Connection
|
||||
( Connection
|
||||
, ConnectionError (..)
|
||||
, ConnectionError
|
||||
, unsafeWithRawConnection
|
||||
, withRawConnection
|
||||
, connect
|
||||
@ -12,7 +12,6 @@ module Database.PostgreSQL.Opium.Connection
|
||||
) where
|
||||
|
||||
import Control.Concurrent.MVar (MVar, newMVar, modifyMVar_, withMVar)
|
||||
import Control.Exception (Exception)
|
||||
import Data.Maybe (fromMaybe)
|
||||
import Data.Text (Text)
|
||||
import Database.PostgreSQL.LibPQ (ConnStatus (..))
|
||||
@ -28,8 +27,6 @@ newtype Connection = Connection
|
||||
newtype ConnectionError = ConnectionError Text
|
||||
deriving (Show)
|
||||
|
||||
instance Exception ConnectionError
|
||||
|
||||
withRawConnection
|
||||
:: HasCallStack
|
||||
=> (Maybe LibPQ.Connection -> IO a)
|
||||
|
@ -17,8 +17,8 @@ data Error
|
||||
| ErrorInvalidOid Text Oid
|
||||
| ErrorUnexpectedNull ErrorPosition
|
||||
| ErrorInvalidField ErrorPosition Oid ByteString String
|
||||
| ErrorNotExactlyOneRow Int
|
||||
| ErrorMoreThanOneRow Int
|
||||
| ErrorNotExactlyOneRow
|
||||
| ErrorMoreThanOneRow
|
||||
| ErrorConnectionClosed
|
||||
deriving (Eq, Show)
|
||||
|
||||
|
@ -7,10 +7,8 @@ module Database.PostgreSQL.Opium.ToField
|
||||
|
||||
import Data.Bits (Bits (..))
|
||||
import Data.ByteString (ByteString)
|
||||
import Data.Int (Int32)
|
||||
import Data.List (singleton)
|
||||
import Data.Text (Text)
|
||||
import Data.Time (Day, diffDays, fromGregorian)
|
||||
import Data.Word (Word32)
|
||||
import Database.PostgreSQL.LibPQ (Format (..), Oid)
|
||||
import Unsafe.Coerce (unsafeCoerce)
|
||||
@ -50,11 +48,3 @@ instance ToField Float where
|
||||
|
||||
instance ToField Double where
|
||||
toField x = Just (Oid.doublePrecision, encodeBigEndian @Word 8 $ unsafeCoerce x, Binary)
|
||||
|
||||
toPostgresJulian :: Day -> Integer
|
||||
toPostgresJulian date = diffDays date postgresEpoch
|
||||
where
|
||||
postgresEpoch = fromGregorian 2000 1 1
|
||||
|
||||
instance ToField Day where
|
||||
toField x = Just (Oid.date, encodeBigEndian @Int32 4 $ fromIntegral $ toPostgresJulian x, Binary)
|
||||
|
@ -20,7 +20,7 @@ name: opium
|
||||
-- PVP summary: +-+------- breaking API changes
|
||||
-- | | +----- non-breaking API additions
|
||||
-- | | | +--- code changes with no API change
|
||||
version: 1.1.1.0
|
||||
version: 1.1.0.0
|
||||
|
||||
-- A short (one-line) description of the package.
|
||||
-- synopsis:
|
||||
@ -119,8 +119,7 @@ test-suite opium-test
|
||||
SpecHook,
|
||||
Database.PostgreSQL.OpiumSpec,
|
||||
Database.PostgreSQL.Opium.FromFieldSpec,
|
||||
Database.PostgreSQL.Opium.FromRowSpec,
|
||||
Database.PostgreSQL.Opium.ToFieldSpec
|
||||
Database.PostgreSQL.Opium.FromRowSpec
|
||||
|
||||
-- Test dependencies.
|
||||
build-depends:
|
||||
|
@ -1,30 +0,0 @@
|
||||
{-# LANGUAGE DeriveGeneric #-}
|
||||
{-# LANGUAGE OverloadedStrings #-}
|
||||
|
||||
module Database.PostgreSQL.Opium.ToFieldSpec (spec) where
|
||||
|
||||
import Data.Functor.Identity (Identity (..))
|
||||
import Data.Text (Text)
|
||||
import Data.Time (fromGregorian)
|
||||
import GHC.Generics (Generic)
|
||||
import Test.Hspec (SpecWith, describe, it, shouldBe)
|
||||
|
||||
import qualified Database.PostgreSQL.Opium as Opium
|
||||
|
||||
newtype Result = Result
|
||||
{ a :: Bool
|
||||
} deriving (Show, Eq, Generic)
|
||||
|
||||
instance Opium.FromRow Result
|
||||
|
||||
shouldBeTrue :: Opium.ToParamList p => Opium.Connection -> Text -> p -> IO ()
|
||||
shouldBeTrue conn query params = do
|
||||
result <- Opium.fetch query params conn
|
||||
result `shouldBe` Right (Identity (Result True))
|
||||
|
||||
spec :: SpecWith Opium.Connection
|
||||
spec = do
|
||||
describe "ToField Day" $ do
|
||||
it "Encodes Day" $ \conn -> do
|
||||
shouldBeTrue conn "SELECT $1 = date '1997-09-29' AS a" (Identity $ fromGregorian 1997 9 29)
|
||||
shouldBeTrue conn "SELECT $1 = date '2025-07-28' AS a" (Identity $ fromGregorian 2025 7 28)
|
Loading…
x
Reference in New Issue
Block a user