Skip to content

Commit

Permalink
force outdated ODBC drivers to use legacy datetime type
Browse files Browse the repository at this point in the history
  • Loading branch information
michiya committed Aug 21, 2014
1 parent 210bc46 commit ce785d1
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
3 changes: 2 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,8 @@ Dictionary. Current available keys are:
(the same behavior as the original ``django-pyodbc``). Otherwise, they
are mapped to new dedicated data types (``date``, ``time``, ``datetime2``).
Default value is ``False``, and note that the feature is always activated
when you use SQL Server 2005 or FreeTDS.
when you use SQL Server 2005, or the outdated ODBC drivers
(``"FreeTDS"``/``"SQL Server"``/``"SQL Native Client"``).

backend-specific settings
~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down
16 changes: 9 additions & 7 deletions sql_server/pyodbc/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,24 +261,26 @@ def _cursor(self):

self.drv_name = self.connection.getinfo(Database.SQL_DRIVER_NAME).upper()

freetds = self.drv_name.startswith('LIBTDSODBC')
if freetds:
self.use_legacy_datetime = True
driver_is_freetds = self.drv_name.startswith('LIBTDSODBC')
driver_is_sqlsrv32 = self.drv_name == 'SQLSRV32.DLL'
driver_is_snac9 = self.drv_name == "SQLNCLI.DLL"

self.use_legacy_datetime = \
driver_is_freetds or driver_is_sqlsrv32 or driver_is_snac9

if self.ops.sql_server_ver < 2008:
self.use_legacy_datetime = True
self.features.has_bulk_insert = False

if self.use_legacy_datetime:
types = self.creation.data_types
types['DateField'] = types['DateTimeField'] = types['TimeField'] = 'datetime'
self.creation.use_legacy_datetime()
self.features.supports_microsecond_precision = False
self.features.supports_mixed_date_datetime_comparisons = True

ms_drv_names = re.compile('^(LIB)?(SQLN?CLI|MSODBCSQL)')
if self.driver_needs_utf8 is None:
self.driver_needs_utf8 = False
if self.drv_name == 'SQLSRV32.DLL' or ms_drv_names.match(self.drv_name):
if driver_is_sqlsrv32 or ms_drv_names.match(self.drv_name):
self.driver_needs_utf8 = False

# http://msdn.microsoft.com/en-us/library/ms131686.aspx
Expand All @@ -290,7 +292,7 @@ def _cursor(self):
# FreeTDS can't execute some sql queries like CREATE DATABASE etc.
# in multi-statement, so we need to commit the above SQL sentence(s)
# to avoid this
if freetds and not self.connection.autocommit:
if driver_is_freetds and not self.connection.autocommit:
self.connection.commit()

return CursorWrapper(cursor, self)
Expand Down
4 changes: 4 additions & 0 deletions sql_server/pyodbc/creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,7 @@ def sql_table_creation_suffix(self):
if self.connection.settings_dict['TEST_COLLATION']:
suffix.append('COLLATE %s' % self.connection.settings_dict['TEST_COLLATION'])
return ' '.join(suffix)

def use_legacy_datetime(self):
for field in ('DateField', 'DateTimeField', 'TimeField'):
self.data_types[field] = 'datetime'

0 comments on commit ce785d1

Please sign in to comment.