dba/databases  library module
URA

Summary

Put resources.
Authors
  • Christian Grün, BaseX Team 2005-23, BSD License
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
dba/databases
imports
dba/html

Variables

3.1 $dba:CAT

Summary
Top category
Type
Source ( 1 lines)
variable $dba:CAT := 'databases'

3.2 $dba:SUB

Summary
Sub category
Type
Source ( 1 lines)
variable $dba:SUB := 'database'

Functions

4.1 dba:db-put

Arities: #5RA

Summary
Form for putting a new resource.
Signatures
dba:db-put ( $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
Invoked by 0 functions from 0 modules
    Annotations (8)
    %rest:GET()
    %rest:path('/dba/db-put')
    %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 ( 53 lines)
    function dba:db-put(
      $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 ''
      return html:wrap(map { 'header': ($dba:CAT, $name), 'error': $error },
        <tr>
          <td>
            <form action='db-put' 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-put', 'Put')
              }</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'/>
                  }</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('stripws', 'Strip whitespaces', $opts),
                    html:option('xinclude', 'Use XInclude', $opts)
                  }</td>
                </tr>
              </table>
            </form>
          </td>
        </tr>
      )
    }

    4.2 dba:db-put-post

    Arities: #5UR

    Summary
    Puts a resource.
    Signatures
    dba:db-put-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
    Invoked by 0 functions from 0 modules
      Annotations (8)
      %updating()
      %rest:POST()
      %rest:path('/dba/db-put')
      %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 ( 31 lines)
      function dba:db-put-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 (
            let $input := $file($key)
            return if($binary) then (
              db:put-binary($name, $input, $path)
            ) else (
              db:put($name, fetch:binary-doc($input), $path, map:merge(
                ('intparse', 'dtd', 'stripns', 'stripws', 'xinclude') ! map:entry(., $opts = .))
              )
            ),
            util:redirect($dba:SUB,
              map { 'name': $name, 'path': $path, 'info': 'Resource was put.' }
            )
          )
        } catch * {
          util:redirect('db-put', map {
            'name': $name, 'opts': $opts, 'path': $path, 'binary': $binary, 'error': $err:description
          })
        }
      }

      Namespaces

      The following namespaces are defined:

      PrefixUri
      dbhttp://basex.org/modules/db
      dbadba/databases 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
      errhttp://www.w3.org/2005/xqt-errors
      fetchhttp://basex.org/modules/fetch
      htmldba/html
      maphttp://www.w3.org/2005/xpath-functions/map
      outputhttp://www.w3.org/2010/xslt-xquery-serialization
      resthttp://exquery.org/ns/restxq
      utildba/util
      xshttp://www.w3.org/2001/XMLSchema

      6 RestXQ

      Paths defined 2.

      PathMethodFunction
      /dba/db-putGETdba:db-put#5
      /dba/db-putPOSTdba:db-put-post#5

      Source Code

      (:~
       : Put resources.
       :
       : @author Christian Grün, BaseX Team 2005-23, 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 putting 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-put')
        %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-put(
        $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 ''
        return html:wrap(map { 'header': ($dba:CAT, $name), 'error': $error },
          <tr>
            <td>
              <form action='db-put' 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-put', 'Put')
                }</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'/>
                    }</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('stripws', 'Strip whitespaces', $opts),
                      html:option('xinclude', 'Use XInclude', $opts)
                    }</td>
                  </tr>
                </table>
              </form>
            </td>
          </tr>
        )
      };
      
      (:~
       : Puts 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-put')
        %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-put-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 (
            let $input := $file($key)
            return if($binary) then (
              db:put-binary($name, $input, $path)
            ) else (
              db:put($name, fetch:binary-doc($input), $path, map:merge(
                ('intparse', 'dtd', 'stripns', 'stripws', 'xinclude') ! map:entry(., $opts = .))
              )
            ),
            util:redirect($dba:SUB,
              map { 'name': $name, 'path': $path, 'info': 'Resource was put.' }
            )
          )
        } catch * {
          util:redirect('db-put', map {
            'name': $name, 'opts': $opts, 'path': $path, 'binary': $binary, 'error': $err:description
          })
        }
      };