<!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>[12210] twext/trunk/twext/enterprise/dal</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/12210">12210</a></dd>
<dt>Author</dt> <dd>cdaboo@apple.com</dd>
<dt>Date</dt> <dd>2014-01-02 09:02:37 -0800 (Thu, 02 Jan 2014)</dd>
</dl>
<h3>Log Message</h3>
<pre>Add "NOT" and comma-based cross join syntax to support CalDAV/CardDAV queries.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#twexttrunktwextenterprisedalsyntaxpy">twext/trunk/twext/enterprise/dal/syntax.py</a></li>
<li><a href="#twexttrunktwextenterprisedaltesttest_sqlsyntaxpy">twext/trunk/twext/enterprise/dal/test/test_sqlsyntax.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 (12209 => 12210)</h4>
<pre class="diff"><span>
<span class="info">--- twext/trunk/twext/enterprise/dal/syntax.py        2014-01-02 16:53:18 UTC (rev 12209)
+++ twext/trunk/twext/enterprise/dal/syntax.py        2014-01-02 17:02:37 UTC (rev 12210)
</span><span class="lines">@@ -407,16 +407,41 @@
</span><span class="cx"> @param other: a constant parameter or sub-select
</span><span class="cx"> @type other: L{Parameter} or L{Select}
</span><span class="cx"> """
</span><ins>+ return self._commonIn('in', other)
+
+
+ def NotIn(self, other):
+ """
+ We support two forms of the SQL "NOT IN" syntax: one where a list of values
+ is supplied, the other where a sub-select is used to provide a set of
+ values.
+
+ @param other: a constant parameter or sub-select
+ @type other: L{Parameter} or L{Select}
+ """
+ return self._commonIn('not in', other)
+
+
+ def _commonIn(self, op, other):
+ """
+ We support two forms of the SQL "IN" and "NOT IN" syntax: one where a list
+ of values is supplied, the other where a sub-select is used to provide a set
+ of values.
+
+ @param other: a constant parameter or sub-select
+ @type other: L{Parameter} or L{Select}
+ """
+
</ins><span class="cx"> if isinstance(other, Parameter):
</span><span class="cx"> if other.count is None:
</span><span class="cx"> raise DALError(
</span><del>- "IN expression needs an explicit count of parameters"
</del><ins>+ "{} expression needs an explicit count of parameters".format(op.upper())
</ins><span class="cx"> )
</span><del>- return CompoundComparison(self, "in", Constant(other))
</del><ins>+ return CompoundComparison(self, op, Constant(other))
</ins><span class="cx"> else:
</span><span class="cx"> # Can't be Select.__contains__ because __contains__ gets
</span><span class="cx"> # __nonzero__ called on its result by the "in" syntax.
</span><del>- return CompoundComparison(self, "in", other)
</del><ins>+ return CompoundComparison(self, op, other)
</ins><span class="cx">
</span><span class="cx">
</span><span class="cx"> def StartsWith(self, other):
</span><span class="lines">@@ -426,6 +451,13 @@
</span><span class="cx"> )
</span><span class="cx">
</span><span class="cx">
</span><ins>+ def NotStartsWith(self, other):
+ return CompoundComparison(
+ self, "not like",
+ CompoundComparison(Constant(other), "||", Constant("%"))
+ )
+
+
</ins><span class="cx"> def EndsWith(self, other):
</span><span class="cx"> return CompoundComparison(
</span><span class="cx"> self, "like",
</span><span class="lines">@@ -433,6 +465,13 @@
</span><span class="cx"> )
</span><span class="cx">
</span><span class="cx">
</span><ins>+ def NotEndsWith(self, other):
+ return CompoundComparison(
+ self, "not like",
+ CompoundComparison(Constant("%"), "||", Constant(other))
+ )
+
+
</ins><span class="cx"> def Contains(self, other):
</span><span class="cx"> return CompoundComparison(
</span><span class="cx"> self, "like",
</span><span class="lines">@@ -443,7 +482,17 @@
</span><span class="cx"> )
</span><span class="cx">
</span><span class="cx">
</span><ins>+ def NotContains(self, other):
+ return CompoundComparison(
+ self, "not like",
+ CompoundComparison(
+ Constant("%"), "||",
+ CompoundComparison(Constant(other), "||", Constant("%"))
+ )
+ )
</ins><span class="cx">
</span><ins>+
+
</ins><span class="cx"> class FunctionInvocation(ExpressionSyntax):
</span><span class="cx"> def __init__(self, function, *args):
</span><span class="cx"> self.function = function
</span><span class="lines">@@ -647,7 +696,7 @@
</span><span class="cx"> """
</span><span class="cx"> Create a L{Join}, representing a join between two tables.
</span><span class="cx"> """
</span><del>- if on is None:
</del><ins>+ if on is None and not type:
</ins><span class="cx"> type = "cross"
</span><span class="cx"> return Join(self, type, otherTableSyntax, on)
</span><span class="cx">
</span><span class="lines">@@ -792,13 +841,16 @@
</span><span class="cx"> def subSQL(self, queryGenerator, allTables):
</span><span class="cx"> stmt = SQLFragment()
</span><span class="cx"> stmt.append(self.leftSide.subSQL(queryGenerator, allTables))
</span><del>- stmt.text += " "
- if self.type:
- stmt.text += self.type
</del><ins>+ if self.type == ",":
+ stmt.text += ", "
+ else:
</ins><span class="cx"> stmt.text += " "
</span><del>- stmt.text += "join "
</del><ins>+ if self.type:
+ stmt.text += self.type
+ stmt.text += " "
+ stmt.text += "join "
</ins><span class="cx"> stmt.append(self.rightSide.subSQL(queryGenerator, allTables))
</span><del>- if self.type != "cross":
</del><ins>+ if self.type not in ("cross", ","):
</ins><span class="cx"> stmt.text += " on "
</span><span class="cx"> stmt.append(self.on.subSQL(queryGenerator, allTables))
</span><span class="cx"> return stmt
</span><span class="lines">@@ -984,6 +1036,26 @@
</span><span class="cx">
</span><span class="cx">
</span><span class="cx">
</span><ins>+class Not(Comparison):
+ """
+ A L{NotColumn} is a logical NOT of an expression.
+ """
+ def __init__(self, a):
+ # "op" and "b" are always None for this comparison type
+ super(Not, self).__init__(a, None, None)
+
+
+ def subSQL(self, queryGenerator, allTables):
+ sqls = SQLFragment()
+ sqls.text += "not "
+ result = self.a.subSQL(queryGenerator, allTables)
+ if isinstance(self.a, CompoundComparison) and self.a.op in ("or", "and"):
+ result = _inParens(result)
+ sqls.append(result)
+ return sqls
+
+
+
</ins><span class="cx"> class NullComparison(Comparison):
</span><span class="cx"> """
</span><span class="cx"> A L{NullComparison} is a comparison of a column or expression with None.
</span></span></pre></div>
<a id="twexttrunktwextenterprisedaltesttest_sqlsyntaxpy"></a>
<div class="modfile"><h4>Modified: twext/trunk/twext/enterprise/dal/test/test_sqlsyntax.py (12209 => 12210)</h4>
<pre class="diff"><span>
<span class="info">--- twext/trunk/twext/enterprise/dal/test/test_sqlsyntax.py        2014-01-02 16:53:18 UTC (rev 12209)
+++ twext/trunk/twext/enterprise/dal/test/test_sqlsyntax.py        2014-01-02 17:02:37 UTC (rev 12210)
</span><span class="lines">@@ -33,8 +33,7 @@
</span><span class="cx"> Savepoint, RollbackToSavepoint, ReleaseSavepoint, SavepointAction,
</span><span class="cx"> Union, Intersect, Except, SetExpression, DALError,
</span><span class="cx"> ResultAliasSyntax, Count, QueryGenerator, ALL_COLUMNS,
</span><del>- DatabaseLock, DatabaseUnlock
-)
</del><ins>+ DatabaseLock, DatabaseUnlock, Not)
</ins><span class="cx"> from twext.enterprise.dal.syntax import FixedPlaceholder, NumericPlaceholder
</span><span class="cx"> from twext.enterprise.dal.syntax import Function
</span><span class="cx"> from twext.enterprise.dal.syntax import SchemaSyntax
</span><span class="lines">@@ -411,6 +410,17 @@
</span><span class="cx"> )
</span><span class="cx">
</span><span class="cx">
</span><ins>+ def test_commaJoin(self):
+ """
+ A join with no clause specified will generate a cross join. This variant
+ uses a "," between table names rather than "cross join".
+ """
+ self.assertEquals(
+ Select(From=self.schema.FOO.join(self.schema.BOZ, type=",")).toSQL(),
+ SQLFragment("select * from FOO, BOZ")
+ )
+
+
</ins><span class="cx"> def test_crossJoin(self):
</span><span class="cx"> """
</span><span class="cx"> A join with no clause specified will generate a cross join. (This is
</span><span class="lines">@@ -965,13 +975,13 @@
</span><span class="cx"> From=self.schema.FOO,
</span><span class="cx"> Where=(
</span><span class="cx"> (
</span><del>- self.schema.FOO.BAZ == Parameter("P1")
- ).Or(
- self.schema.FOO.BAR.In(Parameter("names", len(items)))
- ).And(
- self.schema.FOO.BAZ == Parameter("P2")
</del><ins>+ self.schema.FOO.BAZ == Parameter("P1")
+ ).Or(
+ self.schema.FOO.BAR.In(Parameter("names", len(items))
+ ).And(
+ self.schema.FOO.BAZ == Parameter("P2")
</ins><span class="cx"> )
</span><del>- )
</del><ins>+ ))
</ins><span class="cx"> ).toSQL().bind(P1="P1", P2="P2", names=items),
</span><span class="cx"> SQLFragment(
</span><span class="cx"> "select * from FOO where BAZ = ? or BAR in (?, ?) and BAZ = ?",
</span><span class="lines">@@ -1121,6 +1131,73 @@
</span><span class="cx"> )
</span><span class="cx">
</span><span class="cx">
</span><ins>+ def test_not(self):
+ """
+ Test for the string starts with comparison.
+ (Note that this should be updated to use different techniques
+ as necessary in different databases.)
+ """
+ self.assertEquals(
+ Select([
+ self.schema.TEXTUAL.MYTEXT],
+ From=self.schema.TEXTUAL,
+ Where=Not(self.schema.TEXTUAL.MYTEXT.StartsWith("test")),
+ ).toSQL(),
+ SQLFragment(
+ "select MYTEXT from TEXTUAL where not MYTEXT like (? || ?)",
+ ["test", "%"]
+ )
+ )
+
+ self.assertEquals(
+ Select([
+ self.schema.TEXTUAL.MYTEXT],
+ From=self.schema.TEXTUAL,
+ Where=Not(self.schema.TEXTUAL.MYTEXT == "test"),
+ ).toSQL(),
+ SQLFragment(
+ "select MYTEXT from TEXTUAL where not MYTEXT = ?",
+ ["test"]
+ )
+ )
+
+ self.assertEquals(
+ Select([
+ self.schema.TEXTUAL.MYTEXT],
+ From=self.schema.TEXTUAL,
+ Where=Not((self.schema.TEXTUAL.MYTEXT == "test1").And(self.schema.TEXTUAL.MYTEXT != "test2")),
+ ).toSQL(),
+ SQLFragment(
+ "select MYTEXT from TEXTUAL where not (MYTEXT = ? and MYTEXT != ?)",
+ ["test1", "test2"]
+ )
+ )
+
+ self.assertEquals(
+ Select([
+ self.schema.TEXTUAL.MYTEXT],
+ From=self.schema.TEXTUAL,
+ Where=Not((self.schema.TEXTUAL.MYTEXT == "test1")).And(self.schema.TEXTUAL.MYTEXT != "test2"),
+ ).toSQL(),
+ SQLFragment(
+ "select MYTEXT from TEXTUAL where not MYTEXT = ? and MYTEXT != ?",
+ ["test1", "test2"]
+ )
+ )
+
+ self.assertEquals(
+ Select([
+ self.schema.TEXTUAL.MYTEXT],
+ From=self.schema.TEXTUAL,
+ Where=Not(self.schema.TEXTUAL.MYTEXT.StartsWith("foo").And(self.schema.TEXTUAL.MYTEXT.NotEndsWith("bar"))),
+ ).toSQL(),
+ SQLFragment(
+ "select MYTEXT from TEXTUAL where not (MYTEXT like (? || ?) and MYTEXT not like (? || ?))",
+ ["foo", "%", "%", "bar"]
+ )
+ )
+
+
</ins><span class="cx"> def test_insert(self):
</span><span class="cx"> """
</span><span class="cx"> L{Insert.toSQL} generates an C{insert} statement with all the relevant
</span></span></pre>
</div>
</div>
</body>
</html>