* @param <T> generic type of RowMapper
* @return single result of the RowMapper call
*/
public <T> T queryForObject(String sparql, Map<String, Object> args, RowMapper<T> mapper) {
Connection connection = dataSource.getConnection();
TupleQueryResult result = null;
try {
SelectQuery query = connection.select(sparql);
if (args != null) {
for (Entry<String, Object> arg : args.entrySet()) {
query.parameter(arg.getKey(), arg.getValue());
}
}
result = query.execute();
T returnObject = null;
// return null; for empty queries
if (result == null) {
return returnObject;
}
if (result.hasNext()) {
returnObject = mapper.mapRow(result.next());
}
return returnObject;
} catch (StardogException e) {
log.error("Error sending query to Stardog", e);
throw new RuntimeException(e);
} catch (QueryEvaluationException e) {
log.error("Error evaluating SPARQL query", e);
throw new RuntimeException(e);
} finally {
if (result != null) {
try {
result.close();
}
catch (QueryEvaluationException e) { }
}
dataSource.releaseConnection(connection);
}