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 execution_plan_example_1(Zorba* aZorba) { // the stringstream used for query materialization std::stringstream lExecutionPlan; // materialize a compiled query to a binary format { XQuery_t lQuery = aZorba->compileQuery("1+2"); 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 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; }