MSSQL_RESERVED_WORDS = set(['function'])
+
class _StringType(object):
"""Base for MSSQL string types."""
return "%s(%s)" % (self.__class__.__name__,
', '.join(['%s=%r' % (k, params[k]) for k in params]))
+ def bind_processor(self, dialect):
+ if self.convert_unicode or dialect.convert_unicode:
+ if self.assert_unicode is None:
+ assert_unicode = dialect.assert_unicode
+ else:
+ assert_unicode = self.assert_unicode
+
+ if not assert_unicode:
+ return None
+
+ def process(value):
+ if not isinstance(value, (unicode, sqltypes.NoneType)):
+ if assert_unicode == 'warn':
+ util.warn("Unicode type received non-unicode bind "
+ "param value %r" % value)
+ return value
+ else:
+ raise exc.InvalidRequestError("Unicode type received non-unicode bind param value %r" % value)
+ else:
+ return value
+ return process
+ else:
+ return None
+
+ def result_processor(self, dialect):
+ return None
+
class MSNumeric(sqltypes.Numeric):
def result_processor(self, dialect):
return self._extend("NVARCHAR")
-class AdoMSNVarchar(_StringType, sqltypes.Unicode):
- """overrides bindparam/result processing to not convert any unicode strings"""
-
- def __init__(self, length=None, **kwargs):
- """Construct a NVARCHAR.
-
- :param length: Optional, Maximum data length, in characters.
-
- :param collation: Optional, a column-level collation for this string
- value. Accepts a Windows Collation Name or a SQL Collation Name.
-
- """
- _StringType.__init__(self, **kwargs)
- sqltypes.Unicode.__init__(self, length=length,
- convert_unicode=kwargs.get('convert_unicode', True),
- assert_unicode=kwargs.get('assert_unicode', 'warn'))
-
- def bind_processor(self, dialect):
- return None
-
- def result_processor(self, dialect):
- return None
-
- def get_col_spec(self):
- if self.length:
- return self._extend("NVARCHAR(%(length)s)" % {'length' : self.length})
- else:
- return self._extend("NVARCHAR")
-
-
class MSChar(_StringType, sqltypes.CHAR):
"""MSSQL CHAR type, for fixed-length non-Unicode data with a maximum
of 8,000 characters."""
coltype = self.ischema_names.get(type, None)
kwargs = {}
- if coltype in (MSString, MSChar, MSNVarchar, AdoMSNVarchar, MSNChar, MSText, MSNText):
+ if coltype in (MSString, MSChar, MSNVarchar, MSNChar, MSText, MSNText):
if collation:
kwargs.update(collation=collation)
(type, name))
coltype = sqltypes.NULLTYPE
- elif coltype in (MSNVarchar, AdoMSNVarchar) and charlen == -1:
+ elif coltype in (MSNVarchar,) and charlen == -1:
args[0] = None
coltype = coltype(*args, **kwargs)
colargs = []
ischema_names = MSSQLDialect.ischema_names.copy()
ischema_names['smalldatetime'] = MSDate_pyodbc
ischema_names['datetime'] = MSDateTime_pyodbc
- if supports_unicode:
- colspecs[sqltypes.Unicode] = AdoMSNVarchar
- ischema_names['nvarchar'] = AdoMSNVarchar
def make_connect_string(self, keys, query):
if 'max_identifier_length' in keys:
return module
colspecs = MSSQLDialect.colspecs.copy()
- colspecs[sqltypes.Unicode] = AdoMSNVarchar
colspecs[sqltypes.DateTime] = MSDateTime_adodbapi
ischema_names = MSSQLDialect.ischema_names.copy()
- ischema_names['nvarchar'] = AdoMSNVarchar
ischema_names['datetime'] = MSDateTime_adodbapi
def make_connect_string(self, keys, query):
testing.db.engine.dialect.assert_unicode = False
rawdata = 'Alors vous imaginez ma surprise, au lever du jour, quand une dr\xc3\xb4le de petit voix m\xe2\x80\x99a r\xc3\xa9veill\xc3\xa9. Elle disait: \xc2\xab S\xe2\x80\x99il vous pla\xc3\xaet\xe2\x80\xa6 dessine-moi un mouton! \xc2\xbb\n'
unicodedata = rawdata.decode('utf-8')
- if testing.against('sqlite'):
+ if testing.against('sqlite', 'mssql'):
rawdata = "something"
unicode_table.insert().execute(unicode_varchar=unicodedata,
unicode_text=unicodedata,
print 3, repr(x['plain_varchar'])
self.assert_(isinstance(x['unicode_varchar'], unicode) and x['unicode_varchar'] == unicodedata)
self.assert_(isinstance(x['unicode_text'], unicode) and x['unicode_text'] == unicodedata)
- if not testing.against('sqlite'):
+ if not testing.against('sqlite', 'mssql'):
self.assert_(isinstance(x['plain_varchar'], unicode) and x['plain_varchar'] == unicodedata)
finally:
testing.db.engine.dialect.convert_unicode = prev_unicode