jackrabbit-oak-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From thom...@apache.org
Subject svn commit: r1303334 - in /jackrabbit/oak/trunk/oak-core/src: main/java/org/apache/jackrabbit/oak/query/ main/java/org/apache/jackrabbit/oak/query/ast/ test/java/org/apache/jackrabbit/oak/query/
Date Wed, 21 Mar 2012 09:53:11 GMT
Author: thomasm
Date: Wed Mar 21 09:53:10 2012
New Revision: 1303334

URL: http://svn.apache.org/viewvc?rev=1303334&view=rev
Log:
OAK-28 Query implementation (bind variables)

Modified:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/Query.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/QueryEngine.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/Row.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/BindVariableValueImpl.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/LengthImpl.java
    jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/query/QueryTest.java

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/Query.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/Query.java?rev=1303334&r1=1303333&r2=1303334&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/Query.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/Query.java
Wed Mar 21 09:53:10 2012
@@ -182,6 +182,7 @@ public class Query {
 
             @Override
             public boolean visit(SelectorImpl node) {
+                selectors.add(node);
                 node.setQuery(query);
                 return true;
             }
@@ -284,18 +285,15 @@ public class Query {
         for (int i = 0, size = orderings.length; i < size; i++) {
             Value a = orderValues[i];
             Value b = orderValues2[i];
-            // TODO order by: currently use string compare
-            String as = a == null ? null : a.getString();
-            String bs = b == null ? null : b.getString();
-            if (as == null || bs == null) {
-                if (as == bs) {
+            if (a == null || b == null) {
+                if (a == b) {
                     comp = 0;
-                } else if (as == null) {
+                } else if (a == null) {
                     // TODO order by: nulls first, last, low or high?
                     comp = 1;
                 }
             } else {
-                comp = as.compareTo(bs);
+                comp = a.compareTo(b);
             }
             if (comp != 0) {
                 if (orderings[i].isDescending()) {
@@ -430,4 +428,12 @@ public class Query {
         return offset;
     }
 
+    public Value getBindVariableValue(String bindVariableName) {
+        Value v = bindVariableMap.get(bindVariableName);
+        if (v == null) {
+            throw new RuntimeException("Bind variable value not set: " + bindVariableName);
+        }
+        return v;
+    }
+
 }

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/QueryEngine.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/QueryEngine.java?rev=1303334&r1=1303333&r2=1303334&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/QueryEngine.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/QueryEngine.java
Wed Mar 21 09:53:10 2012
@@ -40,7 +40,7 @@ public class QueryEngine {
         return new QueryEngine(mk);
     }
 
-    public Iterator<Row> executeQuery(String language, String query, Map<String,
String> bindings) throws ParseException {
+    public Iterator<Row> executeQuery(String language, String query, Map<String,
Value> bindings) throws ParseException {
         Query q;
         if (SQL2.equals(language)) {
             q = parserSQL2.parse(query);
@@ -53,8 +53,8 @@ public class QueryEngine {
         }
         q.setMicroKernel(mk);
         if (bindings != null) {
-            for (Entry<String, String> e : bindings.entrySet()) {
-                q.bindValue(e.getKey(), vf.createValue(e.getValue()));
+            for (Entry<String, Value> e : bindings.entrySet()) {
+                q.bindValue(e.getKey(), e.getValue());
             }
         }
         return q.executeQuery(mk.getHeadRevision());

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/Row.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/Row.java?rev=1303334&r1=1303333&r2=1303334&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/Row.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/Row.java Wed
Mar 21 09:53:10 2012
@@ -34,7 +34,7 @@ public class Row implements Comparable<R
     }
 
     public String getPath() {
-        if (paths.length > 0) {
+        if (paths.length > 1) {
             throw new IllegalArgumentException("More than one selector");
         }
         return paths[0];

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/BindVariableValueImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/BindVariableValueImpl.java?rev=1303334&r1=1303333&r2=1303334&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/BindVariableValueImpl.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/BindVariableValueImpl.java
Wed Mar 21 09:53:10 2012
@@ -23,7 +23,6 @@ import org.apache.jackrabbit.oak.query.V
 public class BindVariableValueImpl extends StaticOperandImpl {
 
     private final String bindVariableName;
-    private Value value;
 
     public BindVariableValueImpl(String bindVariableName) {
         this.bindVariableName = bindVariableName;
@@ -45,7 +44,7 @@ public class BindVariableValueImpl exten
 
     @Override
     Value currentValue() {
-        return value;
+        return query.getBindVariableValue(bindVariableName);
     }
 
 }

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/LengthImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/LengthImpl.java?rev=1303334&r1=1303333&r2=1303334&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/LengthImpl.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/LengthImpl.java
Wed Mar 21 09:53:10 2012
@@ -49,6 +49,7 @@ public class LengthImpl extends DynamicO
         if (v == null) {
             return null;
         }
+        // TODO LENGTH(..) is the length of the string representation?
         String value = v.getString();
         return query.getValueFactory().createValue(value.length());
     }

Modified: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/query/QueryTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/query/QueryTest.java?rev=1303334&r1=1303333&r2=1303334&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/query/QueryTest.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/query/QueryTest.java
Wed Mar 21 09:53:10 2012
@@ -13,6 +13,8 @@
  */
 package org.apache.jackrabbit.oak.query;
 
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
 import java.io.FileOutputStream;
 import java.io.InputStream;
 import java.io.InputStreamReader;
@@ -20,6 +22,7 @@ import java.io.LineNumberReader;
 import java.io.OutputStreamWriter;
 import java.io.PrintWriter;
 import java.text.ParseException;
+import java.util.HashMap;
 import java.util.Iterator;
 import org.apache.jackrabbit.mk.MicroKernelFactory;
 import org.apache.jackrabbit.mk.api.MicroKernel;
@@ -58,6 +61,23 @@ public class QueryTest {
         test("queryXpathTest.txt");
     }
 
+    @Test
+    public void bindVariableTest() throws Exception {
+        head = mk.commit("/", "+ \"test\": { \"hello\": {\"id\": \"1\"}, \"world\": {\"id\":
\"2\"}}", null, null);
+        HashMap<String, Value> sv = new HashMap<String, Value>();
+        ValueFactory vf = new ValueFactory();
+        sv.put("id", vf.createValue("1"));
+        Iterator<Row> result;
+        result = qe.executeQuery(QueryEngine.SQL2, "select * from [nt:base] where id = $id",
sv);
+        assertTrue(result.hasNext());
+        assertEquals("/test/hello", result.next().getPath());
+
+        sv.put("id", vf.createValue("2"));
+        result = qe.executeQuery(QueryEngine.SQL2, "select * from [nt:base] where id = $id",
sv);
+        assertTrue(result.hasNext());
+        assertEquals("/test/world", result.next().getPath());
+    }
+
     private void test(String file) throws Exception {
         InputStream in = getClass().getResourceAsStream(file);
         LineNumberReader r = new LineNumberReader(new InputStreamReader(in));



Mime
View raw message