jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mreut...@apache.org
Subject svn commit: r786075 [2/2] - in /jackrabbit/trunk: jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/ jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/ jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/q...
Date Thu, 18 Jun 2009 14:17:53 GMT
Modified: jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/query/qom/SameNodeJoinConditionTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/query/qom/SameNodeJoinConditionTest.java?rev=786075&r1=786074&r2=786075&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/query/qom/SameNodeJoinConditionTest.java
(original)
+++ jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/query/qom/SameNodeJoinConditionTest.java
Thu Jun 18 14:17:52 2009
@@ -18,7 +18,6 @@
 
 import javax.jcr.Node;
 import javax.jcr.RepositoryException;
-import javax.jcr.query.QueryResult;
 import javax.jcr.query.qom.JoinCondition;
 import javax.jcr.query.qom.QueryObjectModel;
 import javax.jcr.query.qom.QueryObjectModelConstants;
@@ -38,19 +37,17 @@
         n1 = testRootNode.addNode(nodeName1, testNodeType);
         n2 = n1.addNode(nodeName2, testNodeType);
         n2.addMixin(mixReferenceable);
-        testRootNode.save();
+        superuser.save();
     }
 
     public void testInnerJoin() throws RepositoryException {
-        QueryObjectModel qom = createQomQuery(QueryObjectModelConstants.JCR_JOIN_TYPE_INNER,
(String) null);
-        QueryResult result = qom.execute();
-        checkResult(result, new Node[][]{{n1, n1}, {n2, n2}});
+        QueryObjectModel qom = createQomQuery(QueryObjectModelConstants.JCR_JOIN_TYPE_INNER,
null);
+        checkQOM(qom, new Node[][]{{n1, n1}, {n2, n2}});
     }
 
     public void testInnerJoinWithPath() throws RepositoryException {
         QueryObjectModel qom = createQomQuery(QueryObjectModelConstants.JCR_JOIN_TYPE_INNER,
nodeName2);
-        QueryResult result = qom.execute();
-        checkResult(result, new Node[][]{{n2, n1}});
+        checkQOM(qom, new Node[][]{{n2, n1}});
     }
 
     public void testLeftOuterJoin() throws RepositoryException {
@@ -63,14 +60,12 @@
                qf.descendantNode(LEFT, testRoot),
                null, null);
 
-        QueryResult result = qom.execute();
-        checkResult(result, new Node[][]{{n1, null}, {n2, n2}});
+        checkQOM(qom, new Node[][]{{n1, null}, {n2, n2}});
     }
 
     public void testLeftOuterJoinWithPath() throws RepositoryException {
         QueryObjectModel qom = createQomQuery(QueryObjectModelConstants.JCR_JOIN_TYPE_LEFT_OUTER,
nodeName2);
-        QueryResult result = qom.execute();
-        checkResult(result, new Node[][]{{n1, null}, {n2, n1}});
+        checkQOM(qom, new Node[][]{{n1, null}, {n2, n1}});
     }
 
     public void testRightOuterJoin() throws RepositoryException {
@@ -83,8 +78,7 @@
                 qf.descendantNode(RIGHT, testRoot),
                 null, null);
 
-        QueryResult result = qom.execute();
-        checkResult(result, new Node[][]{{null, n1}, {n2, n2}});
+        checkQOM(qom, new Node[][]{{null, n1}, {n2, n2}});
     }
 
     public void testRightOuterJoinWithPath() throws RepositoryException {
@@ -97,8 +91,7 @@
                 qf.descendantNode(RIGHT, testRoot),
                 null, null);
 
-        QueryResult result = qom.execute();
-        checkResult(result, new Node[][]{{n2, n1}, {null, n2}});
+        checkQOM(qom, new Node[][]{{n2, n1}, {null, n2}});
     }
 
     //-----------------------------< utilities >--------------------------------

Modified: jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/query/qom/SameNodeTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/query/qom/SameNodeTest.java?rev=786075&r1=786074&r2=786075&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/query/qom/SameNodeTest.java
(original)
+++ jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/query/qom/SameNodeTest.java
Thu Jun 18 14:17:52 2009
@@ -23,6 +23,7 @@
 import javax.jcr.nodetype.NodeTypeManager;
 import javax.jcr.query.InvalidQueryException;
 import javax.jcr.query.Query;
+import javax.jcr.query.qom.QueryObjectModel;
 
 import org.apache.jackrabbit.test.NotExecutableException;
 
