jackrabbit-oak-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From thom...@apache.org
Subject svn commit: r1336192 - in /jackrabbit/oak/trunk/oak-jcr/src: main/java/org/apache/jackrabbit/oak/jcr/ main/java/org/apache/jackrabbit/oak/jcr/query/ main/java/org/apache/jackrabbit/oak/jcr/query/qom/ test/java/org/apache/jackrabbit/oak/jcr/query/ test/...
Date Wed, 09 May 2012 13:27:03 GMT
Author: thomasm
Date: Wed May  9 13:27:02 2012
New Revision: 1336192

URL: http://svn.apache.org/viewvc?rev=1336192&view=rev
Log:
OAK-28 Query implementation

Modified:
    jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/NodeImpl.java
    jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/WorkspaceImpl.java
    jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/query/QueryImpl.java
    jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/query/QueryManagerImpl.java
    jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/query/QueryResultImpl.java
    jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/query/qom/ColumnImpl.java
    jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/query/qom/QueryObjectModelFactoryImpl.java
    jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/query/qom/QueryObjectModelImpl.java
    jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/query/qom/SelectorImpl.java
    jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/query/QueryTest.java
    jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/query/qom/QomTest.java

Modified: jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/NodeImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/NodeImpl.java?rev=1336192&r1=1336191&r2=1336192&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/NodeImpl.java
(original)
+++ jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/NodeImpl.java
Wed May  9 13:27:02 2012
@@ -73,8 +73,8 @@ public class NodeImpl extends ItemImpl i
     private static final Logger log = LoggerFactory.getLogger(NodeImpl.class);
 
     private final NodeDelegate dlg;
-    
-    NodeImpl(NodeDelegate dlg) {
+
+    public NodeImpl(NodeDelegate dlg) {
         super(dlg.getSessionDelegate(), dlg);
         this.dlg = dlg;
     }
@@ -172,7 +172,7 @@ public class NodeImpl extends ItemImpl i
             throw new ConstraintViolationException();
         }
         // TODO: END
-        
+
         NodeDelegate added = parent.addChild(oakName);
         Node childNode = new NodeImpl(added);
         childNode.setPrimaryType(primaryNodeTypeName);
@@ -696,59 +696,59 @@ public class NodeImpl extends ItemImpl i
 
         // TODO
         return new NodeDefinition() {
- 
+
             // This is a workaround to make AbstractJCRTest.cleanup happy
-            
+
             @Override
             public boolean isProtected() {
                 return false;
             }
-            
+
             @Override
             public boolean isMandatory() {
                 return false;
             }
-            
+
             @Override
             public boolean isAutoCreated() {
                 return false;
             }
-            
+
             @Override
             public int getOnParentVersion() {
                 return OnParentVersionAction.COPY;
             }
-            
+
             @Override
             public String getName() {
                 return "default";
             }
-            
+
             @Override
             public NodeType getDeclaringNodeType() {
                 return null;
             }
-            
+
             @Override
             public NodeType[] getRequiredPrimaryTypes() {
                 return null;
             }
-            
+
             @Override
             public String[] getRequiredPrimaryTypeNames() {
                 return null;
             }
-            
+
             @Override
             public String getDefaultPrimaryTypeName() {
                 return null;
             }
-            
+
             @Override
             public NodeType getDefaultPrimaryType() {
                 return null;
             }
-            
+
             @Override
             public boolean allowsSameNameSiblings() {
                 return false;

Modified: jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/WorkspaceImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/WorkspaceImpl.java?rev=1336192&r1=1336191&r2=1336192&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/WorkspaceImpl.java
(original)
+++ jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/WorkspaceImpl.java
Wed May  9 13:27:02 2012
@@ -61,7 +61,7 @@ public class WorkspaceImpl implements Ja
         this.sessionDelegate = sessionDelegate;
         this.nsRegistry = nsRegistry;
         this.nodeTypeManager = new NodeTypeManagerImpl(sessionDelegate.getValueFactory(),
sessionDelegate.getNamePathMapper());
-        this.queryManager = new QueryManagerImpl(this, sessionDelegate);
+        this.queryManager = new QueryManagerImpl(sessionDelegate);
     }
 
     //----------------------------------------------------------< Workspace >---

Modified: jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/query/QueryImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/query/QueryImpl.java?rev=1336192&r1=1336191&r2=1336192&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/query/QueryImpl.java
(original)
+++ jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/query/QueryImpl.java
Wed May  9 13:27:02 2012
@@ -21,7 +21,9 @@ package org.apache.jackrabbit.oak.jcr.qu
 import java.util.HashMap;
 import java.util.List;
 import javax.jcr.Node;
+import javax.jcr.PathNotFoundException;
 import javax.jcr.RepositoryException;
+import javax.jcr.UnsupportedRepositoryOperationException;
 import javax.jcr.Value;
 import javax.jcr.query.InvalidQueryException;
 import javax.jcr.query.Query;
@@ -46,7 +48,11 @@ public class QueryImpl implements Query 
     }
 
     @Override
