4

using: Python 2.7.3 SQLAlchemy 0.7.8 PyODBC 3.0.3

I have implemented my own Dialect for the EXASolution DB using PyODBC as the underlying db driver. I need to make use of PyODBC's output_converter function to translate DECIMAL(x, 0) columns to integers/longs.

The following code snippet does the trick:

pyodbc = self.dbapi
dbapi_con = connection.connection

dbapi_version = dbapi_con.getinfo(pyodbc.SQL_DRIVER_VER)
(major, minor, patch) = [int(x) for x in dbapi_version]
if major >= 3:
    dbapi_con.add_output_converter(pyodbc.SQL_DECIMAL, self.decimal2int)

I have placed this code snippet in the initialize(self, connection) method of

    class EXADialect_pyodbc(PyODBCConnector, EXADialect):

Code gets called, and no exception is thrown, but this is a one time initialization. Later on, other connections are created. These connections are not passed through my initialization code.

Does anyone have a hint on how connection initialization works with SQLAlchemy, and where to place my code so that it gets called for every new connection created?

fthiella
  • 48,073
  • 15
  • 90
  • 106
jank
  • 41
  • 2
  • Stupid StackOverflow keeps me from answering my own questsions (wait 7 hours -ha). Here is the solution: method on_connect(self) of class default.DefaultDialect has to be overridden in the dialect specific class. – jank Jul 26 '12 at 14:34
  • you should post your solution as an answer and accept it. – khoxsey Aug 01 '12 at 18:06

1 Answers1

0

This is an old question, but something I hit recently, so an updated answer may help someone else along the way. In my case, I was trying to automatically downcase mssql UNIQUEIDENTIFIER columns (guids).

You can grab the raw connection (pyodbc) through the session or engine to do this:

engine = create_engine(connection_string)
make_session = sessionmaker(engine)
...
session = make_session()

session.connection().connection.add_output_converter(SQL_DECIMAL, decimal2int)

# or

connection = engine.connect().connection
connection.add_output_converter(SQL_DECIMAL, decimal2int)
Tyler
  • 567
  • 5
  • 5