Changes

Jump to navigation Jump to search
93 bytes removed ,  23:07, 1 December 2023
==Database Nodes==
In BaseX, two internal representations exist for XML nodes.:
* XML fragments are generated by XQuery node constructors1.db:b* Database nodes are:** stored in a persistent database on disk;** nodes of a document that has been generated temporarily with {{Code|fn:doc}}, {{Code|fn:parse-xml}} and other functions; or** the result of a [[XQuery Update#Main-Memory Updates|main-memory update operation]].2. Fragments are similar to DOM structures. They are created when XQuery node constructors are used (<code><a/></code>, <code>element a { }</code>, etc.).
Some operations Fragments require less memory for small XML structures, but database nodes are restricted to database more efficient for larger amounts of data. Fragments nodes, but you can convert XML fragments be converted to database nodes by applying an empty a [[XQuery_Update#updateMain-Memory Updates|main-memory update]] or [[XQuery_Update#transform|transform]] operation with an empty body to a node. Two examples:
* Retrieve the internal node id of an XML fragment: <syntaxhighlight pre 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 {{Function|Full-Text|ft:mark}} for more details): <syntaxhighlight lang="xquery">copy $p := <xml>hello world</xml>modify ()return ft:mark($p[text() contains text 'word'], 'b')</syntaxhighlightpre>
==Updating Functions==
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|<pre>db:system||() as element(system)}}</pre>|-valign="top"
| '''Summary'''
|Returns general information on the database system the current values of all global and local [[Options]]. The {{Command|INFO}} command returns similar output.
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|<pre>db:option|( $name as xs:string|) as xs:string}}</pre>|-valign="top"
| '''Summary'''
|Returns the current value (string, integer, boolean, map) of a global or local [[Options|Option]] with the specified {{Code|$name}}. The {{Command|GETSHOW OPTIONS}} command works returns similaroutput.|-valign="top"
| '''Errors'''
|{{Error|option|#Errors}} the specified option is unknown.
|-valign="top"
| '''Examples'''
|
* <code>db:option('dbpath')</code> returns the database path string.
* <code>db:option('serializer')</code> returns a map with the current serialization parameters.
* <code>declare option db:chop stripws 'true'; db:option('chopstripws')</code> returns {{Code|true}} (irrespective of the global locally assigned value).
|}
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|<pre>db:info|( $db database as xs:string|) as element(database)}}</pre>|-valign="top"
| '''Summary'''
|Returns meta information on the database specified {{Code|$dbdatabase}}. The output is similar to the {{Command|INFO DB}} command.|-valign="top"
| '''Errors'''
|{{Error|open|#Errors}} the addressed database does not exist or could not be opened.
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|<pre>db:property|( $db database as xs:string, $name as xs:string|) as xs:anyAtomicType}}</pre>|-valign="top"
| '''Summary'''
|Returns the value (string, boolean, integer) of a property with the specified {{Code|$name}} in the database specified {{Code|$dbdatabase}}. The available properties are the ones returned by {{Function||db:info}}.|-valign="top"
| '''Errors'''
|{{Error|property|#Errors}} the specified property is unknown.
|-valign="top"
| '''Examples'''
|
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|db:list||xs:string*}}<br/pre>{{Func|db:list|( $db database as xs:string|xs :string*}}<br/>{{Func|db:list|$db as xs:string= (), $path as xs:string| := ()) as xs:string*}}</pre>|-valign="top"
| '''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 {{Code|$dbdatabase}} is specified, paths to all documents and raw files resources of the specified this database are returned.* The list of returned resources results can be restricted by to resources starting with the specified {{Code|$path}} argument.|-valign="top"
| '''Errors'''
|{{Error|open|#Errors}} the addressed database does not exist or could not be opened.
|-valign="top"
| '''Examples'''
|
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|db:list-details||element(database)*}}<br/pre>{{Func|db:list-details|( $db database as xs:string|element := (resource)*}}<br/>{{Func|db:list-details|$db as xs:string, $path as xs:string| := ()) as element(resource)*}}</pre>|-valign="top"
| '''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 {{Code|$dbdatabase}} is specified, an element for each documents and raw file of the specified resource in this database is returned:
* An element has a value, which is the name of the resource, and several attributes, which contain the content type, the modification date, the raw flag (which indicates if the resource is binary or XML), and the size of a resource.
* The value of the size attribute depends on the resource type: for documents, it represents the number of nodes; for binary data, it represents the file size (measured in bytes).
* Returned databases resources The results can be further restricted by to resources starting with the specified {{Code|$path}} argument.|-valign="top"
| '''Errors'''
|{{Error|open|#Errors}} the addressed database does not exist or could not be opened.
|-valign="top"
| '''Examples'''
|
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|<pre>db:dir|( $db database as xs:string, $path as xs:string|) as element()*}}</pre>|-valign="top"
| '''Summary'''
|Returns meta data metadata on all directories and resources of the database a {{Code|$dbdatabase}} 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|dir}} represents a directory. The element value is the directory path; the modification date is returned as in an attribute.Please note that The directories are not stored in BaseXthe internal database layout. Instead, they result implicitly from the paths of stored resources.|-valign="top"
| '''Errors'''
|{{Error|open|#Errors}} the addressed database does not exist or could not be opened.<br/>{{Error|path|#Errors}} the specified path is invalid.
|-valign="top"
| '''Examples'''
|
=Read Operations=
==db:openget==
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|db:open|$db as xs:string|document-node()*}}<br /pre>{{Func|db:open|get( $db database as xs:string, $path as xs:string| := ()) as document-node()*}}</pre>|-valign="top"
| '''Summary'''
|Opens Returns all documents from the database specified {{Code|$dbdatabase}} and returns all document nodes.<br/>The document nodes to be returned can be filtered with , or only documents matching the specified {{Code|$path}} argument.|-valign="top"
| '''Errors'''
|{{Error|open|#Errors}} the addressed database does not exist or could not be opened.
|-valign="top"
| '''Examples'''
|
* {{Code|db:openget("'docs"')}} returns all documents from the database named {{Code|docs}}.* {{Code|db:openget("'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:openget("'db" ' || $i)//item</code> returns all item elements from the databases {{Code|db1}}, {{Code|db2}} and {{Code|db3}}.
|}
==db:openget-pre==
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|<pre>db:openget-pre|( $db database as xs:string, $pres values as xs:integer*|) as node()*}}</pre>|-valign="top"
| '''Summary'''
|Opens the database Returns all nodes from a {{Code|$dbdatabase}} and returns all distinct nodes with the pre values specified PRE {{Code|$presvalues}} 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'''
|{{Error|open|#Errors}} the addressed database does not exist or could not be opened.<br/>{{Error|range|#Errors}} the specified pre PRE value does not exist in the database.|-valign="top"
| '''Examples'''
|
* {{Code|db:openget-pre("docs", 0)}} returns the first database node from the database named {{Code|docs}}.
|}
==db:openget-id==
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|<pre>db:openget-id|( $db database as xs:string, $ids values as xs:integer*|) as node()*}}</pre>|-valign="top"
| '''Summary'''
|Opens the database Returns all nodes from a {{Code|$dbdatabase}} and returns all distinct nodes with the specified ID {{Code|$idsvalues}} 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 ID can be sped up by turning on the {{Option|UPDINDEX}} option.|-valign="top"
| '''Errors'''
|{{Error|open|#Errors}} the addressed database does not exist or could not be opened.<br/>{{Error|range|#Errors}} the specified id ID value does not exist in the database.
|}
==db:nodeget-prebinary==
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|<pre>db:nodeget-pre|binary( $database as xs:string, $nodes path as xs:string) as nodeitem()*|xs:integer*}}</pre>|-valign="top"
| '''Summary'''
|Returns a map with the ''pre'' values paths and binary items of all resources in the specified {{Code|$nodesdatabase}}, which must all be . A single {{FunctionCode|Databasexs:base64Binary}} item is returned if a {{Code|Database Nodes$path}}is specified.<br/>The All items are [[Node Storage#PRE ValueLazy Module|PRE valuelazy]] provides very fast access to an existing database node, but i.e., the actual data will only be retrieved if it will change whenever a node with a smaller ''pre'' values is added to or deleted from a databaseprocessed.|-valign="top"
| '''Errors'''
|{{Error|nodeopen|#Errors}} the addressed database does not exist or could not be opened.<br/>{{CodeError|$nodesmainmem|#Errors}} contains a node which the database is not ''persistent'' (stored in a databaseon disk).|-valign="top"
| '''Examples'''
|
* {{Code|db:nodeget-prebinary(doc("input")'DB', 'music/01.mp3')}} returns {{Code|0}} if 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 {{Code|input}} contains resource as UTF-8 text and returns a single documentstring.
|}
==db:nodeget-idvalue==
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|<pre>db:nodeget-id|value( $database as xs:string, $nodes path as xs:string) as nodeitem()*|xs:integer*}}</pre>|-valign="top"
| '''Summary'''
|Returns a map with the ''id'' paths and values of all resources in the specified {{Code|$nodes}}, which must all be {{Function|Database|Database Nodesdatabase}}.<br/>Each database node has A single value is returned if a ''persistent'' [[Node Storage#ID Value|ID value]]. Access to the node id can be sped up by turning on the {{OptionCode|UPDINDEX$path}} optionis specified.|-valign="top"
| '''Errors'''
|{{Error|nodeopen|#Errors}} the addressed database does not exist or could not be opened.<br/>{{CodeError|mainmem|$nodes#Errors}} contains a node which the database is not ''persistent'' (stored in a databaseon disk).|- valign="top"| '''Examples'''|* {{Code|db:get-value('DB', 'sequence')}} returns the specified sequence.
|}
==db:getnode-pre== {{Announce|Introduced with Version 10.}}
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|<pre>db:get|node-pre( $db nodes as xs:string, $path node()*) as xs:string|item()integer*}}</pre>|-valign="top"
| '''Summary'''
|Returns an XQuery value stored in the database {{Code|$db}} at PRE values of the specified {{Code|$pathnodes}}, 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'''
|{{Error|opennode|#Errors}} the addressed database does not exist or could not be opened.<br/>{{ErrorCode|mainmem|#Errors$nodes}} the database contains a node which is not ''persistent'' (stored on disk)in a database.|-valign="top"
| '''Examples'''
|
* {{Code|db:getnode-pre('DB', 'sequence'doc("input"))}} returns {{Code|0}} if the specified sequencedatabase {{Code|input}} contains a single document.
|}
==db:getnode-binaryid== {{Announce|Updated with Version 10}}: renamed (before: {{Code|db:retrieve}}).
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|<pre>db:getnode-binary|id( $db nodes as xs:string, $path node()*) as xs:string|xs:base64Binary}}integer*</pre>|-valign="top"
| '''Summary'''
|Returns a [[Binary Data|binary resource]] addressed by the database ID values of the specified {{Code|$dbnodes}} and {{Code, which must all be [[#Database Nodes|$path}} as database nodes]].<br/>Each database node has a ''persistent'' [[Streaming ModuleNode Storage#ID Value|streamableID value]] . Access to the node id can be sped up by turning on the {{CodeOption|xs:base64BinaryUPDINDEX}}option.|-valign="top"
| '''Errors'''
|{{Error|opennode|#Errors}} the addressed database does not exist or could not be opened.<br/>{{Error|mainmemCode|#Errors$nodes}} the database contains a node which is not ''persistent'' (stored on disk).|-| '''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.
|}
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|db:export|$db as xs:string, $path as xs:string|empty-sequence()}}<br /pre>{{Func|db:export|( $db database as xs:string, $path as xs:string, $params options as itemmap(*)? := map { })|as empty-sequence()}}<br /pre>|-valign="top"
| '''Summary'''
|Exports the specified database {{Code|$dbdatabase}} to the specified file {{Code|$path}}. Existing files will be overwritten.<br />The {{Code|$paramsoptions}} argument contains [[Serialization|serialization parameters]]. As with (see [https://www.w3.org/TR/xpath-functions-31/#func-serialize fn:serialize(])], the parameters can be specified<br />.* either as children of an {{Code|&lt;output:serialization-parameters/&gt;}} element:<syntaxhighlight lang="xml"><output:serialization-parameters> <output:method value='xml'/> <output:cdata-section-elements valuevalign="divtop"/> ...</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'''
|{{Error|open|#Errors}} the addressed database does not exist or could not be opened.
|-valign="top"
| '''Examples'''
| Export all files as text:<br/>
<syntaxhighlight pre lang="'xquery"'>
db:export("DB", "/home/john/xml/texts", map { 'method': 'text' })
</syntaxhighlightpre>The following query code can be used to export parts of the database:<syntaxhighlight pre lang="'xquery"'>
let $target := '/home/john/xml/target'
for $doc in db:openget('DB', 'collection')
let $path := $target || db:path($doc)
return (
file:write($path, $doc)
)
</syntaxhighlightpre>
|}
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|<pre>db:text|( $db database as xs:string, $strings values as xs:string*|) as text()*}}</pre>|-valign="top"
| '''Summary'''
|Returns all text nodes of the database a {{Code|$dbdatabase}} that have match one of the specified {{Code|$stringsvalues}} as values and that are stored in the text index.|-valign="top"
| '''Errors'''
|{{Error|open|#Errors}} the addressed database does not exist or could not be opened.<br/>{{Error|no-index|#Errors}} the index is not available.<br/>
|-valign="top"
| '''Examples'''
|
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|<pre>db:text-range|( $db database as xs:string, $min as xs:string, $max as xs:string|) as text()*}}</pre>|-valign="top"
| '''Summary'''
|Returns all text nodes of the database a {{Code|$dbdatabase}} whose values are between larger than or equal to {{Code|$min}} and smaller than or equal to {{Code|$max}} and that are stored in the text index.|-valign="top"
| '''Errors'''
|{{Error|open|#Errors}} the addressed database does not exist or could not be opened.<br/>{{Error|no-index|#Errors}} the index is not available.<br/>
|-valign="top"
| '''Examples'''
|
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|db:attribute|$db as xs:string, $strings as xs:string*|attribute()*}}<br/pre>{{Func|db:attribute|( $db database as xs:string, $strings values as xs:string*, $name as xs:string| := ()) as attribute()*}}</pre>|-valign="top"
| '''Summary'''
|Returns all attribute nodes of the database a {{Code|$dbdatabase}} that have match one of the specified {{Code|$stringsvalues}} 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'''
|{{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'''
|
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|db:attribute-range|$db as xs:string, $min as xs:string, $max as xs:string|attribute()*}}<br/pre>{{Func|db:attribute-range|( $db database as xs:string, $min as xs:string, $max as xs:string, $name as xs:string| := ()) as attribute()*}}</pre>|-valign="top"
| '''Summary'''
|Returns all attributes of the database a {{Code|$dbdatabase}}, the string whose 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'''
|{{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'''
|
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|db:token|$db as xs:string, $tokens as xs:string*|attribute()*}}<br/pre>{{Func|db:token|( $db database as xs:string, $tokens as xs:string*, $name as xs:string| := ()) as attribute()*}}</pre>|-valign="top"
| '''Summary'''
|Returns all attribute nodes of the database a {{Code|$dbdatabase}} the values of which contain one of the specified {{Code|$tokens}}.<br />If {{Code|$name}} is specified, the resulting attribute nodes are filtered by their attribute name.|-valign="top"
| '''Errors'''
|{{Error|open|#Errors}} the addressed database does not exist or could not be opened.<br/>{{Error|no-index|#Errors}} the index is not available.<br/>
|-valign="top"
| '''Examples'''
|
=Updates=
All functions in this section are {{Function|Database[[#Updating Functions|Updating Functions}}]].
==db:create==
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|db:create|$db as xs:string|empty-sequence()}}<br/pre>{{Func|db:create|( $db database 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(*)?| := map { }) as empty-sequence()}}</pre>|-valign="top"
| '''Summary'''
|Creates a new database with name {{Code|$dbdatabase}} and adds initial documents specified via the supplied {{Code|$inputs}} to the specified {{Code|$paths}}:* {{Code|$The inputs}} may be strings or nodes:
** nodes may be of any type except for attributes
** strings can be a URI pointing to a file/directory or an XML string (which is detected by the leading <code>&lt;</code> character)
* The parsing and indexing behavior can be controlled via {{Code|$options}}:
** allowed options are {{Option|ADDCACHE}} and the [[Options#Indexing|indexing]], [[Options#Full-Text Indexing|full-text indexing]], [[Options#Parsing|parsing]] and [[Options#XML Parsing|XML parsing]] options, all in lower case
** parsing options will only impact string input (URIs, XML strings), because nodes have already been parsed.
* An existing database will be overwritten.
* Database creation takes place after most other update operations (see [[XQuery Update#Pending Update List|Pending Update List]]). As a consequence, a newly created database cannot be addressed in the same query.
|-valign="top"
| '''Errors'''
|{{Error|lock|#Errors}} a database is opened by another process.<br/>{{Error|name|#Errors}} the specified name is not a [[Commands#Valid_Names|valid database name]].<br/>{{Error|conflict|#Errors}} the same database was addressed more than once.<br/>{{Error|args|#Errors}} the number of specified inputs and paths differs.
|-valign="top"
| '''Examples'''
|
|}
==db:dropadd==
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|<pre>db:drop|add( $database as xs:string, $input as item(), $db path as xs:string|? := (), $options as map(*)? := map { }) as empty-sequence()}}</pre>|-valign="top"
| '''Summary'''
|Drops Adds the specified {{Code|$input}} to a {{Code|$database }} with the specified {{Code|$path}}:* A document with the same path may occur more than once in a database. If you want to enforce single instances, use {{Function||db:put}} instead.* See {{Function||db:create}} for more details on the input and path arguments.* The parsing behavior can be controlled via {{Code|$options}}:** allowed options are {{Option|ADDCACHE}} and the [[Options#Parsing|parsing]] and [[Options#XML Parsing|XML parsing]] options, all connected resources.in lower case** parsing options will only impact string input (URIs, XML strings) because nodes have already been parsed|-valign="top"
| '''Errors'''
|{{Error|open|#Errors}} the addressed database does not exist or could not be opened.<br/>{{Error|lock|#Errors}} a database is opened by another process.<br/>{{Error|conflict|#Errors}} the same database was addressed more than once.|-valign="top"
| '''Examples'''
|
* <code>db:add("DB", "/home/dir/doc.xml")</code> adds the file {{Code|/home/dir/doc.xml}} to the database {{Code|DB}}.* <code>db:dropadd("DB", <a/>, "doc.xml")</code> adds a document node to the database {{Code|DB}} drops under the name {{Code|doc.xml}}.* <code>db:add("DB", "/home/dir", "docs/dir", map { 'addcache': true() })</code> adds all documents in {{Code|/home/dir}} to the database {{Code|DB}}under the path {{Code|docs/dir}}. To reduce memory consumption, the files will be cached before being added to the database.
|}
==db:addput==
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|db:add|$db as xs:string, $input as item()|empty-sequence()}}<br/pre>{{Func|db:add|$db as xs:string, $input as itemput(), $path as xs:string?|empty-sequence()}}<br/>{{Func|db:add|$db database as xs:string, $input as item(), $path as xs:string?, $options as map(*)?|empty-sequence( := map { })|as empty-sequence()}}</pre>|-valign="top"
| '''Summary'''
|Adds documents Replaces a resource, specified by {{Code|$inputpath}} to the database , in a {{Code|$dbdatabase}} with the specified contents of {{Code|$pathinput}}:* A document with the same path may occur more than once in , or adds it as a database. If you want to enforce single instances, use {{Function||db:update}} instead.* See {{Function||dbnew resource:create}} for more details on the input and path arguments.
* The parsing behavior can be controlled via {{Code|$options}}:
** allowed Allowed options are {{Option|ADDCACHE}} and the [[Options#Parsing|parsing]] and [[Options#XML Parsing|XML parsing]] options, all in lower case.** parsing Parsing options will only impact string input (URIs, XML strings), because nodes have already been parsed.* See {{Function||db:create}} for more details on the input argument.|- valign="top"| '''Errors'''|{{Error|open|#Errors}} the addressed database does not exist or could not be opened.<br/>{{Error|target|#Errors}} the path points to a directory.|- valign="top"| '''Examples'''|* {{Code|db:put("DB", "/home/dir/doc.xml", "docs/dir/doc.xml")}} replaces the content of the document {{Code|docs/dir/doc.xml}} in the database {{Code|DB}} with the content of the file {{Code|/home/dir/doc.xml}}.* {{Code|db:put("DB", "<a/>", "docs/dir/doc.xml")}} replaces the content of the document {{Code|docs/dir/doc.xml}} in the database {{Code|DB}} with {{Code|&lt;a/&gt;}}.* {{Code|db:put("DB", document { <a/> }, "docs/dir/doc.xml")}} replaces the content of the document {{Code|docs/dir/doc.xml}} in the database {{Code|DB}} with the specified document node.The following query can be used to import files from a directory to a database:<pre lang='xquery'>let $source := '/home/john/xml/source'for $file in file:list($source, true())let $path := $source || $filewhere not(file:is-dir($path))return db:put('db', doc($path), $file)</pre>|} ==db:put-binary== {| width='100%'|- valign="top"| width='120' | '''Signature'''|<pre>db:put-binary( $database as xs:string, $input as item(), $path as xs:string) as empty-sequence()</pre>|- valign="top"| '''Summary'''|Stores a binary resource specified by {{Code|$input}} in a {{Code|$database}} at the specified {{Code|$path}}. Existing resources are overwritten.|- valign="top"| '''Errors'''|{{Error|open|#Errors}} the addressed database does not exist or could not be opened.<br/>{{Error|mainmem|#Errors}} the database is not ''persistent'' (stored on disk).|- valign="top"| '''Examples'''|* {{Code|db:put-binary('DB', file:read-binary('video.mov'), 'video/sample.mov')}} stores the addressed video file at the specified location.* With the following query, you can copy the binary resources of one database into another:<pre lang='xquery'>let $db := 'db'let $src-path := 'src/'let $trg-path := 'trg/'for $src in db:list($db, $src-path)where db:type($db, $src) = 'binary'let $trg := $trg-path || substring-after($src, $src-path)return db:put-binary($db, db:get-binary($db, $src), $trg)</pre>|} ==db:put-value== {| width='100%'|- valign="top"| width='120' | '''Signature'''|<pre>db:put-value( $database as xs:string, $input as item()*, $path as xs:string) as empty-sequence()</pre>|- valign="top"| '''Summary'''|Stores an {{Code|$input}} value in a {{Code|$database}} at the specified {{Code|$path}}. Existing resources are overwritten. The value can be an arbitrary sequence of atomic items, nodes, maps, and arrays.|-valign="top"
| '''Errors'''
|{{Error|open|#Errors}} the addressed database does not exist or could not be opened.<br/>{{Error|mainmem|#Errors}} the database is not ''persistent'' (stored on disk).|-valign="top"
| '''Examples'''
|
* <code>{{Code|db:addput-value("'DB"', 1 to 10000, "/home/dir/doc.xml"'sequence')</code> adds the file {{Code|/home/dir/doc.xml}} to stores a numeric range in the database {{Code|DB}}.* <code>db:add("DB"With the following query, <a/>, "doc.xml")</code> adds map with countries and associated cities is stored in a document node to the database {{Code|DB}} under the name {{Code|doc.xml}}The value resource can e.g.be used as index in future queries:* <codepre lang='xquery'>db:addput-value("DB" 'factbook', " map:merge( for $country in db:get('factbook')/home/dir"country return map:entry($country/@name, "docs$country//dir"city/name ! string()) ), map { 'addcachecities': true() })</codepre> 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.
|}
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|<pre>db:delete|( $db database as xs:string, $path as xs:string|) as empty-sequence()}}</pre>|-valign="top"
| '''Summary'''
|Deletes resource(s), specified by {{Code|$path}}, from the database specified {{Code|$dbdatabase}}.|-valign="top"
| '''Errors'''
|{{Error|open|#Errors}} the addressed database does not exist or could not be opened.<br/>{{Error|path|#Errors}} the specified path is invalid.
|-valign="top"
| '''Examples'''
|
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|<pre>db:copy|( $db database as xs:string, $new-name as xs:string|) as empty-sequence()}}</pre>|-valign="top"
| '''Summary'''
|Creates a copy of the database {{Code|$dbdatabase}}, which will be called {{Code|$new-name}}.|-valign="top"
| '''Errors'''
|{{Error|open|#Errors}} the addressed database does not exist or could not be opened.<br/>{{Error|lock|#Errors}} a database is opened by another process.<br/>{{Error|name|#Errors}} invalid database name.<br/>{{Error|conflict|#Errors}} the same database was addressed more than once.
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|<pre>db:alter|( $db database as xs:string, $new-name as xs:string|) as empty-sequence()}}</pre>|-valign="top"
| '''Summary'''
|Renames the database a {{Code|$dbdatabase}} to {{Code|$new-name}}.|-valign="top"
| '''Errors'''
|{{Error|open|#Errors}} the addressed database does not exist or could not be opened.<br/>{{Error|lock|#Errors}} a database is opened by another process.<br/>{{Error|name|#Errors}} invalid database name.<br/>{{Error|conflict|#Errors}} the same database was addressed more than once.
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|db:optimize|$db as xs:string|empty-sequence()}}<br/pre>{{Func|db:optimize|( $db database as xs:string, $all as xs:boolean|empty-sequence? := false()}}<br/>{{Func|db:optimize|$db as xs:string, $all as xs:boolean, $options as map(*)?| := map { }) as empty-sequence()}}</pre>|-valign="top"
| '''Summary'''
|Optimizes the meta data metadata and indexes of the database a {{Code|$dbdatabase}}.<br/>If {{Code|$all}} is {{Code|true}}, the complete database will be rebuilt.<br/>The {{Code|$options}} argument can be used to control indexing. The syntax is identical to the {{Function||db:create}} function: Allowed options are all [[Options#Indexing|indexing]] and [[Options#Full-Text|full-text]] options. {{Option|UPDINDEX}} is only supported if {{Code|$all}} is {{Code|true}}.|-valign="top"
| '''Errors'''
|{{Error|open|#Errors}} the addressed database does not exist or could not be opened.
|-valign="top"
| '''Examples'''
|
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|<pre>db:rename|( $db database as xs:string, $source as xs:string, $target as xs:string|) as empty-sequence()}}</pre>|-valign="top"
| '''Summary'''
|Moves all resources(s) of database a {{Code|$dbdatabase}}, which are found in the supplied {{Code|$source}} path, to the supplied {{Code|$target}} path. The paths may point to single resources or directories. No updates will take place if a non-existing source path is supplied.|-valign="top"
| '''Errors'''
|{{Error|open|#Errors}} the addressed database does not exist or could not be opened.<br/>{{Error|path|#Errors}} the specified source or target path, or one of its descendants, is invalid.
|-valign="top"
| '''Examples'''
|
* {{Code|db:rename("DB", "docs/dir/doc.xml", "docs/dir/newdoc.xml")}} renames the resource {{Code|docs/dir/doc.xml}} to {{Code|docs/dir/newdoc.xml}} in the database {{Code|DB}}.
* {{Code|db:rename("DB", "docs/dir", "docs/newdir")}} moves all resources in the database {{Code|DB}} from {{Code|docs/dir}} to {{Code|docs/newdir}}.
|}
==db:updateflush== {{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' | '''SignaturesSignature'''|{{Func|<pre>db:update|flush( $db database as xs:string, $input as item(), $path as xs:string|empty-sequence()}}<br/pre>{{Func|db:update|$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 Explicitly flushes the contents buffers of {{Code|$input}}, or adds it as a new resource:* The parsing behavior can be controlled via {{Code|$optionsdatabase}}:** Allowed options are . This command is only useful if {{Option|ADDCACHEAUTOFLUSH}} 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 has been parsed.* See set to {{FunctionCode||db:createfalse}} 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.|-| '''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 || $filewhere not(file:is-dir($path))return db:update('db', doc($path), $file)</syntaxhighlight>
|}
==db:putdrop== {{Announce|Introduced with Version 10.}}
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|<pre>db:put|drop( $db database as xs:string, $input as item()*, $path as xs:string|empty-sequence()}}</pre>|-valign="top"
| '''Summary'''
|Replaces an XQuery value specified by Drops a {{Code|$input}} in the database {{Code|$db}} and the location specified by {{Code|$path}}, or adds it as new resourceall connected resources.|-valign="top"
| '''Errors'''
|{{Error|open|#Errors}} the addressed database does not exist or could not be opened.<br/>{{Error|mainmemlock|#Errors}} a database is opened by another process.<br/>{{Error|conflict|#Errors}} the same database is not ''persistent'' (stored on disk)was addressed more than once.|-valign="top"
| '''Examples'''
|
* {{Code|db:putdrop('"DB', 1 to 10000, 'sequence')}} stores a numeric range in the database.* 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 drops 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=
 
{{Announce|Introduced with Version 10:}} Support for general data ([[User Management|registered users]], [[Jobs 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}}.
==db:create-backup==
 
{{Announce|Updated with Version 10:}} Options argument added.
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|db:create-backup|$db as xs:string|empty-sequence()}}<br/pre>{{Func|db:create-backup|( $db database as xs:string, $options as map(*)|? := map { }) as empty-sequence()}}</pre>|-valign="top"
| '''Summary'''
|Creates a backup of the database a {{Code|$dbdatabase}}. If no name is supplied, general data will be backed up. The following {{Code|$options}} are available:
* With {{Code|comment}}, a comment string can be attached to the backup.
* By setting {{Code|compress}} to false, the backup will be created faster, but it will take more space on disk.
|-valign="top"
| '''Errors'''
|{{Error|open|#Errors}} the addressed database does not exist or could not be opened.<br/>{{Error|name|#Errors}} invalid database name.<br/>{{Error|conflict|#Errors}} the same database was addressed more than once.
|-valign="top"
| '''Examples'''
|
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|<pre>db:drop-backup|( $name as xs:string|) as empty-sequence()}}</pre>|-valign="top"
| '''Summary'''
|Drops all backups of the database with the specified {{Code|$name}}. If the name ends with a timestamp, only the specified backup file will be deleted. If no name is supplied, backups with general data are addressed.
|-valign="top"
| '''Errors'''
|{{Error|backup|#Errors}} No backup file found.<br/>{{Error|name|#Errors}} invalid database name.<br/>{{Error|conflict|#Errors}} the same database was addressed more than once.
|-valign="top"
| '''Examples'''
|
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|<pre>db:alter-backup|( $name as xs:string, $new-name as xs:string|) as empty-sequence()}}</pre>|-valign="top"
| '''Summary'''
|Renames all backups of the database with the specified {{Code|$name}} to {{Code|$new-name}}. If the name ends with a date, only the specified backup file will be renamed.
|-valign="top"
| '''Errors'''
|{{Error|backup|#Errors}} No backup file found.<br/>{{Error|name|#Errors}} invalid database name.<br/>{{Error|conflict|#Errors}} the same database was addressed more than once.
|-valign="top"
| '''Examples'''
|
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|<pre>db:restore|( $name as xs:string|) as empty-sequence()}}</pre>|-valign="top"
| '''Summary'''
|Restores the database with the specified {{Code|$name}}. The {{Code|$name}} may include the timestamp of the backup file. If no name is supplied, general data will be restored. If general data is restored, it will only be available after BaseX has been restarted.
|-valign="top"
| '''Errors'''
|{{Error|lock|#Errors}} a database is opened by another process.<br/>{{Error|name|#Errors}} invalid database name.<br/>{{Error|no-backup|#Errors}} No backup found.<br/>{{Error|conflict|#Errors}} the same database was addressed more than once.
|-valign="top"
| '''Examples'''
|
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|db:backups||element(backup)*}}<br/pre>{{Func|db:backups|( $db database as xs:string| := ()) as element(backup)*}}</pre>|-valign="top"
| '''Summary'''
|Returns an element sequence containing all available database backups with timestamp, file size and comment.<br/>If a database {{Code|$dbdatabase}} is specified, the sequence will be restricted to the backups matching this database.|-valign="top"
| '''Examples'''
|
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|<pre>db:name|( $node as node()|) as xs:string}}</pre>|-valign="top"
| '''Summary'''
|Returns the name of the database in which the specified [[#Database Nodes|database node]] {{Code|$node}} is stored.
|-valign="top"
| '''Errors'''
|{{Error|node|#Errors}} {{Code|$nodes}} contains a node which is not stored in a database.
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|<pre>db:path|( $node as node()|) as xs:string}}</pre>|-valign="top"
| '''Summary'''
|Returns the path of the database document in which the specified [[#Database Nodes|database node]] {{Code|$node}} is stored.
|-valign="top"
| '''Errors'''
|{{Error|node|#Errors}} {{Code|$nodes}} contains a node which is not stored in a database.
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|db:exists|$db as xs:string|xs:boolean}}<br/pre>{{Func|db:exists|( $db database as xs:string, $path as xs:string| := ()) as xs:boolean}}</pre>|-valign="top"
| '''Summary'''
|Checks if the database a {{Code|$dbdatabase}} exists, or the a resource specified by located at {{Code|$path}} exists. {{Code|false}} is returned if a in this database directory has been addressed.|-valign="top"
| '''Examples'''
|
* {{Code|db:exists("DB")}} returns {{Code|true}} if the database {{Code|DB}} exists.
* {{Code|db:exists("DB", "resource")}} returns {{Code|true}} if {{Code|resource}} is an XML document or a raw fileexists in this database.
|}
==db:type==
 
{{Announce|Introduced with BaseX 10:}} Replaces {{Code|db:is-raw}} and {{Code|db:is-xml}}.
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|<pre>db:type|( $db database as xs:string, $path as xs:string|) as xs:boolean}}string</pre>|-valign="top"
| '''Summary'''
|Returns the type of a resource – ({{Code|xml}}, {{Code|binary}}, or {{Code|value}} ) of a resource in the database a {{Code|$dbdatabase}} at the specified {{Code|$path}}.|-valign="top"
| '''Errors'''
|{{Error|open|#Errors}} the addressed database does not exist or could not be opened.
|-valign="top"
| '''Examples'''
|
* {{Code|db:type("DB", "factbook.xml")}} returns {{Code|true}} if the specified resource is an XML document.
|}
 
==db:is-xml==
 
{| width='100%'
|-
| width='120' | '''Signatures'''
|{{Func|db:is-xml|$db as xs:string, $path as xs:string|xs:boolean}}
|-
| '''Errors'''
|{{Error|open|#Errors}} the addressed database does not exist or could not be opened.
|-
| '''Summary'''
|Checks if the specified resource in the database {{Code|$db}} and the path {{Code|$path}} exists, and if it is an XML document.
|-
| '''Examples'''
|
* {{Code|db:is-xml("DB", "dir/doc.xml")}} returns {{Code|true}}.
|}
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|<pre>db:content-type|( $db database as xs:string, $path as xs:string|) as xs:string}}</pre>|-valign="top"
| '''Summary'''
|Retrieves the content -type of a resource in the database a {{Code|$dbdatabase}} and at the path specified {{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'''
|{{Error|open|#Errors}} the addressed database does not exist or could not be opened.
|-valign="top"
| '''Examples'''
|
{| class="wikitable" width="100%"
! width="110"|Code
|Description|-valign="top"
|{{Code|args}}
|The number of specified inputs and paths differs.
|-valign="top"
|{{Code|conflict}}
|Multiple update operations point to the same target.
|-valign="top"
|{{Code|lock}}
|A database cannot be updated because it is opened by another process.
|-valign="top"
|{{Code|mainmem}}
|The addressed database is not ''persistent'' (stored on disk).
|-valign="top"
|{{Code|name}}
|The name of the specified database is invalid.
|-valign="top"
|{{Code|no-backup}}
|No backup exists for a database.
|-valign="top"
|{{Code|node}}
|The referenced XML node is no [[#Database Nodes|database node]], i.e. it is neither stored in a database nor represented as database fragment.
|-valign="top"
|{{Code|no-index}}
|The database lacks an index structure required by the called function.
|-valign="top"
|{{Code|open}}
|The addressed database does not exist or could not be opened.
|-valign="top"
|{{Code|option}}
|The specified option is unknown.
|-valign="top"
|{{Code|path}}
|The specified database path is invalid.
|-valign="top"
|{{Code|property}}
|The specified database property is unknown.
|-valign="top"
|{{Code|range}}
|The addressed database id ID or pre PRE value is out of range.|-valign="top"
|{{Code|target}}
|Path points to an invalid target.
;Version 10
* Added: {{Function||db:get}}, {{Function||db:put}}, {{Function||db:type}}.
* Added: [[#Backups|Backups]]: Support for general data ([[User Management|registered users]], [[Jobs Job Module#Services|scheduled services]] and [[Store Module|key-value stores]]).* Updated: {{Function||db:replaceget}}, {{Function||db:get-id}}, {{Function||db:get-pre}} renamed (before: {{Code|db:open}}, {{Code|db:open-id}}, {{Code|db:open-pre}} and )* Updated: {{Function||db:put-binary}} renamed (before: {{Code|db:replace}} ); function signature aligned with {{Function||db:add}} (second and third argument swapped).* Updated: {{Function||db:put-binary}} renamed (before: {{Code|db:store}}); function signature aligned with {{Function||db:add}} (second and third argument swapped).
* Updated: {{Function||db:get-binary}} renamed (before: {{Code|db:retrieve}}).
* Updated: {{Function||db:backups}}, {{Function||db:create-backup}}: Options added.
* DeletesRemoved: {{Code|db:is-raw}}, {{Code|db:is-raw}} (new: {{Function||db:type}}).
;Version 9.3
* Added: {{Function||db:alter-backup}}
* Updated: {{Function|Code|db:open-id}}, {{Function|Code|db:open-pre}}: support for multiple integers
;Version 9.2
;Version 7.8
* Removed: db:fulltext (use [[{{Function|Full-Text Module#ft:search|ft:search]] }} instead)
;Version 7.7
* Added: {{Function||db:export}}, {{Function||db:name}}, {{Function||db:path}}
* Updated: {{Code|$options}} argument added to {{Function||db:create}} and {{Function||db:optimize}}.
* Updated: the functions no longer accept {{Function|[[#DatabaseNodes|Database Nodes}} database nodes]] as reference. Instead, the name of a database must now be specified.
;Version 7.6
;Version 7.0
* Added: {{Function||db:exists}}, {{Code|db:retrieve}}, {{Code|db:store}}, {{Code|db:is-raw}}, {{Code|db:is-xml}}
* Updated: {{Function||db:list}}, {{Function|Code|db:open}}, {{Function||db:add}}
Bureaucrats, editor, reviewer, Administrators
13,554

edits

Navigation menu