Difference between revisions of "Database Module"

From BaseX Documentation
Jump to navigation Jump to search
m (Text replace - "{{Mono|" to "{{Code|")
Line 1: Line 1:
This [[Module Library|XQuery Module]] contains functions for processing databases from within XQuery. Existing databases can be opened and listed, its contents can be directly accessed, documents can be added to and removed, etc. All functions are introduced with the {{Mono|db:}} prefix, which is linked to the statically declared {{Mono|http://basex.org/modules/db}} namespace.
+
This [[Module Library|XQuery Module]] contains functions for processing databases from within XQuery. Existing databases can be opened and listed, its contents can be directly accessed, documents can be added to and removed, etc. All functions are introduced with the {{Code|db:}} prefix, which is linked to the statically declared {{Code|http://basex.org/modules/db}} namespace.
  
 
=Conventions=
 
=Conventions=
Line 8: Line 8:
 
==Database Nodes==
 
==Database Nodes==
  
Many function signatures in this and other modules share {{Mono|$db}} as argument to reference an existing database. The argument may either be 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:
+
Many function signatures in this and other modules share {{Code|$db}} as argument to reference an existing database. The argument may either be 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:
  
* '''[[#Errors|BXDB0001]]''' is raised if {{Mono|$db}} references an XML node that is not stored in a database, or is no database fragment.
+
* '''[[#Errors|BXDB0001]]''' is raised if {{Code|$db}} references an XML node that is not stored in a database, or is no database fragment.
 
* '''[[#Errors|BXDB0002]]''' is raised if the addressed database cannot be opened.
 
* '''[[#Errors|BXDB0002]]''' is raised if the addressed database cannot be opened.
  
Line 27: Line 27:
 
|-
 
|-
 
| width='90' | '''Signatures'''
 
| width='90' | '''Signatures'''
|{{Mono|'''db:info'''($db as item()) as element(Database)}}
+
|{{Code|'''db:info'''($db as item()) as element(Database)}}
 
|-
 
|-
 
| '''Summary'''
 
| '''Summary'''
Line 37: Line 37:
 
|-
 
|-
 
| width='90' | '''Signatures'''
 
| width='90' | '''Signatures'''
|{{Mono|'''db:list'''() as xs:string*}}<br/>{{Mono|'''db:list'''($db as item()) as xs:string*}}<br/>{{Mono|'''db:list'''($db as item(), $path as xs:string) as xs:string*}}
+
|{{Code|'''db:list'''() as xs:string*}}<br/>{{Code|'''db:list'''($db as item()) as xs:string*}}<br/>{{Code|'''db:list'''($db as item(), $path as xs:string) as xs:string*}}
 
|-
 
|-
 
| '''Summary'''
 
| '''Summary'''
|Returns an {{Mono|xs:string}} sequence with the names of all databases.<br/>If <code>[[#Database Nodes|$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.
+
|Returns an {{Code|xs:string}} sequence with the names of all databases.<br/>If <code>[[#Database Nodes|$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 {{Code|$path}} argument.
 
|-
 
|-
 
| '''Examples'''
 
| '''Examples'''
 
|
 
|
* {{Mono|db:list("docs")}} returns the names of all documents from the database named {{Mono|docs}}.
+
* {{Code|db:list("docs")}} returns the names of all documents from the database named {{Code|docs}}.
 
|}
 
|}
  
Line 51: Line 51:
 
|-
 
|-
 
| width='90' | '''Signatures'''
 
| width='90' | '''Signatures'''
|{{Mono|'''db:list-details'''() as element(database)*}}<br/>{{Mono|'''db:list-details'''($db as item()) as element(resource)*}}<br/>{{Mono|'''db:list-details'''($db as item(), $path as xs:string) as element(resource)*}}
+
|{{Code|'''db:list-details'''() as element(database)*}}<br/>{{Code|'''db:list-details'''($db as item()) as element(resource)*}}<br/>{{Code|'''db:list-details'''($db as item(), $path as xs:string) as element(resource)*}}
 
|-
 
|-
 
| '''Summary'''
 
| '''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 <code>[[#Database Nodes|$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.
+
|Returns an {{Code|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 Nodes|$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 {{Code|$path}} argument.
 
|-
 
|-
 
| '''Examples'''
 
| '''Examples'''
 
|
 
|
* {{Mono|db:list-details("docs")}} returns the names plus additional data of all documents from the database named {{Mono|docs}}.
+
* {{Code|db:list-details("docs")}} returns the names plus additional data of all documents from the database named {{Code|docs}}.
 
|}
 
|}
  
Line 65: Line 65:
 
|-
 
|-
 
| width='90' | '''Signatures'''
 
| width='90' | '''Signatures'''
|{{Mono|'''db:open'''($db as item()) as document-node()*}}<br />{{Mono|'''db:open'''($db as item(), $path as xs:string) as document-node()*}}
+
|{{Code|'''db:open'''($db as item()) as document-node()*}}<br />{{Code|'''db:open'''($db as item(), $path as xs:string) as document-node()*}}
 
|-
 
|-
 
| '''Summary'''
 
| '''Summary'''
|Returns a sequence with all document nodes contained in the database specified by <code>[[#Database Nodes|$db]]</code>.<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 Nodes|$db]]</code>.<br/>The document nodes to be returned can be restricted by the {{Code|$path}} argument.
 
|-
 
|-
 
| '''Examples'''
 
| '''Examples'''
 
|
 
|
* {{Mono|db:open("docs")}} returns all documents from the database named {{Mono|docs}}.
+
* {{Code|db:open("docs")}} returns all documents from the database named {{Code|docs}}.
* {{Mono|db:open("docs", "one")}} returns all documents from the database named {{Mono|docs}} in the subpath {{Mono|one}}.
+
* {{Code|db:open("docs", "one")}} returns all documents from the database named {{Code|docs}} in the subpath {{Code|one}}.
 
|}
 
|}
  
Line 80: Line 80:
 
|-
 
|-
 
| width='90' | '''Signatures'''
 
| width='90' | '''Signatures'''
|{{Mono|'''db:open-id'''($db as item(), $id as xs:integer) as node()}}
+
|{{Code|'''db:open-id'''($db as item(), $id as xs:integer) as node()}}
 
|-
 
|-
 
| '''Summary'''
 
| '''Summary'''
|Opens the database specified by <code>[[#Database Nodes|$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.
+
|Opens the database specified by <code>[[#Database Nodes|$db]]</code> and returns the node with the specified {{Code|$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.
 
|-
 
|-
 
| '''Errors'''
 
| '''Errors'''
Line 93: Line 93:
 
|-
 
|-
 
| width='90' | '''Signatures'''
 
| width='90' | '''Signatures'''
|{{Mono|'''db:open-pre'''($db as item(), $pre as xs:integer) as node()}}
+
|{{Code|'''db:open-pre'''($db as item(), $pre as xs:integer) as node()}}
 
|-
 
|-
 
| '''Summary'''
 
| '''Summary'''
|Opens the database specified by <code>[[#Database Nodes|$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.
+
|Opens the database specified by <code>[[#Database Nodes|$db]]</code> and returns the node with the specified {{Code|$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.
 
|-
 
|-
 
| '''Errors'''
 
| '''Errors'''
Line 103: Line 103:
 
| '''Examples'''
 
| '''Examples'''
 
|
 
|
* {{Mono|db:open-pre("docs", 0)}} returns the first database node from the database named {{Mono|docs}}.
+
* {{Code|db:open-pre("docs", 0)}} returns the first database node from the database named {{Code|docs}}.
 
|}
 
|}
  
Line 110: Line 110:
 
|-
 
|-
 
| width='90' | '''Signatures'''
 
| width='90' | '''Signatures'''
|{{Mono|'''db:system'''() as element(system)}}
+
|{{Code|'''db:system'''() as element(system)}}
 
|-
 
|-
 
| '''Summary'''
 
| '''Summary'''
Line 122: Line 122:
 
|-
 
|-
 
| width='90' | '''Signatures'''
 
| width='90' | '''Signatures'''
|{{Mono|'''db:attribute'''($db as item(), $string as item()) as attribute()*}}<br/>{{Mono|'''db:attribute'''($db as item(), $string as item(), $attname as xs:string) as attribute()*}}
+
|{{Code|'''db:attribute'''($db as item(), $string as item()) as attribute()*}}<br/>{{Code|'''db:attribute'''($db as item(), $string as item(), $attname as xs:string) as attribute()*}}
 
|-
 
|-
 
| '''Summary'''
 
| '''Summary'''
|Returns all attribute nodes of the database specified by <code>[[#Database Nodes|$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.
+
|Returns all attribute nodes of the database specified by <code>[[#Database Nodes|$db]]</code> that have {{Code|$string}} as string value. If available, the value index is used to speed up evaluation.<br />If {{Code|$attname}} is specified, the resulting attribute nodes are filtered by their attribute name.
 
|-
 
|-
 
| '''Examples'''
 
| '''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.
+
* {{Code|db:attribute("DB", "QUERY", "id")/..}} returns the parents of all {{Code|id}} attribute nodes of the database {{Code|DB}} that have {{Code|QUERY}} as string value.
 
|}
 
|}
  
Line 138: Line 138:
 
|-
 
|-
 
| width='90' | '''Signatures'''
 
| width='90' | '''Signatures'''
|{{Mono|'''db:attribute-range'''($db as item(), $min as xs:string, $max as xs:string) as text()*}}<br/>{{Mono|'''db:attribute-range'''($db as item(), $min as xs:string, $max as xs:string, $attname as xs:string) as attribute()*}}
+
|{{Code|'''db:attribute-range'''($db as item(), $min as xs:string, $max as xs:string) as text()*}}<br/>{{Code|'''db:attribute-range'''($db as item(), $min as xs:string, $max as xs:string, $attname as xs:string) as attribute()*}}
 
|-
 
|-
 
| '''Summary'''
 
| '''Summary'''
|Returns all attributes of the database specified by <code>[[#Database Nodes|$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.
+
|Returns all attributes of the database specified by <code>[[#Database Nodes|$db]]</code>, the string values of which are larger than or equal to {{Code|$min}} and smaller than or equal to {{Code|$max}}. If available, the value index is used to speed up evaluation.
 
|-
 
|-
 
| '''Examples'''
 
| '''Examples'''
 
|
 
|
* {{Mono|db:attribute-range("DB", "id456", "id473", 'id')}} returns all {{Mono|@id}} attributes of the database {{Mono|DB}} that have a string value in between {{Mono|id456}} and {{Mono|id473}}.
+
* {{Code|db:attribute-range("DB", "id456", "id473", 'id')}} returns all {{Code|@id}} attributes of the database {{Code|DB}} that have a string value in between {{Code|id456}} and {{Code|id473}}.
 
|}
 
|}
  
Line 152: Line 152:
 
|-
 
|-
 
| width='90' | '''Signatures'''
 
| width='90' | '''Signatures'''
|{{Mono|'''db:fulltext'''($db as item(), $terms as xs:string) as text()*}}
+
|{{Code|'''db:fulltext'''($db as item(), $terms as xs:string) as text()*}}
 
|-
 
|-
 
| '''Summary'''
 
| '''Summary'''
|Returns all text nodes from the full-text index of the database specified by <code>[[#Database Nodes|$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.
+
|Returns all text nodes from the full-text index of the database specified by <code>[[#Database Nodes|$db]]</code> that contain the text specified as {{Code|$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.
 
|-
 
|-
 
| '''Errors'''
 
| '''Errors'''
Line 162: Line 162:
 
| '''Examples'''
 
| '''Examples'''
 
|
 
|
* {{Mono|db:fulltext("DB", "QUERY")}} returns all text nodes of the database {{Mono|DB}} that contain the string {{Mono|QUERY}}.
+
* {{Code|db:fulltext("DB", "QUERY")}} returns all text nodes of the database {{Code|DB}} that contain the string {{Code|QUERY}}.
 
|}
 
|}
  
Line 169: Line 169:
 
|-
 
|-
 
| width='90' | '''Signatures'''
 
| width='90' | '''Signatures'''
|{{Mono|'''db:node-id'''($nodes as node()*) as xs:integer*}}
+
|{{Code|'''db:node-id'''($nodes as node()*) as xs:integer*}}
 
|-
 
|-
 
| '''Summary'''
 
| '''Summary'''
|Returns the ''id'' values of all {{Mono|$nodes}} of the database specified by <code>[[#Database Nodes|$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.
+
|Returns the ''id'' values of all {{Code|$nodes}} of the database specified by <code>[[#Database Nodes|$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 179: Line 179:
 
|-
 
|-
 
| width='90' | '''Signatures'''
 
| width='90' | '''Signatures'''
|{{Mono|'''db:node-pre'''($nodes as node()*) as xs:integer*}}
+
|{{Code|'''db:node-pre'''($nodes as node()*) as xs:integer*}}
 
|-
 
|-
 
| '''Summary'''
 
| '''Summary'''
|Returns the ''pre'' values of all {{Mono|$nodes}} of the database specified by <code>[[#Database Nodes|$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.
+
|Returns the ''pre'' values of all {{Code|$nodes}} of the database specified by <code>[[#Database Nodes|$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.
 
|-
 
|-
 
| '''Examples'''
 
| '''Examples'''
 
|
 
|
* {{Mono|db:node-pre(doc("input"))}} returns {{Mono|0}} if the database {{Mono|input}} contains a single document.
+
* {{Code|db:node-pre(doc("input"))}} returns {{Code|0}} if the database {{Code|input}} contains a single document.
 
|}
 
|}
  
Line 194: Line 194:
 
|-
 
|-
 
| width='90' | '''Signatures'''
 
| width='90' | '''Signatures'''
|{{Mono|'''db:retrieve'''($db as item(), $path as xs:string) as xs:base64Binary}}
+
|{{Code|'''db:retrieve'''($db as item(), $path as xs:string) as xs:base64Binary}}
 
|-
 
|-
 
| '''Summary'''
 
| '''Summary'''
|Returns a binary database resource addressed by <code>[[#Database Nodes|$db]]</code> and {{Mono|$path}}.
+
|Returns a binary database resource addressed by <code>[[#Database Nodes|$db]]</code> and {{Code|$path}}.
 
|-
 
|-
 
| '''Errors'''
 
| '''Errors'''
Line 204: Line 204:
 
| '''Examples'''
 
| '''Examples'''
 
|
 
|
* {{Mono|declare option output:method 'raw';<br/>db:retrieve("DB", "music/01.mp3")}} returns the specified audio file as raw data.
+
* {{Code|declare option output:method 'raw';<br/>db:retrieve("DB", "music/01.mp3")}} returns the specified audio file as raw data.
 
|}
 
|}
  
Line 211: Line 211:
 
|-
 
|-
 
| width='90' | '''Signatures'''
 
| width='90' | '''Signatures'''
|{{Mono|'''db:text'''($db as item(), $string as item()) as text()*}}
+
|{{Code|'''db:text'''($db as item(), $string as item()) as text()*}}
 
|-
 
|-
 
| '''Summary'''
 
| '''Summary'''
|Returns all text nodes of the database specified by <code>[[#Database Nodes|$db]]</code> 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 Nodes|$db]]</code> that have {{Code|$string}} as their string value. If available, the value index is used to speed up evaluation.
 
|-
 
|-
 
| '''Examples'''
 
| '''Examples'''
 
|
 
|
* {{Mono|db:text("DB", "QUERY")/..}} returns the parents of all text nodes of the database {{Mono|DB}} that match the string {{Mono|QUERY}}.
+
* {{Code|db:text("DB", "QUERY")/..}} returns the parents of all text nodes of the database {{Code|DB}} that match the string {{Code|QUERY}}.
 
|}
 
|}
  
Line 227: Line 227:
 
|-
 
|-
 
| width='90' | '''Signatures'''
 
| width='90' | '''Signatures'''
|{{Mono|'''db:text-range'''($db as item(), $min as xs:string, $max as xs:string) as text()*}}
+
|{{Code|'''db:text-range'''($db as item(), $min as xs:string, $max as xs:string) as text()*}}
 
|-
 
|-
 
| '''Summary'''
 
| '''Summary'''
|Returns all text nodes of the database specified by <code>[[#Database Nodes|$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.
+
|Returns all text nodes of the database specified by <code>[[#Database Nodes|$db]]</code> that are located in between the {{Code|$min}} and {{Code|$max}} strings. If available, the value index is used to speed up evaluation.
 
|-
 
|-
 
| '''Examples'''
 
| '''Examples'''
 
|
 
|
* {{Mono|db:text-range("DB", "2000", "2001")}} returns all text nodes of the database {{Mono|DB}} that are found in between {{Mono|2000}} and {{Mono|2001}}.
+
* {{Code|db:text-range("DB", "2000", "2001")}} returns all text nodes of the database {{Code|DB}} that are found in between {{Code|2000}} and {{Code|2001}}.
 
|}
 
|}
  
Line 246: Line 246:
 
|-
 
|-
 
| width='90' | '''Signatures'''
 
| width='90' | '''Signatures'''
|{{Mono|'''db:add'''($db as item(), $input as item()) as empty-sequence()}}<br/>{{Mono|'''db:add'''($db as item(), $input as item(), $path as xs:string) as empty-sequence()}}
+
|{{Code|'''db:add'''($db as item(), $input as item()) as empty-sequence()}}<br/>{{Code|'''db:add'''($db as item(), $input as item(), $path as xs:string) as empty-sequence()}}
 
|-
 
|-
 
| '''Summary'''
 
| '''Summary'''
|Adds documents specified by {{Mono|$input}} to the database <code>[[#Database Nodes|$db]]</code> and the specified {{Mono|$path}}.
+
|Adds documents specified by {{Code|$input}} to the database <code>[[#Database Nodes|$db]]</code> and the specified {{Code|$path}}.
 
|-
 
|-
 
| '''Errors'''
 
| '''Errors'''
|'''[[XQuery Errors#Functions Errors|FODC0002]]''' is raised if {{Mono|$input}} is a string representing a path, which cannot be read.<br/>'''[[XQuery Errors#Update Errors|FOUP0001]]''' is raised if {{Mono|$input}} is neither string nor a document node.
+
|'''[[XQuery Errors#Functions Errors|FODC0002]]''' is raised if {{Code|$input}} is a string representing a path, which cannot be read.<br/>'''[[XQuery Errors#Update Errors|FOUP0001]]''' is raised if {{Code|$input}} is neither string nor a document node.
 
|-
 
|-
 
| '''Examples'''
 
| '''Examples'''
 
|
 
|
* {{Mono|db:add("DB", "/home/dir/doc.xml")}} adds the file {{Mono|/home/dir/doc.xml}} to the database {{Mono|DB}}.
+
* {{Code|db:add("DB", "/home/dir/doc.xml")}} adds the file {{Code|/home/dir/doc.xml}} to the database {{Code|DB}}.
* {{Mono|db:add("DB", "<a/>", "doc.xml")}} adds a document with content {{Mono|&lt;a/&gt;}} to the database {{Mono|DB}} under the name {{Mono|doc.xml}}.
+
* {{Code|db:add("DB", "<a/>", "doc.xml")}} adds a document with content {{Code|&lt;a/&gt;}} to the database {{Code|DB}} under the name {{Code|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}}.
+
* {{Code|db:add("DB", document { <a/> }, "doc.xml")}} adds the document node to the database {{Code|DB}} under the name {{Code|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}}.
+
* {{Code|db:add("DB", "/home/dir", "docs/dir")}} adds all documents in {{Code|/home/dir}} to the database {{Code|DB}} under the path {{Code|docs/dir}}.
 
|}
 
|}
  
Line 266: Line 266:
 
|-
 
|-
 
| width='90' | '''Signatures'''
 
| width='90' | '''Signatures'''
|{{Mono|'''db:delete'''($db as item(), $path as xs:string) as empty-sequence()}}
+
|{{Code|'''db:delete'''($db as item(), $path as xs:string) as empty-sequence()}}
 
|-
 
|-
 
| '''Summary'''
 
| '''Summary'''
|Deletes document(s), specified by {{Mono|$path}}, from the database <code>[[#Database Nodes|$db]]</code>.
+
|Deletes document(s), specified by {{Code|$path}}, from the database <code>[[#Database Nodes|$db]]</code>.
 
|-
 
|-
 
| '''Examples'''
 
| '''Examples'''
 
|
 
|
* {{Mono|db:delete("DB", "docs/dir/doc.xml")}} deletes the document {{Mono|docs/dir/doc.xml}} in the database {{Mono|DB}}.
+
* {{Code|db:delete("DB", "docs/dir/doc.xml")}} deletes the document {{Code|docs/dir/doc.xml}} in the database {{Code|DB}}.
* {{Mono|db:delete("DB", "docs/dir")}} deletes all documents with paths beginning with {{Mono|docs/dir}} in the database {{Mono|DB}}.
+
* {{Code|db:delete("DB", "docs/dir")}} deletes all documents with paths beginning with {{Code|docs/dir}} in the database {{Code|DB}}.
 
|}
 
|}
  
Line 281: Line 281:
 
|-
 
|-
 
| width='90' | '''Signatures'''
 
| width='90' | '''Signatures'''
|{{Mono|'''db:optimize'''($db as item()) as empty-sequence()}}<br/>{{Mono|'''db:optimize'''($db as item(), $all as xs:boolean) as empty-sequence()}}
+
|{{Code|'''db:optimize'''($db as item()) as empty-sequence()}}<br/>{{Code|'''db:optimize'''($db as item(), $all as xs:boolean) as empty-sequence()}}
 
|-
 
|-
 
| '''Summary'''
 
| '''Summary'''
|Optimizes the meta data and indexes of the database <code>[[#Database Nodes|$db]]</code>.<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 Nodes|$db]]</code>.<br/>If {{Code|$all}} is set to {{Code|true()}}, the complete database will be rebuilt.
 
|-
 
|-
 
| '''Errors'''
 
| '''Errors'''
|'''[[XQuery Errors#BaseX Errors|BASX0014]]''' is raised if an error occurs during optimizing the data structures.<br/>'''[[XQuery Errors#BaseX Errors|BASX0015]]''' is raised if the {{Mono|$all}} flag is set to {{Mono|true()}}, but the database is an in-memory database.<br/>'''[[XQuery Errors#BaseX Errors|BASX0016]]''' is raised if the database {{Mono|$db}} is in use by other user(s).
+
|'''[[XQuery Errors#BaseX Errors|BASX0014]]''' is raised if an error occurs during optimizing the data structures.<br/>'''[[XQuery Errors#BaseX Errors|BASX0015]]''' is raised if the {{Code|$all}} flag is set to {{Code|true()}}, but the database is an in-memory database.<br/>'''[[XQuery Errors#BaseX Errors|BASX0016]]''' is raised if the database {{Code|$db}} is in use by other user(s).
 
|-
 
|-
 
| '''Examples'''
 
| '''Examples'''
 
|
 
|
* {{Mono|db:optimize("DB")}} optimizes the database structures of the database {{Mono|DB}}.
+
* {{Code|db:optimize("DB")}} optimizes the database structures of the database {{Code|DB}}.
* {{Mono|db:optimize("DB", true())}} optimizes all database structures of the database {{Mono|DB}}.
+
* {{Code|db:optimize("DB", true())}} optimizes all database structures of the database {{Code|DB}}.
 
|}
 
|}
  
Line 299: Line 299:
 
|-
 
|-
 
| width='90' | '''Signatures'''
 
| width='90' | '''Signatures'''
|{{Mono|'''db:rename'''($db as item(), $path as xs:string, $newpath as xs:string) as empty-sequence()}}
+
|{{Code|'''db:rename'''($db as item(), $path as xs:string, $newpath as xs:string) as empty-sequence()}}
 
|-
 
|-
 
| '''Summary'''
 
| '''Summary'''
|Renames document(s), specified by {{Mono|$path}} to {{Mono|$newpath}} in the database <code>[[#Database Nodes|$db]]</code>.
+
|Renames document(s), specified by {{Code|$path}} to {{Code|$newpath}} in the database <code>[[#Database Nodes|$db]]</code>.
 
|-
 
|-
 
| '''Errors'''
 
| '''Errors'''
Line 309: Line 309:
 
| '''Examples'''
 
| '''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}}.
+
* {{Code|db:rename("DB", "docs/dir/doc.xml", "docs/dir/newdoc.xml")}} renames the document {{Code|docs/dir/doc.xml}} to {{Code|docs/dir/newdoc.xml}} in the database {{Code|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}}.
+
* {{Code|db:rename("DB", "docs/dir", "docs/newdir")}} renames all documents with paths beginning with {{Code|docs/dir}} to paths beginning with {{Code|docs/newdir}} in the database {{Code|DB}}.
 
|}
 
|}
  
Line 317: Line 317:
 
|-
 
|-
 
| width='90' | '''Signatures'''
 
| width='90' | '''Signatures'''
|{{Mono|'''db:replace'''($db as item(), $path as xs:string, $input as item()) as empty-sequence()}}
+
|{{Code|'''db:replace'''($db as item(), $path as xs:string, $input as item()) as empty-sequence()}}
 
|-
 
|-
 
| '''Summary'''
 
| '''Summary'''
|Replaces a document, specified by {{Mono|$path}}, in the database <code>[[#Database Nodes|$db]]</code> with the content of {{Mono|$input}}.
+
|Replaces a document, specified by {{Code|$path}}, in the database <code>[[#Database Nodes|$db]]</code> with the content of {{Code|$input}}.
 
|-
 
|-
 
| '''Errors'''
 
| '''Errors'''
|'''[[#Errors|BXDB0006]]''' is raised if {{Mono|$path}} is not a single document path.<br/>'''[[XQuery Errors#Functions Errors|FODC0002]]''' is raised if {{Mono|$input}} is a string representing a path, which cannot be read.<br/>'''[[XQuery Errors#Update Errors|FOUP0001]]''' is raised if {{Mono|$input}} is neither a string nor a document node.
+
|'''[[#Errors|BXDB0006]]''' is raised if {{Code|$path}} is not a single document path.<br/>'''[[XQuery Errors#Functions Errors|FODC0002]]''' is raised if {{Code|$input}} is a string representing a path, which cannot be read.<br/>'''[[XQuery Errors#Update Errors|FOUP0001]]''' is raised if {{Code|$input}} is neither a string nor a document node.
 
|-
 
|-
 
| '''Examples'''
 
| '''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}}.
+
* {{Code|db:replace("DB", "docs/dir/doc.xml", "/home/dir/doc.xml")}} replaces the content of the document {{Code|docs/dir/doc.xml}} in the database {{Code|DB}} with the content of the file {{Code|/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|&lt;a/&gt;}}.
+
* {{Code|db:replace("DB", "docs/dir/doc.xml", "<a/>")}} replaces the content of the document {{Code|docs/dir/doc.xml}} in the database {{Code|DB}} with {{Code|&lt;a/&gt;}}.
* {{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.
+
* {{Code|db:replace("DB", "docs/dir/doc.xml", document { <a/> })}} replaces the content of the document {{Code|docs/dir/doc.xml}} in the database {{Code|DB}} with the specified document node.
 
|}
 
|}
  
Line 336: Line 336:
 
|-
 
|-
 
| width='90' | '''Signatures'''
 
| width='90' | '''Signatures'''
|{{Mono|'''db:store'''($db as item(), $path as xs:string, $data as item()) as empty-sequence()}}
+
|{{Code|'''db:store'''($db as item(), $path as xs:string, $data as item()) as empty-sequence()}}
 
|-
 
|-
 
| '''Summary'''
 
| '''Summary'''
|Stores a binary resource specified by {{Mono|$data}} in the database specified by <code>[[#Database Nodes|$db]]</code> and the location specified by {{Mono|$path}}.
+
|Stores a binary resource specified by {{Code|$data}} in the database specified by <code>[[#Database Nodes|$db]]</code> and the location specified by {{Code|$path}}.
 
|-
 
|-
 
| '''Errors'''
 
| '''Errors'''
Line 346: Line 346:
 
| '''Examples'''
 
| '''Examples'''
 
|
 
|
* {{Mono|db:store("DB", "video/sample.mov", file:read-binary('video.mov'))}} stores the addressed video file at the specified location.
+
* {{Code|db:store("DB", "video/sample.mov", file:read-binary('video.mov'))}} stores the addressed video file at the specified location.
 
|}
 
|}
  
Line 355: Line 355:
 
|-
 
|-
 
| width='90' | '''Signatures'''
 
| width='90' | '''Signatures'''
|{{Mono|'''db:output'''($data as item()*) as empty-sequence()}}
+
|{{Code|'''db:output'''($data as item()*) as empty-sequence()}}
 
|-
 
|-
 
| '''Summary'''
 
| '''Summary'''
Line 362: Line 362:
 
| '''Examples'''
 
| '''Examples'''
 
|
 
|
* {{Mono|db:output("Prices have been deleted."), delete node //price}} deletes all {{Mono|price}} elements in a database and returns an info message.
+
* {{Code|db:output("Prices have been deleted."), delete node //price}} deletes all {{Code|price}} elements in a database and returns an info message.
 
|}
 
|}
  
Line 371: Line 371:
 
|-
 
|-
 
| width='90' | '''Signatures'''
 
| width='90' | '''Signatures'''
|{{Mono|'''db:exists'''($db as item()) as xs:boolean}}<br/>{{Mono|'''db:exists'''($db as item(), $path as xs:string) as xs:boolean}}
+
|{{Code|'''db:exists'''($db as item()) as xs:boolean}}<br/>{{Code|'''db:exists'''($db as item(), $path as xs:string) as xs:boolean}}
 
|-
 
|-
 
| '''Summary'''
 
| '''Summary'''
|Checks if the database specified by <code>[[#Database Nodes|$db]]</code> 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 Nodes|$db]]</code> or the resource specified by {[Mono|$path}} exists. {{Code|false}} is returned if a database directory has been addressed.
 
|-
 
|-
 
| '''Examples'''
 
| '''Examples'''
 
|
 
|
* {{Mono|db:exists("DB")}} returns {{Mono|true}} if the database {{Mono|DB}} exists.
+
* {{Code|db:exists("DB")}} returns {{Code|true}} if the database {{Code|DB}} exists.
* {{Mono|db:exists("DB", "resource")}} returns {{Mono|true}} if {{Mono|resource}} is an XML document or a raw file.
+
* {{Code|db:exists("DB", "resource")}} returns {{Code|true}} if {{Code|resource}} is an XML document or a raw file.
 
|}
 
|}
  
Line 386: Line 386:
 
|-
 
|-
 
| width='90' | '''Signatures'''
 
| width='90' | '''Signatures'''
|{{Mono|'''db:is-raw'''($db as item(), $path as xs:string) as xs:boolean}}
+
|{{Code|'''db:is-raw'''($db as item(), $path as xs:string) as xs:boolean}}
 
|-
 
|-
 
| '''Summary'''
 
| '''Summary'''
|Checks if the specified resource in the database <code>[[#Database Nodes|$db]]</code> and the path {{Mono|$path}} exists, and if it is a raw file.
+
|Checks if the specified resource in the database <code>[[#Database Nodes|$db]]</code> and the path {{Code|$path}} exists, and if it is a raw file.
 
|-
 
|-
 
| '''Examples'''
 
| '''Examples'''
 
|
 
|
* {{Mono|db:is-raw("DB", "music/01.mp3")}} returns {{Mono|true}}.
+
* {{Code|db:is-raw("DB", "music/01.mp3")}} returns {{Code|true}}.
 
|}
 
|}
  
Line 400: Line 400:
 
|-
 
|-
 
| width='90' | '''Signatures'''
 
| width='90' | '''Signatures'''
|{{Mono|'''db:is-xml'''($db as item(), $path as xs:string) as xs:boolean}}
+
|{{Code|'''db:is-xml'''($db as item(), $path as xs:string) as xs:boolean}}
 
|-
 
|-
 
| '''Summary'''
 
| '''Summary'''
|Checks if the specified resource in the database <code>[[#Database Nodes|$db]]</code> and the path {{Mono|$path}} exists, and if it is an XML document.
+
|Checks if the specified resource in the database <code>[[#Database Nodes|$db]]</code> and the path {{Code|$path}} exists, and if it is an XML document.
 
|-
 
|-
 
| '''Examples'''
 
| '''Examples'''
 
|
 
|
* {{Mono|db:is-xml("DB", "dir/doc.xml")}} returns {{Mono|true}}.
+
* {{Code|db:is-xml("DB", "dir/doc.xml")}} returns {{Code|true}}.
 
|}
 
|}
  
Line 414: Line 414:
 
|-
 
|-
 
| width='90' | '''Signatures'''
 
| width='90' | '''Signatures'''
|{{Mono|'''db:content-type'''($db as item(), $path as xs:string) as xs:string}}
+
|{{Code|'''db:content-type'''($db as item(), $path as xs:string) as xs:string}}
 
|-
 
|-
 
| '''Summary'''
 
| '''Summary'''
|Retrieves the content type of a resource in the database <code>[[#Database Nodes|$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.
+
|Retrieves the content type of a resource in the database <code>[[#Database Nodes|$db]]</code> and the path {{Code|$path}}.<br/>The file extension is used to recognize the content-type of a resource stored in the database. Content-type {{Code|application/xml}} will be returned for any XML document stored in the database, regardless of its file name extension.
 
|-
 
|-
 
| '''Errors'''
 
| '''Errors'''
Line 424: Line 424:
 
| '''Examples'''
 
| '''Examples'''
 
|
 
|
* {{Mono|db:content-type("DB", "docs/doc01.pdf")}} returns {{Mono|application/pdf}}.
+
* {{Code|db:content-type("DB", "docs/doc01.pdf")}} returns {{Code|application/pdf}}.
* {{Mono|db:content-type("DB", "docs/doc01.xml")}} returns {{Mono|application/xml}}.
+
* {{Code|db:content-type("DB", "docs/doc01.xml")}} returns {{Code|application/xml}}.
* {{Mono|db:content-type("DB", "docs/doc01")}} returns {{Mono|application/xml}}, if {{Mono|db:is-xml("DB", "docs/doc01")}} returns {{Mono|true}}.
+
* {{Code|db:content-type("DB", "docs/doc01")}} returns {{Code|application/xml}}, if {{Code|db:is-xml("DB", "docs/doc01")}} returns {{Code|true}}.
 
|}
 
|}
  
Line 433: Line 433:
 
|-
 
|-
 
| width='90' | '''Signatures'''
 
| width='90' | '''Signatures'''
|{{Mono|'''db:event'''($name as xs:string, $query as item()) as empty-sequence()}}
+
|{{Code|'''db:event'''($name as xs:string, $query as item()) as empty-sequence()}}
 
|-
 
|-
 
| '''Summary'''
 
| '''Summary'''
|Executes a {{Mono|$query}} and sends the resulting value to all clients watching the [[Events|Event]] with the specified {{Mono|$name}}. The query may also perform updates; no event will be sent to the client that fired the event.
+
|Executes a {{Code|$query}} and sends the resulting value to all clients watching the [[Events|Event]] with the specified {{Code|$name}}. The query may also perform updates; no event will be sent to the client that fired the event.
 
|-
 
|-
 
| '''Errors'''
 
| '''Errors'''
Line 464: Line 464:
 
|-
 
|-
 
|<code>BXDB0006</code>
 
|<code>BXDB0006</code>
|A database path addressed with {{Mono|doc()}} contains more than one document.
+
|A database path addressed with {{Code|doc()}} contains more than one document.
 
|-
 
|-
 
|<code>BXDB0007</code>
 
|<code>BXDB0007</code>

Revision as of 15:12, 26 May 2012

This XQuery Module contains functions for processing databases from within XQuery. Existing databases can be opened and listed, its contents can be directly accessed, documents can be added to and removed, etc. All functions are introduced with the db: prefix, which is linked to the statically declared http://basex.org/modules/db namespace.

Conventions

All functions in this module are assigned to the http://basex.org/modules/db namespace, which is statically bound to the db prefix.
All errors are assigned to the http://basex.org/errors namespace, which is statically bound to the bxerr prefix.

Database Nodes

Many function signatures in this and other modules share $db as argument to reference an existing database. The argument may either be 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:

  • BXDB0001 is raised if $db references an XML node that is not stored in a database, or is no database fragment.
  • BXDB0002 is raised if the addressed database cannot be opened.

Last not but least, the argument may also reference a BaseX-specific database fragment. All XML fragments can be converted to database fragments by applying the transform expression on an XML fragment:

copy $c := element hello { 'world' }
modify ()
return db:text($c, 'world')

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("docs") returns the names of all documents from the database named docs.

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:list-details("docs") returns the names plus additional data of all documents from the database named docs.

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("docs") returns all documents from the database named docs.
  • db:open("docs", "one") returns all documents from the database named docs in the subpath one.

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.
Errors BXDB0009 is raised if the specified id value 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.
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 BXDB0009 is raised if the specified pre value does not exist in the database.
Examples
  • db:open-pre("docs", 0) returns the first database node from the database named docs.

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("DB", "QUERY", "id")/.. returns the parents of all id attribute nodes of the database DB that have QUERY as string value.

db:attribute-range

Template:Mark

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:attribute-range("DB", "id456", "id473", 'id') returns all @id attributes of the database DB that have a string value in between id456 and id473.

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 BXDB0004 is raised if the full-text index is not available.
Examples
  • db:fulltext("DB", "QUERY") returns all text nodes of the database DB that contain the string QUERY.

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:node-pre(doc("input")) returns 0 if the database input contains a single document.

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 BXDB0003 is raised if the databse is not persistent (stored on disk).
FODC0002 is raised if the addressed resource cannot be retrieved.
FODC0007 is raised if the specified path is invalid.
Examples
  • declare option output:method 'raw';
    db:retrieve("DB", "music/01.mp3")
    returns the specified audio file as raw data.

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("DB", "QUERY")/.. returns the parents of all text nodes of the database DB that match the string QUERY.

db:text-range

Template:Mark

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
  • db:text-range("DB", "2000", "2001") returns all text nodes of the database DB that are found in between 2000 and 2001.

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 neither string nor a document node.
Examples
  • db:add("DB", "/home/dir/doc.xml") adds the file /home/dir/doc.xml to the database DB.
  • db:add("DB", "<a/>", "doc.xml") adds a document with content <a/> to the database DB under the name doc.xml.
  • db:add("DB", document { <a/> }, "doc.xml") adds the document node to the database DB under the name doc.xml.
  • db:add("DB", "/home/dir", "docs/dir") adds all documents in /home/dir to the database DB under the path docs/dir.

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:delete("DB", "docs/dir/doc.xml") deletes the document docs/dir/doc.xml in the database DB.
  • db:delete("DB", "docs/dir") deletes all documents with paths beginning with docs/dir in the database DB.

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:optimize("DB") optimizes the database structures of the database DB.
  • db:optimize("DB", true()) optimizes all database structures of the database DB.

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 BXDB0008 is raised if new document names would be empty.
Examples
  • db:rename("DB", "docs/dir/doc.xml", "docs/dir/newdoc.xml") renames the document docs/dir/doc.xml to docs/dir/newdoc.xml in the database DB.
  • db:rename("DB", "docs/dir", "docs/newdir") renames all documents with paths beginning with docs/dir to paths beginning with docs/newdir in the database DB.

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 BXDB0006 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 neither a string nor a document node.
Examples
  • db:replace("DB", "docs/dir/doc.xml", "/home/dir/doc.xml") replaces the content of the document docs/dir/doc.xml in the database DB with the content of the file /home/dir/doc.xml.
  • db:replace("DB", "docs/dir/doc.xml", "<a/>") replaces the content of the document docs/dir/doc.xml in the database DB with <a/>.
  • db:replace("DB", "docs/dir/doc.xml", document { <a/> }) replaces the content of the document docs/dir/doc.xml in the database DB with the specified document node.

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 BXDB0003 is raised if the databse is not persistent (stored on disk).
FODC0007 is raised if the specified path is invalid.
FOUP0002 is raised if the resource cannot be stored at the specified location.
Examples
  • db:store("DB", "video/sample.mov", file:read-binary('video.mov')) stores the addressed video file at the specified location.

db:output

Template:Mark

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
  • db:output("Prices have been deleted."), delete node //price deletes all price elements in a database and returns an info message.

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:exists("DB") returns true if the database DB exists.
  • db:exists("DB", "resource") returns true if resource is an XML document or a raw file.

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-raw("DB", "music/01.mp3") returns true.

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:is-xml("DB", "dir/doc.xml") returns true.

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:content-type("DB", "docs/doc01.pdf") returns application/pdf.
  • db:content-type("DB", "docs/doc01.xml") returns application/xml.
  • db:content-type("DB", "docs/doc01") returns application/xml, if db:is-xml("DB", "docs/doc01") returns true.

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 BXDB0010 is raised if the specified event is unknown.
SEPM0016 is raised if serialization errors occurred while sending the value.

Errors

Code Description
BXDB0001 A referenced XML node is no database node, i.e. is neither stored in a database nor a database fragment.
BXDB0002 An I/O error occurred while opening a database.
BXDB0003 The addressed database is not persistent (stored on disk).
BXDB0004 The database lacks an index structure required by the called function.
BXDB0005 A query is expected to exclusively return database nodes of a single database.
BXDB0006 A database path addressed with doc() contains more than one document.
BXDB0007 A database cannot be updated because it is opened by another process.
BXDB0008 Database paths cannot be renamed to empty strings.
BXDB0009 The addressed database id or pre value is out of range.
BXDB0010 The specified event is unknown.

Changelog

Version 7.2.1

Version 7.1

Version 7.0