You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@chemistry.apache.org by bu...@apache.org on 2011/03/29 21:50:11 UTC

svn commit: r787720 - /websites/staging/chemistry/trunk/content/java/how-to/how-to-process-query.html

Author: buildbot
Date: Tue Mar 29 19:50:11 2011
New Revision: 787720

Log:
Staging update by buildbot

Modified:
    websites/staging/chemistry/trunk/content/java/how-to/how-to-process-query.html

Modified: websites/staging/chemistry/trunk/content/java/how-to/how-to-process-query.html
==============================================================================
--- websites/staging/chemistry/trunk/content/java/how-to/how-to-process-query.html (original)
+++ websites/staging/chemistry/trunk/content/java/how-to/how-to-process-query.html Tue Mar 29 19:50:11 2011
@@ -193,6 +193,7 @@ Apache Chemistry - Query Integration
 <li><a href="#using_queryobject">Using QueryObject</a></li>
 <li><a href="#processing_a_node_and_referencing_types_and_properties">Processing a node and referencing types and properties</a></li>
 <li><a href="#building_the_result_list">Building the result list</a></li>
+<li><a href="#limitations">Limitations</a></li>
 </ul>
 </li>
 </ul>
@@ -212,7 +213,7 @@ prefer a different language parsing tool
 <li>Implement query in the discovery service</li>
 <li>Use the built-in ANTLR and ANTLR CMISQL grammar</li>
 <li>Use OpenCMIS CMISQL grammar and integrate into ANTLR query walker</li>
-<li>Use predefined query walker and integrate into interface <code>IQueryConditionProcessor</code>.</li>
+<li>Use predefined query walker and integrate into interface <code>PredicateWalker</code>.</li>
 </ol>
 <h2 id="implement_query_in_the_discovery_service">Implement query in the discovery service</h2>
 <p>The first way is to implement the <code>query()</code> method like any other service
@@ -226,7 +227,7 @@ integrate query by using the ANTLR mecha
 abstract syntax tree. Please refer to the ANTLR documentation for further
 information. This is the right level to use if you need custom parser tree
 transformations or would like to extend the grammar with your own
-constructs. For demonstration purposes OpenCMIS provides a sample extended
+constructs. For demonstration purposes OpenCMIS provides an extended
 grammar as an example.</p>
 <h2 id="use_opencmis_cmsiql_grammar_and_integrate_into_antlr_query_walker">Use OpenCMIS CMSIQL grammar and integrate into ANTLR query walker</h2>
 <p>If the standard CMISQL grammar is sufficient for you there is another level
@@ -240,63 +241,76 @@ these common tasks. You can make use of 
 aliases and walk the resulting abstract syntax tree (AST) to evaluate the
 query. You are free to walk the AST as many times as you need and in the
 order you prefer. The basic idea is that the SELECT and FROM parts are
-processed by OpenCMIS and you are responsible for the WHERE part.&nbsp; The
-CMIS InMemory server provides an example for this level of integration: For
-each object contained in the repository the tree is traversed and checked
+processed by OpenCMIS and you are responsible for the WHERE part. The
+InMemory server provides an example for this level of integration: For
+each object contained in the repository the tree is traversed and it's checked
 if it matches the current query. You can take the InMemory code as an
-example if you decide to use this integration point.</p>
+example if you decide to use this integration level.</p>
 <h2 id="use_predefined_query_walker">Use predefined query walker</h2>
 <p>For some repositories a simple and one-pass query traversal is sufficient.
 This can be the case if for example your query needs to be translated to a
 SQL query statement. Because ANTLR has some complexity OpenCMIS provides a
-predefined walker that does a simple one pass depth-first traversal. If
+predefined walker that performs a simple one pass depth-first traversal. If
 this is sufficient this interface hides most of the complexity of ANTLR.
 All you have to do is to implement a Java interface
