dba/options  library module
PA

Summary

Global options.
Tags

Author: Christian Grün, BaseX Team 2005-21, BSD License

__source : lib/options.xqm

Related documents
ViewDescriptionFormat
xqdocxqDoc xml file from the source modulexml
xqparsexqparse xml file from the source modulexml

Imports

This module is imported by 9 modules. It imports 0 modules.

dba/files 2 3 4 5 6 7 8
dba/users 2 3 4 5 6 7
dba/users 2 3 4 5 6 7
dba/users 2 3 4 5 6 7
imports
this
imports
(None)

Variables

3.1 $options:DBA-DIRECTORY

Summary
DBA directory.
Type
item()*

3.2 $options:DEFAULTS

Summary
Default options.
Type
map(*)
Annotations
%basex:lazy()
%private()

3.3 $options:FILE

Summary
Options file.
Type
xs:string
Annotations
%private()

3.4 $options:IGNORE-LOGS

Summary
Show DBA log entries.
Type
xs:string

3.5 $options:MAXCHARS

Summary
Maximum length of XML characters.
Type
xs:string

3.6 $options:MAXROWS

Summary
Maximum number of table entries.
Type
xs:string

3.7 $options:MEMORY

Summary
Maximal memory consumption.
Type
xs:string

3.8 $options:OPTIONS

Summary
Currently assigned options.
Type
item()*
Annotations
%basex:lazy()
%private()

3.9 $options:PERMISSION

Summary
Permission when running queries.
Type
xs:string

3.10 $options:PERMISSIONS

Summary
Permissions.
Type
item()*

3.11 $options:TIMEOUT

Summary
Query timeout.
Type
xs:string

Functions

4.1 options:get

Arities: options:get#1

Summary
Returns the value of an option.
Signature
options:get ( $name as xs:string )  as xs:anyAtomicType
Parameters
  • name as xs:string name of option
Return
  • xs:anyAtomicTypevalue
Invoked by 7 functions from 3 modules
Source ( 5 lines)
function options:get(
  $name  as xs:string
) as xs:anyAtomicType {
  $options:OPTIONS($name)
}

4.2 options:save

Arities: options:save#1

Summary
Saves options.
Signature
options:save ( $options as map(*) )  as empty-sequence()
Parameters
  • options as map(*) keys/values that have been changed
Return
  • empty-sequence()
Invokes 2 functions from 2 modules
  • {http://expath.org/ns/file}write#2
  • {http://www.w3.org/2005/xpath-functions/map}for-each#2
Invoked by 1 functions from 1 modules
Source ( 9 lines)
function options:save(
  $options  as map(*)
) as empty-sequence() {
  file:write($options:FILE, element options {
    map:for-each($options:DEFAULTS, function($key, $value) {
      element { $key } { ($options($key), $value)[1] }
    })
  })
}

Namespaces

The following namespaces are defined:

PrefixUri
annhttp://www.w3.org/2012/xquery
basexhttp://basex.org
optionsdba/options

6 RestXQ

None

Source Code

(:~
 : Global options.
 :
 : @author Christian Grün, BaseX Team 2005-21, BSD License
 :)
module namespace options = 'dba/options';

(:~ DBA directory. :)
declare variable $options:DBA-DIRECTORY := (
  for $dir in db:option('dbpath') || '/.dba'
  return (
    if(file:exists($dir)) then () else file:create-dir($dir),
    file:path-to-native($dir)
  )
);

(:~ Permissions. :)
declare variable $options:PERMISSIONS := ('none', 'read', 'write', 'create', 'admin');

(:~ Maximum length of XML characters. :)
declare variable $options:MAXCHARS := 'maxchars';
(:~ Maximum number of table entries. :)
declare variable $options:MAXROWS := 'maxrows';
(:~ Query timeout. :)
declare variable $options:TIMEOUT := 'timeout';
(:~ Maximal memory consumption. :)
declare variable $options:MEMORY := 'memory';
(:~ Permission when running queries. :)
declare variable $options:PERMISSION := 'permission';
(:~ Show DBA log entries. :)
declare variable $options:IGNORE-LOGS := 'ignore-logs';

(:~ Options file. :)
declare %private variable $options:FILE := $options:DBA-DIRECTORY || '.dba.xml';

(:~ Default options. :)
declare %basex:lazy %private variable $options:DEFAULTS := map {
  $options:MAXCHARS  : 200000,
  $options:MAXROWS   : 200,
  $options:TIMEOUT   : 30,
  $options:MEMORY    : 500,
  $options:PERMISSION: 'admin',
  $options:IGNORE-LOGS: ''
};

(:~ Currently assigned options. :)
declare %basex:lazy %private variable $options:OPTIONS := (
  if(file:exists($options:FILE)) then (
    try {
      (: merge defaults with saved options :)
      let $options := fetch:xml($options:FILE)/options
      return map:merge(
        map:for-each($options:DEFAULTS, function($key, $value) {
          map:entry($key,
            let $option := $options/*[name() = $key]
            return if($option) then (
              typeswitch($value)
                case xs:numeric  return xs:integer($option)
                case xs:boolean  return xs:boolean($option)
                default          return xs:string($option)
            ) else (
              $value
            )
          )
        })
      )
    } catch * {
      (: use defaults if an error occurs while parsing the options :)
      $options:DEFAULTS
    }
  ) else (
    $options:DEFAULTS
  )
);

(:~
 : Returns the value of an option.
 : @param  $name  name of option
 : @return value
 :)
declare function options:get(
  $name  as xs:string
) as xs:anyAtomicType {
  $options:OPTIONS($name)
};

(:~
 : Saves options.
 : @param  $options  keys/values that have been changed
 :)
declare function options:save(
  $options  as map(*)
) as empty-sequence() {
  file:write($options:FILE, element options {
    map:for-each($options:DEFAULTS, function($key, $value) {
      element { $key } { ($options($key), $value)[1] }
    })
  })
};