Search in sources :

Example 1 with OpExpressionNode

use of org.apache.cayenne.access.sqlbuilder.sqltree.OpExpressionNode in project cayenne by apache.

the class FrontBaseSQLTreeProcessor method onFunctionNode.

@Override
protected void onFunctionNode(Node parent, FunctionNode child, int index) {
    switch(child.getFunctionName()) {
        case "CONCAT":
            replaceChild(parent, index, new OpExpressionNode("||"));
            break;
        case "LOCATE":
            // POSITION (substr IN str)
            replaceChild(parent, index, new FunctionNode("POSITION", child.getAlias()) {

                @Override
                public void appendChildrenSeparator(QuotingAppendable buffer, int childIdx) {
                    buffer.append(" IN ");
                }
            });
            break;
        case "LENGTH":
            replaceChild(parent, index, new FunctionNode("CHAR_LENGTH", child.getAlias()));
            break;
        case "SUBSTRING":
            // SUBSTRING (str FROM offset FOR length)
            replaceChild(parent, index, new FunctionNode("SUBSTRING", child.getAlias()) {

                @Override
                public void appendChildrenSeparator(QuotingAppendable buffer, int childIdx) {
                    if (childIdx == 0) {
                        buffer.append(" FROM ");
                    } else if (childIdx == 1) {
                        buffer.append(" FOR ");
                    }
                }
            });
            break;
        case "YEAR":
        case "MONTH":
        case "DAY":
        case "DAY_OF_MONTH":
        case "HOUR":
        case "MINUTE":
        case "SECOND":
            Node functionReplacement = new ExtractFunctionNode(child.getAlias());
            String functionName = child.getFunctionName();
            if ("DAY_OF_MONTH".equals(functionName)) {
                functionName = "DAY";
            }
            functionReplacement.addChild(new TextNode(functionName));
            replaceChild(parent, index, functionReplacement);
            break;
        case "DAY_OF_WEEK":
        case "DAY_OF_YEAR":
        case "WEEK":
            throw new CayenneRuntimeException("Function %s() is unsupported in FrontBase.", child.getFunctionName());
    }
}
Also used : OpExpressionNode(org.apache.cayenne.access.sqlbuilder.sqltree.OpExpressionNode) TextNode(org.apache.cayenne.access.sqlbuilder.sqltree.TextNode) Node(org.apache.cayenne.access.sqlbuilder.sqltree.Node) FunctionNode(org.apache.cayenne.access.sqlbuilder.sqltree.FunctionNode) OpExpressionNode(org.apache.cayenne.access.sqlbuilder.sqltree.OpExpressionNode) FunctionNode(org.apache.cayenne.access.sqlbuilder.sqltree.FunctionNode) QuotingAppendable(org.apache.cayenne.access.sqlbuilder.QuotingAppendable) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) TextNode(org.apache.cayenne.access.sqlbuilder.sqltree.TextNode)

Example 2 with OpExpressionNode

use of org.apache.cayenne.access.sqlbuilder.sqltree.OpExpressionNode in project cayenne by apache.

the class IngressSQLTreeProcessor method onFunctionNode.