-(<code>IQueryConditionProcessor</code>). You can refer to the unit tests for example
-code. The class <code>TestQueryProcessor</code> nested in the unit test <code>ProcessQueryTest</code>
-provides an example of such a walker. Some utility methods like for example
-parsing literals like <code>"abc"</code>, <code>-123</code> to Java objects like <code>String</code> and <code>Integer</code>
-are common tasks. Therefore this is implemented in an abstract class
-<code>AbstractQueryConditionProcessor</code>. This declares all interface methods as
-abstract and provides default implementations for common tasks. In most
-cases you will derive your implementation from
-<code>AbstractQueryConditionProcessor</code> and not directly implement the interface.</p>
-<p>Note: There is currently no predefined walker for the JOIN statements. If
-you need to support JOINS you have to build your own walker for this part
+(<code>PredicateWalker</code>). You can refer to the InMemory server for example
+code (<code>InMemoryWhereClauseWalker</code>). </p>
+<p><code>AbstractPredicateWalker</code> implements interface <code>PredicateWalker</code> and 
+implements common functionality useful for traversing the tree. For example
+parsing literals like <code>"abc"</code>, <code>-123</code> to Java objects like <code>String</code> 
+and <code>Integer</code> is handled there.</p>
+<p>If the interface of the predefined walker <code>PredicateWalker</code> does not
+fit your needs you can define your own interface. The code generated
+by ANTLR does not make any assumptions how you design the walking of
+your tree. The only dependency is contained in the interface 
+<code>PredicateWalkerBase</code> consisting of a single method. If you start 
+defining your own walker you have to implement or extend <code>PredicateWalkerBase</code>.
+The unit tests contain an example for this. See class <code>QueryConditionProcessor</code>
+in the unit tests for the InMemory server.</p>
+<p>Note: There is currently no predefined walker for JOIN statements. If
+you need to support JOINs you have to build your own walker for this part
 as outlined in the previous section.</p>
 <h2 id="using_queryobject">Using QueryObject</h2>
 <p>The class <code>QueryObject</code> provides all the basic functionality for resolving
 types and properties and performs common validation tasks. The <code>QueryObject</code>
 processes the <code>SELECT</code> and <code>FROM</code> parts as well as all property references from
-the <code>WHERE</code> part. It maintains a list of Java objects and interface that you
+the <code>WHERE</code> part. It maintains a list of Java objects and an interface that you
 can use to access the property and type definitions given your current
 position in the statement. For an example refer to the class
 <code>StoreManagerImpl</code> of the InMemory Server and method <code>query()</code>.
 To be able to use this object <code>QueryObj</code> needs to get access to the types contained in your
 repository. For this purpose you need to pass an interface to a <code>TypeManager</code>
-as input parameter. The second parameter is your query walker implementing
-<code>IQueryConditionProcessor</code>. Your code will typically look like this:</p>
-<div class="codehilite"><pre><span class="n">TypeManager</span> <span class="n">tm</span> <span class="o">=</span> <span class="k">new</span> <span class="n">MyTypeManager</span><span class="o">();</span> <span class="c1">// implements interface TypeManager</span>
-
-<span class="n">IQueryConditionProcessor</span> <span class="n">myWalker</span> <span class="o">=</span> <span class="k">new</span> <span class="n">MyWalker</span><span class="o">();</span>
-                         <span class="c1">// implements interface IQueryConditionProcessor</span>
-                         <span class="c1">// or extends AbstractQueryConditionProcessor</span>
+as input parameter. Your code will typically look like this:</p>
+<div class="codehilite"><pre><span class="kd">public</span> <span class="kd">class</span> <span class="nc">MyWalker</span> <span class="kd">extends</span> <span class="n">AbstractPredicateWalker</span> <span class="o">{</span>
+                         <span class="c1">// extends AbstractPredicateWalker</span>
+                         <span class="c1">// or implements interface PredicateWalker</span>
+                         <span class="c1">// or implements interface PredicateWalkerBase</span>
+  <span class="c1">// . . .</span>
+<span class="o">}</span>
+
+<span class="n">TypeManager</span> <span class="n">tm</span> <span class="o">=</span> <span class="k">new</span> <span class="n">MyTypeManager</span><span class="o">();</span> <span class="c1">// implements interface TypeManager</span>
+<span class="n">MyWalker</span> <span class="n">myWalker</span> <span class="o">=</span> <span class="k">new</span> <span class="n">MyWalker</span><span class="o">();</span>    
+<span class="n">queryObj</span> <span class="o">=</span> <span class="k">new</span> <span class="n">QueryObject</span><span class="o">(</span><span class="n">tm</span><span class="o">);</span>
+<span class="n">QueryUtil</span> <span class="n">queryUtil</span> <span class="o">=</span> <span class="k">new</span> <span class="n">QueryUtil</span><span class="o">();</span>
 
