postgresql-simple 0.0.2 → 0.0.3
raw patch · 3 files changed
+64/−11 lines, 3 filesdep ~base
Dependency ranges changed: base
Files
- postgresql-simple.cabal +2/−2
- src/Database/PostgreSQL/Simple/Internal.hs +50/−4
- src/Database/PostgreSQL/Simple/Result.hs +12/−5
postgresql-simple.cabal view
@@ -1,5 +1,5 @@ Name: postgresql-simple-Version: 0.0.2+Version: 0.0.3 Synopsis: Mid-Level PostgreSQL client library Description: Mid-Level PostgreSQL client library, forked from mysql-simple.@@ -53,4 +53,4 @@ source-repository this type: git location: http://github.com/lpsmith/postgresql-simple- tag: v0.0.2+ tag: v0.0.3
src/Database/PostgreSQL/Simple/Internal.hs view
@@ -1,5 +1,23 @@ {-# LANGUAGE OverloadedStrings, NamedFieldPuns, RecordWildCards #-} {-# LANGUAGE DeriveDataTypeable #-}+------------------------------------------------------------------------------+-- |+-- Module: Database.PostgreSQL.Simple.Internal+-- Copyright: (c) 2011 Leon P Smith+-- License: BSD3+-- Maintainer: Leon P Smith <leon@melding-monads.com>+-- Stability: experimental+-- Portability: portable+--+-- Internal bits. This interface is less stable and can change at any time.+-- In particular this means that while the rest of the postgresql-simple +-- package endeavors to follow the package versioning policy, this module +-- does not. Also, at the moment there are things in here that aren't +-- particularly internal and are exported elsewhere; these will eventually +-- disappear from this module.+--+------------------------------------------------------------------------------+ module Database.PostgreSQL.Simple.Internal where import Prelude hiding (catch)@@ -15,8 +33,6 @@ import Database.PostgreSQL.LibPQ(Oid(..)) import qualified Database.PostgreSQL.LibPQ as PQ import Database.PostgreSQL.Simple.BuiltinTypes (BuiltinType)-import Foreign.ForeignPtr (newForeignPtr_, unsafeForeignPtrToPtr)-import Foreign.Ptr (nullPtr) import System.IO.Unsafe (unsafePerformIO) -- | A Field represents metadata about a particular field@@ -75,6 +91,24 @@ , connectDatabase :: String } deriving (Eq,Read,Show,Typeable) +-- | Default information for setting up a connection.+--+-- Defaults are as follows:+--+-- * Server on @localhost@+--+-- * Port on @5432@+--+-- * User @postgres@+--+-- * No password+--+-- * Database @postgres@+--+-- Use as in the following example:+--+-- > connect defaultConnectInfo { connectHost = "db.example.com" }+ defaultConnectInfo :: ConnectInfo defaultConnectInfo = ConnectInfo { connectHost = "127.0.0.1"@@ -89,6 +123,10 @@ connect :: ConnectInfo -> IO Connection connect = connectPostgreSQL . postgreSQLConnectionString +-- | Attempt to make a connection based on a libpq connection string.+-- See <http://www.postgresql.org/docs/9.1/static/libpq-connect.html>+-- for more information.+ connectPostgreSQL :: ByteString -> IO Connection connectPostgreSQL connstr = do conn <- PQ.connectdb connstr@@ -104,6 +142,8 @@ , sqlErrorMsg = msg , sqlState = "" } +-- | Turns a 'ConnectInfo' data structure into a libpq connection string.+ postgreSQLConnectionString :: ConnectInfo -> ByteString postgreSQLConnectionString connectInfo = fromString connstr where@@ -116,12 +156,12 @@ str name field | null value = id- | otherwise = (name ++) . quote value . space+ | otherwise = showString name . quote value . space where value = field connectInfo num name field | value <= 0 = id- | otherwise = (name ++) . (show value ++) . space+ | otherwise = showString name . shows value . space where value = field connectInfo quote str rest = '\'' : foldr delta ('\'' : rest) str@@ -177,5 +217,11 @@ `finally` do putMVar connectionHandle =<< PQ.newNullConnection )++newNullConnection :: IO Connection+newNullConnection = do+ connectionHandle <- newMVar =<< PQ.newNullConnection+ connectionObjects <- newMVar IntMap.empty+ return Connection{..} data RawResult = RawResult { rawField :: Field, rawData :: Maybe ByteString }
src/Database/PostgreSQL/Simple/Result.hs view
@@ -177,11 +177,18 @@ convert f dat = ST.unpack <$> convert f dat instance Result UTCTime where- convert f = doConvert f ok $ \bs ->- case parseTime defaultTimeLocale "%F %T%Q%z" (B8.unpack bs ++ "00") of- Just t -> Right t- Nothing -> returnError ConversionFailed f "could not parse"- where ok = mkCompats [TimestampWithTimeZone]+ convert f =+ case oid2builtin (typeOid f) of+ Just Timestamp -> doIt "%F %T%Q" id+ Just TimestampWithTimeZone -> doIt "%F %T%Q%z" (++ "00")+ _ -> const $ returnError Incompatible f "types incompatible"+ where+ doIt _ _ Nothing = returnError UnexpectedNull f ""+ doIt fmt preprocess (Just bs) =+ case parseTime defaultTimeLocale fmt str of+ Just t -> Right t+ Nothing -> returnError ConversionFailed f "could not parse"+ where str = preprocess (B8.unpack bs) instance Result Day where convert f = atto ok date f