<?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.LIKEConstraint">
  <title>LIKE Constraint</title>

  <para>Find all nodes with mixin type 'mix:title' and where the property
  'jcr:title' starts with 'P'.</para>

  <note>
    <para>See also the article about "<link linkend="JCR.NOTConstraint">Find
    all mix:title nodes where jcr:title does NOT start with 'P'</link>"</para>
  </note>

  <section id="JCR.LIKEConstraint.RepositoryStructure">
    <title>Repository structure:</title>

    <para>The repository contains 3 mix:title nodes, where each jcr:title has
    a different value.</para>

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

        <itemizedlist>
          <listitem>
            <para>document1 (mix:title) jcr:title="Star wars"
            jcr:description="Dart rules!!"</para>
          </listitem>

          <listitem>
            <para>document2 (mix:title) jcr:title="Prison break"
            jcr:description="Run, Forest, run ))"</para>
          </listitem>

          <listitem>
            <para>document3 (mix:title) jcr:title="Panopticum"
            jcr:description="It's imagine film"</para>
          </listitem>
        </itemizedlist>
      </listitem>
    </itemizedlist>
  </section>

  <section id="JCR.LIKEConstraint.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 LIKE 'P%'";
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:like(@jcr:title, 'P%')]";
Query query = queryManager.createQuery(xpathStatement, Query.XPATH);
// execute query and fetch result
QueryResult result = query.execute();</programlisting>
  </section>

  <section id="JCR.LIKEConstraint.FetchingResult">
    <title>Fetching result</title>

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

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

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

    <para>The NodeIterator will return "document2" and "document3".</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>The 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>Prison break</entry>

              <entry>Run, Forest, run ))</entry>

              <entry>/document2</entry>

              <entry>4713</entry>
            </row>

            <row>
              <entry>Panopticum</entry>

              <entry>It's imagine film</entry>

              <entry>/document3</entry>

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