|
From: Ian B. <ia...@co...> - 2003-11-09 18:25:41
|
On Nov 9, 2003, at 8:32 AM, John Baker wrote:
> Hello,
>
> Does SQLObject have any kind of Unicode support? We seem unable to
> find any,
> and we've put a few hacks in there to encode Strings as utf-8 before
> they are
> placed into the database, and decodes them after they come out. It's
> nothing
> special, but I do feel SQLObject is lacking something here.
No, there's no specific Unicode support, mostly because I'm not sure
how they should best be encoded -- if they should be UTF-8 encoded, if
databases have unicode literals, etc.
If you wanted to make a validator/converter to do UTF-8 encoding, it
might look like:
from SQLObject.include import Validator
class UTF8Validator(Validator.Validator):
def fromPython(self, v, state):
# test isinstance(v, unicode)?
return v.encode('UTF-8') # strict, loose?
def toPython(self, v, state):
return v.decode('UTF-8')
You could then use something like:
class MyThing(SQLObject):
unicodeColumn = String(validator=UTFValidator())
I'm not entirely sure about all the details, but this pattern could
probably be added to Col.py, and show up either as a subclass of
String, or something that could be enabled with a keyword argument.
(In that case it probably would be good to include other encodings,
especially for legacy databases that don't use UTF-8, and perhaps some
clearer handling of strict vs. loose and how to handle non-unicode
strings)
--
Ian Bicking | ia...@co... | http://blog.ianbicking.org
|