Merge lp:~zorba-coders/zorba/bug1064978_xqxq_bind_cast_variable into lp:zorba

Proposed by Juan Zacarias
Status: Needs review
Proposed branch: lp:~zorba-coders/zorba/bug1064978_xqxq_bind_cast_variable
Merge into: lp:zorba
Diff against target: 409 lines (+249/-1)
19 files modified
include/zorba/dynamic_context.h (+15/-0)
modules/xqxq/xqxq.xq (+78/-0)
modules/xqxq/xqxq.xq.src/xqxq.cpp (+45/-0)
modules/xqxq/xqxq.xq.src/xqxq.h (+20/-0)
src/api/dynamiccontextimpl.cpp (+44/-1)
src/api/dynamiccontextimpl.h (+6/-0)
test/rbkt/ExpQueryResults/zorba/xqxq/bind-cast-variable.xml.res (+1/-0)
test/rbkt/ExpQueryResults/zorba/xqxq/bind-cast-variable2.xml.res (+1/-0)
test/rbkt/ExpQueryResults/zorba/xqxq/bind-cast-variable3.xml.res (+1/-0)
test/rbkt/ExpQueryResults/zorba/xqxq/variable-type.xml.res (+1/-0)
test/rbkt/Queries/zorba/xqxq/bind-cast-variable.xq (+4/-0)
test/rbkt/Queries/zorba/xqxq/bind-cast-variable2.xq (+4/-0)
test/rbkt/Queries/zorba/xqxq/bind-cast-variable3.xq (+4/-0)
test/rbkt/Queries/zorba/xqxq/bind-cast-variable4.spec (+1/-0)
test/rbkt/Queries/zorba/xqxq/bind-cast-variable4.xq (+4/-0)
test/rbkt/Queries/zorba/xqxq/query-plan3.xq (+1/-0)
test/rbkt/Queries/zorba/xqxq/variable-type.xq (+13/-0)
test/rbkt/Queries/zorba/xqxq/variable-type2.spec (+1/-0)
test/rbkt/Queries/zorba/xqxq/variable-type2.xq (+5/-0)
To merge this branch: bzr merge lp:~zorba-coders/zorba/bug1064978_xqxq_bind_cast_variable
Reviewer Review Type Date Requested Status
Juan Zacarias Pending
Review via email: mp+160214@code.launchpad.net

Commit message

*Implementation of Dynamic Context function getVariableType.
*Included new Function getVariableType to the dynamicContext API.
*Implementation of new xqxq functions: xqxq:variable-type
and xqxq:bind-cast-variable.

Description of the change

*Implementation of Dynamic Context function getVariableType.
*Included new Function getVariableType to the dynamicContext API.
*Implementation of new xqxq functions: xqxq:variable-type
and xqxq:bind-cast-variable.

To post a comment you must log in.
Revision history for this message
Zorba Build Bot (zorba-buildbot) wrote :
Revision history for this message
Zorba Build Bot (zorba-buildbot) wrote :

The attempt to merge lp:~zorba-coders/zorba/bug1064978_xqxq_bind_cast_variable into lp:zorba failed. Below is the output from the failed tests.

CMake Error at /home/ceej/zo/testing/zorbatest/tester/TarmacLander.cmake:275 (message):
  Validation queue job
  bug1064978_xqxq_bind_cast_variable-2013-04-22T21-34-44.988Z is finished.
  The final status was:

  2 tests did not succeed - changes not commited.

Error in read script: /home/ceej/zo/testing/zorbatest/tester/TarmacLander.cmake

Unmerged revisions

11397. By Juan Zacarias

