Difference between revisions of "SQL Module"

From BaseX Documentation
Jump to navigation Jump to search
m
Line 118: Line 118:
 
A prepared select statement can be executed in the following way:
 
A prepared select statement can be executed in the following way:
  
<pre class="brush:xml">
+
<pre class="brush:xquery">
 
(: Establish a connection :)
 
(: Establish a connection :)
 
let $conn := sql:connect("jdbc:postgresql://localhost:5432/coffeehouse")
 
let $conn := sql:connect("jdbc:postgresql://localhost:5432/coffeehouse")

Revision as of 21:29, 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)