dba/util
library moduleUP
Summary
Utility functions.
- Tags
Author: Christian Grün, BaseX Team 2005-21, BSD License
__source : lib/util.xqm
Imports
This module is imported by 30 modules. It imports 2 modules.
dba/queries 2 3 4 5
imports
→this
imports
→Variables
None
Functions
4.1 util:capitalize
Arities: util:capitalize#1
util:capitalize
(
$string
as
xs:string
)
as
xs:string
- string
as
xs:string
string
xs:string
capitalized 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
util:chop
(
$string
as
xs:string
, $max
as
xs:integer
)
as
xs:string
- string
as
xs:string
string - max
as
xs:integer
maximum number of characters
xs:string
string
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
util:end
(
$page
as
xs:integer
, $sort
as
xs:string
)
as
xs:integer
- page
as
xs:integer
current page - sort
as
xs:string
sort key
xs:integer
last result
Invokes 2 functions from 2 modules
- options:get#1
- {http://www.w3.org/2005/xpath-functions}not#1
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
util:finalize
(
$result
as
item()*
)
as
xs:string
- result
as
item()*
query result
xs:string
empty sequence
Invokes 3 functions from 3 modules
- options:get#1
- util:chop#2
- {http://www.w3.org/2005/xpath-functions}serialize#2
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
util:info
(
$items
as
item()*
, $name
as
xs:string
, $action
as
xs:string
)
as
xs:string
- items
as
item()*
items - name
as
xs:string
name of item (singular form) - action
as
xs:string
action label (past tense)
xs:string
result
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
util:item-join
(
$items
as
item()*
, $sep
as
item()
)
as
item() *
- items
as
item()*
items - sep
as
item()
separator
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
util:query
(
$query
as
xs:string?
, $context
as
item()*
)
as
xs:string
- query
as
xs:string?
query string - context
as
item()*
initial context value
xs:string
serialized result of query
Invokes 3 functions from 2 modules
- util:finalize#1
- util:query-options#0
- {http://basex.org/modules/xquery}eval#3
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
util:query-options
(
)
as
map(*)
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
util:redirect
(
$url
as
xs:string
, $params
as
map(*)
)
as
empty-sequence()
- url
as
xs:string
URL - params
as
map(*)
query parameters
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
util:start
(
$page
as
xs:integer
, $sort
as
xs:string
)
as
xs:integer
- page
as
xs:integer
current page - sort
as
xs:string
sort key
xs:integer
last result
Invokes 2 functions from 2 modules
- options:get#1
- {http://www.w3.org/2005/xpath-functions}not#1
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
util:update-query
(
$query
as
xs:string?
)
as
empty-sequence()
- query
as
xs:string?
query string
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:
Prefix | Uri |
---|---|
ann | http://www.w3.org/2012/xquery |
config | dba/config |
options | dba/options |
util | dba/util |
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))
};