dba/databases  library module
URA

Summary

Add resources.
Tags

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

__source : databases/resources/db-add.xqm

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

Imports

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

(None)
imports
this
imports

Variables

3.1 $dba:CAT

Summary
Top category
Type
xs:string

3.2 $dba:SUB

Summary
Sub category
Type
xs:string

Functions

4.1 dba:db-add

Arities: dba:db-add#5RA

Summary
Form for adding a new resource.
Signature
dba:db-add ( $name as xs:string, $opts as xs:string*, $path as xs:string?, $binary as xs:string?, $error as xs:string? )  as element(html)
Parameters
  • name as xs:string entered name
  • opts as xs:string* chosen parsing options
  • path as xs:string? database path
  • binary as xs:string? store as binary
  • error as xs:string? error string
Return
  • element(html)page
Invokes 7 functions from 1 modules
Invoked by 0 functions from 0 modules
    Annotations
    %rest:GET()
    %rest:path('/dba/db-add')
    %rest:query-param('name','{$name}')
    %rest:query-param('opts','{$opts}')
    %rest:query-param('path','{$path}')
    %rest:query-param('binary','{$binary}')
    %rest:query-param('error','{$error}')
    %output:method('html')
    Source ( 54 lines)
    function dba:db-add(
      $name    as xs:string,
      $opts    as xs:string*,
      $path    as xs:string?,
      $binary  as xs:string?,
      $error   as xs:string?
    ) as element(html) {
      let $opts := if($opts = 'x') then $opts else 'chop'
      return html:wrap(map { 'header': ($dba:CAT, $name), 'error': $error },
        <tr>
          <td>
            <form action='db-add' method='post' enctype='multipart/form-data' autocomplete='off'>
              <h2>{
                html:link('Databases', $dba:CAT), ' » ',
                html:link($name, $dba:SUB, map { 'name': $name }), ' » ',
                html:button('db-add', 'Add')
              }</h2>
              <!-- dummy value; prevents reset of options when nothing is selected -->
              <input type='hidden' name='opts' value='x'/>
              <input type='hidden' name='name' value='{ $name }'/>
              <table>
                <tr>
                  <td>Input:</td>
                  <td>{
                    <input type='file' name='file' id='file'/>,
                    html:focus('file')
                  }</td>
                </tr>
                <tr>
                  <td>Database Path:</td>
                  <td>
                    <input type='text' name='path' value='{ $path }'/>
                  </td>
                </tr>
                <tr>
                  <td>Binary Storage:</td>
                  <td>{ html:checkbox('binary', 'true', $binary = 'true', '') }</td>
                </tr>
                <tr>
                  <td colspan='2'>{
                    <h3>Parsing Options</h3>,
                    html:option('intparse', 'Use internal XML parser', $opts),
                    html:option('dtd', 'Parse DTDs and entities', $opts),
                    html:option('stripns', 'Strip namespaces', $opts),
                    html:option('chop', 'Chop whitespaces', $opts),
                    html:option('xinclude', 'Use XInclude', $opts)
                  }</td>
                </tr>
              </table>
            </form>
          </td>
        </tr>
      )
    }

    4.2 dba:db-add-post

    Arities: dba:db-add-post#5UR

    Summary
    Adds a resource.
    Signature
    dba:db-add-post ( $name as xs:string, $opts as xs:string*, $path as xs:string, $file as map(*), $binary as xs:string? )  as empty-sequence()
    Parameters
    • name as xs:string database
    • opts as xs:string* chosen parsing options
    • path as xs:string database path
    • file as map(*) uploaded file
    • binary as xs:string? store as binary file
    Return
    • empty-sequence()redirection
    Invokes 11 functions from 5 modules
    • util:redirect#2
    • {http://basex.org/modules/db}add#4
    • {http://basex.org/modules/db}exists#2
    • {http://basex.org/modules/db}store#3
    • {http://basex.org/modules/fetch}xml-binary#1
    • {http://www.w3.org/2005/xpath-functions/map}entry#2
    • {http://www.w3.org/2005/xpath-functions/map}keys#1
    • {http://www.w3.org/2005/xpath-functions/map}merge#1
    • {http://www.w3.org/2005/xpath-functions}ends-with#2
    • {http://www.w3.org/2005/xpath-functions}error#2
    • {http://www.w3.org/2005/xpath-functions}not#1
    Invoked by 0 functions from 0 modules
      Annotations
      %updating()
      %rest:POST()
      %rest:path('/dba/db-add')
      %rest:form-param('name','{$name}')
      %rest:form-param('opts','{$opts}')
      %rest:form-param('path','{$path}')
      %rest:form-param('file','{$file}')
      %rest:form-param('binary','{$binary}')
      Source ( 33 lines)
      function dba:db-add-post(
        $name    as xs:string,
        $opts    as xs:string*,
        $path    as xs:string,
        $file    as map(*),
        $binary  as xs:string?
      ) as empty-sequence() {
        try {
          let $key := map:keys($file)
          let $path := if(not($path) or ends-with($path, '/')) then ($path || $key) else $path
          return if($key = '') then (
            error((), 'No input specified.')
          ) else if(db:exists($name, $path)) then (
            error((), 'Resource already exists: ' || $path)
          ) else (
            let $input := $file($key)
            return if($binary) then (
              db:store($name, $path, $input)
            ) else (
              db:add($name, fetch:xml-binary($input), $path, map:merge(
                ('intparse', 'dtd', 'stripns', 'chop', 'xinclude') ! map:entry(., $opts = .))
              )
            ),
            util:redirect($dba:SUB,
              map { 'name': $name, 'path': $path, 'info': 'Resource was added.' }
            )
          )
        } catch * {
          util:redirect('db-add', map {
            'name': $name, 'opts': $opts, 'path': $path, 'binary': $binary, 'error': $err:description
          })
        }
      }

      Namespaces

      The following namespaces are defined:

      PrefixUri
      annhttp://www.w3.org/2012/xquery
      dbadba/databases 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
      htmldba/html
      outputhttp://www.w3.org/2010/xslt-xquery-serialization
      resthttp://exquery.org/ns/restxq
      utildba/util

      6 RestXQ

      Paths defined 2.

      PathMethodFunction
      /dba/db-addGETdba:db-add#5
      /dba/db-addPOSTdba:db-add-post#5

      Source Code

      (:~
       : Add resources.
       :
       : @author Christian Grün, BaseX Team 2005-21, BSD License
       :)
      module namespace dba = 'dba/databases';
      
      import module namespace html = 'dba/html' at '../../lib/html.xqm';
      import module namespace util = 'dba/util' at '../../lib/util.xqm';
      
      (:~ Top category :)
      declare variable $dba:CAT := 'databases';
      (:~ Sub category :)
      declare variable $dba:SUB := 'database';
      
      (:~
       : Form for adding a new resource.
       : @param  $name    entered name
       : @param  $opts    chosen parsing options
       : @param  $path    database path
       : @param  $binary  store as binary
       : @param  $error   error string
       : @return page
       :)
      declare
        %rest:GET
        %rest:path('/dba/db-add')
        %rest:query-param('name',   '{$name}')
        %rest:query-param('opts',   '{$opts}')
        %rest:query-param('path',   '{$path}')
        %rest:query-param('binary', '{$binary}')
        %rest:query-param('error',  '{$error}')
        %output:method('html')
      function dba:db-add(
        $name    as xs:string,
        $opts    as xs:string*,
        $path    as xs:string?,
        $binary  as xs:string?,
        $error   as xs:string?
      ) as element(html) {
        let $opts := if($opts = 'x') then $opts else 'chop'
        return html:wrap(map { 'header': ($dba:CAT, $name), 'error': $error },
          <tr>
            <td>
              <form action='db-add' method='post' enctype='multipart/form-data' autocomplete='off'>
                <h2>{
                  html:link('Databases', $dba:CAT), ' » ',
                  html:link($name, $dba:SUB, map { 'name': $name }), ' » ',
                  html:button('db-add', 'Add')
                }</h2>
                <!-- dummy value; prevents reset of options when nothing is selected -->
                <input type='hidden' name='opts' value='x'/>
                <input type='hidden' name='name' value='{ $name }'/>
                <table>
                  <tr>
                    <td>Input:</td>
                    <td>{
                      <input type='file' name='file' id='file'/>,
                      html:focus('file')
                    }</td>
                  </tr>
                  <tr>
                    <td>Database Path:</td>
                    <td>
                      <input type='text' name='path' value='{ $path }'/>
                    </td>
                  </tr>
                  <tr>
                    <td>Binary Storage:</td>
                    <td>{ html:checkbox('binary', 'true', $binary = 'true', '') }</td>
                  </tr>
                  <tr>
                    <td colspan='2'>{
                      <h3>Parsing Options</h3>,
                      html:option('intparse', 'Use internal XML parser', $opts),
                      html:option('dtd', 'Parse DTDs and entities', $opts),
                      html:option('stripns', 'Strip namespaces', $opts),
                      html:option('chop', 'Chop whitespaces', $opts),
                      html:option('xinclude', 'Use XInclude', $opts)
                    }</td>
                  </tr>
                </table>
              </form>
            </td>
          </tr>
        )
      };
      
      (:~
       : Adds a resource.
       : @param  $name    database
       : @param  $opts    chosen parsing options
       : @param  $path    database path
       : @param  $file    uploaded file
       : @param  $binary  store as binary file
       : @return redirection
       :)
      declare
        %updating
        %rest:POST
        %rest:path('/dba/db-add')
        %rest:form-param('name',   '{$name}')
        %rest:form-param('opts',   '{$opts}')
        %rest:form-param('path',   '{$path}')
        %rest:form-param('file',   '{$file}')
        %rest:form-param('binary', '{$binary}')
      function dba:db-add-post(
        $name    as xs:string,
        $opts    as xs:string*,
        $path    as xs:string,
        $file    as map(*),
        $binary  as xs:string?
      ) as empty-sequence() {
        try {
          let $key := map:keys($file)
          let $path := if(not($path) or ends-with($path, '/')) then ($path || $key) else $path
          return if($key = '') then (
            error((), 'No input specified.')
          ) else if(db:exists($name, $path)) then (
            error((), 'Resource already exists: ' || $path)
          ) else (
            let $input := $file($key)
            return if($binary) then (
              db:store($name, $path, $input)
            ) else (
              db:add($name, fetch:xml-binary($input), $path, map:merge(
                ('intparse', 'dtd', 'stripns', 'chop', 'xinclude') ! map:entry(., $opts = .))
              )
            ),
            util:redirect($dba:SUB,
              map { 'name': $name, 'path': $path, 'info': 'Resource was added.' }
            )
          )
        } catch * {
          util:redirect('db-add', map {
            'name': $name, 'opts': $opts, 'path': $path, 'binary': $binary, 'error': $err:description
          })
        }
      };