dba/databases 
                    library moduleURA
Summary
Put resources.
- Authors
 - Christian Grün, BaseX Team 2005-23, BSD License
 
Imports
This module is imported by 0 modules. It imports 2 modules.
Variables
Functions
4.1 dba:db-put
Arities: #5RA
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)- name
asxs:stringentered name - opts
asxs:string*chosen parsing options - path
asxs:string?database path - binary
asxs:string?store as binary - error
asxs:string?error string 
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
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- name
asxs:stringdatabase - opts
asxs:string*chosen parsing options - path
asxs:stringdatabase path - file
asmap(*)uploaded file - binary
asxs:string?store as binary file 
empty-sequenceredirection
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:
| Prefix | Uri | 
|---|---|
| db | http://basex.org/modules/db | 
| dba | dba/databases 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | 
| err | http://www.w3.org/2005/xqt-errors | 
| fetch | http://basex.org/modules/fetch | 
| html | dba/html | 
| map | http://www.w3.org/2005/xpath-functions/map | 
| output | http://www.w3.org/2010/xslt-xquery-serialization | 
| rest | http://exquery.org/ns/restxq | 
| util | dba/util | 
| xs | http://www.w3.org/2001/XMLSchema | 
6 RestXQ
Paths defined 2.
| Path | Method | Function | 
|---|---|---|
| /dba/db-put | GET | dba:db-put#5 | 
| /dba/db-put | POST | dba: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
    })
  }
};