Author: phd
Date: 2006-09-04 09:17:56 -0600 (Mon, 04 Sep 2006)
New Revision: 1899
Modified:
home/phd/SQLObject/paramstyles/sqlobject/dbconnection.py
home/phd/SQLObject/paramstyles/sqlobject/main.py
home/phd/SQLObject/paramstyles/sqlobject/sqlbuilder.py
home/phd/SQLObject/paramstyles/sqlobject/tests/test_unicode.py
Log:
Merged patches from the revisions 1890:1895 from the trunk
Modified: home/phd/SQLObject/paramstyles/sqlobject/dbconnection.py
===================================================================
--- home/phd/SQLObject/paramstyles/sqlobject/dbconnection.py 2006-09-04 15:15:00 UTC (rev 1898)
+++ home/phd/SQLObject/paramstyles/sqlobject/dbconnection.py 2006-09-04 15:17:56 UTC (rev 1899)
@@ -96,7 +96,8 @@
else:
host, rest = rest.split('/', 1)
if host and host.find('@') != -1:
- user, host = host.rsplit('@', 1)
+ user = host[:host.rfind('@')] # Python 2.3 doesn't have .rsplit()
+ host = host[host.rfind('@')+1:] # !!!
if user.find(':') != -1:
user, password = user.split(':', 1)
else:
Modified: home/phd/SQLObject/paramstyles/sqlobject/main.py
===================================================================
--- home/phd/SQLObject/paramstyles/sqlobject/main.py 2006-09-04 15:15:00 UTC (rev 1898)
+++ home/phd/SQLObject/paramstyles/sqlobject/main.py 2006-09-04 15:17:56 UTC (rev 1899)
@@ -1328,6 +1328,11 @@
select = classmethod(select)
def selectBy(cls, connection=None, **kw):
+ for key, column in cls.sqlmeta.columns.items():
+ if (key in kw) and isinstance(column, col.SOUnicodeCol):
+ value = kw[key]
+ if isinstance(value, unicode):
+ kw[key] = value.encode(column.dbEncoding)
conn = connection or cls._connection
return cls.SelectResultsClass(cls,
conn._SO_columnClause(cls, kw),
Modified: home/phd/SQLObject/paramstyles/sqlobject/sqlbuilder.py
===================================================================
--- home/phd/SQLObject/paramstyles/sqlobject/sqlbuilder.py 2006-09-04 15:15:00 UTC (rev 1898)
+++ home/phd/SQLObject/paramstyles/sqlobject/sqlbuilder.py 2006-09-04 15:17:56 UTC (rev 1899)
@@ -363,6 +363,23 @@
registerConverter(SQLObjectField, SQLExprConverter)
+
+class UnicodeField(SQLObjectField):
+ def __init__(self, tableName, fieldName, original, column):
+ SQLObjectField.__init__(self, tableName, fieldName, original)
+ self.column = column
+ def __eq__(self, other):
+ if isinstance(other, unicode):
+ other = other.encode(self.column.dbEncoding)
+ return SQLOp('=', self, other)
+ def __ne__(self, other):
+ if isinstance(other, unicode):
+ other = other.encode(self.column.dbEncoding)
+ return SQLOp('<>', self, other)
+
+registerConverter(UnicodeField, SQLExprConverter)
+
+
class Table(SQLExpression, SQLListMixin):
FieldClass = Field
@@ -379,6 +396,7 @@
class SQLObjectTable(Table):
FieldClass = SQLObjectField
+ UnicodeFieldClass = UnicodeField
def __init__(self, soClass):
self.soClass = soClass
@@ -395,9 +413,12 @@
elif attr not in self.soClass.sqlmeta.columns:
raise AttributeError("%s instance has no attribute '%s'" % (self.soClass.__name__, attr))
else:
- return self.FieldClass(self.tableName,
- self.soClass.sqlmeta.columns[attr].dbName,
- attr)
+ column = self.soClass.sqlmeta.columns[attr]
+ if hasattr(column, "dbEncoding"):
+ return self.UnicodeFieldClass(self.tableName, column.dbName,
+ attr, column)
+ else:
+ return self.FieldClass(self.tableName, column.dbName, attr)
class TableSpace:
TableClass = Table
Modified: home/phd/SQLObject/paramstyles/sqlobject/tests/test_unicode.py
===================================================================
--- home/phd/SQLObject/paramstyles/sqlobject/tests/test_unicode.py 2006-09-04 15:15:00 UTC (rev 1898)
+++ home/phd/SQLObject/paramstyles/sqlobject/tests/test_unicode.py 2006-09-04 15:17:56 UTC (rev 1899)
@@ -5,7 +5,7 @@
## Unicode columns
########################################
-class Unicode1(SQLObject):
+class TestUnicode(SQLObject):
count = IntCol(alternateID=True)
col1 = UnicodeCol()
col2 = UnicodeCol(dbEncoding='latin-1')
@@ -16,24 +16,54 @@
def enumerate(lst):
return [(i, lst[i]) for i in range(len(lst))]
-def test_create():
- setupClass(Unicode1)
- data = [u'\u00f0', u'test', 'ascii test']
+data = [u'\u00f0', u'test', 'ascii test']
+items = []
+def setup():
+ global items
items = []
+ setupClass(TestUnicode)
for i, n in enumerate(data):
- items.append(Unicode1(count=i, col1=n, col2=n))
+ items.append(TestUnicode(count=i, col1=n, col2=n))
+
+def test_create():
+ setup()
for n, item in zip(data, items):
- item.col1 = item.col2 = n
- for n, item in zip(data, items):
assert item.col1 == item.col2
assert item.col1 == n
- conn = Unicode1._connection
+
+ conn = TestUnicode._connection
rows = conn.queryAll("""
SELECT count, col1, col2
- FROM unicode1
+ FROM test_unicode
ORDER BY count
""")
for count, col1, col2 in rows:
assert data[count].encode('utf-8') == col1
assert data[count].encode('latin1') == col2
+
+def test_select():
+ setup()
+ for value in data:
+ rows = list(TestUnicode.select(TestUnicode.q.col1 == value))
+ assert len(rows) == 1
+ rows = list(TestUnicode.select(TestUnicode.q.col2 == value))
+ assert len(rows) == 1
+ rows = list(TestUnicode.select(AND(
+ TestUnicode.q.col1 == value,
+ TestUnicode.q.col2 == value
+ )))
+ assert len(rows) == 1
+ rows = list(TestUnicode.selectBy(col1=value))
+ assert len(rows) == 1
+ rows = list(TestUnicode.selectBy(col2=value))
+ assert len(rows) == 1
+ rows = list(TestUnicode.selectBy(col1=value, col2=value))
+ assert len(rows) == 1
+ rows = list(TestUnicode.select(OR(
+ TestUnicode.q.col1 == u'\u00f0',
+ TestUnicode.q.col2 == u'test'
+ )))
+ assert len(rows) == 2
+ rows = list(TestUnicode.selectBy(col1=u'\u00f0', col2=u'test'))
+ assert len(rows) == 0
|