@@ -33,24 +34,24 @@
 
     public void testSameNode() throws RepositoryException {
         Node n = testRootNode.addNode(nodeName1, testNodeType);
-        testRootNode.save();
+        superuser.save();
 
-        Query q = qf.createQuery(qf.selector(testNodeType, "s"),
+        QueryObjectModel qom = qf.createQuery(qf.selector(testNodeType, "s"),
                 qf.sameNode("s", testRoot + "/" + nodeName1), null, null);
-        checkResult(q.execute(), new Node[]{n});
+        checkQOM(qom, new Node[]{n});
     }
 
     public void testPathDoesNotExist() throws RepositoryException {
-        Query q = qf.createQuery(qf.selector(testNodeType, "s"),
+        QueryObjectModel qom = qf.createQuery(qf.selector(testNodeType, "s"),
                 qf.sameNode("s", testRoot + "/" + nodeName1),
                 null, null);
-        checkResult(q.execute(), new Node[]{});
+        checkQOM(qom, new Node[]{});
     }
 
     public void testChildNodesDoNotMatchSelector()
             throws RepositoryException, NotExecutableException {
         testRootNode.addNode(nodeName1, testNodeType);
-        testRootNode.save();
+        superuser.save();
 
         NodeTypeManager ntMgr = superuser.getWorkspace().getNodeTypeManager();
         NodeTypeIterator it = ntMgr.getPrimaryNodeTypes();
@@ -59,9 +60,9 @@
             NodeType nt = it.nextNodeType();
             if (!testNt.isNodeType(nt.getName())) {
                 // perform test
-                Query q = qf.createQuery(qf.selector(nt.getName(), "s"),
+                QueryObjectModel qom = qf.createQuery(qf.selector(nt.getName(), "s"),
                         qf.sameNode("s", testRoot + "/" + nodeName1), null, null);
-                checkResult(q.execute(), new Node[]{});
+                checkQOM(qom, new Node[]{});
                 return;
             }
         }
@@ -78,18 +79,35 @@
         } catch (InvalidQueryException e) {
             // expected
         }
+        try {
+            String stmt = "SELECT * FROM [" + testNodeType + "] AS s " +
+                    "WHERE ISSAMENODE(s, [" + testPath + "]";
+            qm.createQuery(stmt, Query.JCR_SQL2).execute();
+            fail("ISSAMENODE() with relative path argument must throw InvalidQueryException");
+        } catch (InvalidQueryException e) {
+            // expected
+        }
     }
 
     public void testSyntacticallyInvalidPath() throws RepositoryException {
+        String invalidPath = testRoot + "/" + nodeName1 + "[";
         try {
             Query q = qf.createQuery(qf.selector(testNodeType, "s"),
-                    qf.sameNode("s", testRoot + "/" + nodeName1 + "["),
+                    qf.sameNode("s", invalidPath),
                     null, null);
             q.execute();
             fail("SameNode with syntactically invalid path argument must throw InvalidQueryException");
         } catch (InvalidQueryException e) {
             // expected
         }
+        try {
+            String stmt = "SELECT * FROM [" + testNodeType + "] AS s " +
+                    "WHERE ISSAMENODE(s, [" + invalidPath + "]";
+            qm.createQuery(stmt, Query.JCR_SQL2).execute();
+            fail("ISSAMENODE() with syntactically invalid path argument must throw InvalidQueryException");
+        } catch (InvalidQueryException e) {
+            // expected
+        }
     }
 
     public void testNotASelectorName() throws RepositoryException {
@@ -101,6 +119,13 @@
         } catch (InvalidQueryException e) {
             // expected
         }
+        try {
+            String stmt = "SELECT * FROM [" + testNodeType + "] AS s " +
+                    "WHERE ISSAMENODE(x, [" + testRoot + "]";
+            qm.createQuery(stmt, Query.JCR_SQL2).execute();
+            fail("ISSAMENODE with an invalid selector name must throw InvalidQueryException");
+        } catch (InvalidQueryException e) {
+            // expected
+        }
     }
-
 }

Modified: jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/query/qom/SelectorTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/query/qom/SelectorTest.java?rev=786075&r1=786074&r2=786075&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/query/qom/SelectorTest.java
(original)
+++ jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/query/qom/SelectorTest.java
Thu Jun 18 14:17:52 2009
@@ -23,6 +23,7 @@
 import javax.jcr.query.InvalidQueryException;
 import javax.jcr.query.Query;
 import javax.jcr.query.qom.QueryObjectModelConstants;
