<!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><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[14586] twext/trunk/twext/enterprise</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { 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 #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#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>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.calendarserver.org//changeset/14586">14586</a></dd>
<dt>Author</dt> <dd>cdaboo@apple.com</dd>
<dt>Date</dt> <dd>2015-03-13 10:33:19 -0700 (Fri, 13 Mar 2015)</dd>
</dl>

<h3>Log Message</h3>
<pre>Fixes for Oracle DB.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#twexttrunktwextenterprisedalsyntaxpy">twext/trunk/twext/enterprise/dal/syntax.py</a></li>
<li><a href="#twexttrunktwextenterprisedaltesttest_parseschemapy">twext/trunk/twext/enterprise/dal/test/test_parseschema.py</a></li>
<li><a href="#twexttrunktwextenterprisejobqueuepy">twext/trunk/twext/enterprise/jobqueue.py</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="twexttrunktwextenterprisedalsyntaxpy"></a>
<div class="modfile"><h4>Modified: twext/trunk/twext/enterprise/dal/syntax.py (14585 => 14586)</h4>
<pre class="diff"><span>
<span class="info">--- twext/trunk/twext/enterprise/dal/syntax.py        2015-03-13 02:18:06 UTC (rev 14585)
+++ twext/trunk/twext/enterprise/dal/syntax.py        2015-03-13 17:33:19 UTC (rev 14586)
</span><span class="lines">@@ -1445,9 +1445,13 @@
</span><span class="cx">             # FOR UPDATE not supported with sqlite - but that is probably not relevant
</span><span class="cx">             # given that sqlite does file level locking of the DB
</span><span class="cx">             if queryGenerator.dialect != SQLITE_DIALECT:
</span><del>-                stmt.text += &quot; for update&quot;
-                if self.NoWait:
-                    stmt.text += &quot; nowait&quot;
</del><ins>+                # Oracle turns this statement into a sub-select if Limit is non-zero, but we can't have
+                # the &quot;for update&quot; in the sub-select. So suppress it here and add it in the outer limit
+                # select later.
+                if self.Limit is None or queryGenerator.dialect != ORACLE_DIALECT:
+                    stmt.text += &quot; for update&quot;
+                    if self.NoWait:
+                        stmt.text += &quot; nowait&quot;
</ins><span class="cx"> 
</span><span class="cx">         if self.Limit is not None:
</span><span class="cx">             limitConst = Constant(self.Limit).subSQL(queryGenerator, allTables)
</span><span class="lines">@@ -1460,6 +1464,12 @@
</span><span class="cx">                 stmt.text += &quot; limit &quot;
</span><span class="cx">             stmt.append(limitConst)
</span><span class="cx"> 
</span><ins>+            # Add in any Oracle &quot;for update&quot;
+            if self.ForUpdate and queryGenerator.dialect == ORACLE_DIALECT:
+                stmt.text += &quot; for update&quot;
+                if self.NoWait:
+                    stmt.text += &quot; nowait&quot;
+
</ins><span class="cx">         return stmt
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="lines">@@ -2042,12 +2052,20 @@
</span><span class="cx">         self._name = name
</span><span class="cx"> 
</span><span class="cx"> 
</span><ins>+    def _safeName(self, txn):
+        if txn.dialect == ORACLE_DIALECT:
+            # Oracle limits the length of identifiers
+            return self._name[:30]
+        else:
+            return self._name
+
+
</ins><span class="cx">     def acquire(self, txn):
</span><del>-        return Savepoint(self._name).on(txn)
</del><ins>+        return Savepoint(self._safeName(txn)).on(txn)
</ins><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx">     def rollback(self, txn):
</span><del>-        return RollbackToSavepoint(self._name).on(txn)
</del><ins>+        return RollbackToSavepoint(self._safeName(txn)).on(txn)
</ins><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx">     def release(self, txn):
</span><span class="lines">@@ -2057,7 +2075,7 @@
</span><span class="cx">             # do anything.
</span><span class="cx">             return NoOp()
</span><span class="cx">         else:
</span><del>-            return ReleaseSavepoint(self._name).on(txn)
</del><ins>+            return ReleaseSavepoint(self._safeName(txn)).on(txn)
</ins><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx"> 
</span></span></pre></div>
<a id="twexttrunktwextenterprisedaltesttest_parseschemapy"></a>
<div class="modfile"><h4>Modified: twext/trunk/twext/enterprise/dal/test/test_parseschema.py (14585 => 14586)</h4>
<pre class="diff"><span>
<span class="info">--- twext/trunk/twext/enterprise/dal/test/test_parseschema.py        2015-03-13 02:18:06 UTC (rev 14585)
+++ twext/trunk/twext/enterprise/dal/test/test_parseschema.py        2015-03-13 17:33:19 UTC (rev 14586)
</span><span class="lines">@@ -110,12 +110,17 @@
</span><span class="cx">             create table thetable (
</span><span class="cx">                 thecolumn integer default nextval('thingy')
</span><span class="cx">             );
</span><ins>+            create table thetable2 (
+                thecolumn2 integer primary key default nextval('thingy'),
+                ignoreme integer
+            );
</ins><span class="cx">             &quot;&quot;&quot;)
</span><span class="cx">         self.assertEquals(len(s.sequences), 1)
</span><span class="cx">         self.assertEquals(s.sequences[0].name, &quot;thingy&quot;)
</span><span class="cx">         self.assertEquals(s.tables[0].columns[0].default, s.sequences[0])
</span><ins>+        self.assertEquals(s.tables[1].columns[0].default, s.sequences[0])
</ins><span class="cx">         self.assertEquals(s.sequences[0].referringColumns,
</span><del>-                          [s.tables[0].columns[0]])
</del><ins>+                          [s.tables[0].columns[0], s.tables[1].columns[0]])
</ins><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx">     def test_sequenceDefault(self):
</span></span></pre></div>
<a id="twexttrunktwextenterprisejobqueuepy"></a>
<div class="modfile"><h4>Modified: twext/trunk/twext/enterprise/jobqueue.py (14585 => 14586)</h4>
<pre class="diff"><span>
<span class="info">--- twext/trunk/twext/enterprise/jobqueue.py        2015-03-13 02:18:06 UTC (rev 14585)
+++ twext/trunk/twext/enterprise/jobqueue.py        2015-03-13 17:33:19 UTC (rev 14586)
</span><span class="lines">@@ -145,13 +145,13 @@
</span><span class="cx">     SchemaSyntax, Lock, NamedValue
</span><span class="cx"> )
</span><span class="cx"> 
</span><del>-from twext.enterprise.dal.model import ProcedureCall
</del><ins>+from twext.enterprise.dal.model import ProcedureCall, Sequence
</ins><span class="cx"> from twext.enterprise.dal.record import Record, fromTable, NoSuchRecord
</span><span class="cx"> from twisted.python.failure import Failure
</span><span class="cx"> 
</span><span class="cx"> from twext.enterprise.dal.model import Table, Schema, SQLType, Constraint
</span><span class="cx"> from twisted.internet.endpoints import TCP4ServerEndpoint
</span><del>-from twext.enterprise.ienterprise import IQueuer
</del><ins>+from twext.enterprise.ienterprise import IQueuer, ORACLE_DIALECT
</ins><span class="cx"> from zope.interface.interface import Interface
</span><span class="cx"> 
</span><span class="cx"> import collections
</span><span class="lines">@@ -229,7 +229,7 @@
</span><span class="cx">     # transaction is made aware of somehow.
</span><span class="cx">     JobTable = Table(inSchema, &quot;JOB&quot;)
</span><span class="cx"> 
</span><del>-    JobTable.addColumn(&quot;JOB_ID&quot;, SQLType(&quot;integer&quot;, None), default=ProcedureCall(&quot;nextval&quot;, [&quot;JOB_SEQ&quot;]), notNull=True, primaryKey=True)
</del><ins>+    JobTable.addColumn(&quot;JOB_ID&quot;, SQLType(&quot;integer&quot;, None), default=Sequence(inSchema, &quot;JOB_SEQ&quot;), notNull=True, primaryKey=True)
</ins><span class="cx">     JobTable.addColumn(&quot;WORK_TYPE&quot;, SQLType(&quot;varchar&quot;, 255), notNull=True)
</span><span class="cx">     JobTable.addColumn(&quot;PRIORITY&quot;, SQLType(&quot;integer&quot;, 0), default=0)
</span><span class="cx">     JobTable.addColumn(&quot;WEIGHT&quot;, SQLType(&quot;integer&quot;, 0), default=0)
</span><span class="lines">@@ -579,17 +579,38 @@
</span><span class="cx">         @rtype: L{JobItem}
</span><span class="cx">         &quot;&quot;&quot;
</span><span class="cx"> 
</span><del>-        jobs = yield cls.query(
-            txn,
-            (cls.notBefore &lt;= now).And(cls.priority &gt;= minPriority).And(
-                (cls.assigned == None).Or(cls.overdue &lt; now)
-            ),
-            order=(cls.assigned, cls.priority),
-            ascending=False,
-            forUpdate=True,
-            noWait=False,
-            limit=limit,
-        )
</del><ins>+        if txn.dialect == ORACLE_DIALECT:
+            # Oracle does not support a &quot;for update&quot; clause with &quot;order by&quot;. So do the
+            # &quot;for update&quot; as a second query right after the first. Will need to check
+            # how this might impact concurrency in a multi-host setup.
+            jobs = yield cls.query(
+                txn,
+                (cls.notBefore &lt;= now).And(cls.priority &gt;= minPriority).And(
+                    (cls.assigned == None).Or(cls.overdue &lt; now)
+                ),
+                order=(cls.assigned, cls.priority),
+                ascending=False,
+                limit=limit,
+            )
+            if jobs:
+                yield cls.query(
+                    txn,
+                    (cls.jobID.In([job.jobID for job in jobs])),
+                    forUpdate=True,
+                    noWait=False,
+                )
+        else:
+            jobs = yield cls.query(
+                txn,
+                (cls.notBefore &lt;= now).And(cls.priority &gt;= minPriority).And(
+                    (cls.assigned == None).Or(cls.overdue &lt; now)
+                ),
+                order=(cls.assigned, cls.priority),
+                ascending=False,
+                forUpdate=True,
+                noWait=False,
+                limit=limit,
+            )
</ins><span class="cx"> 
</span><span class="cx">         returnValue(jobs)
</span><span class="cx"> 
</span></span></pre>
</div>
</div>

</body>
</html>