Adding a new Database Driver

Peewee comes with built-in support for Postgres, MySQL and SQLite. These databases are very popular and run the gamut from fast, embeddable databases to heavyweight servers suitable for large-scale deployments. That being said, there are a ton of cool databases out there and adding support for your database-of-choice should be really easy, provided the driver supports the DB-API 2.0 spec.

The DB-API 2.0 spec should be familiar to you if you’ve used the standard library sqlite3 driver, psycopg2 or the like. Peewee currently relies on a handful of parts:

  • Connection.commit
  • Connection.execute
  • Connection.rollback
  • Cursor.description
  • Cursor.fetchone

These methods are generally wrapped up in higher-level abstractions and exposed by the Database, so even if your driver doesn’t do these exactly you can still get a lot of mileage out of peewee. An example is the apsw sqlite driver in the “playhouse” module.

The first thing is to provide a subclass of Database that will open a connection.

  1. from peewee import Database
  2. import foodb # Our fictional DB-API 2.0 driver.
  3. class FooDatabase(Database):
  4. def _connect(self, database, **kwargs):
  5. return foodb.connect(database, **kwargs)

The Database provides a higher-level API and is responsible for executing queries, creating tables and indexes, and introspecting the database to get lists of tables. The above implementation is the absolute minimum needed, though some features will not work – for best results you will want to additionally add a method for extracting a list of tables and indexes for a table from the database. We’ll pretend that FooDB is a lot like MySQL and has special “SHOW” statements:

  1. class FooDatabase(Database):
  2. def _connect(self, database, **kwargs):
  3. return foodb.connect(database, **kwargs)
  4. def get_tables(self):
  5. res = self.execute('SHOW TABLES;')
  6. return [r[0] for r in res.fetchall()]

Other things the database handles that are not covered here include:

  • last_insert_id() and rows_affected()
  • param and quote, which tell the SQL-generating code how to add parameter placeholders and quote entity names.
  • field_types for mapping data-types like INT or TEXT to their vendor-specific type names.
  • operations for mapping operations such as “LIKE/ILIKE” to their database equivalent

Refer to the Database API reference or the source code. for details.

Note

If your driver conforms to the DB-API 2.0 spec, there shouldn’t be much work needed to get up and running.

Our new database can be used just like any of the other database subclasses:

  1. from peewee import *
  2. from foodb_ext import FooDatabase
  3. db = FooDatabase('my_database', user='foo', password='secret')
  4. class BaseModel(Model):
  5. class Meta:
  6. database = db
  7. class Blog(BaseModel):
  8. title = CharField()
  9. contents = TextField()
  10. pub_date = DateTimeField()