Difference between revisions of "Query Mode"
Jump to navigation
Jump to search
(Created page with "<p>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 <code>query()</code> function of the <code>Session</code...") |
m (Text replacement - "syntaxhighlight" to "pre") Tags: Mobile web edit Mobile edit |
||
| (49 intermediate revisions by 4 users not shown) | |||
| Line 1: | Line 1: | ||
| − | <p> | + | <p>The query mode of the [[Clients]] allows you to bind external variables to a query |
| − | in an iterative manner. | + | and evaluate the query in an iterative manner. The <code>query()</code> function of the |
| − | of the <code>Session</code> | + | <code>Session</code> instance returns a new query instance.</p> |
| − | |||
| − | |||
| − | |||
| − | + | ==Usage== | |
| − | + | The query execution works as follows: | |
| − | + | # Create a new session instance with hostname, port, username and password. | |
| − | + | # Call <code>query()</code> with your XQuery expression to get a query object. | |
| − | + | # Optionally bind variables to the query with one of the <code>bind()</code> functions. | |
| − | + | # Optionally bind a value to the context item via <code>context()</code>. | |
| − | + | # Iterate through the query object with the <code>more()</code> and <code>next()</code> functions. | |
| − | + | # As an alternative, call <code>execute()</code> to get the whole result at a time. | |
| − | + | # <code>info()</code> gives you information on query evaluation. | |
| − | < | + | # <code>options()</code> returns the query serialization parameters. |
| − | </ | + | # Don't forget to close the query with <code>close()</code>. |
| − | + | ||
| − | + | ==PHP Example== | |
| − | + | ||
| − | + | Taken from our [https://github.com/BaseXdb/basex/blob/master/basex-api/src/main/php/QueryBindExample.php repository]: | |
| − | + | ||
| − | + | <pre lang="php"> | |
| − | + | <?php | |
| − | + | /* | |
| − | + | * This example shows how queries can be executed in an iterative manner. | |
| − | + | * Documentation: http://basex.org/api | |
| − | + | * | |
| − | + | * (C) BaseX Team 2005-15, BSD License | |
| − | + | */ | |
| − | + | include("BaseXClient.php"); | |
| − | + | ||
| − | + | try { | |
| − | + | // create session | |
| − | + | $session = new Session("localhost", 1984, "admin", "..."); | |
| + | |||
| + | try { | ||
| + | // create query instance | ||
| + | $input = 'declare variable $name external; '. | ||
| + | 'for $i in 1 to 10 return element { $name } { $i }'; | ||
| + | $query = $session->query($input); | ||
| + | |||
| + | // bind variable | ||
| + | $query->bind("$name", "number"); | ||
| + | |||
| + | // print result | ||
| + | print $query->execute()."\n"; | ||
| + | |||
| + | // close query instance | ||
| + | $query->close(); | ||
| + | |||
| + | } catch (Exception $e) { | ||
| + | // print exception | ||
| + | print $e->getMessage(); | ||
| + | } | ||
| + | |||
| + | // close session | ||
| + | $session->close(); | ||
| + | |||
| + | } catch (Exception $e) { | ||
| + | // print exception | ||
| + | print $e->getMessage(); | ||
| + | } | ||
| + | ?> | ||
| + | </pre> | ||
| + | |||
| + | =Changelog= | ||
| + | |||
| + | ;Version 7.2 | ||
| + | |||
| + | * Added: {{Code|context()}} function | ||
Latest revision as of 18:38, 1 December 2023
The query mode of the Clients allows you to bind external variables to a query
and evaluate the query in an iterative manner. The query() function of the
Session instance returns a new query instance.
Usage[edit]
The query execution works as follows:
- Create a new session instance with hostname, port, username and password.
- Call
query()with your XQuery expression to get a query object. - Optionally bind variables to the query with one of the
bind()functions. - Optionally bind a value to the context item via
context(). - Iterate through the query object with the
more()andnext()functions. - As an alternative, call
execute()to get the whole result at a time. info()gives you information on query evaluation.options()returns the query serialization parameters.- Don't forget to close the query with
close().
PHP Example[edit]
Taken from our repository:
<?php
/*
* This example shows how queries can be executed in an iterative manner.
* Documentation: http://basex.org/api
*
* (C) BaseX Team 2005-15, BSD License
*/
include("BaseXClient.php");
try {
// create session
$session = new Session("localhost", 1984, "admin", "...");
try {
// create query instance
$input = 'declare variable $name external; '.
'for $i in 1 to 10 return element { $name } { $i }';
$query = $session->query($input);
// bind variable
$query->bind("$name", "number");
// print result
print $query->execute()."\n";
// close query instance
$query->close();
} catch (Exception $e) {
// print exception
print $e->getMessage();
}
// close session
$session->close();
} catch (Exception $e) {
// print exception
print $e->getMessage();
}
?>
Changelog[edit]
- Version 7.2
- Added:
context()function