adodbapi.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. # mssql/adodbapi.py
  2. # Copyright (C) 2005-2017 the SQLAlchemy authors and contributors
  3. # <see AUTHORS file>
  4. #
  5. # This module is part of SQLAlchemy and is released under
  6. # the MIT License: http://www.opensource.org/licenses/mit-license.php
  7. """
  8. .. dialect:: mssql+adodbapi
  9. :name: adodbapi
  10. :dbapi: adodbapi
  11. :connectstring: mssql+adodbapi://<username>:<password>@<dsnname>
  12. :url: http://adodbapi.sourceforge.net/
  13. .. note::
  14. The adodbapi dialect is not implemented SQLAlchemy versions 0.6 and
  15. above at this time.
  16. """
  17. import datetime
  18. from sqlalchemy import types as sqltypes, util
  19. from sqlalchemy.dialects.mssql.base import MSDateTime, MSDialect
  20. import sys
  21. class MSDateTime_adodbapi(MSDateTime):
  22. def result_processor(self, dialect, coltype):
  23. def process(value):
  24. # adodbapi will return datetimes with empty time
  25. # values as datetime.date() objects.
  26. # Promote them back to full datetime.datetime()
  27. if type(value) is datetime.date:
  28. return datetime.datetime(value.year, value.month, value.day)
  29. return value
  30. return process
  31. class MSDialect_adodbapi(MSDialect):
  32. supports_sane_rowcount = True
  33. supports_sane_multi_rowcount = True
  34. supports_unicode = sys.maxunicode == 65535
  35. supports_unicode_statements = True
  36. driver = 'adodbapi'
  37. @classmethod
  38. def import_dbapi(cls):
  39. import adodbapi as module
  40. return module
  41. colspecs = util.update_copy(
  42. MSDialect.colspecs,
  43. {
  44. sqltypes.DateTime: MSDateTime_adodbapi
  45. }
  46. )
  47. def create_connect_args(self, url):
  48. def check_quote(token):
  49. if ";" in str(token):
  50. token = "'%s'" % token
  51. return token
  52. keys = dict(
  53. (k, check_quote(v)) for k, v in url.query.items()
  54. )
  55. connectors = ["Provider=SQLOLEDB"]
  56. if 'port' in keys:
  57. connectors.append("Data Source=%s, %s" %
  58. (keys.get("host"), keys.get("port")))
  59. else:
  60. connectors.append("Data Source=%s" % keys.get("host"))
  61. connectors.append("Initial Catalog=%s" % keys.get("database"))
  62. user = keys.get("user")
  63. if user:
  64. connectors.append("User Id=%s" % user)
  65. connectors.append("Password=%s" % keys.get("password", ""))
  66. else:
  67. connectors.append("Integrated Security=SSPI")
  68. return [[";".join(connectors)], {}]
  69. def is_disconnect(self, e, connection, cursor):
  70. return isinstance(e, self.dbapi.adodbapi.DatabaseError) and \
  71. "'connection failure'" in str(e)
  72. dialect = MSDialect_adodbapi