Hi all, 

the company I worked for has decided to change a RDBMS behind our ERP.
The side effect of this is that the columns will no longer be prefixed with 
*t$*
but with* t_* instead. I do not want to change all the occurences of column
names in my code. I should also mention, that I use only selects, and no 
ORM.

So what I did was I made a new dialect as a subclass of mssql.pyodbc and
I overrode execution context and statement compiler. In statement 
compiler's 
visit_select I simply replace "t$" with "t_" in the select returned from 
the parent, 
and in execution context's get_result_proxy I return a proxy whose row 
proxy's 
keymap is slightly updated (don't worry, I am attaching the code).

My question: Is this the right way to do it?
Thanks in advance

--
Petr

( attaching the code of the dialect - same as this: 
https://gist.github.com/4058539 )

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/sqlalchemy/-/nXkzhvJiwysJ.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

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

Reply via email to