-    public void bindValue(String varName, Value value) {
+    public void bindValue(String varName, Value value) throws RepositoryException {
+        parse();
+        if (!bindVariableMap.containsKey(varName)) {
+            throw new IllegalArgumentException("Variable name " + varName + " is not a valid
variable in this query");
+        }
         bindVariableMap.put(varName, value);
     }
 
@@ -101,8 +107,12 @@ public class QueryImpl implements Query 
 
     @Override
     public Node storeAsNode(String absPath) throws RepositoryException {
+        manager.ensureIsAlive();
+        if (manager.getSessionDelegate().getNode(absPath) == null) {
+            throw new PathNotFoundException("The specified path does not exist: " + absPath);
+        }
         // TODO not implemented yet
-        return null;
+        throw new UnsupportedRepositoryOperationException("This feature is not supported");
     }
 
 }

Modified: jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/query/QueryManagerImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/query/QueryManagerImpl.java?rev=1336192&r1=1336191&r2=1336192&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/query/QueryManagerImpl.java
(original)
+++ jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/query/QueryManagerImpl.java
Wed May  9 13:27:02 2012
@@ -18,13 +18,12 @@
  */
 package org.apache.jackrabbit.oak.jcr.query;
 
-import org.apache.jackrabbit.oak.api.CoreValue;
-import org.apache.jackrabbit.oak.api.QueryEngine;
-import org.apache.jackrabbit.oak.api.Result;
-import org.apache.jackrabbit.oak.jcr.SessionDelegate;
-import org.apache.jackrabbit.oak.jcr.WorkspaceImpl;
-import org.apache.jackrabbit.oak.jcr.query.qom.QueryObjectModelFactoryImpl;
-
+import java.text.ParseException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map.Entry;
 import javax.jcr.Node;
 import javax.jcr.RepositoryException;
 import javax.jcr.Value;
@@ -33,27 +32,34 @@ import javax.jcr.query.Query;
 import javax.jcr.query.QueryManager;
 import javax.jcr.query.QueryResult;
 import javax.jcr.query.qom.QueryObjectModelFactory;
-import java.text.ParseException;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map.Entry;
+import org.apache.jackrabbit.oak.api.CoreValue;
+import org.apache.jackrabbit.oak.api.QueryEngine;
+import org.apache.jackrabbit.oak.api.Result;
+import org.apache.jackrabbit.oak.jcr.SessionDelegate;
+import org.apache.jackrabbit.oak.jcr.query.qom.QueryObjectModelFactoryImpl;
 
 /**
  * The implementation of the corresponding JCR interface.
  */
 public class QueryManagerImpl implements QueryManager {
 
-    private final QueryObjectModelFactoryImpl qomFactory = new QueryObjectModelFactoryImpl();
+    private final QueryObjectModelFactoryImpl qomFactory;
     private final QueryEngine queryEngine;
     private final SessionDelegate sessionDelegate;
+    private final HashSet<String> supportedQueryLanguages = new HashSet<String>();
 
-    public QueryManagerImpl(WorkspaceImpl workspace, SessionDelegate sessionDelegate) {
-        queryEngine = sessionDelegate.getQueryEngine();
+    public QueryManagerImpl(SessionDelegate sessionDelegate) {
         this.sessionDelegate = sessionDelegate;
+        qomFactory = new QueryObjectModelFactoryImpl(this, sessionDelegate.getValueFactory());
+        queryEngine = sessionDelegate.getQueryEngine();
+        supportedQueryLanguages.addAll(queryEngine.getSupportedQueryLanguages());
     }
 
     @Override
-    public Query createQuery(String statement, String language) throws RepositoryException
{
+    public QueryImpl createQuery(String statement, String language) throws RepositoryException
{
+        if (!supportedQueryLanguages.contains(language)) {
+            throw new InvalidQueryException("The specified language is not supported: " +
language);
+        }
         return new QueryImpl(this, statement, language);
     }
 
@@ -70,15 +76,12 @@ public class QueryManagerImpl implements
 
     @Override
     public String[] getSupportedQueryLanguages() throws RepositoryException {
-        @SuppressWarnings("deprecation")
+        ArrayList<String> list = new ArrayList<String>(queryEngine.getSupportedQueryLanguages());
+        // JQOM is supported in this level only (converted to JCR_SQL2)
+        list.add(Query.JCR_JQOM);
         // create a new instance each time because the array is mutable
         // (the caller could modify it)
-        String[] s = {
-            Query.JCR_JQOM,
-            Query.JCR_SQL2,
-            Query.XPATH
-        };
-        return s;
+        return list.toArray(new String[list.size()]);
     }
 
     public List<String> parse(String statement, String language) throws InvalidQueryException
{
@@ -94,14 +97,15 @@ public class QueryManagerImpl implements
         try {
             HashMap<String, CoreValue> bindMap = convertMap(bindVariableMap);
             Result r = queryEngine.executeQuery(statement, language, bindMap);
-            return new QueryResultImpl(r, sessionDelegate.getValueFactory());
+            return new QueryResultImpl(sessionDelegate, r);
+        } catch (IllegalArgumentException e) {
+            throw new InvalidQueryException(e);
         } catch (ParseException e) {
             throw new InvalidQueryException(e);
         }
     }
 
     private HashMap<String, CoreValue> convertMap(HashMap<String, Value> bindVariableMap)
{
-
         HashMap<String, CoreValue> map = new HashMap<String, CoreValue>();
         for (Entry<String, Value> e : bindVariableMap.entrySet()) {
             map.put(e.getKey(), sessionDelegate.getValueFactory().getCoreValue(e.getValue()));
@@ -109,4 +113,15 @@ public class QueryManagerImpl implements
         return map;
     }
 
+    SessionDelegate getSessionDelegate() {
+        return sessionDelegate;
+    }
+
+    void ensureIsAlive() throws RepositoryException {
+        // check session status
+        if (!sessionDelegate.isAlive()) {
+            throw new RepositoryException("This session has been closed.");
+        }
+    }
+
 }

Modified: jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/query/QueryResultImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/query/QueryResultImpl.java?rev=1336192&r1=1336191&r2=1336192&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/query/QueryResultImpl.java
(original)
+++ jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/query/QueryResultImpl.java
Wed May  9 13:27:02 2012
@@ -18,9 +18,14 @@
  */
 package org.apache.jackrabbit.oak.jcr.query;
 
+import org.apache.jackrabbit.commons.iterator.NodeIteratorAdapter;
 import org.apache.jackrabbit.commons.iterator.RowIteratorAdapter;
 import org.apache.jackrabbit.oak.api.Result;
 import org.apache.jackrabbit.oak.api.ResultRow;
+import org.apache.jackrabbit.oak.commons.PathUtils;
+import org.apache.jackrabbit.oak.jcr.NodeDelegate;
+import org.apache.jackrabbit.oak.jcr.NodeImpl;
+import org.apache.jackrabbit.oak.jcr.SessionDelegate;
 import org.apache.jackrabbit.oak.jcr.value.ValueFactoryImpl;
 
 import javax.jcr.NodeIterator;
@@ -28,18 +33,26 @@ import javax.jcr.RepositoryException;
 import javax.jcr.query.QueryResult;
 import javax.jcr.query.RowIterator;
 import java.util.Iterator;
+import java.util.NoSuchElementException;
 
 /**
  * The implementation of the corresponding JCR interface.
  */
 public class QueryResultImpl implements QueryResult {
 
-    private final Result result;
-    private final ValueFactoryImpl valueFactory;
+    final SessionDelegate sessionDelegate;
+    final ValueFactoryImpl valueFactory;
+    final Result result;
+    final String pathFilter;
 
-    public QueryResultImpl(Result result, ValueFactoryImpl valueFactory) {
+    public QueryResultImpl(SessionDelegate sessionDelegate, Result result) {
+        this.sessionDelegate = sessionDelegate;
         this.result = result;
-        this.valueFactory = valueFactory;
+        this.valueFactory = sessionDelegate.getValueFactory();
+
+        // TODO the path currently contains the workspace name
+        // TODO filter in oak-core once we support workspaces there
+        pathFilter = "/" + sessionDelegate.getWorkspaceName();
     }
 
     @Override
@@ -57,20 +70,42 @@ public class QueryResultImpl implements 
         Iterator<RowImpl> it = new Iterator<RowImpl>() {
 
             private final Iterator<? extends ResultRow> it = result.getRows().iterator();
+            private RowImpl current;
+
+            {
+                fetch();
+            }
+
+            private void fetch() {
+                current = null;
+                while(it.hasNext()) {
+                    ResultRow r = it.next();
+                    String path = r.getPath();
+                    if (PathUtils.isAncestor(pathFilter, path)) {
+                        current = new RowImpl(r, valueFactory);
+                        break;
+                    }
+                }
+            }
 
             @Override
             public boolean hasNext() {
-                return it.hasNext();
+                return current != null;
             }
 
             @Override
             public RowImpl next() {
-                return new RowImpl(it.next(), valueFactory);
+                if (current == null) {
+                    throw new NoSuchElementException();
+                }
+                RowImpl r = current;
+                fetch();
+                return r;
             }
 
             @Override
             public void remove() {
-                it.remove();
+                throw new UnsupportedOperationException();
             }
 
         };
@@ -79,7 +114,51 @@ public class QueryResultImpl implements 
 
     @Override
     public NodeIterator getNodes() throws RepositoryException {
-        return null;
+        Iterator<NodeImpl> it = new Iterator<NodeImpl>() {
+
+            private final Iterator<? extends ResultRow> it = result.getRows().iterator();
+            private NodeImpl current;
+
+            {
+                fetch();
+            }
+
+            private void fetch() {
+                current = null;
+                while(it.hasNext()) {
+                    ResultRow r = it.next();
+                    String path = r.getPath();
+                    if (PathUtils.isAncestor(pathFilter, path)) {
+                        path = PathUtils.relativize(pathFilter, path);
+                        NodeDelegate d = sessionDelegate.getNode(path);
+                        current = new NodeImpl(d);
+                        break;
+                    }
+                }
+            }
+
+            @Override
+            public boolean hasNext() {
+                return current != null;
+            }
+
+            @Override
+            public NodeImpl next() {
+                if (current == null) {
+                    throw new NoSuchElementException();
+                }
+                NodeImpl n = current;
+                fetch();
+                return n;
+            }
+
+            @Override
+            public void remove() {
+                throw new UnsupportedOperationException();
+            }
+
+        };
+        return new NodeIteratorAdapter(it);
     }
 
 }

Modified: jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/query/qom/ColumnImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/query/qom/ColumnImpl.java?rev=1336192&r1=1336191&r2=1336192&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/query/qom/ColumnImpl.java
(original)
+++ jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/query/qom/ColumnImpl.java
Wed May  9 13:27:02 2012
@@ -52,7 +52,7 @@ public class ColumnImpl extends QOMNode 
     public String toString() {
         if (propertyName != null) {
             return getSelectorName() + '.' + getPropertyName()
-                    + " AS " + getColumnName();
+                    + " AS [" + columnName + "]";
         } else {
             return getSelectorName() + ".*";
         }

Modified: jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/query/qom/QueryObjectModelFactoryImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/query/qom/QueryObjectModelFactoryImpl.java?rev=1336192&r1=1336191&r2=1336192&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/query/qom/QueryObjectModelFactoryImpl.java
(original)
+++ jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/query/qom/QueryObjectModelFactoryImpl.java
Wed May  9 13:27:02 2012
@@ -14,6 +14,7 @@
 package org.apache.jackrabbit.oak.jcr.query.qom;
 
 import javax.jcr.Value;
+import javax.jcr.ValueFactory;
 import javax.jcr.query.qom.ChildNode;
 import javax.jcr.query.qom.ChildNodeJoinCondition;
 import javax.jcr.query.qom.Column;
@@ -45,12 +46,21 @@ import javax.jcr.query.qom.Selector;
 import javax.jcr.query.qom.Source;
 import javax.jcr.query.qom.StaticOperand;
 import javax.jcr.query.qom.UpperCase;
+import org.apache.jackrabbit.oak.jcr.query.QueryManagerImpl;
 
 /**
  * The implementation of the corresponding JCR interface.
  */
 public class QueryObjectModelFactoryImpl implements QueryObjectModelFactory {
 
+    private final QueryManagerImpl queryManager;
+    private final ValueFactory valueFactory;
+
+    public QueryObjectModelFactoryImpl(QueryManagerImpl queryManager, ValueFactory valueFactory)
{
+        this.queryManager = queryManager;
+        this.valueFactory = valueFactory;
+    }
+
     @Override
     public AndImpl and(Constraint constraint1, Constraint constraint2) {
         return new AndImpl((ConstraintImpl) constraint1, (ConstraintImpl) constraint2);
@@ -193,7 +203,7 @@ public class QueryObjectModelFactoryImpl
 
     @Override
     public QueryObjectModel createQuery(Source source, Constraint constraint, Ordering[]
orderings, Column[] columns) {
-        QueryObjectModelImpl qom = new QueryObjectModelImpl(source, constraint, orderings,
columns);
+        QueryObjectModelImpl qom = new QueryObjectModelImpl(queryManager, valueFactory, source,
constraint, orderings, columns);
         qom.bindVariables();
         return qom;
     }

Modified: jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/query/qom/QueryObjectModelImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/query/qom/QueryObjectModelImpl.java?rev=1336192&r1=1336191&r2=1336192&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/query/qom/QueryObjectModelImpl.java
(original)
+++ jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/query/qom/QueryObjectModelImpl.java
Wed May  9 13:27:02 2012
@@ -13,12 +13,13 @@
  */
 package org.apache.jackrabbit.oak.jcr.query.qom;
 
-import org.apache.jackrabbit.commons.SimpleValueFactory;
-
+import java.util.ArrayList;
+import java.util.HashMap;
 import javax.jcr.Node;
 import javax.jcr.RepositoryException;
 import javax.jcr.Value;
 import javax.jcr.ValueFactory;
+import javax.jcr.query.Query;
 import javax.jcr.query.QueryResult;
 import javax.jcr.query.qom.Column;
 import javax.jcr.query.qom.Constraint;
@@ -26,27 +27,29 @@ import javax.jcr.query.qom.Ordering;
 import javax.jcr.query.qom.QueryObjectModel;
 import javax.jcr.query.qom.Selector;
 import javax.jcr.query.qom.Source;
-import java.util.ArrayList;
-import java.util.HashMap;
+import org.apache.jackrabbit.oak.jcr.query.QueryManagerImpl;
 
 /**
  * The implementation of the corresponding JCR interface.
  */
 public class QueryObjectModelImpl implements QueryObjectModel {
 
+    private final QueryManagerImpl queryManager;
+    private final ValueFactory valueFactory;
     final Source source;
     final Constraint constraint;
     final HashMap<String, Value> bindVariableMap = new HashMap<String, Value>();
     final ArrayList<Selector> selectors = new ArrayList<Selector>();
-
     private final Ordering[] orderings;
     private final Column[] columns;
     private long limit;
     private long offset;
-    private final ValueFactory valueFactory = new SimpleValueFactory();
+    private boolean parsed;
 
-    public QueryObjectModelImpl(Source source, Constraint constraint, Ordering[] orderings,
+    public QueryObjectModelImpl(QueryManagerImpl queryManager, ValueFactory valueFactory,
Source source, Constraint constraint, Ordering[] orderings,
             Column[] columns) {
+        this.queryManager = queryManager;
+        this.valueFactory = valueFactory;
         this.source = source;
         this.constraint = constraint;
         this.orderings = orderings;
@@ -54,7 +57,9 @@ public class QueryObjectModelImpl implem
     }
 
     public void bindVariables() {
-        ((ConstraintImpl) constraint).bindVariables(this);
+        if (constraint != null) {
+            ((ConstraintImpl) constraint).bindVariables(this);
+        }
     }
 
     @Override
@@ -78,10 +83,11 @@ public class QueryObjectModelImpl implem
     }
 
     @Override
-    public String[] getBindVariableNames() {
-        String[] array = new String[bindVariableMap.size()];
-        array = bindVariableMap.keySet().toArray(array);
-        return array;
+    public String[] getBindVariableNames() throws RepositoryException {
+        parse();
+        String[] names = new String[bindVariableMap.size()];
+        bindVariableMap.keySet().toArray(names);
+        return names;
     }
 
     @Override
@@ -99,28 +105,67 @@ public class QueryObjectModelImpl implem
     }
 
     @Override
-    public void bindValue(String arg0, javax.jcr.Value arg1) throws IllegalArgumentException,
-            RepositoryException {
-        // TODO Auto-generated method stub
-
+    public void bindValue(String varName, Value value) throws RepositoryException {
+        parse();
+        if (!bindVariableMap.containsKey(varName)) {
+            throw new IllegalArgumentException("Variable name " + varName + " is not a valid
variable in this query");
+        }
+        bindVariableMap.put(varName, value);
+    }
+
+    private void parse() throws RepositoryException {
+        if (parsed) {
+            return;
+        }
+        String[] names = queryManager.createQuery(getStatement(), Query.JCR_SQL2).getBindVariableNames();
+        for (String n : names) {
+            bindVariableMap.put(n, null);
+        }
     }
 
     @Override
     public QueryResult execute() throws RepositoryException {
-        // TODO Auto-generated method stub
-        return null;
+        return queryManager.executeQuery(getStatement(), Query.JCR_SQL2, bindVariableMap,
limit, offset);
     }
 
     @Override
     public String getLanguage() {
-        // TODO Auto-generated method stub
-        return null;
+        return Query.JCR_JQOM;
     }
 
     @Override
     public String getStatement() {
-        // TODO Auto-generated method stub
-        return null;
+        StringBuilder buff = new StringBuilder();
+        buff.append("select ");
+        int i;
+        if (columns != null) {
+            i = 0;
+            for (Column c : columns) {
+                if (i++ > 0) {
+                    buff.append(", ");
+                }
+                buff.append(c);
+            }
+        } else {
+            buff.append("*");
+        }
+        buff.append(" from ");
+        buff.append(source);
+        if (constraint != null) {
+            buff.append(" where ");
+            buff.append(constraint);
+        }
+        if (orderings != null) {
+            buff.append(" order by ");
+            i = 0;
+            for (Ordering o : orderings) {
+                if (i++ > 0) {
+                    buff.append(", ");
+                }
+                buff.append(o);
+            }
+        }
+        return buff.toString();
     }
 
     @Override

Modified: jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/query/qom/SelectorImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/query/qom/SelectorImpl.java?rev=1336192&r1=1336191&r2=1336192&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/query/qom/SelectorImpl.java
(original)
+++ jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/query/qom/SelectorImpl.java
Wed May  9 13:27:02 2012
@@ -45,7 +45,7 @@ public class SelectorImpl extends Source
     @Override
     public String toString() {
         // TODO quote nodeTypeName?
-        return nodeTypeName + " AS " + getSelectorName();
+        return "[" + nodeTypeName + "] AS " + getSelectorName();
     }
 
 }

Modified: jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/query/QueryTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/query/QueryTest.java?rev=1336192&r1=1336191&r2=1336192&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/query/QueryTest.java
(original)
+++ jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/query/QueryTest.java
Wed May  9 13:27:02 2012
@@ -22,6 +22,7 @@ import static junit.framework.Assert.ass
 import static junit.framework.Assert.assertTrue;
 import static org.junit.Assert.assertFalse;
 import javax.jcr.Node;
+import javax.jcr.NodeIterator;
 import javax.jcr.Repository;
 import javax.jcr.RepositoryException;
 import javax.jcr.Session;
@@ -67,6 +68,13 @@ public class QueryTest extends AbstractR
             assertEquals("hello world", row.getValue("text").getString());
             assertFalse(it.hasNext());
 
+            r = q.execute();
+            NodeIterator nodeIt = r.getNodes();
+            assertTrue(nodeIt.hasNext());
+            Node n = nodeIt.nextNode();
+            assertEquals("hello world", n.getProperty("text").getString());
+            assertFalse(it.hasNext());
+
         } finally {
             session.logout();
         }

Modified: jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/query/qom/QomTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/query/qom/QomTest.java?rev=1336192&r1=1336191&r2=1336192&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/query/qom/QomTest.java
(original)
+++ jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/query/qom/QomTest.java
Wed May  9 13:27:02 2012
@@ -18,12 +18,13 @@
  */
 package org.apache.jackrabbit.oak.jcr.query.qom;
 
-import org.apache.jackrabbit.commons.SimpleValueFactory;
-import org.junit.Test;
-
+import static org.junit.Assert.assertEquals;
+import javax.jcr.Repository;
 import javax.jcr.RepositoryException;
+import javax.jcr.Session;
 import javax.jcr.Value;
 import javax.jcr.ValueFactory;
+import javax.jcr.query.QueryManager;
 import javax.jcr.query.qom.And;
 import javax.jcr.query.qom.BindVariableValue;
 import javax.jcr.query.qom.ChildNode;
@@ -55,15 +56,26 @@ import javax.jcr.query.qom.SameNodeJoinC
 import javax.jcr.query.qom.Selector;
 import javax.jcr.query.qom.Source;
 import javax.jcr.query.qom.UpperCase;
-
-import static org.junit.Assert.assertEquals;
+import org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest;
+import org.junit.Before;
+import org.junit.Test;
 
 /**
  * Tests the QueryObjectModelFactory and other QOM classes.
  */
-public class QomTest {
-    private final QueryObjectModelFactory f = new QueryObjectModelFactoryImpl();
-    private final ValueFactory vf = new SimpleValueFactory();
+public class QomTest extends AbstractRepositoryTest {
+
+    private ValueFactory vf;
+    private QueryObjectModelFactory f;
+
+    @Before
+    public void before() throws RepositoryException {
+        Repository repository = getRepository();
+        Session session = repository.login();
+        vf = session.getValueFactory();
+        QueryManager qm = session.getWorkspace().getQueryManager();
+        f = qm.getQOMFactory();
+    }
 
     @Test
     public void and() throws RepositoryException {



Mime
View raw message