You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jackrabbit.apache.org by th...@apache.org on 2011/05/03 17:08:16 UTC
svn commit: r1099087 - in /jackrabbit/branches/2.2/jackrabbit-core/src:
main/java/org/apache/jackrabbit/core/query/lucene/
test/java/org/apache/jackrabbit/core/query/
Author: thomasm
Date: Tue May 3 15:08:16 2011
New Revision: 1099087
URL: http://svn.apache.org/viewvc?rev=1099087&view=rev
Log:
JCR-2956 SQL2: Implement LIKE support for node names
Added:
jackrabbit/branches/2.2/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/query/SQL2NodeLocalNameTest.java
Modified:
jackrabbit/branches/2.2/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/LuceneQueryFactory.java
jackrabbit/branches/2.2/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/query/AbstractQueryTest.java
Modified: jackrabbit/branches/2.2/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/LuceneQueryFactory.java
URL: http://svn.apache.org/viewvc/jackrabbit/branches/2.2/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/LuceneQueryFactory.java?rev=1099087&r1=1099086&r2=1099087&view=diff
==============================================================================
--- jackrabbit/branches/2.2/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/LuceneQueryFactory.java (original)
+++ jackrabbit/branches/2.2/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/LuceneQueryFactory.java Tue May 3 15:08:16 2011
@@ -331,10 +331,8 @@ public class LuceneQueryFactory {
StaticOperand right = c.getOperand2();
if (left instanceof Length
|| left instanceof FullTextSearchScore
- || ((!JCR_OPERATOR_EQUAL_TO.equals(operator)
- || transform.transform != TRANSFORM_NONE)
- && (left instanceof NodeName
- || left instanceof NodeLocalName))) {
+ || (((!JCR_OPERATOR_EQUAL_TO.equals(operator) && !JCR_OPERATOR_LIKE
+ .equals(operator)) || transform.transform != TRANSFORM_NONE) && (left instanceof NodeName || left instanceof NodeLocalName))) {
try {
int type = PropertyType.UNDEFINED;
if (left instanceof Length) {
@@ -616,15 +614,18 @@ public class LuceneQueryFactory {
}
}
- protected Query getNodeLocalNameQuery(
- int transform, String operator, StaticOperand right)
- throws RepositoryException {
+ protected Query getNodeLocalNameQuery(int transform, String operator,
+ StaticOperand right) throws RepositoryException {
if (transform != TRANSFORM_NONE
- || !JCR_OPERATOR_EQUAL_TO.equals(operator)) {
+ || (!JCR_OPERATOR_EQUAL_TO.equals(operator) && !JCR_OPERATOR_LIKE
+ .equals(operator))) {
throw new UnsupportedRepositoryOperationException();
}
-
String name = evaluator.getValue(right).getString();
+
+ if (JCR_OPERATOR_LIKE.equals(operator)) {
+ return new WildcardQuery(LOCAL_NAME, null, name, transform, cache);
+ }
return new JackrabbitTermQuery(new Term(LOCAL_NAME, name));
}
Modified: jackrabbit/branches/2.2/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/query/AbstractQueryTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/branches/2.2/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/query/AbstractQueryTest.java?rev=1099087&r1=1099086&r2=1099087&view=diff
==============================================================================
--- jackrabbit/branches/2.2/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/query/AbstractQueryTest.java (original)
+++ jackrabbit/branches/2.2/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/query/AbstractQueryTest.java Tue May 3 15:08:16 2011
@@ -16,6 +16,8 @@
*/
package org.apache.jackrabbit.core.query;
+import static javax.jcr.query.Query.JCR_SQL2;
+
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
@@ -124,11 +126,11 @@ public class AbstractQueryTest extends A
* @return the elements of the iterator as an array of Nodes.
*/
protected Node[] toArray(NodeIterator it) {
- List nodes = new ArrayList();
+ List<Node> nodes = new ArrayList<Node>();
while (it.hasNext()) {
nodes.add(it.nextNode());
}
- return (Node[]) nodes.toArray(new Node[nodes.size()]);
+ return nodes.toArray(new Node[nodes.size()]);
}
/**
@@ -201,22 +203,22 @@ public class AbstractQueryTest extends A
protected void checkResult(NodeIterator result, Node[] nodes)
throws RepositoryException {
// collect paths
- Set expectedPaths = new HashSet();
- for (int i = 0; i < nodes.length; i++) {
- expectedPaths.add(nodes[i].getPath());
+ Set<String> expectedPaths = new HashSet<String>();
+ for (Node n : nodes) {
+ expectedPaths.add(n.getPath());
}
- Set resultPaths = new HashSet();
+ Set<String> resultPaths = new HashSet<String>();
while (result.hasNext()) {
resultPaths.add(result.nextNode().getPath());
}
// check if all expected are in result
- for (Iterator it = expectedPaths.iterator(); it.hasNext();) {
- String path = (String) it.next();
+ for (Iterator<String> it = expectedPaths.iterator(); it.hasNext();) {
+ String path = it.next();
assertTrue(path + " is not part of the result set", resultPaths.contains(path));
}
// check result does not contain more than expected
- for (Iterator it = resultPaths.iterator(); it.hasNext();) {
- String path = (String) it.next();
+ for (Iterator<String> it = resultPaths.iterator(); it.hasNext();) {
+ String path = it.next();
assertTrue(path + " is not expected to be part of the result set", expectedPaths.contains(path));
}
}
@@ -255,6 +257,11 @@ public class AbstractQueryTest extends A
return qm.createQuery(statement, Query.XPATH).execute();
}
}
+
+ protected QueryResult executeSQL2Query(String statement)
+ throws RepositoryException {
+ return qm.createQuery(statement, JCR_SQL2).execute();
+ }
/**
* Returns a reference to the underlying search index.
Added: jackrabbit/branches/2.2/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/query/SQL2NodeLocalNameTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/branches/2.2/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/query/SQL2NodeLocalNameTest.java?rev=1099087&view=auto
==============================================================================
--- jackrabbit/branches/2.2/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/query/SQL2NodeLocalNameTest.java (added)
+++ jackrabbit/branches/2.2/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/query/SQL2NodeLocalNameTest.java Tue May 3 15:08:16 2011
@@ -0,0 +1,94 @@
+/*
+ * 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.core.query;
+
+import javax.jcr.Node;
+
+import org.apache.jackrabbit.commons.JcrUtils;
+
+/**
+ * Test case for Node LocalName queries with JCR_SQL2
+ *
+ * Inspired by <a
+ * href="https://issues.apache.org/jira/browse/JCR-2956">JCR-2956</a>
+ */
+public class SQL2NodeLocalNameTest extends AbstractQueryTest {
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+ testRootNode.addNode("SQL2NodeLocalNameTest_node1");
+ testRootNode.addNode("SQL2NodeLocalNameTest_node2");
+ testRootNode.getSession().save();
+ }
+
+ @Override
+ protected void tearDown() throws Exception {
+ for (Node c : JcrUtils.getChildNodes(testRootNode)) {
+ testRootNode.getSession().removeItem(c.getPath());
+ }
+ superuser.save();
+ super.tearDown();
+ }
+
+ public void testEq() throws Exception {
+ checkResult(
+ executeSQL2Query("SELECT * FROM [nt:base] as NODE WHERE localname(NODE) = 'SQL2NodeLocalNameTest_node1' "),
+ 1);
+ }
+
+ public void testLikeEnd() throws Exception {
+ checkResult(
+ executeSQL2Query("SELECT * FROM [nt:base] as NODE WHERE localname(NODE) like 'SQL2NodeLocalNameTest%' "),
+ 2);
+ }
+
+ public void testLikeMiddle() throws Exception {
+ checkResult(
+ executeSQL2Query("SELECT * FROM [nt:base] as NODE WHERE localname(NODE) like '%NodeLocalNameTest%' "),
+ 2);
+ }
+
+ public void testLikeNoHits() throws Exception {
+ checkResult(
+ executeSQL2Query("SELECT * FROM [nt:base] as NODE WHERE localname(NODE) like 'XXX_NodeLocalNameTest_XXX' "),
+ 0);
+ }
+
+ /**
+ * funny enough, this will return the <b>root</b> node
+ */
+ public void testLikeEmpty() throws Exception {
+ checkResult(
+ executeSQL2Query("SELECT * FROM [nt:base] as NODE WHERE localname(NODE) like '' "),
+ 1);
+ }
+
+ public void testLikeEmptyAsChild() throws Exception {
+ checkResult(
+ executeSQL2Query("SELECT * FROM [nt:base] as NODE WHERE ischildnode(NODE, ["
+ + testRootNode.getPath()
+ + "]) AND localname(NODE) like '' "), 0);
+ }
+
+ public void testLikeAllAsChild() throws Exception {
+ checkResult(
+ executeSQL2Query("SELECT * FROM [nt:base] as NODE WHERE ischildnode(NODE, ["
+ + testRootNode.getPath()
+ + "]) AND localname(NODE) like '%' "), 2);
+ }
+}