dibbler/sqlalchemy/dialects/mssql/adodbapi.py

88 lines
2.6 KiB
Python
Raw Normal View History

2017-04-15 18:33:29 +02:00
# mssql/adodbapi.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:: mssql+adodbapi
:name: adodbapi
:dbapi: adodbapi
:connectstring: mssql+adodbapi://<username>:<password>@<dsnname>
:url: http://adodbapi.sourceforge.net/
.. note::
The adodbapi dialect is not implemented SQLAlchemy versions 0.6 and
above at this time.
2010-05-07 19:33:49 +02:00
"""
2017-04-15 18:33:29 +02:00
import datetime
2010-05-07 19:33:49 +02:00
from sqlalchemy import types as sqltypes, util
from sqlalchemy.dialects.mssql.base import MSDateTime, MSDialect
import sys
2017-04-15 18:33:29 +02:00
2010-05-07 19:33:49 +02:00
class MSDateTime_adodbapi(MSDateTime):
def result_processor(self, dialect, coltype):
def process(value):
2017-04-15 18:33:29 +02:00
# adodbapi will return datetimes with empty time
# values as datetime.date() objects.
2010-05-07 19:33:49 +02:00
# Promote them back to full datetime.datetime()
if type(value) is datetime.date:
return datetime.datetime(value.year, value.month, value.day)
return value
return process
class MSDialect_adodbapi(MSDialect):
supports_sane_rowcount = True
supports_sane_multi_rowcount = True
supports_unicode = sys.maxunicode == 65535
supports_unicode_statements = True
driver = 'adodbapi'
2017-04-15 18:33:29 +02:00
2010-05-07 19:33:49 +02:00
@classmethod
def import_dbapi(cls):
import adodbapi as module
return module
colspecs = util.update_copy(
MSDialect.colspecs,
{
2017-04-15 18:33:29 +02:00
sqltypes.DateTime: MSDateTime_adodbapi
2010-05-07 19:33:49 +02:00
}
)
def create_connect_args(self, url):
2017-04-15 18:33:29 +02:00
def check_quote(token):
if ";" in str(token):
token = "'%s'" % token
return token
keys = dict(
(k, check_quote(v)) for k, v in url.query.items()
)
2010-05-07 19:33:49 +02:00
connectors = ["Provider=SQLOLEDB"]
if 'port' in keys:
2017-04-15 18:33:29 +02:00
connectors.append("Data Source=%s, %s" %
(keys.get("host"), keys.get("port")))
2010-05-07 19:33:49 +02:00
else:
2017-04-15 18:33:29 +02:00
connectors.append("Data Source=%s" % keys.get("host"))
connectors.append("Initial Catalog=%s" % keys.get("database"))
2010-05-07 19:33:49 +02:00
user = keys.get("user")
if user:
connectors.append("User Id=%s" % user)
connectors.append("Password=%s" % keys.get("password", ""))
else:
connectors.append("Integrated Security=SSPI")
2017-04-15 18:33:29 +02:00
return [[";".join(connectors)], {}]
2010-05-07 19:33:49 +02:00
2017-04-15 18:33:29 +02:00
def is_disconnect(self, e, connection, cursor):
return isinstance(e, self.dbapi.adodbapi.DatabaseError) and \
"'connection failure'" in str(e)
2010-05-07 19:33:49 +02:00
dialect = MSDialect_adodbapi