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 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_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 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; }