// test for bug #3085093 "ZorbaError not caught in SerializerImpl::serialize" bool test_serialization_error(Zorba* aZorba) { XQuery_t lQuery = aZorba->compileQuery("<a a='1'/>/@a"); Zorba_SerializerOptions_t lOptions; Serializer_t lSerializer = Serializer::createSerializer(lOptions); Iterator_t lIterator = lQuery->iterator(); try { lIterator->open(); Item lItem; while ( lIterator->next(lItem) ) { // we have to wrap the item in a Serializable object SingletonItemSequence lSequence(lItem); lSerializer->serialize(&lSequence, std::cout); } lIterator->close(); } catch (ZorbaException& e) { std::cerr << e << std::endl; lIterator->close(); return true; } return false; }
int sax2( int argc, char * argv[] ) { // create a SAX content handler that prints all events to standard out XMLSerializer lContentHandler( std::cout ); void* lStore = zorba::StoreManager::getStore(); // initialize the Zorba engine and get a pointer to it Zorba* lZorba = Zorba::getInstance(lStore); try { // compile a query XQuery_t lQuery = lZorba->compileQuery("<a xmlns:f=\"foo\" xmlns=\"http://zorba.io/defaultns\"> text a text a <b xmlns:ns1=\"http://zorba.io/usecase1\" attr1=\"value1\" attr2=\"value2\"> text b </b><f:bar>foo</f:bar><foo /><bar /><b><![CDATA[ foo ]]></b></a>"); // register the content handler created above lQuery->registerSAXHandler( &lContentHandler ); // execute the query and call according SAX callbacks // i.e. equivalent to serializing to xml and parsing using SAX). lQuery->executeSAX(); } catch ( ZorbaException &e ) { std::cerr << e << std::endl; } lZorba->shutdown(); zorba::StoreManager::shutdownStore(lStore); return 0; }
bool cxx_api_changes_test2 (Zorba* aZorba) { try { //test the use of the isSequential function in the c++ API std::ifstream lIn("cxx_api_ch2.xq"); assert(lIn.good()); XQuery_t lQuery = aZorba->compileQuery(lIn); if(lQuery->isSequential()) return false; } catch (XQueryException& qe) { std::cerr << qe << std::endl; return false; } catch (ZorbaException& e) { std::cerr << e << std::endl; return false; } return true; }
/** * Test accessing a JSONArray's members */ bool example_1(Zorba* aZorba) { Iterator_t lIterator; XQuery_t lQuery = aZorba->compileQuery("[ 1, 2, 3 ]"); lIterator = lQuery->iterator(); lIterator->open(); Item lItem; lIterator->next(lItem); // Ensure we got a JSON array if (!lItem.isJSONItem() || lItem.getJSONItemKind() != store::StoreConsts::jsonArray) { std::cerr << "Item is not JSON Array!" << std::endl; return false; } // Ensure array has 3 integer members uint64_t lSize = lItem.getArraySize(); if (lSize != 3) { std::cerr << lSize << " array members returned, expecting 3" << std::endl; return false; } for(uint64_t i = 1; i <= lSize; ++i) { Item lMember = lItem.getArrayValue(i); // This will throw an exception if the item isn't an integer std::cout << lMember.getLongValue() << std::endl; } lIterator->close(); 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 cxx_api_changes_test5(Zorba* aZorba) { try { std::string lIn = "declare variable $a external; 1+1"; XQuery_t lQuery = aZorba->compileQuery(lIn); std::vector<Item> lVars; Iterator_t varsIte; lQuery->getExternalVariables(varsIte); varsIte->open(); Item temp; while(varsIte->next(temp)) lVars.push_back(temp); varsIte->close(); std::vector<Item>::const_iterator lIte = lVars.begin(); std::vector<Item>::const_iterator lEnd = lVars.end(); Item item = aZorba->getItemFactory()->createInt(4); bool isBound1; bool isBound2; for(; lIte != lEnd; ++lIte) { Item qname = *lIte; isBound1 = lQuery->getDynamicContext()->isBoundExternalVariable(qname.getNamespace(), qname.getLocalName()); Item value = aZorba->getItemFactory()->createString("foo"); lQuery->getDynamicContext()->setVariable(qname.getStringValue(), value); isBound2 = lQuery->getDynamicContext()->isBoundExternalVariable(qname.getNamespace(), qname.getLocalName()); } if (!isBound1 && isBound2) return true; } catch (XQueryException& qe) { std::cerr << qe << std::endl; return false; } catch (ZorbaException& e) { std::cerr << e << std::endl; return false; } return true; }
bool cxx_api_changes_test3(Zorba* aZorba) { try { //tests the use of getExternalVariables std::ifstream lIn("cxx_api_ch3.xq"); assert(lIn.good()); XQuery_t lQuery = aZorba->compileQuery(lIn); 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() != 2) { std::cout << "Expected 2 variables but got " << lVars.size() << std::endl; return false; } std::vector<Item>::iterator lIte = lVars.begin(); std::vector<Item>::iterator lEnd = lVars.end(); for (; lIte != lEnd; ++lIte) { String name = lIte->getStringValue(); if (name != "a" && name != "b") return false; } } catch (XQueryException& qe) { std::cerr << qe << std::endl; return false; } catch (ZorbaException& e) { std::cerr << e << std::endl; return false; } return true; }
/** * Example to invoke the serializer using the serialize() call on the query object. */ bool serialization_example_2(Zorba* aZorba) { try { XQuery_t lQuery = aZorba->compileQuery("for $i in (1 to 3) return <a> { $i } </a>"); lQuery->execute(std::cout); } catch (ZorbaException& e) { std::cerr << e << std::endl; return false; } return true; }
bool example1() { std::stringstream lStr; lStr << "(<anXMLExample/>, "; lStr << "<json type='object'><pair name='firstName' type='string'>"; lStr << "John</pair></json>, "; lStr << "42)"; XQuery_t lQuery = theZorba->compileQuery(lStr.str()); try { lQuery->execute(std::cout, callback, this); } catch (ZorbaException& e) { std::cerr << e << std::endl; return false; } return true; }
/** * Example to serialize the result of a query with indentation. */ bool serialization_example_4(Zorba* aZorba) { try { XQuery_t lQuery = aZorba->compileQuery("for $i in (1 to 3) return <a> { $i } </a>"); Zorba_SerializerOptions lSerOptions; lSerOptions.indent = ZORBA_INDENT_YES; lQuery->execute(std::cout, &lSerOptions); } catch (ZorbaException& e) { std::cerr << e << std::endl; return false; } return true; }
/** * Example that shows HTML serialization of the results. */ bool serialization_example_3(Zorba* aZorba) { try { XQuery_t lQuery = aZorba->compileQuery("for $i in (1 to 3) return <a> { $i } </a>"); Zorba_SerializerOptions lSerOptions; lSerOptions.ser_method = ZORBA_SERIALIZATION_METHOD_HTML; lQuery->execute(std::cout, &lSerOptions); } catch (ZorbaException& e) { std::cerr << e << std::endl; return false; } return true; }
bool serialization_example_6(Zorba* aZorba) { try { XQuery_t lQuery = aZorba->compileQuery("for $i in (1 to 3) return <a> { $i } </a>"); Zorba_SerializerOptions lSerOptions; lSerOptions.byte_order_mark = ZORBA_BYTE_ORDER_MARK_YES; lQuery->execute(std::cout, &lSerOptions); } catch (ZorbaException& e) { std::cerr << e << std::endl; return false; } return true; }
bool serialization_example_5(Zorba* aZorba) { try { XQuery_t lQuery = aZorba->compileQuery("for $i in (1 to 3) return <a> { $i } </a>"); Zorba_SerializerOptions lSerOptions; lSerOptions.omit_xml_declaration = ZORBA_OMIT_XML_DECLARATION_YES; lQuery->execute(std::cout, &lSerOptions); } catch (ZorbaException& e) { std::cerr << e << std::endl; return false; } return true; }
bool example_2(Zorba* aZorba) { XQuery_t lQuery = aZorba->createQuery(); std::string path( "/home/adam/proj/marc2bibframe/xbin/zorba3-0.xqy"); std::unique_ptr<std::istream> qfile; qfile.reset( new std::ifstream( path.c_str() ) ); Zorba_CompilerHints lHints; // lQuery->setFileName("http://base/"); lQuery->setFileName("/home/adam/proj/marc2bibframe/xbin/"); lQuery->compile(*qfile, lHints); zorba::DynamicContext* lDynamicContext = lQuery->getDynamicContext(); zorba::Item lItem = aZorba->getItemFactory()->createString("http://base/"); lDynamicContext->setVariable("baseuri", lItem); lItem = aZorba->getItemFactory()->createString( "/home/adam/proj/yaz/test/marc6.xml"); lDynamicContext->setVariable("marcxmluri", lItem); lItem = aZorba->getItemFactory()->createString("rdfxml"); lDynamicContext->setVariable("serialization", lItem); std::cout << lQuery << std::endl; lItem = aZorba->getItemFactory()->createString( "/home/adam/proj/yaz/test/marc7.xml"); lDynamicContext->setVariable("marcxmluri", lItem); std::stringstream ss; lQuery->execute(ss); std::string result = ss.str(); std::cout << result << std::endl; return true; }
/** * Test accessing a non-existent JSONObject pair value */ bool example_5(Zorba* aZorba) { Iterator_t lIterator; XQuery_t lQuery = aZorba->compileQuery("{ \"one\" : 1, \"two\" : 2 }"); lIterator = lQuery->iterator(); lIterator->open(); Item lItem; lIterator->next(lItem); Item lNonValue = lItem.getObjectValue("three"); lIterator->close(); if (!lNonValue.isNull()) { return false; } return true; }
bool error_example_4(Zorba* aZorba) { MyDiagnosticHandler lHandler; try { // move this outside if constant folding is fixed XQuery_t lQuery = aZorba->compileQuery("1 div 0"); lQuery->registerDiagnosticHandler(&lHandler); std::cout << lQuery << std::endl; } catch (ZorbaException const& e) { std::cerr << e << std::endl; if ( e.diagnostic().kind() == diagnostic::XQUERY_DYNAMIC ) return true; } return false; }
/** * Test accessing a non-existent JSONArray member */ bool example_4(Zorba* aZorba) { Iterator_t lIterator, lMembers; XQuery_t lQuery = aZorba->compileQuery("[ 1, 2, 3 ]"); lIterator = lQuery->iterator(); lIterator->open(); Item lItem; lIterator->next(lItem); Item lNonMember = lItem.getArrayValue(4); lIterator->close(); if (!lNonMember.isNull()) { return false; } return true; }
/** * Test accessing a JSONObject's values directly by name */ bool example_3(Zorba* aZorba) { Iterator_t lIterator; XQuery_t lQuery = aZorba->compileQuery("{ \"one\" : 1, \"two\" : 2 }"); lIterator = lQuery->iterator(); lIterator->open(); Item lItem; lIterator->next(lItem); Item lValue; lValue = lItem.getObjectValue("one"); std::cout << lValue.getLongValue() << std::endl; lValue = lItem.getObjectValue("two"); std::cout << lValue.getLongValue() << std::endl; lIterator->close(); return true; }
/** * Test accessing a JSONArray's members directly by index */ bool example_2(Zorba* aZorba) { Iterator_t lIterator; XQuery_t lQuery = aZorba->compileQuery("[ 1, 2, 3 ]"); lIterator = lQuery->iterator(); lIterator->open(); Item lItem; lIterator->next(lItem); Item lMember; for (int i = 1; i <= 3; i++) { lMember = lItem.getArrayValue(i); std::cout << lMember.getLongValue() << std::endl; } lIterator->close(); return true; }
/** * Utility function: Given an item, bind that item to a simple query and * serialize the result, then ensure the result matches the expected string. */ bool serialize(Zorba* aZorba, Item aItem, std::string aExpected) { Zorba_SerializerOptions lSerialOpt; lSerialOpt.omit_xml_declaration = ZORBA_OMIT_XML_DECLARATION_YES; XQuery_t lQuery = aZorba->compileQuery("declare variable $i external; $i"); lQuery->getDynamicContext()->setVariable("i", aItem); std::stringstream lStream; lQuery->execute(lStream, &lSerialOpt); std::string lResult = lStream.str(); std::cout << lResult << std::endl; if (lResult.compare(aExpected) != 0) { std::cout << "Wrong value! Expected " << aExpected << std::endl; return false; } return true; }
void* query_stress_test_1(void *param) { argv* var = (argv*)param; std::ostringstream os; os << var->index; XQuery_t aQuery = var->lZorba->compileQuery(".//chapter[@id=" + os.str() + "]"); DynamicContext* lCtx = aQuery->getDynamicContext(); lCtx->setContextItem(var->lItem); os.clear(); os << aQuery << std::endl; var->lItem = NULL; aQuery->close(); return (void*)0; }
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 cxx_api_changes_test6(Zorba* aZorba) { try { std::string lIn = "1+1"; XQuery_t lQuery = aZorba->compileQuery(lIn); bool isBound1; bool isBound2; isBound1 = lQuery->getDynamicContext()->isBoundContextItem(); Item lContextItem = aZorba->getItemFactory()->createString("foo"); lQuery->getDynamicContext()->setContextItem(lContextItem); isBound2 = lQuery->getDynamicContext()->isBoundContextItem(); if (!isBound1 && isBound2) return true; } 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_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; }