[Sqlalchemy-commits] [1133] sqlalchemy/trunk: added selectfirst_by/selectone_by, selectone throws e
Brought to you by:
zzzeek
From: <co...@sq...> - 2006-03-13 02:54:04
|
<!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>[1133] sqlalchemy/trunk: added selectfirst_by/selectone_by, selectone throws exception if more than one row returned, courtesy J.Ellis</title> </head> <body> <div id="msg"> <dl> <dt>Revision</dt> <dd>1133</dd> <dt>Author</dt> <dd>zzzeek</dd> <dt>Date</dt> <dd>2006-03-12 20:53:51 -0600 (Sun, 12 Mar 2006)</dd> </dl> <h3>Log Message</h3> <pre>added selectfirst_by/selectone_by, selectone throws exception if more than one row returned, courtesy J.Ellis</pre> <h3>Modified Paths</h3> <ul> <li><a href="#sqlalchemytrunkCHANGES">sqlalchemy/trunk/CHANGES</a></li> <li><a href="#sqlalchemytrunklibsqlalchemymappingmapperpy">sqlalchemy/trunk/lib/sqlalchemy/mapping/mapper.py</a></li> </ul> </div> <div id="patch"> <h3>Diff</h3> <a id="sqlalchemytrunkCHANGES"></a> <div class="modfile"><h4>Modified: sqlalchemy/trunk/CHANGES (1132 => 1133)</h4> <pre class="diff"><span> <span class="info">--- sqlalchemy/trunk/CHANGES 2006-03-13 02:39:52 UTC (rev 1132) +++ sqlalchemy/trunk/CHANGES 2006-03-13 02:53:51 UTC (rev 1133) </span><span class="lines">@@ -7,6 +7,9 @@ </span><span class="cx"> create joins against the table that is specific to the mapper itself (i.e. and </span><span class="cx"> not any tables that are inherited/are further down the inheritance chain), </span><span class="cx"> this can be overridden by using custom primary/secondary joins. </span><ins>+- added J.Ellis patch to mapper.py so that selectone() throws an exception +if query returns more than one object row, selectfirst() to not throw the +exception. also adds selectfirst_by (synonymous with get_by) and selectone_by </ins><span class="cx"> - added onupdate parameter to Column, will exec SQL/python upon an update </span><span class="cx"> statement.Also adds "for_update=True" to all DefaultGenerator subclasses </span><span class="cx"> - added user-contributed support for Oracle table reflection; still </span></span></pre></div> <a id="sqlalchemytrunklibsqlalchemymappingmapperpy"></a> <div class="modfile"><h4>Modified: sqlalchemy/trunk/lib/sqlalchemy/mapping/mapper.py (1132 => 1133)</h4> <pre class="diff"><span> <span class="info">--- sqlalchemy/trunk/lib/sqlalchemy/mapping/mapper.py 2006-03-13 02:39:52 UTC (rev 1132) +++ sqlalchemy/trunk/lib/sqlalchemy/mapping/mapper.py 2006-03-13 02:53:51 UTC (rev 1133) </span><span class="lines">@@ -253,7 +253,7 @@ </span><span class="cx"> populate_existing = kwargs.get('populate_existing', False) </span><span class="cx"> </span><span class="cx"> result = util.HistoryArraySet() </span><del>- if len(mappers): </del><ins>+ if mappers: </ins><span class="cx"> otherresults = [] </span><span class="cx"> for m in mappers: </span><span class="cx"> otherresults.append(util.HistoryArraySet()) </span><span class="lines">@@ -273,10 +273,9 @@ </span><span class="cx"> for value in imap.values(): </span><span class="cx"> objectstore.get_session().register_clean(value) </span><span class="cx"> </span><del>- if len(mappers): - return [result] + otherresults - else: - return result </del><ins>+ if mappers: + result.extend(otherresults) + return result </ins><span class="cx"> </span><span class="cx"> def get(self, *ident): </span><span class="cx"> """returns an instance of the object based on the given identifier, or None </span><span class="lines">@@ -374,7 +373,7 @@ </span><span class="cx"> e.g. u = usermapper.get_by(user_name = 'fred') </span><span class="cx"> """ </span><span class="cx"> x = self.select_whereclause(self._by_clause(*args, **params), limit=1) </span><del>- if len(x): </del><ins>+ if x: </ins><span class="cx"> return x[0] </span><span class="cx"> else: </span><span class="cx"> return None </span><span class="lines">@@ -393,7 +392,19 @@ </span><span class="cx"> e.g. result = usermapper.select_by(user_name = 'fred') </span><span class="cx"> """ </span><span class="cx"> return self.select_whereclause(self._by_clause(*args, **params)) </span><ins>+ + def selectfirst_by(self, *args, **params): + """works like select_by(), but only returns the first result by itself, or None if no + objects returned. Synonymous with get_by()""" + return self.get_by(*args, **params) </ins><span class="cx"> </span><ins>+ def selectone_by(self, *args, **params): + """works like selectfirst(), but throws an error if not exactly one result was returned.""" + ret = self.select_by(*args, **params) + if len(ret) == 1: + return ret[0] + raise InvalidRequestError('Multiple rows returned for selectone') + </ins><span class="cx"> def count_by(self, *args, **params): </span><span class="cx"> """returns the count of instances based on the given clauses and key/value criterion. </span><span class="cx"> The criterion is constructed in the same way as the select_by() method.""" </span><span class="lines">@@ -448,16 +459,23 @@ </span><span class="cx"> else: </span><span class="cx"> raise AttributeError(key) </span><span class="cx"> </span><del>- def selectone(self, *args, **params): </del><ins>+ def selectfirst(self, *args, **params): </ins><span class="cx"> """works like select(), but only returns the first result by itself, or None if no </span><span class="cx"> objects returned.""" </span><span class="cx"> params['limit'] = 1 </span><span class="cx"> ret = self.select(*args, **params) </span><del>- if len(ret): </del><ins>+ if ret: </ins><span class="cx"> return ret[0] </span><span class="cx"> else: </span><span class="cx"> return None </span><span class="cx"> </span><ins>+ def selectone(self, *args, **params): + """works like selectfirst(), but throws an error if not exactly one result was returned.""" + ret = self.select(*args, **params) + if len(ret) == 1: + return ret[0] + raise InvalidRequestError('Multiple rows returned for selectone') + </ins><span class="cx"> def select(self, arg = None, **kwargs): </span><span class="cx"> """selects instances of the object from the database. </span><span class="cx"> </span><span class="lines">@@ -983,12 +1001,5 @@ </span><span class="cx"> """given a class, returns the primary Mapper associated with the class.""" </span><span class="cx"> try: </span><span class="cx"> return mapper_registry[class_] </span><del>- except KeyError: - pass - except AttributeError: - pass - raise InvalidRequestError("Class '%s' has no mapper associated with it" % class_.__name__) - - - - </del><ins>+ except (KeyError, AttributeError): + raise InvalidRequestError("Class '%s' has no mapper associated with it" % class_.__name__) </ins></span></pre> </div> </div> </body> </html> |