jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From thom...@apache.org
Subject svn commit: r1294545 - in /jackrabbit/sandbox/jackrabbit-microkernel/src: main/java/org/apache/jackrabbit/query/ main/java/org/apache/jackrabbit/query/qom/ main/java/org/apache/jackrabbit/query/qom/tree/ test/java/org/apache/jackrabbit/query/ test/java...
Date Tue, 28 Feb 2012 10:10:47 GMT
Author: thomasm
Date: Tue Feb 28 10:10:46 2012
New Revision: 1294545

URL: http://svn.apache.org/viewvc?rev=1294545&view=rev
Log:
Query implementation (WIP)

Added:
    jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/query/QueryEngine.java
    jackrabbit/sandbox/jackrabbit-microkernel/src/test/java/org/apache/jackrabbit/query/
    jackrabbit/sandbox/jackrabbit-microkernel/src/test/java/org/apache/jackrabbit/query/QueryTest.java
    jackrabbit/sandbox/jackrabbit-microkernel/src/test/java/org/apache/jackrabbit/query/SQL2Test.java
    jackrabbit/sandbox/jackrabbit-microkernel/src/test/java/org/apache/jackrabbit/query/XPathTest.java
    jackrabbit/sandbox/jackrabbit-microkernel/src/test/java/org/apache/jackrabbit/query/qom/
    jackrabbit/sandbox/jackrabbit-microkernel/src/test/java/org/apache/jackrabbit/query/qom/QomTest.java
Modified:
    jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/query/ParserSQL2.java
    jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/query/qom/QueryObjectModelFactoryImpl.java
    jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/query/qom/QueryObjectModelImpl.java
    jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/query/qom/tree/QOMTreeVisitor.java
    jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/query/qom/tree/SelectorImpl.java
    jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/query/qom/tree/SourceImpl.java

Modified: jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/query/ParserSQL2.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/query/ParserSQL2.java?rev=1294545&r1=1294544&r2=1294545&view=diff
==============================================================================
--- jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/query/ParserSQL2.java
(original)
+++ jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/query/ParserSQL2.java
Tue Feb 28 10:10:46 2012
@@ -34,12 +34,12 @@ import javax.jcr.query.qom.Literal;
 import javax.jcr.query.qom.Ordering;
 import javax.jcr.query.qom.PropertyExistence;
 import javax.jcr.query.qom.PropertyValue;
-import javax.jcr.query.qom.QueryObjectModel;
 import javax.jcr.query.qom.QueryObjectModelConstants;
-import javax.jcr.query.qom.QueryObjectModelFactory;
 import javax.jcr.query.qom.Selector;
 import javax.jcr.query.qom.Source;
 import javax.jcr.query.qom.StaticOperand;
+import org.apache.jackrabbit.query.qom.QueryObjectModelFactoryImpl;
+import org.apache.jackrabbit.query.qom.QueryObjectModelImpl;
 import org.apache.jackrabbit.query.val.ExceptionFactory;
 
 /**
@@ -78,7 +78,7 @@ public class ParserSQL2 {
     // SQL injection protection: if disabled, literals are not allowed
     private boolean allowTextLiterals = true, allowNumberLiterals = true;
 
-    private QueryObjectModelFactory factory;
+    private QueryObjectModelFactoryImpl factory;
     private ValueFactory valueFactory;
 
     /**
@@ -87,7 +87,7 @@ public class ParserSQL2 {
      * @param factory the query object model factory
      * @param valueFactory the value factory
      */
