<?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.SetOffsetandSetLimit">
  <title>SetOffset and SetLimit</title>

  <para>Select all nodes with primary type 'nt:unstructured' and returns only
  3 nodes starting with the second node in the list.</para>

  <section id="JCR.SetOffsetandSetLimit.CommonInfo">
    <title>Common info</title>

    <para>QueryImpl class has two methods: one to indicate how many results
    shall be returned at most, and another to fix the starting
    position.</para>

    <itemizedlist>
      <listitem>
        <para>setOffset(long offset) - Sets the start offset of the result
        set.</para>
      </listitem>

      <listitem>
        <para>setLimit(long position) - Sets the maximum size of the result
        set.</para>
      </listitem>
    </itemizedlist>
  </section>

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

    <para>Repository contains mix:title nodes, where jcr:title has different
    values.</para>

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

        <itemizedlist>
          <listitem>
            <para>node1 (nt:unstructured)</para>
          </listitem>

          <listitem>
            <para>node2 (nt:unstructured)</para>
          </listitem>

          <listitem>
            <para>node3 (nt:unstructured)</para>
          </listitem>

          <listitem>
            <para>node4 (nt:unstructured)</para>
          </listitem>

          <listitem>
            <para>node5 (nt:unstructured)</para>
          </listitem>

          <listitem>
            <para>node6 (nt:unstructured)</para>
          </listitem>
        </itemizedlist>
      </listitem>
    </itemizedlist>
  </section>

  <section id="JCR.SetOffsetandSetLimit.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 nt:unstructured";
QueryImpl query = (QueryImpl)queryManager.createQuery(sqlStatement, Query.SQL);
//return starting with second result
query.setOffset(1);
// return 3 results
query.setLimit(3);
// execute query and fetch result
QueryResult result = query.execute();</programlisting>
  </section>

  <section id="JCR.SetOffsetandSetLimit.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>In usual case (without using setOffset and setLimit methods), Node
    iterator returns all nodes (node1...node6). But in our case NodeIterator
    will return "node2","node3" and "node4".</para>

    <para>\[node1 <emphasis role="bold">node2</emphasis> <emphasis
    role="bold">node3</emphasis> <emphasis role="bold">node4</emphasis> node5
    node6\]</para>
  </section>
</section>