// check certain common query types that can often be optimized by
// subclasses
if (fromItems.size() == 1 && groupByItems.isEmpty() && havingItems.isEmpty()) {
final FromItem fromItem = query.getFromClause().getItem(0);
final Table table = fromItem.getTable();
if (table != null) {
// check for approximate SELECT COUNT(*) queries
if (selectItems.size() == 1) {
final SelectItem selectItem = query.getSelectClause().getItem(0);
if (SelectItem.isCountAllItem(selectItem)) {
final boolean functionApproximationAllowed = selectItem.isFunctionApproximationAllowed();
if (isMainSchemaTable(table)) {
logger.debug("Query is a COUNT query with {} where items. Trying executeCountQuery(...)",
whereItems.size());
final Number count = executeCountQuery(table, whereItems, functionApproximationAllowed);
if (count == null) {
logger.debug("DataContext did not return any count query results. Proceeding with manual counting.");
} else {
List<Row> data = new ArrayList<Row>(1);
final DataSetHeader header = new SimpleDataSetHeader(new SelectItem[] { selectItem });
data.add(new DefaultRow(header, new Object[] { count }));
return new InMemoryDataSet(header, data);
}
}
}
}
// check for lookup query by primary key
if (whereItems.size() == 1) {
final FilterItem whereItem = whereItems.get(0);
final SelectItem selectItem = whereItem.getSelectItem();
if (!whereItem.isCompoundFilter() && selectItem != null && selectItem.getColumn() != null) {
final Column column = selectItem.getColumn();
if (column.isPrimaryKey() && whereItem.getOperator() == OperatorType.EQUALS_TO) {
logger.debug("Query is a primary key lookup query. Trying executePrimaryKeyLookupQuery(...)");
if (table != null) {
if (isMainSchemaTable(table)) {
final Object operand = whereItem.getOperand();
final Row row = executePrimaryKeyLookupQuery(table, selectItems, column, operand);
if (row == null) {
logger.debug("DataContext did not return any GET query results. Proceeding with manual lookup.");
} else {
final DataSetHeader header = new SimpleDataSetHeader(selectItems);
return new InMemoryDataSet(header, row);
}
}
}
}
}
}
}
}
final int firstRow = (query.getFirstRow() == null ? 1 : query.getFirstRow());
final int maxRows = (query.getMaxRows() == null ? -1 : query.getMaxRows());
// Check for very simple queries with max rows property set (typically
// preview), see Ticket #187
previewTable: if (whereItems.isEmpty() && groupByItems.isEmpty() && havingItems.isEmpty()
&& orderByItems.isEmpty() && fromItems.size() == 1) {
final Table table = fromItems.get(0).getTable();
if (table != null) {
for (SelectItem item : selectItems) {
if (item.getFunction() != null || item.getExpression() != null) {
break previewTable;
}
}
DataSet dataSet = materializeTable(table, selectItems, firstRow, maxRows);
dataSet = MetaModelHelper.getSelection(selectItems, dataSet);
return dataSet;
}
}
// Creates a list for all select items that are needed to execute query
// (some may only be used as part of a filter, but not shown in result)
List<SelectItem> workSelectItems = CollectionUtils.concat(true, selectItems, whereSelectItems,
groupBySelectItems, havingSelectItems, orderBySelectItems);
// Materialize the tables in the from clause
final DataSet[] fromDataSets = new DataSet[fromItems.size()];
for (int i = 0; i < fromDataSets.length; i++) {
FromItem fromItem = fromItems.get(i);
fromDataSets[i] = materializeFromItem(fromItem, workSelectItems);
}
// Execute the query using the raw data
DataSet dataSet = MetaModelHelper.getCarthesianProduct(fromDataSets, whereItems);