use of com.thinkbiganalytics.metadata.rest.model.jcr.JcrQueryResultColumn in project kylo by Teradata.
the class DebugController method queryJcr.
/**
* Prints the nodes of the JCR path given, for debugging.
*
* @param query the jcr query
* @return a printout of the JCR tree
*/
@GET
@Path("jcr-sql")
@Produces({ MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON })
public JcrQueryResult queryJcr(@QueryParam("query") final String query) {
return metadata.read(() -> {
this.accessController.checkPermission(AccessController.SERVICES, MetadataAccessControl.ADMIN_METADATA);
List<List<String>> rows = new ArrayList<>();
Long startTime = System.currentTimeMillis();
JcrQueryResult jcrQueryResult = new JcrQueryResult();
try {
Session session = JcrMetadataAccess.getActiveSession();
Workspace workspace = (Workspace) session.getWorkspace();
String explainPlain = JcrQueryUtil.explainPlain(session, query);
// start the timer now:
startTime = System.currentTimeMillis();
QueryResult result = JcrQueryUtil.query(session, query);
jcrQueryResult.setExplainPlan(explainPlain);
RowIterator rowItr = result.getRows();
List<JcrQueryResultColumn> columns = new ArrayList<>();
String colsStr = StringUtils.substringAfter(query.toLowerCase(), "select");
colsStr = StringUtils.substringBefore(colsStr, "from");
if (StringUtils.isNotBlank(colsStr)) {
colsStr = colsStr.trim();
columns = Arrays.asList(colsStr.split(",")).stream().map(c -> {
String columnName = c;
if (c.contains("as ")) {
columnName = StringUtils.substringAfter(c, "as ");
} else if (c.contains(" ")) {
columnName = StringUtils.substringAfter(c, " ");
}
return new JcrQueryResultColumn(columnName);
}).collect(Collectors.toList());
}
jcrQueryResult.setColumns(columns);
while (rowItr.hasNext()) {
Row row = rowItr.nextRow();
Value[] rowValues = row.getValues();
if (rowValues != null) {
if (rowValues.length != columns.size()) {
columns = IntStream.range(0, rowValues.length).mapToObj(i -> new JcrQueryResultColumn("Column " + i)).collect(Collectors.toList());
jcrQueryResult.setColumns(columns);
}
JcrQueryResultRow jcrQueryResultRow = new JcrQueryResultRow();
jcrQueryResult.addRow(jcrQueryResultRow);
List<JcrQueryResultColumnValue> jcrQueryResultColumnValues = Arrays.asList(rowValues).stream().map(v -> {
try {
String value = v.getString();
return new JcrQueryResultColumnValue(value);
} catch (Exception e) {
return new JcrQueryResultColumnValue("ERROR: " + e.getMessage());
}
}).collect(Collectors.toList());
jcrQueryResultRow.setColumnValues(jcrQueryResultColumnValues);
}
}
} catch (Exception e) {
throw new RuntimeException(e);
}
long totalTime = System.currentTimeMillis() - startTime;
jcrQueryResult.setQueryTime(totalTime);
return jcrQueryResult;
});
}
Aggregations