Author: phd
Date: Sun Apr 28 04:50:18 2013
New Revision: 4585
Log:
NCHAR/NVARCHAR and N''-quoted strings for MS SQL [closes bugs 277, 278, 279]
Modified:
SQLObject/trunk/docs/News.txt
SQLObject/trunk/sqlobject/col.py
Modified: SQLObject/trunk/docs/News.txt
==============================================================================
--- SQLObject/trunk/docs/News.txt Sat Apr 20 04:24:27 2013 (r4584)
+++ SQLObject/trunk/docs/News.txt Sun Apr 28 04:50:18 2013 (r4585)
@@ -22,6 +22,8 @@
* Changed the way to get if the table has identity in MS SQL.
+* NCHAR/NVARCHAR and N''-quoted strings for MS SQL.
+
SQLObject 1.3.2
===============
Modified: SQLObject/trunk/sqlobject/col.py
==============================================================================
--- SQLObject/trunk/sqlobject/col.py Sat Apr 20 04:24:27 2013 (r4584)
+++ SQLObject/trunk/sqlobject/col.py Sun Apr 28 04:50:18 2013 (r4585)
@@ -474,7 +474,7 @@
if self.connection and self.connection.can_use_max_types():
type = 'VARCHAR(MAX)'
else:
- type = 'varchar(4000)'
+ type = 'VARCHAR(4000)'
elif self.varchar:
type = 'VARCHAR(%i)' % self.length
else:
@@ -533,6 +533,17 @@
class StringCol(Col):
baseClass = SOStringCol
+
+class NQuoted(sqlbuilder.SQLExpression):
+ def __init__(self, value):
+ assert isinstance(value, unicode)
+ self.value = value
+ def __hash__(self):
+ return hash(self.value)
+ def __sqlrepr__(self, db):
+ assert db == 'mssql'
+ return "N" + sqlbuilder.sqlrepr(self.value, db)
+
class UnicodeStringValidator(validators.Validator):
def getDbEncoding(self, state):
@@ -561,6 +572,13 @@
if isinstance(value, (str, sqlbuilder.SQLExpression)):
return value
if isinstance(value, unicode):
+ try:
+ connection = state.connection or state.soObject._connection
+ except AttributeError:
+ pass
+ else:
+ if connection.dbName == 'mssql':
+ return NQuoted(value)
return value.encode(self.getDbEncoding(state))
if hasattr(value, '__unicode__'):
return unicode(value).encode(self.getDbEncoding(state))
@@ -572,6 +590,11 @@
self.dbEncoding = kw.pop('dbEncoding', None)
super(SOUnicodeCol, self).__init__(**kw)
+ def _mssqlType(self):
+ if self.customSQLType is not None:
+ return self.customSQLType
+ return 'N' + super(SOUnicodeCol, self)._mssqlType()
+
def createValidators(self):
return [UnicodeStringValidator(name=self.name, soCol=self)] + \
super(SOUnicodeCol, self).createValidators()
|