Difference between revisions of "Database Module"
Jump to navigation
Jump to search
Line 3: | Line 3: | ||
A [[Database Functions (Snapshot)|preview page]] contains all functions offered by the [http://files.basex.org/releases/latest/ latest stable snapshot]. | A [[Database Functions (Snapshot)|preview page]] contains all functions offered by the [http://files.basex.org/releases/latest/ latest stable snapshot]. | ||
− | ==db: | + | =General Functions= |
+ | |||
+ | ==db:info== | ||
{| | {| | ||
|- | |- | ||
| valign='top' width='90' | '''Signatures''' | | valign='top' width='90' | '''Signatures''' | ||
− | |<code><b>db: | + | |<code><b>db:info</b>($db as item()) as xs:string</code><br/><code><b>db:info</b>($db as item(), $type as xs:string) as xs:string</code> |
|- | |- | ||
| valign='top' | '''Summary''' | | valign='top' | '''Summary''' | ||
− | | | + | |Returns information for a database.<br />If <code>$type</code> is specified, the function returns information on a database index. It must be one of the values <code>TEXT</code>, <code>ATTRIBUTE</code>, <code>FULLTEXT</code>, <code>PATH</code>, <code>TAG</code>, or <code>ATTNAME</code>. |
+ | |- | ||
+ | | valign='top' | '''Errors''' | ||
+ | |<b>[[XQuery Errors#BaseX Errors|BASX0001]]</b> is raised if the specified index is not available. | ||
+ | |- | ||
+ | | valign='top' | '''Examples''' | ||
+ | | | ||
+ | * <code>db:info("DB", "FULLTEXT")</code> returns information about the full-text index in the database <code>DB</code>. | ||
|} | |} | ||
Line 17: | Line 26: | ||
|- | |- | ||
| valign='top' width='90' | '''Signatures''' | | valign='top' width='90' | '''Signatures''' | ||
− | |<code><b>db:list</b>() as xs:string*</code><br/><code><b>db:list</b>($path as xs:string) as xs:string*</code> | + | |<code><b>db:list</b>() as xs:string*</code><br/><code><b>db:list</b>($db as xs:string) as xs:string*</code><br/><code><b>db:list</b>($db as xs:string, $path as xs:string) as xs:string*</code> |
|- | |- | ||
| valign='top' | '''Summary''' | | valign='top' | '''Summary''' | ||
− | |Returns an <code>xs:string</code> sequence with the names of all databases. If | + | |Returns an <code>xs:string</code> sequence with the names of all databases. If <code>$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>$path</code> argument.<br/><font color='orangered'>Modified in Version 7.0</font>: database and resource path are now specified as two separate arguments. |
|} | |} | ||
Line 27: | Line 36: | ||
|- | |- | ||
| valign='top' width='90' | '''Signatures''' | | valign='top' width='90' | '''Signatures''' | ||
− | |<code><b>db:open</b>($ | + | |<code><b>db:open</b>($db as xs:string) as document-node()*</code><br /><code><b>db:open</b>($db as xs:string, $path as xs:string) as document-node()*</code> |
|- | |- | ||
| valign='top' | '''Summary''' | | valign='top' | '''Summary''' | ||
− | |Returns a sequence with all document nodes contained in the database specified by <code>$ | + | |Returns a sequence with all document nodes contained in the database specified by <code>$db</code>. The returned documents may be filtered by the <code>$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' | '''Errors''' | ||
Line 38: | Line 47: | ||
| | | | ||
* <code>db:open("docs")</code> returns all documents from the database named <code>docs</code>. | * <code>db:open("docs")</code> returns all documents from the database named <code>docs</code>. | ||
− | * <code>db:open("docs | + | * <code>db:open("docs", "one")</code> returns all documents from the database named <code>docs</code> in the subpath <code>one</code>. |
+ | |} | ||
+ | |||
+ | ==db:open-id== | ||
+ | {| | ||
+ | |- | ||
+ | | valign='top' width='90' | '''Signatures''' | ||
+ | |<code><b>db:open-id</b>($db as item(), $id as xs:integer) as node()</code> | ||
+ | |- | ||
+ | | valign='top' | '''Summary''' | ||
+ | |Opens the database specified by <code>$db</code> and returns the node with the specified <code>$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>$id</code> does not exist in the database. | ||
|} | |} | ||
Line 48: | Line 70: | ||
|- | |- | ||
| valign='top' | '''Summary''' | | valign='top' | '''Summary''' | ||
− | |Opens the database specified by <code>$ | + | |Opens the database specified by <code>$db</code> and returns the node with the specified <code>$pre</code> value. |
|- | |- | ||
| valign='top' | '''Errors''' | | valign='top' | '''Errors''' | ||
Line 58: | Line 80: | ||
|} | |} | ||
− | ==db: | + | ==db:system== |
+ | {| | ||
+ | |- | ||
+ | | valign='top' width='90' | '''Signatures''' | ||
+ | |<code><b>db:system</b>() as xs:string</code> | ||
+ | |- | ||
+ | | valign='top' | '''Summary''' | ||
+ | |Prints information about the database system, including the database path and current database settings. | ||
+ | |} | ||
+ | |||
+ | =Read Operations= | ||
+ | |||
+ | ==db:attribute== | ||
+ | {| | ||
+ | |- | ||
+ | | valign='top' width='90' | '''Signatures''' | ||
+ | |<code><b>db:attribute</b>($db as item(), $string as item()) as attribute()*</code><br/><code><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>$string</code> as their string value.<br />If <code>$attname</code> is specified, the resulting attribute nodes are filtered by their attribute name. | ||
+ | |- | ||
+ | | valign='top' | '''Examples''' | ||
+ | | | ||
+ | * <code>db:attribute("DB", "QUERY", "id")/..</code> returns the parents of all <code>id</code> attribute nodes of the database <code>DB</code> that have <code>QUERY</code> as string value. | ||
+ | |} | ||
+ | |||
+ | ==db:fulltext== | ||
{| | {| | ||
|- | |- | ||
| valign='top' width='90' | '''Signatures''' | | valign='top' width='90' | '''Signatures''' | ||
− | |<code><b>db: | + | |<code><b>db:fulltext</b>($db as item(), $text as item()) as text()*</code> |
|- | |- | ||
| valign='top' | '''Summary''' | | valign='top' | '''Summary''' | ||
− | | | + | |Returns all text nodes from the full-text index that contain the string <code>$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' | '''Errors''' | ||
− | |<b>[[XQuery Errors#BaseX Errors| | + | |<b>[[XQuery Errors#BaseX Errors|BASX0001]]</b> is raised if the index is not available. |
+ | |- | ||
+ | | valign='top' | '''Examples''' | ||
+ | | | ||
+ | * <code>db:fulltext("DB", "QUERY")</code> returns all text nodes of the database <code>DB</code> that contain the string <code>QUERY</code>. | ||
+ | |} | ||
+ | |||
+ | ==db:node-id== | ||
+ | {| | ||
+ | |- | ||
+ | | valign='top' width='90' | '''Signatures''' | ||
+ | |<code><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>$nodes</code>. <i>id</i> values are pointers to database nodes, which are not changed by updates. | ||
|} | |} | ||
Line 85: | Line 147: | ||
|} | |} | ||
− | ==db: | + | ==db:retrieve== |
+ | |||
+ | <font color='orangered'>Introduced with Version 7.0:</font> | ||
{| | {| | ||
|- | |- | ||
| valign='top' width='90' | '''Signatures''' | | valign='top' width='90' | '''Signatures''' | ||
− | |<code><b>db: | + | |<code><b>db:retrieve</b>($db as xs:string, $path as xs:string) as xs:raw</code> |
|- | |- | ||
| valign='top' | '''Summary''' | | valign='top' | '''Summary''' | ||
− | | | + | |Retrieves the binary resource specified by <code>$path</code> in the (implementation specific) <code>xs:raw</code> format. |
+ | |- | ||
+ | | valign='top' | '''Errors''' | ||
+ | |<b>[[XQuery Errors#Functions Errors|FODC0002]]</b> is raised if the addressed resource is not found or cannot be retrieved. | ||
+ | |- | ||
+ | | valign='top' | '''Examples''' | ||
+ | | | ||
+ | * <code>declare option output:method 'raw';<br/>db:retrieve("DB", "music/01.mp3")</code> returns the specified audio file as raw data. | ||
|} | |} | ||
Line 109: | Line 180: | ||
|} | |} | ||
− | ==db: | + | =Updates= |
+ | |||
+ | ==db:add== | ||
+ | |||
+ | <font color='orangered'>Modified in Version 7.0:</font> | ||
+ | |||
{| | {| | ||
|- | |- | ||
| valign='top' width='90' | '''Signatures''' | | valign='top' width='90' | '''Signatures''' | ||
− | |<code><b>db: | + | |<code><b>db:add</b>($db as item(), $input as item()) as empty-sequence()</code><br/><code><b>db:add</b>($db as item(), $input as item(), $path as xs:string) as empty-sequence()</code> |
|- | |- | ||
| valign='top' | '''Summary''' | | valign='top' | '''Summary''' | ||
− | | | + | |Add documents specified by <code>$input</code> to the database <code>$db</code> and the specified <code>$path</code>. |
+ | |- | ||
+ | | valign='top' | '''Errors''' | ||
+ | |<b>[[XQuery Errors#BaseX Errors|BASX0002]]</b> is raised if <code>$db</code> is not a database node.<br/><b>[[XQuery Errors#BaseX Errors|BASX0003]]</b> is raised if <code>$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>$input</code> is a string representing a path, which cannot be read.<br/><b>[[XQuery Errors#Update Errors|FOUP0001]]</b> is raised if <code>$input</code> is not a string and not a document node. | ||
|- | |- | ||
| valign='top' | '''Examples''' | | valign='top' | '''Examples''' | ||
| | | | ||
− | * <code>db: | + | * <code>db:add("DB", "/home/dir/doc.xml")</code> adds the file <code>/home/dir/doc.xml</code> to the database <code>DB</code>. |
+ | * <code>db:add("DB", "<a/>", "doc.xml")</code> adds a document with content <code><a/></code> to the database <code>DB</code> under the name <code>doc.xml</code>. | ||
+ | * <code>db:add("DB", document { <a/> }, "doc.xml")</code> adds the document node to the database <code>DB</code> under the name <code>doc.xml</code>. | ||
+ | * <code>db:add("DB", "/home/dir", "docs/dir")</code> adds all documents in <code>/home/dir</code> to the database <code>DB</code> under the path <code>docs/dir</code>. | ||
|} | |} | ||
− | ==db: | + | ==db:delete== |
{| | {| | ||
|- | |- | ||
| valign='top' width='90' | '''Signatures''' | | valign='top' width='90' | '''Signatures''' | ||
− | |<code><b>db: | + | |<code><b>db:delete</b>($db as item(), $path as xs:string) as empty-sequence()</code> |
|- | |- | ||
| valign='top' | '''Summary''' | | valign='top' | '''Summary''' | ||
− | | | + | |Delete document(s), specified by <code>$path</code> in the database <code>$db</code>. |
|- | |- | ||
| valign='top' | '''Errors''' | | valign='top' | '''Errors''' | ||
− | |<b>[[XQuery Errors#BaseX Errors| | + | |<b>[[XQuery Errors#BaseX Errors|BASX0002]]</b> is raised if <code>$db</code> is not a database node.<br/><b>[[XQuery Errors#BaseX Errors|BASX0003]]</b> is raised if <code>$db</code> is a string and a database with that name cannot be opened. |
|- | |- | ||
| valign='top' | '''Examples''' | | valign='top' | '''Examples''' | ||
| | | | ||
− | * <code>db: | + | * <code>db:delete("DB", "docs/dir/doc.xml")</code> deletes the document <code>docs/dir/doc.xml</code> in the database <code>DB</code>. |
+ | * <code>db:delete("DB", "docs/dir", "<a/>")</code> deletes all documents with paths beginning with <code>docs/dir</code> in the database <code>DB</code>. | ||
|} | |} | ||
− | ==db: | + | ==db:optimize== |
{| | {| | ||
|- | |- | ||
| valign='top' width='90' | '''Signatures''' | | valign='top' width='90' | '''Signatures''' | ||
− | |<code><b>db: | + | |<code><b>db:optimize</b>($db as xs:string) as empty-sequence()</code><br/><code><b>db:optimize</b>($db as xs:string, $all as xs:boolean) as empty-sequence()</code> |
|- | |- | ||
| valign='top' | '''Summary''' | | valign='top' | '''Summary''' | ||
− | | | + | |Optimize the data structures of the database <code>$db</code>. If <code>$all</code> is set to <code>true()</code>, then the whole database will be rebuilt. |
|- | |- | ||
| valign='top' | '''Errors''' | | valign='top' | '''Errors''' | ||
− | |<b>[[XQuery Errors#BaseX Errors| | + | |<b>[[XQuery Errors#BaseX Errors|BASX0002]]</b> is raised if <code>$db</code> is not a database node.<br/><b>[[XQuery Errors#BaseX Errors|BASX0003]]</b> is raised if <code>$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>$all</code> flag is set to <code>true()</code>, but the database is an in-memory database.<br/><b>[[XQuery Errors#BaseX Errors|BASX0016]]</b> is raised if the database <code>$db</code> is in use by other user(s). |
|- | |- | ||
| valign='top' | '''Examples''' | | valign='top' | '''Examples''' | ||
| | | | ||
− | * <code>db: | + | * <code>db:optimize("DB")</code> optimizes the database structures of the database <code>DB</code>. |
+ | * <code>db:optimize("DB", true())</code> optimizes all database structures of the database <code>DB</code>. | ||
|} | |} | ||
− | ==db: | + | ==db:rename== |
{| | {| | ||
|- | |- | ||
| valign='top' width='90' | '''Signatures''' | | valign='top' width='90' | '''Signatures''' | ||
− | |<code><b>db: | + | |<code><b>db:rename</b>($db as item(), $path as xs:string, $newpath as xs:string) as empty-sequence()</code> |
|- | |- | ||
| valign='top' | '''Summary''' | | valign='top' | '''Summary''' | ||
− | | | + | |Rename document(s), specified by <code>$path</code> to <code>$newpath</code> in the database <code>$db</code>. |
|- | |- | ||
| valign='top' | '''Errors''' | | valign='top' | '''Errors''' | ||
− | |<b>[[XQuery Errors#BaseX Errors|BASX0002]]</b> is raised if <code>$db</code> is not a database node.<br/><b>[[XQuery Errors#BaseX Errors|BASX0003]]</b> is raised if <code>$db</code> is a string and a database with that name cannot be | + | |<b>[[XQuery Errors#BaseX Errors|BASX0002]]</b> is raised if <code>$db</code> is not a database node.<br/><b>[[XQuery Errors#BaseX Errors|BASX0003]]</b> is raised if <code>$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''' | | valign='top' | '''Examples''' | ||
| | | | ||
− | * <code>db: | + | * <code>db:rename("DB", "docs/dir/doc.xml", "docs/dir/newdoc.xml")</code> renames the document <code>docs/dir/doc.xml</code> to <code>docs/dir/newdoc.xml</code> in the database <code>DB</code>. |
− | + | * <code>db:rename("DB", "docs/dir", "docs/newdir")</code> renames all documents with paths beginning with <code>docs/dir</code> to paths beginning with <code>docs/newdir</code> in the database <code>DB</code>. | |
− | |||
− | * <code>db: | ||
|} | |} | ||
Line 187: | Line 269: | ||
|- | |- | ||
| valign='top' | '''Errors''' | | valign='top' | '''Errors''' | ||
− | |<b>[[XQuery Errors#BaseX Errors|BASX0002]]</b> is raised if <code>$db</code> is not a database node.<br/><b>[[XQuery Errors#BaseX Errors|BASX0003]]</b> is raised if <code>$db</code> is a string and a database with that name cannot be | + | |<b>[[XQuery Errors#BaseX Errors|BASX0002]]</b> is raised if <code>$db</code> is not a database node.<br/><b>[[XQuery Errors#BaseX Errors|BASX0003]]</b> is raised if <code>$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>$path</code> is not a single document path.<br/><b>[[XQuery Errors#Functions Errors|FODC0002]]</b> is raised if <code>$input</code> is a string representing a path, which cannot be read.<br/><b>[[XQuery Errors#Update Errors|FOUP0001]]</b> is raised if <code>$input</code> is not a string and not a document node. |
|- | |- | ||
| valign='top' | '''Examples''' | | valign='top' | '''Examples''' | ||
Line 196: | Line 278: | ||
|} | |} | ||
− | ==db: | + | ==db:store== |
+ | |||
+ | <font color='orangered'>Introduced with Version 7.0:</font> | ||
{| | {| | ||
|- | |- | ||
| valign='top' width='90' | '''Signatures''' | | valign='top' width='90' | '''Signatures''' | ||
− | |<code><b>db: | + | |<code><b>db:store</b>($db as xs:string, $path as xs:string, $data as item()) as empty-sequence()</code> |
|- | |- | ||
| valign='top' | '''Summary''' | | valign='top' | '''Summary''' | ||
− | | | + | |Stores a binary resource specified by <code>$data</code> at the location specified by <code>$path</code>. |
|- | |- | ||
| valign='top' | '''Errors''' | | valign='top' | '''Errors''' | ||
− | |<b>[[XQuery Errors# | + | |<b>[[XQuery Errors#Update Errors|FOUP0002]]</b> is raised if the resource cannot be stored at the specified location. |
|- | |- | ||
| valign='top' | '''Examples''' | | valign='top' | '''Examples''' | ||
| | | | ||
− | * <code>db: | + | * <code>db:store("DB", "video/sample.mov", file:read-binary('video.mov'))</code> stores the addressed video file at the specified location. |
− | |||
|} | |} | ||
− | ==db: | + | =Helper Functions= |
+ | |||
+ | ==db:content-type== | ||
+ | |||
+ | <font color='orangered'>Introduced with Version 7.0:</font> | ||
{| | {| | ||
|- | |- | ||
| valign='top' width='90' | '''Signatures''' | | valign='top' width='90' | '''Signatures''' | ||
− | |<code><b>db: | + | |<code><b>db:content-type</b>($db as xs:string, $path as xs:string) as xs:string</code> |
|- | |- | ||
| valign='top' | '''Summary''' | | valign='top' | '''Summary''' | ||
− | | | + | |Retrieves the content type of the resource specified by <code>$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>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' | '''Errors''' | ||
− | |<b>[[XQuery Errors# | + | |<b>[[XQuery Errors#Functions Errors|FODC0002]]</b> is raised if the addressed resource is not found or cannot be retrieved. |
|- | |- | ||
| valign='top' | '''Examples''' | | valign='top' | '''Examples''' | ||
| | | | ||
− | * <code>db: | + | * <code>db:content-type("DB", "docs/doc01.pdf")</code> returns <code>application/pdf</code>. |
− | * <code>db: | + | * <code>db:content-type("DB", "docs/doc01.xml")</code> returns <code>application/xml</code>. |
+ | * <code>db:content-type("DB", "docs/doc01")</code> returns <code>application/xml</code>, if <code>db:is-xml("DB", "docs/doc01")</code> returns <code>true</code>. | ||
|} | |} | ||
− | ==db: | + | ==db:event== |
{| | {| | ||
|- | |- | ||
| valign='top' width='90' | '''Signatures''' | | valign='top' width='90' | '''Signatures''' | ||
− | |<code><b>db: | + | |<code><b>db:event</b>($name as xs:string, $query as item()) as empty-sequence()</code> |
|- | |- | ||
| valign='top' | '''Summary''' | | valign='top' | '''Summary''' | ||
− | | | + | |Executes a <code>$query</code> and sends the resulting value to all clients watching the [[Events|Event]] with the specified <code>$name</code>. No event will be sent to the client that fired the event. |
|- | |- | ||
| valign='top' | '''Errors''' | | valign='top' | '''Errors''' | ||
− | |<b>[[XQuery Errors#BaseX Errors| | + | |<b>[[XQuery Errors#BaseX Errors|BASX0009]]</b> is raised if the specified event is unknown.<br/><b>[[XQuery_Errors#Serialization_Errors|SEPM0016]]</b> is raised if serialization errors occurred while sending the value.<br/> |
+ | |} | ||
+ | |||
+ | ==db:exists== | ||
+ | |||
+ | <font color='orangered'>Introduced with Version 7.0:</font> | ||
+ | {| | ||
+ | |- | ||
+ | | valign='top' width='90' | '''Signatures''' | ||
+ | |<code><b>db:exists</b>($db as xs:string) as xs:boolean</code><br/><code><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>false</code> if a database directory is specified. | ||
+ | |- | ||
+ | | valign='top' | '''Examples''' | ||
+ | | | ||
+ | * <code>db:exists("DB")</code> returns <code>true</code> if the database <code>DB</code> exists. | ||
+ | * <code>db:exists("DB", "resource")</code> returns <code>true</code> if <code>resource</code> is an XML document or a raw file. | ||
+ | |} | ||
+ | |||
+ | ==db:is-raw== | ||
+ | |||
+ | <font color='orangered'>Introduced with Version 7.0:</font> | ||
+ | {| | ||
+ | |- | ||
+ | | valign='top' width='90' | '''Signatures''' | ||
+ | |<code><b>db:is-raw</b>($db as xs:string, $path as xs:string) as xs:boolean</code> | ||
+ | |- | ||
+ | | valign='top' | '''Summary''' | ||
+ | |Checks if the specified resource exists and if it is a raw file. | ||
|- | |- | ||
| valign='top' | '''Examples''' | | valign='top' | '''Examples''' | ||
| | | | ||
− | * <code>db: | + | * <code>db:is-raw("DB", "music/01.mp3")</code> returns <code>true</code>. |
− | * <code>db: | + | |} |
+ | |||
+ | ==db:is-xml== | ||
+ | |||
+ | <font color='orangered'>Introduced with Version 7.0:</font> | ||
+ | {| | ||
+ | |- | ||
+ | | valign='top' width='90' | '''Signatures''' | ||
+ | |<code><b>db:is-xml</b>($db as xs:string, $path as xs:string) as xs:boolean</code> | ||
+ | |- | ||
+ | | valign='top' | '''Summary''' | ||
+ | |Checks if the specified resource exists and if it is an XML document. | ||
+ | |- | ||
+ | | valign='top' | '''Examples''' | ||
+ | | | ||
+ | * <code>db:is-xml("DB", "dir/doc.xml")</code> returns <code>true</code>. | ||
|} | |} | ||
[[Category:XQuery]] | [[Category:XQuery]] |
Revision as of 17:21, 18 October 2011
This module contains 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 db:
prefix, which is linked to the http://www.basex.org/db
namespace.
A preview page contains all functions offered by the latest stable snapshot.
General Functions
db:info
Signatures | db:info($db as item()) as xs:string db:info($db as item(), $type as xs:string) as xs:string
|
Summary | Returns information for a database. If $type is specified, the function returns information on a database index. It must be one of the values TEXT , ATTRIBUTE , FULLTEXT , PATH , TAG , or ATTNAME .
|
Errors | BASX0001 is raised if the specified index is not available. |
Examples |
|
db:list
Signatures | db:list() as xs:string* db:list($db as xs:string) as xs:string* db:list($db as xs:string, $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.Modified in Version 7.0: database and resource path are now specified as two separate arguments. |
db:open
Signatures | db:open($db as xs:string) as document-node()* db:open($db as xs:string, $path as xs:string) as document-node()*
|
Summary | Returns a sequence with all document nodes contained in the database specified by $db . The returned documents may be filtered by the $path argument.Modified in Version 7.0: database and resource path are now specified as two separate arguments. |
Errors | BASX0003 is raised if the database is not found. |
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.In contrast to the pre value, the id will remain valid after update operations. |
Errors | BASX0004 is raised if the specified $id does not exist in the database.
|
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.
|
Errors | BASX0004 is raised if the specified $pre value does not exist in the database.
|
Examples |
|
db:system
Signatures | db:system() as xs:string
|
Summary | Prints information about the database system, including 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 that have $string as their string value.If $attname is specified, the resulting attribute nodes are filtered by their attribute name.
|
Examples |
|
db:fulltext
Signatures | db:fulltext($db as item(), $text as item()) as text()*
|
Summary | Returns all text nodes from the full-text index that contain the string $text . 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.
|
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 database nodes specified by $nodes . id values are pointers to database nodes, which are not changed by updates.
|
db:node-pre
Signatures | db:node-pre($nodes as node()*) as xs:integer*
|
Summary | Returns the pre values of all database nodes specified by $nodes . pre values are direct, internal pointers to database nodes, which might be changed by updates.
|
Examples |
|
db:retrieve
Introduced with Version 7.0:
Signatures | db:retrieve($db as xs:string, $path as xs:string) as xs:raw
|
Summary | Retrieves the binary resource specified by $path in the (implementation specific) xs:raw format.
|
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 that have $string as their string value.
|
Examples |
|
Updates
db:add
Modified in Version 7.0:
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 | Add documents specified by $input to the database $db and the specified $path .
|
Errors | BASX0002 is raised if $db is not a database node.BASX0003 is raised if $db is a string and a database with that name cannot be opened.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 | Delete document(s), specified by $path in the database $db .
|
Errors | BASX0002 is raised if $db is not a database node.BASX0003 is raised if $db is a string and a database with that name cannot be opened.
|
Examples |
|
db:optimize
Signatures | db:optimize($db as xs:string) as empty-sequence() db:optimize($db as xs:string, $all as xs:boolean) as empty-sequence()
|
Summary | Optimize the data structures of the database $db . If $all is set to true() , then the whole database will be rebuilt.
|
Errors | BASX0002 is raised if $db is not a database node.BASX0003 is raised if $db is a string and a database with that name cannot be opened.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 | Rename document(s), specified by $path to $newpath in the database $db .
|
Errors | BASX0002 is raised if $db is not a database node.BASX0003 is raised if $db is a string and a database with that name cannot be opened.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 | Replace a document, specified by $path in the database $db with the content of $input .
|
Errors | BASX0002 is raised if $db is not a database node.BASX0003 is raised if $db is a string and a database with that name cannot be opened.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
Introduced with Version 7.0:
Signatures | db:store($db as xs:string, $path as xs:string, $data as item()) as empty-sequence()
|
Summary | Stores a binary resource specified by $data at the location specified by $path .
|
Errors | FOUP0002 is raised if the resource cannot be stored at the specified location. |
Examples |
|
Helper Functions
db:content-type
Introduced with Version 7.0:
Signatures | db:content-type($db as xs:string, $path as xs:string) as xs:string
|
Summary | Retrieves the content type of the resource specified by $path . 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 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 . 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. |
db:exists
Introduced with Version 7.0:
Signatures | db:exists($db as xs:string) as xs:boolean db:exists($db as xs:string, $path as xs:string) as xs:boolean
|
Summary | Checks if the specified database or resource exists. Returns false if a database directory is specified.
|
Examples |
|
db:is-raw
Introduced with Version 7.0:
Signatures | db:is-raw($db as xs:string, $path as xs:string) as xs:boolean
|
Summary | Checks if the specified resource exists and if it is a raw file. |
Examples |
|
db:is-xml
Introduced with Version 7.0:
Signatures | db:is-xml($db as xs:string, $path as xs:string) as xs:boolean
|
Summary | Checks if the specified resource exists and if it is an XML document. |
Examples |
|