Difference between revisions of "Database Module"
Line 10: | Line 10: | ||
|- | |- | ||
| valign='top' | '''Summary''' | | valign='top' | '''Summary''' | ||
− | |Returns meta information on the database specified by [[#Database Argument|$db]]. | + | |Returns meta information on the database specified by <code>[[#Database Argument|$db]]</code>. |
|} | |} | ||
Line 20: | Line 20: | ||
|- | |- | ||
| valign='top' | '''Summary''' | | valign='top' | '''Summary''' | ||
− | |Returns an {{Mono|xs:string}} sequence with the names of all databases.<br/>If [[#Database Argument|$db]] is specified, all documents and raw files of the specified database are returned.<br/>The list of resources can be further restricted by the {{Mono|$path}} argument. | + | |Returns an {{Mono|xs:string}} sequence with the names of all databases.<br/>If <code>[[#Database Argument|$db]]</code> is specified, all documents and raw files of the specified database are returned.<br/>The list of resources can be further restricted by the {{Mono|$path}} argument. |
|- | |- | ||
| valign='top' | '''Examples''' | | valign='top' | '''Examples''' | ||
Line 34: | Line 34: | ||
|- | |- | ||
| valign='top' | '''Summary''' | | valign='top' | '''Summary''' | ||
− | |Returns an {{Mono|element}} sequence with the names of all databases together with their database path, the number of stored resources and the date of modification.<br/>If [[#Database Argument|$db]] is specified, all documents and raw files of the specified database together with their content-type, the modification date and the resource type are returned.<br/>The list of resources can be further restricted by the {{Mono|$path}} argument. | + | |Returns an {{Mono|element}} sequence with the names of all databases together with their database path, the number of stored resources and the date of modification.<br/>If <code>[[#Database Argument|$db]]</code> is specified, all documents and raw files of the specified database together with their content-type, the modification date and the resource type are returned.<br/>The list of resources can be further restricted by the {{Mono|$path}} argument. |
|- | |- | ||
| valign='top' | '''Examples''' | | valign='top' | '''Examples''' | ||
Line 48: | Line 48: | ||
|- | |- | ||
| valign='top' | '''Summary''' | | valign='top' | '''Summary''' | ||
− | |Returns a sequence with all document nodes contained in the database specified by [[#Database Argument|$db]].<br/>The document nodes to be returned can be restricted by the {{Mono|$path}} argument. | + | |Returns a sequence with all document nodes contained in the database specified by <code>[[#Database Argument|$db]]</code>.<br/>The document nodes to be returned can be restricted by the {{Mono|$path}} argument. |
|- | |- | ||
| valign='top' | '''Examples''' | | valign='top' | '''Examples''' | ||
Line 63: | Line 63: | ||
|- | |- | ||
| valign='top' | '''Summary''' | | valign='top' | '''Summary''' | ||
− | |Opens the database specified by [[#Database Argument|$db]] and returns the node with the specified {{Mono|$id}} value.<br />Each database node has a persistent ''id'', which remains valid after update operations. If no updates are performed, the ''pre'' value can be requested, which provides access to database nodes in constant time. | + | |Opens the database specified by <code>[[#Database Argument|$db]]</code> and returns the node with the specified {{Mono|$id}} value.<br />Each database node has a persistent ''id'', which remains valid after update operations. If no updates are performed, the ''pre'' value can be requested, which provides access to database nodes in constant time. |
|} | |} | ||
Line 73: | Line 73: | ||
|- | |- | ||
| valign='top' | '''Summary''' | | valign='top' | '''Summary''' | ||
− | |Opens the database specified by [[#Database Argument|$db]] and returns the node with the specified {{Mono|$pre}} value.<br />The ''pre'' value provides access to a database node in constant time, but it is ''transient'', i.e., it may change when database updates are performed. | + | |Opens the database specified by <code>[[#Database Argument|$db]]</code> and returns the node with the specified {{Mono|$pre}} value.<br />The ''pre'' value provides access to a database node in constant time, but it is ''transient'', i.e., it may change when database updates are performed. |
|- | |- | ||
| valign='top' | '''Errors''' | | valign='top' | '''Errors''' | ||
Line 102: | Line 102: | ||
|- | |- | ||
| valign='top' | '''Summary''' | | valign='top' | '''Summary''' | ||
− | |Returns all attribute nodes of the database specified by [[#Database Argument|$db]] that have {{Mono|$string}} as string value. If available, the value index is used to speed up evaluation.<br />If {{Mono|$attname}} is specified, the resulting attribute nodes are filtered by their attribute name. | + | |Returns all attribute nodes of the database specified by <code>[[#Database Argument|$db]]</code> that have {{Mono|$string}} as string value. If available, the value index is used to speed up evaluation.<br />If {{Mono|$attname}} is specified, the resulting attribute nodes are filtered by their attribute name. |
|- | |- | ||
| valign='top' | '''Examples''' | | valign='top' | '''Examples''' | ||
Line 118: | Line 118: | ||
|- | |- | ||
| valign='top' | '''Summary''' | | valign='top' | '''Summary''' | ||
− | |Returns all attributes of the database specified by [[#Database Argument|$db]], the string values of which are larger than or equal to {{Mono|$min}} and smaller than or equal to {{Mono|$max}}. If available, the value index is used to speed up evaluation. | + | |Returns all attributes of the database specified by <code>[[#Database Argument|$db]]</code>, the string values of which are larger than or equal to {{Mono|$min}} and smaller than or equal to {{Mono|$max}}. If available, the value index is used to speed up evaluation. |
|- | |- | ||
| valign='top' | '''Examples''' | | valign='top' | '''Examples''' | ||
Line 132: | Line 132: | ||
|- | |- | ||
| valign='top' | '''Summary''' | | valign='top' | '''Summary''' | ||
− | |Returns all text nodes from the full-text index of the database specified by [[#Database Argument|$db]] that contain the text specified as {{Mono|$terms}}.<br/>The options used for building the full-text will also be applied to the search terms. As an example, if the index terms have been stemmed, the search string will be stemmed as well. | + | |Returns all text nodes from the full-text index of the database specified by <code>[[#Database Argument|$db]]</code> that contain the text specified as {{Mono|$terms}}.<br/>The options used for building the full-text will also be applied to the search terms. As an example, if the index terms have been stemmed, the search string will be stemmed as well. |
|- | |- | ||
| valign='top' | '''Errors''' | | valign='top' | '''Errors''' | ||
Line 149: | Line 149: | ||
|- | |- | ||
| valign='top' | '''Summary''' | | valign='top' | '''Summary''' | ||
− | |Returns the ''id'' values of all {{Mono|$nodes}} of the database specified by [[#Database Argument|$db]].<br/>Each database node has a persistent ''id'', which remains valid after update operations. If no updates are performed, the ''pre'' value can be requested, which provides access to database nodes in constant time. | + | |Returns the ''id'' values of all {{Mono|$nodes}} of the database specified by <code>[[#Database Argument|$db]]</code>.<br/>Each database node has a persistent ''id'', which remains valid after update operations. If no updates are performed, the ''pre'' value can be requested, which provides access to database nodes in constant time. |
|} | |} | ||
Line 159: | Line 159: | ||
|- | |- | ||
| valign='top' | '''Summary''' | | valign='top' | '''Summary''' | ||
− | |Returns the ''pre'' values of all {{Mono|$nodes}} of the database specified by [[#Database Argument|$db]].<br/>The ''pre'' value provides access to a database node in constant time, but it is ''transient'', i.e., it may change when database updates are performed. | + | |Returns the ''pre'' values of all {{Mono|$nodes}} of the database specified by <code>[[#Database Argument|$db]]</code>.<br/>The ''pre'' value provides access to a database node in constant time, but it is ''transient'', i.e., it may change when database updates are performed. |
|- | |- | ||
| valign='top' | '''Examples''' | | valign='top' | '''Examples''' | ||
Line 174: | Line 174: | ||
|- | |- | ||
| valign='top' | '''Summary''' | | valign='top' | '''Summary''' | ||
− | |Returns a binary database resource addressed by [[#Database Argument|$db]] and {{Mono|$path}}. | + | |Returns a binary database resource addressed by <code>[[#Database Argument|$db]]</code> and {{Mono|$path}}. |
|- | |- | ||
| valign='top' | '''Errors''' | | valign='top' | '''Errors''' | ||
Line 191: | Line 191: | ||
|- | |- | ||
| valign='top' | '''Summary''' | | valign='top' | '''Summary''' | ||
− | |Returns all text nodes of the database specified by [[#Database Argument|$db]] that have {{Mono|$string}} as their string value. If available, the value index is used to speed up evaluation. | + | |Returns all text nodes of the database specified by <code>[[#Database Argument|$db]]</code> that have {{Mono|$string}} as their string value. If available, the value index is used to speed up evaluation. |
|- | |- | ||
| valign='top' | '''Examples''' | | valign='top' | '''Examples''' | ||
Line 207: | Line 207: | ||
|- | |- | ||
| valign='top' | '''Summary''' | | valign='top' | '''Summary''' | ||
− | |Returns all text nodes of the database specified by [[#Database Argument|$db]] that are located in between the {{Mono|$min}} and {{Mono|$max}} strings. If available, the value index is used to speed up evaluation. | + | |Returns all text nodes of the database specified by <code>[[#Database Argument|$db]]</code> that are located in between the {{Mono|$min}} and {{Mono|$max}} strings. If available, the value index is used to speed up evaluation. |
|- | |- | ||
| valign='top' | '''Examples''' | | valign='top' | '''Examples''' | ||
Line 226: | Line 226: | ||
|- | |- | ||
| valign='top' | '''Summary''' | | valign='top' | '''Summary''' | ||
− | |Adds documents specified by {{Mono|$input}} to the database [[#Database Argument|$db]] and the specified {{Mono|$path}}. | + | |Adds documents specified by {{Mono|$input}} to the database <code>[[#Database Argument|$db]]</code> and the specified {{Mono|$path}}. |
|- | |- | ||
| valign='top' | '''Errors''' | | valign='top' | '''Errors''' | ||
Line 246: | Line 246: | ||
|- | |- | ||
| valign='top' | '''Summary''' | | valign='top' | '''Summary''' | ||
− | |Deletes document(s), specified by {{Mono|$path}}, from the database [[#Database Argument|$db]]. | + | |Deletes document(s), specified by {{Mono|$path}}, from the database <code>[[#Database Argument|$db]]</code>. |
|- | |- | ||
| valign='top' | '''Examples''' | | valign='top' | '''Examples''' | ||
Line 261: | Line 261: | ||
|- | |- | ||
| valign='top' | '''Summary''' | | valign='top' | '''Summary''' | ||
− | |Optimizes the meta data and indexes of the database [[#Database Argument|$db]].<br/>If {{Mono|$all}} is set to {{Mono|true()}}, the complete database will be rebuilt. | + | |Optimizes the meta data and indexes of the database <code>[[#Database Argument|$db]]</code>.<br/>If {{Mono|$all}} is set to {{Mono|true()}}, the complete database will be rebuilt. |
|- | |- | ||
| valign='top' | '''Errors''' | | valign='top' | '''Errors''' | ||
Line 279: | Line 279: | ||
|- | |- | ||
| valign='top' | '''Summary''' | | valign='top' | '''Summary''' | ||
− | |Renames document(s), specified by {{Mono|$path}} to {{Mono|$newpath}} in the database [[#Database Argument|$db]]. | + | |Renames document(s), specified by {{Mono|$path}} to {{Mono|$newpath}} in the database <code>[[#Database Argument|$db]]</code>. |
|- | |- | ||
| valign='top' | '''Errors''' | | valign='top' | '''Errors''' | ||
Line 297: | Line 297: | ||
|- | |- | ||
| valign='top' | '''Summary''' | | valign='top' | '''Summary''' | ||
− | |Replaces a document, specified by {{Mono|$path}}, in the database [[#Database Argument|$db]] with the content of {{Mono|$input}}. | + | |Replaces a document, specified by {{Mono|$path}}, in the database <code>[[#Database Argument|$db]]</code> with the content of {{Mono|$input}}. |
|- | |- | ||
| valign='top' | '''Errors''' | | valign='top' | '''Errors''' | ||
Line 316: | Line 316: | ||
|- | |- | ||
| valign='top' | '''Summary''' | | valign='top' | '''Summary''' | ||
− | |Stores a binary resource specified by {{Mono|$data}} in the database specified by [[#Database Argument|$db]] and the location specified by {{Mono|$path}}. | + | |Stores a binary resource specified by {{Mono|$data}} in the database specified by <code>[[#Database Argument|$db]]</code> and the location specified by {{Mono|$path}}. |
|- | |- | ||
| valign='top' | '''Errors''' | | valign='top' | '''Errors''' | ||
Line 351: | Line 351: | ||
|- | |- | ||
| valign='top' | '''Summary''' | | valign='top' | '''Summary''' | ||
− | |Checks if the database specified by [[#Database Argument|$db]] or the resource specified by {[Mono|$path}} exists. {{Mono|false}} is returned if a database directory has been addressed. | + | |Checks if the database specified by <code>[[#Database Argument|$db]]</code> or the resource specified by {[Mono|$path}} exists. {{Mono|false}} is returned if a database directory has been addressed. |
|- | |- | ||
| valign='top' | '''Examples''' | | valign='top' | '''Examples''' | ||
Line 366: | Line 366: | ||
|- | |- | ||
| valign='top' | '''Summary''' | | valign='top' | '''Summary''' | ||
− | |Checks if the specified resource in the database [[#Database Argument|$db]] and the path {{Mono|$path}} exists, and if it is a raw file. | + | |Checks if the specified resource in the database <code>[[#Database Argument|$db]]</code> and the path {{Mono|$path}} exists, and if it is a raw file. |
|- | |- | ||
| valign='top' | '''Examples''' | | valign='top' | '''Examples''' | ||
Line 380: | Line 380: | ||
|- | |- | ||
| valign='top' | '''Summary''' | | valign='top' | '''Summary''' | ||
− | |Checks if the specified resource in the database [[#Database Argument|$db]] and the path {{Mono|$path}} exists, and if it is an XML document. | + | |Checks if the specified resource in the database <code>[[#Database Argument|$db]]</code> and the path {{Mono|$path}} exists, and if it is an XML document. |
|- | |- | ||
| valign='top' | '''Examples''' | | valign='top' | '''Examples''' | ||
Line 394: | Line 394: | ||
|- | |- | ||
| valign='top' | '''Summary''' | | valign='top' | '''Summary''' | ||
− | |Retrieves the content type of a resource in the database [[#Database Argument|$db]] and the path {{Mono|$path}}.<br/>The file extension is used to recognize the content-type of a resource stored in the database. Content-type {{Mono|application/xml}} will be returned for any XML document stored in the database, regardless of its file name extension. | + | |Retrieves the content type of a resource in the database <code>[[#Database Argument|$db]]</code> and the path {{Mono|$path}}.<br/>The file extension is used to recognize the content-type of a resource stored in the database. Content-type {{Mono|application/xml}} will be returned for any XML document stored in the database, regardless of its file name extension. |
|- | |- | ||
| valign='top' | '''Errors''' | | valign='top' | '''Errors''' |
Revision as of 16:44, 1 May 2012
This XQuery Module contains functions for handling databases from within XQuery. Existing databases can be opened and listed, index structures can be directly accessed, documents can be added to and removed from collections, etc. All functions are introduced with the db:
prefix, which is linked to the statically declared http://basex.org/modules/db
namespace.
Contents
General Functions
db:info
Signatures | db:info($db as item()) as element(Database)
|
Summary | Returns meta information on the database specified by $db .
|
db:list
Signatures | db:list() as xs:string* db:list($db as item()) as xs:string* db:list($db as item(), $path as xs:string) as xs:string*
|
Summary | Returns an xs:string sequence with the names of all databases.If $db is specified, all documents and raw files of the specified database are returned.The list of resources can be further restricted by the $path argument.
|
Examples |
|
db:list-details
Signatures | db:list-details() as element(database)* db:list-details($db as item()) as element(resource)* db:list-details($db as item(), $path as xs:string) as element(resource)*
|
Summary | Returns an element sequence with the names of all databases together with their database path, the number of stored resources and the date of modification.If $db is specified, all documents and raw files of the specified database together with their content-type, the modification date and the resource type are returned.The list of resources can be further restricted by the $path argument.
|
Examples |
|
db:open
Signatures | db:open($db as item()) as document-node()* db:open($db as item(), $path as xs:string) as document-node()*
|
Summary | Returns a sequence with all document nodes contained in the database specified by $db .The document nodes to be returned can be restricted by the $path argument.
|
Examples |
|
db:open-id
Signatures | db:open-id($db as item(), $id as xs:integer) as node()
|
Summary | Opens the database specified by $db and returns the node with the specified $id value.Each database node has a persistent id, which remains valid after update operations. If no updates are performed, the pre value can be requested, which provides access to database nodes in constant time. |
db:open-pre
Signatures | db:open-pre($db as item(), $pre as xs:integer) as node()
|
Summary | Opens the database specified by $db and returns the node with the specified $pre value.The pre value provides access to a database node in constant time, but it is transient, i.e., it may change when database updates are performed. |
Errors | BASX0004 is raised if the specified $pre value does not exist in the database.
|
Examples |
|
db:system
Signatures | db:system() as element(system)
|
Summary | Returns information on the database system, such as the database path and current database settings. |
Read Operations
db:attribute
Signatures | db:attribute($db as item(), $string as item()) as attribute()* db:attribute($db as item(), $string as item(), $attname as xs:string) as attribute()*
|
Summary | Returns all attribute nodes of the database specified by $db that have $string as string value. If available, the value index is used to speed up evaluation.If $attname is specified, the resulting attribute nodes are filtered by their attribute name.
|
Examples |
|
db:attribute-range
Signatures | db:attribute-range($db as item(), $min as xs:string, $max as xs:string) as text()* db:attribute-range($db as item(), $min as xs:string, $max as xs:string, $attname as xs:string) as attribute()*
|
Summary | Returns all attributes of the database specified by $db , the string values of which are larger than or equal to $min and smaller than or equal to $max . If available, the value index is used to speed up evaluation.
|
Examples |
|
db:fulltext
Signatures | db:fulltext($db as item(), $terms as xs:string) as text()*
|
Summary | Returns all text nodes from the full-text index of the database specified by $db that contain the text specified as $terms .The options used for building the full-text will also be applied to the search terms. As an example, if the index terms have been stemmed, the search string will be stemmed as well. |
Errors | BASX0001 is raised if the index is not available. |
Examples |
|
db:node-id
Signatures | db:node-id($nodes as node()*) as xs:integer*
|
Summary | Returns the id values of all $nodes of the database specified by $db .Each database node has a persistent id, which remains valid after update operations. If no updates are performed, the pre value can be requested, which provides access to database nodes in constant time. |
db:node-pre
Signatures | db:node-pre($nodes as node()*) as xs:integer*
|
Summary | Returns the pre values of all $nodes of the database specified by $db .The pre value provides access to a database node in constant time, but it is transient, i.e., it may change when database updates are performed. |
Examples |
|
db:retrieve
Signatures | db:retrieve($db as item(), $path as xs:string) as xs:base64Binary
|
Summary | Returns a binary database resource addressed by $db and $path .
|
Errors | FODC0002 is raised if the addressed resource is not found or cannot be retrieved. |
Examples |
|
db:text
Signatures | db:text($db as item(), $string as item()) as text()*
|
Summary | Returns all text nodes of the database specified by $db that have $string as their string value. If available, the value index is used to speed up evaluation.
|
Examples |
|
db:text-range
Signatures | db:text-range($db as item(), $min as xs:string, $max as xs:string) as text()*
|
Summary | Returns all text nodes of the database specified by $db that are located in between the $min and $max strings. If available, the value index is used to speed up evaluation.
|
Examples |
|
Updates
All functions in this section are treated as updating: they are not immediately executed, but queued on a pending update list, which is processed after the actual query has been evaluated. The XQuery Update page gives more insight into the relevant concepts.
db:add
Signatures | db:add($db as item(), $input as item()) as empty-sequence() db:add($db as item(), $input as item(), $path as xs:string) as empty-sequence()
|
Summary | Adds documents specified by $input to the database $db and the specified $path .
|
Errors | FODC0002 is raised if $input is a string representing a path, which cannot be read.FOUP0001 is raised if $input is not a string and not a document node.
|
Examples |
|
db:delete
Signatures | db:delete($db as item(), $path as xs:string) as empty-sequence()
|
Summary | Deletes document(s), specified by $path , from the database $db .
|
Examples |
|
db:optimize
Signatures | db:optimize($db as item()) as empty-sequence() db:optimize($db as item(), $all as xs:boolean) as empty-sequence()
|
Summary | Optimizes the meta data and indexes of the database $db .If $all is set to true() , the complete database will be rebuilt.
|
Errors | BASX0014 is raised if an error occurs during optimizing the data structures. BASX0015 is raised if the $all flag is set to true() , but the database is an in-memory database.BASX0016 is raised if the database $db is in use by other user(s).
|
Examples |
|
db:rename
Signatures | db:rename($db as item(), $path as xs:string, $newpath as xs:string) as empty-sequence()
|
Summary | Renames document(s), specified by $path to $newpath in the database $db .
|
Errors | BASX0013 is raised if new document name(s) will be empty. |
Examples |
|
db:replace
Signatures | db:replace($db as item(), $path as xs:string, $input as item()) as empty-sequence()
|
Summary | Replaces a document, specified by $path , in the database $db with the content of $input .
|
Errors | BASX0012 is raised if $path is not a single document path.FODC0002 is raised if $input is a string representing a path, which cannot be read.FOUP0001 is raised if $input is not a string and not a document node.
|
Examples |
|
db:store
Signatures | db:store($db as item(), $path as xs:string, $data as item()) as empty-sequence()
|
Summary | Stores a binary resource specified by $data in the database specified by $db and the location specified by $path .
|
Errors | FOUP0002 is raised if the resource cannot be stored at the specified location. |
Examples |
|
db:output
Signatures | db:output($data as item()*) as empty-sequence()
|
Summary | This function can be used to both perform updates and return results in a single query. The argument of the function will be evaluated, and the resulting value will be cached and returned after the updates on the pending update list have been processed. The function can only be used together with updating expressions; if the function is called within a transform expression, its results will be discarded. |
Examples |
|
Helper Functions
db:exists
Signatures | db:exists($db as item()) as xs:boolean db:exists($db as item(), $path as xs:string) as xs:boolean
|
Summary | Checks if the database specified by $db or the resource specified by {[Mono|$path}} exists. false is returned if a database directory has been addressed.
|
Examples |
|
db:is-raw
Signatures | db:is-raw($db as item(), $path as xs:string) as xs:boolean
|
Summary | Checks if the specified resource in the database $db and the path $path exists, and if it is a raw file.
|
Examples |
|
db:is-xml
Signatures | db:is-xml($db as item(), $path as xs:string) as xs:boolean
|
Summary | Checks if the specified resource in the database $db and the path $path exists, and if it is an XML document.
|
Examples |
|
db:content-type
Signatures | db:content-type($db as item(), $path as xs:string) as xs:string
|
Summary | Retrieves the content type of a resource in the database $db and the path $path .The file extension is used to recognize the content-type of a resource stored in the database. Content-type application/xml will be returned for any XML document stored in the database, regardless of its file name extension.
|
Errors | FODC0002 is raised if the addressed resource is not found or cannot be retrieved. |
Examples |
|
db:event
Signatures | db:event($name as xs:string, $query as item()) as empty-sequence()
|
Summary | Executes a $query and sends the resulting value to all clients watching the Event with the specified $name . The query may also perform updates; no event will be sent to the client that fired the event.
|
Errors | BASX0009 is raised if the specified event is unknown. SEPM0016 is raised if serialization errors occurred while sending the value. |
Database Argument
Many functions share $db
as argument, which may either reference a string, denoting the name of the addressed database, or a single node from an already opened database. The following errors may be raised by these functions:
- BASX0002 is raised if
$db
references a node that is not stored in a database (i.e., references a main-memory XML fragment). - BASX0003 is raised if the addressed database cannot be opened.
The argument may also contain a BaseX-specific database fragment. All XML fragments can be converted to database fragments by calling the transform expression:
copy $c := element hello { 'world' } modify () return db:text($c, 'world')
Changelog
Version 7.2.1
- Added: db:text-range, db:attribute-range, db:output
Version 7.1
- Added: db:list-details, db:content-type
- Updated: db:info, db:system, db:retrieve