Author: phd
Date: 2006-09-30 07:29:02 -0600 (Sat, 30 Sep 2006)
New Revision: 1961
Modified:
SQLObject/branches/0.7-bugfix/sqlobject/col.py
SQLObject/branches/0.7-bugfix/sqlobject/mysql/mysqlconnection.py
Log:
Store the charset value from the URI as dbEncoding; sent SET NAMES query using dbEncoding;
use dbEncoding instead of "ascii" to convert from unicode from MySQLdb.
Modified: SQLObject/branches/0.7-bugfix/sqlobject/col.py
===================================================================
--- SQLObject/branches/0.7-bugfix/sqlobject/col.py 2006-09-29 16:03:32 UTC (rev 1960)
+++ SQLObject/branches/0.7-bugfix/sqlobject/col.py 2006-09-30 13:29:02 UTC (rev 1961)
@@ -510,7 +510,9 @@
if value is None:
return None
if isinstance(value, unicode):
- return value.encode("ascii")
+ connection = state.soObject._connection
+ dbEncoding = getattr(connection, "dbEncoding", None) or "ascii"
+ return value.encode(dbEncoding)
return value
def from_python(self, value, state):
@@ -1195,9 +1197,9 @@
if value is None:
return None
if isinstance(value, str):
- module = state.soObject._connection.module
- if module.__name__ in ("sqlite", "pysqlite2.dbapi2"):
- value = module.decode(value)
+ connection = state.soObject._connection
+ if connection.dbName == "sqlite":
+ value = connection.module.decode(value)
return value
if isinstance(value, (buffer_type, state.soObject._connection._binaryType)):
cachedValue = self._cachedValue
@@ -1256,7 +1258,9 @@
if value is None:
return None
if isinstance(value, unicode):
- value = value.encode("ascii")
+ connection = state.soObject._connection
+ dbEncoding = getattr(connection, "dbEncoding", None) or "ascii"
+ value = value.encode(dbEncoding)
if isinstance(value, str):
return pickle.loads(value)
raise validators.Invalid("expected a pickle string in the PickleCol '%s', got %s %r instead" % \
Modified: SQLObject/branches/0.7-bugfix/sqlobject/mysql/mysqlconnection.py
===================================================================
--- SQLObject/branches/0.7-bugfix/sqlobject/mysql/mysqlconnection.py 2006-09-29 16:03:32 UTC (rev 1960)
+++ SQLObject/branches/0.7-bugfix/sqlobject/mysql/mysqlconnection.py 2006-09-30 13:29:02 UTC (rev 1961)
@@ -26,13 +26,17 @@
else:
self.need_unicode = False
for key in ("unix_socket", "init_command",
- "read_default_file", "read_default_group", "charset"):
+ "read_default_file", "read_default_group"):
if key in kw:
self.kw[key] = col.popKey(kw, key)
for key in ("connect_timeout", "compress", "named_pipe", "use_unicode",
"client_flag", "local_infile"):
if key in kw:
self.kw[key] = int(col.popKey(kw, key))
+ if "charset" in kw:
+ self.dbEncoding = self.kw["charset"] = col.popKey(kw, "charset")
+ else:
+ self.dbEncoding = None
if "sqlobject_encoding" in kw:
self.encoding = col.popKey(kw, "sqlobject_encoding")
else:
@@ -57,6 +61,8 @@
if hasattr(conn, 'autocommit'):
conn.autocommit(bool(self.autoCommit))
+ if self.dbEncoding:
+ conn.query('SET NAMES ' + self.dbEncoding)
return conn
|