Search in sources :

Example 6 with FunctionNode

use of org.apache.cayenne.access.sqlbuilder.sqltree.FunctionNode 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)

Example 7 with FunctionNode

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

the class SybaseSQLTreeProcessor method onFunctionNode.

@Override
protected void onFunctionNode(Node parent, FunctionNode child, int index) {
    String functionName = child.getFunctionName();
    Node replacement = null;
    switch(functionName) {
        case "LENGTH":
            replacement = new FunctionNode("LEN", child.getAlias(), true);
            break;
        case "LOCATE":
            replacement = new FunctionNode("CHARINDEX", child.getAlias(), true);
            break;
        case "MOD":
            replacement = new OpExpressionNode("%");
            break;
        case "TRIM":
            Node rtrim = new FunctionNode("RTRIM", null, true);
            replacement = new FunctionNode("LTRIM", child.getAlias(), true);
            for (int i = 0; i < child.getChildrenCount(); i++) {
                rtrim.addChild(child.getChild(i));
            }
            replacement.addChild(rtrim);
            parent.replaceChild(index, replacement);
            return;
        case "CURRENT_DATE":
            replacement = new FunctionNode("{fn CURDATE()}", child.getAlias(), false);
            break;
        case "CURRENT_TIME":
            replacement = new FunctionNode("{fn CURTIME()}", child.getAlias(), false);
            break;
        case "CURRENT_TIMESTAMP":
            replacement = new FunctionNode("CURRENT_TIMESTAMP", child.getAlias(), false);
            break;
        case "YEAR":
        case "MONTH":
        case "WEEK":
        case "DAY_OF_YEAR":
        case "DAY":
        case "DAY_OF_MONTH":
        case "DAY_OF_WEEK":
        case "HOUR":
        case "MINUTE":
        case "SECOND":
            replacement = new FunctionNode("DATEPART", child.getAlias(), true);
            switch(functionName) {
                case "DAY_OF_MONTH":
                    functionName = "DAY";
                    break;
                case "DAY_OF_WEEK":
                    functionName = "WEEKDAY";
                    break;
                case "DAY_OF_YEAR":
                    functionName = "DAYOFYEAR";
                    break;
            }
            replacement.addChild(new TextNode(functionName));
            break;
    }
    if (replacement != null) {
        replaceChild(parent, index, replacement);
    }
}
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) TopNode(org.apache.cayenne.access.sqlbuilder.sqltree.TopNode) EmptyNode(org.apache.cayenne.access.sqlbuilder.sqltree.EmptyNode) FunctionNode(org.apache.cayenne.access.sqlbuilder.sqltree.FunctionNode) TextNode(org.apache.cayenne.access.sqlbuilder.sqltree.TextNode)

Example 8 with FunctionNode

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

the class SQLiteTreeProcessor method replaceExtractFunction.

private void replaceExtractFunction(Node parent, FunctionNode original, int index, String format) {
    Node replacement = new FunctionNode("cast", original.getAlias(), true) {

        @Override
        public void appendChildrenSeparator(QuotingAppendable buffer, int childIdx) {
            buffer.append(" as ");
        }
    };
    FunctionNode strftime = new FunctionNode("strftime", null, true);
    strftime.addChild(new TextNode(format));
    strftime.addChild(original.getChild(0));
    replacement.addChild(strftime);
    replacement.addChild(new TextNode("integer"));
    parent.replaceChild(index, replacement);
}
Also used : MysqlLimitOffsetNode(org.apache.cayenne.dba.mysql.sqltree.MysqlLimitOffsetNode) 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) FunctionNode(org.apache.cayenne.access.sqlbuilder.sqltree.FunctionNode) QuotingAppendable(org.apache.cayenne.access.sqlbuilder.QuotingAppendable) TextNode(org.apache.cayenne.access.sqlbuilder.sqltree.TextNode)

Example 9 with FunctionNode

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

the class SQLiteTreeProcessor method onFunctionNode.

@Override
protected void onFunctionNode(Node parent, FunctionNode child, int index) {
    String functionName = child.getFunctionName();
    Node replacement = null;
    switch(functionName) {
        case "LOCATE":
            replacement = new FunctionNode("INSTR", child.getAlias(), true);
            for (int i = 0; i <= 1; i++) {
                replacement.addChild(child.getChild(1 - i));
            }
            parent.replaceChild(index, replacement);
            return;
        case "DAY_OF_YEAR":
            replaceExtractFunction(parent, child, index, "'%j'");
            return;
        case "DAY_OF_WEEK":
            replaceExtractFunction(parent, child, index, "'%w'");
            return;
        case "WEEK":
            replaceExtractFunction(parent, child, index, "'%W'");
            return;
        case "YEAR":
            replaceExtractFunction(parent, child, index, "'%Y'");
            return;
        case "MONTH":
            replaceExtractFunction(parent, child, index, "'%m'");
            return;
        case "DAY":
        case "DAY_OF_MONTH":
            replaceExtractFunction(parent, child, index, "'%d'");
            return;
        case "HOUR":
            replaceExtractFunction(parent, child, index, "'%H'");
            return;
        case "MINUTE":
            replaceExtractFunction(parent, child, index, "'%M'");
            return;
        case "SECOND":
            replaceExtractFunction(parent, child, index, "'%S'");
            return;
        case "SUBSTRING":
            replacement = new FunctionNode("SUBSTR", child.getAlias(), true);
            break;
        case "CONCAT":
            replacement = new OpExpressionNode("||");
            break;
        case "MOD":
            replacement = new OpExpressionNode("%");
            break;
        case "CURRENT_DATE":
        case "CURRENT_TIMESTAMP":
        case "CURRENT_TIME":
            replacement = new FunctionNode(functionName, child.getAlias(), false);
            break;
    }
    if (replacement != null) {
        replaceChild(parent, index, replacement);
    }
}
Also used : OpExpressionNode(org.apache.cayenne.access.sqlbuilder.sqltree.OpExpressionNode) MysqlLimitOffsetNode(org.apache.cayenne.dba.mysql.sqltree.MysqlLimitOffsetNode) 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) FunctionNode(org.apache.cayenne.access.sqlbuilder.sqltree.FunctionNode)

