Changes

Jump to navigation Jump to search
418 bytes added ,  16:45, 10 August 2022
=Conventions=
 
{{Mark|Updated with Version 9.0}}:
All functions and errors in this module are assigned to the <code><nowiki>http://basex.org/modules/sql</nowiki></code> namespace, which is statically bound to the {{Code|sql}} prefix.<br/>
{| width='100%'
|-valign="top"
| width='120' | '''Signatures'''
|{{Func|sql:init|$class as xs:string|empty-sequence()}}
|-valign="top"
| '''Summary'''
|This function initializes a JDBC driver specified via {{Code|$class}}. This step might be superfluous if the SQL database is not embedded.<br/ >
|-valign="top"
| '''Errors'''
|{{Error|init|XQuery Errors#SQL Functions Errors}} the specified driver is not found.
|}
{| width='100%'
|-valign="top"
| width='120' | '''Signatures'''
|{{Func|sql:connect|$url as xs:string|xs:anyURI}}<br/ >{{Func|sql:connect|$url as xs:string, $user username as xs:string, $password as xs:string|xs:anyURI}}<br/ >{{Func|sql:connect|$url as xs:string, $user username as xs:string, $password as xs:string, $options as map(xs:string, item()*)?|xs:anyURI}}<br/ >|-valign="top"
| '''Summary'''
|This function establishes a connection to a relational database and returns a connection id. 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|$userusername}} and {{Code|$password}} are specified, they are used as credentials for connecting to the database. The {{Code|$options}} parameter can be used to set connection options.|-valign="top"
| '''Errors'''
|{{Error|error|XQuery Errors#SQL Functions Errors}} an SQL exception occurs, e.g. missing JDBC driver or not existing relationoccurred when connecting to the database.|-valign="top"
| '''Examples'''
|Connects to an SQL Server and sets autocommit to {{Code|true}}:
<pre classsyntaxhighlight lang="brush:xquery">
sql:connect('dbc:sqlserver://DBServer', map { 'autocommit': true() })
</presyntaxhighlight>
|}
==sql:execute==
 
{{Mark|Updated with Version 9.0}}: Return update count for updating statements.
{| width='100%'
|-valign="top"
| width='120' | '''Signatures'''
|{{Func|sql:execute|$id as xs:anyURI, $statement as xs:string|item()*}}<br/>{{Func|sql:execute|$id as xs:anyURI, $statement as xs:string, $options as map(*)?|item()*}}|-valign="top"
| '''Summary'''
| This function executes an SQL {{Code|$statement}}, using the connection with the specified {{Code|$id}}. The returned result depends on the kind of statement:
* If an update statement was executed, the number of updated rows will be returned as integer.
* Otherwise, an XML representation of all results will be returned.
With {{Code|$options}}, the following parameter can be set:* {{Code|timeout}}: query execution will be interrupted after the specified number of seconds.|-valign="top"
| '''Errors'''
|{{Error|error|XQuery Errors#SQL Functions Errors}} an error occurred while executing SQL exception occurs, e.g. not existing relation is retrieved.<br/ >{{Error|id|XQuery Errors#SQL Functions Errors}} the specified connection does not exist.<br/>{{Error|timeout|#Errors}} query execution exceeded timeout.<br/ >
|}
==sql:execute-prepared==
 
{{Mark|Updated with Version 9.0}}: Return update count for updating statements.
{| width='100%'
|-valign="top"
| width='120' | '''Signatures'''
|{{Func|sql:execute-prepared|$id as xs:anyURI, $params as element(sql:parameters)|item()*}}<br/>{{Func|sql:execute-prepared|$id as xs:anyURI, $params as element(sql:parameters), $options as map(*)?|item()*}}|-valign="top"
| '''Summary'''
| This function executes a prepared statement:* with the specified {{Code|$id}}. The output format is identical to [[#sql:execute{{Function||sql:execute]].* The parameter {{Code|$id}} refers to the prepared statement.* The optional parameter {{Code|$params}} is an element {{Code|<sql:parameters/>}} representing the parameters for a prepared statement along with their types and values. The following schema shall be used:<br/ ><pre classsyntaxhighlight lang="brush:xquery">
element sql:parameters {
element sql:parameter {
attribute type { "intbigdecimal" | "stringboolean" | "booleanbyte" | "date" | "double" | "float" | "floatint" | "long" | "short" | "timesqlxml" | "string" | "timestamptime" | "sqlxmltimestamp" },
attribute null { "true" | "false" }?,
text
}+
}?
</presyntaxhighlight>With {{Code|$options}}, the following parameter can be set:* {{Code|timeout}}: query execution will be interrupted after the specified number of seconds.|-valign="top"
| '''Errors'''
|{{Error|attribute|#Errors}} an attribute different from {{Code|type}} and {{Code|null}} is set for a {{Code|<sql:parameter/>}} element.<br/ >{{Error|error|XQuery Errors#SQL Functions Errors}} an error occurred while executing SQL exception occurs, e.g. not existing relation is retrieved.<br/ >{{Error|id|XQuery Errors#SQL Functions Errors}} the specified connection does not exist.<br/ >{{Error|parameters|XQuery Errors#SQL Functions Errors}} wrong number of {{Code|<sql:parameter/>}} elements, or no parameter type is not specified.<br/>{{Error|attributetimeout|XQuery Errors#SQL Functions Errors}} an attribute different from {{Code|type}} and {{Code|null}} is set for a {{Code|<sql:parameter/>}} elementquery execution exceeded timeout.<br/ >{{Error|type|XQuery Errors#SQL Functions Errors}} the value of a parameter is from type date, time or timestamp and its value is in an invalid cannot be converted to the specified format.<br/ >
|}
{| width='100%'
|-valign="top"
| width='120' | '''Signatures'''
|{{Func|sql:prepare|$id as xs:anyURI, $statement as xs:string|xs:anyURI}}
|-valign="top"
| '''Summary'''
|This function prepares an SQL {{Code|$statement}}, using the specified connection {{Code|$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 {{Code|NULL}}, then the attribute {{Code|null}} of the {{Code|<sql:parameter/>}} element must be {{Code|true}}.
|-valign="top"
| '''Errors'''
|{{Error|error|XQuery Errors#SQL Functions Errors}} an error occurred while executing SQL exception occurs.<br/ >{{Error|id|XQuery Errors#SQL Functions Errors}} the specified connection does not exist.<br/ >
|}
{| width='100%'
|-valign="top"
| width='120' | '''Signatures'''
|{{Func|sql:commit|$id as xs:anyURI|empty-sequence()}}
|-valign="top"
| '''Summary'''
| This function commits the changes made to a relational database, using the specified connection {{Code|$id}}.
|-valign="top"
| '''Errors'''
|{{Error|error|XQuery Errors#SQL Functions Errors}} an error occurred while executing SQL exception occurs.<br/ >{{Error|id|XQuery Errors#SQL Functions Errors}} the specified connection does not exist.<br/ >
|}
{| width='100%'
|-valign="top"
| width='120' | '''Signatures'''
|{{Func|sql:rollback|$id as xs:anyURI|empty-sequence()}}
|-valign="top"
| '''Summary'''
| This function rolls back the changes made to a relational database, using the specified connection {{Code|$id}}.
|-valign="top"
| '''Errors'''
|{{Error|error|XQuery Errors#SQL Functions Errors}} an error occurred while executing SQL exception occurs.<br/ >{{Error|id|XQuery Errors#SQL Functions Errors}} the specified connection does not exist.<br/ >
|}
{| width='100%'
|-valign="top"
| width='120' | '''Signatures'''
|{{Func|sql:close|$id as xs:anyURI|empty-sequence()}}
|-valign="top"
| '''Summary'''
| This function closes a database connection with the specified {{Code|$id}}.<br/>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.
|-valign="top"
| '''Errors'''
|{{Error|error|XQuery Errors#SQL Functions Errors}} an error occurred while executing SQL exception occurs.<br/ >{{Error|id|XQuery Errors#SQL Functions Errors}} the specified connection does not exist.<br/ >
|}
A simple select statement can be executed as follows:
<pre classsyntaxhighlight lang="brush:xquery">
let $id := sql:connect("jdbc:postgresql://localhost:5432/coffeehouse")
return sql:execute($id, "SELECT * FROM coffees WHERE price < 10")
</presyntaxhighlight>
The result may look like:
<pre classsyntaxhighlight lang="brush:xml">
<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">93.5</sql:column>
<sql:column name="sales">15</sql:column>
<sql:column name="total">30</sql:column>
<sql:column name="total">14</sql:column>
</sql:row>
</presyntaxhighlight>
==Prepared Statements==
A prepared select statement can be executed in the following way:
<pre classsyntaxhighlight lang="brush:xquery">
(: Establish a connection :)
let $conn := sql:connect("jdbc:postgresql://localhost:5432/coffeehouse")
(: Execute prepared statement :)
return sql:execute-prepared($prep, $params)
</presyntaxhighlight>
==SQLite==
The following expression demonstrates how SQLite can be addressed using with the [httphttps://bitbucket.org/xerial/sqlite-jdbc / Xerial SQLite JDBC driver]:
<pre classsyntaxhighlight lang="brush:xquery">
(: Initialize driver :)
sql:init("org.sqlite.JDBC"),
sql:execute($conn, "select * from person")
)
</presyntaxhighlight>
=Errors=
 
{{Mark|Updated with Version 9.0}}:
{| class="wikitable" width="100%"
! width="110"|Code
|Description
|-valign="top"
|{{Code|attribute}}
|An attribute different from {{Code|type}} and {{Code|null}} is set for a {{Code|&lt;sql:parameter/&gt;}} element.
|-valign="top"
|{{Code|error}}
|An SQL exception occurred.
|-valign="top"
|{{Code|id}}
|A connection does not exist.
|-valign="top"
|{{Code|init}}
|A database driver is not found.
|-valign="top"
|{{Code|parameters}}
|Wrong number of No parameter type specified.|- valign="top"|{{Code|&lt;sql:parameter/&gt;timeout}} elements, or parameter type is not specified|Query execution exceeded timeout.|-valign="top"
|{{Code|type}}
|A The value of a parameter is from type date, time or timestamp and its value is in an invalid cannot be converted to the specified format.
|}
=Changelog=
 
;Version 9.6
* Updated: {{Function||sql:execute-prepared}}, additional types added
;Version 9.0
* Updated: [[#sql:execute{{Function||sql:execute]]}}, [[#sql:execute-prepared{{Function||sql:execute-prepared]]}}: Return update count for updating statements. {{Code|$options}} argument added.
* Updated: Connection ids are URIs now.
* Updated: error codes updatesupdated; errors now use the module namespace
;Version 7.5
* Updated: prepared statements are now executed via [[#sql:execute-prepared{{Function||sql:execute-prepared]]}}
The module was introduced with Version 7.0.
Bureaucrats, editor, reviewer, Administrators
13,550

edits

Navigation menu