Difference between revisions of "Database Module"

From BaseX Documentation
Jump to navigation Jump to search
m (Text replacement - "[[Jobs Module" to "[[Job Module")
(21 intermediate revisions by the same user not shown)
Line 41: Line 41:
  
 
{| width='100%'
 
{| width='100%'
|-
+
|- valign="top"
 
| width='120' | '''Signatures'''
 
| width='120' | '''Signatures'''
 
|{{Func|db:system||element(system)}}
 
|{{Func|db:system||element(system)}}
|-
+
|- valign="top"
 
| '''Summary'''
 
| '''Summary'''
 
|Returns general information on the database system the current values of all global and local [[Options]]. The {{Command|INFO}} command returns similar output.
 
|Returns general information on the database system the current values of all global and local [[Options]]. The {{Command|INFO}} command returns similar output.
Line 52: Line 52:
  
 
{| width='100%'
 
{| width='100%'
|-
+
|- valign="top"
 
| width='120' | '''Signatures'''
 
| width='120' | '''Signatures'''
 
|{{Func|db:option|$name as xs:string|xs:string}}
 
|{{Func|db:option|$name as xs:string|xs:string}}
|-
+
|- valign="top"
 
| '''Summary'''
 
| '''Summary'''
|Returns the current value (string, integer, boolean, map) of a global or local [[Options|Option]] with the specified {{Code|$name}}. The {{Command|GET}} command works similar.
+
|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'''
 
| '''Errors'''
 
|{{Error|option|#Errors}} the specified option is unknown.
 
|{{Error|option|#Errors}} the specified option is unknown.
|-
+
|- valign="top"
 
| '''Examples'''
 
| '''Examples'''
 
|
 
|
 
* <code>db:option('dbpath')</code> returns the database path string.
 
* <code>db:option('dbpath')</code> returns the database path string.
 
* <code>db:option('serializer')</code> returns a map with the current serialization parameters.
 
* <code>db:option('serializer')</code> returns a map with the current serialization parameters.
* <code>declare option db:chop 'true'; db:option('chop')</code> returns {{Code|true}} (irrespective of the global value).
+
* <code>declare option db:stripws 'true'; db:option('stripws')</code> returns the locally assigned value.
 
|}
 
|}
  
Line 72: Line 72:
  
 
{| width='100%'
 
{| width='100%'
|-
+
|- valign="top"
 
| width='120' | '''Signatures'''
 
| width='120' | '''Signatures'''
 
|{{Func|db:info|$db as xs:string|element(database)}}
 
|{{Func|db:info|$db as xs:string|element(database)}}
|-
+
|- valign="top"
 
| '''Summary'''
 
| '''Summary'''
 
|Returns meta information on the database {{Code|$db}}. The output is similar to the {{Command|INFO DB}} command.
 
|Returns meta information on the database {{Code|$db}}. The output is similar to the {{Command|INFO DB}} command.
|-
+
|- valign="top"
 
| '''Errors'''
 
| '''Errors'''
 
|{{Error|open|#Errors}} the addressed database does not exist or could not be opened.
 
|{{Error|open|#Errors}} the addressed database does not exist or could not be opened.
Line 86: Line 86:
  
 
{| width='100%'
 
{| width='100%'
|-
+
|- valign="top"
 
| width='120' | '''Signatures'''
 
| width='120' | '''Signatures'''
 
|{{Func|db:property|$db as xs:string, $name as xs:string|xs:anyAtomicType}}
 
|{{Func|db:property|$db as xs:string, $name as xs:string|xs:anyAtomicType}}
|-
+
|- valign="top"
 
| '''Summary'''
 
| '''Summary'''
 
|Returns the value (string, boolean, integer) of a property with the specified {{Code|$name}} in the database {{Code|$db}}. The available properties are the ones returned by {{Function||db:info}}.
 
|Returns the value (string, boolean, integer) of a property with the specified {{Code|$name}} in the database {{Code|$db}}. The available properties are the ones returned by {{Function||db:info}}.
|-
+
|- valign="top"
 
| '''Errors'''
 
| '''Errors'''
 
|{{Error|property|#Errors}} the specified property is unknown.
 
|{{Error|property|#Errors}} the specified property is unknown.
|-
+
|- valign="top"
 
| '''Examples'''
 
| '''Examples'''
 
|
 
|
Line 106: Line 106:
  
 
{| width='100%'
 
{| width='100%'
|-
+
|- valign="top"
 
| width='120' | '''Signatures'''
 
| width='120' | '''Signatures'''
 
|{{Func|db:list||xs:string*}}<br/>{{Func|db:list|$db as xs:string|xs:string*}}<br/>{{Func|db:list|$db as xs:string, $path as xs:string|xs:string*}}
 
|{{Func|db:list||xs:string*}}<br/>{{Func|db:list|$db as xs:string|xs:string*}}<br/>{{Func|db:list|$db as xs:string, $path as xs:string|xs:string*}}
|-
+
|- valign="top"
 
| '''Summary'''
 
| '''Summary'''
 
|The result of this function is dependent on the number of arguments:
 
|The result of this function is dependent on the number of arguments:
Line 115: Line 115:
 
* If a database {{Code|$db}} is specified, all documents and raw files of the specified database are returned.
 
* If a database {{Code|$db}} is specified, all documents and raw files of the specified database are returned.
 
* The list of returned resources can be restricted by the {{Code|$path}} argument.
 
* The list of returned resources can be restricted by the {{Code|$path}} argument.
|-
+
|- valign="top"
 
| '''Errors'''
 
| '''Errors'''
 
|{{Error|open|#Errors}} the addressed database does not exist or could not be opened.
 
|{{Error|open|#Errors}} the addressed database does not exist or could not be opened.
|-
+
|- valign="top"
 
| '''Examples'''
 
| '''Examples'''
 
|
 
|
Line 127: Line 127:
  
 
{| width='100%'
 
{| width='100%'
|-
+
|- valign="top"
 
| width='120' | '''Signatures'''
 
| width='120' | '''Signatures'''
 
|{{Func|db:list-details||element(database)*}}<br/>{{Func|db:list-details|$db as xs:string|element(resource)*}}<br/>{{Func|db:list-details|$db as xs:string, $path as xs:string|element(resource)*}}
 
|{{Func|db:list-details||element(database)*}}<br/>{{Func|db:list-details|$db as xs:string|element(resource)*}}<br/>{{Func|db:list-details|$db as xs:string, $path as xs:string|element(resource)*}}
|-
+
|- valign="top"
 
| '''Summary'''
 
| '''Summary'''
 
|Without arguments, an element is returned for each database that is accessible to the current user:
 
|Without arguments, an element is returned for each database that is accessible to the current user:
Line 138: Line 138:
 
* 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 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).
 
* Returned databases resources can be further restricted by the {{Code|$path}} argument.
 
* Returned databases resources can be further restricted by the {{Code|$path}} argument.
|-
+
|- valign="top"
 
| '''Errors'''
 
| '''Errors'''
 
|{{Error|open|#Errors}} the addressed database does not exist or could not be opened.
 
|{{Error|open|#Errors}} the addressed database does not exist or could not be opened.
|-
+
|- valign="top"
 
| '''Examples'''
 
| '''Examples'''
 
|
 
|
Line 150: Line 150:
  
 
{| width='100%'
 
{| width='100%'
|-
+
|- valign="top"
 
| width='120' | '''Signatures'''
 
| width='120' | '''Signatures'''
 
|{{Func|db:dir|$db as xs:string, $path as xs:string|element()*}}
 
|{{Func|db:dir|$db as xs:string, $path as xs:string|element()*}}
|-
+
|- valign="top"
 
| '''Summary'''
 
| '''Summary'''
|Returns meta data on all directories and resources of the database {{Code|$db}} in the specified directory {{Code|$path}}. Two types of elements are returned:
+
|Returns metadata on all directories and resources of the database {{Code|$db}} in the specified directory {{Code|$path}}. Two types of elements are returned:
 
* {{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.
 
* {{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.
 
* {{Code|dir}} represents a directory. The element value is the directory path; the modification date is returned as attribute.
 
* {{Code|dir}} represents a directory. The element value is the directory path; the modification date is returned as attribute.
 
Please note that directories are not stored in BaseX. Instead, they result implicitly from the paths of stored resources.
 
Please note that directories are not stored in BaseX. Instead, they result implicitly from the paths of stored resources.
|-
+
|- valign="top"
 
| '''Errors'''
 
| '''Errors'''
 
|{{Error|open|#Errors}} the addressed database does not exist or could not be opened.<br/>{{Error|path|#Errors}} the specified path is invalid.
 
|{{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'''
 
| '''Examples'''
 
|
 
|
Line 170: Line 170:
 
=Read Operations=
 
=Read Operations=
  
==db:open==
+
==db:get==
 +
 
 +
{{Announce|Updated with Version 10:}} Renamed (before: {{Code|db:open}}). Due to its widespread use, the old function name will be supported for some more time.
  
 
{| width='100%'
 
{| width='100%'
|-
+
|- valign="top"
 
| width='120' | '''Signatures'''
 
| width='120' | '''Signatures'''
|{{Func|db:open|$db as xs:string|document-node()*}}<br />{{Func|db:open|$db as xs:string, $path as xs:string|document-node()*}}
+
|{{Func|db:get|$db as xs:string|document-node()*}}<br />{{Func|db:get|$db as xs:string, $path as xs:string|document-node()*}}
|-
+
|- valign="top"
 
| '''Summary'''
 
| '''Summary'''
|Opens the database {{Code|$db}} and returns all document nodes.<br/>The document nodes to be returned can be filtered with the {{Code|$path}} argument.
+
|Returns all documents from the database {{Code|$db}}, or only documents matching the specified {{Code|$path}}.
|-
+
|- valign="top"
 
| '''Errors'''
 
| '''Errors'''
 
|{{Error|open|#Errors}} the addressed database does not exist or could not be opened.
 
|{{Error|open|#Errors}} the addressed database does not exist or could not be opened.
|-
+
|- valign="top"
 
| '''Examples'''
 
| '''Examples'''
 
|
 
|
* {{Code|db:open("docs")}} returns all documents from the database named {{Code|docs}}.
+
* {{Code|db:get('docs')}} returns all documents from the database named {{Code|docs}}.
* {{Code|db:open("db", "one")}} returns all documents from the database named {{Code|db}} located in the path {{Code|one}}.
+
* {{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:open("db" || $i)//item</code> returns all item elements from the databases {{Code|db1}}, {{Code|db2}} and {{Code|db3}}.
+
* <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:open-pre==
+
==db:get-pre==
 +
 
 +
{{Announce|Updated with Version 10:}} Renamed (before: {{Code|db:open-pre}}).
  
 
{| width='100%'
 
{| width='100%'
|-
+
|- valign="top"
 
| width='120' | '''Signatures'''
 
| width='120' | '''Signatures'''
|{{Func|db:open-pre|$db as xs:string, $pres as xs:integer*|node()*}}
+
|{{Func|db:get-pre|$db as xs:string, $pres as xs:integer*|node()*}}
|-
+
|- valign="top"
 
| '''Summary'''
 
| '''Summary'''
|Opens the database {{Code|$db}} and returns all distinct nodes with the pre values {{Code|$pres}} in 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.
+
|Returns all nodes from the database {{Code|$db}} with the pre values {{Code|$pres}} 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.
|-
+
|- valign="top"
 
| '''Errors'''
 
| '''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.
 
|{{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'''
 
| '''Examples'''
 
|
 
|
* {{Code|db:open-pre("docs", 0)}} returns the first database node from the database named {{Code|docs}}.
+
* {{Code|db:get-pre("docs", 0)}} returns the first database node from the database named {{Code|docs}}.
 
|}
 
|}
  
==db:open-id==
+
==db:get-id==
 +
 
 +
{{Announce|Updated with Version 10:}} Renamed (before: {{Code|open-id}}).
  
 
{| width='100%'
 
{| width='100%'
|-
+
|- valign="top"
 
| width='120' | '''Signatures'''
 
| width='120' | '''Signatures'''
|{{Func|db:open-id|$db as xs:string, $ids as xs:integer*|node()*}}
+
|{{Func|db:get-id|$db as xs:string, $ids as xs:integer*|node()*}}
|-
+
|- valign="top"
 
| '''Summary'''
 
| '''Summary'''
|Opens the database {{Code|$db}} and returns all distinct nodes with the specified {{Code|$ids}} in 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.
+
|Returns all nodes from the database {{Code|$db}} with the pre values {{Code|$ids}} 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'''
 
| '''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.
+
|{{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:node-pre==
+
==db:get-binary==
 +
 
 +
{{Announce|Updated with Version 10}}: renamed (before: {{Code|db:retrieve}}).
  
 
{| width='100%'
 
{| width='100%'
|-
+
|- valign="top"
 
| width='120' | '''Signatures'''
 
| width='120' | '''Signatures'''
|{{Func|db:node-pre|$nodes as node()*|xs:integer*}}
+
|{{Func|db:get-binary|$db as xs:string, $path as xs:string|item()}}
|-
+
|- valign="top"
 
| '''Summary'''
 
| '''Summary'''
|Returns the ''pre'' values of the specified {{Code|$nodes}}, which must all be {{Function|Database|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.
+
|Returns a map with all paths and binary resources of the database {{Code|$db}}. 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'''
 
| '''Errors'''
|{{Error|node|#Errors}} {{Code|$nodes}} contains a node which is not stored in a database.
+
|{{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'''
 
| '''Examples'''
 
|
 
|
* {{Code|db:node-pre(doc("input"))}} returns {{Code|0}} if the database {{Code|input}} contains a single document.
+
* {{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:node-id==
+
==db:get-value==
 +
 
 +
{{Announce|Introduced with Version 10.}}
  
 
{| width='100%'
 
{| width='100%'
|-
+
|- valign="top"
 
| width='120' | '''Signatures'''
 
| width='120' | '''Signatures'''
|{{Func|db:node-id|$nodes as node()*|xs:integer*}}
+
|{{Func|db:get-value|$db as xs:string, $path as xs:string|item()*}}
|-
+
|- valign="top"
 
| '''Summary'''
 
| '''Summary'''
|Returns the ''id'' values of the specified {{Code|$nodes}}, which must all be {{Function|Database|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.
+
|Returns a map with all paths and values of the database {{Code|$db}}. A single value is returned if a {{Code|$path}} is specified.
|-
+
|- valign="top"
 
| '''Errors'''
 
| '''Errors'''
|{{Error|node|#Errors}} {{Code|$nodes}} contains a node which is not stored in a database.
+
|{{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:get==
+
==db:node-pre==
 
 
{{Announce|Introduced with Version 10.}}
 
  
 
{| width='100%'
 
{| width='100%'
|-
+
|- valign="top"
 
| width='120' | '''Signatures'''
 
| width='120' | '''Signatures'''
|{{Func|db:get|$db as xs:string, $path as xs:string|item()*}}
+
|{{Func|db:node-pre|$nodes as node()*|xs:integer*}}
|-
+
|- valign="top"
 
| '''Summary'''
 
| '''Summary'''
|Returns an XQuery value stored in the database {{Code|$db}} at the specified {{Code|$path}}.
+
|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"
 
| '''Errors'''
 
| '''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).
+
|{{Error|node|#Errors}} {{Code|$nodes}} contains a node which is not stored in a database.
|-
+
|- valign="top"
 
| '''Examples'''
 
| '''Examples'''
 
|
 
|
* {{Code|db:get('DB', 'sequence')}} returns the specified sequence.
+
* {{Code|db:node-pre(doc("input"))}} returns {{Code|0}} if the database {{Code|input}} contains a single document.
 
|}
 
|}
  
==db:get-binary==
+
==db:node-id==
 
 
{{Announce|Updated with Version 10}}: renamed (before: {{Code|db:retrieve}}).
 
  
 
{| width='100%'
 
{| width='100%'
|-
+
|- valign="top"
 
| width='120' | '''Signatures'''
 
| width='120' | '''Signatures'''
|{{Func|db:get-binary|$db as xs:string, $path as xs:string|xs:base64Binary}}
+
|{{Func|db:node-id|$nodes as node()*|xs:integer*}}
|-
+
|- valign="top"
 
| '''Summary'''
 
| '''Summary'''
|Returns a [[Binary Data|binary resource]] addressed by the database {{Code|$db}} and {{Code|$path}} as [[Streaming Module|streamable]] {{Code|xs:base64Binary}}.
+
|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'''
 
| '''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).
+
|{{Error|node|#Errors}} {{Code|$nodes}} contains a node which is not stored in a database.
|-
 
| '''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.
 
 
|}
 
|}
  
Line 299: Line 305:
  
 
{| width='100%'
 
{| width='100%'
|-
+
|- valign="top"
 
| width='120' | '''Signatures'''
 
| width='120' | '''Signatures'''
 
|{{Func|db:export|$db as xs:string, $path as xs:string|empty-sequence()}}<br />{{Func|db:export|$db as xs:string, $path as xs:string, $params as item()|empty-sequence()}}<br />
 
|{{Func|db:export|$db as xs:string, $path as xs:string|empty-sequence()}}<br />{{Func|db:export|$db as xs:string, $path as xs:string, $params as item()|empty-sequence()}}<br />
|-
+
|- valign="top"
 
| '''Summary'''
 
| '''Summary'''
 
|Exports the specified database {{Code|$db}} to the specified file {{Code|$path}}. Existing files will be overwritten.<br />The {{Code|$params}} argument contains [[Serialization|serialization parameters]]. As with [https://www.w3.org/TR/xpath-functions-31/#func-serialize fn:serialize()], the parameters can be specified<br />
 
|Exports the specified database {{Code|$db}} to the specified file {{Code|$path}}. Existing files will be overwritten.<br />The {{Code|$params}} argument contains [[Serialization|serialization parameters]]. As with [https://www.w3.org/TR/xpath-functions-31/#func-serialize fn:serialize()], the parameters can be specified<br />
Line 317: Line 323:
 
map { "method": "xml", "cdata-section-elements": "div", ... }
 
map { "method": "xml", "cdata-section-elements": "div", ... }
 
</syntaxhighlight>
 
</syntaxhighlight>
|-
+
|- valign="top"
 
| '''Errors'''
 
| '''Errors'''
 
|{{Error|open|#Errors}} the addressed database does not exist or could not be opened.
 
|{{Error|open|#Errors}} the addressed database does not exist or could not be opened.
|-
+
|- valign="top"
 
| '''Examples'''
 
| '''Examples'''
 
| Export all files as text:<br/>
 
| Export all files as text:<br/>
Line 326: Line 332:
 
db:export("DB", "/home/john/xml/texts", map { 'method': 'text' })
 
db:export("DB", "/home/john/xml/texts", map { 'method': 'text' })
 
</syntaxhighlight>
 
</syntaxhighlight>
The following query can be used to export parts of the database:
+
The following code can be used to export parts of the database:
 
<syntaxhighlight lang="xquery">
 
<syntaxhighlight lang="xquery">
 
let $target := '/home/john/xml/target'
 
let $target := '/home/john/xml/target'
for $doc in db:open('DB', 'collection')
+
for $doc in db:get('DB', 'collection')
 
let $path := $target || db:path($doc)
 
let $path := $target || db:path($doc)
 
return (
 
return (
Line 343: Line 349:
  
 
{| width='100%'
 
{| width='100%'
|-
+
|- valign="top"
 
| width='120' | '''Signatures'''
 
| width='120' | '''Signatures'''
 
|{{Func|db:text|$db as xs:string, $strings as xs:string*|text()*}}
 
|{{Func|db:text|$db as xs:string, $strings as xs:string*|text()*}}
|-
+
|- valign="top"
 
| '''Summary'''
 
| '''Summary'''
 
|Returns all text nodes of the database {{Code|$db}} that have one of the specified {{Code|$strings}} as values and that are stored in the text index.
 
|Returns all text nodes of the database {{Code|$db}} that have one of the specified {{Code|$strings}} as values and that are stored in the text index.
|-
+
|- valign="top"
 
| '''Errors'''
 
| '''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/>
 
|{{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'''
 
| '''Examples'''
 
|
 
|
Line 361: Line 367:
  
 
{| width='100%'
 
{| width='100%'
|-
+
|- valign="top"
 
| width='120' | '''Signatures'''
 
| width='120' | '''Signatures'''
 
|{{Func|db:text-range|$db as xs:string, $min as xs:string, $max as xs:string|text()*}}
 
|{{Func|db:text-range|$db as xs:string, $min as xs:string, $max as xs:string|text()*}}
|-
+
|- valign="top"
 
| '''Summary'''
 
| '''Summary'''
 
|Returns all text nodes of the database {{Code|$db}} whose values are between {{Code|$min}} and {{Code|$max}} and that are stored in the text index.
 
|Returns all text nodes of the database {{Code|$db}} whose values are between {{Code|$min}} and {{Code|$max}} and that are stored in the text index.
|-
+
|- valign="top"
 
| '''Errors'''
 
| '''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/>
 
|{{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'''
 
| '''Examples'''
 
|
 
|
Line 379: Line 385:
  
 
{| width='100%'
 
{| width='100%'
|-
+
|- valign="top"
 
| width='120' | '''Signatures'''
 
| width='120' | '''Signatures'''
 
|{{Func|db:attribute|$db as xs:string, $strings as xs:string*|attribute()*}}<br/>{{Func|db:attribute|$db as xs:string, $strings as xs:string*, $name as xs:string|attribute()*}}
 
|{{Func|db:attribute|$db as xs:string, $strings as xs:string*|attribute()*}}<br/>{{Func|db:attribute|$db as xs:string, $strings as xs:string*, $name as xs:string|attribute()*}}
|-
+
|- valign="top"
 
| '''Summary'''
 
| '''Summary'''
 
|Returns all attribute nodes of the database {{Code|$db}} that have one of the specified {{Code|$strings}} as 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.
 
|Returns all attribute nodes of the database {{Code|$db}} that have one of the specified {{Code|$strings}} as 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'''
 
| '''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/>
 
|{{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'''
 
| '''Examples'''
 
|
 
|
Line 397: Line 403:
  
 
{| width='100%'
 
{| width='100%'
|-
+
|- valign="top"
 
| width='120' | '''Signatures'''
 
| width='120' | '''Signatures'''
 
|{{Func|db:attribute-range|$db as xs:string, $min as xs:string, $max as xs:string|attribute()*}}<br/>{{Func|db:attribute-range|$db as xs:string, $min as xs:string, $max as xs:string, $name as xs:string|attribute()*}}
 
|{{Func|db:attribute-range|$db as xs:string, $min as xs:string, $max as xs:string|attribute()*}}<br/>{{Func|db:attribute-range|$db as xs:string, $min as xs:string, $max as xs:string, $name as xs:string|attribute()*}}
|-
+
|- valign="top"
 
| '''Summary'''
 
| '''Summary'''
 
|Returns all attributes of the database {{Code|$db}}, the string values of which are larger than or equal to {{Code|$min}} and smaller than or equal to {{Code|$max}} and that are stored in the attribute index.
 
|Returns all attributes of the database {{Code|$db}}, the string values of which 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'''
 
| '''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/>
 
|{{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'''
 
| '''Examples'''
 
|
 
|
Line 415: Line 421:
  
 
{| width='100%'
 
{| width='100%'
|-
+
|- valign="top"
 
| width='120' | '''Signatures'''
 
| width='120' | '''Signatures'''
 
|{{Func|db:token|$db as xs:string, $tokens as xs:string*|attribute()*}}<br/>{{Func|db:token|$db as xs:string, $tokens as xs:string*, $name as xs:string|attribute()*}}
 
|{{Func|db:token|$db as xs:string, $tokens as xs:string*|attribute()*}}<br/>{{Func|db:token|$db as xs:string, $tokens as xs:string*, $name as xs:string|attribute()*}}
|-
+
|- valign="top"
 
| '''Summary'''
 
| '''Summary'''
 
|Returns all attribute nodes of the database {{Code|$db}} 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.
 
|Returns all attribute nodes of the database {{Code|$db}} 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'''
 
| '''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/>
 
|{{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'''
 
| '''Examples'''
 
|
 
|
Line 432: Line 438:
 
=Updates=
 
=Updates=
  
All functions in this section are {{Function|Database|Updating Functions}}.
+
All functions in this section are [[#Updating Functions|Updating Functions]].
  
 
==db:create==
 
==db:create==
  
 
{| width='100%'
 
{| width='100%'
|-
+
|- valign="top"
 
| width='120' | '''Signatures'''
 
| width='120' | '''Signatures'''
 
|{{Func|db:create|$db as xs:string|empty-sequence()}}<br/>{{Func|db:create|$db as xs:string, $inputs as item()*|empty-sequence()}}<br/>{{Func|db:create|$db as xs:string, $inputs as item()*, $paths as xs:string*|empty-sequence()}}<br/>{{Func|db:create|$db as xs:string, $inputs as item()*, $paths as xs:string*, $options as map(*)?|empty-sequence()}}
 
|{{Func|db:create|$db as xs:string|empty-sequence()}}<br/>{{Func|db:create|$db as xs:string, $inputs as item()*|empty-sequence()}}<br/>{{Func|db:create|$db as xs:string, $inputs as item()*, $paths as xs:string*|empty-sequence()}}<br/>{{Func|db:create|$db as xs:string, $inputs as item()*, $paths as xs:string*, $options as map(*)?|empty-sequence()}}
|-
+
|- valign="top"
 
| '''Summary'''
 
| '''Summary'''
 
|Creates a new database with name {{Code|$db}} and adds initial documents specified via {{Code|$inputs}} to the specified {{Code|$paths}}:
 
|Creates a new database with name {{Code|$db}} and adds initial documents specified via {{Code|$inputs}} to the specified {{Code|$paths}}:
Line 452: Line 458:
 
* An existing database will be overwritten.
 
* 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.
 
* 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'''
 
| '''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.
 
|{{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'''
 
| '''Examples'''
 
|
 
|
Line 463: Line 469:
 
* {{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", "/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.
 
* <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:drop==
 
 
{| width='100%'
 
|-
 
| width='120' | '''Signatures'''
 
|{{Func|db:drop|$db as xs:string|empty-sequence()}}
 
|-
 
| '''Summary'''
 
|Drops the database {{Code|$db}} and all connected resources.
 
|-
 
| '''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.
 
|-
 
| '''Examples'''
 
|
 
* {{Code|db:drop("DB")}} drops the database {{Code|DB}}.
 
 
|}
 
|}
  
Line 486: Line 474:
  
 
{| width='100%'
 
{| width='100%'
|-
+
|- valign="top"
 
| width='120' | '''Signatures'''
 
| width='120' | '''Signatures'''
 
|{{Func|db:add|$db as xs:string, $input as item()|empty-sequence()}}<br/>{{Func|db:add|$db as xs:string, $input as item(), $path as xs:string?|empty-sequence()}}<br/>{{Func|db:add|$db as xs:string, $input as item(), $path as xs:string?, $options as map(*)?|empty-sequence()|empty-sequence()}}
 
|{{Func|db:add|$db as xs:string, $input as item()|empty-sequence()}}<br/>{{Func|db:add|$db as xs:string, $input as item(), $path as xs:string?|empty-sequence()}}<br/>{{Func|db:add|$db as xs:string, $input as item(), $path as xs:string?, $options as map(*)?|empty-sequence()|empty-sequence()}}
|-
+
|- valign="top"
 
| '''Summary'''
 
| '''Summary'''
 
|Adds documents specified by {{Code|$input}} to the database {{Code|$db}} with the specified {{Code|$path}}:
 
|Adds documents specified by {{Code|$input}} to the database {{Code|$db}} 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:update}} instead.
+
* 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.
 
* See {{Function||db:create}} for more details on the input and path arguments.
 
* The parsing behavior can be controlled via {{Code|$options}}:
 
* 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
 
** 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
 
** parsing options will only impact string input (URIs, XML strings), because nodes have already been parsed
|-
+
|- valign="top"
 
| '''Errors'''
 
| '''Errors'''
 
|{{Error|open|#Errors}} the addressed database does not exist or could not be opened.
 
|{{Error|open|#Errors}} the addressed database does not exist or could not be opened.
|-
+
|- valign="top"
 
| '''Examples'''
 
| '''Examples'''
 
|
 
|
Line 506: Line 494:
 
* <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", <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}}. In order to reduce memory consumption, the files will be cached before being added to the database.
 
* <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}}. In order to reduce memory consumption, the files will be cached before being added to the database.
 +
|}
 +
 +
==db:put==
 +
 +
{{Announce|Updated with Version 10}}: renamed (before: {{Code|db:replace}}); function signature aligned with {{Function||db:add}} (second and third argument swapped).
 +
 +
{| width='100%'
 +
|- valign="top"
 +
| width='120' | '''Signatures'''
 +
|{{Func|db:put|$db as xs:string, $input as item(), $path as xs:string|empty-sequence()}}<br/>{{Func|db:put|$db as xs:string, $input as item(), $path as xs:string, $options as map(*)?|empty-sequence()|empty-sequence()}}
 +
|- valign="top"
 +
| '''Summary'''
 +
|Replaces a resource, specified by {{Code|$path}}, in the database {{Code|$db}} 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:
 +
<syntaxhighlight 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)
 +
</syntaxhighlight>
 +
|}
 +
 +
==db:put-binary==
 +
 +
{{Announce|Updated with Version 10}}: renamed (before: {{Code|db:put}}); function signature aligned with {{Function||db:add}} (second and third argument swapped).
 +
 +
{| width='100%'
 +
|- valign="top"
 +
| width='120' | '''Signatures'''
 +
|{{Func|db:put-binary|$db as xs:string, $input as item(), $path as xs:string|empty-sequence()}}
 +
|- valign="top"
 +
| '''Summary'''
 +
|Stores a binary resource specified by {{Code|$input}} in the database {{Code|$db}} 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:
 +
<syntaxhighlight 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)
 +
</syntaxhighlight>
 +
|}
 +
 +
==db:put-value==
 +
 +
{{Announce|Introduced with Version 10.}}
 +
 +
{| width='100%'
 +
|- valign="top"
 +
| width='120' | '''Signatures'''
 +
|{{Func|db:put-value|$db as xs:string, $input as item()*, $path as xs:string|empty-sequence()}}
 +
|- valign="top"
 +
| '''Summary'''
 +
|Stores a value specified by {{Code|$input}} in the database {{Code|$db}} 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:
 +
<syntaxhighlight 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'
 +
)</syntaxhighlight>
 
|}
 
|}
  
Line 511: Line 593:
  
 
{| width='100%'
 
{| width='100%'
|-
+
|- valign="top"
 
| width='120' | '''Signatures'''
 
| width='120' | '''Signatures'''
 
|{{Func|db:delete|$db as xs:string, $path as xs:string|empty-sequence()}}
 
|{{Func|db:delete|$db as xs:string, $path as xs:string|empty-sequence()}}
|-
+
|- valign="top"
 
| '''Summary'''
 
| '''Summary'''
 
|Deletes resource(s), specified by {{Code|$path}}, from the database {{Code|$db}}.
 
|Deletes resource(s), specified by {{Code|$path}}, from the database {{Code|$db}}.
|-
+
|- valign="top"
 
| '''Errors'''
 
| '''Errors'''
 
|{{Error|open|#Errors}} the addressed database does not exist or could not be opened.<br/>{{Error|path|#Errors}} the specified path is invalid.
 
|{{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'''
 
| '''Examples'''
 
|
 
|
Line 530: Line 612:
  
 
{| width='100%'
 
{| width='100%'
|-
+
|- valign="top"
 
| width='120' | '''Signatures'''
 
| width='120' | '''Signatures'''
 
|{{Func|db:copy|$db as xs:string, $name as xs:string|empty-sequence()}}
 
|{{Func|db:copy|$db as xs:string, $name as xs:string|empty-sequence()}}
|-
+
|- valign="top"
 
| '''Summary'''
 
| '''Summary'''
 
|Creates a copy of the database {{Code|$db}}, which will be called {{Code|$name}}.
 
|Creates a copy of the database {{Code|$db}}, which will be called {{Code|$name}}.
|-
+
|- valign="top"
 
| '''Errors'''
 
| '''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.
 
|{{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.
Line 544: Line 626:
  
 
{| width='100%'
 
{| width='100%'
|-
+
|- valign="top"
 
| width='120' | '''Signatures'''
 
| width='120' | '''Signatures'''
 
|{{Func|db:alter|$db as xs:string, $name as xs:string|empty-sequence()}}
 
|{{Func|db:alter|$db as xs:string, $name as xs:string|empty-sequence()}}
|-
+
|- valign="top"
 
| '''Summary'''
 
| '''Summary'''
 
|Renames the database {{Code|$db}} to {{Code|$name}}.
 
|Renames the database {{Code|$db}} to {{Code|$name}}.
|-
+
|- valign="top"
 
| '''Errors'''
 
| '''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.
 
|{{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.
Line 558: Line 640:
  
 
{| width='100%'
 
{| width='100%'
|-
+
|- valign="top"
 
| width='120' | '''Signatures'''
 
| width='120' | '''Signatures'''
 
|{{Func|db:optimize|$db as xs:string|empty-sequence()}}<br/>{{Func|db:optimize|$db as xs:string, $all as xs:boolean|empty-sequence()}}<br/>{{Func|db:optimize|$db as xs:string, $all as xs:boolean, $options as map(*)?|empty-sequence()}}
 
|{{Func|db:optimize|$db as xs:string|empty-sequence()}}<br/>{{Func|db:optimize|$db as xs:string, $all as xs:boolean|empty-sequence()}}<br/>{{Func|db:optimize|$db as xs:string, $all as xs:boolean, $options as map(*)?|empty-sequence()}}
|-
+
|- valign="top"
 
| '''Summary'''
 
| '''Summary'''
|Optimizes the meta data and indexes of the database {{Code|$db}}.<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}}.
+
|Optimizes the metadata and indexes of the database {{Code|$db}}.<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'''
 
| '''Errors'''
 
|{{Error|open|#Errors}} the addressed database does not exist or could not be opened.
 
|{{Error|open|#Errors}} the addressed database does not exist or could not be opened.
|-
+
|- valign="top"
 
| '''Examples'''
 
| '''Examples'''
 
|
 
|
Line 577: Line 659:
  
 
{| width='100%'
 
{| width='100%'
|-
+
|- valign="top"
 
| width='120' | '''Signatures'''
 
| width='120' | '''Signatures'''
 
|{{Func|db:rename|$db as xs:string, $source as xs:string, $target as xs:string|empty-sequence()}}
 
|{{Func|db:rename|$db as xs:string, $source as xs:string, $target as xs:string|empty-sequence()}}
|-
+
|- valign="top"
 
| '''Summary'''
 
| '''Summary'''
 
|Moves all resources(s) of database {{Code|$db}}, 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.
 
|Moves all resources(s) of database {{Code|$db}}, 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'''
 
| '''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.
 
|{{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'''
 
| '''Examples'''
 
|
 
|
Line 593: Line 675:
 
|}
 
|}
  
==db:update==
+
==db:flush==
 
 
{{Announce|Updated with Version 10}}: renamed (before: {{Code|db:replace}}); function signature aligned with {{Function||db:add}} (second and third argument swapped).
 
  
 
{| width='100%'
 
{| width='100%'
|-
+
|- valign="top"
 
| width='120' | '''Signatures'''
 
| width='120' | '''Signatures'''
|{{Func|db:update|$db as xs:string, $input as item(), $path as xs:string|empty-sequence()}}<br/>{{Func|db:update|$db as xs:string, $input as item(), $path as xs:string, $options as map(*)?|empty-sequence()|empty-sequence()}}
+
|{{Func|db:flush|$db as xs:string|empty-sequence()}}
|-
+
|- valign="top"
 
| '''Summary'''
 
| '''Summary'''
|Replaces a resource, specified by {{Code|$path}}, in the database {{Code|$db}} with the contents of {{Code|$input}}, or adds it as a new resource:
+
|Explicitly flushes the buffers of the database {{Code|$db}}. This command is only useful if {{Option|AUTOFLUSH}} has been set to {{Code|false}}.
* The parsing behavior can be controlled via {{Code|$options}}:
+
|- valign="top"
** 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.
 
|-
 
 
| '''Errors'''
 
| '''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.
+
|{{Error|open|#Errors}} the addressed database does not exist or could not be opened.
|-
 
| '''Examples'''
 
|
 
* {{Code|db:update("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:update("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:update("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:
 
<syntaxhighlight 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:update('db', doc($path), $file)
 
</syntaxhighlight>
 
 
|}
 
|}
  
==db:put==
+
==db:drop==
 
 
{{Announce|Introduced with Version 10.}}
 
  
 
{| width='100%'
 
{| width='100%'
|-
+
|- valign="top"
 
| width='120' | '''Signatures'''
 
| width='120' | '''Signatures'''
|{{Func|db:put|$db as xs:string, $input as item()*, $path as xs:string|empty-sequence()}}
+
|{{Func|db:drop|$db as xs:string|empty-sequence()}}
|-
+
|- valign="top"
 
| '''Summary'''
 
| '''Summary'''
|Replaces an XQuery value specified by {{Code|$input}} in the database {{Code|$db}} and the location specified by {{Code|$path}}, or adds it as new resource.
+
|Drops the database {{Code|$db}} and all connected resources.
|-
+
|- valign="top"
 
| '''Errors'''
 
| '''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).
+
|{{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'''
 
| '''Examples'''
 
|
 
|
* {{Code|db:put('DB', 1 to 10000, 'sequence')}} stores a numeric range in the database.
+
* {{Code|db:drop("DB")}} drops the database {{Code|DB}}.
* With the following query, you can copy the XQuery values of one database into another:
 
<syntaxhighlight 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) = 'value'
 
let $trg := $trg-path || substring-after($src, $src-path)
 
return db:put($db, db:get($db, $src), $trg)
 
</syntaxhighlight>
 
|}
 
 
 
==db:put-binary==
 
 
 
{{Announce|Updated with Version 10}}: renamed (before: {{Code|db:put}}); function signature aligned with {{Function||db:add}} (second and third argument swapped).
 
 
 
{| width='100%'
 
|-
 
| width='120' | '''Signatures'''
 
|{{Func|db:put-binary|$db as xs:string, $input as item(), $path as xs:string|empty-sequence()}}
 
|-
 
| '''Summary'''
 
|Replaces a binary resource specified by {{Code|$input}} in the database {{Code|$db}} and the location specified by {{Code|$path}}, or adds it as new resource.
 
|-
 
| '''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).
 
|-
 
| '''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 binaries of one database into another:
 
<syntaxhighlight 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)
 
</syntaxhighlight>
 
|}
 
 
 
==db:flush==
 
 
 
{| width='100%'
 
|-
 
| width='120' | '''Signatures'''
 
|{{Func|db:flush|$db as xs:string|empty-sequence()}}
 
|-
 
| '''Summary'''
 
|Explicitly flushes the buffers of the database {{Code|$db}}. This command is only useful if {{Option|AUTOFLUSH}} has been set to {{Code|false}}.
 
|-
 
| '''Errors'''
 
|{{Error|open|#Errors}} the addressed database does not exist or could not be opened.
 
 
|}
 
|}
  
 
=Backups=
 
=Backups=
  
{{Announce|Introduced with Version 10:}} Support for general data ([[User Management|registered users]], [[Jobs Module#Services|scheduled services]] and [[Store Module|key-value stores]]).
+
{{Announce|Introduced with Version 10:}} Support for general data ([[User Management|registered users]], [[Job Module#Services|scheduled services]] and [[Store Module|key-value stores]]).
  
 
All functions in this section except for {{Function||db:backups}} are {{Function|Database|Updating Functions}}.
 
All functions in this section except for {{Function||db:backups}} are {{Function|Database|Updating Functions}}.
Line 712: Line 718:
  
 
{| width='100%'
 
{| width='100%'
|-
+
|- valign="top"
 
| width='120' | '''Signatures'''
 
| width='120' | '''Signatures'''
 
|{{Func|db:create-backup|$db as xs:string|empty-sequence()}}<br/>{{Func|db:create-backup|$db as xs:string, $options as map(*)|empty-sequence()}}
 
|{{Func|db:create-backup|$db as xs:string|empty-sequence()}}<br/>{{Func|db:create-backup|$db as xs:string, $options as map(*)|empty-sequence()}}
|-
+
|- valign="top"
 
| '''Summary'''
 
| '''Summary'''
 
|Creates a backup of the database {{Code|$db}}. If no name is supplied, general data will be backed up. The following {{Code|$options}} are available:
 
|Creates a backup of the database {{Code|$db}}. 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.
 
* 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.
 
* By setting {{Code|compress}} to false, the backup will be created faster, but it will take more space on disk.
|-
+
|- valign="top"
 
| '''Errors'''
 
| '''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.
 
|{{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'''
 
| '''Examples'''
 
|
 
|
Line 732: Line 738:
  
 
{| width='100%'
 
{| width='100%'
|-
+
|- valign="top"
 
| width='120' | '''Signatures'''
 
| width='120' | '''Signatures'''
 
|{{Func|db:drop-backup|$name as xs:string|empty-sequence()}}
 
|{{Func|db:drop-backup|$name as xs:string|empty-sequence()}}
|-
+
|- valign="top"
 
| '''Summary'''
 
| '''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.
 
|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'''
 
| '''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.
 
|{{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'''
 
| '''Examples'''
 
|
 
|
Line 751: Line 757:
  
 
{| width='100%'
 
{| width='100%'
|-
+
|- valign="top"
 
| width='120' | '''Signatures'''
 
| width='120' | '''Signatures'''
 
|{{Func|db:alter-backup|$name as xs:string, $new-name as xs:string|empty-sequence()}}
 
|{{Func|db:alter-backup|$name as xs:string, $new-name as xs:string|empty-sequence()}}
|-
+
|- valign="top"
 
| '''Summary'''
 
| '''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.
 
|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'''
 
| '''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.
 
|{{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'''
 
| '''Examples'''
 
|
 
|
Line 769: Line 775:
  
 
{| width='100%'
 
{| width='100%'
|-
+
|- valign="top"
 
| width='120' | '''Signatures'''
 
| width='120' | '''Signatures'''
 
|{{Func|db:restore|$name as xs:string|empty-sequence()}}
 
|{{Func|db:restore|$name as xs:string|empty-sequence()}}
|-
+
|- valign="top"
 
| '''Summary'''
 
| '''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.
 
|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'''
 
| '''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.
 
|{{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'''
 
| '''Examples'''
 
|
 
|
Line 788: Line 794:
  
 
{| width='100%'
 
{| width='100%'
|-
+
|- valign="top"
 
| width='120' | '''Signatures'''
 
| width='120' | '''Signatures'''
 
|{{Func|db:backups||element(backup)*}}<br/>{{Func|db:backups|$db as xs:string|element(backup)*}}
 
|{{Func|db:backups||element(backup)*}}<br/>{{Func|db:backups|$db as xs:string|element(backup)*}}
|-
+
|- valign="top"
 
| '''Summary'''
 
| '''Summary'''
 
|Returns an element sequence containing all available database backups with timestamp, file size and comment.<br/>If a database {{Code|$db}} is specified, the sequence will be restricted to the backups matching this database.
 
|Returns an element sequence containing all available database backups with timestamp, file size and comment.<br/>If a database {{Code|$db}} is specified, the sequence will be restricted to the backups matching this database.
|-
+
|- valign="top"
 
| '''Examples'''
 
| '''Examples'''
 
|
 
|
Line 805: Line 811:
  
 
{| width='100%'
 
{| width='100%'
|-
+
|- valign="top"
 
| width='120' | '''Signatures'''
 
| width='120' | '''Signatures'''
 
|{{Func|db:name|$node as node()|xs:string}}
 
|{{Func|db:name|$node as node()|xs:string}}
|-
+
|- valign="top"
 
| '''Summary'''
 
| '''Summary'''
 
|Returns the name of the database in which the specified [[#Database Nodes|database node]] {{Code|$node}} is stored.
 
|Returns the name of the database in which the specified [[#Database Nodes|database node]] {{Code|$node}} is stored.
|-
+
|- valign="top"
 
| '''Errors'''
 
| '''Errors'''
 
|{{Error|node|#Errors}} {{Code|$nodes}} contains a node which is not stored in a database.
 
|{{Error|node|#Errors}} {{Code|$nodes}} contains a node which is not stored in a database.
Line 819: Line 825:
  
 
{| width='100%'
 
{| width='100%'
|-
+
|- valign="top"
 
| width='120' | '''Signatures'''
 
| width='120' | '''Signatures'''
 
|{{Func|db:path|$node as node()|xs:string}}
 
|{{Func|db:path|$node as node()|xs:string}}
|-
+
|- valign="top"
 
| '''Summary'''
 
| '''Summary'''
 
|Returns the path of the database document in which the specified [[#Database Nodes|database node]] {{Code|$node}} is stored.
 
|Returns the path of the database document in which the specified [[#Database Nodes|database node]] {{Code|$node}} is stored.
|-
+
|- valign="top"
 
| '''Errors'''
 
| '''Errors'''
 
|{{Error|node|#Errors}} {{Code|$nodes}} contains a node which is not stored in a database.
 
|{{Error|node|#Errors}} {{Code|$nodes}} contains a node which is not stored in a database.
Line 833: Line 839:
  
 
{| width='100%'
 
{| width='100%'
|-
+
|- valign="top"
 
| width='120' | '''Signatures'''
 
| width='120' | '''Signatures'''
 
|{{Func|db:exists|$db as xs:string|xs:boolean}}<br/>{{Func|db:exists|$db as xs:string, $path as xs:string|xs:boolean}}
 
|{{Func|db:exists|$db as xs:string|xs:boolean}}<br/>{{Func|db:exists|$db as xs:string, $path as xs:string|xs:boolean}}
|-
+
|- valign="top"
 
| '''Summary'''
 
| '''Summary'''
 
|Checks if the database {{Code|$db}} or the resource specified by {{Code|$path}} exists. {{Code|false}} is returned if a database directory has been addressed.
 
|Checks if the database {{Code|$db}} or the resource specified by {{Code|$path}} exists. {{Code|false}} is returned if a database directory has been addressed.
|-
+
|- valign="top"
 
| '''Examples'''
 
| '''Examples'''
 
|
 
|
Line 851: Line 857:
  
 
{| width='100%'
 
{| width='100%'
|-
+
|- valign="top"
 
| width='120' | '''Signatures'''
 
| width='120' | '''Signatures'''
 
|{{Func|db:type|$db as xs:string, $path as xs:string|xs:boolean}}
 
|{{Func|db:type|$db as xs:string, $path as xs:string|xs:boolean}}
|-
+
|- valign="top"
 
| '''Summary'''
 
| '''Summary'''
 
|Returns the type of a resource – {{Code|xml}}, {{Code|binary}}, or {{Code|value}} – in the database {{Code|$db}} at the specified {{Code|$path}}.
 
|Returns the type of a resource – {{Code|xml}}, {{Code|binary}}, or {{Code|value}} – in the database {{Code|$db}} at the specified {{Code|$path}}.
|-
+
|- valign="top"
 
| '''Errors'''
 
| '''Errors'''
 
|{{Error|open|#Errors}} the addressed database does not exist or could not be opened.
 
|{{Error|open|#Errors}} the addressed database does not exist or could not be opened.
|-
+
|- valign="top"
 
| '''Examples'''
 
| '''Examples'''
 
|
 
|
Line 869: Line 875:
  
 
{| width='100%'
 
{| width='100%'
|-
+
|- valign="top"
 
| width='120' | '''Signatures'''
 
| width='120' | '''Signatures'''
 
|{{Func|db:content-type|$db as xs:string, $path as xs:string|xs:string}}
 
|{{Func|db:content-type|$db as xs:string, $path as xs:string|xs:string}}
|-
+
|- valign="top"
 
| '''Summary'''
 
| '''Summary'''
 
|Retrieves the content type of a resource in the database {{Code|$db}} 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.
 
|Retrieves the content type of a resource in the database {{Code|$db}} 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.
|-
+
|- valign="top"
 
| '''Errors'''
 
| '''Errors'''
 
|{{Error|open|#Errors}} the addressed database does not exist or could not be opened.
 
|{{Error|open|#Errors}} the addressed database does not exist or could not be opened.
|-
+
|- valign="top"
 
| '''Examples'''
 
| '''Examples'''
 
|
 
|
Line 891: Line 897:
 
! width="110"|Code
 
! width="110"|Code
 
|Description
 
|Description
|-
+
|- valign="top"
 
|{{Code|args}}
 
|{{Code|args}}
 
|The number of specified inputs and paths differs.
 
|The number of specified inputs and paths differs.
|-
+
|- valign="top"
 
|{{Code|conflict}}
 
|{{Code|conflict}}
 
|Multiple update operations point to the same target.
 
|Multiple update operations point to the same target.
|-
+
|- valign="top"
 
|{{Code|lock}}
 
|{{Code|lock}}
 
|A database cannot be updated because it is opened by another process.
 
|A database cannot be updated because it is opened by another process.
|-
+
|- valign="top"
 
|{{Code|mainmem}}
 
|{{Code|mainmem}}
 
|The addressed database is not ''persistent'' (stored on disk).
 
|The addressed database is not ''persistent'' (stored on disk).
|-
+
|- valign="top"
 
|{{Code|name}}
 
|{{Code|name}}
 
|The name of the specified database is invalid.
 
|The name of the specified database is invalid.
|-
+
|- valign="top"
 
|{{Code|no-backup}}
 
|{{Code|no-backup}}
 
|No backup exists for a database.
 
|No backup exists for a database.
|-
+
|- valign="top"
 
|{{Code|node}}
 
|{{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.
 
|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}}
 
|{{Code|no-index}}
 
|The database lacks an index structure required by the called function.
 
|The database lacks an index structure required by the called function.
|-
+
|- valign="top"
 
|{{Code|open}}
 
|{{Code|open}}
 
|The addressed database does not exist or could not be opened.
 
|The addressed database does not exist or could not be opened.
|-
+
|- valign="top"
 
|{{Code|option}}
 
|{{Code|option}}
 
|The specified option is unknown.
 
|The specified option is unknown.
|-
+
|- valign="top"
 
|{{Code|path}}
 
|{{Code|path}}
 
|The specified database path is invalid.
 
|The specified database path is invalid.
|-
+
|- valign="top"
 
|{{Code|property}}
 
|{{Code|property}}
 
|The specified database property is unknown.
 
|The specified database property is unknown.
|-
+
|- valign="top"
 
|{{Code|range}}
 
|{{Code|range}}
 
|The addressed database id or pre value is out of range.
 
|The addressed database id or pre value is out of range.
|-
+
|- valign="top"
 
|{{Code|target}}
 
|{{Code|target}}
 
|Path points to an invalid target.
 
|Path points to an invalid target.
Line 939: Line 945:
 
;Version 10
 
;Version 10
 
* Added: {{Function||db:get}}, {{Function||db:put}}, {{Function||db:type}}.
 
* Added: {{Function||db:get}}, {{Function||db:put}}, {{Function||db:type}}.
* Added: [[#Backups|Backups]]: Support for general data ([[User Management|registered users]], [[Jobs Module#Services|scheduled services]] and [[Store Module|key-value stores]]).
+
* 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:replace}} and {{Function||db:put-binary}} renamed (before: {{Code|db:replace}} and {{Code|db:store}}); function signature aligned with {{Function||db:add}} (second and third argument swapped).
+
* 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:get-binary}} renamed (before: {{Code|db:retrieve}}).
 
* Updated: {{Function||db:backups}}, {{Function||db:create-backup}}: Options added.
 
* Updated: {{Function||db:backups}}, {{Function||db:create-backup}}: Options added.
* Deletes: {{Code|db:is-raw}}, {{Code|db:is-raw}} (new: {{Function||db:type}}).
+
* Removed: {{Code|db:is-raw}}, {{Code|db:is-raw}} (new: {{Function||db:type}}).
  
 
;Version 9.3
 
;Version 9.3
 
* Added: {{Function||db:alter-backup}}
 
* Added: {{Function||db:alter-backup}}
* Updated: {{Function||db:open-id}}, {{Function||db:open-pre}}: support for multiple integers
+
* Updated: {{Code|db:open-id}}, {{Code|db:open-pre}}: support for multiple integers
  
 
;Version 9.2
 
;Version 9.2
Line 982: Line 990:
  
 
;Version 7.8
 
;Version 7.8
* Removed: db:fulltext (use [[Full-Text Module#ft:search|ft:search]] instead)
+
* Removed: db:fulltext (use {{Function|Full-Text|ft:search}} instead)
  
 
;Version 7.7
 
;Version 7.7
 
* Added: {{Function||db:export}}, {{Function||db:name}}, {{Function||db:path}}
 
* Added: {{Function||db:export}}, {{Function||db:name}}, {{Function||db:path}}
 
* Updated: {{Code|$options}} argument added to {{Function||db:create}} and {{Function||db:optimize}}.
 
* Updated: {{Code|$options}} argument added to {{Function||db:create}} and {{Function||db:optimize}}.
* Updated: the functions no longer accept {{Function|Database|Database Nodes}} as reference. Instead, the name of a database must now be specified.
+
* 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
 
;Version 7.6
Line 1,010: Line 1,018:
 
;Version 7.0
 
;Version 7.0
 
* Added: {{Function||db:exists}}, {{Code|db:retrieve}}, {{Code|db:store}}, {{Code|db:is-raw}}, {{Code|db:is-xml}}
 
* Added: {{Function||db:exists}}, {{Code|db:retrieve}}, {{Code|db:store}}, {{Code|db:is-raw}}, {{Code|db:is-xml}}
* Updated: {{Function||db:list}}, {{Function||db:open}}, {{Function||db:add}}
+
* Updated: {{Function||db:list}}, {{Code|db:open}}, {{Function||db:add}}

Revision as of 10:24, 28 July 2022

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

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

In BaseX, two internal representations exist for nodes.

  • XML fragments are generated by XQuery node constructors.db:b
  • 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
    • result of a main-memory update operation.

Some operations are restricted to database nodes, but you can convert XML fragments to database nodes by applying an empty update or transform operation to a node. Two examples:

  • Retrieve the internal node id of an XML fragment:

<syntaxhighlight lang="xquery"> let $xml := <xml>hello world</xml> update {} return db:node-id($xml/text()) </syntaxhighlight>

  • Puts a marker element around the result of a full-text request (see ft:mark for more details):

<syntaxhighlight lang="xquery"> copy $p := <xml>hello world</xml> modify () return ft:mark($p[text() contains text 'word'], 'b') </syntaxhighlight>

Updating Functions

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

db:system

Signatures 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

Signatures 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

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

db:property

Signatures db:property($db 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 database $db. 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

Signatures db:list() as xs:string*
db:list($db as xs:string) as xs:string*
db:list($db as xs:string, $path as xs:string) as xs:string*
Summary The result of this function is dependent on the number of arguments:
  • Without arguments, the names of all databases are returned that are accessible to the current user.
  • If a database $db is specified, all documents and raw files of the specified database are returned.
  • The list of returned resources can be restricted by the $path argument.
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

Signatures db:list-details() as element(database)*
db:list-details($db as xs:string) as element(resource)*
db:list-details($db as xs:string, $path as xs:string) as element(resource)*
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 a database $db is specified, an element for each documents and raw file of the specified 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).
  • Returned databases resources can be further restricted by the $path argument.
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

Signatures db:dir($db as xs:string, $path as xs:string) as element()*
Summary Returns metadata on all directories and resources of the database $db in the specified directory $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 as attribute.

Please note that directories are not stored in BaseX. 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

db:get

Updated with Version 10: Renamed (before: db:open). Due to its widespread use, the old function name will be supported for some more time.

Signatures db:get($db as xs:string) as document-node()*
db:get($db as xs:string, $path as xs:string) as document-node()*
Summary Returns all documents from the database $db, 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

Updated with Version 10: Renamed (before: db:open-pre).

Signatures db:get-pre($db as xs:string, $pres as xs:integer*) as node()*
Summary Returns all nodes from the database $db with the pre values $pres 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

Updated with Version 10: Renamed (before: open-id).

Signatures db:get-id($db as xs:string, $ids as xs:integer*) as node()*
Summary Returns all nodes from the database $db with the pre values $ids 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

Updated with Version 10: renamed (before: db:retrieve).

Signatures db:get-binary($db as xs:string, $path as xs:string) as item()
Summary Returns a map with all paths and binary resources of the database $db. 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

Introduced with Version 10.

Signatures db:get-value($db as xs:string, $path as xs:string) as item()*
Summary Returns a map with all paths and values of the database $db. 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

Signatures 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

Signatures 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

Signatures db:export($db as xs:string, $path as xs:string) as empty-sequence()
db:export($db as xs:string, $path as xs:string, $params as item()) as empty-sequence()
Summary Exports the specified database $db to the specified file $path. Existing files will be overwritten.
The $params argument contains serialization parameters. As with fn:serialize(), the parameters can be specified
  • either as children of an <output:serialization-parameters/> element:

<syntaxhighlight lang="xml"> <output:serialization-parameters>

 <output:method value='xml'/>
 <output:cdata-section-elements value="div"/>
 ...

</output:serialization-parameters> </syntaxhighlight>

  • or as map, which contains all key/value pairs:

<syntaxhighlight lang="xquery"> map { "method": "xml", "cdata-section-elements": "div", ... } </syntaxhighlight>

Errors open: the addressed database does not exist or could not be opened.
Examples Export all files as text:

<syntaxhighlight lang="xquery"> db:export("DB", "/home/john/xml/texts", map { 'method': 'text' }) </syntaxhighlight> The following code can be used to export parts of the database: <syntaxhighlight 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)

) </syntaxhighlight>

Value Indexes

db:text

Signatures db:text($db as xs:string, $strings as xs:string*) as text()*
Summary Returns all text nodes of the database $db that have one of the specified $strings as 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

Signatures db:text-range($db as xs:string, $min as xs:string, $max as xs:string) as text()*
Summary Returns all text nodes of the database $db whose values are between $min and $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

Signatures db:attribute($db as xs:string, $strings as xs:string*) as attribute()*
db:attribute($db as xs:string, $strings as xs:string*, $name as xs:string) as attribute()*
Summary Returns all attribute nodes of the database $db that have one of the specified $strings as 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

Signatures db:attribute-range($db as xs:string, $min as xs:string, $max as xs:string) as attribute()*
db:attribute-range($db as xs:string, $min as xs:string, $max as xs:string, $name as xs:string) as attribute()*
Summary Returns all attributes of the database $db, the string values of which 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

Signatures db:token($db as xs:string, $tokens as xs:string*) as attribute()*
db:token($db as xs:string, $tokens as xs:string*, $name as xs:string) as attribute()*
Summary Returns all attribute nodes of the database $db 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

All functions in this section are Updating Functions.

db:create

Signatures db:create($db as xs:string) as empty-sequence()
db:create($db as xs:string, $inputs as item()*) as empty-sequence()
db:create($db as xs:string, $inputs as item()*, $paths as xs:string*) as empty-sequence()
db:create($db as xs:string, $inputs as item()*, $paths as xs:string*, $options as map(*)?) as empty-sequence()
Summary Creates a new database with name $db and adds initial documents specified via $inputs to the specified $paths:
  • $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

Signatures db:add($db as xs:string, $input as item()) as empty-sequence()
db:add($db as xs:string, $input as item(), $path as xs:string?) as empty-sequence()
db:add($db as xs:string, $input as item(), $path as xs:string?, $options as map(*)?) as empty-sequence()
Summary Adds documents specified by $input to the database $db 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. In order to reduce memory consumption, the files will be cached before being added to the database.

db:put

Updated with Version 10: renamed (before: db:replace); function signature aligned with db:add (second and third argument swapped).

Signatures db:put($db as xs:string, $input as item(), $path as xs:string) as empty-sequence()
db:put($db as xs:string, $input as item(), $path as xs:string, $options as map(*)?) as empty-sequence()
Summary Replaces a resource, specified by $path, in the database $db 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: <syntaxhighlight 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) </syntaxhighlight>

db:put-binary

Updated with Version 10: renamed (before: db:put); function signature aligned with db:add (second and third argument swapped).

Signatures db:put-binary($db as xs:string, $input as item(), $path as xs:string) as empty-sequence()
Summary Stores a binary resource specified by $input in the database $db 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:

<syntaxhighlight 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) </syntaxhighlight>

db:put-value

Introduced with Version 10.

Signatures db:put-value($db as xs:string, $input as item()*, $path as xs:string) as empty-sequence()
Summary Stores a value specified by $input in the database $db 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:

<syntaxhighlight 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'

)</syntaxhighlight>

db:delete

Signatures db:delete($db as xs:string, $path as xs:string) as empty-sequence()
Summary Deletes resource(s), specified by $path, from the database $db.
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

Signatures db:copy($db as xs:string, $name as xs:string) as empty-sequence()
Summary Creates a copy of the database $db, which will be called $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

Signatures db:alter($db as xs:string, $name as xs:string) as empty-sequence()
Summary Renames the database $db to $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

Signatures db:optimize($db as xs:string) as empty-sequence()
db:optimize($db as xs:string, $all as xs:boolean) as empty-sequence()
db:optimize($db as xs:string, $all as xs:boolean, $options as map(*)?) as empty-sequence()
Summary Optimizes the metadata and indexes of the database $db.
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

Signatures db:rename($db as xs:string, $source as xs:string, $target as xs:string) as empty-sequence()
Summary Moves all resources(s) of database $db, 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 {Code|docs/newdir}}.

db:flush

Signatures db:flush($db as xs:string) as empty-sequence()
Summary Explicitly flushes the buffers of the database $db. 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

Signatures db:drop($db as xs:string) as empty-sequence()
Summary Drops the database $db 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

Introduced with Version 10: Support for general data (registered users, scheduled services and key-value stores).

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

db:create-backup

Updated with Version 10: Options argument added.

Signatures db:create-backup($db as xs:string) as empty-sequence()
db:create-backup($db as xs:string, $options as map(*)) as empty-sequence()
Summary Creates a backup of the database $db. 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

Signatures 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

Signatures 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

Signatures 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

Signatures db:backups() as element(backup)*
db:backups($db 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 $db 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

db:name

Signatures 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

Signatures 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

Signatures db:exists($db as xs:string) as xs:boolean
db:exists($db as xs:string, $path as xs:string) as xs:boolean
Summary Checks if the database $db or the resource specified by $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:type

Introduced with BaseX 10: Replaces db:is-raw and db:is-xml.

Signatures db:type($db as xs:string, $path as xs:string) as xs:boolean
Summary Returns the type of a resource – xml, binary, or value – in the database $db 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

Signatures db:content-type($db as xs:string, $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 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

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

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