From: Mike Bayer Date: Sat, 23 Sep 2006 21:13:10 +0000 (+0000) Subject: added scalar() to ResultProxy X-Git-Tag: rel_0_3_0~133 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a7866293516d41971e0baa3ad4d2da56326d019b;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git added scalar() to ResultProxy --- diff --git a/CHANGES b/CHANGES index e7954d9830..e3dc2f2741 100644 --- a/CHANGES +++ b/CHANGES @@ -18,7 +18,7 @@ after commit/rollback - added extract() function to sql dialect - added an implicit close() on the cursor in ResultProxy when the result closes -- added scalar() method to ComposedSQLEngine +- added scalar() method to ComposedSQLEngine, ResultProxy - post_update behavior improved; does a better job at not updating too many rows, updates only required columns [ticket:208] diff --git a/lib/sqlalchemy/engine/base.py b/lib/sqlalchemy/engine/base.py index 28d5ea94e6..1285e8490d 100644 --- a/lib/sqlalchemy/engine/base.py +++ b/lib/sqlalchemy/engine/base.py @@ -593,6 +593,7 @@ class ResultProxy: rec = self.props[key] self.__key_cache[key] = rec return rec + def _has_key(self, row, key): try: self._convert_key(key) @@ -624,7 +625,7 @@ class ResultProxy: return self.executioncontext.supports_sane_rowcount() def fetchall(self): - """fetches all rows, just like DBAPI cursor.fetchall().""" + """fetch all rows, just like DBAPI cursor.fetchall().""" l = [] while True: v = self.fetchone() @@ -633,7 +634,7 @@ class ResultProxy: l.append(v) def fetchone(self): - """fetches one row, just like DBAPI cursor.fetchone().""" + """fetch one row, just like DBAPI cursor.fetchone().""" row = self.cursor.fetchone() if row is not None: if self.echo: self.engine.log(repr(row)) @@ -644,6 +645,18 @@ class ResultProxy: # and not just plain tuples ? self.close() return None + + def scalar(self): + """fetch the first column of the first row, and close the result set.""" + row = self.cursor.fetchone() + try: + if row is not None: + if self.echo: self.engine.log(repr(row)) + return row[0] + else: + return None + finally: + self.close() class RowProxy: """proxies a single cursor row for a parent ResultProxy."""