Author: thomasm
Date: Tue May 21 09:26:47 2013
New Revision: 1484714

URL: http://svn.apache.org/r1484714
Log:
OAK-830 XPathToSQL2Converter fails to wrap "or" clauses

Modified:
    
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/XPathToSQL2Converter.java
    
jackrabbit/oak/trunk/oak-core/src/test/resources/org/apache/jackrabbit/oak/query/xpath.txt

Modified: 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/XPathToSQL2Converter.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/XPathToSQL2Converter.java?rev=1484714&r1=1484713&r2=1484714&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/XPathToSQL2Converter.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/XPathToSQL2Converter.java
 Tue May 21 09:26:47 2013
@@ -348,30 +348,20 @@ public class XPathToSQL2Converter {
         }
         
         // where ...
-        StringBuilder condition = new StringBuilder();
+        Expression where = null;
         for (int i = 0; i < selectors.size(); i++) {
             Selector s = selectors.get(i);
             if (s.condition != null) {
-                if (condition.length() > 0) {
-                    condition.append(" and ");
-                }
-                if (s.condition.getPrecedence() == Expression.PRECEDENCE_OR) {
-                    if (condition.length() > 0) {
-                        condition.append("(");
-                        condition.append(s.condition);
-                        condition.append(")");
-                    } else {
-                        condition.append(s.condition);
-                    }
+                if (where == null) {
+                    where = s.condition;
                 } else {
-                    condition.append(s.condition);
+                    where = new Condition(where, "and", s.condition, 
Expression.PRECEDENCE_AND);
                 }
             }
         }
-        if (condition.length() > 0) {
-            buff.append(" where ").append(condition.toString());
+        if (where != null) {
+            buff.append(" where ").append(where.toString());
         }
-
         // order by ...
         if (!orderList.isEmpty()) {
             buff.append(" order by ");

Modified: 
jackrabbit/oak/trunk/oak-core/src/test/resources/org/apache/jackrabbit/oak/query/xpath.txt
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/resources/org/apache/jackrabbit/oak/query/xpath.txt?rev=1484714&r1=1484713&r2=1484714&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/test/resources/org/apache/jackrabbit/oak/query/xpath.txt
 (original)
+++ 
jackrabbit/oak/trunk/oak-core/src/test/resources/org/apache/jackrabbit/oak/query/xpath.txt
 Tue May 21 09:26:47 2013
@@ -126,6 +126,12 @@ select [jcr:path], [jcr:score], * from [
 xpath2sql 
//element(*,rep:Authorizable)[(((jcr:contains(profile/givenName,'**') or 
jcr:contains(profile/familyName,'**')) or jcr:contains(profile/email,'**')) or 
(jcr:like(rep:principalName,'%%') or jcr:like(fn:name(.),'%%')))] order by 
rep:principalName ascending
 select [jcr:path], [jcr:score], * from [rep:Authorizable] as a where 
contains([profile/givenName/*], '**') or contains([profile/familyName/*], '**') 
or contains([profile/email/*], '**') or [rep:principalName/*] like '%%' or 
name(a) like '%%' order by [rep:principalName/*] /* xpath: 
//element(*,rep:Authorizable)[(((jcr:contains(profile/givenName,'**') or 
jcr:contains(profile/familyName,'**')) or jcr:contains(profile/email,'**')) or 
(jcr:like(rep:principalName,'%%') or jcr:like(fn:name(.),'%%')))] order by 
rep:principalName ascending */
 
+xpath2sql //*[@a=1 or @b=1]/sub[@c=1]
+select b.[jcr:path] as [jcr:path], b.[jcr:score] as [jcr:score], b.* from 
[nt:base] as a inner join [nt:base] as b on ischildnode(b, a) where (a.[a] = 1 
or a.[b] = 1) and b.[c] = 1 and name(b) = 'sub' /* xpath: //*[@a=1 or 
@b=1]/sub[@c=1] */
+
+xpath2sql //*[@a=1 or @b=1]/sub[@c=1 or @d=1]
+select b.[jcr:path] as [jcr:path], b.[jcr:score] as [jcr:score], b.* from 
[nt:base] as a inner join [nt:base] as b on ischildnode(b, a) where (a.[a] = 1 
or a.[b] = 1) and (b.[c] = 1 or b.[d] = 1) and name(b) = 'sub' /* xpath: 
//*[@a=1 or @b=1]/sub[@c=1 or @d=1] */
+
 xpath2sql 
//element(*,rep:Authorizable)[(((jcr:contains(profile/@givenName,'**') or 
jcr:contains(profile/@familyName,'**')) or jcr:contains(profile/@email,'**')) 
or (jcr:like(@rep:principalName,'%%') or jcr:like(fn:name(.),'%%')))] order by 
@rep:principalName ascending
 select [jcr:path], [jcr:score], * from [rep:Authorizable] as a where 
contains([profile/givenName], '**') or contains([profile/familyName], '**') or 
contains([profile/email], '**') or [rep:principalName] like '%%' or name(a) 
like '%%' order by [rep:principalName] /* xpath: 
//element(*,rep:Authorizable)[(((jcr:contains(profile/@givenName,'**') or 
jcr:contains(profile/@familyName,'**')) or jcr:contains(profile/@email,'**')) 
or (jcr:like(@rep:principalName,'%%') or jcr:like(fn:name(.),'%%')))] order by 
@rep:principalName ascending */
 
@@ -147,7 +153,8 @@ select [jcr:path], [jcr:score], * from [
 xpath2sql /jcr:root/testroot/*[@prop1 = 1 and jcr:like(fn:name(), 'F%')]
 select [jcr:path], [jcr:score], * from [nt:base] as a where [prop1] = 1 and 
name(a) like 'F%' and ischildnode(a, '/testroot') /* xpath: 
/jcr:root/testroot/*[@prop1 = 1 and jcr:like(fn:name(), 'F%')] */
 
-# TODO support rep:excerpt() and rep:similar()? how?
+# TODO support rep:similar()? how?
+
 xpath2sql /jcr:root/testroot/*[jcr:contains(., 'jackrabbit')]/rep:excerpt(.)
 select b.[jcr:path] as [jcr:path], b.[jcr:score] as [jcr:score], 
b.[rep:excerpt] as [rep:excerpt] from [nt:base] as a inner join [nt:base] as b 
on ischildnode(b, a) where contains(a.*, 'jackrabbit') and ischildnode(a, 
'/testroot') /* xpath: /jcr:root/testroot/*[jcr:contains(., 
'jackrabbit')]/rep:excerpt(.) */
 


Reply via email to