Changeset 875 for trunk


Ignore:
Timestamp:
Jul 2, 2016, 4:46:03 AM (3 years ago)
Author:
cito
Message:

Let connect() pass any other connection parameters

Location:
trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/docs/contents/changelog.rst

    r872 r875  
    77  of the primary key if both are provided. This restores backward compatibility
    88  with PyGreSQL 4.x and allows updating the primary key itself if an OID exists.
    9 - Made C extension compatible with MSVC 9 again (needed for Python 2 on Windws).
     9- The connect() function of the DB API 2.0 module now accepts additional keyword
     10  parameters such as "application_name" which will be passed on to PostgreSQL.
     11- Made C extension compatible with MSVC 9 again (this was needed to compile for
     12  Python 2 on Windows).
    1013
    1114Version 5.0 (2016-03-20)
  • trunk/docs/contents/pgdb/module.rst

    r817 r875  
    1111---------------------------------------
    1212
    13 .. function:: connect([dsn], [user], [password], [host], [database])
     13.. function:: connect([dsn], [user], [password], [host], [database], [**kwargs])
    1414
    1515    Return a new connection to the database
     
    2020    :param str host: the hostname of the database
    2121    :param database: the name of the database
     22    :param dict kwargs: other connection parameters
    2223    :returns: a connection object
    2324    :rtype: :class:`Connection`
     
    3132arguments, which always take precedence. The *host* can also contain a port
    3233if specified in the format ``'host:port'``. In the *opt* part of the *dsn*
    33 you can pass command-line options to the server.
     34you can pass command-line options to the server. You can pass additional
     35connection parameters using the optional *kwargs* keyword arguments.
    3436
    3537Example::
  • trunk/docs/index.rst

    r831 r875  
    1 .. PyGreSQL index page without toc (for use with cloud theme)
     1.. PyGreSQL index page with toc (for use without cloud theme)
    22
    33Welcome to PyGreSQL
     
    55
    66.. toctree::
    7     :hidden:
     7    :maxdepth: 2
    88
     9    about
    910    copyright
    1011    announce
     
    1213    contents/index
    1314    community/index
    14 
    15 .. include:: about.txt
  • trunk/pgdb.py

    r849 r875  
    14431443def connect(dsn=None,
    14441444        user=None, password=None,
    1445         host=None, database=None):
     1445        host=None, database=None, **kwargs):
    14461446    """Connect to a database."""
    14471447    # first get params from DSN
    14481448    dbport = -1
    14491449    dbhost = ""
    1450     dbbase = ""
     1450    dbname = ""
    14511451    dbuser = ""
    14521452    dbpasswd = ""
     
    14551455        params = dsn.split(":")
    14561456        dbhost = params[0]
    1457         dbbase = params[1]
     1457        dbname = params[1]
    14581458        dbuser = params[2]
    14591459        dbpasswd = params[3]
     
    14681468        dbpasswd = password
    14691469    if database is not None:
    1470         dbbase = database
     1470        dbname = database
    14711471    if host is not None:
    14721472        try:
     
    14831483        dbuser = None
    14841484
     1485    # pass keyword arguments as connection info string
     1486    if kwargs:
     1487        kwargs = list(kwargs.items())
     1488        if '=' in dbname:
     1489            dbname = [dbname]
     1490        else:
     1491            kwargs.insert(0, ('dbname', dbname))
     1492            dbname = []
     1493        for kw, value in kwargs:
     1494            value = str(value)
     1495            if not value or ' ' in value:
     1496                value = "'%s'" % value.replace(
     1497                    "'", "\\'").replace('\\', '\\\\')
     1498            dbname.append('%s=%s' % (kw, value))
     1499        dbname = ' '.join(dbname)
     1500
    14851501    # open the connection
    1486     cnx = _connect(dbbase, dbhost, dbport, dbopt, dbuser, dbpasswd)
     1502    cnx = _connect(dbname, dbhost, dbport, dbopt, dbuser, dbpasswd)
    14871503    return Connection(cnx)
    14881504
  • trunk/tests/test_dbapi20.py

    r871 r875  
    8080        dbapi20.DatabaseAPI20Test.tearDown(self)
    8181
    82     def testVersion(self):
     82    def test_version(self):
    8383        v = pgdb.version
    8484        self.assertIsInstance(v, str)
    8585        self.assertIn('.', v)
    8686        self.assertEqual(pgdb.__version__, v)
     87
     88    def test_connect_kwargs(self):
     89        application_name = 'PyGreSQL DB API 2.0 Test'
     90        self.connect_kw_args['application_name'] = application_name
     91        con = self._connect()
     92        cur = con.cursor()
     93        cur.execute("select application_name from pg_stat_activity"
     94            " where application_name = %s", (application_name,))
     95        self.assertEqual(cur.fetchone(), (application_name,))
    8796
    8897    def test_percent_sign(self):
Note: See TracChangeset for help on using the changeset viewer.