+import javax.jcr.query.qom.QueryObjectModel;
 
 /**
  * <code>SelectorTest</code>...
@@ -32,24 +33,37 @@
     public void testSelector() throws RepositoryException {
         // make sure there's at least one node with this node type
         testRootNode.addNode(nodeName1, testNodeType);
-        testRootNode.save();
-        Query q = qf.createQuery(
+        superuser.save();
+        QueryObjectModel qom = qf.createQuery(
                 qf.selector(testNodeType, "s"), null, null, null);
-        NodeIterator it = q.execute().getNodes();
-        while (it.hasNext()) {
-            assertTrue("Wrong node type", it.nextNode().isNodeType(testNodeType));
-        }
+        forQOMandSQL2(qom, new Callable() {
+            public Object call(Query query) throws RepositoryException {
+                NodeIterator it = query.execute().getNodes();
+                while (it.hasNext()) {
+                    assertTrue("Wrong node type", it.nextNode().isNodeType(testNodeType));
+                }
+                return null;
+            }
+        });
     }
 
     public void testSyntacticallyInvalidName() throws RepositoryException {
+        String invalidNodeType = testNodeType + "[";
         try {
-            Query q = qf.createQuery(
-                    qf.selector(testNodeType + "[", "s"), null, null, null);
+            Query q = qf.createQuery(qf.selector(invalidNodeType, "s"),
+                    null, null, null);
             q.execute();
             fail("Selector with syntactically invalid name must throw InvalidQueryException");
         } catch (InvalidQueryException e) {
             // expected
         }
+        try {
+            String stmt = "SELECT * FROM [" + invalidNodeType + "]";
+            qm.createQuery(stmt, Query.JCR_SQL2).execute();
+            fail("selectorName with syntactically invalid name must throw InvalidQueryException");
+        } catch (InvalidQueryException e) {
+            // expected
+        }
     }
 
     public void testUnknownNodeType() throws RepositoryException {
@@ -63,10 +77,19 @@
                 break;
             }
         }
-        Query q = qf.createQuery(qf.selector(ntName, "s"),
-                null, null, null);
-        assertFalse("Selector must not select nodes for unknown node type",
-                q.execute().getNodes().hasNext());
+        try {
+            qf.createQuery(qf.selector(ntName, "s"), null, null, null).execute();
+            fail("Selector with unknown node type must throw InvalidQueryException");
+        } catch (InvalidQueryException e) {
+            // expected
+        }
+        try {
+            String stmt = "SELECT * FROM [" + ntName + "] AS nt";
+            qm.createQuery(stmt, Query.JCR_SQL2).execute();
+            fail("Selector with unknown node type must throw InvalidQueryException");
+        } catch (InvalidQueryException e) {
+            // expected
+        }
     }
 
     public void testDuplicateNodeType() throws RepositoryException {
@@ -83,6 +106,13 @@
         } catch (InvalidQueryException e) {
             // expected
         }
+        try {
+            String stmt = "SELECT * FROM [" + testNodeType + "] AS nt, [" +
+                    testNodeType + "] AS nt nt INNER JOIN nt ON ISDESCENDANTNODE(nt, nt)";
+            qm.createQuery(stmt, Query.JCR_SQL2).execute();
+            fail("selectorName with syntactically invalid name must throw InvalidQueryException");
+        } catch (InvalidQueryException e) {
+            // expected
+        }
     }
-
 }

Modified: jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/query/qom/UpperLowerCaseTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/query/qom/UpperLowerCaseTest.java?rev=786075&r1=786074&r2=786075&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/query/qom/UpperLowerCaseTest.java
(original)
+++ jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/query/qom/UpperLowerCaseTest.java
Thu Jun 18 14:17:52 2009
@@ -20,9 +20,9 @@
 import javax.jcr.PropertyType;
 import javax.jcr.RepositoryException;
 import javax.jcr.Value;
-import javax.jcr.query.Query;
 import javax.jcr.query.qom.DynamicOperand;
 import javax.jcr.query.qom.QueryObjectModelConstants;
+import javax.jcr.query.qom.QueryObjectModel;
 
 /**
  * <code>UpperLowerCaseTest</code> performs tests with upper- and lower-case
@@ -168,15 +168,16 @@
                               int type,
                               boolean[] matches) throws RepositoryException {
         for (int i = 0; i < literals.length; i++) {
-            Query query = createQuery(operand, toUpper, operator, vf.createValue(literals[i],
type));
-            checkResult(query.execute(), matches[i] ? new Node[]{node} : new Node[0]);
+            QueryObjectModel qom = createQuery(operand, toUpper, operator, vf.createValue(literals[i],
type));
+            checkQOM(qom, matches[i] ? new Node[]{node} : new Node[0]);
         }
     }
     
-    private Query createQuery(DynamicOperand operand,
-                              boolean toUpper,
-                              String operator,
-                              Value literal) throws RepositoryException {
+    private QueryObjectModel createQuery(DynamicOperand operand,
+                                         boolean toUpper,
+                                         String operator,
+                                         Value literal)
+            throws RepositoryException {
         if (toUpper) {
             operand = qf.upperCase(operand);
         } else {

Modified: jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/query/QueryObjectModelBuilder.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/query/QueryObjectModelBuilder.java?rev=786075&r1=786074&r2=786075&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/query/QueryObjectModelBuilder.java
(original)
+++ jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/query/QueryObjectModelBuilder.java
Thu Jun 18 14:17:52 2009
@@ -22,8 +22,6 @@
 import javax.jcr.ValueFactory;
 import javax.jcr.RepositoryException;
 
-import org.apache.jackrabbit.spi.commons.conversion.NameResolver;
-
 /**
  * <code>QueryObjectModelBuilder</code> defines an interface for building a
  * query object model from a string based query statement and vice versa.
@@ -68,12 +66,11 @@
      * this <code>QueryObjectModelBuilder</code> can handle.
      *
      * @param qom      the query object model.
-     * @param resolver to resolve Names.
      * @return a String representation of the QOM.
      * @throws InvalidQueryException if the query object model cannot be
      *                               converted into a String representation due
      *                               to restrictions in this syntax.
      */
