jackrabbit-oak-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ju...@apache.org
Subject svn commit: r1421254 - in /jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak: api/QueryEngine.java query/QueryEngineImpl.java query/QueryParser.java
Date Thu, 13 Dec 2012 12:35:14 GMT
Author: jukka
Date: Thu Dec 13 12:35:13 2012
New Revision: 1421254

URL: http://svn.apache.org/viewvc?rev=1421254&view=rev
Log:
OAK-36: Implement a query parser interface (to make parsing extensible)

First step: add the QueryParser inteface. WIP...

Added:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/QueryParser.java
Modified:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/api/QueryEngine.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/QueryEngineImpl.java

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/api/QueryEngine.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/api/QueryEngine.java?rev=1421254&r1=1421253&r2=1421254&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/api/QueryEngine.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/api/QueryEngine.java
Thu Dec 13 12:35:13 2012
@@ -19,6 +19,7 @@ package org.apache.jackrabbit.oak.api;
 import java.text.ParseException;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 
 import org.apache.jackrabbit.oak.namepath.NamePathMapper;
 
@@ -30,11 +31,11 @@ import org.apache.jackrabbit.oak.namepat
 public interface QueryEngine {
 
     /**
-     * Get the list of supported query languages.
+     * Get the set of supported query languages.
      *
      * @return the supported query languages
      */
-    List<String> getSupportedQueryLanguages();
+    Set<String> getSupportedQueryLanguages();
 
     /**
      * Parse the query (check if it's valid) and get the list of bind variable names.

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/QueryEngineImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/QueryEngineImpl.java?rev=1421254&r1=1421253&r2=1421254&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/QueryEngineImpl.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/QueryEngineImpl.java
Thu Dec 13 12:35:13 2012
@@ -17,9 +17,9 @@
 package org.apache.jackrabbit.oak.query;
 
 import java.text.ParseException;
-import java.util.Arrays;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 import java.util.Map.Entry;
 
 import org.apache.jackrabbit.oak.api.PropertyValue;
@@ -35,6 +35,8 @@ import org.apache.jackrabbit.oak.spi.sta
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import com.google.common.collect.ImmutableSet;
+
 /**
  * The query engine implementation.
  */
@@ -51,6 +53,46 @@ public abstract class QueryEngineImpl im
 
     private final QueryIndexProvider indexProvider;
 
+    // TODO: Turn into a standalone class
+    private final QueryParser parser = new QueryParser() {
+        @Override
+        public Set<String> getSupportedLanguages() {
+            return ImmutableSet.of(
+                    SQL2, SQL, XPATH, JQOM,
+                    SQL2 + NO_LITERALS,
+                    SQL + NO_LITERALS,
+                    XPATH + NO_LITERALS);
+        }
+        @Override
+        public Query parse(String statement, String language)
+                throws ParseException {
+            LOG.debug("Parsing {} statement: {}", language, statement);
+            SQL2Parser parser = new SQL2Parser();
+            if (language.endsWith(NO_LITERALS)) {
+                language = language.substring(0, language.length() - NO_LITERALS.length());
+                parser.setAllowNumberLiterals(false);
+                parser.setAllowTextLiterals(false);
+            }
+            if (SQL2.equals(language) || JQOM.equals(language)) {
+                return parser.parse(statement);
+            } else if (SQL.equals(language)) {
+                parser.setSupportSQL1(true);
+                return parser.parse(statement);
+            } else if (XPATH.equals(language)) {
+                XPathToSQL2Converter converter = new XPathToSQL2Converter();
+                String sql2 = converter.convert(statement);
+                LOG.debug("XPath > SQL2: {}", sql2);
+                try {
+                    return parser.parse(sql2);
+                } catch (ParseException e) {
+                    throw new ParseException(statement + " converted to SQL-2 " + e.getMessage(),
0);
+                }
+            } else {
+                throw new ParseException("Unsupported language: " + language, 0);
+            }
+        }
+    };
+
     public QueryEngineImpl(QueryIndexProvider indexProvider) {
         this.indexProvider = indexProvider;
     }
@@ -70,9 +112,8 @@ public abstract class QueryEngineImpl im
     protected abstract Root getRootTree();
 
     @Override
-    public List<String> getSupportedQueryLanguages() {
-        return Arrays.asList(SQL2, SQL, XPATH, JQOM,
-                SQL2 + NO_LITERALS, SQL + NO_LITERALS, XPATH + NO_LITERALS);
+    public Set<String> getSupportedQueryLanguages() {
+        return parser.getSupportedLanguages();
     }
 
     /**
@@ -89,37 +130,8 @@ public abstract class QueryEngineImpl im
         return q.getBindVariableNames();
     }
 
-    private static Query parseQuery(String statement, String language) throws ParseException
{
-        Query q;
-        if (LOG.isDebugEnabled()) {
-            LOG.debug(language + ": " + statement);
-        }
-        SQL2Parser parser = new SQL2Parser();
-        if (language.endsWith(NO_LITERALS)) {
-            language = language.substring(0, language.length() - NO_LITERALS.length());
-            parser.setAllowNumberLiterals(false);
-            parser.setAllowTextLiterals(false);
-        }
-        if (SQL2.equals(language) || JQOM.equals(language)) {
-            q = parser.parse(statement);
-        } else if (SQL.equals(language)) {
-            parser.setSupportSQL1(true);
-            q = parser.parse(statement);
-        } else if (XPATH.equals(language)) {
-            XPathToSQL2Converter converter = new XPathToSQL2Converter();
-            String sql2 = converter.convert(statement);
-            if (LOG.isDebugEnabled()) {
-                LOG.debug("XPath > SQL2: " + sql2);
-            }
-            try {
-                q = parser.parse(sql2);
-            } catch (ParseException e) {
-                throw new ParseException(statement + " converted to SQL-2 " + e.getMessage(),
0);
-            }
-        } else {
-            throw new ParseException("Unsupported language: " + language, 0);
-        }
-        return q;
+    private Query parseQuery(String statement, String language) throws ParseException {
+        return parser.parse(statement, language);
     }
     
     @Override

Added: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/QueryParser.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/QueryParser.java?rev=1421254&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/QueryParser.java
(added)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/QueryParser.java
Thu Dec 13 12:35:13 2012
@@ -0,0 +1,45 @@
+/*
+ * 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.oak.query;
+
+import java.text.ParseException;
+import java.util.Set;
+
+/**
+ * Query parser interface.
+ *
+ * TODO: move to o.a.j.o.spi (figure out what to do with the Query class)
+ */
+public interface QueryParser {
+
+    /**
+     * Returns the set of query languages supported by this parser.
+     *
+     * @return supported query languages
+     */
+    Set<String> getSupportedLanguages();
+
+    /**
+     * Parses the given query string, expressed in the specified language.
+     *
+     * TODO: Include name mapping information
+     *
+     * @param query query string
+     * @param language query language
+     * @return parsed query
+     * @throws ParseException if the query string could not be parsed
+     */
+    Query parse(String query, String language) throws ParseException;
+
+}



Mime
View raw message