joeforker
joeforker

Reputation: 41817

How do I write an external dialect for SQLAlchemy?

I wrote a minimal database dialect for SQLAlchemy that doesn't really belong in the core. How do I make it work as its own Python package?

Upvotes: 24

Views: 10574

Answers (3)

Sean Lynch
Sean Lynch

Reputation: 6487

As of SQLAlchemy 0.8, you can register the dialects in-process without needing to have a separate install.

from sqlalchemy.dialects import registry
registry.register("mysql.foodialect", "myapp.dialect", "MyMySQLDialect")

The above will respond to create_engine("mysql+foodialect://") and load the MyMySQLDialect class from the myapp.dialect module.

See: https://docs.sqlalchemy.org/en/latest/core/connections.html#registering-new-dialects

Upvotes: 22

joeforker
joeforker

Reputation: 41817

When SQLAlchemy resolves a database url example://... it will first try to find it in import sqlalchemy.dialects.example. If that doesn't work it falls back to pkg_resources.iter_entry_points('sqlachemy.databases').

Put the new dialect in a package using setuptools, include an entry point named after your dialect, run python setup.py develop or python setup.py install, and SQLAlchemy should be able to find the dialect.

In setup.py:

   entry_points = {
     'sqlalchemy.databases': ['example = example_sa:base.dialect',]
   },

example_sa:base.dialect means import example_sa; return example_sa.base.dialect.

After installing this package, pkg_resources.iter_entry_points(group) yields pkg_resources.EntryPoint instances from group. Call .load() on the EntryPoint with entrypoint.name='example' and you get example_sa.base.dialect.

I was pleasantly surprised at how easy it is to write new dialects for SQLAlchemy 0.6. If your database has just a few quirks compared to standard SQL, chances are you will be able to inherit from the standard (MySQL-like) SQL dialect, define your database's keywords, and copy the implementation of those quirks (like SELECT TOP 10 instead of SELECT ... LIMIT 10) from an existing dialect.

Upvotes: 18

awknaust
awknaust

Reputation: 47

At least in SQLAlchemy 5.x you can just drop your driver/dialect in the databases directory, wherever that is on your system, (if you installed from source, it might be /usr/local/lib/python2.7/dist-packages/SQLAlchemy-0.5.8-py2.7.egg/sqlalchemy/databases)

Upvotes: -1

Related Questions