Search in sources :

Example 1 with SelectItem

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;
}
Also used : AggregationSelectItem(io.shardingjdbc.core.parsing.parser.context.selectitem.AggregationSelectItem) SelectItem(io.shardingjdbc.core.parsing.parser.context.selectitem.SelectItem) AggregationSelectItem(io.shardingjdbc.core.parsing.parser.context.selectitem.AggregationSelectItem) LinkedList(java.util.LinkedList)

Example 2 with SelectItem

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++;
    }
}
Also used : AggregationSelectItem(io.shardingjdbc.core.parsing.parser.context.selectitem.AggregationSelectItem) SelectItem(io.shardingjdbc.core.parsing.parser.context.selectitem.SelectItem) AggregationSelectItem(io.shardingjdbc.core.parsing.parser.context.selectitem.AggregationSelectItem)

Example 3 with SelectItem

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;
}
Also used : SelectItem(io.shardingjdbc.core.parsing.parser.context.selectitem.SelectItem) StarSelectItem(io.shardingjdbc.core.parsing.parser.context.selectitem.StarSelectItem) CommonSelectItem(io.shardingjdbc.core.parsing.parser.context.selectitem.CommonSelectItem) AggregationSelectItem(io.shardingjdbc.core.parsing.parser.context.selectitem.AggregationSelectItem) CommonSelectItem(io.shardingjdbc.core.parsing.parser.context.selectitem.CommonSelectItem)

Aggregations

AggregationSelectItem (io.shardingjdbc.core.parsing.parser.context.selectitem.AggregationSelectItem)3 SelectItem (io.shardingjdbc.core.parsing.parser.context.selectitem.SelectItem)3 CommonSelectItem (io.shardingjdbc.core.parsing.parser.context.selectitem.CommonSelectItem)1 StarSelectItem (io.shardingjdbc.core.parsing.parser.context.selectitem.StarSelectItem)1 LinkedList (java.util.LinkedList)1