|The PEAK Developers' Center||FrontPage||RecentChanges||TitleIndex||WordIndex||SiteNavigation||HelpContents|
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 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 mymodule.py:
1 from peak.storage.SQL import GenericSQL_URL, PGSQLConnection 2 3 class PsycopgURL(GenericSQL_URL): 4 supportedSchemes = ('psycopg',) 5 defaultFactory = 'mymodule.PsycopgConnection' 6 7 class PsycopgConnection(PGSQLConnection): 8 DRIVER = 'psycopg' 9 10 supportedTypes = ( 11 'NUMBER', 'LONGINTEGER', 'INTEGER', 'FLOAT', 'STRING', 'BOOLEAN', 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:
[peak.naming.schemes] 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.