Difference between revisions of "SQL Module"

From BaseX Documentation
Jump to navigation Jump to search
Line 3: Line 3:
 
=Conventions=
 
=Conventions=
  
All functions in this module are assigned to the <code>http://basex.org/modules/sql</code> namespace, which is statically bound to the <code>sql</code> prefix.<br/>
+
All functions in this module are assigned to the {{Code|http://basex.org/modules/sql}} namespace, which is statically bound to the {{Code|sql}} prefix.<br/>
All errors are assigned to the <code>http://basex.org/errors</code> namespace, which is statically bound to the <code>bxerr</code> prefix.
+
All errors are assigned to the {{Code|http://basex.org/errors}} namespace, which is statically bound to the {{Code|bxerr}} prefix.
  
 
=Functions=
 
=Functions=
Line 12: Line 12:
 
|-
 
|-
 
| width='90' | '''Signatures'''
 
| width='90' | '''Signatures'''
|<code><b>sql:init</b>($class as xs:string) as empty-sequence()</code>
+
|{{Func|sql:init|$class as xs:string|empty-sequence()}}
 
|-
 
|-
 
| '''Summary'''
 
| '''Summary'''
|This function initializes a JDBC driver specified via <code>$class</code>. This step might be superfluous if the SQL database is not embedded.<br/ >
+
|This function initializes a JDBC driver specified via {{Code|$class}}. This step might be superfluous if the SQL database is not embedded.<br/ >
 
|-
 
|-
 
| '''Errors'''
 
| '''Errors'''
|<b>[[XQuery Errors#SQL Functions Errors|BXSQ0007]]</b> is raised if the specified driver class is not found.
+
|{{Error|BXSQ0007|XQuery Errors#SQL Functions Errors}} the specified driver class is not found.
 
|}
 
|}
  
Line 25: Line 25:
 
|-
 
|-
 
| width='90' | '''Signatures'''
 
| width='90' | '''Signatures'''
|<code><b>sql:connect</b>($url as xs:string) as xs:integer</code><br/ ><code><b>sql:connect</b>($url as xs:string, $user as xs:string, $password as xs:string) as xs:integer</code><br/ ><code><b>sql:connect</b>($url as xs:string, $user as xs:string, $password as xs:string, $options as item()) as xs:integer</code><br/ >
+
|{{Func|sql:connect|$url as xs:string|xs:integer}}<br/ >{{Func|sql:connect|$url as xs:string, $user as xs:string, $password as xs:string|xs:integer}}<br/ >{{Func|sql:connect|$url as xs:string, $user as xs:string, $password as xs:string, $options as item()|xs:integer}}<br/ >
 
|-
 
|-
 
| '''Summary'''
 
| '''Summary'''
|This function establishes a connection to a relational database. As a result a connection handle is returned. The parameter <code>$url</code> is the URL of the database and shall be of the form: <code>jdbc:<driver name>:[//<server>[/<database>]</code>. If the parameters <code>$user</code> and <code>$password</code> are specified, they are used as credentials for connecting to the database. The parameter <code>$options</code> can be used to set connection options, e.g. auto-commit mode. It can be specified as:
+
|This function establishes a connection to a relational database. As a result a connection handle is returned. The parameter {{Code|$url}} is the URL of the database and shall be of the form: {{Code|jdbc:<driver name>:[//<server>[/<database>]}}. If the parameters {{Code|$user}} and {{Code|$password}} are specified, they are used as credentials for connecting to the database. The parameter {{Code|$options}} can be used to set connection options, e.g. auto-commit mode. It can be specified as:
* <code>element(sql:options):</code> <code>&lt;sql:options/&gt;</code> must be used as root element, and the options are specified as child nodes, with the element name representing the key and the text node representing the value:<br /
+
* {{Code|element(sql:options):}} {{Code|&lt;sql:options/&gt;}} must be used as root element, and the options are specified as child nodes, with the element name representing the key and the text node representing the value:<br /
/><code>&lt;sql:options&gt;<br/>&nbsp;&nbsp;&lt;sql:autocommit&gt;true&lt;/sql:autocommit&gt;<br/>&nbsp;&nbsp;...<br/>&lt;/sql:options&gt;</code>
+
/>{{Code|&lt;sql:options&gt;<br/>&nbsp;&nbsp;&lt;sql:autocommit&gt;true&lt;/sql:autocommit&gt;<br/>&nbsp;&nbsp;...<br/>&lt;/sql:options&gt;}}
* [[Map Module|map structure]]: all options can be directly represented as key/value pairs:<br /><code>map { "autocommit" := "true", ... </code>}<br/>
+
* [[Map Module|map structure]]: all options can be directly represented as key/value pairs:<br />{{Code|map { "autocommit" := "true", ... }}}<br/>
 
|-
 
|-
 
| '''Errors'''
 
| '''Errors'''
|<b>[[XQuery Errors#SQL Functions Errors|BXSQ0001]]</b> is raised if an SQL exception occurs, e.g. missing JDBC driver or not existing relation.
+
|{{Error|BXSQ0001|XQuery Errors#SQL Functions Errors}} an SQL exception occurs, e.g. missing JDBC driver or not existing relation.
 
|}
 
|}
  
Line 43: Line 43:
 
|-
 
|-
 
| width='90' | '''Signatures'''
 
| width='90' | '''Signatures'''
|<code><b>sql:execute</b>($connection as xs:integer, $item as item()) as element()*</code>
+
|{{Func|sql:execute|$connection as xs:integer, $item as item()|element()*}}
 
|-
 
|-
 
| '''Summary'''
 
| '''Summary'''
| This function executes a query, update or prepared statement. The parameter <code>$id</code> specifies either a connection handle or a prepared statement handle. The parameter <code>$item</code> is either a string representing an SQL statement or an element <code><sql:parameters/></code> representing the parameters for a prepared statement along with their types and values. In case of the latter, the following schema shall be used:<br/ >
+
| This function executes a query, update or prepared statement. The parameter {{Code|$id}} specifies either a connection handle or a prepared statement handle. The parameter {{Code|$item}} is either a string representing an SQL statement or an element {{Code|<sql:parameters/>}} representing the parameters for a prepared statement along with their types and values. In case of the latter, the following schema shall be used:<br/ >
 
<pre class="brush:xml">
 
<pre class="brush:xml">
 
element sql:parameters {
 
element sql:parameters {
Line 56: Line 56:
 
|-
 
|-
 
| '''Errors'''
 
| '''Errors'''
|<b>[[XQuery Errors#SQL Functions Errors|BXSQ0001]]</b> is raised if an SQL exception occurs, e.g. not existing relation is retrieved.<br/ >
+
|{{Error|BXSQ0001|XQuery Errors#SQL Functions Errors}} an SQL exception occurs, e.g. not existing relation is retrieved.<br/ >
<b>[[XQuery Errors#SQL Functions Errors|BXSQ0002]]</b> is raised if a wrong connection handle or prepared statement handle is passed.<br/ >
+
{{Error|BXSQ0002|XQuery Errors#SQL Functions Errors}} a wrong connection handle or prepared statement handle is passed.<br/ >
<b>[[XQuery Errors#SQL Functions Errors|BXSQ0003]]</b> is raised if the number of <code><sql:parameter/></code> elements in <code><sql:parameters/></code> differs from the number of placeholders in the prepared statement.<br/ >
+
{{Error|BXSQ0003|XQuery Errors#SQL Functions Errors}} the number of {{Code|<sql:parameter/>}} elements in {{Code|<sql:parameters/>}} differs from the number of placeholders in the prepared statement.<br/ >
<b>[[XQuery Errors#SQL Functions Errors|BXSQ0004]]</b> is raised if the type of a parameter for a prepared statement is not specified.<br/ >
+
{{Error|BXSQ0004|XQuery Errors#SQL Functions Errors}} the type of a parameter for a prepared statement is not specified.<br/ >
<b>[[XQuery Errors#SQL Functions Errors|BXSQ0005]]</b> is raised if an attribute different from <code>type</code> and <code>null</code> is set for a <code><sql:parameter/></code> element.<br/ >
+
{{Error|BXSQ0005|XQuery Errors#SQL Functions Errors}} an attribute different from {{Code|type}} and {{Code|null}} is set for a {{Code|<sql:parameter/>}} element.<br/ >
<b>[[XQuery Errors#SQL Functions Errors|BXSQ0006]]</b> is raised if a parameter is from type date, time or timestamp and its value is in an invalid format.<br/ >
+
{{Error|BXSQ0006|XQuery Errors#SQL Functions Errors}} a parameter is from type date, time or timestamp and its value is in an invalid format.<br/ >
 
|}
 
|}
  
Line 68: Line 68:
 
|-
 
|-
 
| width='90' | '''Signatures'''
 
| width='90' | '''Signatures'''
|<code><b>sql:prepare</b>($connection as xs:integer, $statement as xs:string) as xs:integer</code>
+
|{{Func|sql:prepare|$connection as xs:integer, $statement as xs:string|xs:integer}}
 
|-
 
|-
 
| '''Summary'''
 
| '''Summary'''
|This function prepares a statement and returns a handle to it. The parameter <code>$connection</code> indicates the connection handle to be used. The parameter <code>$statement</code> is a string representing an SQL statement with one or more '?' placeholders. If the value of a field has to be set to <code>NULL</code>, then the attribute <code>null</code> of the element <code><sql:parameter/></code> has to be <code>true</code>.
+
|This function prepares a statement and returns a handle to it. The parameter {{Code|$connection}} indicates the connection handle to be used. The parameter {{Code|$statement}} is a string representing an SQL statement with one or more '?' placeholders. If the value of a field has to be set to {{Code|NULL}}, then the attribute {{Code|null}} of the element {{Code|<sql:parameter/>}} has to be {{Code|true}}.
 
|-
 
|-
 
| '''Errors'''
 
| '''Errors'''
|<b>[[XQuery Errors#SQL Functions Errors|BXSQ0001]]</b> is raised if an SQL exception occurs.<br/ >
+
|{{Error|BXSQ0001|XQuery Errors#SQL Functions Errors}} an SQL exception occurs.<br/ >
<b>[[XQuery Errors#SQL Functions Errors|BXSQ0002]]</b> is raised if a wrong connection handle is passed.<br/ >
+
{{Error|BXSQ0002|XQuery Errors#SQL Functions Errors}} a wrong connection handle is passed.<br/ >
 
|}
 
|}
  
Line 82: Line 82:
 
|-
 
|-
 
| width='90' | '''Signatures'''
 
| width='90' | '''Signatures'''
|<code><b>sql:commit</b>($connection as xs:integer) as empty-sequence()</code>
+
|{{Func|sql:commit|$connection as xs:integer|empty-sequence()}}
 
|-
 
|-
 
| '''Summary'''
 
| '''Summary'''
| This function commits the changes made to a relational database. <code>$connection</code> specifies the connection handle.
+
| This function commits the changes made to a relational database. {{Code|$connection}} specifies the connection handle.
 
|-
 
|-
 
| '''Errors'''
 
| '''Errors'''
|<b>[[XQuery Errors#SQL Functions Errors|BXSQ0001]]</b> is raised if an SQL exception occurs.<br/ >
+
|{{Error|BXSQ0001|XQuery Errors#SQL Functions Errors}} an SQL exception occurs.<br/ >
<b>[[XQuery Errors#SQL Functions Errors|BXSQ0002]]</b> is raised if a wrong connection handle is passed.<br/ >
+
{{Error|BXSQ0002|XQuery Errors#SQL Functions Errors}} a wrong connection handle is passed.<br/ >
 
|}
 
|}
  
Line 96: Line 96:
 
|-
 
|-
 
| width='90' | '''Signatures'''
 
| width='90' | '''Signatures'''
|<code><b>sql:rollback</b>($connection as xs:integer) as empty-sequence()</code>
+
|{{Func|sql:rollback|$connection as xs:integer|empty-sequence()}}
 
|-
 
|-
 
| '''Summary'''
 
| '''Summary'''
| This function rolls back the changes made to a relational database. <code>$connection</code> specifies the connection handle.
+
| This function rolls back the changes made to a relational database. {{Code|$connection}} specifies the connection handle.
 
|-
 
|-
 
| '''Errors'''
 
| '''Errors'''
|<b>[[XQuery Errors#SQL Functions Errors|BXSQ0001]]</b> is raised if an SQL exception occurs.<br/ >
+
|{{Error|BXSQ0001|XQuery Errors#SQL Functions Errors}} an SQL exception occurs.<br/ >
<b>[[XQuery Errors#SQL Functions Errors|BXSQ0002]]</b> is raised if a wrong connection handle is passed.<br/ >
+
{{Error|BXSQ0002|XQuery Errors#SQL Functions Errors}} a wrong connection handle is passed.<br/ >
 
|}
 
|}
  
Line 110: Line 110:
 
|-
 
|-
 
| width='90' | '''Signatures'''
 
| width='90' | '''Signatures'''
|<code><b>sql:close</b>($connection as xs:integer) as empty-sequence()</code>
+
|{{Func|sql:close|$connection as xs:integer|empty-sequence()}}
 
|-
 
|-
 
| '''Summary'''
 
| '''Summary'''
| This function closes a connection to a relational database. <code>$connection</code> specifies the connection handle.
+
| This function closes a connection to a relational database. {{Code|$connection}} specifies the connection handle.
 
|-
 
|-
 
| '''Errors'''
 
| '''Errors'''
|<b>[[XQuery Errors#SQL Functions Errors|BXSQ0001]]</b> is raised if an SQL exception occurs.<br/ >
+
|{{Error|BXSQ0001|XQuery Errors#SQL Functions Errors}} an SQL exception occurs.<br/ >
<b>[[XQuery Errors#SQL Functions Errors|BXSQ0002]]</b> is raised if a wrong connection handle is passed.<br/ >
+
{{Error|BXSQ0002|XQuery Errors#SQL Functions Errors}} a wrong connection handle is passed.<br/ >
 
|}
 
|}
  
Line 180: Line 180:
 
! width="95%"|Description
 
! width="95%"|Description
 
|-
 
|-
|<code>BXSQ0001</code>
+
|{{Code|BXSQ0001}}
 
|An SQL exception occurred (e.g.: a non-existing relation is retrieved).
 
|An SQL exception occurred (e.g.: a non-existing relation is retrieved).
 
|-
 
|-
|<code>BXSQ0002</code>
+
|{{Code|BXSQ0002}}
 
|A wrong connection handle or prepared statement handle is passed.
 
|A wrong connection handle or prepared statement handle is passed.
 
|-
 
|-
|<code>BXSQ0003</code>
+
|{{Code|BXSQ0003}}
|The number of <code>&lt;sql:parameter/&gt;</code> elements in <code>&lt;sql:parameters/&gt;</code> differs from the number of placeholders in the prepared statement.
+
|The number of {{Code|&lt;sql:parameter/&gt;}} elements in {{Code|&lt;sql:parameters/&gt;}} differs from the number of placeholders in the prepared statement.
 
|-
 
|-
|<code>BXSQ0004</code>
+
|{{Code|BXSQ0004}}
 
|The type of a parameter for a prepared statement is not specified.
 
|The type of a parameter for a prepared statement is not specified.
 
|-
 
|-
|<code>BXSQ0005</code>
+
|{{Code|BXSQ0005}}
|An attribute different from <code>type</code> and <code>null</code> is set for a <code>&lt;sql:parameter/&gt;</code> element.
+
|An attribute different from {{Code|type}} and {{Code|null}} is set for a {{Code|&lt;sql:parameter/&gt;}} element.
 
|-
 
|-
|<code>BXSQ0006</code>
+
|{{Code|BXSQ0006}}
 
|A parameter is from type date, time or timestamp and its value is in an invalid format.
 
|A parameter is from type date, time or timestamp and its value is in an invalid format.
 
|-
 
|-
|<code>BXSQ0007</code>
+
|{{Code|BXSQ0007}}
 
|A specified database driver class is not found.
 
|A specified database driver class is not found.
 
|}
 
|}

Revision as of 16:16, 26 May 2012

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.

Conventions

All functions in this module are assigned to the http://basex.org/modules/sql namespace, which is statically bound to the sql prefix.
All errors are assigned to the http://basex.org/errors namespace, which is statically bound to the bxerr 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 BXSQ0007: the specified driver class is not found.

sql:connect

Signatures sql:connect($url as xs:string) as xs:integer
sql:connect($url as xs:string, $user as xs:string, $password as xs:string) as xs:integer
sql:connect($url as xs:string, $user as xs:string, $password as xs:string, $options as item()) as xs:integer
Summary This function establishes a connection to a relational database. As a result a connection handle is returned. 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 parameter $options can be used to set connection options, e.g. auto-commit mode. It can be specified as:
  • element(sql:options): <sql:options/> must be used as root element, and the options are specified as child nodes, with the element name representing the key and the text node representing the value:
    <sql:options>
      <sql:autocommit>true</sql:autocommit>
      ...
    </sql:options>
  • map structure: all options can be directly represented as key/value pairs:
    {{{1}}}}
Errors BXSQ0001: an SQL exception occurs, e.g. missing JDBC driver or not existing relation.

sql:execute

Once a connection is established, the returned connection handle can be used to execute queries on the database. Our SQL module supports both direct queries and prepared statements.

Signatures sql:execute($connection as xs:integer, $item as item()) as element()*
Summary This function executes a query, update or prepared statement. The parameter $id specifies either a connection handle or a prepared statement handle. The parameter $item is either a string representing an SQL statement or an element <sql:parameters/> representing the parameters for a prepared statement along with their types and values. In case of the latter, the following schema shall be used:
element sql:parameters {
 element sql:parameter {
  attribute type { "int"|"string"|"boolean"|"date"|"double"|"float"|"short"|"time"|"timestamp" },
  attribute null { "true"|"false" }?,
  text }+ }?
Errors BXSQ0001: an SQL exception occurs, e.g. not existing relation is retrieved.

BXSQ0002: a wrong connection handle or prepared statement handle is passed.
BXSQ0003: the number of <sql:parameter/> elements in <sql:parameters/> differs from the number of placeholders in the prepared statement.
BXSQ0004: the type of a parameter for a prepared statement is not specified.
BXSQ0005: an attribute different from type and null is set for a <sql:parameter/> element.
BXSQ0006: a parameter is from type date, time or timestamp and its value is in an invalid format.

sql:prepare

Signatures sql:prepare($connection as xs:integer, $statement as xs:string) as xs:integer
Summary This function prepares a statement and returns a handle to it. The parameter $connection indicates the connection handle to be used. The parameter $statement is a string representing an SQL statement with one or more '?' placeholders. If the value of a field has to be set to NULL, then the attribute null of the element <sql:parameter/> has to be true.
Errors BXSQ0001: an SQL exception occurs.

BXSQ0002: a wrong connection handle is passed.

sql:commit

Signatures sql:commit($connection as xs:integer) as empty-sequence()
Summary This function commits the changes made to a relational database. $connection specifies the connection handle.
Errors BXSQ0001: an SQL exception occurs.

BXSQ0002: a wrong connection handle is passed.

sql:rollback

Signatures sql:rollback($connection as xs:integer) as empty-sequence()
Summary This function rolls back the changes made to a relational database. $connection specifies the connection handle.
Errors BXSQ0001: an SQL exception occurs.

BXSQ0002: a wrong connection handle is passed.

sql:close

Signatures sql:close($connection as xs:integer) as empty-sequence()
Summary This function closes a connection to a relational database. $connection specifies the connection handle.
Errors BXSQ0001: an SQL exception occurs.

BXSQ0002: a wrong connection handle is passed.

Examples

Direct queries

A simple select statement can be executed on the following way:

let $conn := sql:connect("jdbc:postgresql://localhost:5432/coffeehouse")
return sql:execute($conn, "SELECT * FROM coffees WHERE price < 10")

The result will 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>
<sql:row xmlns:sql="http://basex.org/modules/sql">
  <sql:column name="cof_name">Colombian_Decaf</sql:column>
  <sql:column name="sup_id">101</sql:column>
  <sql:column name="price">8.75</sql:column>
  <sql:column name="sales">6</sql:column>
  <sql:column name="total">12</sql:column>
  <sql:column name="date">2010-10-10 13:56:11.0</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($prep, $params)

Errors

Code Description
BXSQ0001 An SQL exception occurred (e.g.: a non-existing relation is retrieved).
BXSQ0002 A wrong connection handle or prepared statement handle is passed.
BXSQ0003 The number of <sql:parameter/> elements in <sql:parameters/> differs from the number of placeholders in the prepared statement.
BXSQ0004 The type of a parameter for a prepared statement is not specified.
BXSQ0005 An attribute different from type and null is set for a <sql:parameter/> element.
BXSQ0006 A parameter is from type date, time or timestamp and its value is in an invalid format.
BXSQ0007 A specified database driver class is not found.

Changelog

The module was introduced with Version 7.0.