Example 10 with FunctionNode

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

the class PostgreSQLTreeProcessor method onFunctionNode.

protected Optional<Node> onFunctionNode(Node parent, FunctionNode child, int index) {
    Node replacement = null;
    String functionName = child.getFunctionName();
    if (EXTRACT_FUNCTION_NAMES.contains(functionName)) {
        replacement = new PostgresExtractFunctionNode(functionName);
    } else if ("CURRENT_DATE".equals(functionName) || "CURRENT_TIME".equals(functionName) || "CURRENT_TIMESTAMP".equals(functionName)) {
        replacement = new FunctionNode(functionName, child.getAlias(), false);
    } else if ("LOCATE".equals(functionName)) {
        replacement = new PositionFunctionNode(child.getAlias());
    }
    return Optional.ofNullable(replacement);
}
Also used : PostgresExtractFunctionNode(org.apache.cayenne.dba.postgres.sqltree.PostgresExtractFunctionNode) Node(org.apache.cayenne.access.sqlbuilder.sqltree.Node) FunctionNode(org.apache.cayenne.access.sqlbuilder.sqltree.FunctionNode) LimitOffsetNode(org.apache.cayenne.access.sqlbuilder.sqltree.LimitOffsetNode) PositionFunctionNode(org.apache.cayenne.dba.postgres.sqltree.PositionFunctionNode) PostgresLikeNode(org.apache.cayenne.dba.postgres.sqltree.PostgresLikeNode) ColumnNode(org.apache.cayenne.access.sqlbuilder.sqltree.ColumnNode) PostgresLimitOffsetNode(org.apache.cayenne.dba.postgres.sqltree.PostgresLimitOffsetNode) LikeNode(org.apache.cayenne.access.sqlbuilder.sqltree.LikeNode) TrimmingColumnNode(org.apache.cayenne.access.sqlbuilder.sqltree.TrimmingColumnNode) PostgresExtractFunctionNode(org.apache.cayenne.dba.postgres.sqltree.PostgresExtractFunctionNode) PostgresExtractFunctionNode(org.apache.cayenne.dba.postgres.sqltree.PostgresExtractFunctionNode) FunctionNode(org.apache.cayenne.access.sqlbuilder.sqltree.FunctionNode) PositionFunctionNode(org.apache.cayenne.dba.postgres.sqltree.PositionFunctionNode) PositionFunctionNode(org.apache.cayenne.dba.postgres.sqltree.PositionFunctionNode)

Aggregations

FunctionNode (org.apache.cayenne.access.sqlbuilder.sqltree.FunctionNode)10 Node (org.apache.cayenne.access.sqlbuilder.sqltree.Node)8 LimitOffsetNode (org.apache.cayenne.access.sqlbuilder.sqltree.LimitOffsetNode)7 OpExpressionNode (org.apache.cayenne.access.sqlbuilder.sqltree.OpExpressionNode)7 TextNode (org.apache.cayenne.access.sqlbuilder.sqltree.TextNode)6 QuotingAppendable (org.apache.cayenne.access.sqlbuilder.QuotingAppendable)5 ColumnNode (org.apache.cayenne.access.sqlbuilder.sqltree.ColumnNode)3 TrimmingColumnNode (org.apache.cayenne.access.sqlbuilder.sqltree.TrimmingColumnNode)3 EmptyNode (org.apache.cayenne.access.sqlbuilder.sqltree.EmptyNode)2 InNode (org.apache.cayenne.access.sqlbuilder.sqltree.InNode)2 ValueNode (org.apache.cayenne.access.sqlbuilder.sqltree.ValueNode)2 MysqlLimitOffsetNode (org.apache.cayenne.dba.mysql.sqltree.MysqlLimitOffsetNode)2 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)1 LikeNode (org.apache.cayenne.access.sqlbuilder.sqltree.LikeNode)1 OffsetFetchNextNode (org.apache.cayenne.access.sqlbuilder.sqltree.OffsetFetchNextNode)1 TopNode (org.apache.cayenne.access.sqlbuilder.sqltree.TopNode)1 FirebirdLimitNode (org.apache.cayenne.dba.firebird.sqltree.FirebirdLimitNode)1 FirebirdSubstringFunctionNode (org.apache.cayenne.dba.firebird.sqltree.FirebirdSubstringFunctionNode)1 PositionFunctionNode (org.apache.cayenne.dba.postgres.sqltree.PositionFunctionNode)1 PostgresExtractFunctionNode (org.apache.cayenne.dba.postgres.sqltree.PostgresExtractFunctionNode)1