This module contains [[Querying#Functions|XQuery 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 <code>{{Mono|db:</code> }} prefix, which is linked to the <code>{{Mono|http://www.basex.org/db</code> }} namespace.
A [[Database Functions (Snapshot)|preview page]] contains all functions offered by the [http://files.basex.org/releases/latest/ latest stable snapshot].
|-
| valign='top' width='90' | '''Signatures'''
|<code>{{Mono|<b>db:info</b>($db as item()) as xs:string</code>}}<br/><code>{{Mono|<b>db:info</b>($db as item(), $type as xs:string) as xs:string</code>}}
|-
| valign='top' | '''Summary'''
|Returns information for a database.<br />If <code>{{Mono|$type</code> }} is specified, the function returns information on a database index. It must be one of the values <code>{{Mono|TEXT</code>}}, <code>{{Mono|ATTRIBUTE</code>}}, <code>{{Mono|FULLTEXT</code>}}, <code>{{Mono|PATH</code>}}, <code>{{Mono|TAG</code>}}, or <code>{{Mono|ATTNAME</code>}}.
|-
| valign='top' | '''Errors'''
| valign='top' | '''Examples'''
|
* <code>{{Mono|db:info("DB", "FULLTEXT")</code> }} returns information about the full-text index in the database <code>{{Mono|DB</code>}}.
|}
|-
| valign='top' width='90' | '''Signatures'''
|<code>{{Mono|<b>db:list</b>() as xs:string*</code>}}<br/><code>{{Mono|<b>db:list</b>($db as xs:string) as xs:string*</code>}}<br/><code>{{Mono|<b>db:list</b>($db as xs:string, $path as xs:string) as xs:string*</code>}}
|-
| valign='top' | '''Summary'''
|Returns an <code>{{Mono|xs:string</code> }} sequence with the names of all databases. If <code>{{Mono|$db</code> }} is specified, all documents and raw files of the specified database are returned. The list of resources can be further restricted by the <code>{{Mono|$path</code> }} argument.<br/><font color='orangered'>{{Mark|Modified in Version 7.0</font>}}: database and resource path are now specified as two separate arguments.
|}
|-
| valign='top' width='90' | '''Signatures'''
|<code>{{Mono|<b>db:open</b>($db as xs:string) as document-node()*</code>}}<br /><code>{{Mono|<b>db:open</b>($db as xs:string, $path as xs:string) as document-node()*</code>}}
|-
| valign='top' | '''Summary'''
|Returns a sequence with all document nodes contained in the database specified by <code>{{Mono|$db</code>}}. The returned documents may be filtered by the <code>{{Mono|$path</code> }} argument.<br/><font color='orangered'>Modified in Version 7.0</font>: database and resource path are now specified as two separate arguments.
|-
| valign='top' | '''Errors'''
| valign='top' | '''Examples'''
|
* <code>{{Mono|db:open("docs")</code> }} returns all documents from the database named <code>{{Mono|docs</code>}}.* <code>{{Mono|db:open("docs", "one")</code> }} returns all documents from the database named <code>{{Mono|docs</code> }} in the subpath <code>{{Mono|one</code>}}.
|}
|-
| valign='top' width='90' | '''Signatures'''
|<code>{{Mono|<b>db:open-id</b>($db as item(), $id as xs:integer) as node()</code>}}
|-
| valign='top' | '''Summary'''
|Opens the database specified by <code>{{Mono|$db</code> }} and returns the node with the specified <code>{{Mono|$id</code> }} value.<br />In contrast to the <i>pre</i> value, the <i>id</i> will remain valid after update operations.
|-
| valign='top' | '''Errors'''
|<b>[[XQuery Errors#BaseX Errors|BASX0004]]</b> is raised if the specified <code>{{Mono|$id</code> }} does not exist in the database.
|}
|-
| valign='top' width='90' | '''Signatures'''
|<code>{{Mono|<b>db:open-pre</b>($db as item(), $pre as xs:integer) as node()</code>}}
|-
| valign='top' | '''Summary'''
|Opens the database specified by <code>{{Mono|$db</code> }} and returns the node with the specified <code>{{Mono|$pre</code> }} value.
|-
| valign='top' | '''Errors'''
|<b>[[XQuery Errors#BaseX Errors|BASX0004]]</b> is raised if the specified <code>{{Mono|$pre</code> }} value does not exist in the database.
|-
| valign='top' | '''Examples'''
|
* <code>{{Mono|db:open-pre("docs", 0)</code> }} returns the first database node from the database named <code>{{Mono|docs</code>}}.
|}
|-
| valign='top' width='90' | '''Signatures'''
|<code>{{Mono|<b>db:system</b>() as xs:string</code>}}
|-
| valign='top' | '''Summary'''
|-
| valign='top' width='90' | '''Signatures'''
|<code>{{Mono|<b>db:attribute</b>($db as item(), $string as item()) as attribute()*</code>}}<br/><code>{{Mono|<b>db:attribute</b>($db as item(), $string as item(), $attname as xs:string) as attribute()*</code>}}
|-
| valign='top' | '''Summary'''
|Returns all attribute nodes that have <code>{{Mono|$string</code> }} as their string value.<br />If <code>{{Mono|$attname</code> }} is specified, the resulting attribute nodes are filtered by their attribute name.
|-
| valign='top' | '''Examples'''
|
* <code>{{Mono|db:attribute("DB", "QUERY", "id")/..</code> }} returns the parents of all <code>{{Mono|id</code> }} attribute nodes of the database <code>{{Mono|DB</code> }} that have <code>{{Mono|QUERY</code> }} as string value.
|}
|-
| valign='top' width='90' | '''Signatures'''
|<code>{{Mono|<b>db:fulltext</b>($db as item(), $text as item()) as text()*</code>}}
|-
| valign='top' | '''Summary'''
|Returns all text nodes from the full-text index that contain the string <code>{{Mono|$text</code>}}. The index full-text options are used for searching, i.e., if the index terms have been stemmed, the search string will be stemmed as well.
|-
| valign='top' | '''Errors'''
| valign='top' | '''Examples'''
|
* <code>{{Mono|db:fulltext("DB", "QUERY")</code> }} returns all text nodes of the database <code>{{Mono|DB</code> }} that contain the string <code>{{Mono|QUERY</code>}}.
|}
|-
| valign='top' width='90' | '''Signatures'''
|<code>{{Mono|<b>db:node-id</b>($nodes as node()*) as xs:integer*</code>}}
|-
| valign='top' | '''Summary'''
|Returns the <i>id</i> values of all database nodes specified by <code>{{Mono|$nodes</code>}}. <i>id</i> values are pointers to database nodes, which are not changed by updates.
|}
|-
| valign='top' width='90' | '''Signatures'''
|<code>{{Mono|<b>db:node-pre</b>($nodes as node()*) as xs:integer*</code>}}
|-
| valign='top' | '''Summary'''
|Returns the <i>pre</i> values of all database nodes specified by <code>{{Mono|$nodes</code>}}. <i>pre</i> values are direct, internal pointers to database nodes, which might be changed by updates.
|-
| valign='top' | '''Examples'''
|
* <code>{{Mono|db:node-pre(doc("input"))</code> }} returns <code>{{Mono|0</code> }} if the database <code>{{Mono|input</code> }} contains a single document.
|}
|-
| valign='top' width='90' | '''Signatures'''
|<code>{{Mono|<b>db:retrieve</b>($db as xs:string, $path as xs:string) as xs:raw</code>}}
|-
| valign='top' | '''Summary'''
|Retrieves the binary resource specified by <code>{{Mono|$path</code> }} in the (implementation specific) <code>{{Mono|xs:raw</code> }} format.
|-
| valign='top' | '''Errors'''
| valign='top' | '''Examples'''
|
* <code>{{Mono|declare option output:method 'raw';<br/>db:retrieve("DB", "music/01.mp3")</code> }} returns the specified audio file as raw data.
|}
|-
| valign='top' width='90' | '''Signatures'''
|<code>{{Mono|<b>db:text</b>($db as item(), $string as item()) as text()*</code>}}
|-
| valign='top' | '''Summary'''
|Returns all text nodes that have <code>{{Mono|$string</code> }} as their string value.
|-
| valign='top' | '''Examples'''
|
* <code>{{Mono|db:text("DB", "QUERY")/..</code> }} returns the parents of all text nodes of the database <code>{{Mono|DB</code> }} that match the string <code>{{Mono|QUERY</code>}}.
|}
|-
| valign='top' width='90' | '''Signatures'''
|<code>{{Mono|<b>db:add</b>($db as item(), $input as item()) as empty-sequence()</code>}}<br/><code>{{Mono|<b>db:add</b>($db as item(), $input as item(), $path as xs:string) as empty-sequence()</code>}}
|-
| valign='top' | '''Summary'''
|Add documents specified by <code>{{Mono|$input</code> }} to the database <code>{{Mono|$db</code> }} and the specified <code>{{Mono|$path</code>}}.
|-
| valign='top' | '''Errors'''
|<b>[[XQuery Errors#BaseX Errors|BASX0002]]</b> is raised if <code>{{Mono|$db</code> }} is not a database node.<br/><b>[[XQuery Errors#BaseX Errors|BASX0003]]</b> is raised if <code>{{Mono|$db</code> }} is a string and a database with that name cannot be opened.<br/><b>[[XQuery Errors#Functions Errors|FODC0002]]</b> is raised if <code>{{Mono|$input</code> }} is a string representing a path, which cannot be read.<br/><b>[[XQuery Errors#Update Errors|FOUP0001]]</b> is raised if <code>{{Mono|$input</code> }} is not a string and not a document node.
|-
| valign='top' | '''Examples'''
|
* <code>{{Mono|db:add("DB", "/home/dir/doc.xml")</code> }} adds the file <code>{{Mono|/home/dir/doc.xml</code> }} to the database <code>{{Mono|DB</code>}}.* <code>{{Mono|db:add("DB", "<a/>", "doc.xml")</code> }} adds a document with content <code>{{Mono|<a/></code> }} to the database <code>{{Mono|DB</code> }} under the name <code>{{Mono|doc.xml</code>}}.* <code>{{Mono|db:add("DB", document { <a/> }, "doc.xml")</code> }} adds the document node to the database <code>{{Mono|DB</code> }} under the name <code>{{Mono|doc.xml</code>}}.* <code>{{Mono|db:add("DB", "/home/dir", "docs/dir")</code> }} adds all documents in <code>{{Mono|/home/dir</code> }} to the database <code>{{Mono|DB</code> }} under the path <code>{{Mono|docs/dir</code>}}.
|}
|-
| valign='top' width='90' | '''Signatures'''
|<code>{{Mono|<b>db:delete</b>($db as item(), $path as xs:string) as empty-sequence()</code>}}
|-
| valign='top' | '''Summary'''
|Delete document(s), specified by <code>{{Mono|$path</code> }} in the database <code>{{Mono|$db</code>}}.
|-
| valign='top' | '''Errors'''
|<b>[[XQuery Errors#BaseX Errors|BASX0002]]</b> is raised if <code>{{Mono|$db</code> }} is not a database node.<br/><b>[[XQuery Errors#BaseX Errors|BASX0003]]</b> is raised if <code>{{Mono|$db</code> }} is a string and a database with that name cannot be opened.
|-
| valign='top' | '''Examples'''
|
* <code>{{Mono|db:delete("DB", "docs/dir/doc.xml")</code> }} deletes the document <code>{{Mono|docs/dir/doc.xml</code> }} in the database <code>{{Mono|DB</code>}}.* <code>{{Mono|db:delete("DB", "docs/dir", "<a/>")</code> }} deletes all documents with paths beginning with <code>{{Mono|docs/dir</code> }} in the database <code>{{Mono|DB</code>}}.
|}
|-
| valign='top' width='90' | '''Signatures'''
|<code>{{Mono|<b>db:optimize</b>($db as xs:string) as empty-sequence()</code>}}<br/><code>{{Mono|<b>db:optimize</b>($db as xs:string, $all as xs:boolean) as empty-sequence()</code>}}
|-
| valign='top' | '''Summary'''
|Optimize the data structures of the database <code>{{Mono|$db</code>}}. If <code>{{Mono|$all</code> }} is set to <code>{{Mono|true()</code>}}, then the whole database will be rebuilt.
|-
| valign='top' | '''Errors'''
|<b>[[XQuery Errors#BaseX Errors|BASX0002]]</b> is raised if <code>{{Mono|$db</code> }} is not a database node.<br/><b>[[XQuery Errors#BaseX Errors|BASX0003]]</b> is raised if <code>{{Mono|$db</code> }} is a string and a database with that name cannot be opened.<br/><b>[[XQuery Errors#BaseX Errors|BASX0014]]</b> is raised if an error occurs during optimizing the data structures.<br/><b>[[XQuery Errors#BaseX Errors|BASX0015]]</b> is raised if the <code>{{Mono|$all</code> }} flag is set to <code>{{Mono|true()</code>}}, but the database is an in-memory database.<br/><b>[[XQuery Errors#BaseX Errors|BASX0016]]</b> is raised if the database <code>{{Mono|$db</code> }} is in use by other user(s).
|-
| valign='top' | '''Examples'''
|
* <code>{{Mono|db:optimize("DB")</code> }} optimizes the database structures of the database <code>{{Mono|DB</code>}}.* <code>{{Mono|db:optimize("DB", true())</code> }} optimizes all database structures of the database <code>{{Mono|DB</code>}}.
|}
|-
| valign='top' width='90' | '''Signatures'''
|<code>{{Mono|<b>db:rename</b>($db as item(), $path as xs:string, $newpath as xs:string) as empty-sequence()</code>}}
|-
| valign='top' | '''Summary'''
|Rename document(s), specified by <code>{{Mono|$path</code> }} to <code>{{Mono|$newpath</code> }} in the database <code>{{Mono|$db</code>}}.
|-
| valign='top' | '''Errors'''
|<b>[[XQuery Errors#BaseX Errors|BASX0002]]</b> is raised if <code>{{Mono|$db</code> }} is not a database node.<br/><b>[[XQuery Errors#BaseX Errors|BASX0003]]</b> is raised if <code>{{Mono|$db</code> }} is a string and a database with that name cannot be opened.<br/><b>[[XQuery Errors#BaseX Errors|BASX0013]]</b> is raised if new document name(s) will be empty.
|-
| valign='top' | '''Examples'''
|
* <code>{{Mono|db:rename("DB", "docs/dir/doc.xml", "docs/dir/newdoc.xml")</code> }} renames the document <code>{{Mono|docs/dir/doc.xml</code> }} to <code>{{Mono|docs/dir/newdoc.xml</code> }} in the database <code>{{Mono|DB</code>}}.* <code>{{Mono|db:rename("DB", "docs/dir", "docs/newdir")</code> }} renames all documents with paths beginning with <code>{{Mono|docs/dir</code> }} to paths beginning with <code>{{Mono|docs/newdir</code> }} in the database <code>{{Mono|DB</code>}}.
|}
|-
| valign='top' width='90' | '''Signatures'''
|<code>{{Mono|<b>db:replace</b>($db as item(), $path as xs:string, $input as item()) as empty-sequence()</code>}}
|-
| valign='top' | '''Summary'''
|Replace a document, specified by <code>{{Mono|$path</code> }} in the database <code>{{Mono|$db</code> }} with the content of <code>{{Mono|$input</code>}}.
|-
| valign='top' | '''Errors'''
|<b>[[XQuery Errors#BaseX Errors|BASX0002]]</b> is raised if <code>{{Mono|$db</code> }} is not a database node.<br/><b>[[XQuery Errors#BaseX Errors|BASX0003]]</b> is raised if <code>{{Mono|$db</code> }} is a string and a database with that name cannot be opened.<br/><b>[[XQuery Errors#BaseX Errors|BASX0012]]</b> is raised if <code>{{Mono|$path</code> }} is not a single document path.<br/><b>[[XQuery Errors#Functions Errors|FODC0002]]</b> is raised if <code>{{Mono|$input</code> }} is a string representing a path, which cannot be read.<br/><b>[[XQuery Errors#Update Errors|FOUP0001]]</b> is raised if <code>{{Mono|$input</code> }} is not a string and not a document node.
|-
| valign='top' | '''Examples'''
|
* <code>{{Mono|db:replace("DB", "docs/dir/doc.xml", "/home/dir/doc.xml")</code> }} replaces the content of the document <code>{{Mono|docs/dir/doc.xml</code> }} in the database <code>{{Mono|DB</code> }} with the content of the file <code>{{Mono|/home/dir/doc.xml</code>}}.* <code>{{Mono|db:replace("DB", "docs/dir/doc.xml", "<a/>")</code> }} replaces the content of the document <code>{{Mono|docs/dir/doc.xml</code> }} in the database <code>{{Mono|DB</code> }} with <code>{{Mono|<a/></code>}}.* <code>{{Mono|db:replace("DB", "docs/dir/doc.xml", document { <a/> })</code> }} replaces the content of the document <code>{{Mono|docs/dir/doc.xml</code> }} in the database <code>{{Mono|DB</code> }} with the specified document node.
|}
|-
| valign='top' width='90' | '''Signatures'''
|<code>{{Mono|<b>db:store</b>($db as xs:string, $path as xs:string, $data as item()) as empty-sequence()</code>}}
|-
| valign='top' | '''Summary'''
|Stores a binary resource specified by <code>{{Mono|$data</code> }} at the location specified by <code>{{Mono|$path</code>}}.
|-
| valign='top' | '''Errors'''
| valign='top' | '''Examples'''
|
* <code>{{Mono|db:store("DB", "video/sample.mov", file:read-binary('video.mov'))</code> }} stores the addressed video file at the specified location.
|}
|-
| valign='top' width='90' | '''Signatures'''
|<code>{{Mono|<b>db:content-type</b>($db as xs:string, $path as xs:string) as xs:string</code>}}
|-
| valign='top' | '''Summary'''
|Retrieves the content type of the resource specified by <code>{{Mono|$path</code>}}. Currently, the implementation uses the file extension to recognize the content-type of a resource stored in the database (this mechanism is provided by Java). Content-type <code>{{Mono|application/xml</code> }} will be returned for any XML document stored in the database, regardless of its file name extension.
|-
| valign='top' | '''Errors'''
| valign='top' | '''Examples'''
|
* <code>{{Mono|db:content-type("DB", "docs/doc01.pdf")</code> }} returns <code>{{Mono|application/pdf</code>}}.* <code>{{Mono|db:content-type("DB", "docs/doc01.xml")</code> }} returns <code>{{Mono|application/xml</code>}}.* <code>{{Mono|db:content-type("DB", "docs/doc01")</code> }} returns <code>{{Mono|application/xml</code>}}, if <code>{{Mono|db:is-xml("DB", "docs/doc01")</code> }} returns <code>{{Mono|true</code>}}.
|}
|-
| valign='top' width='90' | '''Signatures'''
|<code>{{Mono|<b>db:event</b>($name as xs:string, $query as item()) as empty-sequence()</code>}}
|-
| valign='top' | '''Summary'''
|Executes a <code>{{Mono|$query</code> }} and sends the resulting value to all clients watching the [[Events|Event]] with the specified <code>{{Mono|$name</code>}}. No event will be sent to the client that fired the event.
|-
| valign='top' | '''Errors'''
|-
| valign='top' width='90' | '''Signatures'''
|<code>{{Mono|<b>db:exists</b>($db as xs:string) as xs:boolean</code>}}<br/><code>{{Mono|<b>db:exists</b>($db as xs:string, $path as xs:string) as xs:boolean</code>}}
|-
| valign='top' | '''Summary'''
|Checks if the specified database or resource exists. Returns <code>{{Mono|false</code> }} if a database directory is specified.
|-
| valign='top' | '''Examples'''
|
* <code>{{Mono|db:exists("DB")</code> }} returns <code>{{Mono|true</code> }} if the database <code>{{Mono|DB</code> }} exists.* <code>{{Mono|db:exists("DB", "resource")</code> }} returns <code>{{Mono|true</code> }} if <code>{{Mono|resource</code> }} is an XML document or a raw file.
|}
|-
| valign='top' width='90' | '''Signatures'''
|<code>{{Mono|<b>db:is-raw</b>($db as xs:string, $path as xs:string) as xs:boolean</code>}}
|-
| valign='top' | '''Summary'''
| valign='top' | '''Examples'''
|
* <code>{{Mono|db:is-raw("DB", "music/01.mp3")</code> }} returns <code>{{Mono|true</code>}}.
|}
|-
| valign='top' width='90' | '''Signatures'''
|<code>{{Mono|<b>db:is-xml</b>($db as xs:string, $path as xs:string) as xs:boolean</code>}}
|-
| valign='top' | '''Summary'''
| valign='top' | '''Examples'''
|
* <code>{{Mono|db:is-xml("DB", "dir/doc.xml")</code> }} returns <code>{{Mono|true</code>}}.
|}
[[Category:XQuery]]