XQuery Module
This XQuery Module contains functions for evaluating XQuery strings and modules at runtime.
Conventions
All functions and errors in this module are assigned to the http://basex.org/modules/xquery
namespace, which is statically bound to the xquery
prefix.
Functions
This XQuery Module contains functions to access relational databases from XQuery using SQL. With this module, you can execute query, update and prepared statements, and the result sets are returned as sequences of XML elements representing tuples. Each element has children representing the columns returned by the SQL statement.
This module uses JDBC to connect to a SQL server. Hence, your JDBC driver will need to be added to the classpath, too. If you work with the full distributions of BaseX, you can copy the driver into the lib
directory. To connect to MySQL, for example, download the Connector/J Driver and extract the archive into this directory.
Conventions
All functions and errors in this module are assigned to the http://basex.org/modules/sql
namespace, which is statically bound to the sql
prefix.
Functions
sql:init
Signatures | sql:init($class as xs:string) as empty-sequence()
|
Summary | This function initializes a JDBC driver specified via $class . This step might be superfluous if the SQL database is not embedded. |
Errors | init : the specified driver is not found.
|
sql:connect
Signatures | sql:connect($url as xs:string) as xs:anyURI sql:connect($url as xs:string, $user as xs:string, $password as xs:string) as xs:anyURI sql:connect($url as xs:string, $user as xs:string, $password as xs:string, $options as map(*)) as xs:anyURI |
Summary | This function establishes a connection to a relational database and returns a connection id. The parameter $url is the URL of the database and shall be of the form: jdbc:<driver name>:<server> [/<database>] . If the parameters $user and $password are specified, they are used as credentials for connecting to the database. The $options parameter can be used to set connection options.
|
Errors | error : an SQL exception occurs, e.g. missing JDBC driver or not existing relation.
|
Examples | Connects to an SQL Server and sets autocommit to true :
sql:connect('dbc:sqlserver://DBServer', map { 'autocommit': true() }) |
sql:execute
Template:Mark: Return update count for updating statements. $options
argument added.
Signatures | sql:execute($id as xs:anyURI, $statement as xs:string) as item()* sql:execute($id as xs:anyURI, $statement as xs:string, $options as map(*)) as item()*
|
Summary | This function executes an SQL $statement , using the connection with the specified $id . The returned result depends on the kind of statement:
With
|
Errors | error : an SQL exception occurs, e.g. not existing relation is retrieved.id : the specified connection does not exist. |
sql:execute-prepared
Template:Mark: Return update count for updating statements.
Signatures | sql:execute-prepared($id as xs:anyURI, $params as element(sql:parameters)) as item()* sql:execute-prepared($id as xs:anyURI, $params as element(sql:parameters), $options as map(*)) as item()*
|
Summary | This function executes a prepared statement:
With
element sql:parameters { element sql:parameter { attribute type { "int" | "string" | "boolean" | "date" | "double" | "float" | "short" | "time" | "timestamp" | "sqlxml" }, attribute null { "true" | "false" }?, text }+ }? |
Errors | attribute : an attribute different from type and null is set for a <sql:parameter/> element.error : an SQL exception occurs, e.g. not existing relation is retrieved.id : the specified connection does not exist.parameters : wrong number of <sql:parameter/> elements, or parameter type is not specified.type : the value of a parameter cannot be converted to the specified format.
|
sql:prepare
Signatures | sql:prepare($id as xs:anyURI, $statement as xs:string) as xs:anyURI
|
Summary | This function prepares an SQL $statement , using the specified connection $id , and returns the id reference to this statement. The statement is a string with one or more '?' placeholders. If the value of a field has to be set to NULL , then the attribute null of the <sql:parameter/> element must be true .
|
Errors | error : an SQL exception occurs.id : the specified connection does not exist. |
sql:commit
Signatures | sql:commit($id as xs:anyURI) as empty-sequence()
|
Summary | This function commits the changes made to a relational database, using the specified connection $id .
|
Errors | error : an SQL exception occurs.id : the specified connection does not exist. |
sql:rollback
Signatures | sql:rollback($id as xs:anyURI) as empty-sequence()
|
Summary | This function rolls back the changes made to a relational database, using the specified connection $id .
|
Errors | error : an SQL exception occurs.id : the specified connection does not exist. |
sql:close
Signatures | sql:close($id as xs:anyURI) as empty-sequence()
|
Summary | This function closes a database connection with the specified $id .Opened connections will automatically be closed after the XQuery expression has been evaluated, but in order to save memory, it is always recommendable to close connections that are not used anymore. |
Errors | error : an SQL exception occurs.id : the specified connection does not exist. |
Examples
Direct queries
A simple select statement can be executed as follows:
let $id := sql:connect("jdbc:postgresql://localhost:5432/coffeehouse") return sql:execute($id, "SELECT * FROM coffees WHERE price < 10")
The result may look like:
<sql:row xmlns:sql="http://basex.org/modules/sql"> <sql:column name="cof_name">French_Roast</sql:column> <sql:column name="sup_id">49</sql:column> <sql:column name="price">9.5</sql:column> <sql:column name="sales">15</sql:column> <sql:column name="total">30</sql:column> </sql:row> <sql:row xmlns:sql="http://basex.org/modules/sql"> <sql:column name="cof_name">French_Roast_Decaf</sql:column> <sql:column name="sup_id">49</sql:column> <sql:column name="price">7.5</sql:column> <sql:column name="sales">10</sql:column> <sql:column name="total">14</sql:column> </sql:row>
Prepared Statements
A prepared select statement can be executed in the following way:
(: Establish a connection :) let $conn := sql:connect("jdbc:postgresql://localhost:5432/coffeehouse") (: Obtain a handle to a prepared statement :) let $prep := sql:prepare($conn, "SELECT * FROM coffees WHERE price < ? AND cof_name = ?") (: Values and types of prepared statement parameters :) let $params := <sql:parameters> <sql:parameter type='double'>10</sql:parameter> <sql:parameter type='string'>French_Roast</sql:parameter> </sql:parameters> (: Execute prepared statement :) return sql:execute-prepared($prep, $params)
SQLite
The following expression demonstrates how SQLite can be addressed using the Xerial SQLite JDBC driver:
(: Initialize driver :) sql:init("org.sqlite.JDBC"), (: Establish a connection :) let $conn := sql:connect("jdbc:sqlite:database.db") return ( (: Create a new table :) sql:execute($conn, "drop table if exists person"), sql:execute($conn, "create table person (id integer, name string)"), (: Run 10 updates :) for $i in 1 to 10 let $q := "insert into person values(" || $i || ", '" || $i || "')" return sql:execute($conn, $q), (: Return table contents :) sql:execute($conn, "select * from person") )
Errors
Code | Description |
---|---|
attribute
|
An attribute different from type and null is set for a <sql:parameter/> element.
|
error
|
An SQL exception occurred. |
id
|
A connection does not exist. |
init
|
A database driver is not found. |
parameters
|
Wrong number of <sql:parameter/> elements, or parameter type is not specified.
|
type
|
The value of a parameter cannot be converted to the specified format. |
Changelog
- Version 9.0
- Updated: sql:execute, sql:execute-prepared: Return update count for updating statements.
$options
argument added.
- Updated: Connection ids are URIs now.
- Updated: error codes updated; errors now use the module namespace
- Version 7.5
- Updated: prepared statements are now executed via sql:execute-prepared
The module was introduced with Version 7.0.
xquery:eval-update
Template:Mark: Renamed (old name: xquery:update)
Signatures | xquery:eval-update($query as xs:string) as item()* xquery:eval-update($query as xs:string, $bindings as map(*)?) as item()* xquery:eval-update($query as xs:string, $bindings as map(*)?, $options as map(xs:string, xs:string)) as item() |
Summary | Evaluates $query as updating XQuery expression at runtime.All updates will be added to the Pending Update List of the main query and performed after the evaluation of the main query. The rules of the $bindings and $options parameters are the same as for xquery:eval.
|
Errors | update : the query contains no updating expressions.permission : insufficient permissions for evaluating the query.timeout : query execution exceeded timeout.limit : query execution exceeded memory limit.nested : nested query evaluation is not allowed.Any other error that may occur while evaluating the query. |
xquery:invoke
Signatures | xquery:invoke($uri as xs:string) as item()* xquery:invoke($uri as xs:string, $bindings as map(*)?) as item()* xquery:invoke($uri as xs:string, $bindings as map(*)?, $options as map(*)) as item()* |
Summary | Evaluates the XQuery module located at $uri at runtime and returns the resulting items. A relative URI will be resolved against the static base URI of the query.The rules of the $bindings and $options parameters are the same as for xquery:eval.
|
Errors | update : the expression contains updating expressions.permission : insufficient permissions for evaluating the query.BXXQ0004 : query execution exceeded timeout.nested : nested query evaluation is not allowed.Any other error that may occur while evaluating the query. |
xquery:invoke-update
Signatures | xquery:invoke-update($uri as xs:string) as item()* xquery:invoke-update($uri as xs:string, $bindings as map(*)?) as item()* xquery:invoke-update($uri as xs:string, $bindings as map(*)?, $options as map(*)) as item()* |
Summary | Evaluates the updating XQuery module located at $uri at runtime. A relative URI will be resolved against the static base URI of the query.The rules of the $bindings and $options parameters are the same as for xquery:eval.
|
Errors | update : the expression contains no updating expressions.permission : insufficient permissions for evaluating the query.BXXQ0004 : query execution exceeded timeout.nested : nested query evaluation is not allowed.Any other error that may occur while evaluating the query. |
xquery:parse
Template:Mark: base-uri
option added.
Signatures | xquery:parse($query as xs:string) as item()? xquery:parse($query as xs:string, $options as map(*)) as item()? |
Summary | Parses the specified $query string as XQuery module and returns the resulting query plan. The $options parameter influences the output:
|
Errors | Any error that may occur while parsing the query. |
Examples |
<MainModule updating="false"> <QueryPlan compiled="false"> <Arith op="+"> <Int value="1" type="xs:integer"/> <Int value="3" type="xs:integer"/> </Arith> </QueryPlan> </MainModule> |
xquery:parse-uri
Template:Mark: base-uri
option added.
Signatures | xquery:parse-uri($uri as xs:string) as item()? xquery:parse-uri($uri as xs:string, $options as map(*)) as item()? |
Summary | Parses the XQuery module located at $uri and returns the resulting query plan. A relative URI will be resolved against the static base URI of the query. The rules for the $options parameter are the same as for xquery:parse.
|
Errors | Any error that may occur while parsing the query. |
Parallelized Execution
Parallel query execution is recommendable if you have various calls that require a lot of time, but cannot be sped up by rewriting the code. This is e. g. the case if external URLs are called. If you are parallelizing local data reads (such as the access to a database), single-threaded queries will usually be faster, because parallelized access to disk data often results in randomized access patterns, which can hardly be optimized by the caching strategies of HD, SSD or operating system.
xquery:fork-join
Signatures | xquery:fork-join($functions as function(*)*) as item()*
|
Summary | This function executes the supplied (non-updating) functions in parallel. |
Examples |
let $f := function() { prof:sleep(1000) } return xquery:fork-join(($f, $f))
let $funcs := for $segment in 1 to 4 let $url := 'http://url.com/path' || $segment return function() { http:send-request((), $url) } return xquery:fork-join($funcs) |
Errors | error : an unexpected error occurred.
|
Errors
Code | Description |
---|---|
permission
|
Insufficient permissions for evaluating the query. |
update
|
updating expression found or expected. |
timeout
|
Query execution exceeded timeout. |
memory
|
Query execution exceeded memory limit. |
nested
|
Nested query evaluation is not allowed. |
error
|
An unexpected error occurred. |
Changelog
- Version 9.0
- Added: xquery:invoke-uri
- Updated: xquery:eval:
pass
option added - Updated: xquery:parse, xquery:parse-uri:
base-uri
option added - Updated: xquery:update renamed to xquery:eval-update
- Updated: error codes updated; errors now use the module namespace
- Version 8.5
- Added: xquery:fork-join
- Updated: xquery:eval:
base-uri
option added - Updated: Relative URIs will always be resolved against the static base URI of the query
- Deleted: xquery:type (moved to Profiling Module
- Version 8.4
- Added: xquery:parse-uri
- Updated: xquery:parse:
pass
option added
- Version 8.0
- Added: xquery:update, xquery:parse
- Deleted: xquery:evaluate (opened databases will now be closed by main query)
- Version 7.8.2
- Added:
$options
argument
- Version 7.8
- Added: xquery:evaluate
- Updated: used variables must be explicitly declared in the query string.
This module was introduced with Version 7.3. Functions have been adopted from the obsolete Utility Module.