Difference between revisions of "Query Mode"

From BaseX Documentation
Jump to navigation Jump to search
Line 6: Line 6:
 
of the query object.</p>  
 
of the query object.</p>  
 
   
 
   
<h2>Usage</h2>
+
==Usage==
 
   
 
   
 
<p>The query execution works as follows:</p>  
 
<p>The query execution works as follows:</p>  
 
   
 
   
<ol start="1">
+
#Create a new session instance with hostname, port, username and password.
<li>Create a new session instance with hostname, port, username and password.</li>
+
#Call the <code>query()</code> function of the session with the query as argument to get your query object.
<li>Call the <code>query()</code> function of the session with the query as argument to get your query object.</li>
+
#Optionally bind variables to the query with the <code>bind()</code> function.
<li>Optionally bind variables to the query with the <code>bind()</code> function.</li>
+
#Initialize query output via <code>init()</code>.
<li>Initialize query output via <code>init()</code>.</li>
+
#Iterate through the query object with the <code>more()</code> and <code>next()</code> functions. If an error occurs, an exception is thrown.
<li>Iterate through the query object with the <code>more()</code> and <code>next()</code> functions.
+
#Close the query with <code>close()</code>.  
If an error occurs, an exception is thrown.</li>
 
<li>Close the query with <code>close()</code>.</li>
 
</ol>
 
 
   
 
   
<h2>Example</h2>
+
==Example==
 
   
 
   
<ol start="1">
+
#Create a session object: <code>Session session = new Session("localhost", 1984, "admin", "admin");</code></li>  
<li>Create a session object: <code>Session session = new Session("localhost", 1984, "admin", "admin");</code></li>  
+
#Define a query and create a query object:<br/><pre class="brush:java">String q = "declare variable $name external; " "for $i in 1 to 10 return element { $name } { $i }";
<li>Define a query and create a query object:<br/>  
+
Query query = session.query(q);</pre>  
    <code>String q = "declare variable $name external; " +
+
#Call the bind method of your query object:<br/><pre class="brush:java">query.bind("$name", "Number");</pre>  
          "for $i in 1 to 10 return element { $name } { $i }";
+
#Call the init method of your query object:<br/><<pre class="brush:java">print query.init();</pre>  
Query query = session.query(q);</code></li>  
+
#Iterate through the query object:<br/><pre class="brush:java">while(query.more()) print query.next();</pre>
<li>Call the bind method of your query object:<br/>  
+
#Close your query object:<br/><pre class="brush:java">print query.close();</pre>
    <code>query.bind("$name", "Number");</code></li>  
 
<li>Call the init method of your query object:<br/>  
 
    <code>print query.init();</code></li>  
 
<li>Iterate through the query object:<br/>  
 
    <code>while(query.more()) print query.next();</code></li>  
 
<li>Close your query object:<br/>  
 
    <code>print query.close();</code></li>  
 
</ol>
 
[[Category:Wikify]]
 

Revision as of 18:30, 12 December 2010

In the query mode a query can be send to the server and executed in an iterative manner. For this you have to call the query() function of the Session with your defined query. This will return a query object which comes with an forward-only iterator to get each result of the query. Furthermore it is possible to bind variables to the query using the bind() function of the query object.

Usage

The query execution works as follows:

  1. Create a new session instance with hostname, port, username and password.
  2. Call the query() function of the session with the query as argument to get your query object.
  3. Optionally bind variables to the query with the bind() function.
  4. Initialize query output via init().
  5. Iterate through the query object with the more() and next() functions. If an error occurs, an exception is thrown.
  6. Close the query with close().

Example

  1. Create a session object: Session session = new Session("localhost", 1984, "admin", "admin");
  2. Define a query and create a query object:
    String q = "declare variable $name external; " "for $i in 1 to 10 return element { $name } { $i }";

Query query = session.query(q);

  1. Call the bind method of your query object:
    query.bind("$name", "Number");
  2. Call the init method of your query object:
    <
    print query.init();
  3. Iterate through the query object:
    while(query.more()) print query.next();
  4. Close your query object:
    print query.close();