use of io.shardingjdbc.core.parsing.parser.context.selectitem.SelectItem in project sharding-jdbc by shardingjdbc.
the class SelectStatement method getAggregationSelectItems.
/**
* Get aggregation select items.
*
* @return aggregation select items
*/
public List<AggregationSelectItem> getAggregationSelectItems() {
List<AggregationSelectItem> result = new LinkedList<>();
for (SelectItem each : items) {
if (each instanceof AggregationSelectItem) {
AggregationSelectItem aggregationSelectItem = (AggregationSelectItem) each;
result.add(aggregationSelectItem);
result.addAll(aggregationSelectItem.getDerivedAggregationSelectItems());
}
}
return result;
}
use of io.shardingjdbc.core.parsing.parser.context.selectitem.SelectItem in project sharding-jdbc by shardingjdbc.
the class AbstractSelectParser method appendAvgDerivedColumns.
private void appendAvgDerivedColumns(final ItemsToken itemsToken, final SelectStatement selectStatement) {
int derivedColumnOffset = 0;
for (SelectItem each : selectStatement.getItems()) {
if (!(each instanceof AggregationSelectItem) || AggregationType.AVG != ((AggregationSelectItem) each).getType()) {
continue;
}
AggregationSelectItem avgItem = (AggregationSelectItem) each;
String countAlias = String.format(DERIVED_COUNT_ALIAS, derivedColumnOffset);
AggregationSelectItem countItem = new AggregationSelectItem(AggregationType.COUNT, avgItem.getInnerExpression(), Optional.of(countAlias));
String sumAlias = String.format(DERIVED_SUM_ALIAS, derivedColumnOffset);
AggregationSelectItem sumItem = new AggregationSelectItem(AggregationType.SUM, avgItem.getInnerExpression(), Optional.of(sumAlias));
avgItem.getDerivedAggregationSelectItems().add(countItem);
avgItem.getDerivedAggregationSelectItems().add(sumItem);
// TODO replace avg to constant, avoid calculate useless avg
itemsToken.getItems().add(countItem.getExpression() + " AS " + countAlias + " ");
itemsToken.getItems().add(sumItem.getExpression() + " AS " + sumAlias + " ");
derivedColumnOffset++;
}
}
use of io.shardingjdbc.core.parsing.parser.context.selectitem.SelectItem in project sharding-jdbc by shardingjdbc.
the class SelectListClauseParser method parseSelectItem.
private SelectItem parseSelectItem(final SelectStatement selectStatement) {
lexerEngine.skipIfEqual(getSkippedKeywordsBeforeSelectItem());
SelectItem result;
if (isRowNumberSelectItem()) {
result = parseRowNumberSelectItem(selectStatement);
} else if (isStarSelectItem()) {
selectStatement.setContainStar(true);
result = parseStarSelectItem();
} else if (isAggregationSelectItem()) {
result = parseAggregationSelectItem(selectStatement);
parseRestSelectItem(selectStatement);
} else {
result = new CommonSelectItem(SQLUtil.getExactlyValue(parseCommonSelectItem(selectStatement) + parseRestSelectItem(selectStatement)), aliasExpressionParser.parseSelectItemAlias());
}
return result;
}
Aggregations