<commit_before>(:~
 : Save query.
 :
 : @author <NAME>, BaseX Team 2005-21, BSD License
 :)
module namespace dba = 'dba/queries';

import module namespace config = 'dba/config' at '../lib/config.xqm';

(:~
 : Saves a query file and returns the list of stored queries.
 : @param  $name   name of query file
 : @param  $query  query string
 : @return names of stored queries
 :)
declare
  %rest:POST('{$query}')
  %rest:path('/dba/query-save')
  %rest:query-param('name', '{$name}')
  %output:method('text')
function dba:query-save(
  $name   as xs:string,
  $query  as xs:string?
) as xs:string {
  let $path := config:directory() || $name
  return (
    try {
      prof:void(xquery:parse($query, map {
        'plan': false(), 'pass': true(), 'base-uri': $path
      }))
    } catch * {
      error($err:code, 'Query was not stored: ' || $err:description, $err:value)
    },
    config:query($name),
    file:write-text($path, $query),
    string-join(config:query-files(), '/')
  )
};<commit_msg>[MIN] DBA: Saving queries (#2030, 7e1040ea7430d4e5d264a2fd1bce8fc79eedfa30)<commit_after>(:~
 : Save query.
 :
 : @author <NAME>, BaseX Team 2005-21, BSD License
 :)
module namespace dba = 'dba/queries';

import module namespace config = 'dba/config' at '../lib/config.xqm';

(:~
 : Saves a query file and returns the list of stored queries.
 : @param  $name   name of query file
 : @param  $query  query string
 : @return names of stored queries
 :)
declare
  %rest:POST('{$query}')
  %rest:path('/dba/query-save')
  %rest:query-param('name', '{$name}')
  %output:method('text')
function dba:query-save(
  $name   as xs:string,
  $query  as xs:string?
) as xs:string {
  let $path := config:directory() || $name
  return (
    try {
      prof:void(xquery:parse(string($query), map {
        'plan': false(), 'pass': true(), 'base-uri': $path
      }))
    } catch * {
      error($err:code, 'Query was not stored: ' || $err:description, $err:value)
    },
    config:query($name),
    file:write-text($path, $query),
    string-join(config:query-files(), '/')
  )
};
{'fraction_non_alphanumeric': 0.1824782187802517, 'fraction_numerical': 0.017909002904162634, 'mean_word_length': 3.552863436123348, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2363334', 'n_tokens_mistral': 759, 'n_tokens_neox': 724, 'n_words': 183}