Search in sources :

Example 6 with AstFieldReference

use of com.developmentontheedge.sql.model.AstFieldReference in project be5 by DevelopmentOnTheEdge.

the class DB2Transformer method transformSelect.

@Override
protected void transformSelect(AstSelect select) {
    AstLimit limit = select.getLimit();
    if (limit != null) {
        if (limit.getOffset() == 0)
            limit.setShape("FETCH FIRST", "ROWS ONLY");
        else {
            if (select.getOrderBy() == null)
                throw new IllegalStateException("The ranking function \"ROW_NUMBER\" must have an ORDER BY clause");
            select.dropLimit();
            SimpleNode parent = select.jjtGetParent();
            int idx = parent.indexOf(select);
            AstIdentifierConstant tmp = new AstIdentifierConstant("tmp");
            AstIdentifierConstant rn = new AstIdentifierConstant("rn");
            AstSelectList list = select.getSelectList();
            AstTableRef tableRef = new AstTableRef(select);
            tableRef.setAsToken(true);
            tableRef.addChild(tmp);
            AstFrom from = new AstFrom(tableRef);
            AstWhere where = new AstWhere();
            AstBetweenPredicate between = new AstBetweenPredicate(new AstFieldReference(tmp, rn), AstNumericConstant.of(limit.getOffset()), AstNumericConstant.of(limit.getOffset() + limit.getLimit()));
            where.addChild(between);
            AstSelect newSelect = new AstSelect((AstSelectList) list.clone(), from, where);
            AstFunNode func = parserContext.getFunction("row_number").node();
            AstDerivedColumn derCol = new AstDerivedColumn(new AstWindowFunction(func, new AstWindowSpecification(select.getOrderBy())));
            select.dropOrder();
            derCol.setPrefixComma(true);
            derCol.setAsToken(true);
            derCol.addChild(rn);
            if (list.isAllColumns()) {
                for (AstTableRef tr : select.getFrom().tableRefs()) {
                    String tableName = tr.getAlias() == null ? tr.getTable() : tr.getAlias();
                    list.addChild(new AstFieldReference(new AstIdentifierConstant(tableName), new AstIdentifierConstant("*")));
                }
            }
            list.addChild(derCol);
            parent.jjtAddChild(newSelect, idx);
        }
    }
    super.transformSelect(select);
}
Also used : AstWhere(com.developmentontheedge.sql.model.AstWhere) AstSelect(com.developmentontheedge.sql.model.AstSelect) AstIdentifierConstant(com.developmentontheedge.sql.model.AstIdentifierConstant) AstFunNode(com.developmentontheedge.sql.model.AstFunNode) AstWindowFunction(com.developmentontheedge.sql.model.AstWindowFunction) AstTableRef(com.developmentontheedge.sql.model.AstTableRef) AstWindowSpecification(com.developmentontheedge.sql.model.AstWindowSpecification) SimpleNode(com.developmentontheedge.sql.model.SimpleNode) AstSelectList(com.developmentontheedge.sql.model.AstSelectList) AstLimit(com.developmentontheedge.sql.model.AstLimit) AstFieldReference(com.developmentontheedge.sql.model.AstFieldReference) AstDerivedColumn(com.developmentontheedge.sql.model.AstDerivedColumn) AstFrom(com.developmentontheedge.sql.model.AstFrom) AstBetweenPredicate(com.developmentontheedge.sql.model.AstBetweenPredicate)

Example 7 with AstFieldReference

use of com.developmentontheedge.sql.model.AstFieldReference in project be5 by DevelopmentOnTheEdge.

the class GenericDbmsTransformer method transformFunction.

