]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- String's (and Unicode's, UnicodeText's, etc.) convert_unicode rel_0_4_8
authorMike Bayer <mike_mp@zzzcomputing.com>
Sun, 12 Oct 2008 14:39:11 +0000 (14:39 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sun, 12 Oct 2008 14:39:11 +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 433f812ad134a3ae72b53322cd034894b48bc4ff..62077572bb34d5fec9ab59e94d3982f0a14a42cb 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -27,6 +27,12 @@ CHANGES
 - sqlite
     - Supplied a custom strftime() function which
       handles dates before 1900.  [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
       
 - oracle
     - has_sequence() now takes schema name into account
index aef819d0c7de14292a261e91b5475f25b973a44b..04f59a8398a2a64d682ca7bb9d8cf88df4de03b3 100644 (file)
@@ -126,15 +126,43 @@ class SLTime(DateTimeMixin, sqltypes.Time):
             return tup and datetime.time(*tup[3:7])
         return process
 
-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, sqltypes.NoneType)):
+                    if assert_unicode == 'warn':
+                        util.warn("Unicode type received non-unicode bind "
+                                  "param value %r" % value)
+                        return value
+                    else:
+                        raise exceptions.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(%(length)s)" % {'length' : self.length}
 
-class SLChar(sqltypes.CHAR):
+class SLChar(SLUnicodeMixin, sqltypes.CHAR):
     def get_col_spec(self):
         return "CHAR(%(length)s)" % {'length' : self.length}
 
index 91cd2c254bff62ba99cbb3f8a652254cc34808cc..eb9ecabc18de025c1d7d641d0ea39ed445eb8e5a 100644 (file)
@@ -46,6 +46,25 @@ class TestTypes(TestBase, AssertsExecutionResults):
         self.assertEquals(bp(dt), '2008-06-27 12:00:00.000125')
         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)
+        
         
     def test_type_reflection(self):
         # (ask_for, roundtripped_as_if_different)