@Override
protected void onFunctionNode(Node parent, FunctionNode child, int index) {
    switch(child.getFunctionName()) {
        case "CONCAT":
            replaceChild(parent, index, new OpExpressionNode("+"));
            return;
        case "LOCATE":
            Node child0 = child.getChild(0);
            child.replaceChild(0, child.getChild(1));
            child.replaceChild(1, child0);
            return;
        case "SUBSTRING":
            Node replacement = new FunctionNode("SUBSTRING", child.getAlias(), true) {

                @Override
                public void appendChildrenSeparator(QuotingAppendable buffer, int childIdx) {
                    // 0, CAST(1 AS INTEGER), CAST(2 AS INTEGER)
                    if (childIdx == 1 || childIdx == 2) {
                        buffer.append(" AS INTEGER)");
                    }
                    if (childIdx == 0 || childIdx == 1) {
                        buffer.append(", CAST(");
                    }
                }

                @Override
                public void appendChildrenEnd(QuotingAppendable buffer) {
                    buffer.append(" AS INTEGER)");
                    super.appendChildrenEnd(buffer);
                }
            };
            replaceChild(parent, index, replacement);
            return;
        case "TRIM":
            replaceChild(parent, index, new FunctionNode("RTRIM(LTRIM", child.getAlias(), true) {

                @Override
                public void appendChildrenEnd(QuotingAppendable buffer) {
                    buffer.append(')');
                    super.appendChildrenEnd(buffer);
                }
            });
            return;
        case "DAY_OF_WEEK":
        case "DAY_OF_MONTH":
        case "DAY_OF_YEAR":
            // ingres variants are without '_'
            replaceChild(parent, index, new FunctionNode(child.getFunctionName().replace("_", ""), child.getAlias(), true));
            return;
    }
}
Also used : OpExpressionNode(org.apache.cayenne.access.sqlbuilder.sqltree.OpExpressionNode) ColumnNode(org.apache.cayenne.access.sqlbuilder.sqltree.ColumnNode) Node(org.apache.cayenne.access.sqlbuilder.sqltree.Node) FunctionNode(org.apache.cayenne.access.sqlbuilder.sqltree.FunctionNode) OpExpressionNode(org.apache.cayenne.access.sqlbuilder.sqltree.OpExpressionNode) LimitOffsetNode(org.apache.cayenne.access.sqlbuilder.sqltree.LimitOffsetNode) TrimmingColumnNode(org.apache.cayenne.access.sqlbuilder.sqltree.TrimmingColumnNode) OffsetFetchNextNode(org.apache.cayenne.access.sqlbuilder.sqltree.OffsetFetchNextNode) FunctionNode(org.apache.cayenne.access.sqlbuilder.sqltree.FunctionNode) QuotingAppendable(org.apache.cayenne.access.sqlbuilder.QuotingAppendable)

Example 3 with OpExpressionNode

use of org.apache.cayenne.access.sqlbuilder.sqltree.OpExpressionNode in project cayenne by apache.

the class HavingTranslationStageTest method perform.

@Test
public void perform() {
    HavingTranslationStage stage = new HavingTranslationStage();
    stage.perform(context);
    Node select = context.getSelectBuilder().build();
    // Content of "select" node:
    // 
    // Having
    // |
    // OpExpression
    // /        \
    // Column     Value
    assertEquals(1, select.getChildrenCount());
    assertThat(select.getChild(0), instanceOf(HavingNode.class));
    Node op = select.getChild(0).getChild(0);
    assertThat(op, instanceOf(OpExpressionNode.class));
    assertEquals(">=", ((OpExpressionNode) op).getOp());
    assertEquals(2, op.getChildrenCount());
    assertThat(op.getChild(0), instanceOf(ColumnNode.class));
    assertThat(op.getChild(1), instanceOf(ValueNode.class));
    ColumnNode columnNode = (ColumnNode) op.getChild(0);
    ValueNode valueNode = (ValueNode) op.getChild(1);
    assertEquals("path", columnNode.getColumn());
    assertEquals(10, valueNode.getValue());
}
Also used : HavingNode(org.apache.cayenne.access.sqlbuilder.sqltree.HavingNode) OpExpressionNode(org.apache.cayenne.access.sqlbuilder.sqltree.OpExpressionNode) Node(org.apache.cayenne.access.sqlbuilder.sqltree.Node) OpExpressionNode(org.apache.cayenne.access.sqlbuilder.sqltree.OpExpressionNode) ValueNode(org.apache.cayenne.access.sqlbuilder.sqltree.ValueNode) ColumnNode(org.apache.cayenne.access.sqlbuilder.sqltree.ColumnNode) HavingNode(org.apache.cayenne.access.sqlbuilder.sqltree.HavingNode) ColumnNode(org.apache.cayenne.access.sqlbuilder.sqltree.ColumnNode) ValueNode(org.apache.cayenne.access.sqlbuilder.sqltree.ValueNode) Test(org.junit.Test)

Example 4 with OpExpressionNode

use of org.apache.cayenne.access.sqlbuilder.sqltree.OpExpressionNode in project cayenne by apache.

the class OracleSQLTreeProcessor method onFunctionNode.

