← Back to team overview

zorba-coders team mailing list archive

[Merge] lp:~zorba-coders/zorba/feature-xqxq_variable-value into lp:zorba

 

Matthias Brantner has proposed merging lp:~zorba-coders/zorba/feature-xqxq_variable-value into lp:zorba.

Commit message:
addex xqxq:variable-value function

Requested reviews:
  Matthias Brantner (matthias-brantner)
  Juan Zacarias (juan457)

For more details, see:
https://code.launchpad.net/~zorba-coders/zorba/feature-xqxq_variable-value/+merge/148617

addex xqxq:variable-value function
-- 
https://code.launchpad.net/~zorba-coders/zorba/feature-xqxq_variable-value/+merge/148617
Your team Zorba Coders is subscribed to branch lp:zorba.
=== modified file 'ChangeLog'
--- ChangeLog	2013-02-13 16:25:55 +0000
+++ ChangeLog	2013-02-15 06:14:24 +0000
@@ -12,6 +12,7 @@
   * In store API, added ability to specify a stream's originating URI (file)
     for streamable strings and base64Binary.
   * Added millis-to-dateTime() function in datetime module.
+  * Addex xqxq:variable-value function.
 
 Optimizations:
   * Extended optimization rules for subsequence function (pushing $startingLoc

=== modified file 'modules/xqxq/xqxq.xq'
--- modules/xqxq/xqxq.xq	2012-10-25 00:07:03 +0000
+++ modules/xqxq/xqxq.xq	2013-02-15 06:14:24 +0000
@@ -369,6 +369,24 @@
 declare %an:sequential function xqxq:delete-query($query-key as xs:anyURI) as
   empty-sequence() external;
 
+(:~
+ : This function returns the value of a variable that is bound in the
+ : given query.
+ :
+ : @param $query-key the identifier of a compiled query.
+ : @param $var-name the name of the variable whose value should be returned.
+ :
+ : @return the value bound to the given variable.
+ :
+ : @error xqxq:NoQueryMatch if no query with the given identifier
+ :   was prepared.
+ : @error xqxq:UndeclaredVariable if the given variable is not declared
+ :   in the query.
+ : @error xqxq:UnboundVariable if the given variable doesn't have a value.
+ :)
+declare function xqxq:variable-value($query-key as xs:anyURI, $var-name as 
+  xs:QName) as item()* external;
+
 
 (:~
  : Internal helper function. Only necessary because of incomplete HOF

=== modified file 'modules/xqxq/xqxq.xq.src/xqxq.cpp'
--- modules/xqxq/xqxq.xq.src/xqxq.cpp	2013-01-28 20:05:18 +0000
+++ modules/xqxq/xqxq.xq.src/xqxq.cpp	2013-02-15 06:14:24 +0000
@@ -81,6 +81,10 @@
     {
       lFunc = new DeleteQueryFunction(this);
     }
+    else if (localName == "variable-value")
+    {
+      lFunc = new VariableValueFunction(this);
+    }
   }
   
   return lFunc;
@@ -907,6 +911,64 @@
   return ItemSequence_t(new EmptySequence());
 }
 
