/** * The query associated with a query execution. * May be null (QueryExecution may have been created by other means) */ @Override public Query getQuery() { return decoratee.getQuery(); }
/** Return a closable resultset for a {@link QueryExecution}. * The {@link QueryExecution} must be for a {@code SELECT} query. * @param queryExecution {@code QueryExecution} must be for a {@code SELECT} query. * @return ResultSetCloseable */ public static ResultSetCloseable closeableResultSet(QueryExecution queryExecution) { if ( queryExecution.getQuery() != null && ! queryExecution.getQuery().isSelectType() ) throw new IllegalArgumentException("Not an execution for a SELECT query"); return new ResultSetCloseable(queryExecution.execSelect(), queryExecution) ; }
private static void doSelectQuery(Prologue prologue, QueryExecution qe, ResultsFormat outputFormat) { if ( prologue == null ) prologue = qe.getQuery().getPrologue() ; if ( outputFormat == null || outputFormat == ResultsFormat.FMT_UNKNOWN ) outputFormat = ResultsFormat.FMT_TEXT ; ResultSet results = qe.execSelect() ; outputResultSet(results, prologue, outputFormat) ; }
/** * Given a SPARQL query, return its default file format as a string. * * @param query the SPARQL query string * @return the format name */ public static String getDefaultFormatName(String query) { QueryExecution qExec = QueryExecutionFactory.create(query, createEmptyDataset()); return getDefaultFormatName(qExec.getQuery()); }
private static void doConstructQuery(Prologue prologue, QueryExecution qe, ResultsFormat outputFormat) { if ( qe.getQuery().isConstructQuad() ) { doConstructQuadsQuery(prologue, qe, outputFormat); return; } if ( outputFormat == null || outputFormat == ResultsFormat.FMT_UNKNOWN ) outputFormat = ResultsFormat.FMT_RDF_TTL ; Model r = qe.execConstruct() ; writeModel(prologue, r, outputFormat) ; }
if(qe.getQuery().isSelectType()) { ResultSet rs = qe.execSelect(); heads = rs.getResultVars();
/** * Given a SPARQL query, return its type as a string: ASK, CONSTRUCT, DESCRIBE, or SELECT. * * @param queryString the SPARQL query string * @return the query type string * @throws IllegalArgumentException on bad query */ public static String getQueryTypeName(String queryString) { QueryExecution qExec = QueryExecutionFactory.create(queryString, createEmptyDataset()); Query query = qExec.getQuery(); String queryType; switch (query.getQueryType()) { case Query.QueryTypeAsk: queryType = "ASK"; break; case Query.QueryTypeConstruct: queryType = "CONSTRUCT"; break; case Query.QueryTypeDescribe: queryType = "DESCRIBE"; break; case Query.QueryTypeSelect: queryType = "SELECT"; break; default: throw new IllegalArgumentException(String.format(queryTypeError, query.getQueryType())); } return queryType; }
public OntModel executeQuery(String queryString, String endpoint, OntModel model, String defaultGraphURI) { Monitor monQueryingTotal = MonitorFactory.start("Query time total") .start(); try { Query query = QueryFactory.create(queryString); log.debug("Jena Query: ", query); QueryExecution qExec; if (defaultGraphURI == null) { qExec = QueryExecutionFactory.sparqlService(endpoint, query); } else { qExec = QueryExecutionFactory.sparqlService(endpoint, query, defaultGraphURI); } log.debug("Qexec: {}", qExec.getQuery()); qExec.execConstruct(model); } catch (JenaException e) { log.warn("Query failed (skipping):\n" + queryString, e); } monQueryingTotal.stop(); return model; } }
final ResultSet namedResults = QueryExecutionFactory.create( findNamed, model ).execSelect(); final QueryExecution qe = QueryExecutionFactory.create( findPersonAddress, model ); System.out.println( "=== Query Before Adding VALUES ===\n" + qe.getQuery() ); qe.getQuery().setValuesDataBlock(variables, values); System.out.println( "\n=== Query After Adding VALUES ===\n" + qe.getQuery() ); ResultSetFormatter.out( qe.execSelect() );
public static void abortAfterFirstRow(QueryExecution qe) { Query query = qe.getQuery(); assert query != null : "QueryExecution did not tell us which query it is bound to - query was null"; int queryType = query.getQueryType(); try { switch (queryType) { case Query.QueryTypeAsk: qe.execAsk(); break; case Query.QueryTypeConstruct: Iterator<Triple> itC = qe.execConstructTriples(); itC.hasNext(); break; case Query.QueryTypeDescribe: Iterator<Triple> itD = qe.execDescribeTriples(); itD.hasNext(); break; case Query.QueryTypeSelect: ResultSet rs = qe.execSelect(); rs.hasNext(); break; default: throw new RuntimeException("Unknown query type - should not happen: queryType = " + queryType); } } finally { qe.abort(); } }
Query query = qe.getQuery(); assert query != null : "QueryExecution did not tell us which query it is bound to - query was null"; int queryType = query.getQueryType();
/** * Given a query execution, a format name, and an output stream, run the query and write to * output. * * @param qExec QueryExecution to run * @param formatName format of output * @param output output stream to write to * @return true if there are results */ public static boolean runSparqlQuery( QueryExecution qExec, String formatName, OutputStream output) { Query query = qExec.getQuery(); if (formatName == null) { formatName = getDefaultFormatName(query); } switch (query.getQueryType()) { case Query.QueryTypeAsk: writeResult(qExec.execAsk(), output); return true; case Query.QueryTypeConstruct: return maybeWriteResult(qExec.execConstruct(), formatName, output); case Query.QueryTypeDescribe: return maybeWriteResult(qExec.execDescribe(), formatName, output); case Query.QueryTypeSelect: return maybeWriteResult(qExec.execSelect(), formatName, output); default: throw new IllegalArgumentException(String.format(queryTypeError, query.getQueryType())); } }
public static void executeQuery(Prologue prologue, QueryExecution queryExecution, ResultsFormat outputFormat) { Query query = queryExecution.getQuery() ; if ( prologue == null ) prologue = query.getPrologue() ; if ( prologue == null ) prologue = dftPrologue ; if ( query.isSelectType() ) doSelectQuery(prologue, queryExecution, outputFormat) ; else if ( query.isDescribeType() ) doDescribeQuery(prologue, queryExecution, outputFormat) ; else if ( query.isConstructQuad() ) // Before isConstructType. doConstructQuadsQuery(prologue, queryExecution, outputFormat) ; else if ( query.isConstructType() ) doConstructQuery(prologue, queryExecution, outputFormat) ; else if ( query.isAskType() ) doAskQuery(prologue, queryExecution, outputFormat) ; else if ( query.isJsonType() ) doJsonQuery(prologue, queryExecution, outputFormat) ; else throw new QueryException("Unrecognized query form"); }