Author: cito
Date: Sat Jul  2 04:46:03 2016
New Revision: 875

Log:
Let connect() pass any other connection parameters

Modified:
   trunk/docs/contents/changelog.rst
   trunk/docs/contents/pgdb/module.rst
   trunk/docs/index.rst
   trunk/pgdb.py
   trunk/tests/test_dbapi20.py

Modified: trunk/docs/contents/changelog.rst
==============================================================================
--- trunk/docs/contents/changelog.rst   Sat Apr 16 03:05:28 2016        (r874)
+++ trunk/docs/contents/changelog.rst   Sat Jul  2 04:46:03 2016        (r875)
@@ -6,7 +6,10 @@
 - 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)
 ------------------------

Modified: trunk/docs/contents/pgdb/module.rst
==============================================================================
--- trunk/docs/contents/pgdb/module.rst Sat Apr 16 03:05:28 2016        (r874)
+++ trunk/docs/contents/pgdb/module.rst Sat Jul  2 04:46:03 2016        (r875)
@@ -10,7 +10,7 @@
 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
 
@@ -19,6 +19,7 @@
     :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
@@ -30,7 +31,8 @@
 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::
 

Modified: trunk/docs/index.rst
==============================================================================
--- trunk/docs/index.rst        Sat Apr 16 03:05:28 2016        (r874)
+++ trunk/docs/index.rst        Sat Jul  2 04:46:03 2016        (r875)
@@ -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
\ No newline at end of file
+    community/index
\ No newline at end of file

Modified: trunk/pgdb.py
==============================================================================
--- trunk/pgdb.py       Sat Apr 16 03:05:28 2016        (r874)
+++ trunk/pgdb.py       Sat Jul  2 04:46:03 2016        (r875)
@@ -1442,19 +1442,19 @@
 
 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]
@@ -1467,7 +1467,7 @@
     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(":")
@@ -1482,8 +1482,24 @@
     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)
 
 

Modified: trunk/tests/test_dbapi20.py
==============================================================================
--- trunk/tests/test_dbapi20.py Sat Apr 16 03:05:28 2016        (r874)
+++ trunk/tests/test_dbapi20.py Sat Jul  2 04:46:03 2016        (r875)
@@ -79,12 +79,21 @@
     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()
_______________________________________________
PyGreSQL mailing list
[email protected]
https://mail.vex.net/mailman/listinfo.cgi/pygresql

Reply via email to