*Implmentation of Dynamic Context function getVariableType.
*Included new Function getVariableType to the dynamicContext API.
*Implementation of new xqxq functions: xqxq:variable-type
and xqxq:bind-cast-variable.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'include/zorba/dynamic_context.h'
2--- include/zorba/dynamic_context.h 2013-03-13 16:17:38 +0000
3+++ include/zorba/dynamic_context.h 2013-04-22 21:33:28 +0000
4@@ -133,6 +133,21 @@
5 Item& aItem,
6 Iterator_t& aIterator) const = 0;
7
8+ /** \brief Returns the type of an external variable.
9+ *
10+ * The named external variable may be located in the main query or in any
11+ * modules imported directly or indirectly by the query.
12+ *
13+ * @param aNamespace the namespace URI of the variable's expanded QName
14+ * @param aLocalname the local name of the variable's expanded QName
15+ * @param outType a String representing the type of
16+ * the external variable.
17+ * @return true if the variable has been retrieved successfully, false otherwise.
18+ * @throw ZorbaException if an error occured.
19+ */
20+ virtual bool
21+ getVariableType(const String& aNamespace, const String& aLocalname, String& outType) = 0;
22+
23 /** \brief Defines the context item.
24 *
25 * @param aItem the Item that is used as value for the context item.
26
27=== modified file 'modules/xqxq/xqxq.xq'
28--- modules/xqxq/xqxq.xq 2013-04-09 14:22:41 +0000
29+++ modules/xqxq/xqxq.xq 2013-04-22 21:33:28 +0000
30@@ -25,6 +25,9 @@
31 :)
32 module namespace xqxq = 'http://www.zorba-xquery.com/modules/xqxq';
33
34+import module namespace parse-xml = "http://www.zorba-xquery.com/modules/xml";
35+import schema namespace opt = "http://www.zorba-xquery.com/modules/xml-options";
36+
37 declare namespace an = "http://www.zorba-xquery.com/annotations";
38 declare namespace ver = "http://www.zorba-xquery.com/options/versioning";
39 declare namespace op = "http://www.zorba-xquery.com/options/features";
40@@ -462,6 +465,81 @@
41 $resolver as item()?, $mapper as item()?) as xs:anyURI external;
42
43 (:~
44+ : This function returns the type of a variable that is bound in the
45+ : given query.
46+ :
47+ : @param $query-key the identifier of a compiled query.
48+ : @param $var-name the name of the variable whose value should be returned.
49+ :
50+ : @return the type of the given variable.
51+ :
52+ : @error xqxq:NoQueryMatch if no query with the given identifier
53+ : was prepared.
54+ : @error xqxq:UndeclaredVariable if the given variable is not declared
55+ : in the query.
56+ : @error xqxq:UnboundVariable if the given variable doesn't have a value.
57+ :)
58+declare function xqxq:variable-type($query-key as xs:anyURI, $var-name as
59+ xs:QName) as xs:string external;
60+
61+(:~
62+ : This function uses a string value to bind the value of an external variable
63+ : by trying to cast the string to the corresponding type defined in the query.
64+ : Currently the types supported for automatic casting are: xs:string*, xs:integer*,
65+ : xs:int*, xs:long*, xs:short*, xs:decimal*, xs:double*, xs:float*, xs:date*,
66+ : xs:time*, xs:dateTime*, object()*, array()*, document-node()* and element()*.
67+ : If the variable doesn't have a defined type xs:string is assumed.
68+ :
69+ : NOTE: if the user wants to set the variable using an item() the use of
70+ : xqxq:bind-variable is suggested.
71+ :
72+ : @param $query-key the identifier for a compiled query
73+ : @param $name the name of the external variable to bind
74+ : @param $value the sequence of strings to which the external variable $name
75+ : should be casted and set
76+ :
77+ : @return the function has side effects and returns the empty
78+ : sequence.
79+ :
80+ : @error xqxq:NoQueryMatch if no query with the given identifier
81+ : was prepared.
82+ : @error xqxq:UndeclaredVariable if the given variable is not declared
83+ : in the query.
84+ :)
85+declare %an:sequential function xqxq:bind-cast-variable($query-key as xs:anyURI, $var-name as
86+ xs:QName, $value as xs:string*) as empty-sequence()
87+{
88+ variable $type := xqxq:variable-type($query-key, $var-name);
89+ variable $unquanttype := fn:replace($type, "[*+?]$", "");
90+ variable $casted-value :=
91+ if (fn:contains($unquanttype, "object")) then
92+ for $val in $value return jn:parse-json($val)
93+ else if (fn:contains($unquanttype, "array")) then
94+ for $val in $value return jn:parse-json($val)
95+ else if (fn:contains($unquanttype, "document-node")) then
96+ for $val in $value return fn:parse-xml($val)
97+ else if (fn:contains($unquanttype, "element")) then
98+ for $val in $value return parse-xml:parse($val,
99+ <opt:options>
100+ <opt:parse-external-parsed-entity/>
101+ </opt:options>)
102+ else
103+ if ($unquanttype eq "xs:anyType")
104+ then $value
105+ else
106+ {
107+ variable $caster := xqxq:prepare-main-module(
108+ fn:concat("declare variable $val as xs:string* external; ",
109+ "for $v in $val return $v cast as ",
110+ $unquanttype));
111+ xqxq:bind-variable($caster, xs:QName("val"), $value);
112+ xqxq:evaluate($caster)
113+ };
114+
115+ xqxq:bind-variable($query-key, $var-name, $casted-value)
116+};
117+
118+(:~
119 : Internal helper function. Only necessary because of incomplete HOF
120 : support in Zorba.
121 :)
122
123=== modified file 'modules/xqxq/xqxq.xq.src/xqxq.cpp'
124--- modules/xqxq/xqxq.xq.src/xqxq.cpp 2013-04-15 12:07:41 +0000
125+++ modules/xqxq/xqxq.xq.src/xqxq.cpp 2013-04-22 21:33:28 +0000
126@@ -93,6 +93,10 @@
127 {
128 lFunc = new LoadFromQueryPlanFunction(this);
129 }
130+ else if (localName == "variable-type")
131+ {
132+ lFunc = new VariableTypeFunction(this);
133+ }
134 }
135
136 return lFunc;
137@@ -1093,6 +1097,47 @@
138 return ItemSequence_t(new SingletonItemSequence(XQXQModule::getItemFactory()->createAnyURI(lStrUUID)));
139 }
140
141+/*******************************************************************************
142+
143+ ********************************************************************************/
144+zorba::ItemSequence_t VariableTypeFunction::evaluate(
145+ const Arguments_t& aArgs,
146+ const zorba::StaticContext* aSctx,
147+ const zorba::DynamicContext* aDctx) const
148+{
149+ String lQueryID = XQXQFunction::getOneStringArgument(aArgs,0);
150+
151+ QueryMap* lQueryMap;
152+ if (!(lQueryMap= dynamic_cast<QueryMap*>(aDctx->getExternalFunctionParameter("xqxqQueryMap"))))
153+ {
154+ throwError("NoQueryMatch", "String identifying query does not exists.");
155+ }
156+
157+ XQuery_t lQuery = getQuery(aDctx, lQueryID);
158+
159+ Item lVarQName = XQXQFunction::getItemArgument(aArgs, 1);
160+
161+ zorba::DynamicContext* lCtx = lQuery->getDynamicContext();
162+ zorba::String lNS = lVarQName.getNamespace(), lLocal = lVarQName.getLocalName();
163+ zorba::String lType;
164+ try
165+ {
166+ if(!lCtx->getVariableType(lNS, lLocal, lType))
167+ {
168+ std::ostringstream lMsg;
169+ lMsg << lLocal << ": variable not bound";
170+ XQXQFunction::throwError("UndeclaredVariable", lMsg.str());
171+ }
172+ }
173+ catch (ZorbaException& ze)
174+ {
175+ if (ze.diagnostic() == zerr::ZAPI0011_VARIABLE_NOT_DECLARED)
176+ XQXQFunction::throwError("UndeclaredVariable", ze.what());
177+ throw; // should not happen
178+ }
179+ return ItemSequence_t(new SingletonItemSequence(XQXQModule::getItemFactory()->createString(lType)));
180+}
181+
182 }/*namespace xqxq*/ }/*namespace zorba*/
183
184 #ifdef WIN32
185
186=== modified file 'modules/xqxq/xqxq.xq.src/xqxq.h'
187--- modules/xqxq/xqxq.xq.src/xqxq.h 2013-04-15 12:07:41 +0000
188+++ modules/xqxq/xqxq.xq.src/xqxq.h 2013-04-22 21:33:28 +0000
189@@ -645,6 +645,26 @@
190 getURLResolver(size_t i) const { return theUrlResolvers.size() < i? NULL : theUrlResolvers[i]; }
191 };
192 };
193+
194+/*******************************************************************************
195+
196+********************************************************************************/
197+class VariableTypeFunction : public XQXQFunction
198+{
199+public:
200+ VariableTypeFunction(const XQXQModule* aModule) : XQXQFunction(aModule) {}
201+
202+ virtual ~VariableTypeFunction() {}
203+
204+ virtual zorba::String
205+ getLocalName() const {return "variable-type"; }
206+
207+ virtual zorba::ItemSequence_t
208+ evaluate(const Arguments_t&,
209+ const zorba::StaticContext*,
210+ const zorba::DynamicContext*) const;
211+};
212+
213 }/*xqxq namespace*/}/*zorba namespace*/
214
215
216
217=== modified file 'src/api/dynamiccontextimpl.cpp'
218--- src/api/dynamiccontextimpl.cpp 2013-03-13 16:17:38 +0000
219+++ src/api/dynamiccontextimpl.cpp 2013-04-22 21:33:28 +0000
220@@ -225,7 +225,50 @@
221 return false;
222 }
223
224-
225+/****************************************************************************//**
226+
227+********************************************************************************/
228+bool DynamicContextImpl::getVariableType(
229+ const String& inNamespace,
230+ const String& inLocalname,
231+ String& outType)
232+{
233+ ZORBA_DCTX_TRY
234+ {
235+ checkNoIterators();
236+
237+ const zstring& nameSpace = Unmarshaller::getInternalString(inNamespace);
238+ const zstring& localName = Unmarshaller::getInternalString(inLocalname);
239+
240+ VarInfo* var = NULL;
241+
242+ try
243+ {
244+ var = get_var_info(nameSpace, localName);
245+
246+ const XQType* varType = var->getType();
247+
248+ if (!varType)
249+ outType = "xs:anyType";
250+ else
251+ outType = varType->toSchemaString();
252+ }
253+ catch (ZorbaException const& e)
254+ {
255+ // Normally, we should be throwing an exception if the variable has not
256+ // been declared inside the xquery program, but this cases many failures
257+ // with the w3c XQTS.
258+ if (e.diagnostic() == err::XPST0008)
259+ {
260+ return false;
261+ }
262+ throw;
263+ }
264+ return true;
265+ }
266+ ZORBA_DCTX_CATCH
267+ return false;
268+}
269 /****************************************************************************//**
270
271 ********************************************************************************/
272
273=== modified file 'src/api/dynamiccontextimpl.h'
274--- src/api/dynamiccontextimpl.h 2013-03-13 16:17:38 +0000
275+++ src/api/dynamiccontextimpl.h 2013-04-22 21:33:28 +0000
276@@ -90,6 +90,12 @@
277 Item& outItem,
278 Iterator_t& outIterator) const;
279
280+ virtual bool
281+ getVariableType(
282+ const String& inNamespace,
283+ const String& inLocalname,
284+ String& outType);
285+
286 virtual bool
287 setVariable(
288 const String& inVarName,
289
290=== added file 'test/rbkt/ExpQueryResults/zorba/xqxq/bind-cast-variable.xml.res'
291--- test/rbkt/ExpQueryResults/zorba/xqxq/bind-cast-variable.xml.res 1970-01-01 00:00:00 +0000
292+++ test/rbkt/ExpQueryResults/zorba/xqxq/bind-cast-variable.xml.res 2013-04-22 21:33:28 +0000
293@@ -0,0 +1,1 @@
294+2 3
295\ No newline at end of file
296
297=== added file 'test/rbkt/ExpQueryResults/zorba/xqxq/bind-cast-variable2.xml.res'
298--- test/rbkt/ExpQueryResults/zorba/xqxq/bind-cast-variable2.xml.res 1970-01-01 00:00:00 +0000
299+++ test/rbkt/ExpQueryResults/zorba/xqxq/bind-cast-variable2.xml.res 2013-04-22 21:33:28 +0000
300@@ -0,0 +1,1 @@
301+2
302\ No newline at end of file
303
304=== added file 'test/rbkt/ExpQueryResults/zorba/xqxq/bind-cast-variable3.xml.res'
305--- test/rbkt/ExpQueryResults/zorba/xqxq/bind-cast-variable3.xml.res 1970-01-01 00:00:00 +0000
306+++ test/rbkt/ExpQueryResults/zorba/xqxq/bind-cast-variable3.xml.res 2013-04-22 21:33:28 +0000
307@@ -0,0 +1,1 @@
308+success
309\ No newline at end of file
310
311=== added file 'test/rbkt/ExpQueryResults/zorba/xqxq/variable-type.xml.res'
312--- test/rbkt/ExpQueryResults/zorba/xqxq/variable-type.xml.res 1970-01-01 00:00:00 +0000
313+++ test/rbkt/ExpQueryResults/zorba/xqxq/variable-type.xml.res 2013-04-22 21:33:28 +0000
314@@ -0,0 +1,1 @@
315+xs:integer xs:string xs:dateTime object() element(*, xs:anyType?)
316\ No newline at end of file
317
318=== added file 'test/rbkt/Queries/zorba/xqxq/bind-cast-variable.xq'
319--- test/rbkt/Queries/zorba/xqxq/bind-cast-variable.xq 1970-01-01 00:00:00 +0000
320+++ test/rbkt/Queries/zorba/xqxq/bind-cast-variable.xq 2013-04-22 21:33:28 +0000
321@@ -0,0 +1,4 @@
322+import module namespace xqxq = 'http://www.zorba-xquery.com/modules/xqxq';
323+variable $queryID := xqxq:prepare-main-module('declare variable $ext as xs:integer* external; for $i in $ext return $i+1');
324+xqxq:bind-cast-variable( $queryID, xs:QName("ext"), ('1', '2'));
325+xqxq:evaluate($queryID)
326\ No newline at end of file
327
328=== added file 'test/rbkt/Queries/zorba/xqxq/bind-cast-variable2.xq'
329--- test/rbkt/Queries/zorba/xqxq/bind-cast-variable2.xq 1970-01-01 00:00:00 +0000
330+++ test/rbkt/Queries/zorba/xqxq/bind-cast-variable2.xq 2013-04-22 21:33:28 +0000
331@@ -0,0 +1,4 @@
332+import module namespace xqxq = 'http://www.zorba-xquery.com/modules/xqxq';
333+variable $queryID := xqxq:prepare-main-module('declare variable $ext as object() external; $ext("value")+1');
334+xqxq:bind-cast-variable( $queryID, xs:QName("ext"), '{ "value" : 1}');
335+xqxq:evaluate($queryID)
336\ No newline at end of file
337
338=== added file 'test/rbkt/Queries/zorba/xqxq/bind-cast-variable3.xq'
339--- test/rbkt/Queries/zorba/xqxq/bind-cast-variable3.xq 1970-01-01 00:00:00 +0000
340+++ test/rbkt/Queries/zorba/xqxq/bind-cast-variable3.xq 2013-04-22 21:33:28 +0000
341@@ -0,0 +1,4 @@
342+import module namespace xqxq = 'http://www.zorba-xquery.com/modules/xqxq';
343+variable $queryID := xqxq:prepare-main-module('declare variable $ext as element() external; $ext//b[@id = 131]/text()');
344+xqxq:bind-cast-variable( $queryID, xs:QName("ext"), '<a><b id="131">success</b><b id="2">foo</b></a>');
345+xqxq:evaluate($queryID)
346\ No newline at end of file
347
348=== added file 'test/rbkt/Queries/zorba/xqxq/bind-cast-variable4.spec'
349--- test/rbkt/Queries/zorba/xqxq/bind-cast-variable4.spec 1970-01-01 00:00:00 +0000
350+++ test/rbkt/Queries/zorba/xqxq/bind-cast-variable4.spec 2013-04-22 21:33:28 +0000
351@@ -0,0 +1,1 @@
352+Error: http://www.zorba-xquery.com/modules/xqxq:UndeclaredVariable
353\ No newline at end of file
354
355=== added file 'test/rbkt/Queries/zorba/xqxq/bind-cast-variable4.xq'
356--- test/rbkt/Queries/zorba/xqxq/bind-cast-variable4.xq 1970-01-01 00:00:00 +0000
357+++ test/rbkt/Queries/zorba/xqxq/bind-cast-variable4.xq 2013-04-22 21:33:28 +0000
358@@ -0,0 +1,4 @@
359+import module namespace xqxq = 'http://www.zorba-xquery.com/modules/xqxq';
360+variable $queryID := xqxq:prepare-main-module('declare variable $ext external; $ext');
361+xqxq:bind-cast-variable( $queryID, xs:QName("ext2"), '2');
362+xqxq:evaluate($queryID)
363\ No newline at end of file
364
365=== modified file 'test/rbkt/Queries/zorba/xqxq/query-plan3.xq'
366--- test/rbkt/Queries/zorba/xqxq/query-plan3.xq 2013-03-22 23:09:40 +0000
367+++ test/rbkt/Queries/zorba/xqxq/query-plan3.xq 2013-04-22 21:33:28 +0000
368@@ -26,3 +26,4 @@
369 variable $query-plan := xqxq:query-plan($queryID);
370 variable $queryID2 := xqxq:load-from-query-plan($query-plan, resolver:url-resolver#2, mapper:uri-mapper#2);
371 xqxq:evaluate ($queryID2)
372+
373
374=== added file 'test/rbkt/Queries/zorba/xqxq/variable-type.xq'
375--- test/rbkt/Queries/zorba/xqxq/variable-type.xq 1970-01-01 00:00:00 +0000
376+++ test/rbkt/Queries/zorba/xqxq/variable-type.xq 2013-04-22 21:33:28 +0000
377@@ -0,0 +1,13 @@
378+import module namespace xqxq = 'http://www.zorba-xquery.com/modules/xqxq';
379+
380+variable $queryID := xqxq:prepare-main-module('declare variable $ext as xs:integer external; $ext');
381+variable $queryID2 := xqxq:prepare-main-module('declare variable $ext as xs:string external; $ext');
382+variable $queryID3 := xqxq:prepare-main-module('declare variable $ext as xs:dateTime external; $ext');
383+variable $queryID4 := xqxq:prepare-main-module('declare variable $ext as object() external; $ext');
384+variable $queryID5 := xqxq:prepare-main-module('declare variable $ext as element() external; $ext');
385+
386+xqxq:variable-type($queryID, xs:QName('ext')),
387+xqxq:variable-type($queryID2, xs:QName('ext')),
388+xqxq:variable-type($queryID3, xs:QName('ext')),
389+xqxq:variable-type($queryID4, xs:QName('ext')),
390+xqxq:variable-type($queryID5, xs:QName('ext'))
391\ No newline at end of file
392
393=== added file 'test/rbkt/Queries/zorba/xqxq/variable-type2.spec'
394--- test/rbkt/Queries/zorba/xqxq/variable-type2.spec 1970-01-01 00:00:00 +0000
395+++ test/rbkt/Queries/zorba/xqxq/variable-type2.spec 2013-04-22 21:33:28 +0000
396@@ -0,0 +1,1 @@
397+Error: http://www.zorba-xquery.com/modules/xqxq:UndeclaredVariable
398\ No newline at end of file
399
400=== added file 'test/rbkt/Queries/zorba/xqxq/variable-type2.xq'
401--- test/rbkt/Queries/zorba/xqxq/variable-type2.xq 1970-01-01 00:00:00 +0000
402+++ test/rbkt/Queries/zorba/xqxq/variable-type2.xq 2013-04-22 21:33:28 +0000
403@@ -0,0 +1,5 @@
404+import module namespace xqxq = 'http://www.zorba-xquery.com/modules/xqxq';
405+
406+variable $queryID := xqxq:prepare-main-module('declare variable $ext as xs:integer external; $ext');
407+
408+xqxq:variable-type($queryID, xs:QName('ext2'))
409\ No newline at end of file

Subscribers

People subscribed via source and target branches