Difference between revisions of "Database Module"
Jump to navigation
Jump to search
Line 1: | Line 1: | ||
− | 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 | + | 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 {{Mono|db:}} prefix, which is linked to the {{Mono|http://www.basex.org/db}} namespace. |
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]. | ||
Line 9: | Line 9: | ||
|- | |- | ||
| valign='top' width='90' | '''Signatures''' | | valign='top' width='90' | '''Signatures''' | ||
− | | | + | |{{Mono|<b>db:info</b>($db as item()) as xs:string}}<br/>{{Mono|<b>db:info</b>($db as item(), $type as xs:string) as xs:string}} |
|- | |- | ||
| valign='top' | '''Summary''' | | valign='top' | '''Summary''' | ||
− | |Returns information for a database.<br />If | + | |Returns information for a database.<br />If {{Mono|$type}} is specified, the function returns information on a database index. It must be one of the values {{Mono|TEXT}}, {{Mono|ATTRIBUTE}}, {{Mono|FULLTEXT}}, {{Mono|PATH}}, {{Mono|TAG}}, or {{Mono|ATTNAME}}. |
|- | |- | ||
| valign='top' | '''Errors''' | | valign='top' | '''Errors''' | ||
Line 19: | Line 19: | ||
| valign='top' | '''Examples''' | | valign='top' | '''Examples''' | ||
| | | | ||
− | * | + | * {{Mono|db:info("DB", "FULLTEXT")}} returns information about the full-text index in the database {{Mono|DB}}. |
|} | |} | ||
Line 26: | Line 26: | ||
|- | |- | ||
| valign='top' width='90' | '''Signatures''' | | valign='top' width='90' | '''Signatures''' | ||
− | | | + | |{{Mono|<b>db:list</b>() as xs:string*}}<br/>{{Mono|<b>db:list</b>($db as xs:string) as xs:string*}}<br/>{{Mono|<b>db:list</b>($db as xs:string, $path as xs:string) as xs:string*}} |
|- | |- | ||
| valign='top' | '''Summary''' | | valign='top' | '''Summary''' | ||
− | |Returns an | + | |Returns an {{Mono|xs:string}} sequence with the names of all databases. If {{Mono|$db}} is specified, all documents and raw files of the specified database are returned. The list of resources can be further restricted by the {{Mono|$path}} argument.<br/>{{Mark|Modified in Version 7.0}}: database and resource path are now specified as two separate arguments. |
|} | |} | ||
Line 36: | Line 36: | ||
|- | |- | ||
| valign='top' width='90' | '''Signatures''' | | valign='top' width='90' | '''Signatures''' | ||
− | | | + | |{{Mono|<b>db:open</b>($db as xs:string) as document-node()*}}<br />{{Mono|<b>db:open</b>($db as xs:string, $path as xs:string) as document-node()*}} |
|- | |- | ||
| valign='top' | '''Summary''' | | valign='top' | '''Summary''' | ||
− | |Returns a sequence with all document nodes contained in the database specified by | + | |Returns a sequence with all document nodes contained in the database specified by {{Mono|$db}}. The returned documents may be filtered by the {{Mono|$path}} 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 46: | Line 46: | ||
| valign='top' | '''Examples''' | | valign='top' | '''Examples''' | ||
| | | | ||
− | * | + | * {{Mono|db:open("docs")}} returns all documents from the database named {{Mono|docs}}. |
− | * | + | * {{Mono|db:open("docs", "one")}} returns all documents from the database named {{Mono|docs}} in the subpath {{Mono|one}}. |
|} | |} | ||
Line 54: | Line 54: | ||
|- | |- | ||
| valign='top' width='90' | '''Signatures''' | | valign='top' width='90' | '''Signatures''' | ||
− | | | + | |{{Mono|<b>db:open-id</b>($db as item(), $id as xs:integer) as node()}} |
|- | |- | ||
| valign='top' | '''Summary''' | | valign='top' | '''Summary''' | ||
− | |Opens the database specified by | + | |Opens the database specified by {{Mono|$db}} and returns the node with the specified {{Mono|$id}} value.<br />In contrast to the <i>pre</i> value, the <i>id</i> will remain valid after update operations. |
|- | |- | ||
| valign='top' | '''Errors''' | | valign='top' | '''Errors''' | ||
− | |<b>[[XQuery Errors#BaseX Errors|BASX0004]]</b> is raised if the specified | + | |<b>[[XQuery Errors#BaseX Errors|BASX0004]]</b> is raised if the specified {{Mono|$id}} does not exist in the database. |
|} | |} | ||
Line 67: | Line 67: | ||
|- | |- | ||
| valign='top' width='90' | '''Signatures''' | | valign='top' width='90' | '''Signatures''' | ||
− | | | + | |{{Mono|<b>db:open-pre</b>($db as item(), $pre as xs:integer) as node()}} |
|- | |- | ||
| valign='top' | '''Summary''' | | valign='top' | '''Summary''' | ||
− | |Opens the database specified by | + | |Opens the database specified by {{Mono|$db}} and returns the node with the specified {{Mono|$pre}} value. |
|- | |- | ||
| valign='top' | '''Errors''' | | valign='top' | '''Errors''' | ||
− | |<b>[[XQuery Errors#BaseX Errors|BASX0004]]</b> is raised if the specified | + | |<b>[[XQuery Errors#BaseX Errors|BASX0004]]</b> is raised if the specified {{Mono|$pre}} value does not exist in the database. |
|- | |- | ||
| valign='top' | '''Examples''' | | valign='top' | '''Examples''' | ||
| | | | ||
− | * | + | * {{Mono|db:open-pre("docs", 0)}} returns the first database node from the database named {{Mono|docs}}. |
|} | |} | ||
Line 84: | Line 84: | ||
|- | |- | ||
| valign='top' width='90' | '''Signatures''' | | valign='top' width='90' | '''Signatures''' | ||
− | | | + | |{{Mono|<b>db:system</b>() as xs:string}} |
|- | |- | ||
| valign='top' | '''Summary''' | | valign='top' | '''Summary''' | ||
Line 96: | Line 96: | ||
|- | |- | ||
| valign='top' width='90' | '''Signatures''' | | valign='top' width='90' | '''Signatures''' | ||
− | | | + | |{{Mono|<b>db:attribute</b>($db as item(), $string as item()) as attribute()*}}<br/>{{Mono|<b>db:attribute</b>($db as item(), $string as item(), $attname as xs:string) as attribute()*}} |
|- | |- | ||
| valign='top' | '''Summary''' | | valign='top' | '''Summary''' | ||
− | |Returns all attribute nodes that have | + | |Returns all attribute nodes that have {{Mono|$string}} as their string value.<br />If {{Mono|$attname}} is specified, the resulting attribute nodes are filtered by their attribute name. |
|- | |- | ||
| valign='top' | '''Examples''' | | valign='top' | '''Examples''' | ||
| | | | ||
− | * | + | * {{Mono|db:attribute("DB", "QUERY", "id")/..}} returns the parents of all {{Mono|id}} attribute nodes of the database {{Mono|DB}} that have {{Mono|QUERY}} as string value. |
|} | |} | ||
Line 110: | Line 110: | ||
|- | |- | ||
| valign='top' width='90' | '''Signatures''' | | valign='top' width='90' | '''Signatures''' | ||
− | | | + | |{{Mono|<b>db:fulltext</b>($db as item(), $text as item()) as text()*}} |
|- | |- | ||
| valign='top' | '''Summary''' | | valign='top' | '''Summary''' | ||
− | |Returns all text nodes from the full-text index that contain the string | + | |Returns all text nodes from the full-text index that contain the string {{Mono|$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. |
|- | |- | ||
| valign='top' | '''Errors''' | | valign='top' | '''Errors''' | ||
Line 120: | Line 120: | ||
| valign='top' | '''Examples''' | | valign='top' | '''Examples''' | ||
| | | | ||
− | * | + | * {{Mono|db:fulltext("DB", "QUERY")}} returns all text nodes of the database {{Mono|DB}} that contain the string {{Mono|QUERY}}. |
|} | |} | ||
Line 127: | Line 127: | ||
|- | |- | ||
| valign='top' width='90' | '''Signatures''' | | valign='top' width='90' | '''Signatures''' | ||
− | | | + | |{{Mono|<b>db:node-id</b>($nodes as node()*) as xs:integer*}} |
|- | |- | ||
| valign='top' | '''Summary''' | | valign='top' | '''Summary''' | ||
− | |Returns the <i>id</i> values of all database nodes specified by | + | |Returns the <i>id</i> values of all database nodes specified by {{Mono|$nodes}}. <i>id</i> values are pointers to database nodes, which are not changed by updates. |
|} | |} | ||
Line 137: | Line 137: | ||
|- | |- | ||
| valign='top' width='90' | '''Signatures''' | | valign='top' width='90' | '''Signatures''' | ||
− | | | + | |{{Mono|<b>db:node-pre</b>($nodes as node()*) as xs:integer*}} |
|- | |- | ||
| valign='top' | '''Summary''' | | valign='top' | '''Summary''' | ||
− | |Returns the <i>pre</i> values of all database nodes specified by | + | |Returns the <i>pre</i> values of all database nodes specified by {{Mono|$nodes}}. <i>pre</i> values are direct, internal pointers to database nodes, which might be changed by updates. |
|- | |- | ||
| valign='top' | '''Examples''' | | valign='top' | '''Examples''' | ||
| | | | ||
− | * | + | * {{Mono|db:node-pre(doc("input"))}} returns {{Mono|0}} if the database {{Mono|input}} contains a single document. |
|} | |} | ||
Line 153: | Line 153: | ||
|- | |- | ||
| valign='top' width='90' | '''Signatures''' | | valign='top' width='90' | '''Signatures''' | ||
− | | | + | |{{Mono|<b>db:retrieve</b>($db as xs:string, $path as xs:string) as xs:raw}} |
|- | |- | ||
| valign='top' | '''Summary''' | | valign='top' | '''Summary''' | ||
− | |Retrieves the binary resource specified by | + | |Retrieves the binary resource specified by {{Mono|$path}} in the (implementation specific) {{Mono|xs:raw}} format. |
|- | |- | ||
| valign='top' | '''Errors''' | | valign='top' | '''Errors''' | ||
Line 163: | Line 163: | ||
| valign='top' | '''Examples''' | | valign='top' | '''Examples''' | ||
| | | | ||
− | * | + | * {{Mono|declare option output:method 'raw';<br/>db:retrieve("DB", "music/01.mp3")}} returns the specified audio file as raw data. |
|} | |} | ||
Line 170: | Line 170: | ||
|- | |- | ||
| valign='top' width='90' | '''Signatures''' | | valign='top' width='90' | '''Signatures''' | ||
− | | | + | |{{Mono|<b>db:text</b>($db as item(), $string as item()) as text()*}} |
|- | |- | ||
| valign='top' | '''Summary''' | | valign='top' | '''Summary''' | ||
− | |Returns all text nodes that have | + | |Returns all text nodes that have {{Mono|$string}} as their string value. |
|- | |- | ||
| valign='top' | '''Examples''' | | valign='top' | '''Examples''' | ||
| | | | ||
− | * | + | * {{Mono|db:text("DB", "QUERY")/..}} returns the parents of all text nodes of the database {{Mono|DB}} that match the string {{Mono|QUERY}}. |
|} | |} | ||
Line 189: | Line 189: | ||
|- | |- | ||
| valign='top' width='90' | '''Signatures''' | | valign='top' width='90' | '''Signatures''' | ||
− | | | + | |{{Mono|<b>db:add</b>($db as item(), $input as item()) as empty-sequence()}}<br/>{{Mono|<b>db:add</b>($db as item(), $input as item(), $path as xs:string) as empty-sequence()}} |
|- | |- | ||
| valign='top' | '''Summary''' | | valign='top' | '''Summary''' | ||
− | |Add documents specified by | + | |Add documents specified by {{Mono|$input}} to the database {{Mono|$db}} and the specified {{Mono|$path}}. |
|- | |- | ||
| valign='top' | '''Errors''' | | valign='top' | '''Errors''' | ||
− | |<b>[[XQuery Errors#BaseX Errors|BASX0002]]</b> is raised if | + | |<b>[[XQuery Errors#BaseX Errors|BASX0002]]</b> is raised if {{Mono|$db}} is not a database node.<br/><b>[[XQuery Errors#BaseX Errors|BASX0003]]</b> is raised if {{Mono|$db}} is a string and a database with that name cannot be opened.<br/><b>[[XQuery Errors#Functions Errors|FODC0002]]</b> is raised if {{Mono|$input}} is a string representing a path, which cannot be read.<br/><b>[[XQuery Errors#Update Errors|FOUP0001]]</b> is raised if {{Mono|$input}} is not a string and not a document node. |
|- | |- | ||
| valign='top' | '''Examples''' | | valign='top' | '''Examples''' | ||
| | | | ||
− | * | + | * {{Mono|db:add("DB", "/home/dir/doc.xml")}} adds the file {{Mono|/home/dir/doc.xml}} to the database {{Mono|DB}}. |
− | * | + | * {{Mono|db:add("DB", "<a/>", "doc.xml")}} adds a document with content {{Mono|<a/>}} to the database {{Mono|DB}} under the name {{Mono|doc.xml}}. |
− | * | + | * {{Mono|db:add("DB", document { <a/> }, "doc.xml")}} adds the document node to the database {{Mono|DB}} under the name {{Mono|doc.xml}}. |
− | * | + | * {{Mono|db:add("DB", "/home/dir", "docs/dir")}} adds all documents in {{Mono|/home/dir}} to the database {{Mono|DB}} under the path {{Mono|docs/dir}}. |
|} | |} | ||
Line 209: | Line 209: | ||
|- | |- | ||
| valign='top' width='90' | '''Signatures''' | | valign='top' width='90' | '''Signatures''' | ||
− | | | + | |{{Mono|<b>db:delete</b>($db as item(), $path as xs:string) as empty-sequence()}} |
|- | |- | ||
| valign='top' | '''Summary''' | | valign='top' | '''Summary''' | ||
− | |Delete document(s), specified by | + | |Delete document(s), specified by {{Mono|$path}} in the database {{Mono|$db}}. |
|- | |- | ||
| valign='top' | '''Errors''' | | valign='top' | '''Errors''' | ||
− | |<b>[[XQuery Errors#BaseX Errors|BASX0002]]</b> is raised if | + | |<b>[[XQuery Errors#BaseX Errors|BASX0002]]</b> is raised if {{Mono|$db}} is not a database node.<br/><b>[[XQuery Errors#BaseX Errors|BASX0003]]</b> is raised if {{Mono|$db}} is a string and a database with that name cannot be opened. |
|- | |- | ||
| valign='top' | '''Examples''' | | valign='top' | '''Examples''' | ||
| | | | ||
− | * | + | * {{Mono|db:delete("DB", "docs/dir/doc.xml")}} deletes the document {{Mono|docs/dir/doc.xml}} in the database {{Mono|DB}}. |
− | * | + | * {{Mono|db:delete("DB", "docs/dir", "<a/>")}} deletes all documents with paths beginning with {{Mono|docs/dir}} in the database {{Mono|DB}}. |
|} | |} | ||
Line 227: | Line 227: | ||
|- | |- | ||
| valign='top' width='90' | '''Signatures''' | | valign='top' width='90' | '''Signatures''' | ||
− | | | + | |{{Mono|<b>db:optimize</b>($db as xs:string) as empty-sequence()}}<br/>{{Mono|<b>db:optimize</b>($db as xs:string, $all as xs:boolean) as empty-sequence()}} |
|- | |- | ||
| valign='top' | '''Summary''' | | valign='top' | '''Summary''' | ||
− | |Optimize the data structures of the database | + | |Optimize the data structures of the database {{Mono|$db}}. If {{Mono|$all}} is set to {{Mono|true()}}, then the whole database will be rebuilt. |
|- | |- | ||
| valign='top' | '''Errors''' | | valign='top' | '''Errors''' | ||
− | |<b>[[XQuery Errors#BaseX Errors|BASX0002]]</b> is raised if | + | |<b>[[XQuery Errors#BaseX Errors|BASX0002]]</b> is raised if {{Mono|$db}} is not a database node.<br/><b>[[XQuery Errors#BaseX Errors|BASX0003]]</b> is raised if {{Mono|$db}} 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 {{Mono|$all}} flag is set to {{Mono|true()}}, but the database is an in-memory database.<br/><b>[[XQuery Errors#BaseX Errors|BASX0016]]</b> is raised if the database {{Mono|$db}} is in use by other user(s). |
|- | |- | ||
| valign='top' | '''Examples''' | | valign='top' | '''Examples''' | ||
| | | | ||
− | * | + | * {{Mono|db:optimize("DB")}} optimizes the database structures of the database {{Mono|DB}}. |
− | * | + | * {{Mono|db:optimize("DB", true())}} optimizes all database structures of the database {{Mono|DB}}. |
|} | |} | ||
Line 245: | Line 245: | ||
|- | |- | ||
| valign='top' width='90' | '''Signatures''' | | valign='top' width='90' | '''Signatures''' | ||
− | | | + | |{{Mono|<b>db:rename</b>($db as item(), $path as xs:string, $newpath as xs:string) as empty-sequence()}} |
|- | |- | ||
| valign='top' | '''Summary''' | | valign='top' | '''Summary''' | ||
− | |Rename document(s), specified by | + | |Rename document(s), specified by {{Mono|$path}} to {{Mono|$newpath}} in the database {{Mono|$db}}. |
|- | |- | ||
| valign='top' | '''Errors''' | | valign='top' | '''Errors''' | ||
− | |<b>[[XQuery Errors#BaseX Errors|BASX0002]]</b> is raised if | + | |<b>[[XQuery Errors#BaseX Errors|BASX0002]]</b> is raised if {{Mono|$db}} is not a database node.<br/><b>[[XQuery Errors#BaseX Errors|BASX0003]]</b> is raised if {{Mono|$db}} 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''' | ||
| | | | ||
− | * | + | * {{Mono|db:rename("DB", "docs/dir/doc.xml", "docs/dir/newdoc.xml")}} renames the document {{Mono|docs/dir/doc.xml}} to {{Mono|docs/dir/newdoc.xml}} in the database {{Mono|DB}}. |
− | * | + | * {{Mono|db:rename("DB", "docs/dir", "docs/newdir")}} renames all documents with paths beginning with {{Mono|docs/dir}} to paths beginning with {{Mono|docs/newdir}} in the database {{Mono|DB}}. |
|} | |} | ||
Line 263: | Line 263: | ||
|- | |- | ||
| valign='top' width='90' | '''Signatures''' | | valign='top' width='90' | '''Signatures''' | ||
− | | | + | |{{Mono|<b>db:replace</b>($db as item(), $path as xs:string, $input as item()) as empty-sequence()}} |
|- | |- | ||
| valign='top' | '''Summary''' | | valign='top' | '''Summary''' | ||
− | |Replace a document, specified by | + | |Replace a document, specified by {{Mono|$path}} in the database {{Mono|$db}} with the content of {{Mono|$input}}. |
|- | |- | ||
| valign='top' | '''Errors''' | | valign='top' | '''Errors''' | ||
− | |<b>[[XQuery Errors#BaseX Errors|BASX0002]]</b> is raised if | + | |<b>[[XQuery Errors#BaseX Errors|BASX0002]]</b> is raised if {{Mono|$db}} is not a database node.<br/><b>[[XQuery Errors#BaseX Errors|BASX0003]]</b> is raised if {{Mono|$db}} is a string and a database with that name cannot be opened.<br/><b>[[XQuery Errors#BaseX Errors|BASX0012]]</b> is raised if {{Mono|$path}} is not a single document path.<br/><b>[[XQuery Errors#Functions Errors|FODC0002]]</b> is raised if {{Mono|$input}} is a string representing a path, which cannot be read.<br/><b>[[XQuery Errors#Update Errors|FOUP0001]]</b> is raised if {{Mono|$input}} is not a string and not a document node. |
|- | |- | ||
| valign='top' | '''Examples''' | | valign='top' | '''Examples''' | ||
| | | | ||
− | * | + | * {{Mono|db:replace("DB", "docs/dir/doc.xml", "/home/dir/doc.xml")}} replaces the content of the document {{Mono|docs/dir/doc.xml}} in the database {{Mono|DB}} with the content of the file {{Mono|/home/dir/doc.xml}}. |
− | * | + | * {{Mono|db:replace("DB", "docs/dir/doc.xml", "<a/>")}} replaces the content of the document {{Mono|docs/dir/doc.xml}} in the database {{Mono|DB}} with {{Mono|<a/>}}. |
− | * | + | * {{Mono|db:replace("DB", "docs/dir/doc.xml", document { <a/> })}} replaces the content of the document {{Mono|docs/dir/doc.xml}} in the database {{Mono|DB}} with the specified document node. |
|} | |} | ||
Line 284: | Line 284: | ||
|- | |- | ||
| valign='top' width='90' | '''Signatures''' | | valign='top' width='90' | '''Signatures''' | ||
− | | | + | |{{Mono|<b>db:store</b>($db as xs:string, $path as xs:string, $data as item()) as empty-sequence()}} |
|- | |- | ||
| valign='top' | '''Summary''' | | valign='top' | '''Summary''' | ||
− | |Stores a binary resource specified by | + | |Stores a binary resource specified by {{Mono|$data}} at the location specified by {{Mono|$path}}. |
|- | |- | ||
| valign='top' | '''Errors''' | | valign='top' | '''Errors''' | ||
Line 294: | Line 294: | ||
| valign='top' | '''Examples''' | | valign='top' | '''Examples''' | ||
| | | | ||
− | * | + | * {{Mono|db:store("DB", "video/sample.mov", file:read-binary('video.mov'))}} stores the addressed video file at the specified location. |
|} | |} | ||
Line 305: | Line 305: | ||
|- | |- | ||
| valign='top' width='90' | '''Signatures''' | | valign='top' width='90' | '''Signatures''' | ||
− | | | + | |{{Mono|<b>db:content-type</b>($db as xs:string, $path as xs:string) as xs:string}} |
|- | |- | ||
| valign='top' | '''Summary''' | | valign='top' | '''Summary''' | ||
− | |Retrieves the content type of the resource specified by | + | |Retrieves the content type of the resource specified by {{Mono|$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 {{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''' | ||
Line 315: | Line 315: | ||
| valign='top' | '''Examples''' | | valign='top' | '''Examples''' | ||
| | | | ||
− | * | + | * {{Mono|db:content-type("DB", "docs/doc01.pdf")}} returns {{Mono|application/pdf}}. |
− | * | + | * {{Mono|db:content-type("DB", "docs/doc01.xml")}} returns {{Mono|application/xml}}. |
− | * | + | * {{Mono|db:content-type("DB", "docs/doc01")}} returns {{Mono|application/xml}}, if {{Mono|db:is-xml("DB", "docs/doc01")}} returns {{Mono|true}}. |
|} | |} | ||
Line 324: | Line 324: | ||
|- | |- | ||
| valign='top' width='90' | '''Signatures''' | | valign='top' width='90' | '''Signatures''' | ||
− | | | + | |{{Mono|<b>db:event</b>($name as xs:string, $query as item()) as empty-sequence()}} |
|- | |- | ||
| valign='top' | '''Summary''' | | valign='top' | '''Summary''' | ||
− | |Executes a | + | |Executes a {{Mono|$query}} and sends the resulting value to all clients watching the [[Events|Event]] with the specified {{Mono|$name}}. No event will be sent to the client that fired the event. |
|- | |- | ||
| valign='top' | '''Errors''' | | valign='top' | '''Errors''' | ||
Line 339: | Line 339: | ||
|- | |- | ||
| valign='top' width='90' | '''Signatures''' | | valign='top' width='90' | '''Signatures''' | ||
− | | | + | |{{Mono|<b>db:exists</b>($db as xs:string) as xs:boolean}}<br/>{{Mono|<b>db:exists</b>($db as xs:string, $path as xs:string) as xs:boolean}} |
|- | |- | ||
| valign='top' | '''Summary''' | | valign='top' | '''Summary''' | ||
− | |Checks if the specified database or resource exists. Returns | + | |Checks if the specified database or resource exists. Returns {{Mono|false}} if a database directory is specified. |
|- | |- | ||
| valign='top' | '''Examples''' | | valign='top' | '''Examples''' | ||
| | | | ||
− | * | + | * {{Mono|db:exists("DB")}} returns {{Mono|true}} if the database {{Mono|DB}} exists. |
− | * | + | * {{Mono|db:exists("DB", "resource")}} returns {{Mono|true}} if {{Mono|resource}} is an XML document or a raw file. |
|} | |} | ||
Line 356: | Line 356: | ||
|- | |- | ||
| valign='top' width='90' | '''Signatures''' | | valign='top' width='90' | '''Signatures''' | ||
− | | | + | |{{Mono|<b>db:is-raw</b>($db as xs:string, $path as xs:string) as xs:boolean}} |
|- | |- | ||
| valign='top' | '''Summary''' | | valign='top' | '''Summary''' | ||
Line 363: | Line 363: | ||
| valign='top' | '''Examples''' | | valign='top' | '''Examples''' | ||
| | | | ||
− | * | + | * {{Mono|db:is-raw("DB", "music/01.mp3")}} returns {{Mono|true}}. |
|} | |} | ||
Line 372: | Line 372: | ||
|- | |- | ||
| valign='top' width='90' | '''Signatures''' | | valign='top' width='90' | '''Signatures''' | ||
− | | | + | |{{Mono|<b>db:is-xml</b>($db as xs:string, $path as xs:string) as xs:boolean}} |
|- | |- | ||
| valign='top' | '''Summary''' | | valign='top' | '''Summary''' | ||
Line 379: | Line 379: | ||
| valign='top' | '''Examples''' | | valign='top' | '''Examples''' | ||
| | | | ||
− | * | + | * {{Mono|db:is-xml("DB", "dir/doc.xml")}} returns {{Mono|true}}. |
|} | |} | ||
[[Category:XQuery]] | [[Category:XQuery]] |
Revision as of 23:52, 26 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.Template:Mark: 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 |
|