19
: This module contains functions to compile and evaluate XQuery
20
: programs. Also, it contains function that allow to parameterize
21
: the static or dynamic evaluation phase.
19
: This module contains functions to compile and evaluate queries
20
: written in either JSONiq or XQuery. Also, it contains function that
21
: allow to parameterize the static or dynamic evaluation phase.
23
23
: @author Juan Zacarias
24
: @project Zorba/Programming Languages/XQXQ
24
: @project Zorba/Programming Languages/ZQ
26
module namespace xqxq = 'http://www.zorba-xquery.com/modules/xqxq';
26
module namespace zq = 'http://zorba.io/modules/zorba-query';
28
28
declare namespace an = "http://zorba.io/annotations";
29
29
declare namespace ver = "http://zorba.io/options/versioning";
30
30
declare namespace op = "http://zorba.io/options/features";
31
31
declare namespace f = "http://zorba.io/features";
33
declare option ver:module-version "2.0";
33
declare option ver:module-version "1.0";
36
: The function prepares a given XQuery program for execution.
37
: If the program was successfully compiled, the function returns an
36
: <p>The function prepares a given a query for execution.</p>
37
: <p>If the query was successfully compiled, the function returns an
38
38
: identifier as xs:anyURI. This URI can be passed to other functions
39
: of this module (e.g. to actually evaluate the program). The URI
40
: is opaque and its lilfetime is bound by the lifetime of the XQuery
41
: program that invoked this function. Further reference or uses
42
: of the identifier lead to unexpected results.
44
: Successfully prepared queries need to be deleted by passing the resulting
45
: identifier to the xqxq:delete-query function of this module.
47
: @param $main-module-text the XQuery program that should be prepared.
48
: The program needs to be a XQuery main module.
50
: @return an identifier for the compiled program that can be passed
39
: of this module (e.g. to actually evaluate the query). The URI
40
: is opaque and its lifetime is bound by the lifetime of the query
41
: that invoked this function. Further reference or uses
42
: of the identifier lead to unexpected results.</p>
44
: <p>Successfully prepared queries need to be deleted by passing the resulting
45
: identifier to the zq:delete-query function of this module.</p>
47
: @param $main-module-text the query that should be prepared.
48
: The query needs to be a XQuery or JSONiq main module.
50
: @return an identifier for the compiled query that can be passed
51
51
: as arguments to other functions of this module.
53
53
: @error any (static or type) error that may be raised during the compilation
54
: of the query. For example, err:XPST0003 if the given XQuery program could
54
: of the query. For example, err:XPST0003 if the given query could
57
declare %an:sequential function xqxq:prepare-main-module($main-module-text as xs:string) as
57
declare %an:sequential function zq:prepare-main-module($main-module-text as xs:string) as
58
58
xs:anyURI external;
61
: The function prepares a given XQuery program for execution.
62
: If the program was successfully compiled, the function returns an
61
: <p>The function prepares a given query for execution.</p>
62
: <p>If the query was successfully compiled, the function returns an
63
63
: identifier as xs:anyURI. This URI can be passed to other functions
64
: of this module (e.g. to actually evaluate the program). The URI
65
: is opaque and its lilfetime is bound by the lifetime of the XQuery
66
: program that invoked this function. Further reference or uses
67
: of the identifier lead to unexpected results.
69
: Important notes regarding the second and third parameters of the function:
70
: --------------------------------------------------------------------------
72
: These parameters allow you to specify a URL resolver and a URI mapper
64
: of this module (e.g. to actually evaluate the query). The URI
65
: is opaque and its lifetime is bound by the lifetime of the query
66
: that invoked this function. Further reference or uses
67
: of the identifier lead to unexpected results.</p>
69
: <p>Important notes regarding the second and third parameters of the function:</p>
70
: <p>--------------------------------------------------------------------------</p>
72
: <p>These parameters allow you to specify a URL resolver and a URI mapper
73
73
: for Zorba to use when executing this query. See
74
: http://www.zorba-xquery.com/html/documentation/2.7.0/zorba/uriresolvers
76
: The second parameter is a function item for a URL
74
: <a href="http://www.zorba-xquery.com/html/documentation/2.7.0/zorba/uriresolvers">here</a></p>
76
: <ul>The second parameter is a function item for a URL
77
77
: resolver. The URL resolver function must recive 2 parameters:
78
: A $namespace as xs:string that will contain the url to be resolved.
79
: A $entity as xs:string that will contain the type of resolving needed;
80
: this can be 2 values "module" and "schema".
81
: The function must return an empty sequence when the specified $namespace
82
: or $entity are not the ones to be resolved.
86
: declare function mymod:url-resolver($namespace as xs:string, $entity as xs:string)
78
: <li>A $namespace as xs:string that will contain the url to be resolved.</li>
79
: <li>A $entity as xs:string that will contain the type of resolving needed.
80
: This can be one of two values: "module" or "schema".</li>
82
: <p>The function must return the empty sequence when the specified $namespace
83
: or $entity are not the ones to be resolved.</p>
87
: <code>declare function mymod:url-resolver($namespace as xs:string, $entity as xs:string) as item()?
88
89
: if($namespace = 'http://test.xq')
89
90
: then "module namespace test = 'http://test'; declare function test:foo(){'foo'};"
93
: The URL resolver function's namespace, name, and parameter naming are
94
: not restricted by XQXQ.
96
: The URL resolver function's return type is not restricted, it could be a string, a sequence,
97
: a node, etc. All the outputs types are to be serialized as a string.
99
: The third parameter is a function item for a URI mapper.
100
: The URI mapper function, just like the URL resolver, receives 2 parameters:
101
: A $namespace as xs:string that will contain the URI to be mapped.
102
: A $entity as xs:string that will contain the type of resolving needed;
103
: this can be 2 values "module" and "schema".
104
: The URI mapper must return an empty sequence when the specified $namesapce or $entity
105
: are not to be mapped. Unlike the URL resolver this function must return a sequence of strings.
109
: declare function mymod:uri-mapper($namespace as xs:string, $entity as xs:string)
94
: <p>The URL resolver function's namespace, name, and parameter naming are
95
: not restricted by ZQ.</p>
97
: <p>The URL resolver function's return type is not restricted, it could be a string, a sequence,
98
: a node, etc. All the outputs types are to be serialized as a string.</p>
100
: <p>The third parameter is a function item for a URI mapper.</p>
101
: <ul>The URI mapper function, just like the URL resolver, receives 2 parameters:
102
: <li>A $namespace as xs:string that will contain the URI to be mapped.</li>
103
: <li>A $entity as xs:string that will contain the type of resolving needed.
104
: This can be one of two values: "module" or "schema".</li>
106
: <p>The URI mapper must return an empty sequence when the specified $namesapce or $entity
107
: are not to be mapped. Unlike the URL resolver this function must return a sequence of strings.</p>
111
: <code>declare function mymod:uri-mapper($namespace as xs:string, $entity as xs:string)
111
113
: if($namespace = 'http://test')
112
: then ("http://www.zorba-xquery.com/test", "http://foo.com/schema/test")
114
: then ("http://zorba.io/test", "http://foo.com/schema/test")
116
: The URI mapper function's namespace, name, and parameter naming are
117
: not restricted by XQXQ.
119
: In order to pass the above URL resolver and URI mapper to this function,
120
: use the following syntax:
122
: variable $queryID := xqxq:prepare-main-module("..query text..",
123
: mymod:url-resolver#2, mymod:uri-mapper#2);
125
: That is, the QName of the function followed by "#2". This is XQuery
118
: <p>The URI mapper function's namespace, name, and parameter naming are
119
: not restricted by ZQ.</p>
121
: <p>In order to pass the above URL resolver and URI mapper to this function,
122
: use the following syntax:</p>
124
: <code>variable $queryID := zq:prepare-main-module("..query text..",
125
: mymod:url-resolver#2, mymod:uri-mapper#2);</code>
127
: <p>That is, the QName of the function followed by "#2". This is XQuery
126
128
: "higher-order function" syntax, meaning the function with the specified
127
129
: QName which takes two arguments. Since URL resolvers and URI mappers
128
: must take two arguments, both will always be specified with "#2".
130
: Note that parameters 2 and 3 should be declared as follows:
131
: as function($url as xs:string, $entity as xs:string) as item()
132
: as function($uri as xs:string, $entity as xs:string) as xs:string*
133
: However Zorba's implementation of higher-order functions (HOF) is not
134
: yet complete enough to allow for this. When Zorba's HOF implementation
135
: is complete this function signature will be changed.
137
: Both the URL resolver and URI mapper functions are optional, meaning you
138
: may pass the empty-sequence () for either.
140
: Successfully prepared queries need to be deleted by passing the resulting
141
: identifier to the xqxq:delete-query function of this module.
143
: @param $main-module-text the XQuery program that should be prepared.
144
: The program needs to be a XQuery main module.
130
: must take two arguments, both will always be specified with "#2".</p>
132
: <p>Both the URL resolver and URI mapper functions are optional, meaning you
133
: may pass the empty-sequence () for either.</p>
135
: <p>Successfully prepared queries need to be deleted by passing the resulting
136
: identifier to the zq:delete-query function of this module.</p>
138
: @param $main-module-text the query that should be prepared.
139
: The query needs to be a XQuery or JSONiq main module.
146
141
: @param $resolver the URL resolver function.
148
143
: @param $mapper the URI mapper function.
150
: @return an identifier for the compiled program that can be passed
145
: @return an identifier for the compiled query that can be passed
151
146
: as arguments to other functions of this module.
153
148
: @error any (static or type) error that may be raised during the compilation
154
: of the query. For example, err:XPST0003 if the given XQuery program could
149
: of the query. For example, err:XPST0003 if the given query could
157
declare %an:sequential function xqxq:prepare-main-module($main-module-text as xs:string, $resolver as item()?, $mapper as item()?) as
152
declare %an:sequential function zq:prepare-main-module(
153
$main-module-text as xs:string,
154
$resolver as ( function(xs:string, xs:string) as item()? )?,
155
$mapper as ( function(xs:string, xs:string) as xs:string* )? ) as
161
: This function compiles a given XQuery library module. It can be used
162
: to compile-check a module.
159
: <p>This function compiles a given XQuery or JSONiq library module.
160
: It can be used to compile-check a module.</p>
164
: @param $library-module-text the XQuery library module that should
162
: @param $library-module-text the library module that should
167
: @return the function is declared as sequential.It returns the
165
: @return the empty-sequence.
170
167
: @error any (static or type) error that may be raised during the compilation
171
: of the library module. For example, err:XPST0003 if the given XQuery library
168
: of the library module. For example, err:XPST0003 if the given library
172
169
: module could not be parsed.
174
declare %an:sequential function xqxq:prepare-library-module($library-module-text as xs:string) as
171
declare %an:sequential function zq:prepare-library-module($library-module-text as xs:string) as
175
172
empty-sequence() external ;
178
: The function tests if the context-item is bound for the
179
: execution of the query referred to by the given query identifier.
175
: <p>The function tests if the context-item is bound for the
176
: execution of the query referred to by the given query identifier.</p>
181
178
: @param $query-key the identifier for a compiled query
183
180
: @return true if the context-item is bound, false otherwise.
185
: @error xqxq:NoQueryMatch if no query with the given identifier
182
: @error zq:NO_QUERY_MATCH if no query with the given identifier
188
declare function xqxq:is-bound-context-item($query-key as xs:anyURI)
185
declare function zq:is-bound-context-item($query-key as xs:anyURI)
189
186
as xs:boolean external;
193
: The function tests if the given variable is bound for the
194
: execution of the query referred to by the given query identifier.
190
: <p>The function tests if the given variable is bound for the
191
: execution of the query referred to by the given query identifier.</p>
196
193
: @param $query-key the identifier for a compiled query
197
194
: @param $var-name the name of the variable
199
196
: @return true if the variable is bound, false otherwise.
201
: @error xqxq:NoQueryMatch if no query with the given identifier
198
: @error zq:NO_QUERY_MATCH if no query with the given identifier
203
: @error xqxq:UndeclaredVariable if the given variable is not declared
200
: @error zq:UNDECLARED_VARIABLE if the given variable is not declared
206
declare function xqxq:is-bound-variable($query-key as xs:anyURI, $var-name as
203
declare function zq:is-bound-variable($query-key as xs:anyURI, $var-name as
207
204
xs:QName) as xs:boolean external;
210
: The function returns the names of the external variables that
207
: <p>The function returns the names of the external variables that
211
208
: are declared in the given query (either in the main module or
212
: in any of the imported library modules).
209
: in any of the imported library modules).</p>
214
211
: @param $query-key the identifier for a compiled query
216
213
: @return the sequence of names of the said external variables.
218
: @error xqxq:NoQueryMatch if no query with the given identifier
215
: @error zq:NO_QUERY_MATCH if no query with the given identifier
221
declare function xqxq:external-variables($query-key as xs:anyURI) as
218
declare function zq:external-variables($query-key as xs:anyURI) as
222
219
xs:QName* external ;
225
: The function tests if the query identified by the given key
226
: is an updating query.
222
: <p>The function tests if the query identified by the given key
223
: is an updating query.</p>
228
225
: @param $query-key the identifier for a compiled query
230
227
: @return true if the query is an updating query, false otherwise.
232
: @error xqxq:NoQueryMatch if no query with the given identifier
229
: @error zq:NO_QUERY_MATCH if no query with the given identifier
235
declare function xqxq:is-updating($query-key as xs:anyURI) as
232
declare function zq:is-updating($query-key as xs:anyURI) as
236
233
xs:boolean external;
239
: The function tests if the query identified by the given key
240
: is sequential query.
236
: <p>The function tests if the query identified by the given key
237
: is sequential query.</p>
242
239
: @param $query-key the identifier for a compiled query
244
241
: @return true if the query is a sequential, false otherwise.
246
: @error xqxq:NoQueryMatch if no query with the given identifier
243
: @error zq:NO_QUERY_MATCH if no query with the given identifier
249
declare function xqxq:is-sequential($query-key as xs:anyURI) as
246
declare function zq:is-sequential($query-key as xs:anyURI) as
250
247
xs:boolean external;
253
: This function binds the context-item of the prepared query
254
: identified by the given key to the $dot argument.
250
: <p>This function binds the context-item of the prepared query
251
: identified by the given key to the $dot argument.</p>
256
253
: @param $query-key the identifier for a compiled query
257
254
: @param $dot the context item to bind
277
274
: @return the function has side effects and returns the empty
280
: @error xqxq:NoQueryMatch if no query with the given identifier
277
: @error zq:NO_QUERY_MATCH if no query with the given identifier
282
: @error xqxq:UndeclaredVariable if the given variable is not declared
279
: @error zq:UNDECLARED_VARIABLE if the given variable is not declared
285
declare %an:sequential function xqxq:bind-variable($query-key as xs:anyURI,
282
declare %an:sequential function zq:bind-variable($query-key as xs:anyURI,
286
283
$var as xs:QName, $value as item()*) as empty-sequence() external ;
290
: Evaluates the given prepared query and returns the result
287
: <p>Evaluates the given prepared query and returns the result
291
288
: of the evaluation. The query must not be sequential or
294
291
: @param $query-key the identifier for a compiled query
296
293
: @return the result of evaluating the given query
298
: @error xqxq:NoQueryMatch if no query with the given identifier
295
: @error zq:NO_QUERY_MATCH if no query with the given identifier
301
: @error xqxq:QueryIsUpdating if the query is an updating query.
298
: @error zq:QUERY_IS_UPDATING if the query is an updating query.
303
: @error xqxq:QueryIsSequential if the query is sequential.
300
: @error zq:QUERY_IS_SEQUENTIAL if the query is sequential.
305
302
: @error any dynamic error that is raised by evaluating the
309
declare function xqxq:evaluate($query-key as xs:anyURI) as item()* external;
306
declare function zq:evaluate($query-key as xs:anyURI) as item()* external;
312
: Evaluates the given prepared query and applies the updates
313
: computed by this query. The query must be an updating query.
309
: <p>Evaluates the given prepared query and applies the updates
310
: computed by this query. The query must be an updating query.</p>
315
312
: @param $query-key the identifier for a compiled query
317
314
: @return the function has side effects because it applies
318
315
: the updates of the query. It returns the empty sequence.
320
: @error xqxq:NoQueryMatch if no query with the given identifier
317
: @error zq:NO_QUERY_MATCH if no query with the given identifier
323
: @error xqxq:QueryNotUpdating if the query is not an updating query.
320
: @error zq:QUERY_NOT_UPDATING if the query is not an updating query.
325
: @error xqxq:QueryIsSequential if the query is sequential.
322
: @error zq:QUERY_IS_SEQUENTIAL if the query is sequential.
327
324
: @error any dynamic error that is raised by evaluating the
328
325
: given query or applying its updates.
331
declare updating function xqxq:evaluate-updating($query-key as xs:anyURI) external;
328
declare updating function zq:evaluate-updating($query-key as xs:anyURI) external;
334
: Evaluates the given prepared query and returns the result
335
: of the evaluation. The query must be sequential.
331
: <p>Evaluates the given prepared query and returns the result
332
: of the evaluation. The query must be sequential.</p>
337
334
: @param $query-key the identifier for a compiled query
339
336
: @return the result of evaluating the query.
341
: @error xqxq:NoQueryMatch if no query with the given identifier
338
: @error zq:NO_QUERY_MATCH if no query with the given identifier
344
: @error xqxq:QueryNotSequential if the query is not sequential.
341
: @error zq:QUERY_NOT_SEQUENTIAL if the query is not sequential.
346
: @error xqxq:QueryIsUpdating if the query is an updating query.
343
: @error zq:QUERY_IS_UPDATING if the query is an updating query.
348
345
: @error any dynamic error that is raised by evaluating the
352
declare %an:sequential function xqxq:evaluate-sequential($query-key as
349
declare %an:sequential function zq:evaluate-sequential($query-key as
353
350
xs:string) as item()* external;
356
: Deletes the prepared query associated with the given identifier.
357
: After the query is deleted, the corresponding identifier should
358
: not be used as argument to any of the functions of this module.
353
: <p>Deletes the prepared query associated with the given identifier.</p>
354
: <p>After the query is deleted, the corresponding identifier should
355
: not be used as argument to any of the functions of this module.</p>
360
357
: @param $query-key the identifier for a compiled query
362
359
: @return the function has side effects and returns the empty sequence.
364
: @error xqxq:NoQueryMatch if no query with the given identifier
361
: @error zq:NO_QUERY_MATCH if no query with the given identifier
368
declare %an:sequential function xqxq:delete-query($query-key as xs:anyURI) as
365
declare %an:sequential function zq:delete-query($query-key as xs:anyURI) as
369
366
empty-sequence() external;
372
: This function returns the value of a variable that is bound in the
369
: <p>This function returns the value of a variable that is bound in the
375
372
: @param $query-key the identifier of a compiled query.
376
373
: @param $var-name the name of the variable whose value should be returned.
378
375
: @return the value bound to the given variable.
380
: @error xqxq:NoQueryMatch if no query with the given identifier
377
: @error zq:NO_QUERY_MATCH if no query with the given identifier
382
: @error xqxq:UndeclaredVariable if the given variable is not declared
379
: @error zq:UNDECLARED_VARIABLE if the given variable is not declared
384
: @error xqxq:UnboundVariable if the given variable doesn't have a value.
381
: @error zq:UNBOUND_VARIABLE if the given variable doesn't have a value.
386
declare function xqxq:variable-value($query-key as xs:anyURI, $var-name as
383
declare function zq:variable-value($query-key as xs:anyURI, $var-name as
387
384
xs:QName) as item()* external;
390
: Returns the compiled query identified by the given query-key
387
: <p>Returns the compiled query identified by the given query-key
388
: as binary data.</p>
393
390
: @param $query-key the identifier of a compiled query.
395
392
: @return the query as xs:base64Binary.
397
: @error xqxq:NoQueryMatch if no query with the given identifier
394
: @error zq:NO_QUERY_MATCH if no query with the given identifier
399
: @error xqxq:QueryPlanError if there is an error serializing the query.
396
: @error zq:NO_QUERY_PLAN if there is an error serializing the query.
401
declare function xqxq:query-plan($query-key as xs:anyURI)
398
declare function zq:query-plan($query-key as xs:anyURI)
402
399
as xs:base64Binary external;
406
: The function loads a given XQuery program for execution from a
407
: xs:base64Binary query plan, obtained through the xqxq:query-plan function.
408
: If the program was successfully loaded, the function returns an
403
: <p>The function loads a given query for execution from a
404
: xs:base64Binary query plan, obtained through the zq:query-plan function.</p>
405
: <p>If the query was successfully loaded, the function returns an
409
406
: identifier as xs:anyURI. This URI can be passed to other functions
410
: of this module (e.g. to actually evaluate the program). The URI
411
: is opaque and its lifetime is bound by the lifetime of the XQuery
412
: program that invoked this function. Further reference or uses
413
: of the identifier lead to unexpected results.
415
: Successfully prepared queries need to be deleted by passing the resulting
416
: identifier to the xqxq:delete-query function of this module.
418
: @param $main-module-text the XQuery program that should be prepared.
419
: The program needs to be a XQuery main module.
421
: @return an identifier for the compiled program that can be passed
407
: of this module (e.g. to actually evaluate the query). The URI
408
: is opaque and its lifetime is bound by the lifetime of the query
409
: that invoked this function. Further reference or uses
410
: of the identifier lead to unexpected results.</p>
412
: <p>Successfully prepared queries need to be deleted by passing the resulting
413
: identifier to the zq:delete-query function of this module.</p>
415
: @param $plan the binary query plan.
417
: @return an identifier for the compiled query that can be passed
422
418
: as arguments to other functions of this module.
424
420
: @error any (static or type) error that may be raised during the compilation
425
: of the query. For example, err:XPST0003 if the given XQuery program could
421
: of the query. For example, err:XPST0003 if the given query could
428
declare function xqxq:load-from-query-plan($plan as xs:base64Binary)
424
declare function zq:load-from-query-plan($plan as xs:base64Binary)
429
425
as xs:anyURI external;
432
: The function loads a given XQuery program for execution from a
433
: xs:base64Binary query plan, obtained through the xqxq:query-plan function.
434
: If the program was successfully loaded, the function returns an
428
: <p>The function loads a given query for execution from a
429
: xs:base64Binary query plan, obtained through the zq:query-plan function.</p>
430
: <p>If the query was successfully loaded, the function returns an
435
431
: identifier as xs:anyURI. This URI can be passed to other functions
436
: of this module (e.g. to actually evaluate the program). The URI
437
: is opaque and its lilfetime is bound by the lifetime of the XQuery
438
: program that invoked this function. Further reference or uses
439
: of the identifier lead to unexpected results.
441
: For important notes regarding the second and third parameters of the
442
: function, review the comments in xqxq:prepare-main-module#3.
444
: Successfully prepared queries need to be deleted by passing the resulting
445
: identifier to the xqxq:delete-query function of this module.
447
: @param $main-module-text the XQuery program that should be prepared.
448
: The program needs to be a XQuery main module.
432
: of this module (e.g. to actually evaluate the query). The URI
433
: is opaque and its lilfetime is bound by the lifetime of the query
434
: that invoked this function. Further reference or uses
435
: of the identifier lead to unexpected results.</p>
437
: <p>For important notes regarding the second and third parameters of the
438
: function, review the comments in zq:prepare-main-module#3.</p>
440
: <p>Successfully prepared queries need to be deleted by passing the resulting
441
: identifier to the zq:delete-query function of this module.</p>
443
: @param $plan the binary query plan.
450
445
: @param $resolver the URL resolver function.
452
447
: @param $mapper the URI mapper function.
454
: @return an identifier for the compiled program that can be passed
449
: @return an identifier for the compiled query that can be passed
455
450
: as arguments to other functions of this module.
457
452
: @error any (static or type) error that may be raised during the compilation
458
: of the query. For example, err:XPST0003 if the given XQuery program could
453
: of the query. For example, err:XPST0003 if the given query could
461
declare function xqxq:load-from-query-plan($plan as xs:base64Binary,
456
declare function zq:load-from-query-plan($plan as xs:base64Binary,
462
457
$resolver as item()?, $mapper as item()?) as xs:anyURI external;
465
: Internal helper function. Only necessary because of incomplete HOF
460
: <p>Internal helper function. Only necessary because of incomplete HOF
461
: support in Zorba.</p>
468
declare %private function xqxq:hof-invoker($hof as item(),
463
declare %private function zq:hof-invoker($hof as item(),
469
464
$ns as xs:string, $entity as xs:string) as item()*
471
466
$hof($ns, $entity)