-    String toString(QueryObjectModel qom, NameResolver resolver)
+    String toString(QueryObjectModel qom)
             throws InvalidQueryException;
 }

Modified: jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/query/sql2/Parser.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/query/sql2/Parser.java?rev=786075&r1=786074&r2=786075&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/query/sql2/Parser.java
(original)
+++ jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/query/sql2/Parser.java
Thu Jun 18 14:17:52 2009
@@ -248,7 +248,7 @@
     private Constraint parseCondition() throws RepositoryException {
         Constraint a;
         if (readIf("NOT")) {
-            a = parseConstraint();
+            a = factory.not(parseConstraint());
         } else if (readIf("(")) {
             a = parseConstraint();
             read(")");

Added: jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/query/sql2/QOMFormatter.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/query/sql2/QOMFormatter.java?rev=786075&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/query/sql2/QOMFormatter.java
(added)
+++ jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/query/sql2/QOMFormatter.java
Thu Jun 18 14:17:52 2009
@@ -0,0 +1,595 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.spi.commons.query.sql2;
+
+import java.util.BitSet;
+import java.util.Arrays;
+
+import javax.jcr.query.qom.QueryObjectModel;
+import javax.jcr.query.qom.Join;
+import javax.jcr.query.qom.Source;
+import javax.jcr.query.qom.Selector;
+import javax.jcr.query.qom.Column;
+import javax.jcr.query.qom.Constraint;
+import javax.jcr.query.qom.Ordering;
+import javax.jcr.query.qom.JoinCondition;
+import javax.jcr.query.qom.EquiJoinCondition;
+import javax.jcr.query.qom.ChildNodeJoinCondition;
+import javax.jcr.query.qom.DescendantNodeJoinCondition;
+import javax.jcr.query.qom.SameNodeJoinCondition;
+import javax.jcr.query.qom.And;
+import javax.jcr.query.qom.ChildNode;
+import javax.jcr.query.qom.Comparison;
+import javax.jcr.query.qom.DescendantNode;
+import javax.jcr.query.qom.FullTextSearch;
+import javax.jcr.query.qom.Not;
+import javax.jcr.query.qom.Or;
+import javax.jcr.query.qom.PropertyExistence;
+import javax.jcr.query.qom.SameNode;
+import javax.jcr.query.qom.DynamicOperand;
+import javax.jcr.query.qom.StaticOperand;
+import javax.jcr.query.qom.BindVariableValue;
+import javax.jcr.query.qom.Literal;
+import javax.jcr.query.qom.FullTextSearchScore;
+import javax.jcr.query.qom.Length;
+import javax.jcr.query.qom.LowerCase;
+import javax.jcr.query.qom.NodeLocalName;
+import javax.jcr.query.qom.NodeName;
+import javax.jcr.query.qom.PropertyValue;
+import javax.jcr.query.qom.UpperCase;
+import javax.jcr.query.qom.QueryObjectModelConstants;
+import javax.jcr.Value;
+import javax.jcr.PropertyType;
+import javax.jcr.RepositoryException;
+
+/**
+ * <code>QOMFormatter</code> implements a formatter that translates a query
+ * object model into a JCR_SQL2 string statement.
+ */
+public class QOMFormatter implements QueryObjectModelConstants {
+
+    /**
+     * BitSet of valid SQL identifier start characters.
+     */
+    private static final BitSet IDENTIFIER_START = new BitSet();
+
+    /**
+     * BitSet of valid SQL identifier body characters.
+     */
+    private static final BitSet IDENTIFIER_PART_OR_UNDERSCORE = new BitSet();
+
+    static {
+        for (char c = 'a'; c <= 'z'; c++) {
+            IDENTIFIER_START.set(c);
+        }
+        for (char c = 'A'; c <= 'Z'; c++) {
+            IDENTIFIER_START.set(c);
+        }
+        IDENTIFIER_PART_OR_UNDERSCORE.or(IDENTIFIER_START);
+        for (char c = '0'; c <= '9'; c++) {
+            IDENTIFIER_PART_OR_UNDERSCORE.set(c);
+        }
+        IDENTIFIER_PART_OR_UNDERSCORE.set('_');
+    }
+
+    /**
+     * The query object model to format.
+     */
+    private final QueryObjectModel qom;
+
+    /**
+     * The JCR_SQL2 statement.
+     */
+    private final StringBuilder sb = new StringBuilder();
+
+    /**
+     * Private constructor.
+     *
+     * @param qom the query object model to format.
+     */
+    private QOMFormatter(QueryObjectModel qom) {
+        this.qom = qom;
+    }
+
+    /**
+     * Formats the given <code>qom</code> as a JCR_SQL2 query statement.
+     *
+     * @param qom the query object model to translate.
+     * @return the JCR_SQL2 statement.
+     * @throws RepositoryException if an error occurs while formatting the qom. 
+     */
+    public static String format(QueryObjectModel qom)
+            throws RepositoryException {
+        return new QOMFormatter(qom).format();
+    }
+
+    private String format() throws RepositoryException {
+        sb.append("SELECT");
+        ws();
+        format(qom.getColumns());
+        ws();
+        sb.append("FROM");
+        ws();
+        format(qom.getSource());
+        Constraint c = qom.getConstraint();
+        if (c != null) {
+            ws();
+            sb.append("WHERE");
+            ws();
+            format(c);
+        }
+        Ordering[] orderings = qom.getOrderings();
+        if (orderings.length > 0) {
+            ws();
+            sb.append("ORDER BY");
+            ws();
+            format(orderings);
+        }
+        return sb.toString();
+    }
+
+    private void format(Ordering[] orderings) {
+        String comma = "";
+        for (Ordering ordering : orderings) {
+            sb.append(comma);
+            comma = ", ";
+            format(ordering.getOperand());
+            if (JCR_ORDER_DESCENDING.equals(ordering.getOrder())) {
+                ws();
+                sb.append("DESC");
+            }
+        }
+    }
+
+    private void format(Constraint c)
+            throws RepositoryException {
+        if (c instanceof And) {
+            format((And) c);
+        } else if (c instanceof ChildNode) {
+            format((ChildNode) c);
+        } else if (c instanceof Comparison) {
+            format((Comparison) c);
+        } else if (c instanceof DescendantNode) {
+            format((DescendantNode) c);
+        } else if (c instanceof FullTextSearch) {
+            format((FullTextSearch) c);
+        } else if (c instanceof Not) {
+            format((Not) c);
+        } else if (c instanceof Or) {
+            format((Or) c);
+        } else if (c instanceof PropertyExistence) {
+            format((PropertyExistence) c);
+        } else {
+            format((SameNode) c);
+        }
+    }
+
+    private void format(And constraint)
+            throws RepositoryException {
+        String and = "";
+        for (Constraint c : Arrays.asList(
+                constraint.getConstraint1(),
+                constraint.getConstraint2())) {
+            sb.append(and);
+            and = " AND ";
+            boolean paren = c instanceof Or;
+            if (paren) {
+                sb.append("(");
+            }
+            format(c);
+            if (paren) {
+                sb.append(")");
+            }
+        }
+    }
+
+    private void format(ChildNode constraint) {
+        sb.append("ISCHILDNODE(");
+        formatName(constraint.getSelectorName());
+        sb.append(",");
+        ws();
+        formatPath(constraint.getParentPath());
+        sb.append(")");
+    }
+
+    private void format(Comparison constraint)
+            throws RepositoryException {
+        format(constraint.getOperand1());
+        ws();
+        formatOperator(constraint.getOperator());
+        ws();
+        format(constraint.getOperand2());
+    }
+
+    private void format(StaticOperand operand)
+            throws RepositoryException {
+        if (operand instanceof BindVariableValue) {
+            format((BindVariableValue) operand);
+        } else {
+            format((Literal) operand);
+        }
+    }
+
+    private void format(BindVariableValue value) {
+        sb.append("$");
+        sb.append(value.getBindVariableName());
+    }
+
+    private void format(Literal value)
+            throws RepositoryException {
+        Value v = value.getLiteralValue();
+        switch (v.getType()) {
+            case PropertyType.BINARY:
+                formatCastLiteral(v.getString(), "BINARY");
+                break;
+            case PropertyType.BOOLEAN:
+                sb.append(v.getString());
+                break;
+            case PropertyType.DATE:
+                formatCastLiteral(v.getString(), "DATE");
+                break;
+            case PropertyType.DECIMAL:
+                sb.append(v.getString());
+                break;
+            case PropertyType.DOUBLE:
+                sb.append(v.getString());
+                break;
+            case PropertyType.LONG:
+                sb.append(v.getString());
+                break;
+            case PropertyType.NAME:
+                formatCastLiteral(v.getString(), "NAME");
+                break;
+            case PropertyType.PATH:
+                formatCastLiteral(v.getString(), "PATH");
+                break;
+            case PropertyType.REFERENCE:
+                formatCastLiteral(v.getString(), "REFERENCE");
+                break;
+            case PropertyType.STRING:
+                formatStringLiteral(v.getString());
+                break;
+            case PropertyType.URI:
+                formatCastLiteral(v.getString(), "URI");
+                break;
+            case PropertyType.WEAKREFERENCE:
+                formatCastLiteral(v.getString(), "WEAKREFERENCE");
+                break;
+        }
+    }
+
+    private void formatCastLiteral(String value, String propertyType) {
+        sb.append("CAST(");
+        formatStringLiteral(value);
+        ws();
+        sb.append("AS");
+        ws();
+        sb.append(propertyType);
+        sb.append(")");
+    }
+
+    private void formatStringLiteral(String value) {
+        sb.append("'");
+        sb.append(value.replaceAll("'", "''"));
+        sb.append("'");
+    }
+
+    private void formatOperator(String operator) {
+        if (JCR_OPERATOR_EQUAL_TO.equals(operator)) {
+            sb.append("=");
+        } else if (JCR_OPERATOR_GREATER_THAN.equals(operator)) {
+            sb.append(">");
+        } else if (JCR_OPERATOR_GREATER_THAN_OR_EQUAL_TO.equals(operator)) {
+            sb.append(">=");
+        } else if (JCR_OPERATOR_LESS_THAN.equals(operator)) {
+            sb.append("<");
+        } else if (JCR_OPERATOR_LESS_THAN_OR_EQUAL_TO.equals(operator)) {
+            sb.append("<=");
+        } else if (JCR_OPERATOR_LIKE.equals(operator)) {
+            sb.append("LIKE");
+        } else {
+            sb.append("<>");
+        }
+    }
+
+    private void format(DynamicOperand operand) {
+        if (operand instanceof FullTextSearchScore) {
+            format((FullTextSearchScore) operand);
+        } else if (operand instanceof Length) {
+            format((Length) operand);
+        } else if (operand instanceof LowerCase) {
+            format((LowerCase) operand);
+        } else if (operand instanceof NodeLocalName) {
+            format((NodeLocalName) operand);
+        } else if (operand instanceof NodeName) {
+            format((NodeName) operand);
+        } else if (operand instanceof PropertyValue) {
+            format((PropertyValue) operand);
+        } else {
+            format((UpperCase) operand);
+        }
+    }
+
+    private void format(FullTextSearchScore operand) {
+        sb.append("SCORE(");
+        formatName(operand.getSelectorName());
+        sb.append(")");
+    }
+
+    private void format(Length operand) {
+        sb.append("LENGTH(");
+        format(operand.getPropertyValue());
+        sb.append(")");
+    }
+
+    private void format(LowerCase operand) {
+        sb.append("LOWER(");
+        format(operand.getOperand());
+        sb.append(")");
+    }
+
+    private void format(NodeLocalName operand) {
+        sb.append("LOCALNAME(");
+        formatName(operand.getSelectorName());
+        sb.append(")");
+    }
+
+    private void format(NodeName operand) {
+        sb.append("NAME(");
+        formatName(operand.getSelectorName());
+        sb.append(")");
+    }
+
+    private void format(PropertyValue operand) {
+        formatName(operand.getSelectorName());
+        sb.append(".");
+        formatName(operand.getPropertyName());
+    }
+
+    private void format(UpperCase operand) {
+        sb.append("UPPER(");
+        format(operand.getOperand());
+        sb.append(")");
+    }
+
+    private void format(DescendantNode constraint) {
+        sb.append("ISDESCENDANTNODE(");
+        formatName(constraint.getSelectorName());
+        sb.append(",");
+        ws();
+        formatPath(constraint.getAncestorPath());
+        sb.append(")");
+    }
+
+    private void format(FullTextSearch constraint)
+            throws RepositoryException {
+        sb.append("CONTAINS(");
+        formatName(constraint.getSelectorName());
+        sb.append(".");
+        String propName = constraint.getPropertyName();
+        if (propName == null) {
+            sb.append("*");
+        } else {
+            formatName(propName);
+        }
+        sb.append(",");
+        ws();
+        format(constraint.getFullTextSearchExpression());
+        sb.append(")");
+    }
+
+    private void format(Not constraint)
+            throws RepositoryException {
+        sb.append("NOT");
+        ws();
+        Constraint c = constraint.getConstraint();
+        boolean paren = c instanceof And || c instanceof Or;
+        if (paren) {
+            sb.append("(");
+        }
+        format(c);
+        if (paren) {
+            sb.append(")");
+        }
+    }
+
+    private void format(Or constraint)
+            throws RepositoryException {
+        format(constraint.getConstraint1());
+        ws();
+        sb.append("OR");
+        ws();
+        format(constraint.getConstraint2());
+    }
+
+    private void format(PropertyExistence constraint) {
+        formatName(constraint.getSelectorName());
+        sb.append(".");
+        formatName(constraint.getPropertyName());
+        ws();
+        sb.append("IS NOT NULL");
+    }
+
+    private void format(SameNode constraint) {
+        sb.append("ISSAMENODE(");
+        formatName(constraint.getSelectorName());
+        sb.append(",");
+        ws();
+        formatPath(constraint.getPath());
+        sb.append(")");
+    }
+
+    private void format(Column[] columns) {
+        if (columns.length == 0) {
+            sb.append("*");
+        } else {
+            String comma = "";
+            for (Column c : columns) {
+                sb.append(comma);
+                comma = ", ";
+                formatName(c.getSelectorName());
+                sb.append(".");
+                String propName = c.getPropertyName();
+                if (propName != null) {
+                    formatName(propName);
+                    ws();
+                    sb.append("AS");
+                    ws();
+                    formatName(c.getColumnName());
+                } else {
+                    sb.append("*");
+                }
+            }
+        }
+    }
+
+    private void format(Source source) {
+        if (source instanceof Join) {
+            format((Join) source);
+        } else {
+            format((Selector) source);
+        }
+    }
+
+    private void format(Join join) {
+        format(join.getLeft());
+        ws();
+        formatJoinType(join.getJoinType());
+        ws();
+        sb.append("JOIN");
+        ws();
+        format(join.getRight());
+        ws();
+        sb.append("ON");
+        ws();
+        format(join.getJoinCondition());
+    }
+
+    private void format(JoinCondition joinCondition) {
+        if (joinCondition instanceof EquiJoinCondition) {
+            format((EquiJoinCondition) joinCondition);
+        } else if (joinCondition instanceof ChildNodeJoinCondition) {
+            format((ChildNodeJoinCondition) joinCondition);
+        } else if (joinCondition instanceof DescendantNodeJoinCondition) {
+            format((DescendantNodeJoinCondition) joinCondition);
+        } else {
+            format((SameNodeJoinCondition) joinCondition);
+        }
+    }
+
+    private void format(EquiJoinCondition condition) {
+        formatName(condition.getSelector1Name());
+        sb.append(".");
+        formatName(condition.getProperty1Name());
+        ws();
+        sb.append("=");
+        ws();
+        formatName(condition.getSelector2Name());
+        sb.append(".");
+        formatName(condition.getProperty2Name());
+    }
+
+    private void format(ChildNodeJoinCondition condition) {
+        sb.append("ISCHILDNODE(");
+        formatName(condition.getChildSelectorName());
+        sb.append(",");
+        ws();
+        formatName(condition.getParentSelectorName());
+        sb.append(")");
+    }
+
+    private void format(DescendantNodeJoinCondition condition) {
+        sb.append("ISDESCENDANTNODE(");
+        formatName(condition.getDescendantSelectorName());
+        sb.append(",");
+        ws();
+        formatName(condition.getAncestorSelectorName());
+        sb.append(")");
+    }
+
+    private void format(SameNodeJoinCondition condition) {
+        sb.append("ISSAMENODE(");
+        formatName(condition.getSelector1Name());
+        sb.append(",");
+        ws();
+        formatName(condition.getSelector2Name());
+        if (condition.getSelector2Path() != null) {
+            sb.append(",");
+            ws();
+            formatPath(condition.getSelector2Path());
+        }
+        sb.append(")");
+    }
+
+    private void formatPath(String path) {
+        if (isSimpleName(path)) {
+            sb.append(path);
+        } else {
+            sb.append("[");
+            sb.append(path);
+            sb.append("]");
+        }
+    }
+
+    private void formatJoinType(String joinType) {
+        if (joinType.equals(JCR_JOIN_TYPE_INNER)) {
+            sb.append("INNER");
+        } else if (joinType.equals(JCR_JOIN_TYPE_LEFT_OUTER)) {
+            sb.append("LEFT OUTER");
+        } else {
+            sb.append("RIGHT OUTER");
+        }
+    }
+
+    private void format(Selector selector) {
+        formatName(selector.getNodeTypeName());
+        ws();
+        sb.append("AS");
+        ws();
+        formatName(selector.getSelectorName());
+    }
+
+    private void formatName(String name) {
+        if (isSimpleName(name)) {
+            sb.append(name);
+        } else {
+            sb.append("[");
+            sb.append(name);
+            sb.append("]");
+        }
+    }
+
+    private static boolean isSimpleName(String name) {
+        for (int i = 0; i < name.length(); i++) {
+            char c = name.charAt(i);
+            if (i == 0) {
+                if (!IDENTIFIER_START.get(c)) {
+                    return false;
+                }
+            } else {
+                if (!IDENTIFIER_PART_OR_UNDERSCORE.get(c)) {
+                    return false;
+                }
+            }
+        }
+        return true;
+    }
+
+    private void ws() {
+        sb.append(" ");
+    }
+}

Propchange: jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/query/sql2/QOMFormatter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/query/sql2/SQL2QOMBuilder.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/query/sql2/SQL2QOMBuilder.java?rev=786075&r1=786074&r2=786075&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/query/sql2/SQL2QOMBuilder.java
(original)
+++ jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/query/sql2/SQL2QOMBuilder.java
Thu Jun 18 14:17:52 2009
@@ -24,7 +24,6 @@
 import javax.jcr.RepositoryException;
 
 import org.apache.jackrabbit.spi.commons.query.QueryObjectModelBuilder;
-import org.apache.jackrabbit.spi.commons.conversion.NameResolver;
 
 /**
  * <code>SQL2QOMBuilder</code> implements QOM builder that understands
@@ -56,9 +55,15 @@
         return new String[]{Query.JCR_SQL2};
     }
 
-    public String toString(QueryObjectModel qom, NameResolver resolver)
+    /**
+     * {@inheritDoc}
+     */
+    public String toString(QueryObjectModel qom)
             throws InvalidQueryException {
-        // TODO
-        return null;
+        try {
+            return QOMFormatter.format(qom);
+        } catch (RepositoryException e) {
+            throw new InvalidQueryException(e.getMessage(), e);
+        }
     }
 }



Mime
View raw message