Created
November 12, 2012 10:28
-
-
Save petrblahos/4058539 to your computer and use it in GitHub Desktop.
SQLAlchemy dialect for renamed columns
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import copy | |
import sqlalchemy.engine.base | |
import sqlalchemy.dialects.mssql.base | |
import sqlalchemy.dialects.mssql.pyodbc | |
class SQLABaanRowProxy(sqlalchemy.engine.base.RowProxy): | |
def __init__(self, parent, row, processors, keymap): | |
""" | |
For all keys starting with "t_" adds also a "t$..." key. | |
""" | |
km = copy.deepcopy(keymap) | |
for (k, v) in keymap.iteritems(): | |
if isinstance(k, basestring): | |
if k.startswith("t_"): | |
km["t$" + k[2:]] = v | |
super(SQLABaanRowProxy, self).__init__(parent, row, processors, km) | |
class SQLABaanResultProxy(sqlalchemy.engine.base.ResultProxy): | |
_process_row = SQLABaanRowProxy | |
class SQLABaanCompiler(sqlalchemy.dialects.mssql.base.MSSQLCompiler): | |
def visit_select(self, select, **kwargs): | |
""" | |
Brute-force replace of "t$" to "t_...":w | |
""" | |
ret = super(SQLABaanCompiler, self).visit_select(select, **kwargs) | |
ret = ret.replace("t$", "t_") | |
return ret | |
class SQLABaanExecutionContext(sqlalchemy.dialects.mssql.pyodbc.MSExecutionContext_pyodbc): | |
def get_result_proxy(self): | |
if self._result_proxy: | |
return self._result_proxy #can never happen as we do only selects | |
else: | |
return SQLABaanResultProxy(self) | |
class SQLABaan(sqlalchemy.dialects.mssql.pyodbc.MSDialect_pyodbc): | |
execution_ctx_cls = SQLABaanExecutionContext | |
statement_compiler = SQLABaanCompiler | |
dialect = SQLABaan |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment