Changes

Jump to navigation Jump to search
718 bytes removed ,  16:25, 21 November 2017
no edit summary
=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/>All errors are assigned to the <code><nowiki>http://basex.org/errors</nowiki></code> namespace, which is statically bound to the {{Code|bxerr}} prefix.
=Functions=
|-
| '''Errors'''
|{{Error|BXSQ0007init|XQuery Errors#SQL Functions Errors}} the specified driver class is not found.
|}
|-
| width='120' | '''Signatures'''
|{{Func|sql:connect|$url as xs:string|xs:integeranyURI}}<br/ >{{Func|sql:connect|$url as xs:string, $user as xs:string, $password as xs:string|xs:integeranyURI}}<br/ >{{Func|sql:connect|$url as xs:string, $user as xs:string, $password as xs:string, $options as map(xs:string, item())|xs:integeranyURI}}<br/ >
|-
| '''Summary'''
|This function establishes a connection to a relational database. As a result and returns a connection handle is returnedid. 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 {{Code|$options}} parameter can be used to set connection options.
|-
| '''Errors'''
|{{Error|BXSQ0001error|XQuery Errors#SQL Functions Errors}} an SQL exception occurs, e.g. missing JDBC driver or not existing relation.
|-
| '''Examples'''
{{Mark|Updated with Version 9.0}}: Return update count for updating statements.
 
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.
{| width='100%'
|-
| width='120' | '''Signatures'''
|{{Func|sql:execute|$connection id as xs:integeranyURI, $query statement as xs:string|item()*}}
|-
| '''Summary'''
| This function executes a query or update statement. The parameter an SQL {{Code|$connectionstatement}} specifies a , using the connection handle. The parameter with the specified {{Code|$queryid}} is a string representing an SQL statement. The returned result depends on the kind of statement:* If the result is updatingan update statement was executed, the number of updated rows will be returned as integer.
* Otherwise, an XML representation of all results will be returned.
|-
| '''Errors'''
|{{Error|BXSQ0001error|XQuery Errors#SQL Functions Errors}} an SQL exception occurs, e.g. not existing relation is retrieved.<br/ >{{Error|BXSQ0002id|XQuery Errors#SQL Functions Errors}} a wrong the specified connection handle is passeddoes not exist.<br/ >
|}
|-
| width='120' | '''Signatures'''
|{{Func|sql:execute-prepared|$id as xs:integeranyURI, $params as element(sql:parameters)|item()*}}
|-
| '''Summary'''
| This function executes a prepared statement:
* The output format is identical to [[#sql:execute|sql:execute]].
* The parameter {{Code|$id}} specifies a refers to the prepared statement handle.
* 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 class="brush:xquery">
|-
| '''Errors'''
|{{Error|BXSQ0001error|XQuery Errors#SQL Functions Errors}} an SQL exception occurs, e.g. not existing relation is retrieved.<br/ >{{Error|BXSQ0002id|XQuery Errors#SQL Functions Errors}} a wrong prepared statement handle is passedthe specified connection does not exist.<br/ >{{Error|BXSQ0003parameters|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/ >{{Error|BXSQ0004|XQuery Errors#SQL Functions Errors}} the type of a parameter for a prepared statement is not specified.<br/ >{{Error|BXSQ0005attribute|XQuery Errors#SQL Functions Errors}} an attribute different from {{Code|type}} and {{Code|null}} is set for a {{Code|<sql:parameter/>}} element.<br/ >{{Error|BXSQ0006type|XQuery Errors#SQL Functions Errors}} a parameter is from type date, time or timestamp and its value is in an invalid format.<br/ >
|}
|-
| width='120' | '''Signatures'''
|{{Func|sql:prepare|$connection id as xs:integeranyURI, $statement as xs:string|xs:integeranyURI}}
|-
| '''Summary'''
|This function prepares a statement and returns a handle to it. The parameter an SQL {{Code|$connectionstatement}} indicates , using the specified connection handle to be used. The parameter {{Code|$statementid}} , and returns the id reference to this statement. The 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 element must be {{Code|true}}.
|-
| '''Errors'''
|{{Error|BXSQ0001error|XQuery Errors#SQL Functions Errors}} an SQL exception occurs.<br/ >{{Error|BXSQ0002id|XQuery Errors#SQL Functions Errors}} a wrong the specified connection handle is passeddoes not exist.<br/ >
|}
|-
| width='120' | '''Signatures'''
|{{Func|sql:commit|$connection id as xs:integeranyURI|empty-sequence()}}
|-
| '''Summary'''
| This function commits the changes made to a relational database. , using the specified connection {{Code|$connectionid}} specifies the connection handle.
|-
| '''Errors'''
|{{Error|BXSQ0001error|XQuery Errors#SQL Functions Errors}} an SQL exception occurs.<br/ >{{Error|BXSQ0002id|XQuery Errors#SQL Functions Errors}} a wrong the specified connection handle is passeddoes not exist.<br/ >
|}
|-
| width='120' | '''Signatures'''
|{{Func|sql:rollback|$connection id as xs:integeranyURI|empty-sequence()}}
|-
| '''Summary'''
| This function rolls back the changes made to a relational database. , using the specified connection {{Code|$connectionid}} specifies the connection handle.
|-
| '''Errors'''
|{{Error|BXSQ0001error|XQuery Errors#SQL Functions Errors}} an SQL exception occurs.<br/ >{{Error|BXSQ0002id|XQuery Errors#SQL Functions Errors}} a wrong the specified connection handle is passeddoes not exist.<br/ >
|}
|-
| width='120' | '''Signatures'''
|{{Func|sql:close|$connection id as xs:integeranyURI|empty-sequence()}}
|-
| '''Summary'''
| This function closes a database connection to a relational database. with the specified {{Code|$connectionid}} specifies the connection handle.<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 explicitly close them with this function if you open many connectionsthat are not used anymore.
|-
| '''Errors'''
|{{Error|BXSQ0001error|XQuery Errors#SQL Functions Errors}} an SQL exception occurs.<br/ >{{Error|BXSQ0002id|XQuery Errors#SQL Functions Errors}} a wrong the specified connection handle is passeddoes not exist.<br/ >
|}
<pre class="brush:xquery">
let $conn id := sql:connect("jdbc:postgresql://localhost:5432/coffeehouse")return sql:execute($connid, "SELECT * FROM coffees WHERE price < 10")
</pre>
=Errors=
 
{{Mark|Updated with Version 9.0}}:
{| class="wikitable" width="100%"
|Description
|-
|{{Code|BXSQ0001attribute}}|An SQL exception occurred (e.g.attribute different from {{Code|type}} and {{Code|null}} is set for a {{Code|&lt;sql: a non-existing relation is retrieved)parameter/&gt;}} element.
|-
|{{Code|BXSQ0002error}}|A wrong connection handle or prepared statement handle is passedAn SQL exception occurred.
|-
|{{Code|BXSQ0003init}}|The number of {{Code|&lt;sql:parameter/&gt;}} elements in {{Code|&lt;sql:parameters/&gt;}} differs from the number of placeholders in the prepared statementA database driver is not found.
|-
|{{Code|BXSQ0004id}}|The type of a parameter for a prepared statement is A connection does not specifiedexist.
|-
|{{Code|BXSQ0005parameters}}|An attribute different from {{Code|type}} and {{Code|null}} is set for a Wrong number of {{Code|&lt;sql:parameter/&gt;}} elementelements, or parameter type is not specified.  
|-
|{{Code|BXSQ0006type}}
|A parameter is from type date, time or timestamp and its value is in an invalid format.
|-
|{{Code|BXSQ0007}}
|A specified database driver class is not found.
|}
;Version 9.0
* Updated: [[#sql:execute|sql:execute]], [[#sql:execute-prepared|sql:execute-prepared]]: Return update count for updating statements.
* Updated: Connection ids are URIs now.
* Updated: error codes updates; errors now use the module namespace
;Version 7.5
Bureaucrats, editor, reviewer, Administrators
13,550

edits

Navigation menu