Difference between revisions of "SQL Module"

From BaseX Documentation
Jump to navigation Jump to search
m
Line 1: Line 1:
 
The SQL Module contains [[Querying#Functions|XQuery 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 attributes representing the columns returned by the SQL statement. All functions dealing with access to relational databases use the <code>sql</code> prefix, which is linked to the <code><nowiki>http://www.basex.org/sql</nowiki></code> namespace. The module will be officially supported with the upcoming <font color="red">Version 6.8</font> of BaseX.
 
The SQL Module contains [[Querying#Functions|XQuery 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 attributes representing the columns returned by the SQL statement. All functions dealing with access to relational databases use the <code>sql</code> prefix, which is linked to the <code><nowiki>http://www.basex.org/sql</nowiki></code> namespace. The module will be officially supported with the upcoming <font color="red">Version 6.8</font> of BaseX.
  
==Connections==
+
==sql:connect==
A connection to a relational database can be established using the function <code>sql:connect</code>. As a result a connection handle is returned. The possible signatures are:
+
{|
 +
|-
 +
| valign='top' width='90' | '''Signatures'''
 +
|<code><b>sql:connect</b>($url as xs:string) as xs:int</code><br/ ><code><b>sql:connect</b>($url as xs:string, $auto-commit as xs:boolean) as xs:int</code><br/ ><code><b>sql:connect</b>($url as xs:string, $auto-commit as xs:boolean, $user as xs:string, $password as xs:string) as xs:int</code><br/ >
 +
|-
 +
| valign='top' | '''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>. The parameter <code>$auto-commit</code> is used to indicate if the auto-commit mode shall be activated or not. Its default value is true. If the parameters <code>$user</code> and <code>$password</code> are specified, they are used as credentials for connecting to the database.<br/ >
 +
|-
 +
| valign='top' | '''Errors'''
 +
|<b>[[XQuery Errors#SQL Functions Errors (XSQL)|XSQL0001]]</b> is raised if an SQL exception occurs, e.g. missing JDBC driver or not existing relation.
 +
|}
  
<code><b>sql:connect</b>($url as xs:string) as xs:int</code><br/>
+
==sql:execute==
<code><b>sql:connect</b>($url as xs:string, $auto-commit as xs:boolean) as xs:int</code><br/>
+
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.
<code><b>sql:connect</b>($url as xs:string, $auto-commit as xs:boolean, $user as xs:string, $password as xs:string) as xs:int</code>
 
  
The parameter <code>$url</code> is the URL of the database and shall be of the form: <code>jdbc:<driver name>:[//<server>[/<database>]</code>. The parameter <code>$auto-commit</code> is used to indicate if the auto-commit mode shall be activated or not. Its default value is true. If the parameters <code>$user</code> and <code>$password</code> are specified, they are used as credentials for connecting to the database.
+
{|
 +
|-
 +
| valign='top' width='90' | '''Signatures'''
 +
|<code><b>sql:execute</b>($connection as xs:int, $item as xs:item) as element()*</code>
 +
|-
 +
| valign='top' | '''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/ >
 +
<pre class="brush:xml">
 +
element sql:parameters {
 +
element sql:parameter {
 +
  attribute type { "int"|"string"|"boolean"|"date"|"double"|"float"|"short"|"time"|"timestamp" },
 +
  attribute null { "true"|"false" }?,
 +
  text }+ }?
 +
</pre>
 +
|-
 +
| valign='top' | '''Errors'''
 +
|<b>[[XQuery Errors#SQL Functions Errors (XSQL)|XSQL0001]]</b> is raised if an SQL exception occurs, e.g. not existing relation is retrieved.<br/ >
 +
<b>[[XQuery Errors#SQL Functions Errors (XSQL)|XSQL0002]]</b> is raised if a wrong connection handle or prepared statement handle is passed.<br/ >
 +
<b>[[XQuery Errors#SQL Functions Errors (XSQL)|XSQL0003]]</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/ >
 +
<b>[[XQuery Errors#SQL Functions Errors (XSQL)|XSQL0004]]</b> is raised if the type of a parameter for a prepared statement is not specified.<br/ >
 +
<b>[[XQuery Errors#SQL Functions Errors (XSQL)|XSQL0005]]</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/ >
 +
<b>[[XQuery Errors#SQL Functions Errors (XSQL)|XSQL0006]]</b> is raised if a parameter is from type date, time or timestamp and its value is in an invalid format.<br/ >
 +
|}
  
==Executing Queries==
+
==sql:prepare==
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.
+
{|
 +
|-
 +
| valign='top' width='90' | '''Signatures'''
 +
|<code><b>sql:prepare</b>($connection as xs:int, $statement as xs:string) as xs:int</code>
 +
|-
 +
| valign='top' | '''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>.
 +
|-
 +
| valign='top' | '''Errors'''
 +
|<b>[[XQuery Errors#SQL Functions Errors (XSQL)|XSQL0001]]</b> is raised if an SQL exception occurs.<br/ >
 +
<b>[[XQuery Errors#SQL Functions Errors (XSQL)|XSQL0002]]</b> is raised if a wrong connection handle is passed.<br/ >
 +
|}
 +
 
 +
==sql:commit==
 +
{|
 +
|-
 +
| valign='top' width='90' | '''Signatures'''
 +
|<code><b>sql:commit</b>($connection as xs:int) as xs:element?</code>
 +
|-
 +
| valign='top' | '''Summary'''
 +
| This function commits the changes made to a relational database. <code>$connection</code> specifies the connection handle.
 +
|-
 +
| valign='top' | '''Errors'''
 +
|<b>[[XQuery Errors#SQL Functions Errors (XSQL)|XSQL0001]]</b> is raised if an SQL exception occurs.<br/ >
 +
<b>[[XQuery Errors#SQL Functions Errors (XSQL)|XSQL0002]]</b> is raised if a wrong connection handle is passed.<br/ >
 +
|}
 +
 
 +
==sql:rollback==
 +
{|
 +
|-
 +
| valign='top' width='90' | '''Signatures'''
 +
|<code><b>sql:rollback</b>($connection as xs:int) as xs:element?</code>
 +
|-
 +
| valign='top' | '''Summary'''
 +
| This function rolls back the changes made to a relational database. <code>$connection</code> specifies the connection handle.
 +
|-
 +
| valign='top' | '''Errors'''
 +
|<b>[[XQuery Errors#SQL Functions Errors (XSQL)|XSQL0001]]</b> is raised if an SQL exception occurs.<br/ >
 +
<b>[[XQuery Errors#SQL Functions Errors (XSQL)|XSQL0002]]</b> is raised if a wrong connection handle is passed.<br/ >
 +
|}
  
===Direct queries===
+
==sql:close==
A query or an update statement can be executed using the function <code>sql:execute</code>:
+
{|
 +
|-
 +
| valign='top' width='90' | '''Signatures'''
 +
|<code><b>sql:close</b>($connection as xs:int) as xs:element?</code>
 +
|-
 +
| valign='top' | '''Summary'''
 +
| This function closes a connection to a relational database. <code>$connection</code> specifies the connection handle.
 +
|-
 +
| valign='top' | '''Errors'''
 +
|<b>[[XQuery Errors#SQL Functions Errors (XSQL)|XSQL0001]]</b> is raised if an SQL exception occurs.<br/ >
 +
<b>[[XQuery Errors#SQL Functions Errors (XSQL)|XSQL0002]]</b> is raised if a wrong connection handle is passed.<br/ >
 +
|}
  
<code><b>sql:execute</b>($connection as xs:int, $statement as xs:string) as element()*</code>
+
==Examples==
  
The parameter <code>$connection</code> specifies the connection handle to be used and the parameter <code>$statement</code> - the statement to be executed. If a query statement is executed, the result set is returned as a sequence of <code><sql:tuple/></code> elements, each one representing a tuple. In case of an update statement an empty sequence is returned. For example, a simple select statement can be executed on the following way:
+
===Direct queries===
 +
A simple select statement can be executed on the following way:
  
 
<pre class="brush:xquery">
 
<pre class="brush:xquery">
Line 34: Line 116:
  
 
===Prepared Statements===
 
===Prepared Statements===
In order to execute a prepared statement, first a prepared statement handle has to be created. This can be done with the function <code>sql:prepare</code>:
+
A prepared select statement can be executed in the following way:
 
 
<code><b>sql:prepare</b>($connection as xs:int, $statement as xs:string) as xs:int</code>
 
 
 
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. The prepared statement can then be executed using the following signature of the <code>sql:execute</code> function:
 
 
 
<code><b>sql:execute</b>($prepared-statement as xs:int, $parameters as xs:element) as element()*</code>
 
 
 
The parameter <code>$prepared-statement</code> is the handle to the prepared statement created via <code>sql:prepare</code>. <code>$parameters</code> specifies the parameters of the prepared statement and must contain exactly the same number of <code><parameter/></code> elements as the number of placeholders in the statement. It shall follow the schema:
 
 
 
<pre class="brush:xml">
 
element sql:parameters {
 
element sql:parameter {
 
  attribute type { "int"|"string"|"boolean"|"date"|"double"|"float"|"short"|"time"|"timestamp" },
 
  attribute null { "true"|"false" }?,
 
  text }+ }?
 
</pre>
 
 
 
For example, a prepared select statement can be executed in the following way:
 
  
 
<pre class="brush:xml">
 
<pre class="brush:xml">
 +
(: Establish a connection :)
 
let $conn := sql:connect("jdbc:postgresql://localhost:5432/coffeehouse")
 
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 = ?")
 
let $prep := sql:prepare($conn, "SELECT * FROM coffees WHERE price < ? AND cof_name = ?")
 +
(: Values and types of prepared statement parameters :)
 
let $params := <sql:parameters>
 
let $params := <sql:parameters>
 
                 <sql:parameter type='double'>10</sql:parameter>
 
                 <sql:parameter type='double'>10</sql:parameter>
 
                 <sql:parameter type='string'>French_Roast</sql:parameter>
 
                 <sql:parameter type='string'>French_Roast</sql:parameter>
 
               </sql:parameters>
 
               </sql:parameters>
 +
(: Execute prepared statement :)
 
return sql:execute($prep, $params)
 
return sql:execute($prep, $params)
 
</pre>
 
</pre>
 
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>.
 
 
==Commit==
 
Changes made to a relational database using a connection handle <code>$connection</code> can be committed using the function <code>sql:commit</code>:
 
 
<code><b>sql:commit</b>($connection as xs:int)</code>
 
 
==Rollback==
 
Changes made to a relational database using a connection handle <code>$connection</code> can be rolled back using the function <code>sql:rollback</code>:
 
 
<code><b>sql:rollback</b>($connection as xs:int)</code>
 
 
==Closing a connection==
 
A connection to a relational database can be closed using the <code>sql:close</code> function:
 
 
<code><b>sql:close</b>($connection as xs:int)</code>
 

Revision as of 14:28, 6 September 2011

The SQL Module contains XQuery 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 attributes representing the columns returned by the SQL statement. All functions dealing with access to relational databases use the sql prefix, which is linked to the http://www.basex.org/sql namespace. The module will be officially supported with the upcoming Version 6.8 of BaseX.

sql:connect

Signatures sql:connect($url as xs:string) as xs:int
sql:connect($url as xs:string, $auto-commit as xs:boolean) as xs:int
sql:connect($url as xs:string, $auto-commit as xs:boolean, $user as xs:string, $password as xs:string) as xs:int
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>. The parameter $auto-commit is used to indicate if the auto-commit mode shall be activated or not. Its default value is true. If the parameters $user and $password are specified, they are used as credentials for connecting to the database.
Errors XSQL0001 is raised if 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:int, $item as xs: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 XSQL0001 is raised if an SQL exception occurs, e.g. not existing relation is retrieved.

XSQL0002 is raised if a wrong connection handle or prepared statement handle is passed.
XSQL0003 is raised if the number of <sql:parameter/> elements in <sql:parameters/> differs from the number of placeholders in the prepared statement.
XSQL0004 is raised if the type of a parameter for a prepared statement is not specified.
XSQL0005 is raised if an attribute different from type and null is set for a <sql:parameter/> element.
XSQL0006 is raised if 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:int, $statement as xs:string) as xs:int
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 XSQL0001 is raised if an SQL exception occurs.

XSQL0002 is raised if a wrong connection handle is passed.

sql:commit

Signatures sql:commit($connection as xs:int) as xs:element?
Summary This function commits the changes made to a relational database. $connection specifies the connection handle.
Errors XSQL0001 is raised if an SQL exception occurs.

XSQL0002 is raised if a wrong connection handle is passed.

sql:rollback

Signatures sql:rollback($connection as xs:int) as xs:element?
Summary This function rolls back the changes made to a relational database. $connection specifies the connection handle.
Errors XSQL0001 is raised if an SQL exception occurs.

XSQL0002 is raised if a wrong connection handle is passed.

sql:close

Signatures sql:close($connection as xs:int) as xs:element?
Summary This function closes a connection to a relational database. $connection specifies the connection handle.
Errors XSQL0001 is raised if an SQL exception occurs.

XSQL0002 is raised if 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:tuple xmlns:sql="http://www.basex.org/sql" cof_name="French_Roast" sup_id="49" price="9.5" sales="15" total="30"/>
<sql:tuple xmlns:sql="http://www.basex.org/sql" cof_name="French_Roast_Decaf" sup_id="49" price="7.5" sales="10" total="14"/>
<sql:tuple xmlns:sql="http://www.basex.org/sql" cof_name="Colombian_Decaf" sup_id="101" price="8.75" sales="6" total="12" date="2010-10-10 13:56:11.0"/>

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)