void setPathsOnContext( const ZorbaCMDProperties& aProperties, StaticContext_t& aStaticCtx) { std::vector<String> lPath; std::string lPathStr; // Compute the current working directory to append to all paths. filesystem_path lCWD; // setModulePaths() *overwrites* the URI path and lib path, so there's no // sense in calling both. So if --module-path exists, just use it. aProperties.getModulePath(lPathStr); if (lPathStr.length() > 0) { tokenizePath(lPathStr, lPath); lPath.push_back(lCWD.get_path()); aStaticCtx->setModulePaths(lPath); } else { // Compute and set URI path aProperties.getURIPath(lPathStr); tokenizePath(lPathStr, lPath); lPath.push_back(lCWD.get_path()); aStaticCtx->setURIPath(lPath); lPath.clear(); // Compute and set lib path aProperties.getLibPath(lPathStr); tokenizePath(lPathStr, lPath); lPath.push_back(lCWD.get_path()); aStaticCtx->setLibPath(lPath); } }
bool error_example_6(Zorba* aZorba) { try { Item lQName = aZorba->getItemFactory()->createQName( "http://zorba.io/options/warnings", "", "error"); // make sure that the warning zwarn::ZWST0002 is turned // into an error StaticContext_t lCtx = aZorba->createStaticContext(); lCtx->declareOption(lQName, "ZWST0003"); std::ostringstream s; s << "declare namespace z = 'http://zorba.io/annotations';" << std::endl << "declare %z:sequential function local:foo() { 1 };" << std::endl << "local:foo()" << std::endl; XQuery_t lQuery = aZorba->compileQuery(s.str(), lCtx); std::cout << lQuery << std::endl; } catch (const XQueryException&) { return true; } catch (const ZorbaException& ze) { std::cerr << ze << std::endl; return false; } return false; }
bool external_function_test_4(Zorba* aZorba) { try { std::ifstream lIn("ext_main3.xq"); assert(lIn.good()); std::ostringstream lOut; MyExternalModule lMod; StaticContext_t lSctx = aZorba->createStaticContext(); lSctx->registerModule(&lMod); { XQuery_t lQuery = aZorba->compileQuery(lIn, lSctx); std::cout << lQuery << std::endl; } } catch (XQueryException& qe) { std::cerr << qe << std::endl; return false; } catch (ZorbaException& e) { std::cerr << e << std::endl; return false; } catch (...) { return false; } return true; }
bool cxx_api_changes_test4(Zorba* aZorba) { try { std::ifstream lIn("cxx_api_ch4.xq"); assert(lIn.good()); StaticContext_t lStaticContext = aZorba->createStaticContext(); std::vector<String> lModulePaths; lModulePaths.push_back(zorba::CMAKE_BINARY_DIR+"/TEST_URI_PATH/"); lStaticContext->setModulePaths(lModulePaths); XQuery_t lQuery = aZorba->compileQuery(lIn, lStaticContext); std::vector<Item> lVars; Iterator_t varsIte; lQuery->getExternalVariables(varsIte); varsIte->open(); Item temp; while(varsIte->next(temp)) lVars.push_back(temp); varsIte->close(); if (lVars.size() != 3) return false; std::ostringstream lOut; std::vector<Item>::const_iterator lIte = lVars.begin(); std::vector<Item>::const_iterator lEnd = lVars.end(); for (; lIte != lEnd; ++lIte) { lOut << lIte->getStringValue() << " "; } std::string out = lOut.str(); if (out != "testGetExtVarA:ext a testGetExtVarB:ext " && out != "testGetExtVarB:ext a testGetExtVarA:ext " && out != "a testGetExtVarA:ext testGetExtVarB:ext " && out != "a testGetExtVarB:ext testGetExtVarA:ext " && out != "testGetExtVarA:ext testGetExtVarB:ext a " && out != "testGetExtVarB:ext testGetExtVarA:ext a ") return false; } catch (XQueryException& qe) { std::cerr << qe << std::endl; return false; } catch (ZorbaException& e) { std::cerr << e << std::endl; return false; } return true; }
bool external_function_test_2(Zorba* aZorba) { try { std::ifstream lIn("ext_main.xq"); assert(lIn.good()); std::ostringstream lOut; MyExternalModule lMod; StaticContext_t lSctx = aZorba->createStaticContext(); lSctx->registerModule(&lMod); { XQuery_t lQuery = aZorba->compileQuery(lIn, lSctx); zorba::DynamicContext* lDynContext = lQuery->getDynamicContext(); // must be released in MyExternalFunctionParameter::destroy MyExternalFunctionParameter* lParam1 = new MyExternalFunctionParameter(); MyExternalFunctionParameter* lParam2 = new MyExternalFunctionParameter(); lDynContext->addExternalFunctionParameter("myparam", lParam1); lDynContext->addExternalFunctionParameter("myparam", lParam2); // make sure that destroy is invoked if the first parameter is overwritten if (!lDestroyedParam) { return false; } else { lDestroyedParam = false; } lDynContext->setVariable("local:foo", aZorba->getItemFactory()->createString("foo")); std::cout << lQuery << std::endl; } // destroy is called if the XQuery object is destroyed return lGotParam && lDestroyedParam; } catch (XQueryException& qe) { std::cerr << qe << std::endl; return false; } catch (ZorbaException& e) { std::cerr << e << std::endl; return false; } return true; }
bool execution_plan_example_4(Zorba* aZorba) { StaticContext_t lContext = aZorba->createStaticContext(); Zorba_CompilerHints_t lHints; std::stringstream lPredeclaredModules; lPredeclaredModules << "import module namespace libjn = " << "'http://jsoniq.org/function-library';" << std::endl; lContext->loadProlog(lPredeclaredModules.str(), lHints); std::vector<zorba::String> lDefaultNS; lDefaultNS.push_back("http://jsoniq.org/functions"); lContext->setDefaultFunctionNamespaces(lDefaultNS); // the stringstream used for query materialization std::stringstream lExecutionPlan; // materialize a compiled query to a binary format { XQuery_t lQuery = aZorba->compileQuery("jn:encode-for-roundtrip(xs:dateTime('2014-05-21T00:00:01'))", lContext); lQuery->saveExecutionPlan(lExecutionPlan); std::cout << lQuery << std::endl; } // read a compiled query from an input stream // and execute it { XQuery_t lQuery = aZorba->createQuery(); lQuery->loadExecutionPlan(lExecutionPlan); std::cout << lQuery << std::endl; } return true; }
bool execution_plan_example_3(Zorba* aZorba) { // the stringstream used for query materialization std::stringstream lExecutionPlan; { MySerializableExternalModule lModule; // materialize a compiled query to a binary format StaticContext_t sctx = aZorba->createStaticContext(); sctx->registerModule(&lModule); std::ostringstream lText; lText << "declare namespace foo=\"urn:foo\";" << std::endl << "declare function foo:bar1($a1, $a2) external;" << std::endl << "foo:bar1((1,2,3), (4,5,6))" << std::endl; XQuery_t lQuery = aZorba->compileQuery(lText.str(), sctx); lQuery->saveExecutionPlan(lExecutionPlan); std::cout << lQuery << std::endl; } // read a compiled query from an input stream // but forgot to register a SerializationCallback try { XQuery_t lQuery = aZorba->createQuery(); lQuery->loadExecutionPlan(lExecutionPlan); std::cout << lQuery << std::endl; } catch (ZorbaException &e) { std::cerr << e << std::endl; return true; } return false; }
bool example1(Zorba* aZorba) { StaticContext_t lStcxt = aZorba->createStaticContext(); FileModuleDisallow lChecker; lStcxt->registerURIMapper(&lChecker); std::stringstream lStream; lStream << "import module namespace file = 'http://www.zorba-xquery.com/modules/file';"; lStream << std::endl << std::endl; lStream << "file:files('.')" << std::endl; try { XQuery_t lQuery = aZorba->compileQuery(lStream, lStcxt); std::cout << lQuery << std::endl; } catch (ZorbaException &e) { if (e.diagnostic() == zerr::ZXQP0029_URI_ACCESS_DENIED) { return true; } return false; } catch (...) { return false; } return false; }
bool external_function_test_5(Zorba* aZorba) { try { std::ifstream lIn("ext_main4.xq"); assert(lIn.good()); std::ostringstream lOut; MyExternalModule lMod; StaticContext_t lSctx = aZorba->createStaticContext(); lSctx->registerModule(&lMod); { XQuery_t lQuery = aZorba->compileQuery(lIn, lSctx); std::cout << lQuery << std::endl; } } catch (XQueryException& qe) { if (std::string("ZXQP0001") == qe.diagnostic().qname().localname()) { std::cerr << qe << std::endl; return true; } else { std::cerr << qe << std::endl; return false; } } catch (...) { return false; } return false; }
bool external_function_test_1(Zorba* aZorba) { try { // test the sausalito use case // serialize a query and afterwards execute it // by calling a dynamic function (i.e. using eval) { std::ifstream lIn("ext_main.xq"); assert(lIn.good()); std::ostringstream lOut; MyExternalModule lMod; { StaticContext_t lSctx = aZorba->createStaticContext(); lSctx->registerModule(&lMod); XQuery_t lQuery = aZorba->compileQuery(lIn, lSctx); lQuery->saveExecutionPlan(lOut); zorba::DynamicContext* lDynContext = lQuery->getDynamicContext(); lDynContext->setVariable("local:foo", aZorba->getItemFactory()->createString("foo")); // make sure constant folding doesn't happen, i.e. the function is not evaluated if (lCalled) { return 1; } // evaluate the function and check if it was really called std::cout << lQuery << std::endl; if (!lCalled) { return 2; } } { MySerializationCallback lCallback; // load the query saved above // this tests if, when loaded, the functions of the static context // that have not yet been compiled, can be compiled properly std::istringstream lIn(lOut.str()); XQuery_t lQuery = aZorba->createQuery(); lQuery->loadExecutionPlan(lIn, &lCallback); // set the parameter for evaluating a different dynamic function then // in the test above zorba::DynamicContext* lDynContext = lQuery->getDynamicContext(); lDynContext->setVariable("local:foo", aZorba->getItemFactory()->createString("foo2")); // evaluate the query std::cout << lQuery << std::endl; } } } catch (XQueryException& qe) { std::cerr << qe << std::endl; return false; } catch (ZorbaException& e) { std::cerr << e << std::endl; return false; } return true; }