-    public ParserSQL2(QueryObjectModelFactory factory, ValueFactory valueFactory) {
+    public ParserSQL2(QueryObjectModelFactoryImpl factory, ValueFactory valueFactory) {
         this.factory = factory;
         this.valueFactory = valueFactory;
     }
@@ -99,7 +99,7 @@ public class ParserSQL2 {
      * @return the query object model
      * @throws RepositoryException if parsing failed
      */
-    public QueryObjectModel createQueryObjectModel(String query) throws RepositoryException
{
+    public QueryObjectModelImpl createQueryObjectModel(String query) throws RepositoryException
{
         initialize(query);
         selectors = new ArrayList<Selector>();
         expected = new ArrayList<String>();

Added: jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/query/QueryEngine.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/query/QueryEngine.java?rev=1294545&view=auto
==============================================================================
--- jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/query/QueryEngine.java
(added)
+++ jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/query/QueryEngine.java
Tue Feb 28 10:10:46 2012
@@ -0,0 +1,60 @@
+/*
+ * 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.query;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Map.Entry;
+import javax.jcr.RepositoryException;
+import javax.jcr.ValueFactory;
+import org.apache.jackrabbit.commons.SimpleValueFactory;
+import org.apache.jackrabbit.mk.api.MicroKernel;
+import org.apache.jackrabbit.query.qom.QueryObjectModelFactoryImpl;
+import org.apache.jackrabbit.query.qom.QueryObjectModelImpl;
+import org.apache.jackrabbit.query.reader.IndexReader;
+
+public class QueryEngine {
+
+    private final MicroKernel mk;
+    private ValueFactory vf;
+    private ParserSQL2 parserSQL2;
+    private ArrayList<IndexReader> indexes = new ArrayList<IndexReader>();
+
+    private QueryEngine(MicroKernel mk) {
+        this.mk = mk;
+        QueryObjectModelFactoryImpl qomf = new QueryObjectModelFactoryImpl();
+        vf = new SimpleValueFactory();
+        parserSQL2 = new ParserSQL2(qomf, vf);
+    }
+
+    public static QueryEngine getInstance(MicroKernel mk) {
+        return new QueryEngine(mk);
+    }
+
+    public Iterator<String> executeQuery(String query, Map<String, String> bindings)
throws RepositoryException {
+        QueryObjectModelImpl q = parserSQL2.createQueryObjectModel(query);
+        q.setMicroKernel(mk);
+        if (bindings != null) {
+            for (Entry<String, String> e : bindings.entrySet()) {
+                q.bindValue(e.getKey(), vf.createValue(e.getValue()));
+            }
+        }
+        return q.executeQuery(mk.getHeadRevision());
+    }
+
+}

Modified: jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/query/qom/QueryObjectModelFactoryImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/query/qom/QueryObjectModelFactoryImpl.java?rev=1294545&r1=1294544&r2=1294545&view=diff
==============================================================================
--- jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/query/qom/QueryObjectModelFactoryImpl.java
(original)
+++ jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/query/qom/QueryObjectModelFactoryImpl.java
Tue Feb 28 10:10:46 2012
@@ -15,7 +15,6 @@ package org.apache.jackrabbit.query.qom;
 
 import javax.jcr.RepositoryException;
 import javax.jcr.Value;
-import javax.jcr.query.Query;
 import javax.jcr.query.qom.ChildNode;
 import javax.jcr.query.qom.ChildNodeJoinCondition;
 import javax.jcr.query.qom.Column;
@@ -39,7 +38,6 @@ import javax.jcr.query.qom.Or;
 import javax.jcr.query.qom.Ordering;
 import javax.jcr.query.qom.PropertyExistence;
 import javax.jcr.query.qom.PropertyValue;
-import javax.jcr.query.qom.QueryObjectModel;
 import javax.jcr.query.qom.QueryObjectModelFactory;
 import javax.jcr.query.qom.SameNode;
 import javax.jcr.query.qom.SameNodeJoinCondition;
@@ -117,9 +115,8 @@ public class QueryObjectModelFactoryImpl
         return new ComparisonImpl((DynamicOperandImpl)operand1, Operator.getOperatorByName(operator),
(StaticOperandImpl)operand2);
     }
 
-    public QueryObjectModel createQuery(Source source, Constraint constraint, Ordering[]
orderings, Column[] columns)
+    public QueryObjectModelImpl createQuery(Source source, Constraint constraint, Ordering[]
orderings, Column[] columns)
             throws RepositoryException {
-        String statement = null;
         if (orderings != null && !(orderings instanceof OrderingImpl[])) {
             OrderingImpl[] orderings2 = new OrderingImpl[orderings.length];
             System.arraycopy(orderings, 0, orderings2, 0, orderings.length);
@@ -130,7 +127,7 @@ public class QueryObjectModelFactoryImpl
             System.arraycopy(columns, 0, columns2, 0, columns.length);
             columns = columns2;
         }
-        QueryObjectModelImpl qom = new QueryObjectModelImpl(Query.JCR_SQL2, statement, (SourceImpl)
source,
+        QueryObjectModelImpl qom = new QueryObjectModelImpl((SourceImpl) source,
                 (ConstraintImpl) constraint, (OrderingImpl[]) orderings, (ColumnImpl[]) columns);
         qom.init();
         return qom;

Modified: jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/query/qom/QueryObjectModelImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/query/qom/QueryObjectModelImpl.java?rev=1294545&r1=1294544&r2=1294545&view=diff
==============================================================================
--- jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/query/qom/QueryObjectModelImpl.java
(original)
+++ jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/query/qom/QueryObjectModelImpl.java
Tue Feb 28 10:10:46 2012
@@ -14,6 +14,7 @@
 package org.apache.jackrabbit.query.qom;
 
 import java.util.HashMap;
+import java.util.Iterator;
 import javax.jcr.Node;
 import javax.jcr.RepositoryException;
 import javax.jcr.Value;
@@ -21,6 +22,7 @@ import javax.jcr.ValueFactory;
 import javax.jcr.query.QueryResult;
 import javax.jcr.query.qom.QueryObjectModel;
 import org.apache.jackrabbit.commons.SimpleValueFactory;
+import org.apache.jackrabbit.mk.api.MicroKernel;
 import org.apache.jackrabbit.query.qom.tree.BindVariableValueImpl;
 import org.apache.jackrabbit.query.qom.tree.ChildNodeImpl;
 import org.apache.jackrabbit.query.qom.tree.ChildNodeJoinConditionImpl;
@@ -41,6 +43,7 @@ import org.apache.jackrabbit.query.qom.t
 import org.apache.jackrabbit.query.qom.tree.SameNodeJoinConditionImpl;
 import org.apache.jackrabbit.query.qom.tree.SelectorImpl;
 import org.apache.jackrabbit.query.qom.tree.SourceImpl;
+import org.apache.jackrabbit.query.reader.Cursor;
 
 /**
  * The implementation of the corresponding JCR interface. Lifecycle: use the
@@ -49,29 +52,28 @@ import org.apache.jackrabbit.query.qom.t
  */
 public class QueryObjectModelImpl implements QueryObjectModel {
 
-    private final String language;
-    private final String statement;
-    private final SourceImpl source;
+    private MicroKernel mk;
+    final SourceImpl source;
     private final ConstraintImpl constraint;
     private final OrderingImpl[] orderings;
     private final ColumnImpl[] columns;
     final HashMap<String, Value> bindVariableMap = new HashMap<String, Value>();
     private long limit;
     private long offset;
+    private boolean prepared;
 
     private ValueFactory valueFactory = new SimpleValueFactory();
 
-    public QueryObjectModelImpl(String language, String statement, SourceImpl source, ConstraintImpl
constraint, OrderingImpl[] orderings,
+    public QueryObjectModelImpl(SourceImpl source, ConstraintImpl constraint, OrderingImpl[]
orderings,
             ColumnImpl[] columns) {
-        this.language = language;
-        this.statement = statement;
         this.source = source;
         this.constraint = constraint;
         this.orderings = orderings;
         this.columns = columns;
     }
 
-    void init() {
+    public void init() {
+
         new QOMTreeVisitor() {
 
             @Override
@@ -179,8 +181,11 @@ public class QueryObjectModelImpl implem
         bindVariableMap.put(varName, value);
     }
 
-    public QueryResult execute() throws RepositoryException {
+    public void setMicroKernel(MicroKernel mk) {
+        this.mk = mk;
+    }
 
+    public QueryResult execute() throws RepositoryException {
         // TODO
         return null;
     }
@@ -192,11 +197,11 @@ public class QueryObjectModelImpl implem
     }
 
     public String getLanguage() {
-        return language;
+        return null;
     }
 
     public String getStatement() {
-        return statement;
+        return null;
     }
 
     public String getStoredQueryPath() throws RepositoryException {
@@ -222,4 +227,71 @@ public class QueryObjectModelImpl implem
         return valueFactory;
     }
 
+    public Iterator<String> executeQuery(String revisionId) {
+        prepare();
+        ResultIterator result = new ResultIterator(revisionId);
+        return result;
+    }
+
+    private void prepare() {
+        if (prepared) {
+            return;
+        }
+        source.prepare(mk);
+    }
+
+    class ResultIterator implements Iterator<String> {
+
+        private final String revisionId;
+        private Cursor cursor;
+        private String current;
+        private boolean end;
+
+        ResultIterator(String revisionId) {
+            this.revisionId = revisionId;
+        }
+
+        private void fetchNext() {
+            if (cursor == null) {
+                cursor = source.execute(revisionId);
+            }
+            if (cursor.next()) {
+                current = cursor.currentPath();
+            } else {
+                current = null;
+                end = true;
+            }
+        }
+
+        @Override
+        public boolean hasNext() {
+            if (end) {
+                return false;
+            }
+            if (current == null) {
+                fetchNext();
+            }
+            return !end;
+        }
+
+        @Override
+        public String next() {
+            if (end) {
+                return null;
+            }
+            if (current == null) {
+                fetchNext();
+            }
+            String r = current;
+            current = null;
+            return r;
+        }
+
+        @Override
+        public void remove() {
+            throw new UnsupportedOperationException();
+        }
+
+    };
+
 }

Modified: jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/query/qom/tree/QOMTreeVisitor.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/query/qom/tree/QOMTreeVisitor.java?rev=1294545&r1=1294544&r2=1294545&view=diff
==============================================================================
--- jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/query/qom/tree/QOMTreeVisitor.java
(original)
+++ jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/query/qom/tree/QOMTreeVisitor.java
Tue Feb 28 10:10:46 2012
@@ -118,8 +118,10 @@ public abstract class QOMTreeVisitor imp
             constraint.accept(this);
         }
         OrderingImpl[] orderings = node.getOrderings();
-        for (OrderingImpl ordering : orderings) {
-            ordering.accept(this);
+        if (orderings != null) {
+            for (OrderingImpl ordering : orderings) {
+                ordering.accept(this);
+            }
         }
         ColumnImpl[] columns = node.getColumns();
         for (ColumnImpl column : columns) {

Modified: jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/query/qom/tree/SelectorImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/query/qom/tree/SelectorImpl.java?rev=1294545&r1=1294544&r2=1294545&view=diff
==============================================================================
--- jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/query/qom/tree/SelectorImpl.java
(original)
+++ jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/query/qom/tree/SelectorImpl.java
Tue Feb 28 10:10:46 2012
@@ -19,7 +19,7 @@
 package org.apache.jackrabbit.query.qom.tree;
 
 import javax.jcr.query.qom.Selector;
-import org.apache.jackrabbit.mk.mem.NodeImpl;
+import org.apache.jackrabbit.mk.simple.NodeImpl;
 import org.apache.jackrabbit.query.qom.QueryObjectModelImpl;
 
 /**

Modified: jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/query/qom/tree/SourceImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/query/qom/tree/SourceImpl.java?rev=1294545&r1=1294544&r2=1294545&view=diff
==============================================================================
--- jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/query/qom/tree/SourceImpl.java
(original)
+++ jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/query/qom/tree/SourceImpl.java
Tue Feb 28 10:10:46 2012
@@ -19,13 +19,20 @@
 package org.apache.jackrabbit.query.qom.tree;
 
 import javax.jcr.query.qom.Source;
+import org.apache.jackrabbit.mk.api.MicroKernel;
 import org.apache.jackrabbit.query.qom.QueryObjectModelImpl;
+import org.apache.jackrabbit.query.reader.Cursor;
+import org.apache.jackrabbit.query.reader.Filter;
+import org.apache.jackrabbit.query.reader.NodeReader;
+import org.apache.jackrabbit.query.reader.TraversingReader;
 
 public abstract class SourceImpl extends QOMNode implements Source {
 
     protected ConstraintImpl queryConstraint;
     protected JoinConditionImpl joinCondition;
+    protected boolean join;
     protected boolean outerJoin;
+    private NodeReader reader;
 
     public void setQueryConstraint(ConstraintImpl queryConstraint) {
         this.queryConstraint = queryConstraint;
@@ -40,4 +47,13 @@ public abstract class SourceImpl extends
     }
 
     public abstract void init(QueryObjectModelImpl qom);
+
+    public void prepare(MicroKernel mk) {
+        reader = new TraversingReader(mk);
+    }
+
+    public Cursor execute(String revisionId) {
+        return reader.query(new Filter(), revisionId);
+    }
+
 }

Added: jackrabbit/sandbox/jackrabbit-microkernel/src/test/java/org/apache/jackrabbit/query/QueryTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/jackrabbit-microkernel/src/test/java/org/apache/jackrabbit/query/QueryTest.java?rev=1294545&view=auto
==============================================================================
--- jackrabbit/sandbox/jackrabbit-microkernel/src/test/java/org/apache/jackrabbit/query/QueryTest.java
(added)
+++ jackrabbit/sandbox/jackrabbit-microkernel/src/test/java/org/apache/jackrabbit/query/QueryTest.java
Tue Feb 28 10:10:46 2012
@@ -0,0 +1,47 @@
+/*
+ * 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.query;
+
+import static org.junit.Assert.assertEquals;
+import java.util.Iterator;
+import javax.jcr.RepositoryException;
+import org.apache.jackrabbit.mk.MicroKernelFactory;
+import org.apache.jackrabbit.mk.api.MicroKernel;
+import org.junit.Test;
+
+public class QueryTest {
+
+    @Test
+    public void simple() throws RepositoryException {
+        MicroKernel mk = MicroKernelFactory.getInstance("simple:/target/temp;clear");
+        String head = mk.getHeadRevision();
+
+        head = mk.commit("/", "+ \"test\": { \"hello\": {}, \"world\": {} }", head, "");
+        head = mk.commit("/", "+ \"test2\": { \"id\":\"1\" }", head, "");
+
+        QueryEngine qe = QueryEngine.getInstance(mk);
+        Iterator<String> list = qe.executeQuery("select * from [nt:base]", null);
+        StringBuilder buff = new StringBuilder();
+        while (list.hasNext()) {
+            buff.append(list.next()).append('\n');
+        }
+        assertEquals(
+                "/test/hello\n" +
+                "/test/world\n" +
+                "/test\n" +
+                "/test2\n", buff.toString());
+
+    }
+
+}

Added: jackrabbit/sandbox/jackrabbit-microkernel/src/test/java/org/apache/jackrabbit/query/SQL2Test.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/jackrabbit-microkernel/src/test/java/org/apache/jackrabbit/query/SQL2Test.java?rev=1294545&view=auto
==============================================================================
--- jackrabbit/sandbox/jackrabbit-microkernel/src/test/java/org/apache/jackrabbit/query/SQL2Test.java
(added)
+++ jackrabbit/sandbox/jackrabbit-microkernel/src/test/java/org/apache/jackrabbit/query/SQL2Test.java
Tue Feb 28 10:10:46 2012
@@ -0,0 +1,32 @@
+/*
+ * 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.query;
+
+
+public class SQL2Test {
+
+    public void test() throws Exception {
+//        Node n = session.getRootNode().addNode("testQuery");
+//        session.save();
+//        QueryManager qm = session.getWorkspace().getQueryManager();
+//        Query q = qm.createQuery("select * from test", Query.JCR_SQL2);
+//        String stat = q.getStatement();
+    }
+
+}

Added: jackrabbit/sandbox/jackrabbit-microkernel/src/test/java/org/apache/jackrabbit/query/XPathTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/jackrabbit-microkernel/src/test/java/org/apache/jackrabbit/query/XPathTest.java?rev=1294545&view=auto
==============================================================================
--- jackrabbit/sandbox/jackrabbit-microkernel/src/test/java/org/apache/jackrabbit/query/XPathTest.java
(added)
+++ jackrabbit/sandbox/jackrabbit-microkernel/src/test/java/org/apache/jackrabbit/query/XPathTest.java
Tue Feb 28 10:10:46 2012
@@ -0,0 +1,93 @@
+/*
+ * 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.query;
+
+public class XPathTest {
+
+    public void test() throws Exception {
+//        Node n = session.getRootNode().addNode("testQuery");
+//        session.save();
+//        QueryManager qm = session.getWorkspace().getQueryManager();
+//        Query q = qm.createQuery("//*", Query.XPATH);
+//        String stat = q.getStatement();
+    }
+
+    // Axes
+    // child (in abbreviated syntax this is the default axis, represented simply by /, the
location path separator).
+    // descendant-or-self (abbreviated syntax: //).
+    // attribute (abbreviated syntax: @).
+
+    // Predicates
+    // //element(*, employee)[@secretary and @assistant]
+
+    // SELECT * FROM nt:base
+    // //*
+
+    // SELECT * FROM my:type
+    // //element(*, my:type)
+
+    // SELECT my:title FROM my:type
+    // //element(*, my:type)/@my:title
+
+    // SELECT my:title, my:text FROM my:type
+    // //element(*, my:type)/(@my:title | @my:text)
+
+    // SELECT * FROM my:type WHERE jcr:path LIKE '/nodes[%]/%'
+    // /jcr:root/nodes//element(*, my:type)
+
+    // SELECT * FROM my:type WHERE my:title='JSR 170'
+    // //element(*, my:type)[@my:title = 'JSR 170']
+
+    // SELECT * FROM my:type WHERE jcr:path LIKE '/some[%]/nodes[%]'
+    // /jcr:root/some/element(nodes, my:type)
+
+    // SELECT * FROM my:type WHERE jcr:path = '/some/nodes'
+    // /jcr:root/some[1]/element(nodes, my:type)[1]
+
+    // SELECT * FROM my:type
+    // WHERE jcr:path LIKE '/some[%]/nodes[%]/%' AND NOT jcr:path LIKE '/some[%]/nodes[%]/%/%'
+    // /jcr:root/some/nodes/element(*, my:type)
+
+    // SELECT * FROM my:type WHERE jcr:path LIKE '/some/nodes/%' AND NOT jcr:path LIKE '/some/nodes/%/%'
+    // /jcr:root/some[1]/nodes[1]/element(*, my:type)
+
+    // SELECT * FROM my:type WHERE jcr:path LIKE '/some[%]/nodes[%]/%'
+    // /jcr:root/some/nodes//element(*, my:type)
+
+    // SELECT * FROM my:type WHERE jcr:path LIKE '/some/nodes/%'
+    // /jcr:root/some[1]/nodes[1]//element(*, my:type)
+
+    // SELECT * FROM my:type
+    // WHERE jcr:path LIKE '/some[%]/nodes[%]' OR jcr:path LIKE '/some[%]/nodes[%]/%'
+    // /jcr:root/some/nodes//element(*, my:type)
+
+    // SELECT * FROM my:type
+    // WHERE jcr:path = '/some/nodes' OR jcr:path LIKE '/some/nodes/%'
+    // /jcr:root/some[1]/nodes[1]//element(*, my:type)
+
+    // SELECT * FROM my:type ORDER BY my:title
+    // //element(*, my:type) order by @my:title
+
+    // SELECT * FROM my:type ORDER BY my:date DESC, my:title ASC
+    // //element(*, my:type) order by @my:date descending, @my:title ascending
+
+    // SELECT * FROM my:type WHERE
+    // CONTAINS(*, 'jcr') ORDER BY jcr:score DESC
+    // //element(*, my:type)[jcr:contains(., 'jcr')] order by jcr:score() descending
+
+    //
+}

Added: jackrabbit/sandbox/jackrabbit-microkernel/src/test/java/org/apache/jackrabbit/query/qom/QomTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/jackrabbit-microkernel/src/test/java/org/apache/jackrabbit/query/qom/QomTest.java?rev=1294545&view=auto
==============================================================================
--- jackrabbit/sandbox/jackrabbit-microkernel/src/test/java/org/apache/jackrabbit/query/qom/QomTest.java
(added)
+++ jackrabbit/sandbox/jackrabbit-microkernel/src/test/java/org/apache/jackrabbit/query/qom/QomTest.java
Tue Feb 28 10:10:46 2012
@@ -0,0 +1,305 @@
+/*
+ * 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.query.qom;
+
+import static org.junit.Assert.assertEquals;
+import javax.jcr.RepositoryException;
+import javax.jcr.Value;
+import javax.jcr.ValueFactory;
+import javax.jcr.query.qom.And;
+import javax.jcr.query.qom.BindVariableValue;
+import javax.jcr.query.qom.ChildNode;
+import javax.jcr.query.qom.ChildNodeJoinCondition;
+import javax.jcr.query.qom.Column;
+import javax.jcr.query.qom.Comparison;
+import javax.jcr.query.qom.Constraint;
+import javax.jcr.query.qom.DescendantNode;
+import javax.jcr.query.qom.DescendantNodeJoinCondition;
+import javax.jcr.query.qom.EquiJoinCondition;
+import javax.jcr.query.qom.FullTextSearch;
+import javax.jcr.query.qom.FullTextSearchScore;
+import javax.jcr.query.qom.Join;
+import javax.jcr.query.qom.Length;
+import javax.jcr.query.qom.Literal;
+import javax.jcr.query.qom.LowerCase;
+import javax.jcr.query.qom.NodeLocalName;
+import javax.jcr.query.qom.NodeName;
+import javax.jcr.query.qom.Not;
+import javax.jcr.query.qom.Or;
+import javax.jcr.query.qom.Ordering;
+import javax.jcr.query.qom.PropertyExistence;
+import javax.jcr.query.qom.PropertyValue;
+import javax.jcr.query.qom.QueryObjectModel;
+import javax.jcr.query.qom.QueryObjectModelConstants;
+import javax.jcr.query.qom.QueryObjectModelFactory;
+import javax.jcr.query.qom.SameNode;
+import javax.jcr.query.qom.SameNodeJoinCondition;
+import javax.jcr.query.qom.Selector;
+import javax.jcr.query.qom.Source;
+import javax.jcr.query.qom.UpperCase;
+import org.apache.jackrabbit.commons.SimpleValueFactory;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Tests the QueryObjectModelFactory and other QOM classes.
+ */
+public class QomTest {
+
+    QueryObjectModelFactory f = new QueryObjectModelFactoryImpl();
+    private ValueFactory vf;
+
+    @Before
+    public void setup() {
+        vf = new SimpleValueFactory();
+    }
+
+    @Test
+    public void and() throws RepositoryException {
+        Constraint c0 = f.propertyExistence("x", "c0");
+        Constraint c1 = f.propertyExistence("x", "c1");
+        And and = f.and(c0, c1);
+        assertEquals(and.getConstraint1(), c0);
+        assertEquals(and.getConstraint2(), c1);
+    }
+
+    @Test
+    public void ascending() throws RepositoryException {
+        PropertyValue p = f.propertyValue("selectorName", "propertyName");
+        Ordering o = f.ascending(p);
+        assertEquals(p, o.getOperand());
+        assertEquals(QueryObjectModelConstants.JCR_ORDER_ASCENDING, o.getOrder());
+    }
+
+    @Test
+    public void bindVariable() throws RepositoryException {
+        BindVariableValue b = f.bindVariable("bindVariableName");
+        assertEquals("bindVariableName", b.getBindVariableName());
+    }
+
+    @Test
+    public void childNode() throws RepositoryException {
+        ChildNode cn = f.childNode("selectorName", "parentPath");
+        assertEquals("selectorName", cn.getSelectorName());
+        assertEquals("parentPath", cn.getParentPath());
+    }
+
+    @Test
+    public void childNodeJoinCondition() throws RepositoryException {
+        ChildNodeJoinCondition c = f.childNodeJoinCondition("childSelectorName",
+                "parentSelectorName");
+        assertEquals("childSelectorName", c.getChildSelectorName());
+        assertEquals("parentSelectorName", c.getParentSelectorName());
+    }
+
+    @Test
+    public void column() throws RepositoryException {
+        Column c = f.column("selectorName", "propertyName", "columnName");
+        assertEquals("selectorName", c.getSelectorName());
+        assertEquals("propertyName", c.getPropertyName());
+        assertEquals("columnName", c.getColumnName());
+    }
+
+    @Test
+    public void comparison() throws RepositoryException {
+        PropertyValue p = f.propertyValue("selectorName", "propertyName");
+        Literal l = f.literal(vf.createValue(1));
+        Comparison c = f.comparison(p, QueryObjectModelConstants.JCR_OPERATOR_EQUAL_TO, l);
+        assertEquals(p, c.getOperand1());
+        assertEquals(QueryObjectModelConstants.JCR_OPERATOR_EQUAL_TO, c.getOperator());
+        assertEquals(l, c.getOperand2());
+    }
+
+    @Test
+    public void descendantNode() throws RepositoryException {
+        DescendantNode d = f.descendantNode("selectorName", "path");
+        assertEquals("selectorName", d.getSelectorName());
+        assertEquals("path", d.getAncestorPath());
+    }
+
+    @Test
+    public void descendantNodeJoinCondition() throws RepositoryException {
+        DescendantNodeJoinCondition d = f.descendantNodeJoinCondition("descendantSelectorName",
+                "ancestorSelectorName");
+        assertEquals("descendantSelectorName", d.getDescendantSelectorName());
+        assertEquals("ancestorSelectorName", d.getAncestorSelectorName());
+    }
+
+    @Test
+    public void descending() throws RepositoryException {
+        PropertyValue p = f.propertyValue("selectorName", "propertyName");
+        Ordering o = f.descending(p);
+        assertEquals(p, o.getOperand());
+        assertEquals(QueryObjectModelConstants.JCR_ORDER_DESCENDING, o.getOrder());
+    }
+
+    @Test
+    public void equiJoinCondition() throws RepositoryException {
+        EquiJoinCondition e = f.equiJoinCondition("selector1Name", "property1Name",
+                "selector2Name", "property2Name");
+        assertEquals("selector1Name", e.getSelector1Name());
+        assertEquals("property1Name", e.getProperty1Name());
+        assertEquals("selector2Name", e.getSelector2Name());
+        assertEquals("property2Name", e.getProperty2Name());
+    }
+
+    @Test
+    public void fullTextSearch() throws RepositoryException {
+        Literal l = f.literal(vf.createValue(1));
+        FullTextSearch x = f.fullTextSearch("selectorName", "propertyName", l);
+        assertEquals("selectorName", x.getSelectorName());
+        assertEquals("propertyName", x.getPropertyName());
+        assertEquals(l, x.getFullTextSearchExpression());
+    }
+
+    @Test
+    public void fullTextSearchScore() throws RepositoryException {
+        FullTextSearchScore x = f.fullTextSearchScore("selectorName");
+        assertEquals("selectorName", x.getSelectorName());
+    }
+
+    @Test
+    public void join() throws RepositoryException {
+        Source left = f.selector("nodeTypeName", "selectorName");
+        Source right = f.selector("nodeTypeName2", "selectorName2");
+        ChildNodeJoinCondition jc = f.childNodeJoinCondition("childSelectorName", "parentSelectorName");
+        Join j = f.join(left, right, QueryObjectModelConstants.JCR_JOIN_TYPE_INNER, jc);
+        assertEquals(left, j.getLeft());
+        assertEquals(right, j.getRight());
+        assertEquals(QueryObjectModelConstants.JCR_JOIN_TYPE_INNER, j.getJoinType());
+        assertEquals(jc, j.getJoinCondition());
+    }
+
+    @Test
+    public void length() throws RepositoryException {
+        PropertyValue p = f.propertyValue("selectorName", "propertyName");
+        Length l = f.length(p);
+        assertEquals(p, l.getPropertyValue());
+    }
+
+    @Test
+    public void literal() throws RepositoryException {
+        Value v = vf.createValue(1);
+        Literal l = f.literal(v);
+        assertEquals(v, l.getLiteralValue());
+    }
+
+    @Test
+    public void lowerCase() throws RepositoryException {
+        PropertyValue p = f.propertyValue("selectorName", "propertyName");
+        Length length = f.length(p);
+        LowerCase l = f.lowerCase(length);
+        assertEquals(length, l.getOperand());
+    }
+
+    @Test
+    public void nodeLocalName() throws RepositoryException {
+        NodeLocalName n = f.nodeLocalName("selectorName");
+        assertEquals("selectorName", n.getSelectorName());
+    }
+
+    @Test
+    public void nodeName() throws RepositoryException {
+        NodeName n = f.nodeName("selectorName");
+        assertEquals("selectorName", n.getSelectorName());
+    }
+
+    @Test
+    public void not() throws RepositoryException {
+        Constraint c = f.propertyExistence("x", "c0");
+        Not n = f.not(c);
+        assertEquals(c, n.getConstraint());
+    }
+
+    @Test
+    public void or() throws RepositoryException {
+        Constraint c0 = f.propertyExistence("x", "c0");
+        Constraint c1 = f.propertyExistence("x", "c1");
+        Or or = f.or(c0, c1);
+        assertEquals(or.getConstraint1(), c0);
+        assertEquals(or.getConstraint2(), c1);
+    }
+
+    @Test
+    public void propertyExistence() throws RepositoryException {
+        PropertyExistence pe = f.propertyExistence("selectorName", "propertyName");
+        assertEquals("selectorName", pe.getSelectorName());
+        assertEquals("propertyName", pe.getPropertyName());
+    }
+
+    @Test
+    public void propertyValue() throws RepositoryException {
+        PropertyValue pv = f.propertyValue("selectorName", "propertyName");
+        assertEquals("selectorName", pv.getSelectorName());
+        assertEquals("propertyName", pv.getPropertyName());
+    }
+
+    @Test
+    public void sameNode() throws RepositoryException {
+        SameNode s = f.sameNode("selectorName", "path");
+        assertEquals("selectorName", s.getSelectorName());
+        assertEquals("path", s.getPath());
+    }
+
+    @Test
+    public void sameNodeJoinCondition() throws RepositoryException {
+        SameNodeJoinCondition s = f.sameNodeJoinCondition("selector1Name", "selector2Name",
"selector2Path");
+        assertEquals("selector1Name", s.getSelector1Name());
+        assertEquals("selector2Name", s.getSelector2Name());
+        assertEquals("selector2Path", s.getSelector2Path());
+    }
+
+    @Test
+    public void selector() throws RepositoryException {
+        Selector s = f.selector("nodeTypeName", "selectorName");
+        assertEquals("nodeTypeName", s.getNodeTypeName());
+        assertEquals("selectorName", s.getSelectorName());
+    }
+
+    @Test
+    public void upperCase() throws RepositoryException {
+        PropertyValue p = f.propertyValue("selectorName", "propertyName");
+        Length length = f.length(p);
+        UpperCase u = f.upperCase(length);
+        assertEquals(length, u.getOperand());
+    }
+
+    @Test
+    public void createQuery() throws RepositoryException {
+        Selector s = f.selector("nodeTypeName", "selectorName");
+        BindVariableValue b = f.bindVariable("var");
+        Constraint c = f.propertyExistence("x", "c");
+        PropertyValue p = f.propertyValue("selectorName", "propertyName");
+        c = f.and(f.comparison(p, QueryObjectModelConstants.JCR_OPERATOR_EQUAL_TO, b), c);
+        Ordering o = f.ascending(p);
+        Column col = f.column("selectorName", "propertyName", "columnName");
+        Ordering[] ords = new Ordering[]{o};
+        Column[] cols = new Column[]{col};
+        QueryObjectModel q = f.createQuery(s, c, ords, cols);
+        // assertEquals(Query.JCR_SQL2, q.getLanguage());
+        String[] bv = q.getBindVariableNames();
+        assertEquals(1, bv.length);
+        assertEquals("var", bv[0]);
+        assertEquals(s, q.getSource());
+        assertEquals(c, q.getConstraint());
+        assertEquals(o, q.getOrderings()[0]);
+        assertEquals(col, q.getColumns()[0]);
+    }
+
+}



Mime
View raw message