The PEAK Developers' Center   AddingPsycopgSupport UserPreferences
HelpContents Search Diffs Info Edit Subscribe XML Print View

Note that this code is now included in PEAK, but the page is still useful as a template for adding support for new database drivers.

It is fairly straightforward to add [WWW]PyscoPG support to PEAK as an alternative to the default pgdb PostgreSQL module. This involves the following:

Luckily we can re-use the existing Postgres classes to do most of the work. Put the following code into

    1 from import GenericSQL_URL, PGSQLConnection
    3 class PsycopgURL(GenericSQL_URL):
    4     supportedSchemes = ('psycopg',)
    5     defaultFactory   = 'mymodule.PsycopgConnection'
    7 class PsycopgConnection(PGSQLConnection):
    8     DRIVER = 'psycopg'
   10     supportedTypes = (
   12         'DATETIME', 'TIME', 'DATE', 'INTERVAL', 'BINARY', 'ROWID'
   13     )

This takes care of the URL and Connection classes. Now for the URL scheme. Put the following in a file called my-peak.ini:

psycopg = "mymodule:PsycopgURL" 

Now you can test it using PEAK's Namespace Navigator like so:

env PEAK_CONFIG=./my-peak.ini PYTHONPATH=. peak n2 psycopg:user:pass@host/db 

Change user, pass, host, and db to appropriate values and you should get an environment which allows you to run ad-hoc SQL statements against your database.

Where you actually put the classes and configuration information is up to you. If you already have a site-specific library of Python code, the classes would naturally belong there. Likewise for the addition to peak.naming.schemes: a site-specific peak.ini file would be a good place for this.

EditText of this page (last modified 2004-04-16 18:47:34)
FindPage by browsing, title search , text search or an index
Or try one of these actions: AttachFile, DeletePage, LikePages, LocalSiteMap, SpellCheck