Difference between revisions of "Database Module"
(23 intermediate revisions by the same user not shown) | |||
Line 8: | Line 8: | ||
==Database Nodes== | ==Database Nodes== | ||
− | Database nodes are XML nodes which are either stored in a persistent database or | + | Database nodes are XML nodes which are either stored in a persistent database, or which contained in a so-called ''database fragment''. All XML fragments can be converted to database fragments by e. g. applying the [[XQuery_Update#transform|transform]] expression on an XML fragment: |
<pre class="brush:xquery"> | <pre class="brush:xquery"> | ||
Line 39: | Line 39: | ||
| '''Errors''' | | '''Errors''' | ||
|{{Error|BXDB0002|XQuery Errors#BaseX Errors}} the addressed database does not exist or could not be opened. | |{{Error|BXDB0002|XQuery Errors#BaseX Errors}} the addressed database does not exist or could not be opened. | ||
+ | |} | ||
+ | |||
+ | ==db:property== | ||
+ | |||
+ | {{Mark|Introduced with Version 8.6}}: | ||
+ | |||
+ | {| width='100%' | ||
+ | |- | ||
+ | | width='120' | '''Signatures''' | ||
+ | |{{Func|db:property|$db as xs:string, $property as xs:string|xs:anyAtomicType}} | ||
+ | |- | ||
+ | | '''Summary''' | ||
+ | |Returns the specified {{Code|$property}} of the database {{Code|$db}}. The available properties are the ones returned by [[#db:info|db:info]]. | ||
+ | |- | ||
+ | | '''Errors''' | ||
+ | |{{Error|BXDB0017|XQuery Errors#BaseX Errors}} the specified property is unknown. | ||
+ | |- | ||
+ | | '''Examples''' | ||
+ | | | ||
+ | * <code>db:property('db', 'size')</code> returns the number of bytes occupied by the database <code>db</code>. | ||
+ | * <code>db:property('xmark', 'textindex')</code> indicates if the <code>xmark</code> database has a text index. | ||
+ | * <code>db:property('discogs', 'uptodate')</code> indicates if the database statistics and index structures of the <code>discogs</code> database are up-to-date. | ||
|} | |} | ||
Line 49: | Line 71: | ||
|- | |- | ||
| '''Summary''' | | '''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|$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 resources can be | + | * The list of returned resources can be restricted by the {{Code|$path}} argument. |
|- | |- | ||
| '''Errors''' | | '''Errors''' | ||
Line 58: | Line 81: | ||
| '''Examples''' | | '''Examples''' | ||
| | | | ||
− | * {{Code|db:list("docs")}} returns the names of all documents | + | * {{Code|db:list("docs")}} returns the names of all documents of a database named {{Code|docs}}. |
|} | |} | ||
Line 69: | Line 92: | ||
|- | |- | ||
| '''Summary''' | | '''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|$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 {{Code|$path}} argument. | * Returned databases resources can be further restricted by the {{Code|$path}} argument. | ||
|- | |- | ||
Line 80: | Line 104: | ||
| '''Examples''' | | '''Examples''' | ||
| | | | ||
− | * {{Code|db:list-details(" | + | * {{Code|db:list-details("shop")}} returns the names plus additional info on all resources of a database named {{Code|shop}}. |
|} | |} | ||
Line 201: | Line 225: | ||
* {{Code|db:retrieve("DB", "music/01.mp3")}} returns the specified audio file as raw data. | * {{Code|db:retrieve("DB", "music/01.mp3")}} returns the specified audio file as raw data. | ||
* <code><nowiki>stream:materialize(db:retrieve("DB", "music/01.mp3"))</nowiki></code> materializes the streamable result in main-memory before returning it. | * <code><nowiki>stream:materialize(db:retrieve("DB", "music/01.mp3"))</nowiki></code> materializes the streamable result in main-memory before returning it. | ||
+ | * <code><nowiki>convert:binary-to-string(db:retrieve("DB", "info.txt"), 'UTF-8')</nowiki></code> converts a binary database resource as UTF-8 text and returns a string. | ||
|} | |} | ||
Line 352: | Line 377: | ||
* {{Code|$inputs}} may be strings or nodes: | * {{Code|$inputs}} may be strings or nodes: | ||
** nodes may be of any type except for attributes | ** nodes may be of any type except for attributes | ||
− | ** strings | + | ** strings can be a URI pointing to a file/directory or an XML string (which is detected by the leading <code><</code> character) |
** a path must be specified if the input is not a file or directory reference | ** a path must be specified if the input is not a file or directory reference | ||
− | * The {{Code|$options}} | + | * 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. | + | * 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. | ||
|- | |- | ||
| '''Errors''' | | '''Errors''' | ||
Line 399: | Line 425: | ||
|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 [[#db:replace|db:replace]] instead. | * A document with the same path may occur more than once in a database. If you want to enforce single instances, use [[#db:replace|db:replace]] instead. | ||
− | * | + | * See [[#db:create|db:create]] for more details on the input argument. |
+ | * 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 | ||
|- | |- | ||
| '''Errors''' | | '''Errors''' | ||
Line 406: | Line 435: | ||
| '''Examples''' | | '''Examples''' | ||
| | | | ||
− | * | + | * <code>db:add("DB", "/home/dir/doc.xml")</code> adds the file {{Code|/home/dir/doc.xml}} to the database {{Code|DB}}. |
− | * | + | * <code>db:add("DB", <a/>, "doc.xml")</code> adds a document node to the database {{Code|DB}} under the name {{Code|doc.xml}}. |
− | * | + | * <code>db:add("DB", "/home/dir", "docs/dir", map { 'addcache': true() })</code> adds all documents in {{Code|/home/dir}} to the database {{Code|DB}} under the path {{Code|docs/dir}}. In order to reduce memory consumption, the files will be cached before being added to the database. |
|} | |} | ||
Line 435: | Line 464: | ||
|- | |- | ||
| width='120' | '''Signatures''' | | width='120' | '''Signatures''' | ||
− | |{{Func|db:copy|$db as xs:string, $ | + | |{{Func|db:copy|$db as xs:string, $name as xs:string|empty-sequence()}} |
|- | |- | ||
| '''Summary''' | | '''Summary''' | ||
− | |Creates a copy of the database | + | |Creates a copy of the database {{Code|$db}}, which will be called {{Code|$name}}. |
|- | |- | ||
| '''Errors''' | | '''Errors''' | ||
Line 449: | Line 478: | ||
|- | |- | ||
| width='120' | '''Signatures''' | | width='120' | '''Signatures''' | ||
− | |{{Func|db:alter|$db as xs:string, $ | + | |{{Func|db:alter|$db as xs:string, $name as xs:string|empty-sequence()}} |
|- | |- | ||
| '''Summary''' | | '''Summary''' | ||
− | |Renames the database | + | |Renames the database {{Code|$db}} to {{Code|$name}}. |
|- | |- | ||
| '''Errors''' | | '''Errors''' | ||
Line 553: | Line 582: | ||
==db:replace== | ==db:replace== | ||
− | |||
− | |||
{| width='100%' | {| width='100%' | ||
Line 563: | Line 590: | ||
| '''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: | |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: | ||
− | * See [[#db: | + | * See [[#db:create|db:create]] for more details on the input argument. |
+ | * 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 | ||
+ | * For historical reasons, the order of the 2nd and 3rd argument is different to [[#db:add|db:add]] and [[#db:create|db:create]] | ||
|- | |- | ||
| '''Errors''' | | '''Errors''' | ||
Line 591: | Line 622: | ||
|- | |- | ||
| '''Summary''' | | '''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''' | | '''Errors''' | ||
Line 599: | Line 630: | ||
| | | | ||
* {{Code|db:store("DB", "video/sample.mov", file:read-binary('video.mov'))}} stores the addressed video file at the specified location. | * {{Code|db:store("DB", "video/sample.mov", file:read-binary('video.mov'))}} stores the addressed video file at the specified location. | ||
+ | * With the following query, you can copy full directories: | ||
+ | <pre class="brush:xquery"> | ||
+ | let $db := 'db' | ||
+ | let $src-path := 'src/' | ||
+ | let $trg-path := 'trg/' | ||
+ | for $src in db:list($db, $src-path) | ||
+ | where db:is-raw($db, $src) | ||
+ | let $trg := $trg-path || substring-after($src, $src-path) | ||
+ | return db:store($db, $trg, db:retrieve($db, $src)) | ||
+ | </pre> | ||
|} | |} | ||
Line 787: | Line 828: | ||
|{{Code|BXDB0014}} | |{{Code|BXDB0014}} | ||
|Path points to a directory. | |Path points to a directory. | ||
+ | |- | ||
+ | |{{Code|BXDB0015}} | ||
+ | |No backup is found. | ||
+ | |- | ||
+ | |{{Code|BXDB0016}} | ||
+ | |Name of the source and target database is equal. | ||
+ | |- | ||
+ | |{{Code|BXDB0017}} | ||
+ | |The specified property is unknown.. | ||
|} | |} | ||
=Changelog= | =Changelog= | ||
+ | |||
+ | ;Version 8.6 | ||
+ | * Added: [[#db:property|db:property]] | ||
;Version 8.4 | ;Version 8.4 |
Revision as of 11:24, 20 March 2017
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.
Contents
Conventions
All functions in this module are assigned to the http://basex.org/modules/db
namespace, which is statically bound to the db
prefix.
All errors are assigned to the http://basex.org/errors
namespace, which is statically bound to the bxerr
prefix.
Database Nodes
Database nodes are XML nodes which are either stored in a persistent database, or which contained in a so-called database fragment. All XML fragments can be converted to database fragments by e. g. applying the transform expression on an XML fragment:
copy $c := element hello { 'world' } modify () return $c
General Functions
db:system
Signatures | db:system() as element(system)
|
Summary | Returns information on the database system, such as the database path and current database settings. The output is similar to the INFO command. |
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 | BXDB0002 : the addressed database does not exist or could not be opened.
|
db:property
Signatures | db:property($db as xs:string, $property as xs:string) as xs:anyAtomicType
|
Summary | Returns the specified $property of the database $db . The available properties are the ones returned by db:info.
|
Errors | BXDB0017 : the specified property is unknown.
|
Examples |
|
db:list
Signatures | db:list() as xs:string* db:list($db as xs:string) as xs:string* db:list($db as xs:string, $path as xs:string) as xs:string*
|
Summary | The result of this function is dependent on the number of arguments:
|
Errors | BXDB0002 : the addressed database does not exist or could not be opened.
|
Examples |
|
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:
If a database
|
Errors | BXDB0002 : the addressed database does not exist or could not be opened.
|
Examples |
|
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. If a database $db is specified, the sequence will be restricted to the backups matching this database.
|
Examples |
|
Read Operations
db:open
Signatures | db:open($db as xs:string) as document-node()* db:open($db as xs:string, $path as xs:string) as document-node()*
|
Summary | Opens the database $db and returns all document nodes.The document nodes to be returned can be filtered with the $path argument.
|
Errors | BXDB0002 : the addressed database does not exist or could not be opened.
|
Examples |
|
db:open-pre
Signatures | db:open-pre($db as xs:string, $pre as xs:integer) as node()
|
Summary | Opens the database $db and returns the node with the specified $pre value.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 | BXDB0002 : the addressed database does not exist or could not be opened.BXDB0009 : the specified pre value does not exist in the database.
|
Examples |
|
db:open-id
Signatures | db:open-id($db as xs:string, $id as xs:integer) as node()
|
Summary | Opens the database $db and returns the node with the specified $id value.Each database node has a persistent ID value. Access to the node id can be sped up by turning on the UPDINDEX option.
|
Errors | BXDB0002 : the addressed database does not exist or could not be opened.BXDB0009 : the specified id value does not exist in the database.
|
db:node-pre
Signatures | db:node-pre($nodes as node()*) as xs:integer*
|
Summary | Returns the pre values of the nodes supplied by $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 | BXDB0001 : $nodes contains a node which is not stored in a database.
|
Examples |
|
db:node-id
Signatures | db:node-id($nodes as node()*) as xs:integer*
|
Summary | Returns the id values of the nodes supplied by $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 | BXDB0001 : $nodes contains a node which is not stored in a database.
|
db:retrieve
Signatures | db:retrieve($db as xs:string, $path as xs:string) as xs:base64Binary
|
Summary | Returns a binary resource addressed by the database $db and $path as streamable xs:base64Binary .
|
Errors | BXDB0002 : the addressed database does not exist or could not be opened.BXDB0003 : the database is not persistent (stored on disk).FODC0002 : the addressed resource cannot be retrieved.FODC0007 : the specified path is invalid.
|
Examples |
|
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 (see Serialization for more details), which can either be specified
<output:serialization-parameters> <output:method value='xml'/> <output:cdata-section-elements value="div"/> ... </output:serialization-parameters>
map { "method": "xml", "cdata-section-elements": "div", ... } |
Errors | BXDB0002 : the addressed database does not exist or could not be opened.
|
Examples | Export all files as text:db:export("DB", "/home/john/xml/texts", map { 'method': 'text' }) The following query can be used to export parts of the database: let $target := '/home/john/xml/target' for $doc in db:open('DB', 'collection') let $path := $target || db:path($doc) return ( file:create-dir(file:parent($path)), file:write($path, $doc) ) |
Value Indexes
db:text
Signatures | db:text($db as xs:string, $string as item()) as text()*
|
Summary | Returns all text nodes of the database $db that have $string as their string value and that are stored in the text index.
|
Errors | BXDB0002 : the addressed database does not exist or could not be opened.BXDB0004 : the index is not available. |
Examples |
|
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 that are located in between the $min and $max strings and that are stored in the text index.
|
Errors | BXDB0002 : the addressed database does not exist or could not be opened.BXDB0004 : the index is not available. |
Examples |
|
db:attribute
Signatures | db:attribute($db as xs:string, $string as item()) as attribute()* db:attribute($db as xs:string, $string as item(), $name as xs:string) as attribute()*
|
Summary | Returns all attribute nodes of the database $db that have $string as string value and that are stored in the attribute index.If $name is specified, the resulting attribute nodes are filtered by their attribute name.
|
Errors | BXDB0002 : the addressed database does not exist or could not be opened.BXDB0004 : the index is not available. |
Examples |
|
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 | BXDB0002 : the addressed database does not exist or could not be opened.BXDB0004 : the index is not available. |
Examples |
|
db:token
Signatures | db:token($db as xs:string, $token as item()) as attribute()* db:token($db as xs:string, $token as item(), $name as xs:string) as attribute()*
|
Summary | Returns all attribute nodes of the database $db , the value of which contains the specified $token .If $name is specified, the resulting attribute nodes are filtered by their attribute name.
|
Errors | BXDB0002 : the addressed database does not exist or could not be opened.BXDB0004 : the index is not available. |
Examples |
|
Updates
Important note: All functions in this section are updating functions: they will not be immediately executed, but queued on the Pending Update List, which will be processed after the actual query has been evaluated. This means that the order in which the functions are specified in the query does usually not reflect the order in which the code will be evaluated.
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 :
|
Errors | FODC0002 : an input points to an unknown resource.FOUP0001 : an attribute was specified as input.BXDB0007 : a database is opened by another process.BXDB0011 : the specified name is not a valid database name.BXDB0012 : two db:create statements with the same database name were specified.BXDB0013 : the number of specified inputs and paths differs.
|
Examples |
|
db:drop
Signatures | db:drop($db as xs:string) as empty-sequence()
|
Summary | Drops the database $db and all connected resources.
|
Errors | BXDB0002 : the addressed database does not exist or could not be opened.BXDB0007 : a database is opened by another process.
|
Examples |
|
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 :
|
Errors | BXDB0002 : the addressed database does not exist or could not be opened.FODC0002 : the input points to an unknown resource.FOUP0001 : an attribute was specified as input.
|
Examples |
|
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 | BXDB0002 : the addressed database does not exist or could not be opened.BXDB0008 : the specified path is invalid.
|
Examples |
|
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 | BXDB0002 : the addressed database does not exist or could not be opened.BXDB0011 : invalid database name.BXDB0016 : name of source and target database is equal.
|
db:alter
Signatures | db:alter($db as xs:string, $name as xs:string) as empty-sequence()
|
Summary | Renames the database $db to $name .
|
Errors | BXDB0002 : the addressed database does not exist or could not be opened.BXDB0011 : invalid database name.BXDB0016 : name of source and target database is equal.
|
db:create-backup
Signatures | db:create-backup($db as xs:string) as empty-sequence()
|
Summary | Creates a backup of the database $db .
|
Errors | BXDB0002 : the addressed database does not exist or could not be opened.BXDB0011 : invalid database name.
|
Examples |
|
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 given $name points to a specific backup file, only this specific backup file is deleted.
|
Errors | BXDB0002 : No backup file found.BXDB0011 : invalid database name.
|
Examples |
|
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.
|
Errors | BXDB0011 : invalid database name.BXDB0015 : No backup found.
|
Examples |
|
db:optimize
Signatures | db:optimize($db as xs:string) as empty-sequence() db:optimize($db as xs:string, $all as xs:boolean) as empty-sequence() db:optimize($db as xs:string, $all as xs:boolean, $options as map(*)) as empty-sequence()
|
Summary | Optimizes the meta data 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 | BXDB0002 : the addressed database does not exist or could not be opened.FOUP0002 : an error occurred while optimizing the database.
|
Examples |
|
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 | BXDB0002 : the addressed database does not exist or could not be opened.BXDB0008 : the specified source or target path, or one of its descendants, is invalid.
|
Examples |
|
db:replace
Signatures | db:replace($db as xs:string, $path as xs:string, $input as item()) as empty-sequence() db:replace($db as xs:string, $path as xs:string, $input as item(), $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:
|
Errors | BXDB0002 : the addressed database does not exist or could not be opened.BXDB0014 : the path points to a directory.FODC0002 : the input points to an unknown resource.FOUP0001 : an attribute was specified as input.
|
Examples |
The following query can be used to import files from a directory to a database: let $source := '/home/john/xml/source' for $file in file:list($source, true()) let $path := $source || $file where not(file:is-dir($path)) return db:replace('db', $file, doc($path)) |
db:store
Signatures | db:store($db as xs:string, $path as xs:string, $input as item()) as empty-sequence()
|
Summary | Replaces a binary resource specified by $input in the database $db and the location specified by $path , or adds it as new resource.
|
Errors | BXDB0002 : the addressed database does not exist or could not be opened.BXDB0003 : the database is not persistent (stored on disk).FODC0007 : the specified path is invalid.FOUP0002 : the resource cannot be stored at the specified location.
|
Examples |
let $db := 'db' let $src-path := 'src/' let $trg-path := 'trg/' for $src in db:list($db, $src-path) where db:is-raw($db, $src) let $trg := $trg-path || substring-after($src, $src-path) return db:store($db, $trg, db:retrieve($db, $src)) |
db:output
Signatures | db:output($result as item()*) as empty-sequence()
|
Summary | This function can be used to both perform updates and return results in a single query. The argument of the function will be evaluated, and the resulting items will be cached and returned after the updates on the pending update list have been processed. As nodes may be updated, they will be copied before being cached. The function can only be used together with updating expressions; if the function is called within a transform expression, its results will be discarded. |
Examples |
|
db:output-cache
Signatures | db:output-cache() as item()*
|
Summary | Returns the items that have been cached by db:output. It can be used to check which items will eventually be returned as result of an updating function. This function is non-deterministic: It will return different results before and after items have been cached. It is e. g. useful when writing unit tests. |
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 | BXDB0002 : the addressed database does not exist or could not be opened.
|
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 | BXDB0001 : $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 | BXDB0001 : $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:is-raw
Signatures | db:is-raw($db as xs:string, $path as xs:string) as xs:boolean
|
Summary | Checks if the specified resource in the database $db and the path $path exists, and if it is a binary resource.
|
Errors | BXDB0002 : the addressed database does not exist or could not be opened.
|
Examples |
|
db:is-xml
Signatures | db:is-xml($db as xs:string, $path as xs:string) as xs:boolean
|
Errors | BXDB0002 : the addressed database does not exist or could not be opened.
|
Summary | Checks if the specified resource in the database $db and the path $path exists, and if it is an XML document.
|
Examples |
|
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 | BXDB0002 : the addressed database does not exist or could not be opened.FODC0002 : the addressed resource is not found or cannot be retrieved.
|
Examples |
|
Errors
Code | Description |
---|---|
BXDB0001
|
The referenced XML node is no database node, i.e. it is neither stored in a database nor represented as database fragment. |
BXDB0002
|
The addressed database does not exist or could not be opened. |
BXDB0003
|
The addressed database is not persistent (stored on disk). |
BXDB0004
|
The database lacks an index structure required by the called function. |
BXDB0005
|
A query is expected to exclusively return database nodes of a single database. |
BXDB0006
|
A database path addressed with doc() contains more than one document.
|
BXDB0007
|
A database cannot be updated because it is opened by another process. |
BXDB0008
|
The specified database path is invalid. |
BXDB0009
|
The addressed database id or pre value is out of range. |
BXDB0011
|
The name of the specified database is invalid. |
BXDB0012
|
A database can only be created once. |
BXDB0013
|
The number of specified inputs and paths differs. |
BXDB0014
|
Path points to a directory. |
BXDB0015
|
No backup is found. |
BXDB0016
|
Name of the source and target database is equal. |
BXDB0017
|
The specified property is unknown.. |
Changelog
- Version 8.6
- Added: db:property
- Version 8.4
- Updated: db:create, db:add, db:replace: support for
ADDCACHE
option. - Added: db:token
- 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
- Added: db:output-cache
- Removed: db:event
- Version 7.9
- Updated: parsing options added to db:create, db:add and db:replace.
- Updated: allow
UPDINDEX
if$all
istrue
.
- Version 7.8.2
- Added: db:alter, db:copy, db:create-backup, db:drop-backup, db:restore
- Version 7.8
- Removed: db:fulltext (use ft:search instead)
- Version 7.7
- Added: db:export, db:name, db:path
- Updated:
$options
argument added to db:create and db:optimize. - Updated: the functions no longer accept Database Nodes as reference. Instead, the name of a database must now be specified.
- Version 7.6
- Updated: db:create: allow more than one input and path.
- Version 7.5
- Updated: db:add: input nodes will be automatically converted to document nodes
- Added: db:backups
- Added: db:create
- Added: db:drop
- Version 7.3
- Added: db:flush
- Version 7.2.1
- Added: db:text-range, db:attribute-range, db:output
- Version 7.1
- Added: db:list-details, db:content-type
- Updated: db:info, db:system, db:retrieve
- Version 7.0