+
+/*******************************************************************************
+
+********************************************************************************/
+zorba::ItemSequence_t VariableValueFunction::evaluate(
+    const Arguments_t& aArgs,
+    const zorba::StaticContext* aSctx,
+    const zorba::DynamicContext* aDctx) const 
+{
+  String lQueryID = XQXQFunction::getOneStringArgument(aArgs,0);
+
+  QueryMap* lQueryMap;
+  if (!(lQueryMap= dynamic_cast<QueryMap*>(aDctx->getExternalFunctionParameter("xqxqQueryMap"))))
+  {
+    throwError("NoQueryMatch", "String identifying query does not exists.");
+  }
+
+  XQuery_t lQuery = getQuery(aDctx, lQueryID);
+
+  Item lVarQName = XQXQFunction::getItemArgument(aArgs, 1);
+  bool lIsBoundVariable = false;
+
+  zorba::DynamicContext* lCtx = lQuery->getDynamicContext();
+  zorba::String lNS = lVarQName.getNamespace(), lLocal = lVarQName.getLocalName();
+
+  try
+  {
+    lIsBoundVariable = lCtx->isBoundExternalVariable(lNS, lLocal);
+  }
+  catch (ZorbaException& ze)
+  {
+    if (ze.diagnostic() == zerr::ZAPI0011_VARIABLE_NOT_DECLARED)
+      XQXQFunction::throwError("UndeclaredVariable", ze.what());  
+    throw; // should not happen
+  }
+
+  if (!lIsBoundVariable)
+  {
+    std::ostringstream lMsg;
+    lMsg << lLocal << ": variable not bound";
+    XQXQFunction::throwError("UnboundVariable", lMsg.str());  
+  }
+
+  zorba::Iterator_t lIterator;
+  zorba::Item lItem;
+
+  lCtx->getVariable(lNS, lLocal, lItem, lIterator);
+      
+  if (lIterator)
+  {
+    return ItemSequence_t(new ValueItemSequence(lIterator));
+  }
+  else
+  {
+    return ItemSequence_t(new SingletonItemSequence(lItem));
+  }
+}
+
  
 }/*namespace xqxq*/ }/*namespace zorba*/
 

=== modified file 'modules/xqxq/xqxq.xq.src/xqxq.h'
--- modules/xqxq/xqxq.xq.src/xqxq.h	2013-01-30 12:26:39 +0000
+++ modules/xqxq/xqxq.xq.src/xqxq.h	2013-02-15 06:14:24 +0000
@@ -528,6 +528,38 @@
            const zorba::DynamicContext*) const;
 };
 
+  class VariableValueFunction : public XQXQFunction{
+    protected:
+      class ValueItemSequence : public ItemSequence
+      {
+        protected:
+          Iterator_t theIterator;
+
+        public:
+          ValueItemSequence(Iterator_t& aIter)
+            : theIterator(aIter)
+          {
+          }
+
+          virtual ~ValueItemSequence(){}
+
+          Iterator_t
+          getIterator() { return theIterator; }
+
+      };
+    public:
+      VariableValueFunction(const XQXQModule* aModule) : XQXQFunction(aModule) {}
+
+      virtual ~VariableValueFunction() {}
+
+      virtual zorba::String
+        getLocalName() const {return "variable-value"; }
+
+      virtual zorba::ItemSequence_t
+        evaluate(const Arguments_t&,
+                 const zorba::StaticContext*,
+                 const zorba::DynamicContext*) const;
+  };
 
 
 }/*xqxq namespace*/}/*zorba namespace*/

=== modified file 'test/rbkt/ExpQueryResults/zorba/xqxq/is-bound-variable.xml.res'
--- test/rbkt/ExpQueryResults/zorba/xqxq/is-bound-variable.xml.res	2012-10-25 00:07:03 +0000
+++ test/rbkt/ExpQueryResults/zorba/xqxq/is-bound-variable.xml.res	2013-02-15 06:14:24 +0000
@@ -1,2 +1,2 @@
 <?xml version="1.0" encoding="UTF-8"?>
-true true false
\ No newline at end of file
+true true false foo

=== modified file 'test/rbkt/Queries/zorba/xqxq/is-bound-variable.xq'
--- test/rbkt/Queries/zorba/xqxq/is-bound-variable.xq	2012-10-25 00:07:03 +0000
+++ test/rbkt/Queries/zorba/xqxq/is-bound-variable.xq	2013-02-15 06:14:24 +0000
@@ -7,4 +7,5 @@
 xqxq:bind-variable($query-key, xs:QName('a'), "foo");
 xqxq:is-bound-variable($query-key, xs:QName('a')),
 xqxq:is-bound-variable($query-key, xs:QName('b')),
-xqxq:is-bound-variable($query-key, xs:QName('c'))
\ No newline at end of file
+xqxq:is-bound-variable($query-key, xs:QName('c')),
+xqxq:variable-value($query-key, xs:QName('a'))


Follow ups