2017-04-15 18:33:29 +02:00
|
|
|
# sybase/pyodbc.py
|
|
|
|
# Copyright (C) 2005-2017 the SQLAlchemy authors and contributors
|
|
|
|
# <see AUTHORS file>
|
|
|
|
#
|
|
|
|
# This module is part of SQLAlchemy and is released under
|
|
|
|
# the MIT License: http://www.opensource.org/licenses/mit-license.php
|
2010-05-07 19:33:49 +02:00
|
|
|
|
2017-04-15 18:33:29 +02:00
|
|
|
"""
|
|
|
|
.. dialect:: sybase+pyodbc
|
|
|
|
:name: PyODBC
|
|
|
|
:dbapi: pyodbc
|
|
|
|
:connectstring: sybase+pyodbc://<username>:<password>@<dsnname>\
|
|
|
|
[/<database>]
|
|
|
|
:url: http://pypi.python.org/pypi/pyodbc/
|
2010-05-07 19:33:49 +02:00
|
|
|
|
|
|
|
|
|
|
|
Unicode Support
|
|
|
|
---------------
|
|
|
|
|
2017-04-15 18:33:29 +02:00
|
|
|
The pyodbc driver currently supports usage of these Sybase types with
|
2010-05-07 19:33:49 +02:00
|
|
|
Unicode or multibyte strings::
|
|
|
|
|
|
|
|
CHAR
|
|
|
|
NCHAR
|
|
|
|
NVARCHAR
|
|
|
|
TEXT
|
|
|
|
VARCHAR
|
|
|
|
|
|
|
|
Currently *not* supported are::
|
|
|
|
|
|
|
|
UNICHAR
|
|
|
|
UNITEXT
|
|
|
|
UNIVARCHAR
|
2017-04-15 18:33:29 +02:00
|
|
|
|
2010-05-07 19:33:49 +02:00
|
|
|
"""
|
|
|
|
|
2017-04-15 18:33:29 +02:00
|
|
|
from sqlalchemy.dialects.sybase.base import SybaseDialect,\
|
|
|
|
SybaseExecutionContext
|
2010-05-07 19:33:49 +02:00
|
|
|
from sqlalchemy.connectors.pyodbc import PyODBCConnector
|
2017-04-15 18:33:29 +02:00
|
|
|
from sqlalchemy import types as sqltypes, processors
|
2010-05-07 19:33:49 +02:00
|
|
|
import decimal
|
2017-04-15 18:33:29 +02:00
|
|
|
|
2010-05-07 19:33:49 +02:00
|
|
|
|
|
|
|
class _SybNumeric_pyodbc(sqltypes.Numeric):
|
|
|
|
"""Turns Decimals with adjusted() < -6 into floats.
|
2017-04-15 18:33:29 +02:00
|
|
|
|
|
|
|
It's not yet known how to get decimals with many
|
2010-05-07 19:33:49 +02:00
|
|
|
significant digits or very large adjusted() into Sybase
|
|
|
|
via pyodbc.
|
2017-04-15 18:33:29 +02:00
|
|
|
|
2010-05-07 19:33:49 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
def bind_processor(self, dialect):
|
2017-04-15 18:33:29 +02:00
|
|
|
super_process = super(_SybNumeric_pyodbc, self).\
|
|
|
|
bind_processor(dialect)
|
2010-05-07 19:33:49 +02:00
|
|
|
|
|
|
|
def process(value):
|
|
|
|
if self.asdecimal and \
|
|
|
|
isinstance(value, decimal.Decimal):
|
|
|
|
|
|
|
|
if value.adjusted() < -6:
|
|
|
|
return processors.to_float(value)
|
|
|
|
|
|
|
|
if super_process:
|
|
|
|
return super_process(value)
|
|
|
|
else:
|
|
|
|
return value
|
|
|
|
return process
|
|
|
|
|
2017-04-15 18:33:29 +02:00
|
|
|
|
2010-05-07 19:33:49 +02:00
|
|
|
class SybaseExecutionContext_pyodbc(SybaseExecutionContext):
|
|
|
|
def set_ddl_autocommit(self, connection, value):
|
|
|
|
if value:
|
|
|
|
connection.autocommit = True
|
|
|
|
else:
|
|
|
|
connection.autocommit = False
|
|
|
|
|
2017-04-15 18:33:29 +02:00
|
|
|
|
2010-05-07 19:33:49 +02:00
|
|
|
class SybaseDialect_pyodbc(PyODBCConnector, SybaseDialect):
|
|
|
|
execution_ctx_cls = SybaseExecutionContext_pyodbc
|
|
|
|
|
|
|
|
colspecs = {
|
2017-04-15 18:33:29 +02:00
|
|
|
sqltypes.Numeric: _SybNumeric_pyodbc,
|
2010-05-07 19:33:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
dialect = SybaseDialect_pyodbc
|