[Sqlalchemy-commits] [1299] sqlalchemy/branches/schema/lib/sqlalchemy: selectresults becomes an exte
Brought to you by:
zzzeek
<!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>[1299] sqlalchemy/branches/schema/lib/sqlalchemy: selectresults becomes an extension, the mod just installs it, so you can use selectresults without installing the mod</title> </head> <body> <div id="msg"> <dl> <dt>Revision</dt> <dd>1299</dd> <dt>Author</dt> <dd>zzzeek</dd> <dt>Date</dt> <dd>2006-04-19 15:02:27 -0500 (Wed, 19 Apr 2006)</dd> </dl> <h3>Log Message</h3> <pre>selectresults becomes an extension, the mod just installs it, so you can use selectresults without installing the mod</pre> <h3>Modified Paths</h3> <ul> <li><a href="#sqlalchemybranchesschemalibsqlalchemyenginebasepy">sqlalchemy/branches/schema/lib/sqlalchemy/engine/base.py</a></li> <li><a href="#sqlalchemybranchesschemalibsqlalchemymodsselectresultspy">sqlalchemy/branches/schema/lib/sqlalchemy/mods/selectresults.py</a></li> <li><a href="#sqlalchemybranchesschemalibsqlalchemyschemapy">sqlalchemy/branches/schema/lib/sqlalchemy/schema.py</a></li> </ul> <h3>Added Paths</h3> <ul> <li><a href="#sqlalchemybranchesschemalibsqlalchemyextselectresultspy">sqlalchemy/branches/schema/lib/sqlalchemy/ext/selectresults.py</a></li> </ul> </div> <div id="patch"> <h3>Diff</h3> <a id="sqlalchemybranchesschemalibsqlalchemyenginebasepy"></a> <div class="modfile"><h4>Modified: sqlalchemy/branches/schema/lib/sqlalchemy/engine/base.py (1298 => 1299)</h4> <pre class="diff"><span> <span class="info">--- sqlalchemy/branches/schema/lib/sqlalchemy/engine/base.py 2006-04-19 19:47:02 UTC (rev 1298) +++ sqlalchemy/branches/schema/lib/sqlalchemy/engine/base.py 2006-04-19 20:02:27 UTC (rev 1299) </span><span class="lines">@@ -350,7 +350,7 @@ </span><span class="cx"> else: </span><span class="cx"> conn = connection </span><span class="cx"> try: </span><del>- element.accept_visitor(visitorcallable(self, conn.proxy, **kwargs)) </del><ins>+ element.accept_schema_visitor(visitorcallable(self, conn.proxy, **kwargs)) </ins><span class="cx"> finally: </span><span class="cx"> if connection is None: </span><span class="cx"> conn.close() </span></span></pre></div> <a id="sqlalchemybranchesschemalibsqlalchemyextselectresultspy"></a> <div class="addfile"><h4>Added: sqlalchemy/branches/schema/lib/sqlalchemy/ext/selectresults.py (1298 => 1299)</h4> <pre class="diff"><span> <span class="info">--- sqlalchemy/branches/schema/lib/sqlalchemy/ext/selectresults.py 2006-04-19 19:47:02 UTC (rev 1298) +++ sqlalchemy/branches/schema/lib/sqlalchemy/ext/selectresults.py 2006-04-19 20:02:27 UTC (rev 1299) </span><span class="lines">@@ -0,0 +1,82 @@ </span><ins>+import sqlalchemy.sql as sql + +import sqlalchemy.mapping as mapping + + +class SelectResultsExt(mapping.MapperExtension): + def select_by(self, query, *args, **params): + return SelectResults(query, query._by_clause(*args, **params)) + def select(self, query, arg=None, **kwargs): + if arg is not None and isinstance(arg, sql.Selectable): + return mapping.EXT_PASS + else: + return SelectResults(query, arg, ops=kwargs) + +class SelectResults(object): + def __init__(self, query, clause=None, ops={}): + self._query = query + self._clause = clause + self._ops = {} + self._ops.update(ops) + + def count(self): + return self._query.count(self._clause) + + def min(self, col): + return sql.select([sql.func.min(col)], self._clause, **self._ops).scalar() + + def max(self, col): + return sql.select([sql.func.max(col)], self._clause, **self._ops).scalar() + + def sum(self, col): + return sql.select([sql.func.sum(col)], self._clause, **self._ops).scalar() + + def avg(self, col): + return sql.select([sql.func.avg(col)], self._clause, **self._ops).scalar() + + def clone(self): + return SelectResults(self._query, self._clause, self._ops.copy()) + + def filter(self, clause): + new = self.clone() + new._clause = sql.and_(self._clause, clause) + return new + + def order_by(self, order_by): + new = self.clone() + new._ops['order_by'] = order_by + return new + + def limit(self, limit): + return self[:limit] + + def offset(self, offset): + return self[offset:] + + def list(self): + return list(self) + + def __getitem__(self, item): + if isinstance(item, slice): + start = item.start + stop = item.stop + if (isinstance(start, int) and start < 0) or \ + (isinstance(stop, int) and stop < 0): + return list(self)[item] + else: + res = self.clone() + if start is not None and stop is not None: + res._ops.update(dict(offset=start, limit=stop-start)) + elif start is None and stop is not None: + res._ops.update(dict(limit=stop)) + elif start is not None and stop is None: + res._ops.update(dict(offset=start)) + if item.step is not None: + return list(res)[None:None:item.step] + else: + return res + else: + return list(self[item:item+1])[0] + + def __iter__(self): + return iter(self._query.select_whereclause(self._clause, **self._ops)) </ins></span></pre></div> <a id="sqlalchemybranchesschemalibsqlalchemymodsselectresultspy"></a> <div class="modfile"><h4>Modified: sqlalchemy/branches/schema/lib/sqlalchemy/mods/selectresults.py (1298 => 1299)</h4> <pre class="diff"><span> <span class="info">--- sqlalchemy/branches/schema/lib/sqlalchemy/mods/selectresults.py 2006-04-19 19:47:02 UTC (rev 1298) +++ sqlalchemy/branches/schema/lib/sqlalchemy/mods/selectresults.py 2006-04-19 20:02:27 UTC (rev 1299) </span><span class="lines">@@ -1,87 +1,6 @@ </span><del>-import sqlalchemy.sql as sql </del><ins>+from sqlalchemy.ext.selectresults import * </ins><span class="cx"> </span><del>-import sqlalchemy.mapping as mapping </del><span class="cx"> </span><del>-class SelectResultsExt(mapping.MapperExtension): - def select_by(self, query, *args, **params): - return SelectResults(query, query._by_clause(*args, **params)) - def select(self, query, arg=None, **kwargs): - if arg is not None and isinstance(arg, sql.Selectable): - return mapping.EXT_PASS - else: - return SelectResults(query, arg, ops=kwargs) - -MapperExtension = SelectResultsExt - -class SelectResults(object): - def __init__(self, query, clause=None, ops={}): - self._query = query - self._clause = clause - self._ops = {} - self._ops.update(ops) - - def count(self): - return self._query.count(self._clause) - - def min(self, col): - return sql.select([sql.func.min(col)], self._clause, **self._ops).scalar() - - def max(self, col): - return sql.select([sql.func.max(col)], self._clause, **self._ops).scalar() - - def sum(self, col): - return sql.select([sql.func.sum(col)], self._clause, **self._ops).scalar() - - def avg(self, col): - return sql.select([sql.func.avg(col)], self._clause, **self._ops).scalar() - - def clone(self): - return SelectResults(self._query, self._clause, self._ops.copy()) - - def filter(self, clause): - new = self.clone() - new._clause = sql.and_(self._clause, clause) - return new - - def order_by(self, order_by): - new = self.clone() - new._ops['order_by'] = order_by - return new - - def limit(self, limit): - return self[:limit] - - def offset(self, offset): - return self[offset:] - - def list(self): - return list(self) - - def __getitem__(self, item): - if isinstance(item, slice): - start = item.start - stop = item.stop - if (isinstance(start, int) and start < 0) or \ - (isinstance(stop, int) and stop < 0): - return list(self)[item] - else: - res = self.clone() - if start is not None and stop is not None: - res._ops.update(dict(offset=start, limit=stop-start)) - elif start is None and stop is not None: - res._ops.update(dict(limit=stop)) - elif start is not None and stop is None: - res._ops.update(dict(offset=start)) - if item.step is not None: - return list(res)[None:None:item.step] - else: - return res - else: - return list(self[item:item+1])[0] - - def __iter__(self): - return iter(self._query.select_whereclause(self._clause, **self._ops)) - </del><span class="cx"> def install_plugin(): </span><span class="cx"> mapping.global_extensions.append(SelectResultsExt) </span><span class="cx"> install_plugin() </span></span></pre></div> <a id="sqlalchemybranchesschemalibsqlalchemyschemapy"></a> <div class="modfile"><h4>Modified: sqlalchemy/branches/schema/lib/sqlalchemy/schema.py (1298 => 1299)</h4> <pre class="diff"><span> <span class="info">--- sqlalchemy/branches/schema/lib/sqlalchemy/schema.py 2006-04-19 19:47:02 UTC (rev 1298) +++ sqlalchemy/branches/schema/lib/sqlalchemy/schema.py 2006-04-19 20:02:27 UTC (rev 1299) </span><span class="lines">@@ -183,6 +183,7 @@ </span><span class="cx"> def accept_schema_visitor(self, visitor): </span><span class="cx"> """traverses the given visitor across the Column objects inside this Table, </span><span class="cx"> then calls the visit_table method on the visitor.""" </span><ins>+ print "TABLE ACCEPT VISITOR, C IS", [c for c in self.columns] </ins><span class="cx"> for c in self.columns: </span><span class="cx"> c.accept_schema_visitor(visitor) </span><span class="cx"> return visitor.visit_table(self) </span></span></pre> </div> </div> </body> </html> |