protected void transformFunction(AstFunNode node) {
    String name = node.getFunction().getName().toLowerCase();
    if (node.getFunction() instanceof DbSpecificFunction && !((DbSpecificFunction) node.getFunction()).isApplicable(getDbms()) && !node.withinDbmsTransform()) {
        throw new IllegalStateException("Function/operator '" + node.getFunction().getName() + "' is unsupported for " + getDbms());
    }
    if (DbSpecificFunction.needsTransformation(getDbms()).test(node.getFunction()))
        expandDbFunction(node, getDbms());
    if ("concat".equals(name) || ("||".equals(name) && node.jjtGetNumChildren() > 1))
        transformConcat(node);
    else if ("coalesce".equals(name))
        transformCoalesce(node);
    else if ("substr".equals(name))
        transformSubstr(node);
    else if ("length".equals(name))
        transformLength(node);
    else if ("chr".equals(name))
        transformChr(node);
    else if ("if".equals(name))
        transformIf(node);
    else if ("date_format".equals(name) || ("to_char".equals(name) && node.jjtGetNumChildren() == 2)) {
        SimpleNode child = node.child(1);
        if (child instanceof AstStringConstant) {
            String formatString = ((AstStringConstant) child).getValue();
            DateFormat df = DateFormat.byFormatString(formatString);
            if (df == null)
                throw new IllegalArgumentException("Unknown date format: " + formatString);
            transformDateFormat(node, df);
        } else
            throw new IllegalArgumentException("Date format is not a String");
    } else if ("to_char".equals(name) && node.jjtGetNumChildren() == 1 || "to_number".equals(name) || "to_key".equals(name))
        transformCastOracle(node);
    else if ("lpad".equals(name))
        transformLpad(node);
    else if ("trunc".equals(name))
        transformTrunc(node);
    else if ("now".equals(name))
        transformNow(node);
    else if ("to_date".equals(name))
        transformToDate(node);
    else if ("year".equals(name) || "month".equals(name) || "day".equals(name))
        transformYearMonthDay(node);
    else {
        DateFormat df = DateFormat.byFunction(name);
        if (df != null)
            transformDateFormat(node, df);
    }
    if ("date_trunc".equals(name)) {
        String datePart = ((AstStringConstant) node.child(0)).getValue();
        if (datePart.equalsIgnoreCase("'month'") || datePart.equalsIgnoreCase("'year'"))
            transformDateTrunc(node);
    }
    if ("instr".equals(name))
        transformInstr(node);
    if ("add_months".equals(name) || "add_days".equals(name) || "add_millis".equals(name))
        transformDateAdd(node);
    if ("last_day".equals(name))
        transformLastDay(node);
    if ("seconddiff".equals(name))
        transformDateTimeDiff(node, "SECOND");
    if ("minutediff".equals(name))
        transformDateTimeDiff(node, "MINUTE");
    if ("hourdiff".equals(name))
        transformDateTimeDiff(node, "HOUR");
    if ("daydiff".equals(name))
        transformDateTimeDiff(node, "DAY");
    if ("monthdiff".equals(name))
        transformDateTimeDiff(node, "MONTH");
    if ("yeardiff".equals(name))
        transformDateTimeDiff(node, "YEAR");
    if ("timestampdiff".equals(name)) {
        SimpleNode child = node.child(0);
        String datePart = child instanceof AstFieldReference ? ((AstIdentifierConstant) child.child(0)).getValue() : "";
        transformDateTimeDiff(node, datePart);
    }
    if ("mod".equals(name) || "%".equals(name))
        transformMod(node);
    if ("least".equals(name) || "greatest".equals(name))
        transformLeastGreatest(node);
    if ("&".equals(name))
        transformBitAnd(node);
    if ("|".equals(name))
        transformBitOr(node);
    if ("right".equals(name))
        transformRight(node);
    if ("left".equals(name))
        transformLeft(node);
    if ("decode".equals(name))
        transformDecode(node);
    if ("string_agg".equals(name))
        transformStringAgg(node);
    if ("regexp_like".equals(name) || "~".equals(name))
        transformRegexpLike(node);
    if ("translate".equals(name))
        transformTranslate(node);
    if ("levenshtein".equals(name))
        transformLevenshtein(node);
}
Also used : DbSpecificFunction(com.developmentontheedge.sql.model.DbSpecificFunction) AstFieldReference(com.developmentontheedge.sql.model.AstFieldReference) AstStringConstant(com.developmentontheedge.sql.model.AstStringConstant) SimpleNode(com.developmentontheedge.sql.model.SimpleNode)

Example 8 with AstFieldReference

use of com.developmentontheedge.sql.model.AstFieldReference in project be5 by DevelopmentOnTheEdge.

the class ColumnAdder method processSelect.

private void processSelect(AstSelect select, String tableName, String columnName, String alias) {
    AstSelectList list = select.getSelectList();
    if (list.isAllColumns())
        throw new IllegalArgumentException("Cannot modify the 'SELECT *' statement");
    String tableAlias = getTableAlias(select.getFrom(), tableName);
    AstDerivedColumn col = new AstDerivedColumn(new AstFieldReference(tableAlias, columnName), alias).setSuffixComma(true).setAsToken(true);
    list.addChild(col);
    list.moveToFront(col);
}
Also used : AstSelectList(com.developmentontheedge.sql.model.AstSelectList) AstFieldReference(com.developmentontheedge.sql.model.AstFieldReference) AstDerivedColumn(com.developmentontheedge.sql.model.AstDerivedColumn)

Aggregations

AstFieldReference (com.developmentontheedge.sql.model.AstFieldReference)8 AstFunNode (com.developmentontheedge.sql.model.AstFunNode)4 AstSelectList (com.developmentontheedge.sql.model.AstSelectList)4 SimpleNode (com.developmentontheedge.sql.model.SimpleNode)4 AstDerivedColumn (com.developmentontheedge.sql.model.AstDerivedColumn)3 AstFrom (com.developmentontheedge.sql.model.AstFrom)3 AstIdentifierConstant (com.developmentontheedge.sql.model.AstIdentifierConstant)3 AstLimit (com.developmentontheedge.sql.model.AstLimit)3 AstSelect (com.developmentontheedge.sql.model.AstSelect)3 AstTableRef (com.developmentontheedge.sql.model.AstTableRef)3 AstWhere (com.developmentontheedge.sql.model.AstWhere)3 AstBetweenPredicate (com.developmentontheedge.sql.model.AstBetweenPredicate)2 AstWindowFunction (com.developmentontheedge.sql.model.AstWindowFunction)2 AstWindowSpecification (com.developmentontheedge.sql.model.AstWindowSpecification)2 Test (org.junit.Test)2 AstInsert (com.developmentontheedge.sql.model.AstInsert)1 AstJoin (com.developmentontheedge.sql.model.AstJoin)1 AstStringConstant (com.developmentontheedge.sql.model.AstStringConstant)1 AstUpdate (com.developmentontheedge.sql.model.AstUpdate)1 DbSpecificFunction (com.developmentontheedge.sql.model.DbSpecificFunction)1