chaining.cpp
Example to show query chaining at work. In this example, we will execute query 1 and chain the results of that query as in input to another query (query 2) by means of an external variable.
#include <iostream>
#include <fstream>
#include <string>
#include <zorba/zorba.h>
#include <zorba/iterator.h>
#include <zorba/diagnostic_handler.h>
#include <zorba/store_manager.h>
#include <zorba/zorba_exception.h>
using namespace zorba;
bool
chaining_example_1(Zorba* aZorba)
{
try {
XQuery_t lQuery1 = aZorba->compileQuery("for $i in (1 to 20) return $i");
Iterator_t lIterator = lQuery1->iterator();
XQuery_t lQuery2 = aZorba->compileQuery("declare variable $x external; for $i in $x return $i * $i");
DynamicContext* lCtx = lQuery2->getDynamicContext();
lCtx->setVariable("x", lIterator);
std::cout << lQuery2 << std::endl;
} catch (ZorbaException &e) {
std::cerr << e << std::endl;
return false;
}
return true;
}
class MyChainingDiagnosticHandler : public DiagnosticHandler
{
public:
MyChainingDiagnosticHandler(const std::string& aIdentifier)
: theIdentifier(aIdentifier) {}
void error(const ZorbaException& e)
{
std::cerr << "error handler: " << theIdentifier << " error " << e << std::endl;
}
void warning(const XQueryWarning& w)
{
std::cerr << "warning handler: " << theIdentifier << " warning " << w << std::endl;
}
protected:
std::string theIdentifier;
};
bool
chaining_example_2(Zorba* aZorba)
{
MyChainingDiagnosticHandler lDiagnosticHandler1("handler 1");
MyChainingDiagnosticHandler lDiagnosticHandler2("handler 2");
XQuery_t lQuery1 = aZorba->compileQuery("let $i := (1 to 42) return $i * $i", &lDiagnosticHandler1);
Iterator_t lIterator = lQuery1->iterator();
XQuery_t lQuery2 = aZorba->compileQuery("declare variable $x external; for $i in $x return $i * $i", &lDiagnosticHandler2);
DynamicContext* lCtx = lQuery2->getDynamicContext();
lCtx->setVariable("x", lIterator);
std::cout << lQuery2 << std::endl;
return true;
}
int
chaining(int argc, char* argv[])
{
void* lStore = zorba::StoreManager::getStore();
Zorba* lZorba = Zorba::getInstance(lStore);
bool res = false;
std::cout << "executing example 1" << std::endl;
res = chaining_example_1(lZorba);
if (!res) return 1;
std::cout << std::endl;
std::cout << "executing example 2" << std::endl;
res = chaining_example_2(lZorba);
if (!res) return 1;
std::cout << std::endl;
lZorba->shutdown();
zorba::StoreManager::shutdownStore(lStore);
return 0;
}