From: Mike Bayer Date: Fri, 31 Mar 2006 22:28:17 +0000 (+0000) Subject: added for_update flag to Select/CompoundSelect X-Git-Tag: rel_0_1_6~43 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=66528a7264be790cfc1c332624dd5c470e47b341;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git added for_update flag to Select/CompoundSelect --- diff --git a/lib/sqlalchemy/ansisql.py b/lib/sqlalchemy/ansisql.py index f13a7b84fc..adfa36918b 100644 --- a/lib/sqlalchemy/ansisql.py +++ b/lib/sqlalchemy/ansisql.py @@ -373,6 +373,9 @@ class ANSICompiler(sql.Compiled): text += self.visit_select_postclauses(select) + if select.for_update: + text += " FOR UPDATE" + if getattr(select, 'parens', False): self.strings[select] = "(" + text + ")" else: diff --git a/lib/sqlalchemy/sql.py b/lib/sqlalchemy/sql.py index 86c538f277..b86c965c00 100644 --- a/lib/sqlalchemy/sql.py +++ b/lib/sqlalchemy/sql.py @@ -1176,6 +1176,7 @@ class CompoundSelect(SelectBaseMixin, FromClause): self.use_labels = kwargs.pop('use_labels', False) self.parens = kwargs.pop('parens', False) self.correlate = kwargs.pop('correlate', False) + self.for_update = kwargs.pop('for_update', False) self.oid_column = selects[0].oid_column for s in self.selects: s.group_by(None) @@ -1211,7 +1212,7 @@ class CompoundSelect(SelectBaseMixin, FromClause): class Select(SelectBaseMixin, FromClause): """represents a SELECT statement, with appendable clauses, as well as the ability to execute itself and return a result set.""" - def __init__(self, columns=None, whereclause = None, from_obj = [], order_by = None, group_by=None, having=None, use_labels = False, distinct=False, engine = None, limit=None, offset=None, scalar=False, correlate=True): + def __init__(self, columns=None, whereclause = None, from_obj = [], order_by = None, group_by=None, having=None, use_labels = False, distinct=False, for_update=False, engine=None, limit=None, offset=None, scalar=False, correlate=True): self._froms = util.OrderedDict() self.use_labels = use_labels self.name = None @@ -1221,6 +1222,7 @@ class Select(SelectBaseMixin, FromClause): self.oid_column = None self.limit = limit self.offset = offset + self.for_update = for_update # indicates that this select statement should not expand its columns # into the column clause of an enclosing select, and should instead diff --git a/test/select.py b/test/select.py index cac641fc09..c11cff5ea5 100644 --- a/test/select.py +++ b/test/select.py @@ -240,6 +240,11 @@ sq.myothertable_othername AS sq_myothertable_othername FROM (" + sqstring + ") A select([table2.c.othername, func.count(table2.c.otherid)], group_by = [table2.c.othername], order_by = [table2.c.othername]), "SELECT myothertable.othername, count(myothertable.otherid) FROM myothertable GROUP BY myothertable.othername ORDER BY myothertable.othername" ) + + def testforupdate(self): + self.runtest( + table1.select(table1.c.myid==7, for_update=True), "SELECT mytable.myid, mytable.name, mytable.description FROM mytable WHERE mytable.myid = :mytable_myid FOR UPDATE" + ) def testalias(self): # test the alias for a table1. column names stay the same, table name "changes" to "foo". self.runtest(