public boolean runPriceQuery(String symbol) throws XQException { String query = "declare namespace s=\"http://tpox-benchmark.com/security\";\n" + "declare variable $sym external;\n" + "for $sec in fn:collection(\"CLN_Security\")/s:Security\n" + "where $sec/s:Symbol=$sym\n" + "return\n" + "\t<print>The open price of the security \"{$sec/s:Name/text()}\" is {$sec/s:Price/s:PriceToday/s:Open/text()} dollars</print>\n"; XQPreparedExpression xqpe = xqc.prepareExpression(query); xqpe.bindString(new QName("sym"), symbol, null); //IBM; VFINX; PTTAX XQResultSequence xqs = xqpe.executeQuery(); boolean found = false; while (xqs.next()) { System.out.println(xqs.getItemAsString(null)); found = true; } return found; }
public boolean runSecQuery(String symbol) throws XQException { String query = "declare namespace s=\"http://tpox-benchmark.com/security\";\n" + "declare variable $sym external;\n" + "for $sec in fn:collection(\"CLN_Security\")/s:Security\n" + "where $sec/s:Symbol=$sym\n" + "return $sec\n"; XQPreparedExpression xqpe = xqc.prepareExpression(query); xqpe.bindString(new QName("sym"), symbol, null); XQResultSequence xqs = xqpe.executeQuery(); boolean found = false; while (xqs.next()) { System.out.println(xqs.getItemAsString(null)); found = true; } return found; }
public boolean searchSecQuery() throws XQException { String query = "declare default element namespace \"http://tpox-benchmark.com/security\";\n" + "for $sec in fn:collection(\"CLN_Security\")/s:Security\n" + "where $sec[SecurityInformation/*/Sector = 'Technology' and PE[. >= xs:decimal('25') and . < xs:decimal('28.0')] and Yield > xs:decimal('0')]\n" + "return <Security>\n" + "\t{$sec/Symbol}\n" + "\t{$sec/Name}\n" + "\t{$sec/SecurityType}\n" + "\t{$sec/SecurityInformation//Sector}\n" + "\t{$sec/PE}\n" + "\t{$sec/Yield}\n" + "</Security>"; XQExpression xqe = xqc.createExpression(); XQResultSequence xqs = xqe.executeQuery(query); int cnt = 0; while (xqs.next()) { cnt++; } System.out.println("Got " + cnt + " results"); return cnt > 0; }
@Override public String readDocument(String uri) throws XQException { String query = "declare namespace bgdb=\"http://bagridb.com/bdb\";\n" + "declare variable $uri external;\n" + "let $doc := bgdb:get-document-content($uri)\n" + "return $doc\n"; XQPreparedExpression xqpe = xqConn.prepareExpression(query); xqpe.bindString(new QName("uri"), uri, xqConn.createAtomicType(XQItemType.XQBASETYPE_ANYURI)); XQResultSequence xqs = xqpe.executeQuery(); String result = null; if (xqs.next()) { result = xqs.getItemAsString(null); } return result; }
public boolean runPriceQuery() throws XQException { String query = "declare namespace s=\"http://tpox-benchmark.com/security\";\n" + "declare variable $sym external;\n" + "for $sec in fn:collection(\"/{http://tpox-benchmark.com/security}Security\")/s:Security\n" + "where $sec/s:Symbol=$sym\n" + //'IBM'\n" + "return\n" + "\t<print>The open price of the security \"{$sec/s:Name/text()}\" is {$sec/s:Price/s:PriceToday/s:Open/text()} dollars</print>\n"; XQPreparedExpression xqpe = xqc.prepareExpression(query); xqpe.bindString(new QName("sym"), "IBM", null); //IBM; VFINX; PTTAX XQResultSequence xqs = xqpe.executeQuery(); boolean found = false; while (xqs.next()) { System.out.println(xqs.getItemAsString(null)); found = true; } return found; }
public boolean runSecQuery() throws XQException { String query = "declare namespace s=\"http://tpox-benchmark.com/security\";\n" + "declare variable $sym external;\n" + "for $sec in fn:collection(\"/{http://tpox-benchmark.com/security}Security\")/s:Security\n" + "where $sec/s:Symbol=$sym\n" + //'IBM'\n" + "return $sec\n"; XQPreparedExpression xqpe = xqc.prepareExpression(query); xqpe.bindString(new QName("sym"), "IBM", null); XQResultSequence xqs = xqpe.executeQuery(); boolean found = false; while (xqs.next()) { System.out.println(xqs.getItemAsString(null)); found = true; } return found; }
public boolean searchSecQuery() throws XQException { String query = "declare default element namespace \"http://tpox-benchmark.com/security\";\n" + "for $sec in fn:collection(\"/{http://tpox-benchmark.com/security}Security\")/Security\n" + "where $sec[SecurityInformation/*/Sector = 'Technology' and PE[. >= xs:decimal('25') and . < xs:decimal('28.0')] and Yield > xs:decimal('0')]\n" + "return <Security>\n" + "\t{$sec/Symbol}\n" + "\t{$sec/Name}\n" + "\t{$sec/SecurityType}\n" + "\t{$sec/SecurityInformation//Sector}\n" + "\t{$sec/PE}\n" + "\t{$sec/Yield}\n" + "</Security>"; XQExpression xqe = xqc.createExpression(); XQResultSequence xqs = xqe.executeQuery(query); int cnt = 0; while (xqs.next()) { cnt++; } System.out.println("Got " + cnt + " results"); return cnt > 0; }
@Override protected int execQuery(String query, Map<String, Parameter> params) throws XQException { XQPreparedExpression xqpe = getConnection().prepareExpression(query); bindParams(params, xqpe); XQResultSequence xqs = xqpe.executeQuery(); int cnt = 0; if (fetchSize > 0) { while (xqs.next() && cnt < fetchSize) { cnt++; } } else { while (xqs.next()) { cnt++; } } //xqs.close(); will be closed at next line xqpe.close(); return cnt; }
@Override public XQResultSequence executeQuery(String query) throws XQException { checkState(ex_expression_closed); if (query == null) { throw new XQException("Provided query is null"); } closeResults(); ResultCursor<XQItemAccessor> result = connection.executeQuery(query, context); XQResultSequence sequence; if (context.getScrollability() == XQConstants.SCROLLTYPE_SCROLLABLE) { sequence = new ScrollableXQResultSequence(this, ((ResultCursorBase<XQItemAccessor>) result).getList()); } else { sequence = new IterableXQResultSequence(this, result); } results.add(sequence); return sequence; }
public void queryChildTest() throws XQException { String query = //"declare namespace s=\"http://tpox-benchmark.com/security\";\n" + //"for $sec in fn:collection(\"/{http://tpox-benchmark.com/security}Security\")/s:Security\n" + "for $e in fn:collection(\"/document\")/document\n" + //"where $sec/s:Symbol=$sym\n" + //'IBM'\n" + "return $e//child\n"; XQExpression xqe = xqc.createExpression(); //xqe.bindString(new QName("doc"), xml, xqc.createAtomicType(XQItemType.XQBASETYPE_STRING)); XQResultSequence xqrs = xqe.executeQuery(query); assertNotNull(xqrs); assertFalse(xqrs.isClosed()); boolean found = false; while (xqrs.next()) { System.out.println(xqrs.getItemAsString(null)); found = true; } }
@Test public void queryParentTest() throws XQException { String query = //"declare namespace s=\"http://tpox-benchmark.com/security\";\n" + //"for $sec in fn:collection(\"/{http://tpox-benchmark.com/security}Security\")/s:Security\n" + "for $e in fn:collection(\"/document\")\n" + // /document\n" + //"where $e//child/parent::*[fn:count(*)=3]\n" + //"where fn:count($e//child/parent::*)=3\n" + "where $e/document/root//*[.='IBM']\n" + "return $e\n"; // starts with /document/root; // descendants of document/root; kind = text; XQExpression xqe = xqc.createExpression(); XQResultSequence xqrs = xqe.executeQuery(query); assertNotNull(xqrs); assertFalse(xqrs.isClosed()); boolean found = false; while (xqrs.next()) { System.out.println(xqrs.getItemAsString(null)); found = true; } }
private static String runQuery(String query) throws XQException { System.out.println("runQuery: \n\t" + query); XQDataSource xqs = new ExistXQDataSource(); String returnString = ""; xqs.setProperty("serverName", "localhost"); xqs.setProperty("port", "8080"); XQConnection conn = xqs.getConnection(); XQPreparedExpression xqpe = conn.prepareExpression(query); XQResultSequence rs = xqpe.executeQuery(); while (rs.next()) { returnString += rs.getItemAsString(null).replace("xmlns=\"\"", "") + "\n"; } System.out.println("Query result: \n\t" + returnString); return returnString; }
/** * Helper method to execute a {@link XQPreparedExpression} and return an * {@link OutputStream} of results. This method does not support setting write * properties. * * @param xqPreparedExpression * the expression to execute * @return an output stream with the results */ public static ByteArrayOutputStream executePreparedExpression( final XQPreparedExpression xqPreparedExpression) { try { final XQResultSequence xqResultSequence = xqPreparedExpression.executeQuery(); final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); xqResultSequence.writeSequence(outputStream, null); return outputStream; } catch (final XQException e) { throw new ApplicationException("Unable to execute prepared expression", e); } }
/** * * @throws XQException * @throws XQException * @throws JAXBException * @throws IOException */ @Test public void testXQueryExpression() throws XQException { String sourceFile="synderTest.xml"; String xqFile="synderQuery1.xq"; Configuration saxonConfig = new Configuration(); SaxonXQDataSource dataSource = new SaxonXQDataSource(saxonConfig); XQConnection conn = dataSource.getConnection(); String xsString="for $Template in doc(\"synderTest.xml\")/TemplateContainer/Template/Tags "+ " return $Template" ; XQPreparedExpression exp = conn.prepareExpression(xsString); URI sourcUri=new File(sourceFile).toURI(); // exp.bindString(new QName("docName"), sourcUri.getPath(), conn // .createAtomicType(XQItemType.XQBASETYPE_STRING)); XQResultSequence result = exp.executeQuery(); String rawResult = result.getSequenceAsString(new Properties()); System.out.println("TransformTest.testXQueryExpression()..:"+rawResult); // System.out.println(tester.transfer(sourceFile, xqFile)); }
@ManagedOperation(description="Run XQuery. Returns string output specified by XQuery") @ManagedOperationParameters({ @ManagedOperationParameter(name = "query", description = "A query request provided in XQuery syntax"), @ManagedOperationParameter(name = "useXDM", description = "use XDM (true) or XQJ query interface"), @ManagedOperationParameter(name = "props", description = "Query processing properties")}) public String runQuery(String query, boolean useXDM, Properties props) { logger.debug("runQuery.enter; got query: {}, properties: {}", query, props); String result = null; try { if (useXDM) { ResultCursor<XQItemAccessor> cursor = queryMgr.executeQuery(query, null, props); result = extractResult(cursor, props); cursor.close(); } else { XQStaticContext ctx = xqConn.getStaticContext(); props2Context(schemaManager.getEntity().getProperties(), ctx); props2Context(props, ctx); XQExpression xqExp = xqConn.createExpression(ctx); XQResultSequence xqSec = xqExp.executeQuery(query); result = xqSec.getSequenceAsString(props); xqExp.close(); } } catch (Exception ex) { logger.error("runQuery.error", ex); throw new RuntimeException(ex.getMessage()); } logger.debug("runQuery.exit; returning: {}", result); return result; }
@Override public String queryDocumentByUri(String uri) throws XQException { String query = "for $doc in fn:doc(\"" + uri + "\")\n" + "return $doc\n"; XQExpression xqe = xqConn.createExpression(); XQResultSequence xqs = xqe.executeQuery(query); String result = null; if (xqs.next()) { result = xqs.getItemAsString(null); } return result; }
@Override public String queryDocumentFromCollection() throws XQException { String query = "for $doc in fn:collection()\n" + "return $doc\n"; XQExpression xqe = xqConn.createExpression(); XQResultSequence xqs = xqe.executeQuery(query); String result = null; if (xqs.next()) { result = xqs.getItemAsString(null); } return result; }
public boolean searchSecQueryParams() throws XQException { String query = "declare default element namespace \"http://tpox-benchmark.com/security\";\n" + "declare variable $sect external;\n" + "declare variable $pemin external;\n" + "declare variable $pemax external;\n" + "declare variable $yield external;\n" + "for $sec in fn:collection(\"/{http://tpox-benchmark.com/security}Security\")/Security\n" + "where $sec[SecurityInformation/*/Sector = $sect and PE[. >= $pemin and . < $pemax] and Yield > $yield]\n" + //"where $sec[SecurityInformation/*/Sector = $sect and PE[. >= xs:decimal($pemin) and . < xs:decimal($pemax)] and Yield > xs:decimal($yield)]\n" + "return <Security>\n" + "\t{$sec/Symbol}\n" + "\t{$sec/Name}\n" + "\t{$sec/SecurityType}\n" + "\t{$sec/SecurityInformation//Sector}\n" + "\t{$sec/PE}\n" + "\t{$sec/Yield}\n" + "</Security>"; XQPreparedExpression xqpe = xqc.prepareExpression(query); xqpe.bindString(new QName("sect"), "Technology", null); //xqpe.bindFloat(new QName("pemin"), 25, null); //xqpe.bindFloat(new QName("pemax"), 28, null); //xqpe.bindFloat(new QName("yield"), 0, null); xqpe.bindInt(new QName("pemin"), 25, null); xqpe.bindInt(new QName("pemax"), 28, null); xqpe.bindInt(new QName("yield"), 0, null); XQResultSequence xqs = xqpe.executeQuery(); int cnt = 0; while (xqs.next()) { cnt++; } System.out.println("Got " + cnt + " results"); return cnt > 0; }
private void closeResults() throws XQException { for (XQResultSequence sq: results) { if (!sq.isClosed()) { sq.close(); } } results.clear(); }
@Override public XQResultSequence executeQuery(Reader query) throws XQException { String str; try { str = XMLUtils.textToString(query); } catch (IOException ex) { throw new XQException(ex.getMessage()); } return executeQuery(str); }
@Override public XQResultSequence executeQuery(InputStream query) throws XQException { String str; try { str = XMLUtils.textToString(query); } catch (IOException ex) { throw new XQException(ex.getMessage()); } return executeQuery(str); }
@Override public XQResultSequence executeQuery() throws XQException { checkState(ex_expression_closed); closeResults(); ResultCursor<XQItemAccessor> result = connection.executeQuery(xquery, context); XQResultSequence sequence; if (context.getScrollability() == XQConstants.SCROLLTYPE_SCROLLABLE) { sequence = new ScrollableXQResultSequence(this, ((ResultCursorBase<XQItemAccessor>) result).getList()); } else { sequence = new IterableXQResultSequence(this, result); } results.add(sequence); return sequence; }
public void runApp() throws Exception { XQDataSource xqs = dataSourceFactory.getDataSource(); XQConnection connection = null; try { connection = connectionFactory.getConnection(xqs); XQPreparedExpression preparedExpression = expressionFactory.getExpression(connection); XQResultSequence rs = preparedExpression.executeQuery(); rs.writeSequence(output, outputMethodFactory.getOutputMethodProperties()); } finally { if (connection != null) { connection.close(); } } }
/** * @param result * @throws XQException */ @SuppressWarnings("unused") private String printResultToString(final XQResultSequence result) throws XQException { final Properties serializationProps = newSerializationPropertiesForPrintout(); final String xmlString = result.getSequenceAsString(serializationProps); // Note: BaseX's getSequenceAsString() implementation ignores the serialization // properties, so don't expect much. return xmlString; }
public PipeRunResult doPipe(Object input, IPipeLineSession session) throws PipeRunException { if (input==null) { throw new PipeRunException(this, getLogPrefix(session) + "got null input"); } if (!(input instanceof String)) { throw new PipeRunException(this, getLogPrefix(session) + "got an invalid type as input, expected String, got " + input.getClass().getName()); } try { String stringResult = (String)input; // We already specifically use Saxon in this pipe, hence set xslt2 // to true to make XmlUtils use the Saxon // DocumentBuilderFactoryImpl. ParameterResolutionContext prc = new ParameterResolutionContext(stringResult, session, isNamespaceAware(), true); Map parametervalues = null; if (getParameterList() != null) { parametervalues = prc.getValueMap(getParameterList()); } preparedExpression.bindDocument(XQConstants.CONTEXT_ITEM, stringResult, null, null); Iterator iterator = getParameterList().iterator(); while (iterator.hasNext()) { Parameter parameter = (Parameter)iterator.next(); preparedExpression.bindObject(new QName(parameter.getName()), parametervalues.get(parameter.getName()), null); } XQResultSequence resultSequence = preparedExpression.executeQuery(); stringResult = resultSequence.getSequenceAsString(null); return new PipeRunResult(getForward(), stringResult); } catch (Exception e) { throw new PipeRunException(this, getLogPrefix(session)+" Exception on running xquery", e); } }
/** * Test method for {@link gov.nih.nci.cbiit.cmts.transform.XQueryBuilder#getXQuery()}. * @throws XQException */ @Test public void testXQueryTransform() throws XQException { // String sourceFile="workingspace/simpleMapping/shiporder.xml"; // String xqFile="workingspace/simpleMapping/testXQ1.xq"; String sourceFile="synderTest.xml"; String xqFile="synderQuery.xq"; InputStream in; try { in = new FileInputStream(new File(xqFile)); InputStreamReader inputStream=new InputStreamReader(in); Configuration saxonConfig = new Configuration(); SaxonXQDataSource dataSource = new SaxonXQDataSource(saxonConfig); XQConnection conn = dataSource.getConnection(); XQPreparedExpression exp = conn.prepareExpression(inputStream); URI sourcUri=new File(sourceFile).toURI(); exp.bindString(new QName("docName"), sourcUri.getPath(), conn .createAtomicType(XQItemType.XQBASETYPE_STRING)); XQResultSequence result = exp.executeQuery(); String rawResult = result.getSequenceAsString(new Properties()); System.out.println("TransformTest.testXQueryExpression()..:\n"+rawResult); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } // XQueryTransformer tester= new XQueryTransformer(); // System.out.println(tester.transfer(sourceFile, xqFile)); }
@Override protected void doExecute(String location, ActionInvocation invocation) throws Exception { String xqy_str = getXQYText(location, invocation); // System.out.println(xqy_str); if (xqy_str != null && !"".equals(xqy_str.trim())) { IXQSession session = DptXMLDBSessionFactory.getXQSession(); try { session.beginTransaction(); XQResultSequence sequence = session.getXqConnection() .createExpression().executeQuery(xqy_str); String result = sequence.getSequenceAsString(null); session.commit(); HttpServletResponse response = ServletActionContext .getResponse(); if (charSet != null && !"".equals(charSet.trim())) { response.setContentType("text/html; charset=" + charSet); } else { response.setContentType("text/html"); } response.setCharacterEncoding(charSet); PrintWriter writer = response.getWriter(); writer.write("<!DOCTYPE html>"); StringReader reader = new StringReader(result); try { char[] buffer = new char[BUFFER_SIZE]; int charRead = 0; while ((charRead = reader.read(buffer)) != -1) { writer.write(buffer, 0, charRead); } } finally { if (reader != null) reader.close(); if (writer != null) { writer.flush(); writer.close(); } } } catch (Exception e) { if(session!=null){ session.rollBack(); } } finally { DptXMLDBSessionFactory.closeXQSession(); } } }
@Test public void testXQConnection() throws Exception { final Server srv = Server.getInstance(); final URL fname = BaseXTestSuite.class.getClassLoader().getResource( "sampleHarvest.xml" ); File file = new File(fname.toURI()); srv.startup(file); final XQDataSource xqs = new BaseXXQDataSource(); xqs.setProperty("serverName", Server.SERVER_NAME); xqs.setProperty("port", String.valueOf(Server.PORT)); xqs.setProperty("databaseName", Server.DATABASE_NAME); xqs.setProperty("user", Client.USER); xqs.setProperty("password", Client.PASSWORD); final XQConnection conn = xqs.getConnection(); try { final String query = "declare default element namespace \"http://www.w3.org/1998/Math/MathML\";\n" + "for $m in //*:expr return \n" + "for $x in $m//*:apply\n" + "[*[1]/name() = 'divide']\n" + "where\n" + "fn:count($x/*) = 3\n" + "return\n" + "<result>{$m/@url}</result>"; final XQPreparedExpression xqpe = conn.prepareExpression( query ); final XQResultSequence rs = xqpe.executeQuery(); final String res = rs.getSequenceAsString( null ); Assert.assertEquals( "<result xmlns=\"http://www.w3.org/1998/Math/MathML\" url=\"4#math.4.5\"/> " + "<result xmlns=\"http://www.w3.org/1998/Math/MathML\" url=\"4#math.4.5\"/> " + "<result xmlns=\"http://www.w3.org/1998/Math/MathML\" url=\"5#math.5.2\"/> " + "<result xmlns=\"http://www.w3.org/1998/Math/MathML\" url=\"5#math.5.17\"/> " + "<result xmlns=\"http://www.w3.org/1998/Math/MathML\" url=\"5#math.5.18\"/> " + "<result xmlns=\"http://www.w3.org/1998/Math/MathML\" url=\"5#math.5.18\"/> " + "<result xmlns=\"http://www.w3.org/1998/Math/MathML\" url=\"5#math.5.19\"/> " + "<result xmlns=\"http://www.w3.org/1998/Math/MathML\" url=\"5#math.5.19\"/> " + "<result xmlns=\"http://www.w3.org/1998/Math/MathML\" url=\"5#math.5.19\"/> " + "<result xmlns=\"http://www.w3.org/1998/Math/MathML\" url=\"5#math.5.20\"/> " + "<result xmlns=\"http://www.w3.org/1998/Math/MathML\" url=\"5#math.5.21\"/> " + "<result xmlns=\"http://www.w3.org/1998/Math/MathML\" url=\"5#math.5.22\"/> " + "<result xmlns=\"http://www.w3.org/1998/Math/MathML\" url=\"5#math.5.23\"/> " + "<result xmlns=\"http://www.w3.org/1998/Math/MathML\" url=\"6#math.6.11\"/> " + "<result xmlns=\"http://www.w3.org/1998/Math/MathML\" url=\"6#math.6.14\"/> " + "<result xmlns=\"http://www.w3.org/1998/Math/MathML\" url=\"6#math.6.15\"/> " + "<result xmlns=\"http://www.w3.org/1998/Math/MathML\" url=\"6#math.6.15\"/> " + "<result xmlns=\"http://www.w3.org/1998/Math/MathML\" url=\"6#math.6.15\"/> " + "<result xmlns=\"http://www.w3.org/1998/Math/MathML\" url=\"6#math.6.15\"/> " + "<result xmlns=\"http://www.w3.org/1998/Math/MathML\" url=\"6#math.6.20\"/> " + "<result xmlns=\"http://www.w3.org/1998/Math/MathML\" url=\"7#math.7.0\"/> " + "<result xmlns=\"http://www.w3.org/1998/Math/MathML\" url=\"7#math.7.1\"/> " + "<result xmlns=\"http://www.w3.org/1998/Math/MathML\" url=\"7#math.7.1\"/> " + "<result xmlns=\"http://www.w3.org/1998/Math/MathML\" url=\"7#math.7.2\"/> " + "<result xmlns=\"http://www.w3.org/1998/Math/MathML\" url=\"7#math.7.2\"/> " + "<result xmlns=\"http://www.w3.org/1998/Math/MathML\" url=\"7#math.7.3\"/> " + "<result xmlns=\"http://www.w3.org/1998/Math/MathML\" url=\"7#math.7.3\"/> " + "<result xmlns=\"http://www.w3.org/1998/Math/MathML\" url=\"7#math.7.4\"/> " + "<result xmlns=\"http://www.w3.org/1998/Math/MathML\" url=\"8#math.8.6\"/> " + "<result xmlns=\"http://www.w3.org/1998/Math/MathML\" url=\"8#math.8.7\"/> " + "<result xmlns=\"http://www.w3.org/1998/Math/MathML\" url=\"8#math.8.21\"/> " + "<result xmlns=\"http://www.w3.org/1998/Math/MathML\" url=\"8#math.8.22\"/> " + "<result xmlns=\"http://www.w3.org/1998/Math/MathML\" url=\"8#math.8.23\"/> " + "<result xmlns=\"http://www.w3.org/1998/Math/MathML\" url=\"8#math.8.33\"/> " + "<result xmlns=\"http://www.w3.org/1998/Math/MathML\" url=\"8#math.8.34\"/> " + "<result xmlns=\"http://www.w3.org/1998/Math/MathML\" url=\"8#math.8.35\"/> " + "<result xmlns=\"http://www.w3.org/1998/Math/MathML\" url=\"dummy29\"/>" , res ); } finally { conn.close(); } }
public boolean searchSecQueryParams() throws XQException { String query = "declare default element namespace \"http://tpox-benchmark.com/security\";\n" + "declare variable $sect external;\n" + "declare variable $pemin external;\n" + "declare variable $pemax external;\n" + "declare variable $yield external;\n" + "for $sec in fn:collection(\"CLN_Security\")/s:Security\n" + "where $sec[SecurityInformation/*/Sector = $sect and PE[. >= $pemin and . < $pemax] and Yield > $yield]\n" + "return <Security>\n" + "\t{$sec/Symbol}\n" + "\t{$sec/Name}\n" + "\t{$sec/SecurityType}\n" + "\t{$sec/SecurityInformation//Sector}\n" + "\t{$sec/PE}\n" + "\t{$sec/Yield}\n" + "</Security>"; XQPreparedExpression xqpe = xqc.prepareExpression(query); xqpe.bindString(new QName("sect"), "Technology", null); QName typeName = new QName(xs_ns, "decimal", xs_prefix); int baseType = getBaseTypeForTypeName(typeName); XQItemType type = xqc.createAtomicType(baseType, typeName, null); //xqpe.bindFloat(new QName("pemin"), 25.0f, null); //xqpe.bindFloat(new QName("pemax"), 28.0f, null); //xqpe.bindFloat(new QName("yield"), 0.1f, null); xqpe.bindObject(new QName("pemin"), new java.math.BigDecimal("25.0"), type); xqpe.bindObject(new QName("pemax"), new java.math.BigDecimal("28.0"), type); xqpe.bindObject(new QName("yield"), new java.math.BigDecimal("0.1"), type); //xqpe.b XQResultSequence xqs = xqpe.executeQuery(); int cnt = 0; while (xqs.next()) { //System.out.println(xqs.getItemAsString(null)); cnt++; } System.out.println("Got " + cnt + " results"); return cnt > 0; }
BagriXQResultItem(XQItemType type, Object value, XQResultSequence parent) { this(((BagriXQSequence) parent).getXQProcessor(), type, value); this.parent = parent; }
public static void main(final String[] args) throws Exception { //get the command line arguments if(args.length < 5) { printUseage(); System.exit(1); return; } final String server = args[0]; final String port = args[1]; final String query = args[2]; final File queryFile; if(query.charAt(0) == '@') { queryFile = new File(query.substring(1)); if(!queryFile.exists()) { System.err.println("Query file '" + queryFile.getAbsolutePath() + "' does not exist!"); System.exit(2); } } else { queryFile = null; } final String path = args[3]; final String username = args[4]; final String password; if(args.length == 6) { password = args[5]; } else { password = ""; } //setup the Data Source final XQDataSource dataSource = new ExistXQDataSource(); dataSource.setProperty("serverName", server); dataSource.setProperty("port", port); //get connection with authenticated credentials XQConnection connection = null; try { connection = dataSource.getConnection(username, password); final String uri = "http://" + server + ":" + port + "/exist/rest" + path; logger.info("Starting Query of {}...", uri); //execute the query expression final XQExpression expression = connection.createExpression(); final XQResultSequence result; if(queryFile == null) { result = expression.executeQuery(query); } else { InputStream is = null; try { is = new FileInputStream(queryFile); result = expression.executeQuery(is); } finally { if(is != null) { is.close(); } } } //output the results boolean producedResults = false; while(result.next()) { result.writeItem(System.out, null); producedResults = true; } if(producedResults) { System.out.println(); } else { logger.warn("Your XQuery produced no results!"); } logger.info("Finished Query OK."); } finally { if(connection != null) { connection.close(); } } }