@Override
protected void onFunctionNode(Node parent, FunctionNode child, int index) {
    String functionName = child.getFunctionName();
    Node functionReplacement = null;
    switch(functionName) {
        case "LOCATE":
            functionReplacement = new FunctionNode("INSTR", child.getAlias(), true);
            for (int i = 0; i <= 1; i++) {
                functionReplacement.addChild(child.getChild(1 - i));
            }
            parent.replaceChild(index, functionReplacement);
            return;
        case "DAY_OF_YEAR":
        case "DAY_OF_WEEK":
        case "WEEK":
            functionReplacement = new FunctionNode("TO_CHAR", child.getAlias(), true);
            functionReplacement.addChild(child.getChild(0));
            if ("DAY_OF_YEAR".equals(functionName)) {
                functionName = "'DDD'";
            } else if ("DAY_OF_WEEK".equals(functionName)) {
                functionName = "'D'";
            } else {
                functionName = "'IW'";
            }
            functionReplacement.addChild(new TextNode(functionName));
            parent.replaceChild(index, functionReplacement);
            return;
        case "SUBSTRING":
            functionReplacement = new FunctionNode("SUBSTR", child.getAlias(), true);
            break;
        case "CONCAT":
            functionReplacement = new OpExpressionNode("||");
            break;
        case "CURRENT_TIMESTAMP":
        case "CURRENT_DATE":
            functionReplacement = new FunctionNode(functionName, child.getAlias(), false);
            break;
        case "CURRENT_TIME":
            functionReplacement = new FunctionNode("{fn CURTIME()}", child.getAlias(), false);
            break;
        case "YEAR":
        case "MONTH":
        case "DAY":
        case "DAY_OF_MONTH":
        case "HOUR":
        case "MINUTE":
        case "SECOND":
            functionReplacement = new FunctionNode("EXTRACT", child.getAlias(), true) {

                @Override
                public void appendChildrenSeparator(QuotingAppendable buffer, int childIdx) {
                    buffer.append(' ');
                }
            };
            if ("DAY_OF_MONTH".equals(functionName)) {
                functionName = "DAY";
            }
            functionReplacement.addChild(new TextNode(functionName + " FROM "));
            break;
    }
    if (functionReplacement != null) {
        replaceChild(parent, index, functionReplacement);
    }
}
Also used : OpExpressionNode(org.apache.cayenne.access.sqlbuilder.sqltree.OpExpressionNode) TextNode(org.apache.cayenne.access.sqlbuilder.sqltree.TextNode) Node(org.apache.cayenne.access.sqlbuilder.sqltree.Node) FunctionNode(org.apache.cayenne.access.sqlbuilder.sqltree.FunctionNode) OpExpressionNode(org.apache.cayenne.access.sqlbuilder.sqltree.OpExpressionNode) LimitOffsetNode(org.apache.cayenne.access.sqlbuilder.sqltree.LimitOffsetNode) EmptyNode(org.apache.cayenne.access.sqlbuilder.sqltree.EmptyNode) InNode(org.apache.cayenne.access.sqlbuilder.sqltree.InNode) ValueNode(org.apache.cayenne.access.sqlbuilder.sqltree.ValueNode) ColumnNode(org.apache.cayenne.access.sqlbuilder.sqltree.ColumnNode) TrimmingColumnNode(org.apache.cayenne.access.sqlbuilder.sqltree.TrimmingColumnNode) FunctionNode(org.apache.cayenne.access.sqlbuilder.sqltree.FunctionNode) QuotingAppendable(org.apache.cayenne.access.sqlbuilder.QuotingAppendable) TextNode(org.apache.cayenne.access.sqlbuilder.sqltree.TextNode)

Example 5 with OpExpressionNode

use of org.apache.cayenne.access.sqlbuilder.sqltree.OpExpressionNode in project cayenne by apache.

the class FirebirdSQLTreeProcessor method onFunctionNode.