-<span class="n">queryObj</span> <span class="o">=</span> <span class="k">new</span> <span class="n">QueryObject</span><span class="o">(</span><span class="n">tm</span><span class="o">,</span> <span class="n">myWalker</span><span class="o">);</span>
+<span class="n">CmisQueryWalker</span> <span class="n">queryProcessor</span> <span class="o">=</span> <span class="n">queryUtil</span><span class="o">.</span><span class="na">traverseStatementAndCatchExc</span><span class="o">(</span><span class="n">statement</span><span class="o">,</span> <span class="n">queryObj</span><span class="o">,</span> <span class="n">myWalker</span><span class="o">);</span>
 </pre></div>
 
 
-<p><code>queryObj</code> then will process the statement and call the interface methods of
-your walker:</p>
+<p><code>queryUtil</code> then will process the statement and call the interface methods of
+your walker (Note: This code is in opencmis, you don't have to implement it
+yourself.):</p>
 <div class="codehilite"><pre><span class="k">try</span> <span class="o">{</span>
-
-    <span class="n">CmisQueryWalker</span> <span class="n">walker</span> <span class="o">=</span> <span class="n">QueryObject</span><span class="o">.</span><span class="na">getWalker</span><span class="o">(</span><span class="n">statement</span><span class="o">);</span>
-    <span class="n">walker</span><span class="o">.</span><span class="na">query</span><span class="o">(</span><span class="n">queryObj</span><span class="o">);</span>
-
+    <span class="n">walker</span> <span class="o">=</span> <span class="n">getWalker</span><span class="o">(</span><span class="n">statement</span><span class="o">);</span>
+    <span class="n">walker</span><span class="o">.</span><span class="na">query</span><span class="o">(</span><span class="n">queryObj</span><span class="o">,</span> <span class="n">pw</span><span class="o">);</span>
+    <span class="k">return</span> <span class="n">walker</span><span class="o">;</span> 
 <span class="o">}</span> <span class="k">catch</span> <span class="o">(</span><span class="n">RecognitionException</span> <span class="n">e</span><span class="o">)</span> <span class="o">{</span>
-    <span class="k">throw</span> <span class="k">new</span> <span class="nf">RuntimeException</span><span class="o">(</span><span class="s">&quot;Walking of statement failed with RecognitionException error:\n &quot;</span> <span class="o">+</span> <span class="n">e</span><span class="o">);</span>
+    <span class="n">String</span> <span class="n">errorMsg</span> <span class="o">=</span> <span class="n">queryObj</span><span class="o">.</span><span class="na">getErrorMessage</span><span class="o">();</span>
+    <span class="k">throw</span> <span class="k">new</span> <span class="nf">CmisInvalidArgumentException</span><span class="o">(</span><span class="s">&quot;Walking of statement failed with RecognitionException error: \n   &quot;</span> <span class="o">+</span> <span class="n">errorMsg</span><span class="o">);</span>
+<span class="o">}</span> <span class="k">catch</span> <span class="o">(</span><span class="n">CmisBaseException</span> <span class="n">e</span><span class="o">)</span> <span class="o">{</span>
+    <span class="k">throw</span> <span class="n">e</span><span class="o">;</span>
 <span class="o">}</span> <span class="k">catch</span> <span class="o">(</span><span class="n">Exception</span> <span class="n">e</span><span class="o">)</span> <span class="o">{</span>
-    <span class="k">throw</span> <span class="k">new</span> <span class="nf">RuntimeException</span><span class="o">(</span><span class="s">&quot;Walking of statement failed with exception:\n &quot;</span> <span class="o">+</span> <span class="n">e</span><span class="o">);</span>
+    <span class="k">throw</span> <span class="k">new</span> <span class="nf">CmisInvalidArgumentException</span><span class="o">(</span><span class="s">&quot;Walking of statement failed with exception: \n   &quot;</span> <span class="o">+</span> <span class="n">e</span><span class="o">);</span>
 <span class="o">}</span>
 </pre></div>
 
