Skip to content

Commit

Permalink
Let connect() pass any other connection parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
Cito committed Jul 2, 2016
1 parent 9429728 commit 6c788c6
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 14 deletions.
5 changes: 4 additions & 1 deletion docs/contents/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ Version 5.0.1
- The update() and delete() methods of the DB wrapper now use the OID instead
of the primary key if both are provided. This restores backward compatibility
with PyGreSQL 4.x and allows updating the primary key itself if an OID exists.
- Made C extension compatible with MSVC 9 again (needed for Python 2 on Windws).
- The connect() function of the DB API 2.0 module now accepts additional keyword
parameters such as "application_name" which will be passed on to PostgreSQL.
- Made C extension compatible with MSVC 9 again (this was needed to compile for
Python 2 on Windows).

Version 5.0 (2016-03-20)
------------------------
Expand Down
6 changes: 4 additions & 2 deletions docs/contents/pgdb/module.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ of the module as well as several exception classes.
connect -- Open a PostgreSQL connection
---------------------------------------

.. function:: connect([dsn], [user], [password], [host], [database])
.. function:: connect([dsn], [user], [password], [host], [database], [**kwargs])

Return a new connection to the database

Expand All @@ -19,6 +19,7 @@ connect -- Open a PostgreSQL connection
:param str password: the database password
:param str host: the hostname of the database
:param database: the name of the database
:param dict kwargs: other connection parameters
:returns: a connection object
:rtype: :class:`Connection`
:raises pgdb.OperationalError: error connecting to the database
Expand All @@ -30,7 +31,8 @@ If specified, the *dsn* parameter must be a string with the format
are optional. You can also specify the parameters individually using keyword
arguments, which always take precedence. The *host* can also contain a port
if specified in the format ``'host:port'``. In the *opt* part of the *dsn*
you can pass command-line options to the server.
you can pass command-line options to the server. You can pass additional
connection parameters using the optional *kwargs* keyword arguments.

Example::

Expand Down
9 changes: 4 additions & 5 deletions docs/index.rst
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
.. PyGreSQL index page without toc (for use with cloud theme)
.. PyGreSQL index page with toc (for use without cloud theme)
Welcome to PyGreSQL
===================

.. toctree::
:hidden:
:maxdepth: 2

about
copyright
announce
download/index
contents/index
community/index

.. include:: about.txt
community/index
26 changes: 21 additions & 5 deletions pgdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -1442,19 +1442,19 @@ def executemany(self, operation, param_seq):

def connect(dsn=None,
user=None, password=None,
host=None, database=None):
host=None, database=None, **kwargs):
"""Connect to a database."""
# first get params from DSN
dbport = -1
dbhost = ""
dbbase = ""
dbname = ""
dbuser = ""
dbpasswd = ""
dbopt = ""
try:
params = dsn.split(":")
dbhost = params[0]
dbbase = params[1]
dbname = params[1]
dbuser = params[2]
dbpasswd = params[3]
dbopt = params[4]
Expand All @@ -1467,7 +1467,7 @@ def connect(dsn=None,
if password is not None:
dbpasswd = password
if database is not None:
dbbase = database
dbname = database
if host is not None:
try:
params = host.split(":")
Expand All @@ -1482,8 +1482,24 @@ def connect(dsn=None,
if dbuser == "":
dbuser = None

# pass keyword arguments as connection info string
if kwargs:
kwargs = list(kwargs.items())
if '=' in dbname:
dbname = [dbname]
else:
kwargs.insert(0, ('dbname', dbname))
dbname = []
for kw, value in kwargs:
value = str(value)
if not value or ' ' in value:
value = "'%s'" % value.replace(
"'", "\\'").replace('\\', '\\\\')
dbname.append('%s=%s' % (kw, value))
dbname = ' '.join(dbname)

# open the connection
cnx = _connect(dbbase, dbhost, dbport, dbopt, dbuser, dbpasswd)
cnx = _connect(dbname, dbhost, dbport, dbopt, dbuser, dbpasswd)
return Connection(cnx)


Expand Down
11 changes: 10 additions & 1 deletion tests/test_dbapi20.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,21 @@ def setUp(self):
def tearDown(self):
dbapi20.DatabaseAPI20Test.tearDown(self)

def testVersion(self):
def test_version(self):
v = pgdb.version
self.assertIsInstance(v, str)
self.assertIn('.', v)
self.assertEqual(pgdb.__version__, v)

def test_connect_kwargs(self):
application_name = 'PyGreSQL DB API 2.0 Test'
self.connect_kw_args['application_name'] = application_name
con = self._connect()
cur = con.cursor()
cur.execute("select application_name from pg_stat_activity"
" where application_name = %s", (application_name,))
self.assertEqual(cur.fetchone(), (application_name,))

def test_percent_sign(self):
con = self._connect()
cur = con.cursor()
Expand Down

0 comments on commit 6c788c6

Please sign in to comment.