<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
"http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd">
<section id="JCR.ORConstraint">
  <title>OR Constraint</title>

  <para>Find all documents whose title is 'Cinderella' or whose description is
  'novel'.</para>

  <para>How does it sound in jcr terms? - Find all nodes with a mixin type
  'mix:title' whose property 'jcr:title' equals "Cinderella" or whose
  "jcr:description" property value is "novel".</para>

  <section id="JCR.ORConstraint.RepositoryStructure">
    <title>Repository Structure</title>

    <para>The repository contains mix:title nodes, where jcr:title and
    jcr:description have different values.</para>

    <itemizedlist>
      <listitem>
        <para>root</para>

        <itemizedlist>
          <listitem>
            <para>document1 (mix:title) jcr:title="War and peace"
            jcr:description="novel"</para>
          </listitem>

          <listitem>
            <para>document2 (mix:title) jcr:title="Cinderella"
            jcr:description="fairytale"</para>
          </listitem>

          <listitem>
            <para>document3 (mix:title) jcr:title="Puss in Boots"
            jcr:description="fairytale"</para>
          </listitem>
        </itemizedlist>
      </listitem>
    </itemizedlist>
  </section>

  <section id="JCR.ORConstraint.QueryExecution">
    <title>Query Execution</title>

    <para><emphasis role="bold">SQL</emphasis></para>

    <programlisting language="java">// make SQL query
QueryManager queryManager = workspace.getQueryManager();
// create query
String sqlStatement = "SELECT * FROM mix:title WHERE jcr:title = 'Cinderella' OR jcr:description = 'novel'";
Query query = queryManager.createQuery(sqlStatement, Query.SQL);
// execute query and fetch result
QueryResult result = query.execute();</programlisting>

    <para><emphasis role="bold">XPath</emphasis></para>

    <programlisting language="java">// make XPath query
QueryManager queryManager = workspace.getQueryManager();
// create query
String xpathStatement = "//element(*,mix:title)[@jcr:title='Cinderella' or @jcr:description = 'novel']";
Query query = queryManager.createQuery(xpathStatement, Query.XPATH);
// execute query and fetch result
QueryResult result = query.execute();</programlisting>
  </section>

  <section id="JCR.ORConstraint.FetchingResult">
    <title>Fetching the Result</title>

    <para>Let's get nodes:</para>

    <programlisting language="java">NodeIterator it = result.getNodes();

if(it.hasNext())
{
   Node findedNode = it.nextNode();
}</programlisting>

    <para>NodeIterator will return "document1" and "document2".</para>

    <para>We can also get a table:</para>

    <programlisting language="java">String[] columnNames = result.getColumnNames();
RowIterator rit = result.getRows();
while (rit.hasNext())
{
   Row row = rit.nextRow();
   // get values of the row
   Value[] values = row.getValues();
}</programlisting>

    <para>Table content is: <table>
        <title>Table content</title>

        <tgroup cols="4">
          <thead>
            <row>
              <entry> jcr:title</entry>

              <entry>jcr:description</entry>

              <entry>jcr:path</entry>

              <entry>jcr:score</entry>
            </row>
          </thead>

          <tbody>
            <row>
              <entry>War and peace</entry>

              <entry>novel</entry>

              <entry>/document1</entry>

              <entry>3806</entry>
            </row>

            <row>
              <entry>Cinderella</entry>

              <entry>fairytale</entry>

              <entry>/document2</entry>

              <entry>3806</entry>
            </row>
          </tbody>
        </tgroup>
      </table></para>
  </section>
</section>
