Difference between revisions of "Database Module"

From BaseX Documentation
Jump to navigation Jump to search
 
(488 intermediate revisions by 14 users not shown)
Line 1: Line 1:
<p>This module contains methods to list and open [[database|databases]], explicitly apply available [[Indexes|index structures]], or get information on the database structure. All functions are introduced with the <code>db:</code> prefix.</p>
+
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.
==db:system==
 
  
{|
+
=Conventions=
|-
+
 
| width="90" | <b>Signatures</b>
+
All functions and errors in this module are assigned to the <code><nowiki>http://basex.org/modules/db</nowiki></code> namespace, which is statically bound to the {{Code|db}} prefix.<br/>
|<code><b>db:system</b>() as xs:string*</code>
+
 
|-
+
==Database Nodes==
| <b>Summary</b>
+
 
| Lists information on the database system.
+
In BaseX, two internal representations exist for XML nodes:
|-
+
 
| <b>Rules</b>
+
1. Database nodes are:
| This function returns general information on the database system, such as the database path and current database settings.<br />
+
* stored in a persistent database on disk;
 +
* nodes of a document that has been generated temporarily with {{Code|fn:doc}}, {{Code|fn:parse-xml}} and other functions; or
 +
* the result of a [[XQuery Update#Main-Memory Updates|main-memory update]].
 +
2. Fragments are similar to DOM structures. They are created when XQuery node constructors are used (<code><a/></code>, <code>element a { }</code>, etc.).
 +
 
 +
Fragments require less memory for small XML structures, but database nodes are more efficient for larger amounts of data. Fragments nodes can be converted to database nodes by applying a [[XQuery_Update#Main-Memory Updates|main-memory update]] with an empty body to a node:
 +
 
 +
<pre lang='xquery'>
 +
<xml>hello world</xml> update { }
 +
</pre>
 +
 
 +
==Updating Functions==
 +
 
 +
Various functions in this module are ''updating''. Updating functions will not be immediately executed, but queued on the [[XQuery Update#Pending Update List|Pending Update List]], and processed after the remaining query has been evaluated. This means that the order in which the functions are specified in the query often does not reflect the order in which they will eventually be executed.
 +
 
 +
=General Functions=
 +
 
 +
==db:system==
 +
 
 +
{| width='100%'
 +
|- valign="top"
 +
| width='120' | '''Signature'''
 +
|<pre>db:system() as element(system)</pre>
 +
|- valign="top"
 +
| '''Summary'''
 +
|Returns general information on the database system the current values of all global and local [[Options]]. The {{Command|INFO}} command returns similar output.
 
|}
 
|}
  
==db:list==  
+
==db:option==
{|
+
 
|-
+
{| width='100%'
| width="90" | <b>Signatures</b>
+
|- valign="top"
|<code><b>db:list</b>() as xs:string*</code>  
+
| width='120' | '''Signature'''
|-
+
|<pre>db:option(
| <b>Summary</b>
+
  $name  as xs:string
| Lists all databases.
+
) as xs:string</pre>
|-
+
|- valign="top"
| <b>Rules</b>
+
| '''Summary'''
| This function returns a <code>xs:string</code> sequence with the names of all databases.<br />
+
|Returns the current value (string, integer, boolean, map) of a global or local [[Options|Option]] with the specified {{Code|$name}}. The {{Command|SHOW OPTIONS}} command returns similar output.
 +
|- valign="top"
 +
| '''Errors'''
 +
|{{Error|option|#Errors}} the specified option is unknown.
 +
|- valign="top"
 +
| '''Examples'''
 +
|
 +
* <code>db:option('dbpath')</code> returns the database path string.
 +
* <code>db:option('serializer')</code> returns a map with the current serialization parameters.
 +
* <code>declare option db:stripws 'true'; db:option('stripws')</code> returns the locally assigned value.
 
|}
 
|}
  
==db:open==  
+
==db:info==
{|
+
 
|-
+
{| width='100%'
| width="90" | <b>Signatures</b>
+
|- valign="top"
|<code><b>db:open</b>($name as xs:string) as document-node()</code>  
+
| width='120' | '''Signature'''
|-
+
|<pre>db:info(
| <b>Summary</b>
+
  $database  as xs:string
| Returns documents from a database.
+
) as element(database)</pre>
|-
+
|- valign="top"
| <b>Rules</b>
+
| '''Summary'''
| This function opens the documents of the database specified by <code>$name</code>. The name of the database may be extended by a collection path.<br />
+
|Returns meta information on the specified {{Code|$database}}. The output is similar to the {{Command|INFO DB}} command.
|-
+
|- valign="top"
| <b>Examples</b>
+
| '''Errors'''
| The expression <code>db:open("docs")</code> returns all documents from the database named <code>docs</code>.<br />The expression <code>db:open("docs/one")</code> returns all documents from the database named <code>docs</code> in the subpath <code>one</code>.<br />
+
|{{Error|open|#Errors}} the addressed database does not exist or could not be opened.
|-
 
| <b>Errors</b>
 
| <b>[BASX0003]</b> is raised if the database is not found.<br />
 
 
|}
 
|}
  
==db:open-pre==  
+
==db:property==
{|
+
 
|-
+
{| width='100%'
| width="90" | <b>Signatures</b>
+
|- valign="top"
|<code> <b>db:open-pre</b>($name as xs:string, $pre as xs:integer) as node()</code>  
+
| width='120' | '''Signature'''
|-
+
|<pre>db:property(
| <b>Summary</b>
+
  $database  as xs:string,
| Returns a specific database node.
+
  $name      as xs:string
|-
+
) as xs:anyAtomicType</pre>
| <b>Rules</b>
+
|- valign="top"
| This function opens the database specified by <code>$name</code> and returns the node with the <i>pre</i> value specified by <code>$pre</code>.<br />
+
| '''Summary'''
|-
+
|Returns the value (string, boolean, integer) of a property with the specified {{Code|$name}} in the specified {{Code|$database}}. The available properties are the ones returned by {{Function||db:info}}.
| <b>Examples</b>
+
|- valign="top"
| The expression <code>db:open-pre("docs", 0)</code> returns the first database node from the database named <code>docs</code>.<br />
+
| '''Errors'''
|-
+
|{{Error|property|#Errors}} the specified property is unknown.
| <b>Errors</b>
+
|- valign="top"
| <b>[BASX0004]</b> is raised if the specified <code>$pre</code> value does not exist in the database.<br />
+
| '''Examples'''
 +
|
 +
* <code>db:property('db', 'size')</code> returns the number of bytes occupied by the database <code>db</code>.
 +
* <code>db:property('xmark', 'textindex')</code> indicates if the <code>xmark</code> database has a text index.
 +
* <code>db:property('discogs', 'uptodate')</code> indicates if the database statistics and index structures of the <code>discogs</code> database are up-to-date.
 
|}
 
|}
  
==db:open-id==  
+
==db:list==
{|
+
 
|-
+
{| width='100%'
| width="90" | <b>Signatures</b>
+
|- valign="top"
|<code><b>db:open-id</b>($name as xs:string, $id as xs:integer) as node()</code>  
+
| width='120' | '''Signature'''
|-
+
|<pre>db:list(
| <b>Summary</b>
+
  $database  as xs:string := (),
| Returns a specific database node.
+
  $path      as xs:string  := ()
|-
+
) as xs:string*</pre>
| <b>Rules</b>
+
|- valign="top"
| This function opens the database specified by <code>$name</code> and returns the node with the <i>id</i> value specified by <code>$id</code>.<br />In contrast to the <i>pre</i> value, the <i>id</i> will remain valid after update operations.<br />
+
| '''Summary'''
|-
+
|Without arguments, the names of all databases are returned that are accessible to the current user. If {{Code|$database}} is specified, paths to all resources of this database are returned. The results can be restricted to resources starting with the specified {{Code|$path}}.
| <b>Errors</b>
+
|- valign="top"
| <b>[BASX0004]</b> is raised if the specified <code>$id</code> does not exist in the database.<br />
+
| '''Errors'''
 +
|{{Error|open|#Errors}} the addressed database does not exist or could not be opened.
 +
|- valign="top"
 +
| '''Examples'''
 +
|
 +
* {{Code|db:list("docs")}} returns the names of all documents of a database named {{Code|docs}}.
 
|}
 
|}
  
==db:text-index==  
+
==db:list-details==
{|
+
 
|-
+
{| width='100%'
| width="90" | <b>Signatures</b>
+
|- valign="top"
|<code><b>db:text-index</b>($string as item()) as text()*</code>  
+
| width='120' | '''Signature'''
|-
+
|<pre>db:list-details(
| <b>Summary</b>
+
  $database  as xs:string := (),
| Returns results from a text [[indexes|index]] request.
+
  $path      as xs:string  := ()
|-
+
) as element()*</pre>
| <b>Rules</b>
+
|- valign="top"
| This function accesses the text index and returns all text nodes that have <code>$string</code> as string value.<br />
+
| '''Summary'''
|-
+
|Without arguments, an element is returned for each database that is accessible to the current user:
| <b>Errors</b>
+
* An element has a value, which is the name of the database, and several attributes, which contain the number of stored resources, the modification date, the database size on disk (measured in bytes), and a path to the original database input.
|<b>[BASX0001]</b> is raised if the index is not available.<br /> <b>[BASX0002]</b> is raised if the context item does not represent a database node.<br />
+
If {{Code|$database}} is specified, an element for each resource in this database is returned:
 +
* An element has a value, which is the name of the resource, and several attributes, which contain the content type, the modification date, the raw flag (which indicates if the resource is binary or XML), and the size of a resource.
 +
* The value of the size attribute depends on the resource type: for documents, it represents the number of nodes; for binary data, it represents the file size (measured in bytes).
 +
* The results can be restricted to resources starting with the specified {{Code|$path}}.
 +
|- valign="top"
 +
| '''Errors'''
 +
|{{Error|open|#Errors}} the addressed database does not exist or could not be opened.
 +
|- valign="top"
 +
| '''Examples'''
 +
|
 +
* {{Code|db:list-details("shop")}} returns the names plus additional info on all resources of a database named {{Code|shop}}.
 
|}
 
|}
  
==db:attribute-index==  
+
==db:dir==
{|
+
 
|-
+
{| width='100%'
| width="90" | <b>Signatures</b>
+
|- valign="top"
|<code><b>db:attribute-index</b>($string as item()) as attribute()*</code> <br /> <code> <b>db:attribute-index</b>($string as item(), $name as xs:string) as attribute()*</code>  
+
| width='120' | '''Signature'''
|-
+
|<pre>db:dir(
| <b>Summary</b>
+
  $database  as xs:string,
| Returns results from a attribute index request.
+
  $path      as xs:string
|-
+
) as element()*</pre>
| <b>Rules</b>
+
|- valign="top"
| This function accesses the attribute index and returns all attribute nodes that have <code>$string</code> as string value.<br />If <code>$name</code> is specified, the resulting attribute nodes are filtered by their attribute name.<br />
+
| '''Summary'''
|-
+
|Returns metadata on all directories and resources of a {{Code|$database}} in the specified {{Code|$path}}. Two types of elements are returned:
| <b>Errors</b>
+
* {{Code|resource}} represents a resource. The element value is the directory path; content type, modification date, raw flag (which indicates if the resource is binary or XML), and size of the resource are returned as attributes.
|<b>[BASX0001]</b> is raised if the index is not available.<br /><b>[BASX0002]</b> is raised if the context item does not represent a database node.
+
* {{Code|dir}} represents a directory. The element value is the directory path; the modification date is returned in an attribute.
 +
The directories are not stored in the internal database layout. Instead, they result implicitly from the paths of stored resources.
 +
|- valign="top"
 +
| '''Errors'''
 +
|{{Error|open|#Errors}} the addressed database does not exist or could not be opened.<br/>{{Error|path|#Errors}} the specified path is invalid.
 +
|- valign="top"
 +
| '''Examples'''
 +
|
 +
* {{Code|db:dir('shop', 'books')}} returns all entries of the {{Code|books}} directory of a {{Code|shop}} database.
 
|}
 
|}
  
==db:fulltext-index==  
+
=Read Operations=
{|
+
 
|-
+
==db:get==
| width="90" | <b>Signatures</b>
+
 
|<code><b>db:fulltext-index</b>($text as xs:string) as text()</code>  
+
{| width='100%'
|-
+
|- valign="top"
| <b>Summary</b>
+
| width='120' | '''Signature'''
| Returns results from a full-text index request.
+
|<pre>db:get(
|-
+
  $database  as xs:string,
| <b>Rules</b>
+
  $path      as xs:string := ()
| This function accesses the full-text index and returns all text nodes that contain <code>$text</code>.<br />
+
) as document-node()*</pre>
|-
+
|- valign="top"
| <b>Errors</b>
+
| '''Summary'''
|<b>[BASX0001]</b> is raised if the index is not available.<br /> <b>[BASX0002]</b> is raised if the context item does not represent a database node.<br />
+
|Returns all documents from the specified {{Code|$database}}, or only documents matching the specified {{Code|$path}}.
 +
|- valign="top"
 +
| '''Errors'''
 +
|{{Error|open|#Errors}} the addressed database does not exist or could not be opened.
 +
|- valign="top"
 +
| '''Examples'''
 +
|
 +
* {{Code|db:get('docs')}} returns all documents from the database named {{Code|docs}}.
 +
* {{Code|db:get('db', 'one')}} returns all documents from the database named {{Code|db}} located in the path {{Code|one}}.
 +
* <code>for $i in 1 to 3 return db:get('db' || $i)//item</code> returns all item elements from the databases {{Code|db1}}, {{Code|db2}} and {{Code|db3}}.
 
|}
 
|}
  
==db:info==  
+
==db:get-pre==
{|
+
 
|-
+
{| width='100%'
| width="90" | <b>Signatures</b>
+
|- valign="top"
|<code> <b>db:info</b>() as xs:string</code>  
+
| width='120' | '''Signature'''
|-
+
|<pre>db:get-pre(
| <b>Summary</b>
+
  $database  as xs:string,
| Returns database information.
+
  $values    as xs:integer*
|-
+
) as node()*</pre>
| <b>Rules</b>
+
|- valign="top"
| This function returns information on the database that is referenced by the current context item.<br />
+
| '''Summary'''
|-
+
|Returns all nodes from a {{Code|$database}} with the specified PRE {{Code|values}} in [[Utility Module#util:ddo|distinct document order]].<br/>The [[Node Storage#PRE Value|PRE value]] provides very fast access to an existing database node, but it will change whenever a node with a smaller ''pre'' values is added to or deleted from a database.
| <b>Errors</b>
+
|- valign="top"
|<b>[BASX0002]</b> is raised if the context item does not represent a database node.<br />
+
| '''Errors'''
 +
|{{Error|open|#Errors}} the addressed database does not exist or could not be opened.<br/>{{Error|range|#Errors}} the specified PRE value does not exist in the database.
 +
|- valign="top"
 +
| '''Examples'''
 +
|
 +
* {{Code|db:get-pre("docs", 0)}} returns the first database node from the database named {{Code|docs}}.
 +
|}
 +
 
 +
==db:get-id==
 +
 
 +
{| width='100%'
 +
|- valign="top"
 +
| width='120' | '''Signature'''
 +
|<pre>db:get-id(
 +
  $database  as xs:string,
 +
  $values    as xs:integer*
 +
) as node()*</pre>
 +
|- valign="top"
 +
| '''Summary'''
 +
|Returns all nodes from a {{Code|$database}} with the specified ID {{Code|$values}} in [[Utility Module#util:ddo|distinct document order]].<br/>Each database node has a ''persistent'' [[Node Storage#ID Value|ID value]]. Access to the node ID can be sped up by turning on the {{Option|UPDINDEX}} option.
 +
|- valign="top"
 +
| '''Errors'''
 +
|{{Error|open|#Errors}} the addressed database does not exist or could not be opened.<br/>{{Error|range|#Errors}} the specified ID value does not exist in the database.
 +
|}
 +
 
 +
==db:get-binary==
 +
 
 +
{| width='100%'
 +
|- valign="top"
 +
| width='120' | '''Signature'''
 +
|<pre>db:get-binary(
 +
  $database  as xs:string,
 +
  $path      as xs:string
 +
) as item()</pre>
 +
|- valign="top"
 +
| '''Summary'''
 +
|Returns a map with the paths and binary items of all resources in the specified {{Code|$database}}. A single {{Code|xs:base64Binary}} item is returned if a {{Code|$path}} is specified. All items are [[Lazy Module|lazy]], i.e., the actual data will only be retrieved if it is processed.
 +
|- valign="top"
 +
| '''Errors'''
 +
|{{Error|open|#Errors}} the addressed database does not exist or could not be opened.<br/>{{Error|mainmem|#Errors}} the database is not ''persistent'' (stored on disk).
 +
|- valign="top"
 +
| '''Examples'''
 +
|
 +
* {{Code|db:get-binary('DB', 'music/01.mp3')}} returns the specified audio file as raw data.
 +
* <code><nowiki>stream:materialize(db:get-binary('DB', 'music/01.mp3'))</nowiki></code> materializes the streamable result in main-memory before returning it.
 +
* <code><nowiki>convert:binary-to-string(db:get-binary('DB', 'info.txt'), 'UTF-8')</nowiki></code> converts a binary database resource as UTF-8 text and returns a string.
 +
|}
 +
 
 +
==db:get-value==
 +
 
 +
{| width='100%'
 +
|- valign="top"
 +
| width='120' | '''Signature'''
 +
|<pre>db:get-value(
 +
  $database  as xs:string,
 +
  $path      as xs:string
 +
) as item()*</pre>
 +
|- valign="top"
 +
| '''Summary'''
 +
|Returns a map with the paths and values of all resources in the specified {{Code|$database}}. A single value is returned if a {{Code|$path}} is specified.
 +
|- valign="top"
 +
| '''Errors'''
 +
|{{Error|open|#Errors}} the addressed database does not exist or could not be opened.<br/>{{Error|mainmem|#Errors}} the database is not ''persistent'' (stored on disk).
 +
|- valign="top"
 +
| '''Examples'''
 +
|
 +
* {{Code|db:get-value('DB', 'sequence')}} returns the specified sequence.
 
|}
 
|}
  
==db:index-info==  
+
==db:node-pre==
{|
+
 
|-
+
{| width='100%'
| width="90" | <b>Signatures</b>
+
|- valign="top"
| <code> <b>db:index-info</b>($type as xs:string) as xs:string</code>  
+
| width='120' | '''Signature'''
|-
+
|<pre>db:node-pre(
| <b>Summary</b>
+
  $nodes  as node()*
| Returns database index information.
+
) as xs:integer*</pre>
|-
+
|- valign="top"
| <b>Rules</b>
+
| '''Summary'''
| This function returns information on an index of the database that is referenced by the current context item.<br /> <code>$type</code> 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>.<br />
+
|Returns the PRE values of the specified {{Code|$nodes}}, which must all be [[#Database Nodes|database nodes]].<br/>The [[Node Storage#PRE Value|PRE value]] provides very fast access to an existing database node, but it will change whenever a node with a smaller ''pre'' values is added to or deleted from a database.
|-
+
|- valign="top"
| <b>Errors</b>
+
| '''Errors'''
| <b>[BASX0001]</b> is raised if the specified index is not available.<br /> <b>[BASX0002]</b> is raised if the context item does not represent a database node.<br />
+
|{{Error|node|#Errors}} {{Code|$nodes}} contains a node which is not stored in a database.
 +
|- valign="top"
 +
| '''Examples'''
 +
|
 +
* {{Code|db:node-pre(doc("input"))}} returns {{Code|0}} if the database {{Code|input}} contains a single document.
 
|}
 
|}
[[Category:Commands]]
+
 
 +
==db:node-id==
 +
 
 +
{| width='100%'
 +
|- valign="top"
 +
| width='120' | '''Signature'''
 +
|<pre>db:node-id(
 +
  $nodes  as node()*
 +
) as xs:integer*</pre>
 +
|- valign="top"
 +
| '''Summary'''
 +
|Returns the ID values of the specified {{Code|$nodes}}, which must all be [[#Database Nodes|database nodes]].<br/>Each database node has a ''persistent'' [[Node Storage#ID Value|ID value]]. Access to the node id can be sped up by turning on the {{Option|UPDINDEX}} option.
 +
|- valign="top"
 +
| '''Errors'''
 +
|{{Error|node|#Errors}} {{Code|$nodes}} contains a node which is not stored in a database.
 +
|}
 +
 
 +
==db:export==
 +
 
 +
{| width='100%'
 +
|- valign="top"
 +
| width='120' | '''Signature'''
 +
|<pre>db:export(
 +
  $database  as xs:string,
 +
  $path      as xs:string,
 +
  $options  as map(*)?    := map { }
 +
) as empty-sequence()</pre>
 +
|- valign="top"
 +
| '''Summary'''
 +
|Exports the specified {{Code|$database}} to the specified file {{Code|$path}}. Existing files will be overwritten.<br/>The {{Code|$options}} argument contains [[Serialization|serialization parameters]] (see [https://www.w3.org/TR/xpath-functions-31/#func-serialize fn:serialize]).
 +
|- valign="top"
 +
| '''Errors'''
 +
|{{Error|open|#Errors}} the addressed database does not exist or could not be opened.
 +
|- valign="top"
 +
| '''Examples'''
 +
| Export all files as text:<br/>
 +
<pre lang='xquery'>
 +
db:export("DB", "/home/john/xml/texts", map { 'method': 'text' })
 +
</pre>
 +
The following code can be used to export parts of the database:
 +
<pre lang='xquery'>
 +
let $target := '/home/john/xml/target'
 +
for $doc in db:get('DB', 'collection')
 +
let $path := $target || db:path($doc)
 +
return (
 +
  file:create-dir(file:parent($path)),
 +
  file:write($path, $doc)
 +
)
 +
</pre>
 +
|}
 +
 
 +
=Value Indexes=
 +
 
 +
==db:text==
 +
 
 +
{| width='100%'
 +
|- valign="top"
 +
| width='120' | '''Signature'''
 +
|<pre>db:text(
 +
  $database  as xs:string,
 +
  $values    as xs:string*
 +
) as text()*</pre>
 +
|- valign="top"
 +
| '''Summary'''
 +
|Returns all text nodes of a {{Code|$database}} that match one of the specified {{Code|$values}} and that are stored in the text index.
 +
|- valign="top"
 +
| '''Errors'''
 +
|{{Error|open|#Errors}} the addressed database does not exist or could not be opened.<br/>{{Error|no-index|#Errors}} the index is not available.<br/>
 +
|- valign="top"
 +
| '''Examples'''
 +
|
 +
* {{Code|db:text("DB", "QUERY")/..}} returns the parents of all text nodes of the database {{Code|DB}} that match the string {{Code|QUERY}}.
 +
|}
 +
 
 +
==db:text-range==
 +
 
 +
{| width='100%'
 +
|- valign="top"
 +
| width='120' | '''Signature'''
 +
|<pre>db:text-range(
 +
  $database  as xs:string,
 +
  $min      as xs:string,
 +
  $max      as xs:string
 +
) as text()*</pre>
 +
|- valign="top"
 +
| '''Summary'''
 +
|Returns all text nodes of a {{Code|$database}} whose values are larger than or equal to {{Code|$min}} and smaller than or equal to {{Code|$max}} and that are stored in the text index.
 +
|- valign="top"
 +
| '''Errors'''
 +
|{{Error|open|#Errors}} the addressed database does not exist or could not be opened.<br/>{{Error|no-index|#Errors}} the index is not available.<br/>
 +
|- valign="top"
 +
| '''Examples'''
 +
|
 +
* {{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}}.
 +
|}
 +
 
 +
==db:attribute==
 +
 
 +
{| width='100%'
 +
|- valign="top"
 +
| width='120' | '''Signature'''
 +
|<pre>db:attribute(
 +
  $database  as xs:string,
 +
  $values    as xs:string*,
 +
  $name      as xs:string  := ()
 +
) as attribute()*</pre>
 +
|- valign="top"
 +
| '''Summary'''
 +
|Returns all attribute nodes of a {{Code|$database}} that match one of the specified {{Code|$values}} and that are stored in the attribute index.<br/>If {{Code|$name}} is specified, the resulting attribute nodes are filtered by their attribute name.
 +
|- valign="top"
 +
| '''Errors'''
 +
|{{Error|open|#Errors}} the addressed database does not exist or could not be opened.<br/>{{Error|no-index|#Errors}} the index is not available.<br/>
 +
|- valign="top"
 +
| '''Examples'''
 +
|
 +
* {{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.
 +
|}
 +
 
 +
==db:attribute-range==
 +
 
 +
{| width='100%'
 +
|- valign="top"
 +
| width='120' | '''Signature'''
 +
|<pre>db:attribute-range(
 +
  $database  as xs:string,
 +
  $min      as xs:string,
 +
  $max      as xs:string,
 +
  $name      as xs:string  := ()
 +
) as attribute()*</pre>
 +
|- valign="top"
 +
| '''Summary'''
 +
|Returns all attributes of a {{Code|$database}} whose values are larger than or equal to {{Code|$min}} and smaller than or equal to {{Code|$max}} and that are stored in the attribute index.
 +
|- valign="top"
 +
| '''Errors'''
 +
|{{Error|open|#Errors}} the addressed database does not exist or could not be opened.<br/>{{Error|no-index|#Errors}} the index is not available.<br/>
 +
|- valign="top"
 +
| '''Examples'''
 +
|
 +
* {{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}}.
 +
|}
 +
 
 +
==db:token==
 +
 
 +
{| width='100%'
 +
|- valign="top"
 +
| width='120' | '''Signature'''
 +
|<pre>db:token(
 +
  $database  as xs:string,
 +
  $tokens    as xs:string*,
 +
  $name      as xs:string  := ()
 +
) as attribute()*</pre>
 +
|- valign="top"
 +
| '''Summary'''
 +
|Returns all attribute nodes of a {{Code|$database}} the values of which contain one of the specified {{Code|$tokens}}.<br/>If {{Code|$name}} is specified, the resulting attribute nodes are filtered by their attribute name.
 +
|- valign="top"
 +
| '''Errors'''
 +
|{{Error|open|#Errors}} the addressed database does not exist or could not be opened.<br/>{{Error|no-index|#Errors}} the index is not available.<br/>
 +
|- valign="top"
 +
| '''Examples'''
 +
|
 +
* {{Code|db:token("DB", "row", "class")/parent::div}} returns all {{Code|div}} nodes of database {{Code|DB}} with a {{Code|class}} attribute that contains the token {{Code|row}}.
 +
|}
 +
 
 +
=Updates=
 +
 
 +
All functions in this section are [[#Updating Functions|Updating Functions]].
 +
 
 +
==db:create==
 +
 
 +
{| width='100%'
 +
|- valign="top"
 +
| width='120' | '''Signature'''
 +
|<pre>db:create(
 +
  $database  as xs:string,
 +
  $inputs    as item()*    := (),
 +
  $paths    as xs:string*  := (),
 +
  $options  as map(*)?    := map { }
 +
) as empty-sequence()</pre>
 +
|- valign="top"
 +
| '''Summary'''
 +
|Creates a new {{Code|$database}} and adds the supplied {{Code|$inputs}} to the specified {{Code|$paths}}:
 +
* The inputs may be strings or nodes:
 +
** nodes may be of any type except for attributes
 +
** strings can be a URI pointing to a file/directory or an XML string (which is detected by the leading <code>&lt;</code> character)
 +
** a path must be specified if the input is not a file or directory reference
 +
* The parsing and indexing behavior can be controlled via {{Code|$options}}:
 +
** allowed options are {{Option|ADDCACHE}} and the [[Options#Indexing|indexing]], [[Options#Full-Text Indexing|full-text indexing]], [[Options#Parsing|parsing]] and [[Options#XML Parsing|XML parsing]] options, all in lower case
 +
** parsing options will only impact string input (URIs, XML strings) because nodes have already been parsed.
 +
* An existing database will be overwritten.
 +
* Database creation takes place after most other update operations (see [[XQuery Update#Pending Update List|Pending Update List]]). As a consequence, a newly created database cannot be addressed in the same query.
 +
|- valign="top"
 +
| '''Errors'''
 +
|{{Error|lock|#Errors}} a database is opened by another process.<br/>{{Error|name|#Errors}} the specified name is not a [[Commands#Valid_Names|valid database name]].<br/>{{Error|conflict|#Errors}} the same database was addressed more than once.<br/>{{Error|args|#Errors}} the number of specified inputs and paths differs.
 +
|- valign="top"
 +
| '''Examples'''
 +
|
 +
* {{Code|db:create("DB")}} creates the empty database {{Code|DB}}.
 +
* {{Code|db:create("DB", "/home/dir/doc.xml")}} creates the database {{Code|DB}} and adds the document {{Code|/home/dir/doc.xml}} as initial content.
 +
* {{Code|db:create("DB", <a/>, "doc.xml")}} creates the database {{Code|DB}} and adds the document with content {{Code|&lt;a/&gt;}} under the name {{Code|doc.xml}}.
 +
* {{Code|db:create("DB", "/home/dir/", "docs/dir")}} creates the database {{Code|DB}} and adds the documents in {{Code|/home/dir}} to the database under the path {{Code|docs/dir}}.
 +
* <code>db:create("DB", file:list('.'), (), map { 'ftindex': true() })</code> adds all files of the current working directory to a new database, preserving relative filesystem paths and creating a full-text index.
 +
|}
 +
 
 +
==db:add==
 +
 
 +
{| width='100%'
 +
|- valign="top"
 +
| width='120' | '''Signature'''
 +
|<pre>db:add(
 +
  $database  as xs:string,
 +
  $input    as item(),
 +
  $path      as xs:string?  := (),
 +
  $options  as map(*)?    := map { }
 +
) as empty-sequence()</pre>
 +
|- valign="top"
 +
| '''Summary'''
 +
|Adds the specified {{Code|$input}} to a {{Code|$database}} with the specified {{Code|$path}}:
 +
* A document with the same path may occur more than once in a database. If you want to enforce single instances, use {{Function||db:put}} instead.
 +
* See {{Function||db:create}} for more details on the input and path arguments.
 +
* The parsing behavior can be controlled via {{Code|$options}}:
 +
** allowed options are {{Option|ADDCACHE}} and the [[Options#Parsing|parsing]] and [[Options#XML Parsing|XML parsing]] options, all in lower case
 +
** parsing options will only impact string input (URIs, XML strings) because nodes have already been parsed
 +
|- valign="top"
 +
| '''Errors'''
 +
|{{Error|open|#Errors}} the addressed database does not exist or could not be opened.
 +
|- valign="top"
 +
| '''Examples'''
 +
|
 +
* <code>db:add("DB", "/home/dir/doc.xml")</code> adds the file {{Code|/home/dir/doc.xml}} to the database {{Code|DB}}.
 +
* <code>db:add("DB", <a/>, "doc.xml")</code> adds a document node to the database {{Code|DB}} under the name {{Code|doc.xml}}.
 +
* <code>db:add("DB", "/home/dir", "docs/dir", map { 'addcache': true() })</code> adds all documents in {{Code|/home/dir}} to the database {{Code|DB}} under the path {{Code|docs/dir}}. To reduce memory consumption, the files will be cached before being added to the database.
 +
|}
 +
 
 +
==db:put==
 +
 
 +
{| width='100%'
 +
|- valign="top"
 +
| width='120' | '''Signature'''
 +
|<pre>db:put(
 +
  $database  as xs:string,
 +
  $input    as item(),
 +
  $path      as xs:string,
 +
  $options  as map(*)?    := map { }
 +
) as empty-sequence()</pre>
 +
|- valign="top"
 +
| '''Summary'''
 +
|Replaces a resource, specified by {{Code|$path}}, in a {{Code|$database}} with the contents of {{Code|$input}}, or adds it as a new resource:
 +
* The parsing behavior can be controlled via {{Code|$options}}:
 +
** Allowed options are {{Option|ADDCACHE}} and the [[Options#Parsing|parsing]] and [[Options#XML Parsing|XML parsing]] options, all in lower case.
 +
** Parsing options will only impact string input (URIs, XML strings), because nodes have already been parsed.
 +
* See {{Function||db:create}} for more details on the input argument.
 +
|- valign="top"
 +
| '''Errors'''
 +
|{{Error|open|#Errors}} the addressed database does not exist or could not be opened.<br/>{{Error|target|#Errors}} the path points to a directory.
 +
|- valign="top"
 +
| '''Examples'''
 +
|
 +
* {{Code|db:put("DB", "/home/dir/doc.xml", "docs/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}}.
 +
* {{Code|db:put("DB", "<a/>", "docs/dir/doc.xml")}} replaces the content of the document {{Code|docs/dir/doc.xml}} in the database {{Code|DB}} with {{Code|&lt;a/&gt;}}.
 +
* {{Code|db:put("DB", document { <a/> }, "docs/dir/doc.xml")}} replaces the content of the document {{Code|docs/dir/doc.xml}} in the database {{Code|DB}} with the specified document node.
 +
The following query can be used to import files from a directory to a database:
 +
<pre lang='xquery'>
 +
let $source := '/home/john/xml/source'
 +
for $file in file:list($source, true())
 +
let $path := $source || $file
 +
where not(file:is-dir($path))
 +
return db:put('db', doc($path), $file)
 +
</pre>
 +
|}
 +
 
 +
==db:put-binary==
 +
 
 +
{| width='100%'
 +
|- valign="top"
 +
| width='120' | '''Signature'''
 +
|<pre>db:put-binary(
 +
  $database  as xs:string,
 +
  $input    as item(),
 +
  $path      as xs:string
 +
) as empty-sequence()</pre>
 +
|- valign="top"
 +
| '''Summary'''
 +
|Stores a binary resource specified by {{Code|$input}} in a {{Code|$database}} at the specified {{Code|$path}}. Existing resources are overwritten.
 +
|- valign="top"
 +
| '''Errors'''
 +
|{{Error|open|#Errors}} the addressed database does not exist or could not be opened.<br/>{{Error|mainmem|#Errors}} the database is not ''persistent'' (stored on disk).
 +
|- valign="top"
 +
| '''Examples'''
 +
|
 +
* {{Code|db:put-binary('DB', file:read-binary('video.mov'), 'video/sample.mov')}} stores the addressed video file at the specified location.
 +
* With the following query, you can copy the binary resources of one database into another:
 +
<pre lang='xquery'>
 +
let $db := 'db'
 +
let $src-path := 'src/'
 +
let $trg-path := 'trg/'
 +
for $src in db:list($db, $src-path)
 +
where db:type($db, $src) = 'binary'
 +
let $trg := $trg-path || substring-after($src, $src-path)
 +
return db:put-binary($db, db:get-binary($db, $src), $trg)
 +
</pre>
 +
|}
 +
 
 +
==db:put-value==
 +
 
 +
{| width='100%'
 +
|- valign="top"
 +
| width='120' | '''Signature'''
 +
|<pre>db:put-value(
 +
  $database  as xs:string,
 +
  $input    as item()*,
 +
  $path      as xs:string
 +
) as empty-sequence()</pre>
 +
|- valign="top"
 +
| '''Summary'''
 +
|Stores an {{Code|$input}} value in a {{Code|$database}} at the specified {{Code|$path}}. Existing resources are overwritten. The value can be an arbitrary sequence of atomic items, nodes, maps, and arrays.
 +
|- valign="top"
 +
| '''Errors'''
 +
|{{Error|open|#Errors}} the addressed database does not exist or could not be opened.<br/>{{Error|mainmem|#Errors}} the database is not ''persistent'' (stored on disk).
 +
|- valign="top"
 +
| '''Examples'''
 +
|
 +
* {{Code|db:put-value('DB', 1 to 10000, 'sequence')}} stores a numeric range in the database.
 +
* With the following query, a map with countries and associated cities is stored in a database. The value resource can e.g. be used as index in future queries:
 +
<pre lang='xquery'>
 +
db:put-value(
 +
  'factbook',
 +
  map:merge(
 +
    for $country in db:get('factbook')//country
 +
    return map:entry($country/@name, $country//city/name ! string())
 +
  ),
 +
  'cities'
 +
)</pre>
 +
|}
 +
 
 +
==db:delete==
 +
 
 +
{| width='100%'
 +
|- valign="top"
 +
| width='120' | '''Signature'''
 +
|<pre>db:delete(
 +
  $database  as xs:string,
 +
  $path      as xs:string
 +
) as empty-sequence()</pre>
 +
|- valign="top"
 +
| '''Summary'''
 +
|Deletes resource(s), specified by {{Code|$path}}, from the specified {{Code|$database}}.
 +
|- valign="top"
 +
| '''Errors'''
 +
|{{Error|open|#Errors}} the addressed database does not exist or could not be opened.<br/>{{Error|path|#Errors}} the specified path is invalid.
 +
|- valign="top"
 +
| '''Examples'''
 +
|
 +
* {{Code|db:delete("DB", "docs/dir/doc.xml")}} deletes the resource {{Code|docs/dir/doc.xml}} from {{Code|DB}}.
 +
* {{Code|db:delete("DB", "docs/dir")}} deletes all resources from {{Code|DB}} in the specified path {{Code|docs/dir}}.
 +
|}
 +
 
 +
==db:copy==
 +
 
 +
{| width='100%'
 +
|- valign="top"
 +
| width='120' | '''Signature'''
 +
|<pre>db:copy(
 +
  $database  as xs:string,
 +
  $new-name  as xs:string
 +
) as empty-sequence()</pre>
 +
|- valign="top"
 +
| '''Summary'''
 +
|Creates a copy of {{Code|$database}}, which will be called {{Code|$new-name}}.
 +
|- valign="top"
 +
| '''Errors'''
 +
|{{Error|open|#Errors}} the addressed database does not exist or could not be opened.<br/>{{Error|lock|#Errors}} a database is opened by another process.<br/>{{Error|name|#Errors}} invalid database name.<br/>{{Error|conflict|#Errors}} the same database was addressed more than once.
 +
|}
 +
 
 +
==db:alter==
 +
 
 +
{| width='100%'
 +
|- valign="top"
 +
| width='120' | '''Signature'''
 +
|<pre>db:alter(
 +
  $database  as xs:string,
 +
  $new-name  as xs:string
 +
) as empty-sequence()</pre>
 +
|- valign="top"
 +
| '''Summary'''
 +
|Renames a {{Code|$database}} to {{Code|$new-name}}.
 +
|- valign="top"
 +
| '''Errors'''
 +
|{{Error|open|#Errors}} the addressed database does not exist or could not be opened.<br/>{{Error|lock|#Errors}} a database is opened by another process.<br/>{{Error|name|#Errors}} invalid database name.<br/>{{Error|conflict|#Errors}} the same database was addressed more than once.
 +
|}
 +
 
 +
==db:optimize==
 +
 
 +
{| width='100%'
 +
|- valign="top"
 +
| width='120' | '''Signature'''
 +
|<pre>db:optimize(
 +
  $database  as xs:string,
 +
  $all      as xs:boolean?  := false(),
 +
  $options  as map(*)?      := map { }
 +
) as empty-sequence()</pre>
 +
|- valign="top"
 +
| '''Summary'''
 +
|Optimizes the metadata and indexes of a {{Code|$database}}.<br/>If {{Code|$all}} is {{Code|true}}, the complete database will be rebuilt.<br/>The {{Code|$options}} argument can be used to control indexing. The syntax is identical to the {{Function||db:create}} function: Allowed options are all [[Options#Indexing|indexing]] and [[Options#Full-Text|full-text]] options. {{Option|UPDINDEX}} is only supported if {{Code|$all}} is {{Code|true}}.
 +
|- valign="top"
 +
| '''Errors'''
 +
|{{Error|open|#Errors}} the addressed database does not exist or could not be opened.
 +
|- valign="top"
 +
| '''Examples'''
 +
|
 +
* {{Code|db:optimize("DB")}} optimizes the database structures of the database {{Code|DB}}.
 +
* <code>db:optimize("DB", true(), map { 'ftindex': true() })</code> optimizes all database structures of the database {{Code|DB}} and creates a full-text index.
 +
|}
 +
 
 +
==db:rename==
 +
 
 +
{| width='100%'
 +
|- valign="top"
 +
| width='120' | '''Signature'''
 +
|<pre>db:rename(
 +
  $database  as xs:string,
 +
  $source    as xs:string,
 +
  $target    as xs:string
 +
) as empty-sequence()</pre>
 +
|- valign="top"
 +
| '''Summary'''
 +
|Moves all resources(s) of a {{Code|$database}}, which are found in the supplied {{Code|$source}} path, to the supplied {{Code|$target}} path. The paths may point to single resources or directories. No updates will take place if a non-existing source path is supplied.
 +
|- valign="top"
 +
| '''Errors'''
 +
|{{Error|open|#Errors}} the addressed database does not exist or could not be opened.<br/>{{Error|path|#Errors}} the specified source or target path, or one of its descendants, is invalid.
 +
|- valign="top"
 +
| '''Examples'''
 +
|
 +
* {{Code|db:rename("DB", "docs/dir/doc.xml", "docs/dir/newdoc.xml")}} renames the resource {{Code|docs/dir/doc.xml}} to {{Code|docs/dir/newdoc.xml}} in the database {{Code|DB}}.
 +
* {{Code|db:rename("DB", "docs/dir", "docs/newdir")}} moves all resources in the database {{Code|DB}} from {{Code|docs/dir}} to {{Code|docs/newdir}}.
 +
|}
 +
 
 +
==db:flush==
 +
 
 +
{| width='100%'
 +
|- valign="top"
 +
| width='120' | '''Signature'''
 +
|<pre>db:flush(
 +
  $database  as xs:string
 +
) as empty-sequence()</pre>
 +
|- valign="top"
 +
| '''Summary'''
 +
|Explicitly flushes the buffers of a {{Code|$database}}. This command is only useful if {{Option|AUTOFLUSH}} has been set to {{Code|false}}.
 +
|- valign="top"
 +
| '''Errors'''
 +
|{{Error|open|#Errors}} the addressed database does not exist or could not be opened.
 +
|}
 +
 
 +
==db:drop==
 +
 
 +
{| width='100%'
 +
|- valign="top"
 +
| width='120' | '''Signature'''
 +
|<pre>db:drop(
 +
  $database  as xs:string
 +
) as empty-sequence()</pre>
 +
|- valign="top"
 +
| '''Summary'''
 +
|Drops a {{Code|$database}} and all connected resources.
 +
|- valign="top"
 +
| '''Errors'''
 +
|{{Error|open|#Errors}} the addressed database does not exist or could not be opened.<br/>{{Error|lock|#Errors}} a database is opened by another process.<br/>{{Error|conflict|#Errors}} the same database was addressed more than once.
 +
|- valign="top"
 +
| '''Examples'''
 +
|
 +
* {{Code|db:drop("DB")}} drops the database {{Code|DB}}.
 +
|}
 +
 
 +
=Backups=
 +
 
 +
All functions in this section except for {{Function||db:backups}} are {{Function|Database|Updating Functions}}.
 +
 
 +
==db:create-backup==
 +
 
 +
{| width='100%'
 +
|- valign="top"
 +
| width='120' | '''Signature'''
 +
|<pre>db:create-backup(
 +
  $database  as xs:string,
 +
  $options  as map(*)?    := map { }
 +
) as empty-sequence()</pre>
 +
|- valign="top"
 +
| '''Summary'''
 +
|Creates a backup of a {{Code|$database}}. If no name is supplied, general data will be backed up. The following {{Code|$options}} are available:
 +
* With {{Code|comment}}, a comment string can be attached to the backup.
 +
* By setting {{Code|compress}} to false, the backup will be created faster, but it will take more space on disk.
 +
|- valign="top"
 +
| '''Errors'''
 +
|{{Error|open|#Errors}} the addressed database does not exist or could not be opened.<br/>{{Error|name|#Errors}} invalid database name.<br/>{{Error|conflict|#Errors}} the same database was addressed more than once.
 +
|- valign="top"
 +
| '''Examples'''
 +
|
 +
* {{Code|db:create-backup('DB', map { 'compress': false() })}} creates a backup of the database {{Code|DB}} without compressing its entries.
 +
|}
 +
 
 +
==db:drop-backup==
 +
 
 +
{| width='100%'
 +
|- valign="top"
 +
| width='120' | '''Signature'''
 +
|<pre>db:drop-backup(
 +
  $name  as xs:string
 +
) as empty-sequence()</pre>
 +
|- valign="top"
 +
| '''Summary'''
 +
|Drops all backups of the database with the specified {{Code|$name}}. If the name ends with a timestamp, only the specified backup file will be deleted. If no name is supplied, backups with general data are addressed.
 +
|- valign="top"
 +
| '''Errors'''
 +
|{{Error|backup|#Errors}} No backup file found.<br/>{{Error|name|#Errors}} invalid database name.<br/>{{Error|conflict|#Errors}} the same database was addressed more than once.
 +
|- valign="top"
 +
| '''Examples'''
 +
|
 +
* {{Code|db:drop-backup("DB")}} drops all backups of the database {{Code|DB}}.
 +
* {{Code|db:drop-backup("DB-2014-03-13-17-36-44")}} drops the specific backup file {{Code|DB-2014-03-13-17-36-44.zip}} of the database {{Code|DB}}.
 +
|}
 +
 
 +
==db:alter-backup==
 +
 
 +
{| width='100%'
 +
|- valign="top"
 +
| width='120' | '''Signature'''
 +
|<pre>db:alter-backup(
 +
  $name      as xs:string,
 +
  $new-name  as xs:string
 +
) as empty-sequence()</pre>
 +
|- valign="top"
 +
| '''Summary'''
 +
|Renames all backups of the database with the specified {{Code|$name}} to {{Code|$new-name}}. If the name ends with a date, only the specified backup file will be renamed.
 +
|- valign="top"
 +
| '''Errors'''
 +
|{{Error|backup|#Errors}} No backup file found.<br/>{{Error|name|#Errors}} invalid database name.<br/>{{Error|conflict|#Errors}} the same database was addressed more than once.
 +
|- valign="top"
 +
| '''Examples'''
 +
|
 +
* {{Code|db:alter-backup("DB", "DB2)}} renames all backups of the database {{Code|DB}} to {{Code|DB2}}.
 +
|}
 +
 
 +
==db:restore==
 +
 
 +
{| width='100%'
 +
|- valign="top"
 +
| width='120' | '''Signature'''
 +
|<pre>db:restore(
 +
  $name  as xs:string
 +
) as empty-sequence()</pre>
 +
|- valign="top"
 +
| '''Summary'''
 +
|Restores the database with the specified {{Code|$name}}. The {{Code|$name}} may include the timestamp of the backup file. If no name is supplied, general data will be restored. If general data is restored, it will only be available after BaseX has been restarted.
 +
|- valign="top"
 +
| '''Errors'''
 +
|{{Error|lock|#Errors}} a database is opened by another process.<br/>{{Error|name|#Errors}} invalid database name.<br/>{{Error|no-backup|#Errors}} No backup found.<br/>{{Error|conflict|#Errors}} the same database was addressed more than once.
 +
|- valign="top"
 +
| '''Examples'''
 +
|
 +
* {{Code|db:restore("DB")}} restores the database {{Code|DB}}.
 +
* {{Code|db:restore("DB-2014-03-13-18-05-45")}} restores the database {{Code|DB}} from the backup file with the given timestamp.
 +
|}
 +
 
 +
==db:backups==
 +
 
 +
{| width='100%'
 +
|- valign="top"
 +
| width='120' | '''Signature'''
 +
|<pre>db:backups(
 +
  $database  as xs:string  := ()
 +
) as element(backup)*</pre>
 +
|- valign="top"
 +
| '''Summary'''
 +
|Returns an element sequence containing all available database backups with timestamp, file size and comment.<br/>If a {{Code|$database}} is specified, the sequence will be restricted to the backups matching this database.
 +
|- valign="top"
 +
| '''Examples'''
 +
|
 +
* {{Code|db:backups("factbook")}} returns all backups that have been made from the {{Code|factbook}} database.
 +
|}
 +
 
 +
=Helper Functions=
 +
 
 +
==db:name==
 +
 
 +
{| width='100%'
 +
|- valign="top"
 +
| width='120' | '''Signature'''
 +
|<pre>db:name(
 +
  $node  as node()
 +
) as xs:string</pre>
 +
|- valign="top"
 +
| '''Summary'''
 +
|Returns the name of the database in which the specified [[#Database Nodes|database node]] {{Code|$node}} is stored.
 +
|- valign="top"
 +
| '''Errors'''
 +
|{{Error|node|#Errors}} {{Code|$nodes}} contains a node which is not stored in a database.
 +
|}
 +
 
 +
==db:path==
 +
 
 +
{| width='100%'
 +
|- valign="top"
 +
| width='120' | '''Signature'''
 +
|<pre>db:path(
 +
  $node  as node()
 +
) as xs:string</pre>
 +
|- valign="top"
 +
| '''Summary'''
 +
|Returns the path of the database document in which the specified [[#Database Nodes|database node]] {{Code|$node}} is stored.
 +
|- valign="top"
 +
| '''Errors'''
 +
|{{Error|node|#Errors}} {{Code|$nodes}} contains a node which is not stored in a database.
 +
|}
 +
 
 +
==db:exists==
 +
 
 +
{| width='100%'
 +
|- valign="top"
 +
| width='120' | '''Signature'''
 +
|<pre>db:exists(
 +
  $database  as xs:string,
 +
  $path      as xs:string  := ()
 +
) as xs:boolean</pre>
 +
|- valign="top"
 +
| '''Summary'''
 +
|Checks if a {{Code|$database}} exists, or a resource located at {{Code|$path}} in this database.
 +
|- valign="top"
 +
| '''Examples'''
 +
|
 +
* {{Code|db:exists("DB")}} returns {{Code|true}} if the database {{Code|DB}} exists.
 +
* {{Code|db:exists("DB", "resource")}} returns {{Code|true}} if {{Code|resource}} exists in this database.
 +
|}
 +
 
 +
==db:type==
 +
 
 +
{| width='100%'
 +
|- valign="top"
 +
| width='120' | '''Signature'''
 +
|<pre>db:type(
 +
  $database  as xs:string,
 +
  $path      as xs:string
 +
) as xs:string</pre>
 +
|- valign="top"
 +
| '''Summary'''
 +
|Returns the type ({{Code|xml}}, {{Code|binary}}, {{Code|value}}) of a resource in a {{Code|$database}} at the specified {{Code|$path}}.
 +
|- valign="top"
 +
| '''Errors'''
 +
|{{Error|open|#Errors}} the addressed database does not exist or could not be opened.
 +
|- valign="top"
 +
| '''Examples'''
 +
|
 +
* {{Code|db:type("DB", "factbook.xml")}} returns {{Code|true}} if the specified resource is an XML document.
 +
|}
 +
 
 +
==db:content-type==
 +
 
 +
{| width='100%'
 +
|- valign="top"
 +
| width='120' | '''Signature'''
 +
|<pre>db:content-type(
 +
  $database  as xs:string,
 +
  $path      as xs:string
 +
) as xs:string</pre>
 +
|- valign="top"
 +
| '''Summary'''
 +
|Retrieves the content-type of a resource in a {{Code|$database}} at the specified {{Code|$path}}.<br/>The file extension is used to recognize the content-type of a resource stored in the database. {{Code|application/xml}} will be returned for any XML document stored in the database, regardless of its file name extension.
 +
|- valign="top"
 +
| '''Errors'''
 +
|{{Error|open|#Errors}} the addressed database does not exist or could not be opened.
 +
|- valign="top"
 +
| '''Examples'''
 +
|
 +
* {{Code|db:content-type("DB", "docs/doc01.pdf")}} returns {{Code|application/pdf}}.
 +
* {{Code|db:content-type("DB", "docs/doc01.xml")}} returns {{Code|application/xml}}.
 +
* {{Code|db:content-type("DB", "docs/doc01")}} returns {{Code|application/xml}}, if {{Code|db:is-xml("DB", "docs/doc01")}} returns {{Code|true}}.
 +
|}
 +
 
 +
=Errors=
 +
 
 +
{| class="wikitable" width="100%"
 +
! width="110"|Code
 +
| Description
 +
|- valign="top"
 +
|{{Code|args}}
 +
|The number of specified inputs and paths differs.
 +
|- valign="top"
 +
|{{Code|conflict}}
 +
|Multiple update operations point to the same target.
 +
|- valign="top"
 +
|{{Code|lock}}
 +
|A database cannot be updated because it is opened by another process.
 +
|- valign="top"
 +
|{{Code|mainmem}}
 +
|The addressed database is not ''persistent'' (stored on disk).
 +
|- valign="top"
 +
|{{Code|name}}
 +
|The name of the specified database is invalid.
 +
|- valign="top"
 +
|{{Code|no-backup}}
 +
|No backup exists for a database.
 +
|- valign="top"
 +
|{{Code|node}}
 +
|The referenced XML node is no [[#Database Nodes|database node]], i.e. it is neither stored in a database nor represented as database fragment.
 +
|- valign="top"
 +
|{{Code|no-index}}
 +
|The database lacks an index structure required by the called function.
 +
|- valign="top"
 +
|{{Code|open}}
 +
|The addressed database does not exist or could not be opened.
 +
|- valign="top"
 +
|{{Code|option}}
 +
|The specified option is unknown.
 +
|- valign="top"
 +
|{{Code|path}}
 +
|The specified database path is invalid.
 +
|- valign="top"
 +
|{{Code|property}}
 +
|The specified database property is unknown.
 +
|- valign="top"
 +
|{{Code|range}}
 +
|The addressed database ID or PRE value is out of range.
 +
|- valign="top"
 +
|{{Code|target}}
 +
|Path points to an invalid target.
 +
|}
 +
 
 +
=Changelog=
 +
 
 +
;Version 10
 +
* Added: {{Function||db:get}}, {{Function||db:put}}, {{Function||db:type}}.
 +
* Added: [[#Backups|Backups]]: Support for general data ([[User Management|registered users]], [[Job Module#Services|scheduled services]] and [[Store Module|key-value stores]]).
 +
* Updated: {{Function||db:get}}, {{Function||db:get-id}}, {{Function||db:get-pre}} renamed (before: {{Code|db:open}}, {{Code|db:open-id}}, {{Code|db:open-pre}})
 +
* Updated: {{Function||db:put}} renamed (before: {{Code|db:replace}}); function signature aligned with {{Function||db:add}} (second and third argument swapped).
 +
* Updated: {{Function||db:put-binary}} renamed (before: {{Code|db:store}}); function signature aligned with {{Function||db:add}} (second and third argument swapped).
 +
* Updated: {{Function||db:get-binary}} renamed (before: {{Code|db:retrieve}}).
 +
* Updated: {{Function||db:backups}}, {{Function||db:create-backup}}: Options added.
 +
* Removed: {{Code|db:is-raw}}, {{Code|db:is-raw}} (new: {{Function||db:type}}).
 +
 
 +
;Version 9.3
 +
* Added: {{Function||db:alter-backup}}
 +
* Updated: {{Code|db:open-id}}, {{Code|db:open-pre}}: support for multiple integers
 +
 
 +
;Version 9.2
 +
* Added: {{Function||db:dir}}
 +
* Updated: {{Function||db:add}}: {{Code|$path}} allow empty path argument
 +
 
 +
;Version 9.0
 +
* Added: {{Function||db:option}}
 +
* Updated: db:output renamed to {{Function|Update|update:output}}, db:output-cache renamed to {{Function|Update|update:cache}}
 +
* Updated: error codes updated; errors now use the module namespace
 +
 
 +
;Version 8.6
 +
* Added: {{Function||db:property}}
 +
 
 +
;Version 8.4
 +
* Updated: {{Function||db:create}}, {{Function||db:add}}, {{Code|db:replace}}: support for {{Code|ADDCACHE}} option.
 +
* Added: {{Function||db:token}}
 +
 
 +
;Version 8.3
 +
* Updated: {{Function||db:list-details}}: attributes with name of database and date of backup added to results.
 +
* Updated: {{Function||db:backups}} now include attributes with name of database and date of backup.
 +
* Updated: {{Function|Database|Value Indexes}}: raise error if no index exists.
 +
 
 +
;Version 8.2
 +
* Added: {{Function||db:output-cache}}
 +
* Removed: db:event
 +
 
 +
;Version 7.9
 +
* Updated: parsing options added to {{Function||db:create}}, {{Function||db:add}} and {{Code|db:replace}}.
 +
* Updated: allow {{Option|UPDINDEX}} if {{Code|$all}} is {{Code|true}}.
 +
 
 +
;Version 7.8.2
 +
* Added: {{Function||db:alter}}, {{Function||db:copy}}, {{Function||db:create-backup}}, {{Function||db:drop-backup}}, {{Function||db:restore}}
 +
 
 +
;Version 7.8
 +
* Removed: db:fulltext (use {{Function|Full-Text|ft:search}} instead)
 +
 
 +
;Version 7.7
 +
* Added: {{Function||db:export}}, {{Function||db:name}}, {{Function||db:path}}
 +
* Updated: {{Code|$options}} argument added to {{Function||db:create}} and {{Function||db:optimize}}.
 +
* Updated: the functions no longer accept [[#Database Nodes|database nodes]] as reference. Instead, the name of a database must now be specified.
 +
 
 +
;Version 7.6
 +
* Updated: {{Function||db:create}}: allow more than one input and path.
 +
 
 +
;Version 7.5
 +
* Updated: {{Function||db:add}}: input nodes will be automatically converted to document nodes
 +
* Added: {{Function||db:backups}}
 +
* Added: {{Function||db:create}}
 +
* Added: {{Function||db:drop}}
 +
 
 +
;Version 7.3
 +
* Added: {{Function||db:flush}}
 +
 
 +
;Version 7.2.1
 +
* Added: {{Function||db:text-range}}, {{Function||db:attribute-range}}, {{Function||db:output}}
 +
 
 +
;Version 7.1
 +
* Added: {{Function||db:list-details}}, {{Function||db:content-type}}
 +
* Updated: {{Function||db:info}}, {{Function||db:system}}, {{Code|db:retrieve}}
 +
 
 +
;Version 7.0
 +
* Added: {{Function||db:exists}}, {{Code|db:retrieve}}, {{Code|db:store}}, {{Code|db:is-raw}}, {{Code|db:is-xml}}
 +
* Updated: {{Function||db:list}}, {{Code|db:open}}, {{Function||db:add}}

Latest revision as of 22:07, 1 December 2023

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.

Conventions[edit]

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

Database Nodes[edit]

In BaseX, two internal representations exist for XML nodes:

1. Database nodes are:

  • stored in a persistent database on disk;
  • nodes of a document that has been generated temporarily with fn:doc, fn:parse-xml and other functions; or
  • the result of a main-memory update.

2. Fragments are similar to DOM structures. They are created when XQuery node constructors are used (<a/>, element a { }, etc.).

Fragments require less memory for small XML structures, but database nodes are more efficient for larger amounts of data. Fragments nodes can be converted to database nodes by applying a main-memory update with an empty body to a node:

<xml>hello world</xml> update { }

Updating Functions[edit]

Various functions in this module are updating. Updating functions will not be immediately executed, but queued on the Pending Update List, and processed after the remaining query has been evaluated. This means that the order in which the functions are specified in the query often does not reflect the order in which they will eventually be executed.

General Functions[edit]

db:system[edit]

Signature
db:system() as element(system)
Summary Returns general information on the database system the current values of all global and local Options. The INFO command returns similar output.

db:option[edit]

Signature
db:option(
  $name  as xs:string
) as xs:string
Summary Returns the current value (string, integer, boolean, map) of a global or local Option with the specified $name. The SHOW OPTIONS command returns similar output.
Errors option: the specified option is unknown.
Examples
  • db:option('dbpath') returns the database path string.
  • db:option('serializer') returns a map with the current serialization parameters.
  • declare option db:stripws 'true'; db:option('stripws') returns the locally assigned value.

db:info[edit]

Signature
db:info(
  $database  as xs:string
) as element(database)
Summary Returns meta information on the specified $database. The output is similar to the INFO DB command.
Errors open: the addressed database does not exist or could not be opened.

db:property[edit]

Signature
db:property(
  $database  as xs:string,
  $name      as xs:string
) as xs:anyAtomicType
Summary Returns the value (string, boolean, integer) of a property with the specified $name in the specified $database. The available properties are the ones returned by db:info.
Errors property: the specified property is unknown.
Examples
  • db:property('db', 'size') returns the number of bytes occupied by the database db.
  • db:property('xmark', 'textindex') indicates if the xmark database has a text index.
  • db:property('discogs', 'uptodate') indicates if the database statistics and index structures of the discogs database are up-to-date.

db:list[edit]

Signature
db:list(
  $database  as xs:string  := (),
  $path      as xs:string  := ()
) as xs:string*
Summary Without arguments, the names of all databases are returned that are accessible to the current user. If $database is specified, paths to all resources of this database are returned. The results can be restricted to resources starting with the specified $path.
Errors open: the addressed database does not exist or could not be opened.
Examples
  • db:list("docs") returns the names of all documents of a database named docs.

db:list-details[edit]

Signature
db:list-details(
  $database  as xs:string  := (),
  $path      as xs:string  := ()
) as element()*
Summary Without arguments, an element is returned for each database that is accessible to the current user:
  • An element has a value, which is the name of the database, and several attributes, which contain the number of stored resources, the modification date, the database size on disk (measured in bytes), and a path to the original database input.

If $database is specified, an element for each resource in this database is returned:

  • An element has a value, which is the name of the resource, and several attributes, which contain the content type, the modification date, the raw flag (which indicates if the resource is binary or XML), and the size of a resource.
  • The value of the size attribute depends on the resource type: for documents, it represents the number of nodes; for binary data, it represents the file size (measured in bytes).
  • The results can be restricted to resources starting with the specified $path.
Errors open: the addressed database does not exist or could not be opened.
Examples
  • db:list-details("shop") returns the names plus additional info on all resources of a database named shop.

db:dir[edit]

Signature
db:dir(
  $database  as xs:string,
  $path      as xs:string
) as element()*
Summary Returns metadata on all directories and resources of a $database in the specified $path. Two types of elements are returned:
  • resource represents a resource. The element value is the directory path; content type, modification date, raw flag (which indicates if the resource is binary or XML), and size of the resource are returned as attributes.
  • dir represents a directory. The element value is the directory path; the modification date is returned in an attribute.

The directories are not stored in the internal database layout. Instead, they result implicitly from the paths of stored resources.

Errors open: the addressed database does not exist or could not be opened.
path: the specified path is invalid.
Examples
  • db:dir('shop', 'books') returns all entries of the books directory of a shop database.

Read Operations[edit]

db:get[edit]

Signature
db:get(
  $database  as xs:string,
  $path      as xs:string  := ()
) as document-node()*
Summary Returns all documents from the specified $database, or only documents matching the specified $path.
Errors open: the addressed database does not exist or could not be opened.
Examples
  • db:get('docs') returns all documents from the database named docs.
  • db:get('db', 'one') returns all documents from the database named db located in the path one.
  • for $i in 1 to 3 return db:get('db' || $i)//item returns all item elements from the databases db1, db2 and db3.

db:get-pre[edit]

Signature
db:get-pre(
  $database  as xs:string,
  $values    as xs:integer*
) as node()*
Summary Returns all nodes from a $database with the specified PRE values in distinct document order.
The PRE value provides very fast access to an existing database node, but it will change whenever a node with a smaller pre values is added to or deleted from a database.
Errors open: the addressed database does not exist or could not be opened.
range: the specified PRE value does not exist in the database.
Examples
  • db:get-pre("docs", 0) returns the first database node from the database named docs.

db:get-id[edit]

Signature
db:get-id(
  $database  as xs:string,
  $values    as xs:integer*
) as node()*
Summary Returns all nodes from a $database with the specified ID $values in distinct document order.
Each database node has a persistent ID value. Access to the node ID can be sped up by turning on the UPDINDEX option.
Errors open: the addressed database does not exist or could not be opened.
range: the specified ID value does not exist in the database.

db:get-binary[edit]

Signature
db:get-binary(
  $database  as xs:string,
  $path      as xs:string
) as item()
Summary Returns a map with the paths and binary items of all resources in the specified $database. A single xs:base64Binary item is returned if a $path is specified. All items are lazy, i.e., the actual data will only be retrieved if it is processed.
Errors open: the addressed database does not exist or could not be opened.
mainmem: the database is not persistent (stored on disk).
Examples
  • db:get-binary('DB', 'music/01.mp3') returns the specified audio file as raw data.
  • stream:materialize(db:get-binary('DB', 'music/01.mp3')) materializes the streamable result in main-memory before returning it.
  • convert:binary-to-string(db:get-binary('DB', 'info.txt'), 'UTF-8') converts a binary database resource as UTF-8 text and returns a string.

db:get-value[edit]

Signature
db:get-value(
  $database  as xs:string,
  $path      as xs:string
) as item()*
Summary Returns a map with the paths and values of all resources in the specified $database. A single value is returned if a $path is specified.
Errors open: the addressed database does not exist or could not be opened.
mainmem: the database is not persistent (stored on disk).
Examples
  • db:get-value('DB', 'sequence') returns the specified sequence.

db:node-pre[edit]

Signature
db:node-pre(
  $nodes  as node()*
) as xs:integer*
Summary Returns the PRE values of the specified $nodes, which must all be database nodes.
The PRE value provides very fast access to an existing database node, but it will change whenever a node with a smaller pre values is added to or deleted from a database.
Errors node: $nodes contains a node which is not stored in a database.
Examples
  • db:node-pre(doc("input")) returns 0 if the database input contains a single document.

db:node-id[edit]

Signature
db:node-id(
  $nodes  as node()*
) as xs:integer*
Summary Returns the ID values of the specified $nodes, which must all be database nodes.
Each database node has a persistent ID value. Access to the node id can be sped up by turning on the UPDINDEX option.
Errors node: $nodes contains a node which is not stored in a database.

db:export[edit]

Signature
db:export(
  $database  as xs:string,
  $path      as xs:string,
  $options   as map(*)?    := map { }
) as empty-sequence()
Summary Exports the specified $database to the specified file $path. Existing files will be overwritten.
The $options argument contains serialization parameters (see fn:serialize).
Errors open: the addressed database does not exist or could not be opened.
Examples Export all files as text:
db:export("DB", "/home/john/xml/texts", map { 'method': 'text' })

The following code can be used to export parts of the database:

let $target := '/home/john/xml/target'
for $doc in db:get('DB', 'collection')
let $path := $target || db:path($doc)
return (
  file:create-dir(file:parent($path)),
  file:write($path, $doc)
)

Value Indexes[edit]

db:text[edit]

Signature
db:text(
  $database  as xs:string,
  $values    as xs:string*
) as text()*
Summary Returns all text nodes of a $database that match one of the specified $values and that are stored in the text index.
Errors open: the addressed database does not exist or could not be opened.
no-index: the index is not available.
Examples
  • db:text("DB", "QUERY")/.. returns the parents of all text nodes of the database DB that match the string QUERY.

db:text-range[edit]

Signature
db:text-range(
  $database  as xs:string,
  $min       as xs:string,
  $max       as xs:string
) as text()*
Summary Returns all text nodes of a $database whose values are larger than or equal to $min and smaller than or equal to $max and that are stored in the text index.
Errors open: the addressed database does not exist or could not be opened.
no-index: the index is not available.
Examples
  • db:text-range("DB", "2000", "2001") returns all text nodes of the database DB that are found in between 2000 and 2001.

db:attribute[edit]

Signature
db:attribute(
  $database  as xs:string,
  $values    as xs:string*,
  $name      as xs:string   := ()
) as attribute()*
Summary Returns all attribute nodes of a $database that match one of the specified $values and that are stored in the attribute index.
If $name is specified, the resulting attribute nodes are filtered by their attribute name.
Errors open: the addressed database does not exist or could not be opened.
no-index: the index is not available.
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[edit]

Signature
db:attribute-range(
  $database  as xs:string,
  $min       as xs:string,
  $max       as xs:string,
  $name      as xs:string  := ()
) as attribute()*
Summary Returns all attributes of a $database whose values are larger than or equal to $min and smaller than or equal to $max and that are stored in the attribute index.
Errors open: the addressed database does not exist or could not be opened.
no-index: the index is not available.
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:token[edit]

Signature
db:token(
  $database  as xs:string,
  $tokens    as xs:string*,
  $name      as xs:string   := ()
) as attribute()*
Summary Returns all attribute nodes of a $database the values of which contain one of the specified $tokens.
If $name is specified, the resulting attribute nodes are filtered by their attribute name.
Errors open: the addressed database does not exist or could not be opened.
no-index: the index is not available.
Examples
  • db:token("DB", "row", "class")/parent::div returns all div nodes of database DB with a class attribute that contains the token row.

Updates[edit]

All functions in this section are Updating Functions.

db:create[edit]

Signature
db:create(
  $database  as xs:string,
  $inputs    as item()*     := (),
  $paths     as xs:string*  := (),
  $options   as map(*)?     := map { }
) as empty-sequence()
Summary Creates a new $database and adds the supplied $inputs to the specified $paths:
  • The inputs may be strings or nodes:
    • nodes may be of any type except for attributes
    • strings can be a URI pointing to a file/directory or an XML string (which is detected by the leading < character)
    • a path must be specified if the input is not a file or directory reference
  • The parsing and indexing behavior can be controlled via $options:
  • An existing database will be overwritten.
  • Database creation takes place after most other update operations (see Pending Update List). As a consequence, a newly created database cannot be addressed in the same query.
Errors lock: a database is opened by another process.
name: the specified name is not a valid database name.
conflict: the same database was addressed more than once.
args: the number of specified inputs and paths differs.
Examples
  • db:create("DB") creates the empty database DB.
  • db:create("DB", "/home/dir/doc.xml") creates the database DB and adds the document /home/dir/doc.xml as initial content.
  • db:create("DB", <a/>, "doc.xml") creates the database DB and adds the document with content <a/> under the name doc.xml.
  • db:create("DB", "/home/dir/", "docs/dir") creates the database DB and adds the documents in /home/dir to the database under the path docs/dir.
  • db:create("DB", file:list('.'), (), map { 'ftindex': true() }) adds all files of the current working directory to a new database, preserving relative filesystem paths and creating a full-text index.

db:add[edit]

Signature
db:add(
  $database  as xs:string,
  $input     as item(),
  $path      as xs:string?  := (),
  $options   as map(*)?     := map { }
) as empty-sequence()
Summary Adds the specified $input to a $database with the specified $path:
  • A document with the same path may occur more than once in a database. If you want to enforce single instances, use db:put instead.
  • See db:create for more details on the input and path arguments.
  • The parsing behavior can be controlled via $options:
    • allowed options are ADDCACHE and the parsing and XML parsing options, all in lower case
    • parsing options will only impact string input (URIs, XML strings) because nodes have already been parsed
Errors open: the addressed database does not exist or could not be opened.
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 node to the database DB under the name doc.xml.
  • db:add("DB", "/home/dir", "docs/dir", map { 'addcache': true() }) adds all documents in /home/dir to the database DB under the path docs/dir. To reduce memory consumption, the files will be cached before being added to the database.

db:put[edit]

Signature
db:put(
  $database  as xs:string,
  $input     as item(),
  $path      as xs:string,
  $options   as map(*)?    := map { }
) as empty-sequence()
Summary Replaces a resource, specified by $path, in a $database with the contents of $input, or adds it as a new resource:
  • The parsing behavior can be controlled via $options:
    • Allowed options are ADDCACHE and the parsing and XML parsing options, all in lower case.
    • Parsing options will only impact string input (URIs, XML strings), because nodes have already been parsed.
  • See db:create for more details on the input argument.
Errors open: the addressed database does not exist or could not be opened.
target: the path points to a directory.
Examples
  • db:put("DB", "/home/dir/doc.xml", "docs/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:put("DB", "<a/>", "docs/dir/doc.xml") replaces the content of the document docs/dir/doc.xml in the database DB with <a/>.
  • db:put("DB", document { <a/> }, "docs/dir/doc.xml") replaces the content of the document docs/dir/doc.xml in the database DB with the specified document node.

The following query can be used to import files from a directory to a database:

let $source := '/home/john/xml/source'
for $file in file:list($source, true())
let $path := $source || $file
where not(file:is-dir($path))
return db:put('db', doc($path), $file)

db:put-binary[edit]

Signature
db:put-binary(
  $database  as xs:string,
  $input     as item(),
  $path      as xs:string
) as empty-sequence()
Summary Stores a binary resource specified by $input in a $database at the specified $path. Existing resources are overwritten.
Errors open: the addressed database does not exist or could not be opened.
mainmem: the database is not persistent (stored on disk).
Examples
  • db:put-binary('DB', file:read-binary('video.mov'), 'video/sample.mov') stores the addressed video file at the specified location.
  • With the following query, you can copy the binary resources of one database into another:
let $db := 'db'
let $src-path := 'src/'
let $trg-path := 'trg/'
for $src in db:list($db, $src-path)
where db:type($db, $src) = 'binary'
let $trg := $trg-path || substring-after($src, $src-path)
return db:put-binary($db, db:get-binary($db, $src), $trg)

db:put-value[edit]

Signature
db:put-value(
  $database  as xs:string,
  $input     as item()*,
  $path      as xs:string
) as empty-sequence()
Summary Stores an $input value in a $database at the specified $path. Existing resources are overwritten. The value can be an arbitrary sequence of atomic items, nodes, maps, and arrays.
Errors open: the addressed database does not exist or could not be opened.
mainmem: the database is not persistent (stored on disk).
Examples
  • db:put-value('DB', 1 to 10000, 'sequence') stores a numeric range in the database.
  • With the following query, a map with countries and associated cities is stored in a database. The value resource can e.g. be used as index in future queries:
db:put-value(
  'factbook',
  map:merge(
    for $country in db:get('factbook')//country
    return map:entry($country/@name, $country//city/name ! string())
  ),
  'cities'
)

db:delete[edit]

Signature
db:delete(
  $database  as xs:string,
  $path      as xs:string
) as empty-sequence()
Summary Deletes resource(s), specified by $path, from the specified $database.
Errors open: the addressed database does not exist or could not be opened.
path: the specified path is invalid.
Examples
  • db:delete("DB", "docs/dir/doc.xml") deletes the resource docs/dir/doc.xml from DB.
  • db:delete("DB", "docs/dir") deletes all resources from DB in the specified path docs/dir.

db:copy[edit]

Signature
db:copy(
  $database  as xs:string,
  $new-name  as xs:string
) as empty-sequence()
Summary Creates a copy of $database, which will be called $new-name.
Errors open: the addressed database does not exist or could not be opened.
lock: a database is opened by another process.
name: invalid database name.
conflict: the same database was addressed more than once.

db:alter[edit]

Signature
db:alter(
  $database  as xs:string,
  $new-name  as xs:string
) as empty-sequence()
Summary Renames a $database to $new-name.
Errors open: the addressed database does not exist or could not be opened.
lock: a database is opened by another process.
name: invalid database name.
conflict: the same database was addressed more than once.

db:optimize[edit]

Signature
db:optimize(
  $database  as xs:string,
  $all       as xs:boolean?  := false(),
  $options   as map(*)?      := map { }
) as empty-sequence()
Summary Optimizes the metadata and indexes of a $database.
If $all is true, the complete database will be rebuilt.
The $options argument can be used to control indexing. The syntax is identical to the db:create function: Allowed options are all indexing and full-text options. UPDINDEX is only supported if $all is true.
Errors open: the addressed database does not exist or could not be opened.
Examples
  • db:optimize("DB") optimizes the database structures of the database DB.
  • db:optimize("DB", true(), map { 'ftindex': true() }) optimizes all database structures of the database DB and creates a full-text index.

db:rename[edit]

Signature
db:rename(
  $database  as xs:string,
  $source    as xs:string,
  $target    as xs:string
) as empty-sequence()
Summary Moves all resources(s) of a $database, which are found in the supplied $source path, to the supplied $target path. The paths may point to single resources or directories. No updates will take place if a non-existing source path is supplied.
Errors open: the addressed database does not exist or could not be opened.
path: the specified source or target path, or one of its descendants, is invalid.
Examples
  • db:rename("DB", "docs/dir/doc.xml", "docs/dir/newdoc.xml") renames the resource docs/dir/doc.xml to docs/dir/newdoc.xml in the database DB.
  • db:rename("DB", "docs/dir", "docs/newdir") moves all resources in the database DB from docs/dir to docs/newdir.

db:flush[edit]

Signature
db:flush(
  $database  as xs:string
) as empty-sequence()
Summary Explicitly flushes the buffers of a $database. This command is only useful if AUTOFLUSH has been set to false.
Errors open: the addressed database does not exist or could not be opened.

db:drop[edit]

Signature
db:drop(
  $database  as xs:string
) as empty-sequence()
Summary Drops a $database and all connected resources.
Errors open: the addressed database does not exist or could not be opened.
lock: a database is opened by another process.
conflict: the same database was addressed more than once.
Examples
  • db:drop("DB") drops the database DB.

Backups[edit]

All functions in this section except for db:backups are Updating Functions.

db:create-backup[edit]

Signature
db:create-backup(
  $database  as xs:string,
  $options   as map(*)?    := map { }
) as empty-sequence()
Summary Creates a backup of a $database. If no name is supplied, general data will be backed up. The following $options are available:
  • With comment, a comment string can be attached to the backup.
  • By setting compress to false, the backup will be created faster, but it will take more space on disk.
Errors open: the addressed database does not exist or could not be opened.
name: invalid database name.
conflict: the same database was addressed more than once.
Examples
  • db:create-backup('DB', map { 'compress': false() }) creates a backup of the database DB without compressing its entries.

db:drop-backup[edit]

Signature
db:drop-backup(
  $name  as xs:string
) as empty-sequence()
Summary Drops all backups of the database with the specified $name. If the name ends with a timestamp, only the specified backup file will be deleted. If no name is supplied, backups with general data are addressed.
Errors backup: No backup file found.
name: invalid database name.
conflict: the same database was addressed more than once.
Examples
  • db:drop-backup("DB") drops all backups of the database DB.
  • db:drop-backup("DB-2014-03-13-17-36-44") drops the specific backup file DB-2014-03-13-17-36-44.zip of the database DB.

db:alter-backup[edit]

Signature
db:alter-backup(
  $name      as xs:string,
  $new-name  as xs:string
) as empty-sequence()
Summary Renames all backups of the database with the specified $name to $new-name. If the name ends with a date, only the specified backup file will be renamed.
Errors backup: No backup file found.
name: invalid database name.
conflict: the same database was addressed more than once.
Examples
  • db:alter-backup("DB", "DB2) renames all backups of the database DB to DB2.

db:restore[edit]

Signature
db:restore(
  $name  as xs:string
) as empty-sequence()
Summary Restores the database with the specified $name. The $name may include the timestamp of the backup file. If no name is supplied, general data will be restored. If general data is restored, it will only be available after BaseX has been restarted.
Errors lock: a database is opened by another process.
name: invalid database name.
no-backup: No backup found.
conflict: the same database was addressed more than once.
Examples
  • db:restore("DB") restores the database DB.
  • db:restore("DB-2014-03-13-18-05-45") restores the database DB from the backup file with the given timestamp.

db:backups[edit]

Signature
db:backups(
  $database  as xs:string  := ()
) as element(backup)*
Summary Returns an element sequence containing all available database backups with timestamp, file size and comment.
If a $database is specified, the sequence will be restricted to the backups matching this database.
Examples
  • db:backups("factbook") returns all backups that have been made from the factbook database.

Helper Functions[edit]

db:name[edit]

Signature
db:name(
  $node  as node()
) as xs:string
Summary Returns the name of the database in which the specified database node $node is stored.
Errors node: $nodes contains a node which is not stored in a database.

db:path[edit]

Signature
db:path(
  $node  as node()
) as xs:string
Summary Returns the path of the database document in which the specified database node $node is stored.
Errors node: $nodes contains a node which is not stored in a database.

db:exists[edit]

Signature
db:exists(
  $database  as xs:string,
  $path      as xs:string  := ()
) as xs:boolean
Summary Checks if a $database exists, or a resource located at $path in this database.
Examples
  • db:exists("DB") returns true if the database DB exists.
  • db:exists("DB", "resource") returns true if resource exists in this database.

db:type[edit]

Signature
db:type(
  $database  as xs:string,
  $path      as xs:string
) as xs:string
Summary Returns the type (xml, binary, value) of a resource in a $database at the specified $path.
Errors open: the addressed database does not exist or could not be opened.
Examples
  • db:type("DB", "factbook.xml") returns true if the specified resource is an XML document.

db:content-type[edit]

Signature
db:content-type(
  $database  as xs:string,
  $path      as xs:string
) as xs:string
Summary Retrieves the content-type of a resource in a $database at the specified $path.
The file extension is used to recognize the content-type of a resource stored in the database. application/xml will be returned for any XML document stored in the database, regardless of its file name extension.
Errors open: the addressed database does not exist or could not be opened.
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.

Errors[edit]

Code Description
args The number of specified inputs and paths differs.
conflict Multiple update operations point to the same target.
lock A database cannot be updated because it is opened by another process.
mainmem The addressed database is not persistent (stored on disk).
name The name of the specified database is invalid.
no-backup No backup exists for a database.
node The referenced XML node is no database node, i.e. it is neither stored in a database nor represented as database fragment.
no-index The database lacks an index structure required by the called function.
open The addressed database does not exist or could not be opened.
option The specified option is unknown.
path The specified database path is invalid.
property The specified database property is unknown.
range The addressed database ID or PRE value is out of range.
target Path points to an invalid target.

Changelog[edit]

Version 10
Version 9.3
  • Added: db:alter-backup
  • Updated: db:open-id, db:open-pre: support for multiple integers
Version 9.2
  • Added: db:dir
  • Updated: db:add: $path allow empty path argument
Version 9.0
Version 8.6
Version 8.4
Version 8.3
  • Updated: db:list-details: attributes with name of database and date of backup added to results.
  • Updated: db:backups now include attributes with name of database and date of backup.
  • Updated: Value Indexes: raise error if no index exists.
Version 8.2
Version 7.9
Version 7.8.2
Version 7.8
Version 7.7
Version 7.6
  • Updated: db:create: allow more than one input and path.
Version 7.5
Version 7.3
Version 7.2.1
Version 7.1
Version 7.0