dba/databases  library module
URA

Summary

Create new database.
Tags

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

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

Arities: dba:db-create#3URdba:db-create#4RA

Summary
Form for creating a new database.
Signature
dba:db-create ( $name as xs:string, $opts as xs:string*, $lang as xs:string? )  as empty-sequence()
dba:db-create ( $name as xs:string?, $opts as xs:string*, $lang as xs:string?, $error as xs:string? )  as element(html)
Parameters
  • name as xs:string database
  • opts as xs:string* database options
  • lang as xs:string? language
Return
  • empty-sequence()redirection
Invokes 11 functions from 5 modules
Invoked by 0 functions from 0 modules
    Annotations
    %rest:GET()
    %rest:path('/dba/db-create')
    %rest:query-param('name','{$name}')
    %rest:query-param('opts','{$opts}')
    %rest:query-param('lang','{$lang}','en')
    %rest:query-param('error','{$error}')
    %output:method('html')
    Annotations
    %updating()
    %rest:POST()
    %rest:path('/dba/db-create')
    %rest:query-param('name','{$name}')
    %rest:query-param('opts','{$opts}')
    %rest:query-param('lang','{$lang}')
    Source ( 78 lines)
    function dba:db-create(
      $name  as xs:string,
      $opts  as xs:string*,
      $lang  as xs:string?
    ) as empty-sequence() {
      try {
        if(db:exists($name)) then (
          error((), 'Database already exists.')
        ) else (
          db:create($name, (), (), map:merge((
            for $option in ('textindex', 'attrindex', 'tokenindex', 'ftindex',
              'stemming', 'casesens', 'diacritics', 'updindex')
            return map:entry($option, $opts = $option),
            $lang ! map:entry('language', .)))
          ),
          util:redirect($dba:SUB, map { 'name': $name,
            'info': 'Database "' || $name || '" was created.' })
        )
      } catch * {
        util:redirect('db-create', map {
          'name': $name, 'opts': $opts, 'lang': $lang, 'error': $err:description
        })
      }
    }
    function dba:db-create(
      $name   as xs:string?,
      $opts   as xs:string*,
      $lang   as xs:string?,
      $error  as xs:string?
    ) as element(html) {
      let $opts := if($opts = 'x') then $opts else ('textindex', 'attrindex')
      return html:wrap(map { 'header': $dba:CAT, 'error': $error },
        <tr>
          <td>
            <form action='db-create' method='post' autocomplete='off'>
              <h2>{
                html:link('Databases', $dba:CAT), ' » ',
                html:button('create', 'Create')
              }</h2>
              <!-- dummy value; prevents reset of options when nothing is selected -->
              <input type='hidden' name='opts' value='x'/>
              <table>
                <tr>
                  <td>Name:</td>
                  <td>
                    <input type='hidden' name='opts' value='x'/>
                    <input type='text' name='name' value='{ $name }' id='name'/>
                    { html:focus('name') }
                    <div class='small'/>
                  </td>
                </tr>
                <tr>
                  <td colspan='2'>{
                    <h3>{ html:option('textindex', 'Text Index', $opts) }</h3>,
                    <h3>{ html:option('attrindex', 'Attribute Index', $opts) }</h3>,
                    <h3>{ html:option('tokenindex', 'Token Index', $opts) }</h3>,
                    html:option('updindex', 'Incremental Indexing', $opts),
                    <div class='small'/>,
                    <h3>{ html:option('ftindex', 'Fulltext Indexing', $opts) }</h3>
                  }</td>
                </tr>
                <tr>
                  <td colspan='2'>{
                    html:option('stemming', 'Stemming', $opts),
                    html:option('casesens', 'Case Sensitivity', $opts),
                    html:option('diacritics', 'Diacritics', $opts)
                  }</td>
                </tr>
                <tr>
                  <td>Language:</td>
                  <td><input type='text' name='language' value='{ $lang }'/></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-createGETdba:db-create#4
    /dba/db-createPOSTdba:db-create#3

    Source Code

    (:~
     : Create new 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 creating a new database.
     : @param  $name   entered name
     : @param  $opts   chosen database options
     : @param  $lang   entered language
     : @param  $error  error string
     : @return page
     :)
    declare
      %rest:GET
      %rest:path('/dba/db-create')
      %rest:query-param('name',  '{$name}')
      %rest:query-param('opts',  '{$opts}')
      %rest:query-param('lang',  '{$lang}', 'en')
      %rest:query-param('error', '{$error}')
      %output:method('html')
    function dba:db-create(
      $name   as xs:string?,
      $opts   as xs:string*,
      $lang   as xs:string?,
      $error  as xs:string?
    ) as element(html) {
      let $opts := if($opts = 'x') then $opts else ('textindex', 'attrindex')
      return html:wrap(map { 'header': $dba:CAT, 'error': $error },
        <tr>
          <td>
            <form action='db-create' method='post' autocomplete='off'>
              <h2>{
                html:link('Databases', $dba:CAT), ' » ',
                html:button('create', 'Create')
              }</h2>
              <!-- dummy value; prevents reset of options when nothing is selected -->
              <input type='hidden' name='opts' value='x'/>
              <table>
                <tr>
                  <td>Name:</td>
                  <td>
                    <input type='hidden' name='opts' value='x'/>
                    <input type='text' name='name' value='{ $name }' id='name'/>
                    { html:focus('name') }
                    <div class='small'/>
                  </td>
                </tr>
                <tr>
                  <td colspan='2'>{
                    <h3>{ html:option('textindex', 'Text Index', $opts) }</h3>,
                    <h3>{ html:option('attrindex', 'Attribute Index', $opts) }</h3>,
                    <h3>{ html:option('tokenindex', 'Token Index', $opts) }</h3>,
                    html:option('updindex', 'Incremental Indexing', $opts),
                    <div class='small'/>,
                    <h3>{ html:option('ftindex', 'Fulltext Indexing', $opts) }</h3>
                  }</td>
                </tr>
                <tr>
                  <td colspan='2'>{
                    html:option('stemming', 'Stemming', $opts),
                    html:option('casesens', 'Case Sensitivity', $opts),
                    html:option('diacritics', 'Diacritics', $opts)
                  }</td>
                </tr>
                <tr>
                  <td>Language:</td>
                  <td><input type='text' name='language' value='{ $lang }'/></td>
                </tr>
              </table>
            </form>
          </td>
        </tr>
      )
    };
    
    (:~
     : Creates a database.
     : @param  $name  database
     : @param  $opts  database options
     : @param  $lang  language
     : @return redirection
     :)
    declare
      %updating
      %rest:POST
      %rest:path('/dba/db-create')
      %rest:query-param('name', '{$name}')
      %rest:query-param('opts', '{$opts}')
      %rest:query-param('lang', '{$lang}')
    function dba:db-create(
      $name  as xs:string,
      $opts  as xs:string*,
      $lang  as xs:string?
    ) as empty-sequence() {
      try {
        if(db:exists($name)) then (
          error((), 'Database already exists.')
        ) else (
          db:create($name, (), (), map:merge((
            for $option in ('textindex', 'attrindex', 'tokenindex', 'ftindex',
              'stemming', 'casesens', 'diacritics', 'updindex')
            return map:entry($option, $opts = $option),
            $lang ! map:entry('language', .)))
          ),
          util:redirect($dba:SUB, map { 'name': $name,
            'info': 'Database "' || $name || '" was created.' })
        )
      } catch * {
        util:redirect('db-create', map {
          'name': $name, 'opts': $opts, 'lang': $lang, 'error': $err:description
        })
      }
    };