Search in sources :

Example 16 with AstParenthesis

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

the class GenericDbmsTransformer method transformExtractExpression.

protected void transformExtractExpression(AstExtract extract) {
    String dateField = extract.getDateField().toUpperCase();
    SimpleNode child = extract.child(0);
    if (AstFunNode.isFunction("age").test(child)) {
        SimpleNode parent = extract.jjtGetParent();
        if ("YEAR".equals(dateField)) {
            SimpleNode grandParent = parent.jjtGetParent();
            SimpleNode toReplace = extract;
            if (AstFunNode.isFunction("*").test(parent) && AstFunNode.isFunction("+").test(grandParent) && AstExtract.isExtract("MONTH").test(grandParent.other(parent))) {
                dateField = "MONTH";
                toReplace = parent.jjtGetParent();
            }
            toReplace.replaceWith(getDateTimeDiff(child.child(1), child.child(0), dateField));
        } else if ("MONTH".equals(dateField) && AstFunNode.isFunction("+").test(parent) && AstFunNode.isFunction("*").test(parent.child(1)) && parent.child(1).children().anyMatch(AstExtract.isExtract("YEAR"))) {
            parent.jjtGetParent().replaceWith(getDateTimeDiff(child.child(1), child.child(0), dateField));
        }
    } else if (child instanceof AstParenthesis && AstFunNode.isFunction("-").test(child.child(0)) && ("DAY".equals(dateField) || "EPOCH".equals(dateField))) {
        AstFunNode date = (AstFunNode) child.child(0);
        extract.replaceWith(getDateTimeDiff(date.child(1), date.child(0), "EPOCH".equals(dateField) ? "SECOND" : "DAY"));
    } else
        transformExtract(extract);
}
Also used : AstFunNode(com.developmentontheedge.sql.model.AstFunNode) AstParenthesis(com.developmentontheedge.sql.model.AstParenthesis) SimpleNode(com.developmentontheedge.sql.model.SimpleNode)

Example 17 with AstParenthesis

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

the class ContextApplier method applySqlTag.

private void applySqlTag(AstBeSql sql) {
    if (sql.getExec().equals("include")) {
        String entityName = sql.getEntityName();
        String queryName = sql.getQueryName();
        String subQuery = context.resolveQuery(entityName, queryName);
        sql.replaceWith(SqlQuery.parse(subQuery).getQuery());
        return;
    }
    List<String> beautifiers = Arrays.asList("com.beanexplorer.web.html.HtmlLineGlueBeautifier");
    if (sql.jjtGetParent() instanceof AstInValueList) {
        beautifiers = Arrays.asList("com.beanexplorer.web.html.SqlInClauseQuotedBeautifier", "com.beanexplorer.web.html.SqlInClauseBeautifier");
    } else {
        List<String> inValueAttr = Arrays.asList("entity", "queryName", "filterKeyProperty", "filterKey", "filterValProperty", "filterVal", "outColumns");
        for (String attr : inValueAttr) if (sql.getParameter(attr) != null)
            throw new IllegalArgumentException("Unsupported attribute: " + attr);
    }
    if (!sql.getExec().equals("pre") || !beautifiers.contains(sql.getBeautifier()))
        throw new IllegalArgumentException("Unsupported attributes: exec=\"" + sql.getExec() + "\" beautifier=\"" + sql.getBeautifier() + "\"");
    if (sql.getDistinct().equalsIgnoreCase("yes"))
        new DistinctApplier().transformQuery(sql.getQuery());
    if (sql.getLimit() != null)
        new LimitsApplier(0, sql.getLimit()).transformQuery(sql.getQuery());
    sql.replaceWith(new AstParenthesis(sql.getQuery()));
}
Also used : AstInValueList(com.developmentontheedge.sql.model.AstInValueList) AstParenthesis(com.developmentontheedge.sql.model.AstParenthesis)

Aggregations

AstParenthesis (com.developmentontheedge.sql.model.AstParenthesis)17 SimpleNode (com.developmentontheedge.sql.model.SimpleNode)10 AstIdentifierConstant (com.developmentontheedge.sql.model.AstIdentifierConstant)7 Function (com.developmentontheedge.sql.model.Function)7 PredefinedFunction (com.developmentontheedge.sql.model.PredefinedFunction)7 AstFrom (com.developmentontheedge.sql.model.AstFrom)6 AstSelect (com.developmentontheedge.sql.model.AstSelect)6 AstQuery (com.developmentontheedge.sql.model.AstQuery)5 AstSelectList (com.developmentontheedge.sql.model.AstSelectList)5 AstTableRef (com.developmentontheedge.sql.model.AstTableRef)5 AstFunNode (com.developmentontheedge.sql.model.AstFunNode)4 AstStringConstant (com.developmentontheedge.sql.model.AstStringConstant)4 AstInterval (com.developmentontheedge.sql.model.AstInterval)3 AstWindowFunction (com.developmentontheedge.sql.model.AstWindowFunction)3 AstCast (com.developmentontheedge.sql.model.AstCast)2 AstDateAdd (com.developmentontheedge.sql.model.AstDateAdd)2 AstFirstDayOf (com.developmentontheedge.sql.model.AstFirstDayOf)2 AstOrderBy (com.developmentontheedge.sql.model.AstOrderBy)2 AstWhere (com.developmentontheedge.sql.model.AstWhere)2 AstBooleanExpression (com.developmentontheedge.sql.model.AstBooleanExpression)1