dba/databases  library module
URA

Summary

Copy database.
Tags

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

__source : databases/db-copy.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-copy

Arities: dba:db-copy#2URdba:db-copy#3RA

Summary
Form for copying a database.
Signature
dba:db-copy ( $name as xs:string, $newname as xs:string )  as empty-sequence()
dba:db-copy ( $name as xs:string, $newname as xs:string?, $error as xs:string? )  as element(html)
Parameters
  • name as xs:string name of database
  • newname as xs:string new name
Return
  • empty-sequence()redirection
Invokes 10 functions from 4 modules
Invoked by 0 functions from 0 modules
    Annotations
    %rest:GET()
    %rest:path('/dba/db-copy')
    %rest:query-param('name','{$name}')
    %rest:query-param('newname','{$newname}')
    %rest:query-param('error','{$error}')
    %output:method('html')
    Annotations
    %updating()
    %rest:POST()
    %rest:path('/dba/db-copy')
    %rest:query-param('name','{$name}')
    %rest:query-param('newname','{$newname}')
    Source ( 45 lines)
    function dba:db-copy(
      $name     as xs:string,
      $newname  as xs:string
    ) as empty-sequence() {
      try {
        if(db:exists($newname)) then (
          error((), 'Database already exists.')
        ) else (
          db:copy($name, $newname)
        ),
        util:redirect($dba:SUB, map { 'name': $newname, 'info': 'Database was copied.' })
      } catch * {
        util:redirect('db-copy', map { 'name': $name, 'newname': $newname, 'error': $err:description })
      }
    }
    function dba:db-copy(
      $name     as xs:string,
      $newname  as xs:string?,
      $error    as xs:string?
    ) as element(html) {
      html:wrap(map { 'header': ($dba:CAT, $name), 'error': $error },
        <tr>
          <td>
            <form action='db-copy' method='post' autocomplete='off'>
              <input type='hidden' name='name' value='{ $name }'/>
              <h2>{
                html:link('Databases', $dba:CAT), ' » ',
                html:link($name, $dba:SUB, map { 'name': $name }), ' » ',
                html:button('db-copy', 'Copy')
              }</h2>
              <table>
                <tr>
                  <td>New name:</td>
                  <td>
                    <input type='text' name='newname' value='{ head(($newname, $name)) }' id='newname'/>
                    { html:focus('newname') }
                    <div class='small'/>
                  </td>
                </tr>
              </table>
            </form>
          </td>
        </tr>
      )
    }

    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-copyGETdba:db-copy#3
    /dba/db-copyPOSTdba:db-copy#2

    Source Code

    (:~
     : Copy database.
     :
     : @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 copying a database.
     : @param  $name     name of database
     : @param  $newname  new name
     : @param  $error    error string
     : @return page
     :)
    declare
      %rest:GET
      %rest:path('/dba/db-copy')
      %rest:query-param('name',    '{$name}')
      %rest:query-param('newname', '{$newname}')
      %rest:query-param('error',   '{$error}')
      %output:method('html')
    function dba:db-copy(
      $name     as xs:string,
      $newname  as xs:string?,
      $error    as xs:string?
    ) as element(html) {
      html:wrap(map { 'header': ($dba:CAT, $name), 'error': $error },
        <tr>
          <td>
            <form action='db-copy' method='post' autocomplete='off'>
              <input type='hidden' name='name' value='{ $name }'/>
              <h2>{
                html:link('Databases', $dba:CAT), ' » ',
                html:link($name, $dba:SUB, map { 'name': $name }), ' » ',
                html:button('db-copy', 'Copy')
              }</h2>
              <table>
                <tr>
                  <td>New name:</td>
                  <td>
                    <input type='text' name='newname' value='{ head(($newname, $name)) }' id='newname'/>
                    { html:focus('newname') }
                    <div class='small'/>
                  </td>
                </tr>
              </table>
            </form>
          </td>
        </tr>
      )
    };
    
    (:~
     : Copies a database.
     : @param  $name     name of database
     : @param  $newname  new name
     : @return redirection
     :)
    declare
      %updating
      %rest:POST
      %rest:path('/dba/db-copy')
      %rest:query-param('name',    '{$name}')
      %rest:query-param('newname', '{$newname}')
    function dba:db-copy(
      $name     as xs:string,
      $newname  as xs:string
    ) as empty-sequence() {
      try {
        if(db:exists($newname)) then (
          error((), 'Database already exists.')
        ) else (
          db:copy($name, $newname)
        ),
        util:redirect($dba:SUB, map { 'name': $newname, 'info': 'Database was copied.' })
      } catch * {
        util:redirect('db-copy', map { 'name': $name, 'newname': $newname, 'error': $err:description })
      }
    };