dba/util  library module
UP

Summary

Utility functions.
Tags

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

__source : lib/util.xqm

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

Imports

This module is imported by 30 modules. It imports 2 modules.

dba/databases 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
dba/databases 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
dba/databases 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
dba/databases 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
dba/databases 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
dba/databases 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
dba/databases 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
dba/databases 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
dba/databases 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
dba/databases 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
dba/databases 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
dba/databases 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
dba/databases 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
dba/files 2 3 4 5 6 7 8
dba/files 2 3 4 5 6 7 8
dba/files 2 3 4 5 6 7 8
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
dba/users 2 3 4 5 6 7
dba/users 2 3 4 5 6 7
dba/databases 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
imports
this
imports

Variables

None

Functions

4.1 util:capitalize

Arities: util:capitalize#1

Summary
Capitalizes a string.
Signature
util:capitalize ( $string as xs:string )  as xs:string
Parameters
  • string as xs:string string
Return
  • xs:stringcapitalized string
Invokes 3 functions from 1 modules
  • {http://www.w3.org/2005/xpath-functions}substring#2
  • {http://www.w3.org/2005/xpath-functions}substring#3
  • {http://www.w3.org/2005/xpath-functions}upper-case#1
Invoked by 1 functions from 1 modules
Source ( 5 lines)
function util:capitalize(
  $string  as xs:string
) as xs:string {
  upper-case(substring($string, 1, 1)) || substring($string, 2)
}

4.2 util:chop

Arities: util:chop#2

Summary
Chops a string result to the maximum number of allowed characters.
Signature
util:chop ( $string as xs:string, $max as xs:integer )  as xs:string
Parameters
  • string as xs:string string
  • max as xs:integer maximum number of characters
Return
  • xs:stringstring
Invokes 2 functions from 1 modules
  • {http://www.w3.org/2005/xpath-functions}string-length#1
  • {http://www.w3.org/2005/xpath-functions}substring#3
Invoked by 3 functions from 3 modules
Source ( 10 lines)
function util:chop(
  $string  as xs:string,
  $max     as xs:integer
) as xs:string {
  if(string-length($string) > $max) then (
    substring($string, 1, $max) || '...'
  ) else (
    $string
  )
}

4.3 util:end

Arities: util:end#2

Summary
Returns the index of the last result to generate.
Signature
util:end ( $page as xs:integer, $sort as xs:string )  as xs:integer
Parameters
  • page as xs:integer current page
  • sort as xs:string sort key
Return
  • xs:integerlast result
Invokes 2 functions from 2 modules
Invoked by 2 functions from 2 modules
Source ( 10 lines)
function util:end(
  $page  as xs:integer,
  $sort  as xs:string
) as xs:integer {
  if($page and not($sort)) then (
    $page * options:get($options:MAXROWS)
  ) else (
    999999999
  )
}

4.4 util:finalize

Arities: util:finalize#1P

Summary
Finalizes the result of an evaluated query.
Signature
util:finalize ( $result as item()* )  as xs:string
Parameters
  • result as item()* query result
Return
  • xs:stringempty sequence
Invokes 3 functions from 3 modules
Invoked by 2 functions from 1 modules
Annotations
%private()
Source ( 8 lines)
function util:finalize(
  $result   as item()*
) as xs:string {
  (: serialize more characters than requested, because limit represents number of bytes :)
  let $limit := options:get($options:MAXCHARS)
  let $string := serialize($result, map { 'limit': $limit * 2 + 1, 'method': 'basex' })
  return util:chop($string, $limit)
}

4.5 util:info

Arities: util:info#3

Summary
Returns a count info for the specified items.
Signature
util:info ( $items as item()*, $name as xs:string, $action as xs:string )  as xs:string
Parameters
  • items as item()* items
  • name as xs:string name of item (singular form)
  • action as xs:string action label (past tense)
Return
  • xs:stringresult
Invokes 1 functions from 1 modules
  • {http://www.w3.org/2005/xpath-functions}count#1
Invoked by 11 functions from 11 modules
Source ( 8 lines)
function util:info(
  $items   as item()*,
  $name    as xs:string,
  $action  as xs:string
) as xs:string {
  let $count := count($items)
  return $count || ' ' || $name || (if($count > 1) then 's were ' else ' was ') || $action || '.'
}

4.6 util:item-join

Arities: util:item-join#2

Summary
Joins sequence entries.
Signature
util:item-join ( $items as item()*, $sep as item() )  as item()*
Parameters
  • items as item()* items
  • sep as item() separator
Return
  • item() *result
Invoked by 0 functions from 0 modules
    Source ( 7 lines)
    function util:item-join(
      $items  as item()*,
      $sep    as item()
    ) as item()* {
      for $item at $pos in $items
      return ($sep[$pos > 1], $item)
    }

    4.7 util:query

    Arities: util:query#2

    Summary
    Evaluates a query and returns the result.
    Signature
    util:query ( $query as xs:string?, $context as item()* )  as xs:string
    Parameters
    • query as xs:string? query string
    • context as item()* initial context value
    Return
    • xs:stringserialized result of query
    Invokes 3 functions from 2 modules
    Invoked by 2 functions from 2 modules
    Source ( 7 lines)
    function util:query(
      $query    as xs:string?,
      $context  as item()*
    ) as xs:string {
      let $result := xquery:eval($query, map { '': $context }, util:query-options())
      return util:finalize($result)
    }

    4.8 util:query-options

    Arities: util:query-options#0P

    Summary
    Returns the options for evaluating a query.
    Signature
    util:query-options ( )  as map(*)
    Return
    • map(*)options
    Invokes 3 functions from 2 modules
    Invoked by 2 functions from 1 modules
    Annotations
    %private()
    Source ( 8 lines)
    function util:query-options() as map(*) {
      map {
        'timeout'   : options:get($options:TIMEOUT),
        'memory'    : options:get($options:MEMORY),
        'permission': options:get($options:PERMISSION),
        'base-uri'  : config:directory() || '/' || config:query()
      }
    }

    4.9 util:redirect

    Arities: util:redirect#2U

    Summary
    Convenience function for redirecting to another page from update operations.
    Signature
    util:redirect ( $url as xs:string, $params as map(*) )  as empty-sequence()
    Parameters
    • url as xs:string URL
    • params as map(*) query parameters
    Return
    • empty-sequence()
    Invokes 2 functions from 2 modules
    • {http://basex.org/modules/update}output#1
    • {http://basex.org/modules/web}redirect#2
    Invoked by 16 functions from 15 modules
    Annotations
    %updating()
    Source ( 6 lines)
    function util:redirect(
      $url     as xs:string,
      $params  as map(*)
    ) as empty-sequence() {
      update:output(web:redirect($url, $params))
    }

    4.10 util:start

    Arities: util:start#2

    Summary
    Returns the index of the first result to generate.
    Signature
    util:start ( $page as xs:integer, $sort as xs:string )  as xs:integer
    Parameters
    • page as xs:integer current page
    • sort as xs:string sort key
    Return
    • xs:integerlast result
    Invokes 2 functions from 2 modules
    Invoked by 2 functions from 2 modules
    Source ( 10 lines)
    function util:start(
      $page  as xs:integer,
      $sort  as xs:string
    ) as xs:integer {
      if($page and not($sort)) then (
        ($page - 1) * options:get($options:MAXROWS) + 1
      ) else (
        1
      )
    }

    4.11 util:update-query

    Arities: util:update-query#1U

    Summary
    Runs an updating query.
    Signature
    util:update-query ( $query as xs:string? )  as empty-sequence()
    Parameters
    • query as xs:string? query string
    Return
    • empty-sequence()empty sequence
    Invokes 6 functions from 4 modules
    • util:finalize#1
    • util:query-options#0
    • {http://basex.org/modules/update}cache#1
    • {http://basex.org/modules/update}output#1
    • {http://basex.org/modules/xquery}eval-update#3
    • {http://www.w3.org/2005/xpath-functions}true#0
    Invoked by 1 functions from 1 modules
    Annotations
    %updating()
    Source ( 8 lines)
    function util:update-query(
      $query  as xs:string?
    ) as empty-sequence() {
      xquery:eval-update($query, map { }, util:query-options()),
      
      let $result := update:cache(true())
      return update:output(util:finalize($result))
    }

    Namespaces

    The following namespaces are defined:

    PrefixUri
    annhttp://www.w3.org/2012/xquery
    configdba/config
    optionsdba/options
    utildba/util

    6 RestXQ

    None

    Source Code

    (:~
     : Utility functions.
     :
     : @author Christian Grün, BaseX Team 2005-21, BSD License
     :)
    module namespace util = 'dba/util';
    
    import module namespace options = 'dba/options' at 'options.xqm';
    import module namespace config = 'dba/config' at 'config.xqm';
    
    (:~
     : Evaluates a query and returns the result.
     : @param  $query    query string
     : @param  $context  initial context value
     : @return serialized result of query
     :)
    declare function util:query(
      $query    as xs:string?,
      $context  as item()*
    ) as xs:string {
      let $result := xquery:eval($query, map { '': $context }, util:query-options())
      return util:finalize($result)
    };
    
    (:~
     : Runs an updating query.
     : @param  $query  query string
     : @return empty sequence
     :)
    declare %updating function util:update-query(
      $query  as xs:string?
    ) as empty-sequence() {
      xquery:eval-update($query, map { }, util:query-options()),
      
      let $result := update:cache(true())
      return update:output(util:finalize($result))
    };
    
    (:~
     : Finalizes the result of an evaluated query.
     : @param  $result   query result
     : @return empty sequence
     :)
    declare %private function util:finalize(
      $result   as item()*
    ) as xs:string {
      (: serialize more characters than requested, because limit represents number of bytes :)
      let $limit := options:get($options:MAXCHARS)
      let $string := serialize($result, map { 'limit': $limit * 2 + 1, 'method': 'basex' })
      return util:chop($string, $limit)
    };
    
    (:~
     : Returns the options for evaluating a query.
     : @return options
     :)
    declare %private function util:query-options() as map(*) {
      map {
        'timeout'   : options:get($options:TIMEOUT),
        'memory'    : options:get($options:MEMORY),
        'permission': options:get($options:PERMISSION),
        'base-uri'  : config:directory() || '/' || config:query()
      }
    };
    
    (:~
     : Returns the index of the first result to generate.
     : @param  $page  current page
     : @param  $sort  sort key
     : @return last result
     :)
    declare function util:start(
      $page  as xs:integer,
      $sort  as xs:string
    ) as xs:integer {
      if($page and not($sort)) then (
        ($page - 1) * options:get($options:MAXROWS) + 1
      ) else (
        1
      )
    };
    
    (:~
     : Returns the index of the last result to generate.
     : @param  $page  current page
     : @param  $sort  sort key
     : @return last result
     :)
    declare function util:end(
      $page  as xs:integer,
      $sort  as xs:string
    ) as xs:integer {
      if($page and not($sort)) then (
        $page * options:get($options:MAXROWS)
      ) else (
        999999999
      )
    };
    
    (:~
     : Chops a string result to the maximum number of allowed characters.
     : @param  $string  string
     : @param  $max     maximum number of characters
     : @return string
     :)
    declare function util:chop(
      $string  as xs:string,
      $max     as xs:integer
    ) as xs:string {
      if(string-length($string) > $max) then (
        substring($string, 1, $max) || '...'
      ) else (
        $string
      )
    };
    
    (:~
     : Joins sequence entries.
     : @param  $items  items
     : @param  $sep    separator
     : @return result
     :)
    declare function util:item-join(
      $items  as item()*,
      $sep    as item()
    ) as item()* {
      for $item at $pos in $items
      return ($sep[$pos > 1], $item)
    };
    
    (:~
     : Returns a count info for the specified items.
     : @param  $items   items
     : @param  $name    name of item (singular form)
     : @param  $action  action label (past tense)
     : @return result
     :)
    declare function util:info(
      $items   as item()*,
      $name    as xs:string,
      $action  as xs:string
    ) as xs:string {
      let $count := count($items)
      return $count || ' ' || $name || (if($count > 1) then 's were ' else ' was ') || $action || '.'
    };
    
    (:~
     : Capitalizes a string.
     : @param  $string  string
     : @return capitalized string
     :)
    declare function util:capitalize(
      $string  as xs:string
    ) as xs:string {
      upper-case(substring($string, 1, 1)) || substring($string, 2)
    };
    
    (:~
     : Convenience function for redirecting to another page from update operations.
     : @param  $url     URL
     : @param  $params  query parameters
     :)
    declare %updating function util:redirect(
      $url     as xs:string,
      $params  as map(*)
    ) as empty-sequence() {
      update:output(web:redirect($url, $params))
    };