Author: ianb
Date: 2004-12-07 17:28:19 +0000 (Tue, 07 Dec 2004)
New Revision: 462
Modified:
trunk/SQLObject/docs/News.txt
trunk/SQLObject/docs/SQLObject.txt
trunk/SQLObject/sqlobject/col.py
trunk/SQLObject/tests/test_sqlobject.py
Log:
Added a UnicodeCol
Modified: trunk/SQLObject/docs/News.txt
===================================================================
--- trunk/SQLObject/docs/News.txt 2004-12-07 17:27:40 UTC (rev 461)
+++ trunk/SQLObject/docs/News.txt 2004-12-07 17:28:19 UTC (rev 462)
@@ -7,6 +7,20 @@
.. _start:
+SQLObject 0.6.2
+===============
+
+Interface Changes
+-----------------
+
+* The long broken and unused ``DBMConnection`` has been removed.
+
+Features
+--------
+
+* New ``UnicodeCol()`` that converts to and from Unicode
+ in the database.
+
SQLObject 0.6.1
===============
Modified: trunk/SQLObject/docs/SQLObject.txt
===================================================================
--- trunk/SQLObject/docs/SQLObject.txt 2004-12-07 17:27:40 UTC (rev 461)
+++ trunk/SQLObject/docs/SQLObject.txt 2004-12-07 17:28:19 UTC (rev 462)
@@ -612,26 +612,34 @@
Will create a ``BOOLEAN`` column in Postgres, or ``INT`` in other
databses. It will also convert values to ``"t"/"f"`` or ``0/1``
according to the database backend.
+
`CurrencyCol`:
Equivalent to ``DecimalCol(size=10, precision=2)``.
+
`DateTimeCol`:
A date and time (usually returned as an mxDateTime object).
+
`DecimalCol`:
Base-10, precise number. Uses the keyword arguments `size` for
number of digits stored, and `precision` for the number of digits
after the decimal point.
+
`EnumCol`:
One of several string values -- give the possible strings as a
list, with the `enumValues` keyword argument. MySQL has a native
``ENUM`` type, but will work with other databases too (storage
just won't be as efficient).
+
`FloatCol`:
Floats.
+
`ForeignKey`:
A key to another table/class. Use like ``user =
ForeignKey('User')``.
+
`IntCol`:
Integers.
+
`StringCol`:
A string (character) column. Extra keywords:
@@ -643,6 +651,15 @@
``CHAR`` and ``VARCHAR``, default True, i.e., use
``VARCHAR``.
+`UnicodeCol`:
+ A subclass of `StringCol`. Also accepts a dbEncoding keyword
+ argument, which defaults to ``"UTF-8"``. Values coming in and
+ out from the database will be encoded and decoded. **Note**:
+ parameters in queries will not be automatically encoded, so if
+ you do a query matching a UnicodeCol column you must apply the
+ encoding yourself.
+
+
SQLObject Class: Specifying Your Class
--------------------------------------
Modified: trunk/SQLObject/sqlobject/col.py
===================================================================
--- trunk/SQLObject/sqlobject/col.py 2004-12-07 17:27:40 UTC (rev 461)
+++ trunk/SQLObject/sqlobject/col.py 2004-12-07 17:28:19 UTC (rev 462)
@@ -338,6 +338,28 @@
class StringCol(Col):
baseClass = SOStringCol
+class UnicodeStringValidator(validators.Validator):
+
+ def __init__(self, db_encoding='UTF-8'):
+ self.db_encoding = db_encoding
+
+ def fromPython(self, value, state):
+ return value.encode(self.db_encoding)
+
+ def toPython(self, value, state):
+ return unicode(value, self.db_encoding)
+
+class SOUnicodeCol(SOStringCol):
+
+ def __init__(self, **kw):
+ self.dbEncoding = popKey(kw, 'dbEncoding', 'UTF-8')
+ SOStringCol.__init__(self, **kw)
+ self.validator = validators.All.join(
+ UnicodeStringValidator(self.dbEncoding), self.validator)
+
+class UnicodeCol(Col):
+ baseClass = SOUnicodeCol
+
class SOIntCol(SOCol):
# 3-03 @@: support precision, maybe max and min directly
Modified: trunk/SQLObject/tests/test_sqlobject.py
===================================================================
--- trunk/SQLObject/tests/test_sqlobject.py 2004-12-07 17:27:40 UTC (rev 461)
+++ trunk/SQLObject/tests/test_sqlobject.py 2004-12-07 17:28:19 UTC (rev 462)
@@ -1254,6 +1254,39 @@
{0: 1, 1:1})
########################################
+## Unicode columns
+########################################
+
+class Unicode1(SQLObject):
+ count = IntCol(alternateID=True)
+ col1 = UnicodeCol()
+ col2 = UnicodeCol(dbEncoding='latin-1')
+
+class UnicodeTest(SQLObjectTest):
+
+ classes = [Unicode1]
+
+ data = [u'\u00f0', u'test', 'ascii test']
+
+ def testCreate(self):
+ items = []
+ for i, n in enumerate(self.data):
+ items.append(Unicode1(count=i, col1=n, col2=n))
+ for n, item in zip(self.data, items):
+ item.col1 = item.col2 = n
+ for n, item in zip(self.data, items):
+ self.assertEqual(item.col1, item.col2, n)
+ conn = Unicode1._connection
+ rows = conn.queryAll("""
+ SELECT count, col1, col2
+ FROM unicode1
+ ORDER BY count
+ """)
+ for count, col1, col2 in rows:
+ self.assertEqual(self.data[count].encode('utf-8'), col1)
+ self.assertEqual(self.data[count].encode('latin1'), col2)
+
+########################################
## Run from command-line:
########################################
|