@@ -1,5 +1,21 @@
1{-# LANGUAGE OverloadedStrings, NamedFieldPuns, RecordWildCards #-}
1{-# LANGUAGE OverloadedStrings, NamedFieldPuns, RecordWildCards #-}
2{-# LANGUAGE DeriveDataTypeable #-}
2{-# LANGUAGE DeriveDataTypeable #-}
3+------------------------------------------------------------------------------
5+-- Module: Database.PostgreSQL.Simple.Internal
6+-- Copyright: (c) 2011 Leon P Smith
8+-- Maintainer: Leon P Smith <leon@melding-monads.com>
9+-- Stability: experimental
10+-- Portability: portable
12+-- Internal bits. This interface is less stable and can change at any time.
13+-- Also, at the moment there are things in here that aren't particularly
14+-- internal and are exported elsewhere; these will eventually disappear
17+------------------------------------------------------------------------------
3module Database.PostgreSQL.Simple.Internal where
19module Database.PostgreSQL.Simple.Internal where
5import Prelude hiding (catch)
21import Prelude hiding (catch)
@@ -73,6 +89,24 @@ data ConnectInfo = ConnectInfo {
73 , connectDatabase :: String
89 , connectDatabase :: String
74 } deriving (Eq,Read,Show,Typeable)
90 } deriving (Eq,Read,Show,Typeable)
92+-- | Default information for setting up a connection.
94+-- Defaults are as follows:
96+-- * Server on @localhost@
104+-- * Database @postgres@
106+-- Use as in the following example:
108+-- > connect defaultConnectInfo { connectHost = "db.example.com" }
76defaultConnectInfo :: ConnectInfo
110defaultConnectInfo :: ConnectInfo
77defaultConnectInfo = ConnectInfo {
111defaultConnectInfo = ConnectInfo {
78 connectHost = "127.0.0.1"
112 connectHost = "127.0.0.1"
@@ -87,6 +121,10 @@ defaultConnectInfo = ConnectInfo {
87connect :: ConnectInfo -> IO Connection
121connect :: ConnectInfo -> IO Connection
88connect = connectPostgreSQL . postgreSQLConnectionString
122connect = connectPostgreSQL . postgreSQLConnectionString
124+-- | Attempt to make a connection based on a libpq connection string.
125+-- See <http://www.postgresql.org/docs/9.1/static/libpq-connect.html>
126+-- for more information.
90connectPostgreSQL :: ByteString -> IO Connection
128connectPostgreSQL :: ByteString -> IO Connection
91connectPostgreSQL connstr = do
129connectPostgreSQL connstr = do
92 conn <- PQ.connectdb connstr
130 conn <- PQ.connectdb connstr
@@ -102,6 +140,8 @@ connectPostgreSQL connstr = do
102 , sqlErrorMsg = msg
140 , sqlErrorMsg = msg
103 , sqlState = "" }
141 , sqlState = "" }
143+-- | Turns a 'ConnectInfo' data structure into a libpq connection string.
105postgreSQLConnectionString :: ConnectInfo -> ByteString
145postgreSQLConnectionString :: ConnectInfo -> ByteString
106postgreSQLConnectionString connectInfo = fromString connstr
146postgreSQLConnectionString connectInfo = fromString connstr