Fixed #227 -- the sqlite backend now correctly typecasts unicode objects to bytestrings (thanks hugo)
git-svn-id: http://code.djangoproject.com/svn/django/trunk@383 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
e73c734336
commit
1510ca1a80
|
@ -16,6 +16,14 @@ Database.register_converter("datetime", typecasts.typecast_timestamp)
|
||||||
|
|
||||||
# Database wrapper ############################################################
|
# Database wrapper ############################################################
|
||||||
|
|
||||||
|
def utf8rowFactory(cursor, row):
|
||||||
|
def utf8(s):
|
||||||
|
if type(s) == unicode:
|
||||||
|
return s.encode("utf-8")
|
||||||
|
else:
|
||||||
|
return s
|
||||||
|
return [utf8(r) for r in row]
|
||||||
|
|
||||||
class DatabaseWrapper:
|
class DatabaseWrapper:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.connection = None
|
self.connection = None
|
||||||
|
@ -28,9 +36,12 @@ class DatabaseWrapper:
|
||||||
# register extract and date_trun functions
|
# register extract and date_trun functions
|
||||||
self.connection.create_function("django_extract", 2, _sqlite_extract)
|
self.connection.create_function("django_extract", 2, _sqlite_extract)
|
||||||
self.connection.create_function("django_date_trunc", 2, _sqlite_date_trunc)
|
self.connection.create_function("django_date_trunc", 2, _sqlite_date_trunc)
|
||||||
|
cursor = self.connection.cursor(factory=SQLiteCursorWrapper)
|
||||||
|
cursor.row_factory = utf8rowFactory
|
||||||
if DEBUG:
|
if DEBUG:
|
||||||
return base.CursorDebugWrapper(FormatStylePlaceholderCursor(self.connection), self)
|
return base.CursorDebugWrapper(cursor, self)
|
||||||
return FormatStylePlaceholderCursor(self.connection)
|
else:
|
||||||
|
return cursor
|
||||||
|
|
||||||
def commit(self):
|
def commit(self):
|
||||||
self.connection.commit()
|
self.connection.commit()
|
||||||
|
@ -44,7 +55,7 @@ class DatabaseWrapper:
|
||||||
self.connection.close()
|
self.connection.close()
|
||||||
self.connection = None
|
self.connection = None
|
||||||
|
|
||||||
class FormatStylePlaceholderCursor(Database.Cursor):
|
class SQLiteCursorWrapper(Database.Cursor):
|
||||||
"""
|
"""
|
||||||
Django uses "format" style placeholders, but pysqlite2 uses "qmark" style.
|
Django uses "format" style placeholders, but pysqlite2 uses "qmark" style.
|
||||||
This fixes it -- but note that if you want to use a literal "%s" in a query,
|
This fixes it -- but note that if you want to use a literal "%s" in a query,
|
||||||
|
|
Loading…
Reference in New Issue