]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- String's (and Unicode's, UnicodeText's, etc.) convert_unicode rel_0_5rc2
authorMike Bayer <mike_mp@zzzcomputing.com>
Sun, 12 Oct 2008 14:39:20 +0000 (14:39 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sun, 12 Oct 2008 14:39:20 +0000 (14:39 +0000)
logic disabled in the sqlite dialect, to adjust for pysqlite
2.5.0's new requirement that only Python unicode objects are
accepted;
http://itsystementwicklung.de/pipermail/list-pysqlite/2008-March/000018.html

CHANGES
lib/sqlalchemy/databases/sqlite.py
test/dialect/sqlite.py

diff --git a/CHANGES b/CHANGES
index 22e96cafc00f664d6497162bff5eb9d48b2f6d41..19c2d40b377eb62ec0307b6090541fa5aba1009e 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -71,6 +71,12 @@ CHANGES
       strptime/strftime, to generically support pre-1900 dates,
       dates with microseconds.  [ticket:968]
 
+    - String's (and Unicode's, UnicodeText's, etc.) convert_unicode 
+      logic disabled in the sqlite dialect, to adjust for pysqlite 
+      2.5.0's new requirement that only Python unicode objects are 
+      accepted;
+      http://itsystementwicklung.de/pipermail/list-pysqlite/2008-March/000018.html
+      
 - mysql
     - Temporary tables are now reflectable.
 
index 6689db8f1e320544ba41d23db092fcbc0a66ae86..35f07b784ff43241f947861a6deb9ab9eddab186 100644 (file)
@@ -130,15 +130,43 @@ class SLTime(DateTimeMixin, sqltypes.Time):
     def result_processor(self, dialect):
         return self._result_processor(datetime.time, self._reg)
 
-class SLText(sqltypes.Text):
+class SLUnicodeMixin(object):
+    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, 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 SLText(SLUnicodeMixin, sqltypes.Text):
     def get_col_spec(self):
         return "TEXT"
 
-class SLString(sqltypes.String):
+class SLString(SLUnicodeMixin, sqltypes.String):
     def get_col_spec(self):
         return "VARCHAR" + (self.length and "(%d)" % self.length or "")
 
-class SLChar(sqltypes.CHAR):
+class SLChar(SLUnicodeMixin, sqltypes.CHAR):
     def get_col_spec(self):
         return "CHAR" + (self.length and "(%d)" % self.length or "")
 
index 2c5f32ff92c4271abf94b08433cc34c66607db63..bc6891ac63acfa99e0fe37c08562bb935ad41b88 100644 (file)
@@ -28,6 +28,24 @@ class TestTypes(TestBase, AssertsExecutionResults):
         bp = sldt.bind_processor(None)
         self.assertEquals(bp(dt), '2008-06-27 12:00:00.125')
         self.assertEquals(rp(bp(dt)), dt)
+
+    def test_no_convert_unicode(self):
+        """test no utf-8 encoding occurs"""
+        
+        dialect = sqlite.dialect()
+        for t in (
+                String(convert_unicode=True),
+                CHAR(convert_unicode=True),
+                Unicode(),
+                UnicodeText(),
+                String(assert_unicode=True, convert_unicode=True),
+                CHAR(assert_unicode=True, convert_unicode=True),
+                Unicode(assert_unicode=True),
+                UnicodeText(assert_unicode=True)
+            ):
+
+            bindproc = t.dialect_impl(dialect).bind_processor(dialect)
+            assert not bindproc or isinstance(bindproc(u"some string"), unicode)
         
     @testing.uses_deprecated('Using String type with no length')
     def test_type_reflection(self):