Skip to content

Commit 6074190

Browse files
committed
[bugfix] When an external variable is declared in XQuery throw an error if it was not set before execution
1 parent e5c7cc9 commit 6074190

File tree

33 files changed

+739
-115
lines changed

33 files changed

+739
-115
lines changed

exist-core/pom.xml

Lines changed: 34 additions & 0 deletions
Large diffs are not rendered by default.

exist-core/src/main/java/org/exist/collections/triggers/XQueryTrigger.java

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,28 @@
11
/*
2+
* Elemental
3+
* Copyright (C) 2024, Evolved Binary Ltd
4+
*
5+
6+
* https://www.evolvedbinary.com | https://www.elemental.xyz
7+
*
8+
* This library is free software; you can redistribute it and/or
9+
* modify it under the terms of the GNU Lesser General Public
10+
* License as published by the Free Software Foundation; version 2.1.
11+
*
12+
* This library is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
* Lesser General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public
18+
* License along with this library; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20+
*
21+
* NOTE: Parts of this file contain code from 'The eXist-db Authors'.
22+
* The original license header is included below.
23+
*
24+
* =====================================================================
25+
*
226
* eXist-db Open Source Native XML Database
327
* Copyright (C) 2001 The eXist-db Authors
428
*
@@ -314,28 +338,28 @@ private void finish(final TriggerEvent event, final DBBroker broker, final Txn t
314338

315339
private void declareExternalVariables(final XQueryContext context, final TriggerPhase phase, final TriggerEvent event, final XmldbURI src, final XmldbURI dst, final boolean isCollection) throws XPathException {
316340
//declare external variables
317-
context.declareVariable(bindingPrefix + "type", new StringValue(phase.legacyPhaseName()));
318-
context.declareVariable(bindingPrefix + "event", new StringValue(event.legacyEventName()));
341+
context.declareVariable(bindingPrefix + "type", true, new StringValue(phase.legacyPhaseName()));
342+
context.declareVariable(bindingPrefix + "event", true, new StringValue(event.legacyEventName()));
319343
if (isCollection) {
320-
context.declareVariable(bindingPrefix + "collection", new AnyURIValue(src));
344+
context.declareVariable(bindingPrefix + "collection", true, new AnyURIValue(src));
321345
} else {
322-
context.declareVariable(bindingPrefix + "collection", new AnyURIValue(src.removeLastSegment()));
346+
context.declareVariable(bindingPrefix + "collection", true, new AnyURIValue(src.removeLastSegment()));
323347
}
324-
context.declareVariable(bindingPrefix + "uri", new AnyURIValue(src));
348+
context.declareVariable(bindingPrefix + "uri", true, new AnyURIValue(src));
325349
if (dst == null) {
326-
context.declareVariable(bindingPrefix + "new-uri", Sequence.EMPTY_SEQUENCE);
350+
context.declareVariable(bindingPrefix + "new-uri", true, Sequence.EMPTY_SEQUENCE);
327351
} else {
328-
context.declareVariable(bindingPrefix + "new-uri", new AnyURIValue(dst));
352+
context.declareVariable(bindingPrefix + "new-uri", true, new AnyURIValue(dst));
329353
}
330354

331355
// For backward compatibility
332-
context.declareVariable(bindingPrefix + "eventType", new StringValue(phase.legacyPhaseName()));
333-
context.declareVariable(bindingPrefix + "triggerEvent", new StringValue(event.legacyEventName()));
356+
context.declareVariable(bindingPrefix + "eventType", true, new StringValue(phase.legacyPhaseName()));
357+
context.declareVariable(bindingPrefix + "triggerEvent", true, new StringValue(event.legacyEventName()));
334358
if (isCollection) {
335-
context.declareVariable(bindingPrefix + "collectionName", new AnyURIValue(src));
359+
context.declareVariable(bindingPrefix + "collectionName", true, new AnyURIValue(src));
336360
} else {
337-
context.declareVariable(bindingPrefix + "collectionName", new AnyURIValue(src.removeLastSegment()));
338-
context.declareVariable(bindingPrefix + "documentName", new AnyURIValue(src));
361+
context.declareVariable(bindingPrefix + "collectionName", true, new AnyURIValue(src.removeLastSegment()));
362+
context.declareVariable(bindingPrefix + "documentName", true, new AnyURIValue(src));
339363
}
340364

341365
//declare user defined parameters as external variables
@@ -344,7 +368,7 @@ private void declareExternalVariables(final XQueryContext context, final Trigger
344368
final String varName = (String) o;
345369
final String varValue = userDefinedVariables.getProperty(varName);
346370

347-
context.declareVariable(bindingPrefix + varName, new StringValue(varValue));
371+
context.declareVariable(bindingPrefix + varName, true, new StringValue(varValue));
348372
}
349373
}
350374
}
@@ -373,7 +397,7 @@ private CompiledXQuery getScript(final DBBroker broker, final Txn transaction) t
373397
final String varName = (String) o;
374398
final String varValue = userDefinedVariables.getProperty(varName);
375399

376-
context.declareVariable(bindingPrefix + varName, new StringValue(varValue));
400+
context.declareVariable(bindingPrefix + varName, true, new StringValue(varValue));
377401
}
378402
}
379403

exist-core/src/main/java/org/exist/http/RESTServer.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1544,9 +1544,9 @@ private void declareExternalAndXQJVariables(final XQueryContext context,
15441544

15451545
// now declare variable
15461546
if (prefix != null) {
1547-
context.declareVariable(q.getPrefix() + ":" + q.getLocalPart(), sequence);
1547+
context.declareVariable(q.getPrefix() + ":" + q.getLocalPart(), true, sequence);
15481548
} else {
1549-
context.declareVariable(q.getLocalPart(), sequence);
1549+
context.declareVariable(q.getLocalPart(), true, sequence);
15501550
}
15511551
}
15521552
}
@@ -1651,22 +1651,22 @@ private void executeXProc(final DBBroker broker, final Txn transaction, final Do
16511651
context.prepareForReuse();
16521652
}
16531653

1654-
context.declareVariable("pipeline", resource.getURI().toString());
1654+
context.declareVariable("pipeline", true, resource.getURI().toString());
16551655

16561656
final String stdin = request.getParameter("stdin");
1657-
context.declareVariable("stdin", stdin == null ? "" : stdin);
1657+
context.declareVariable("stdin", true, stdin == null ? "" : stdin);
16581658

16591659
final String debug = request.getParameter("debug");
1660-
context.declareVariable("debug", debug == null ? "0" : "1");
1660+
context.declareVariable("debug", true, debug == null ? "0" : "1");
16611661

16621662
final String bindings = request.getParameter("bindings");
1663-
context.declareVariable("bindings", bindings == null ? "<bindings/>" : bindings);
1663+
context.declareVariable("bindings", true, bindings == null ? "<bindings/>" : bindings);
16641664

16651665
final String autobind = request.getParameter("autobind");
1666-
context.declareVariable("autobind", autobind == null ? "0" : "1");
1666+
context.declareVariable("autobind", true, autobind == null ? "0" : "1");
16671667

16681668
final String options = request.getParameter("options");
1669-
context.declareVariable("options", options == null ? "<options/>" : options);
1669+
context.declareVariable("options", true, options == null ? "<options/>" : options);
16701670

16711671
// TODO: don't hardcode this?
16721672
context.setModuleLoadPath(

exist-core/src/main/java/org/exist/http/urlrewrite/XQueryURLRewrite.java

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,28 @@
11
/*
2+
* Elemental
3+
* Copyright (C) 2024, Evolved Binary Ltd
4+
*
5+
6+
* https://www.evolvedbinary.com | https://www.elemental.xyz
7+
*
8+
* This library is free software; you can redistribute it and/or
9+
* modify it under the terms of the GNU Lesser General Public
10+
* License as published by the Free Software Foundation; version 2.1.
11+
*
12+
* This library is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
* Lesser General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public
18+
* License along with this library; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20+
*
21+
* NOTE: Parts of this file contain code from 'The eXist-db Authors'.
22+
* The original license header is included below.
23+
*
24+
* =====================================================================
25+
*
226
* eXist-db Open Source Native XML Database
327
* Copyright (C) 2001 The eXist-db Authors
428
*
@@ -933,14 +957,14 @@ private void declareVariables(final XQueryContext context, final SourceInfo sour
933957
// RequestModule.NAMESPACE_URI);
934958
context.setHttpContext(new XQueryContext.HttpContext(reqw, respw));
935959

936-
context.declareVariable("exist:controller", sourceInfo.controllerPath);
960+
context.declareVariable("exist:controller", true, sourceInfo.controllerPath);
937961
request.setAttribute("$exist:controller", sourceInfo.controllerPath);
938-
context.declareVariable("exist:root", basePath);
962+
context.declareVariable("exist:root", true, basePath);
939963
request.setAttribute("$exist:root", basePath);
940-
context.declareVariable("exist:context", request.getContextPath());
964+
context.declareVariable("exist:context", true, request.getContextPath());
941965
request.setAttribute("$exist:context", request.getContextPath());
942966
final String prefix = staticRewrite == null ? null : staticRewrite.getPrefix();
943-
context.declareVariable("exist:prefix", prefix == null ? "" : prefix);
967+
context.declareVariable("exist:prefix", true, prefix == null ? "" : prefix);
944968
request.setAttribute("$exist:prefix", prefix == null ? "" : prefix);
945969
String path;
946970
if (sourceInfo.controllerPath.length() > 0 && !"/".equals(sourceInfo.controllerPath)) {
@@ -952,15 +976,15 @@ private void declareVariables(final XQueryContext context, final SourceInfo sour
952976
if (p != Constants.STRING_NOT_FOUND) {
953977
path = path.substring(0, p);
954978
}
955-
context.declareVariable("exist:path", path);
979+
context.declareVariable("exist:path", true, path);
956980
request.setAttribute("$exist:path", path);
957981

958982
String resource = "";
959983
final Matcher nameMatcher = NAME_REGEX.matcher(path);
960984
if (nameMatcher.matches()) {
961985
resource = nameMatcher.group(1);
962986
}
963-
context.declareVariable("exist:resource", resource);
987+
context.declareVariable("exist:resource", true, resource);
964988
request.setAttribute("$exist:resource", resource);
965989

966990
if (LOG.isDebugEnabled()) {

exist-core/src/main/java/org/exist/repo/Deployment.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -712,17 +712,17 @@ private Sequence runQuery(final DBBroker broker, final XmldbURI targetCollection
712712
}
713713
final XQuery xqs = broker.getBrokerPool().getXQueryService();
714714
final XQueryContext ctx = new XQueryContext(broker.getBrokerPool());
715-
ctx.declareVariable("dir", tempDir.toAbsolutePath().toString());
715+
ctx.declareVariable("dir", true, tempDir.toAbsolutePath().toString());
716716
final Optional<Path> home = broker.getConfiguration().getExistHome();
717717
if(home.isPresent()) {
718-
ctx.declareVariable("home", home.get().toAbsolutePath().toString());
718+
ctx.declareVariable("home", true, home.get().toAbsolutePath().toString());
719719
}
720720

721721
if (targetCollection != null) {
722-
ctx.declareVariable("target", targetCollection.toString());
722+
ctx.declareVariable("target", true, targetCollection.toString());
723723
ctx.setModuleLoadPath(XmldbURI.EMBEDDED_SERVER_URI + targetCollection.toString());
724724
} else
725-
{ctx.declareVariable("target", Sequence.EMPTY_SEQUENCE);}
725+
{ctx.declareVariable("target", true, Sequence.EMPTY_SEQUENCE);}
726726
if (QueryPurpose.PREINSTALL == purpose) {
727727
// when running pre-setup scripts, base path should point to directory
728728
// because the target collection does not yet exist

exist-core/src/main/java/org/exist/scheduler/UserXQueryJob.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,28 @@
11
/*
2+
* Elemental
3+
* Copyright (C) 2024, Evolved Binary Ltd
4+
*
5+
6+
* https://www.evolvedbinary.com | https://www.elemental.xyz
7+
*
8+
* This library is free software; you can redistribute it and/or
9+
* modify it under the terms of the GNU Lesser General Public
10+
* License as published by the Free Software Foundation; version 2.1.
11+
*
12+
* This library is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
* Lesser General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public
18+
* License along with this library; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20+
*
21+
* NOTE: Parts of this file contain code from 'The eXist-db Authors'.
22+
* The original license header is included below.
23+
*
24+
* =====================================================================
25+
*
226
* eXist-db Open Source Native XML Database
327
* Copyright (C) 2001 The eXist-db Authors
428
*
@@ -226,7 +250,7 @@ private void executeXQuery(final BrokerPool pool, final DBBroker broker, final S
226250
for (final Entry param : params.entrySet()) {
227251
final String key = (String) param.getKey();
228252
final String value = (String) param.getValue();
229-
context.declareVariable(bindingPrefix + ":" + key, new StringValue(value));
253+
context.declareVariable(bindingPrefix + ":" + key, true, new StringValue(value));
230254
}
231255
}
232256

exist-core/src/main/java/org/exist/storage/serializers/XIncludeFilter.java

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,28 @@
11
/*
2+
* Elemental
3+
* Copyright (C) 2024, Evolved Binary Ltd
4+
*
5+
6+
* https://www.evolvedbinary.com | https://www.elemental.xyz
7+
*
8+
* This library is free software; you can redistribute it and/or
9+
* modify it under the terms of the GNU Lesser General Public
10+
* License as published by the Free Software Foundation; version 2.1.
11+
*
12+
* This library is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
* Lesser General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public
18+
* License along with this library; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20+
*
21+
* NOTE: Parts of this file contain code from 'The eXist-db Authors'.
22+
* The original license header is included below.
23+
*
24+
* =====================================================================
25+
*
226
* eXist-db Open Source Native XML Database
327
* Copyright (C) 2001 The eXist-db Authors
428
*
@@ -439,8 +463,8 @@ protected Optional<ResourceError> processXInclude(final String href, String xpoi
439463

440464
//TODO: change these to putting the XmldbURI in, but we need to warn users!
441465
if (document != null) {
442-
context.declareVariable("xinclude:current-doc", document.getFileURI().toString());
443-
context.declareVariable("xinclude:current-collection", document.getCollection().getURI().toString());
466+
context.declareVariable("xinclude:current-doc", true, document.getFileURI().toString());
467+
context.declareVariable("xinclude:current-collection", true, document.getCollection().getURI().toString());
444468
}
445469

446470
if (xpointer != null) {
@@ -454,7 +478,7 @@ protected Optional<ResourceError> processXInclude(final String href, String xpoi
454478
// pass parameters as variables
455479
if (params != null) {
456480
for (final Map.Entry<String, String> entry : params.entrySet()) {
457-
context.declareVariable(entry.getKey(), entry.getValue());
481+
context.declareVariable(entry.getKey(), true, entry.getValue());
458482
}
459483
}
460484

exist-core/src/main/java/org/exist/test/runner/AbstractTestRunner.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,28 @@
11
/*
2+
* Elemental
3+
* Copyright (C) 2024, Evolved Binary Ltd
4+
*
5+
6+
* https://www.evolvedbinary.com | https://www.elemental.xyz
7+
*
8+
* This library is free software; you can redistribute it and/or
9+
* modify it under the terms of the GNU Lesser General Public
10+
* License as published by the Free Software Foundation; version 2.1.
11+
*
12+
* This library is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
* Lesser General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public
18+
* License along with this library; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20+
*
21+
* NOTE: Parts of this file contain code from 'The eXist-db Authors'.
22+
* The original license header is included below.
23+
*
24+
* =====================================================================
25+
*
226
* eXist-db Open Source Native XML Database
327
* Copyright (C) 2001 The eXist-db Authors
428
*
@@ -19,7 +43,6 @@
1943
* License along with this library; if not, write to the Free Software
2044
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2145
*/
22-
2346
package org.exist.test.runner;
2447

2548
import com.evolvedbinary.j8fu.tuple.Tuple2;
@@ -94,7 +117,7 @@ protected static Sequence executeQuery(final BrokerPool brokerPool, final Source
94117
// declare variables for the query
95118
for(final Function<XQueryContext, Tuple2<String, Object>> externalVariableBinding : externalVariableBindings) {
96119
final Tuple2<String, Object> nameValue = externalVariableBinding.apply(context);
97-
context.declareVariable(nameValue._1, nameValue._2);
120+
context.declareVariable(nameValue._1, true, nameValue._2);
98121
}
99122

100123
final XQuery xqueryService = brokerPool.getXQueryService();

0 commit comments

Comments
 (0)