@@ -312,18 +326,18 @@ example the statement</p>
 </pre></div>
 
 
-<p>will result in calling the method <code>onLessThan()</code> in your walker callback
+<p>will result in calling the method <code>walkLessThan()</code> in your walker callback
 implementation:</p>
-<div class="codehilite"><pre><span class="kd">public</span> <span class="kt">void</span> <span class="nf">onLessThan</span><span class="o">(</span><span class="n">Tree</span> <span class="n">ltNode</span><span class="o">,</span> <span class="n">Tree</span> <span class="n">leftNode</span><span class="o">,</span> <span class="n">Tree</span> <span class="n">rightNode</span><span class="o">)</span> <span class="o">{</span>
+<div class="codehilite"><pre><span class="kd">public</span> <span class="n">Boolean</span> <span class="nf">walkLessThan</span><span class="o">(</span><span class="n">Tree</span> <span class="n">ltNode</span><span class="o">,</span> <span class="n">Tree</span> <span class="n">leftNode</span><span class="o">,</span> <span class="n">Tree</span> <span class="n">rightNode</span><span class="o">)</span> <span class="o">{</span>
 
-    <span class="n">Object</span> <span class="n">rVal</span> <span class="o">=</span> <span class="n">onLiteral</span><span class="o">(</span><span class="n">rightChild</span><span class="o">);</span>
+    <span class="n">Object</span> <span class="n">rVal</span> <span class="o">=</span> <span class="n">walkLiteral</span><span class="o">(</span><span class="n">rightChild</span><span class="o">);</span>
     <span class="n">ColumnReference</span> <span class="n">colRef</span><span class="o">;</span>
 
     <span class="n">CmisSelector</span> <span class="n">sel</span> <span class="o">=</span> <span class="n">queryObj</span><span class="o">.</span><span class="na">getColumnReference</span><span class="o">(</span><span class="n">columnNode</span>
              <span class="o">.</span><span class="na">getTokenStartIndex</span><span class="o">());</span>
 
     <span class="k">if</span> <span class="o">(</span><span class="kc">null</span> <span class="o">==</span> <span class="n">sel</span><span class="o">)</span>
-       <span class="k">throw</span> <span class="k">new</span> <span class="nf">RuntimeException</span><span class="o">(</span><span class="s">&quot;Unknown property query name &quot;</span> <span class="o">+</span>
+       <span class="k">throw</span> <span class="k">new</span> <span class="nf">CmisInvalidArgumentException</span><span class="o">(</span><span class="s">&quot;Unknown property query name &quot;</span> <span class="o">+</span>
               <span class="n">columnNode</span><span class="o">.</span><span class="na">getChild</span><span class="o">(</span><span class="mi">0</span><span class="o">));</span>
     <span class="k">else</span> <span class="nf">if</span> <span class="o">(</span><span class="n">sel</span> <span class="k">instanceof</span> <span class="n">ColumnReference</span><span class="o">)</span>
        <span class="n">colRef</span> <span class="o">=</span> <span class="o">(</span><span class="n">ColumnReference</span><span class="o">)</span> <span class="n">sel</span><span class="o">;</span>
@@ -339,7 +353,7 @@ implementation:</p>
 
 
 <p>The right child node is a literal and you will get an Integer object with
-value 123. The left node is a reference to property and
+value 123. The left node is a reference to a property and
 <code>getColumnReference()</code> will either give you a function (currently the only
 supported function is <code>SCORE()</code>) or a reference to a property in a type of
 your type system. The query object maintains several maps to resolve
@@ -356,7 +370,11 @@ the requested information:</p>
 
 
 <p>Key of the map is the query name and value is the alias if an alias was
-used in the statement or the query name otherwise.</p></div>
+used in the statement or the query name otherwise.</p>
+<h2 id="limitations">Limitations</h2>
+<p>Currently the query parser does not include the full text search part
+of the grammar. Support for JOIN is limited. This will be enhanced in a
+future version</p></div>
              <!-- Content -->
            </td>
           </tr>