[DB-SIG] URI syntax for database (was: [SQLObject] Re: SQLite connection - relative filename)

Andy Dustman farcepest at gmail.com
Mon Apr 4 21:34:43 CEST 2005


I vote for:

pydbapi-<dbms>://[<user>[:<password>]]@[<host>[:<port>]]/[<database>][?param1=...&param2=...]

Where:

dbms: Name of the database server implementation (mysql, postgresql,
oracle, etc.); map server implementation names to callables (factory
functions) that should be passed a mapping of all the above parameters
and return a DB API connection object.

database: database name

paramX: optional implementation-dependent parameters

user, password, host, port, database: all obvious, standard meaning as
in PEP-249 guidelines for connect()

For MySQLdb, the factory function would look like this:

def MySQLdb_Connection(kwargs):
    from MySQLdb import connect
    kwargs2 = kwargs.copy()
    try:
        del kwargs2['database']
        kwargs2['db'] = kwargs['database']
    except KeyError:
        pass
    try:
        del kwargs2['password']
        kwargs2['passwd'] = kwargs['password']
    except KeyError:
        pass
    return connect(**kwargs2)

Of course, if you use db instead of database, and passwd instead of
password, this reduces to:

def MySQLdb_Connection(kwargs):
    from MySQLdb import connect
    return connect(**kwargs)

The factory functions should be fairly trivial for most database modules.

It might also be possible to (ab)use urllib2 by registering an Opener
for each database of interest which returns an DB API connection
object instead of a file-like object, and then using urllib2.urlopen()
to open connections. But it's probably better to have a seperate
module for this, i.e. dburllib.urlopen(url) -> connection. urlparse
does a good bit of the work already:

>>> import urlparse
>>> urlparse.uses_netloc.append("pydbapi-mysql")
>>> urlparse.uses_query.append("pydbapi-mysql")
>>> urlparse.urlparse('pydbapi-mysql://user:password@host:port/database?compress=1')
('pydbapi-mysql', 'user:password at host:port', '/database', '', 'compress=1', '')
>>>

-- 
Computer interfaces should never be made of meat.

Using GMail? Setting Reply-to address to <> disables this annoying feature.

You are in a maze of twisty little passages all alike.
To go north, press 2. To go west, press 4.
To go east, press 6. To go south, press 8. 
If you need assistance, press 0 and a little dwarf
will assist you.


More information about the DB-SIG mailing list