dba/databases  library module
RA

Summary

Database main page.
Tags

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

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

Arities: dba:database#6RA

Summary
Manages a single database.
Signature
dba:database ( $name as xs:string, $resource as xs:string?, $sort as xs:string, $page as xs:integer, $info as xs:string?, $error as xs:string? )  as element()
Parameters
  • name as xs:string database
  • resource as xs:string? resource
  • sort as xs:string table sort key
  • page as xs:integer current page
  • info as xs:string? info string
  • error as xs:string? error string
Return
  • element()page
Invokes 25 functions from 5 modules
Invoked by 0 functions from 0 modules
    Annotations
    %rest:GET()
    %rest:path('/dba/database')
    %rest:query-param('name','{$name}','')
    %rest:query-param('resource','{$resource}')
    %rest:query-param('sort','{$sort}','')
    %rest:query-param('page','{$page}',1)
    %rest:query-param('info','{$info}')
    %rest:query-param('error','{$error}')
    %output:method('html')
    Source ( 118 lines)
    function dba:database(
      $name      as xs:string,
      $resource  as xs:string?,
      $sort      as xs:string,
      $page      as xs:integer,
      $info      as xs:string?,
      $error     as xs:string?
    ) as element() {
      if(not($name)) then web:redirect('databases') else
    
      let $db-exists := db:exists($name)
      return html:wrap(
        map {
          'header': ($dba:CAT, $name), 'info': $info, 'error': $error,
          'css': 'codemirror/lib/codemirror.css',
          'scripts': ('codemirror/lib/codemirror.js', 'codemirror/mode/xml/xml.js')
        },
        <tr>
          <td>
            <form action='{ $dba:SUB }' method='post' id='{ $dba:SUB }' class='update'>
              <input type='hidden' name='name' value='{ $name }' id='name'/>
              <h2>{
                html:link('Databases', $dba:CAT), ' » ',
                $name ! (if(empty($resource)) then . else html:link(., $dba:SUB, map { 'name': . } ))
              }</h2>
              {
                if($db-exists) then (
                  let $headers := (
                    map { 'key': 'resource' , 'label': 'Name' },
                    map { 'key': 'type' , 'label': 'Content type' },
                    map { 'key': 'raw' , 'label': 'Raw' },
                    map { 'key': 'size' , 'label': 'Size', 'type': 'number', 'order': 'desc' }
                  )
                  let $entries :=
                    let $start := util:start($page, $sort)
                    let $end := util:end($page, $sort)
                    for $res in db:list-details($name)[position() = $start to $end]
                    return map {
                      'resource': $res,
                      'type': $res/@content-type,
                      'raw': if($res/@raw = 'true') then '✓' else '–',
                      'size': $res/@size
                    }
                  let $buttons := (
                    html:button('db-add', 'Add…'),
                    html:button('db-delete', 'Delete', true()),
                    html:button('db-copy', 'Copy…', false()),
                    html:button('db-alter', 'Rename…', false()),
                    html:button('db-optimize', 'Optimize…', false(), map { 'class': 'global' })
                  )
                  let $params := map { 'name': $name }
                  let $options := map {
                    'sort': $sort,
                    'link': $dba:SUB,
                    'page': $page,
                    'count': count(db:list($name))
                  }
                  return html:table($headers, $entries, $buttons, $params, $options)
                ) else ()
              }
            </form>
            <form action='{ $dba:SUB }' method='post' class='update'>
              <input type='hidden' name='name' value='{ $name }'/>
              <h3>Backups</h3>
              {
                let $headers := (
                  map { 'key': 'backup', 'label': 'Name', 'order': 'desc' },
                  map { 'key': 'size', 'label': 'Size', 'type': 'bytes' },
                  map { 'key': 'action', 'label': 'Action', 'type': 'dynamic' }
                )
                let $entries :=
                  for $backup in db:backups($name)
                  order by $backup descending
                  return map {
                    'backup': $backup,
                    'size': $backup/@size,
                    'action': function() {
                      html:link('Download', 'backup/' || encode-for-uri($backup) || '.zip')
                    }
                  }
                let $buttons := (
                  html:button('backup-create', 'Create', false(), map { 'class': 'global' }) update (
                    if($db-exists) then () else insert node attribute disabled { '' } into .
                  ),
                  html:button('backup-restore', 'Restore', true()),
                  html:button('backup-drop', 'Drop', true())
                )
                let $params := map { 'name': $name }
                return html:table($headers, $entries, $buttons, $params, map { })
              }
            </form>
          </td>
          <td class='vertical'/>
          <td>{
            if($resource) then (
              <h3>{ $resource }</h3>,
              <form action='resource' method='post' id='resources'>
                <input type='hidden' name='name' value='{ $name }'/>
                <input type='hidden' name='resource' value='{ $resource }' id='resource'/>
                {
                  html:button('db-rename', 'Rename…'), ' ',
                  html:button('db-download', 'Download'), ' ',
                  html:button('db-replace', 'Replace…')
                }
              </form>,
              <h4>Enter your query…</h4>,
              <input style='width:100%' name='input' id='input' onkeyup='queryResource(false)'/>,
              <div class='small'/>,
              <textarea name='output' id='output' readonly='' spellcheck='false'/>,
              html:focus('input'),
              html:js('loadCodeMirror("xml", false); queryResource(true);')
            ) else if($db-exists) then (
              html:properties(db:info($name))
            ) else ()
          }</td>
        </tr>
      )
    }

    4.2 dba:database-redirect

    Arities: dba:database-redirect#4R

    Summary
    Redirects to the specified action.
    Signature
    dba:database-redirect ( $action as xs:string, $name as xs:string, $resources as xs:string*, $backups as xs:string* )  as element(rest:response)
    Parameters
    • action as xs:string action to perform
    • name as xs:string database
    • resources as xs:string* resources
    • backups as xs:string* backups
    Return
    • element(rest:response)redirection
    Invokes 1 functions from 1 modules
    • {http://basex.org/modules/web}redirect#2
    Invoked by 0 functions from 0 modules
      Annotations
      %rest:POST()
      %rest:path('/dba/database')
      %rest:form-param('action','{$action}')
      %rest:form-param('name','{$name}')
      %rest:form-param('resource','{$resources}')
      %rest:form-param('backup','{$backups}')
      Source ( 8 lines)
      function dba:database-redirect(
        $action     as xs:string,
        $name       as xs:string,
        $resources  as xs:string*,
        $backups    as xs:string*
      ) as element(rest:response) {
        web:redirect($action, map { 'name': $name, 'resource': $resources, 'backup': $backups })
      }

      Namespaces

      The following namespaces are defined:

      PrefixUri
      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/databaseGETdba:database#6
      /dba/databasePOSTdba:database-redirect#4

      Source Code

      (:~
       : Database main page.
       :
       : @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';
      
      (:~
       : Manages a single database.
       : @param  $name      database
       : @param  $resource  resource
       : @param  $sort      table sort key
       : @param  $page      current page
       : @param  $info      info string
       : @param  $error     error string
       : @return page
       :)
      declare
        %rest:GET
        %rest:path('/dba/database')
        %rest:query-param('name',     '{$name}', '')
        %rest:query-param('resource', '{$resource}')
        %rest:query-param('sort',     '{$sort}', '')
        %rest:query-param('page',     '{$page}', 1)
        %rest:query-param('info',     '{$info}')
        %rest:query-param('error',    '{$error}')
        %output:method('html')
      function dba:database(
        $name      as xs:string,
        $resource  as xs:string?,
        $sort      as xs:string,
        $page      as xs:integer,
        $info      as xs:string?,
        $error     as xs:string?
      ) as element() {
        if(not($name)) then web:redirect('databases') else
      
        let $db-exists := db:exists($name)
        return html:wrap(
          map {
            'header': ($dba:CAT, $name), 'info': $info, 'error': $error,
            'css': 'codemirror/lib/codemirror.css',
            'scripts': ('codemirror/lib/codemirror.js', 'codemirror/mode/xml/xml.js')
          },
          <tr>
            <td>
              <form action='{ $dba:SUB }' method='post' id='{ $dba:SUB }' class='update'>
                <input type='hidden' name='name' value='{ $name }' id='name'/>
                <h2>{
                  html:link('Databases', $dba:CAT), ' » ',
                  $name ! (if(empty($resource)) then . else html:link(., $dba:SUB, map { 'name': . } ))
                }</h2>
                {
                  if($db-exists) then (
                    let $headers := (
                      map { 'key': 'resource' , 'label': 'Name' },
                      map { 'key': 'type' , 'label': 'Content type' },
                      map { 'key': 'raw' , 'label': 'Raw' },
                      map { 'key': 'size' , 'label': 'Size', 'type': 'number', 'order': 'desc' }
                    )
                    let $entries :=
                      let $start := util:start($page, $sort)
                      let $end := util:end($page, $sort)
                      for $res in db:list-details($name)[position() = $start to $end]
                      return map {
                        'resource': $res,
                        'type': $res/@content-type,
                        'raw': if($res/@raw = 'true') then '✓' else '–',
                        'size': $res/@size
                      }
                    let $buttons := (
                      html:button('db-add', 'Add…'),
                      html:button('db-delete', 'Delete', true()),
                      html:button('db-copy', 'Copy…', false()),
                      html:button('db-alter', 'Rename…', false()),
                      html:button('db-optimize', 'Optimize…', false(), map { 'class': 'global' })
                    )
                    let $params := map { 'name': $name }
                    let $options := map {
                      'sort': $sort,
                      'link': $dba:SUB,
                      'page': $page,
                      'count': count(db:list($name))
                    }
                    return html:table($headers, $entries, $buttons, $params, $options)
                  ) else ()
                }
              </form>
              <form action='{ $dba:SUB }' method='post' class='update'>
                <input type='hidden' name='name' value='{ $name }'/>
                <h3>Backups</h3>
                {
                  let $headers := (
                    map { 'key': 'backup', 'label': 'Name', 'order': 'desc' },
                    map { 'key': 'size', 'label': 'Size', 'type': 'bytes' },
                    map { 'key': 'action', 'label': 'Action', 'type': 'dynamic' }
                  )
                  let $entries :=
                    for $backup in db:backups($name)
                    order by $backup descending
                    return map {
                      'backup': $backup,
                      'size': $backup/@size,
                      'action': function() {
                        html:link('Download', 'backup/' || encode-for-uri($backup) || '.zip')
                      }
                    }
                  let $buttons := (
                    html:button('backup-create', 'Create', false(), map { 'class': 'global' }) update (
                      if($db-exists) then () else insert node attribute disabled { '' } into .
                    ),
                    html:button('backup-restore', 'Restore', true()),
                    html:button('backup-drop', 'Drop', true())
                  )
                  let $params := map { 'name': $name }
                  return html:table($headers, $entries, $buttons, $params, map { })
                }
              </form>
            </td>
            <td class='vertical'/>
            <td>{
              if($resource) then (
                <h3>{ $resource }</h3>,
                <form action='resource' method='post' id='resources'>
                  <input type='hidden' name='name' value='{ $name }'/>
                  <input type='hidden' name='resource' value='{ $resource }' id='resource'/>
                  {
                    html:button('db-rename', 'Rename…'), ' ',
                    html:button('db-download', 'Download'), ' ',
                    html:button('db-replace', 'Replace…')
                  }
                </form>,
                <h4>Enter your query…</h4>,
                <input style='width:100%' name='input' id='input' onkeyup='queryResource(false)'/>,
                <div class='small'/>,
                <textarea name='output' id='output' readonly='' spellcheck='false'/>,
                html:focus('input'),
                html:js('loadCodeMirror("xml", false); queryResource(true);')
              ) else if($db-exists) then (
                html:properties(db:info($name))
              ) else ()
            }</td>
          </tr>
        )
      };
      
      (:~
       : Redirects to the specified action.
       : @param  $action     action to perform
       : @param  $name       database
       : @param  $resources  resources
       : @param  $backups    backups
       : @return redirection
       :)
      declare
        %rest:POST
        %rest:path('/dba/database')
        %rest:form-param('action',   '{$action}')
        %rest:form-param('name',     '{$name}')
        %rest:form-param('resource', '{$resources}')
        %rest:form-param('backup',   '{$backups}')
      function dba:database-redirect(
        $action     as xs:string,
        $name       as xs:string,
        $resources  as xs:string*,
        $backups    as xs:string*
      ) as element(rest:response) {
        web:redirect($action, map { 'name': $name, 'resource': $resources, 'backup': $backups })
      };