Changeset 875 for trunk/pgdb.py


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

Let connect() pass any other connection parameters

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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
Note: See TracChangeset for help on using the changeset viewer.