@Override
protected void onFunctionNode(Node parent, FunctionNode child, int index) {
    switch(child.getFunctionName()) {
        case "LENGTH":
            replaceChild(parent, index, new FunctionNode("CHAR_LENGTH", child.getAlias()));
            break;
        case "LOCATE":
            replaceChild(parent, index, new FunctionNode("POSITION", child.getAlias()));
            break;
        case "CONCAT":
            replaceChild(parent, index, new OpExpressionNode("||"));
            break;
        case "SUBSTRING":
            replaceChild(parent, index, new FirebirdSubstringFunctionNode(child.getAlias()));
            break;
        case "YEAR":
        case "MONTH":
        case "DAY":
        case "DAY_OF_MONTH":
        case "DAY_OF_WEEK":
        case "DAY_OF_YEAR":
        case "WEEK":
        case "HOUR":
        case "MINUTE":
        case "SECOND":
            Node functionReplacement = new FunctionNode("EXTRACT", child.getAlias(), true) {

                @Override
                public void appendChildrenSeparator(QuotingAppendable buffer, int childIdx) {
                    buffer.append(' ');
                }
            };
            String partName = child.getFunctionName();
            if ("DAY_OF_MONTH".equals(partName)) {
                partName = "DAY";
            } else if ("DAY_OF_WEEK".equals(partName)) {
                partName = "WEEKDAY";
            } else if ("DAY_OF_YEAR".equals(partName)) {
                partName = "YEARDAY";
            }
            functionReplacement.addChild(new TextNode(partName + " FROM "));
            replaceChild(parent, index, functionReplacement);
            break;
    }
}
Also used : OpExpressionNode(org.apache.cayenne.access.sqlbuilder.sqltree.OpExpressionNode) TextNode(org.apache.cayenne.access.sqlbuilder.sqltree.TextNode) FirebirdSubstringFunctionNode(org.apache.cayenne.dba.firebird.sqltree.FirebirdSubstringFunctionNode) Node(org.apache.cayenne.access.sqlbuilder.sqltree.Node) FunctionNode(org.apache.cayenne.access.sqlbuilder.sqltree.FunctionNode) OpExpressionNode(org.apache.cayenne.access.sqlbuilder.sqltree.OpExpressionNode) LimitOffsetNode(org.apache.cayenne.access.sqlbuilder.sqltree.LimitOffsetNode) FirebirdLimitNode(org.apache.cayenne.dba.firebird.sqltree.FirebirdLimitNode) InNode(org.apache.cayenne.access.sqlbuilder.sqltree.InNode) ValueNode(org.apache.cayenne.access.sqlbuilder.sqltree.ValueNode) FirebirdSubstringFunctionNode(org.apache.cayenne.dba.firebird.sqltree.FirebirdSubstringFunctionNode) FunctionNode(org.apache.cayenne.access.sqlbuilder.sqltree.FunctionNode) QuotingAppendable(org.apache.cayenne.access.sqlbuilder.QuotingAppendable) TextNode(org.apache.cayenne.access.sqlbuilder.sqltree.TextNode) FirebirdSubstringFunctionNode(org.apache.cayenne.dba.firebird.sqltree.FirebirdSubstringFunctionNode)

Aggregations

Node (org.apache.cayenne.access.sqlbuilder.sqltree.Node)8 OpExpressionNode (org.apache.cayenne.access.sqlbuilder.sqltree.OpExpressionNode)8 FunctionNode (org.apache.cayenne.access.sqlbuilder.sqltree.FunctionNode)6 LimitOffsetNode (org.apache.cayenne.access.sqlbuilder.sqltree.LimitOffsetNode)5 TextNode (org.apache.cayenne.access.sqlbuilder.sqltree.TextNode)5 QuotingAppendable (org.apache.cayenne.access.sqlbuilder.QuotingAppendable)4 ColumnNode (org.apache.cayenne.access.sqlbuilder.sqltree.ColumnNode)4 ValueNode (org.apache.cayenne.access.sqlbuilder.sqltree.ValueNode)4 EmptyNode (org.apache.cayenne.access.sqlbuilder.sqltree.EmptyNode)2 InNode (org.apache.cayenne.access.sqlbuilder.sqltree.InNode)2 TrimmingColumnNode (org.apache.cayenne.access.sqlbuilder.sqltree.TrimmingColumnNode)2 Test (org.junit.Test)2 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)1 HavingNode (org.apache.cayenne.access.sqlbuilder.sqltree.HavingNode)1 OffsetFetchNextNode (org.apache.cayenne.access.sqlbuilder.sqltree.OffsetFetchNextNode)1 TopNode (org.apache.cayenne.access.sqlbuilder.sqltree.TopNode)1 WhereNode (org.apache.cayenne.access.sqlbuilder.sqltree.WhereNode)1 FirebirdLimitNode (org.apache.cayenne.dba.firebird.sqltree.FirebirdLimitNode)1 FirebirdSubstringFunctionNode (org.apache.cayenne.dba.firebird.sqltree.FirebirdSubstringFunctionNode)1 MysqlLimitOffsetNode (org.apache.cayenne.dba.mysql.sqltree.MysqlLimitOffsetNode)1