[Sqlalchemy-commits] [1068] sqlalchemy/trunk/test: added convert_unicode flag to engine + unittest,
Brought to you by:
zzzeek
From: <co...@sq...> - 2006-03-01 00:08:12
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head><style type="text/css"><!-- #msg dl { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; } #msg dt { float: left; width: 6em; font-weight: bold; } #msg dt:after { content:':';} #msg dl, #msg dt, #msg ul, #msg li { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; } #msg dl a { font-weight: bold} #msg dl a:link { color:#fc3; } #msg dl a:active { color:#ff0; } #msg dl a:visited { color:#cc6; } h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; } #msg pre { overflow: auto; background: #ffc; border: 1px #fc0 solid; padding: 6px; } #msg ul, pre { overflow: auto; } #patch { width: 100%; } #patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;} #patch .propset h4, #patch .binary h4 {margin:0;} #patch pre {padding:0;line-height:1.2em;margin:0;} #patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;} #patch .propset .diff, #patch .binary .diff {padding:10px 0;} #patch span {display:block;padding:0 10px;} #patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;} #patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;} #patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;} #patch .lines, .info {color:#888;background:#fff;} --></style> <title>[1068] sqlalchemy/trunk/test: added convert_unicode flag to engine + unittest, does unicode in/out translation on all string/char values when set</title> </head> <body> <div id="msg"> <dl> <dt>Revision</dt> <dd>1068</dd> <dt>Author</dt> <dd>zzzeek</dd> <dt>Date</dt> <dd>2006-02-28 18:08:01 -0600 (Tue, 28 Feb 2006)</dd> </dl> <h3>Log Message</h3> <pre>added convert_unicode flag to engine + unittest, does unicode in/out translation on all string/char values when set</pre> <h3>Modified Paths</h3> <ul> <li><a href="#sqlalchemytrunkdocbuildcontentdbenginemyt">sqlalchemy/trunk/doc/build/content/dbengine.myt</a></li> <li><a href="#sqlalchemytrunklibsqlalchemydatabasespostgrespy">sqlalchemy/trunk/lib/sqlalchemy/databases/postgres.py</a></li> <li><a href="#sqlalchemytrunklibsqlalchemyenginepy">sqlalchemy/trunk/lib/sqlalchemy/engine.py</a></li> <li><a href="#sqlalchemytrunklibsqlalchemytypespy">sqlalchemy/trunk/lib/sqlalchemy/types.py</a></li> <li><a href="#sqlalchemytrunktesttesttypespy">sqlalchemy/trunk/test/testtypes.py</a></li> </ul> </div> <div id="patch"> <h3>Diff</h3> <a id="sqlalchemytrunkdocbuildcontentdbenginemyt"></a> <div class="modfile"><h4>Modified: sqlalchemy/trunk/doc/build/content/dbengine.myt (1067 => 1068)</h4> <pre class="diff"><span> <span class="info">--- sqlalchemy/trunk/doc/build/content/dbengine.myt 2006-02-28 23:33:15 UTC (rev 1067) +++ sqlalchemy/trunk/doc/build/content/dbengine.myt 2006-03-01 00:08:01 UTC (rev 1068) </span><span class="lines">@@ -102,6 +102,7 @@ </span><span class="cx"> <li>default_ordering=False : if True, table objects and associated joins and aliases will generate information used for ordering by primary keys (or OIDs, if the database supports OIDs). This information is used by the Mapper system to when it constructs select queries to supply a default ordering to mapped objects.</li> </span><span class="cx"> <li>use_ansi=True : used only by Oracle; when False, the Oracle driver attempts to support a particular "quirk" of some Oracle databases, that the LEFT OUTER JOIN SQL syntax is not supported, and the "Oracle join" syntax of using <% "<column1>(+)=<column2>" |h%> must be used in order to achieve a LEFT OUTER JOIN. Its advised that the Oracle database be configured to have full ANSI support instead of using this feature.</li> </span><span class="cx"> <li>use_oids=False : used only by Postgres, will enable the column name "oid" as the object ID column. Postgres as of 8.1 has object IDs disabled by default.</li> </span><ins>+ <li>convert_unicode=False : if set to True, all String/character based types will convert Unicode values to raw byte values going into the database, and all raw byte values to Python Unicode coming out in result sets. This is an engine-wide method to provide unicode across the board. For unicode conversion on a column-by-column level, use the Unicode column type instead.</li> </ins><span class="cx"> </ul> </span><span class="cx"> </&> </span><span class="cx"> <&|doclib.myt:item, name="proxy", description="Using the Proxy Engine" &> </span></span></pre></div> <a id="sqlalchemytrunklibsqlalchemydatabasespostgrespy"></a> <div class="modfile"><h4>Modified: sqlalchemy/trunk/lib/sqlalchemy/databases/postgres.py (1067 => 1068)</h4> <pre class="diff"><span> <span class="info">--- sqlalchemy/trunk/lib/sqlalchemy/databases/postgres.py 2006-02-28 23:33:15 UTC (rev 1067) +++ sqlalchemy/trunk/lib/sqlalchemy/databases/postgres.py 2006-03-01 00:08:01 UTC (rev 1068) </span><span class="lines">@@ -85,6 +85,7 @@ </span><span class="cx"> return value </span><span class="cx"> def get_col_spec(self): </span><span class="cx"> return "TIME" </span><ins>+ </ins><span class="cx"> class PGText(sqltypes.TEXT): </span><span class="cx"> def get_col_spec(self): </span><span class="cx"> return "TEXT" </span></span></pre></div> <a id="sqlalchemytrunklibsqlalchemyenginepy"></a> <div class="modfile"><h4>Modified: sqlalchemy/trunk/lib/sqlalchemy/engine.py (1067 => 1068)</h4> <pre class="diff"><span> <span class="info">--- sqlalchemy/trunk/lib/sqlalchemy/engine.py 2006-02-28 23:33:15 UTC (rev 1067) +++ sqlalchemy/trunk/lib/sqlalchemy/engine.py 2006-03-01 00:08:01 UTC (rev 1068) </span><span class="lines">@@ -166,7 +166,7 @@ </span><span class="cx"> SQLEngines are constructed via the create_engine() function inside this package. </span><span class="cx"> """ </span><span class="cx"> </span><del>- def __init__(self, pool=None, echo=False, logger=None, default_ordering=False, echo_pool=False, echo_uow=False, **params): </del><ins>+ def __init__(self, pool=None, echo=False, logger=None, default_ordering=False, echo_pool=False, echo_uow=False, convert_unicode=False, **params): </ins><span class="cx"> """constructs a new SQLEngine. SQLEngines should be constructed via the create_engine() </span><span class="cx"> function which will construct the appropriate subclass of SQLEngine.""" </span><span class="cx"> # get a handle on the connection pool via the connect arguments </span><span class="lines">@@ -181,6 +181,7 @@ </span><span class="cx"> self.default_ordering=default_ordering </span><span class="cx"> self.echo = echo </span><span class="cx"> self.echo_uow = echo_uow </span><ins>+ self.convert_unicode = convert_unicode </ins><span class="cx"> self.context = util.ThreadLocal(raiseerror=False) </span><span class="cx"> self.tables = {} </span><span class="cx"> self._ischema = None </span></span></pre></div> <a id="sqlalchemytrunklibsqlalchemytypespy"></a> <div class="modfile"><h4>Modified: sqlalchemy/trunk/lib/sqlalchemy/types.py (1067 => 1068)</h4> <pre class="diff"><span> <span class="info">--- sqlalchemy/trunk/lib/sqlalchemy/types.py 2006-02-28 23:33:15 UTC (rev 1067) +++ sqlalchemy/trunk/lib/sqlalchemy/types.py 2006-03-01 00:08:01 UTC (rev 1068) </span><span class="lines">@@ -94,6 +94,16 @@ </span><span class="cx"> return TEXT() </span><span class="cx"> else: </span><span class="cx"> return self </span><ins>+ def convert_bind_param(self, value, engine): + if not engine.convert_unicode or not isinstance(value, unicode): + return value + else: + return value.encode('utf-8') + def convert_result_value(self, value, engine): + if not engine.convert_unicode or isinstance(value, unicode): + return value + else: + return value.decode('utf-8') </ins><span class="cx"> </span><span class="cx"> class Unicode(TypeDecorator,String): </span><span class="cx"> def __init__(self, length=None): </span></span></pre></div> <a id="sqlalchemytrunktesttesttypespy"></a> <div class="modfile"><h4>Modified: sqlalchemy/trunk/test/testtypes.py (1067 => 1068)</h4> <pre class="diff"><span> <span class="info">--- sqlalchemy/trunk/test/testtypes.py 2006-02-28 23:33:15 UTC (rev 1067) +++ sqlalchemy/trunk/test/testtypes.py 2006-03-01 00:08:01 UTC (rev 1068) </span><span class="lines">@@ -108,7 +108,21 @@ </span><span class="cx"> self.echo("its sqlite !") </span><span class="cx"> else: </span><span class="cx"> self.assert_(not isinstance(x['plain_data'], unicode) and x['plain_data'] == rawdata) </span><del>- </del><ins>+ def testengineparam(self): + """tests engine-wide unicode conversion""" + prev_unicode = db.engine.convert_unicode + try: + db.engine.convert_unicode = True + rawdata = 'Alors vous imaginez ma surprise, au lever du jour, quand une dr\xc3\xb4le de petit voix m\xe2\x80\x99a r\xc3\xa9veill\xc3\xa9. Elle disait: \xc2\xab S\xe2\x80\x99il vous pla\xc3\xaet\xe2\x80\xa6 dessine-moi un mouton! \xc2\xbb\n' + unicodedata = rawdata.decode('utf-8') + unicode_table.insert().execute(unicode_data=unicodedata, plain_data=rawdata) + x = unicode_table.select().execute().fetchone() + self.echo(repr(x['unicode_data'])) + self.echo(repr(x['plain_data'])) + self.assert_(isinstance(x['unicode_data'], unicode) and x['unicode_data'] == unicodedata) + self.assert_(isinstance(x['plain_data'], unicode) and x['plain_data'] == unicodedata) + finally: + db.engine.convert_unicode = prev_unicode </ins><span class="cx"> </span><span class="cx"> class BinaryTest(AssertMixin): </span><span class="cx"> def setUpAll(self): </span></span></pre> </div> </div> </body> </html> |