[CalendarServer-changes] [7137] CalendarServer/branches/users/glyph/oracle/twext/enterprise/dal/ syntax.py

source_changes at macosforge.org source_changes at macosforge.org
Mon Mar 7 19:04:22 PST 2011


Revision: 7137
          http://trac.macosforge.org/projects/calendarserver/changeset/7137
Author:   glyph at apple.com
Date:     2011-03-07 19:04:21 -0800 (Mon, 07 Mar 2011)
Log Message:
-----------
Refactor Update/Insert/Delete implementation of 'returning' clause to factor out commonalities.

Modified Paths:
--------------
    CalendarServer/branches/users/glyph/oracle/twext/enterprise/dal/syntax.py

Modified: CalendarServer/branches/users/glyph/oracle/twext/enterprise/dal/syntax.py
===================================================================
--- CalendarServer/branches/users/glyph/oracle/twext/enterprise/dal/syntax.py	2011-03-08 03:04:08 UTC (rev 7136)
+++ CalendarServer/branches/users/glyph/oracle/twext/enterprise/dal/syntax.py	2011-03-08 03:04:21 UTC (rev 7137)
@@ -730,7 +730,32 @@
                             for f in self.subfragments)
 
 
+def _returningClause(metadata, stmt, retclause, allTables):
+    """
+    Add a dialect-appropriate 'returning' clause to the end of the given SQL
+    statement.
 
+    @param metadata: describes the database we are generating the statement for.
+    @type metadata: L{ConnectionMetadata}
+
+    @param stmt: the SQL fragment generated without the 'returning' clause
+    @type stmt: L{SQLFragment}
+
+    @param retclause: the C{Return} argument from the current statement.
+    @type retclause: an object with a C{subSQL} method, or a C{tuple} or C{list}
+        of same.
+
+    @return: the C{stmt} parameter.
+    """
+    if isinstance(retclause, (tuple, list)):
+        retclause = _CommaList(retclause)
+    if retclause is not None:
+        stmt.text += ' returning '
+        stmt.append(retclause.subSQL(metadata, allTables))
+    return stmt
+
+
+
 class Insert(_Statement):
     """
     'insert' statement.
@@ -738,8 +763,6 @@
 
     def __init__(self, columnMap, Return=None):
         self.columnMap = columnMap
-        if isinstance(Return, (tuple, list)):
-            Return = _CommaList(Return)
         self.Return = Return
         columns = _modelsFromMap(columnMap)
         table = _fromSameTable(columns)
@@ -782,10 +805,7 @@
         stmt.append(_inParens(_commaJoined(
             [_convert(v).subSQL(metadata, allTables)
              for (c, v) in sortedColumns])))
-        if self.Return is not None:
-            stmt.text += ' returning '
-            stmt.append(self.Return.subSQL(metadata, allTables))
-        return stmt
+        return _returningClause(metadata, stmt, self.Return, allTables)
 
 
 
@@ -811,8 +831,6 @@
         _fromSameTable(_modelsFromMap(columnMap))
         self.columnMap = columnMap
         self.Where = Where
-        if isinstance(Return, (tuple, list)):
-            Return = _CommaList(Return)
         self.Return = Return
 
 
@@ -841,10 +859,7 @@
         )
         result.append(SQLFragment( ' where '))
         result.append(self.Where.subSQL(metadata, allTables))
-        if self.Return is not None:
-            result.append(SQLFragment(' returning '))
-            result.append(self.Return.subSQL(metadata, allTables))
-        return result
+        return _returningClause(metadata, result, self.Return, allTables)
 
 
 
@@ -866,10 +881,7 @@
         result.append(self.From.subSQL(metadata, allTables))
         result.text += ' where '
         result.append(self.Where.subSQL(metadata, allTables))
-        if self.Return is not None:
-            result.append(SQLFragment(' returning '))
-            result.append(self.Return.subSQL(metadata, allTables))
-        return result
+        return _returningClause(metadata, result, self.Return, allTables)
 
 
 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20110307/725bbc22/attachment.html>


More information about the calendarserver-changes mailing list