Search in sources :

Example 1 with MySqlIntervalExpr

use of com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlIntervalExpr in project druid by alibaba.

the class MySqlExprParser method parseInterval.

protected SQLExpr parseInterval() {
    accept(Token.INTERVAL);
    if (lexer.token() == Token.LPAREN) {
        lexer.nextToken();
        SQLMethodInvokeExpr methodInvokeExpr = new SQLMethodInvokeExpr("INTERVAL");
        if (lexer.token() != Token.RPAREN) {
            exprList(methodInvokeExpr.getParameters(), methodInvokeExpr);
        }
        accept(Token.RPAREN);
        if (// 
        methodInvokeExpr.getParameters().size() == 1 && lexer.token() == Token.IDENTIFIER) {
            SQLExpr value = methodInvokeExpr.getParameters().get(0);
            String unit = lexer.stringVal();
            lexer.nextToken();
            MySqlIntervalExpr intervalExpr = new MySqlIntervalExpr();
            intervalExpr.setValue(value);
            intervalExpr.setUnit(MySqlIntervalUnit.valueOf(unit.toUpperCase()));
            return intervalExpr;
        } else {
            return primaryRest(methodInvokeExpr);
        }
    } else {
        SQLExpr value = expr();
        if (lexer.token() != Token.IDENTIFIER) {
            throw new ParserException("Syntax error");
        }
        String unit = lexer.stringVal();
        lexer.nextToken();
        MySqlIntervalExpr intervalExpr = new MySqlIntervalExpr();
        intervalExpr.setValue(value);
        intervalExpr.setUnit(MySqlIntervalUnit.valueOf(unit.toUpperCase()));
        return intervalExpr;
    }
}
Also used : MySqlIntervalExpr(com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlIntervalExpr) ParserException(com.alibaba.druid.sql.parser.ParserException) SQLMethodInvokeExpr(com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 2 with MySqlIntervalExpr

use of com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlIntervalExpr in project dble by actiontech.

the class ItemDateAddInterval method toExpression.

@Override
public SQLExpr toExpression() {
    String funcName = funcName();
    if (dateSubInterval) {
        funcName = "date_sub";
    }
    SQLMethodInvokeExpr method = new SQLMethodInvokeExpr(funcName);
    method.addParameter(args.get(0).toExpression());
    MySqlIntervalExpr intervalExpr = new MySqlIntervalExpr();
    intervalExpr.setValue(args.get(1).toExpression());
    intervalExpr.setUnit(intType);
    method.addParameter(intervalExpr);
    return method;
}
Also used : MySqlIntervalExpr(com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlIntervalExpr) SQLMethodInvokeExpr(com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr)

Example 3 with MySqlIntervalExpr

use of com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlIntervalExpr in project dble by actiontech.

the class MySQLItemVisitor method endVisit.

@Override
public void endVisit(SQLMethodInvokeExpr x) {
    List<Item> args = visitExprList(x.getParameters());
    String funcName = x.getMethodName().toUpperCase();
    Map<String, Object> attributes = x.getAttributes();
    switch(funcName) {
        case "TRIM":
            if (attributes == null) {
                item = new ItemFuncTrim(args.get(0), TrimTypeEnum.DEFAULT);
            } else {
                TrimTypeEnum trimType = TrimTypeEnum.DEFAULT;
                String type = (String) attributes.get(ItemFuncKeyWord.TRIM_TYPE);
                if (type != null) {
                    trimType = TrimTypeEnum.valueOf(type);
                }
                if (attributes.get(ItemFuncKeyWord.FROM) == null) {
                    item = new ItemFuncTrim(args.get(0), trimType);
                } else {
                    SQLCharExpr from = (SQLCharExpr) attributes.get(ItemFuncKeyWord.FROM);
                    item = new ItemFuncTrim(args.get(0), getItem(from), trimType);
                }
            }
            break;
        case "CONVERT":
            if (args.size() >= 2) {
                throw new MySQLOutPutException(ErrorCode.ER_OPTIMIZER, "", "not supported  CONVERT(expr, type) ,please use CAST(expr AS type)");
            }
            if (attributes == null || attributes.get(ItemFuncKeyWord.USING) == null) {
                throw new MySQLOutPutException(ErrorCode.ER_OPTIMIZER, "", "CONVERT(... USING ...) is standard SQL syntax");
            }
            item = new ItemFuncConvCharset(args.get(0), (String) attributes.get(ItemFuncKeyWord.USING));
            break;
        case "CHAR":
            if (attributes == null || attributes.get(ItemFuncKeyWord.USING) == null) {
                attributes = x.getParameters().get(0).getAttributes();
            }
            if (attributes == null || attributes.get(ItemFuncKeyWord.USING) == null) {
                item = new ItemFuncChar(args, this.charsetIndex);
            } else {
                item = new ItemFuncChar(args, (String) attributes.get(ItemFuncKeyWord.USING));
            }
            break;
        case "ORD":
            item = new ItemFuncOrd(args, this.charsetIndex);
            break;
        case "ADDDATE":
            if (x.getParameters().get(1) instanceof SQLIntegerExpr) {
                item = new ItemDateAddInterval(args.get(0), args.get(1), MySqlIntervalUnit.DAY, false);
                break;
            }
        // fallthrough
        case "DATE_ADD":
            MySqlIntervalExpr intervalExpr = (MySqlIntervalExpr) (x.getParameters().get(1));
            item = new ItemDateAddInterval(args.get(0), getItem(intervalExpr.getValue()), getIntervalUnit(x.getParameters().get(1)), false);
            break;
        case "SUBDATE":
            if (x.getParameters().get(1) instanceof SQLIntegerExpr) {
                item = new ItemDateAddInterval(args.get(0), args.get(1), MySqlIntervalUnit.DAY, true);
                break;
            }
        // fallthrough
        case "DATE_SUB":
            MySqlIntervalExpr valueExpr = (MySqlIntervalExpr) (x.getParameters().get(1));
            item = new ItemDateAddInterval(args.get(0), getItem(valueExpr.getValue()), getIntervalUnit(x.getParameters().get(1)), true);
            break;
        case "TIMESTAMPADD":
            SQLIdentifierExpr addUnit = (SQLIdentifierExpr) x.getParameters().get(0);
            item = new ItemDateAddInterval(args.get(2), args.get(1), MySqlIntervalUnit.valueOf(addUnit.getSimpleName()), false);
            break;
        case "TIMESTAMPDIFF":
            SQLIdentifierExpr diffUnit = (SQLIdentifierExpr) x.getParameters().get(0);
            item = new ItemFuncTimestampDiff(args.get(1), args.get(2), MySqlIntervalUnit.valueOf(diffUnit.getSimpleName()));
            break;
        case "VAR_SAMP":
            item = new ItemSumVariance(args, 1, false, null);
            break;
        case "VAR_POP":
        case "VARIANCE":
            item = new ItemSumVariance(args, 0, false, null);
            break;
        case "STD":
        case "STDDEV":
        case "STDDEV_POP":
            item = new ItemSumStd(args, 0, false, null);
            break;
        case "STDDEV_SAMP":
            item = new ItemSumStd(args, 1, false, null);
            break;
        case "BIT_AND":
            item = new ItemSumAnd(args, false, null);
            break;
        case "BIT_OR":
            item = new ItemSumOr(args, false, null);
            break;
        case "BIT_XOR":
            item = new ItemSumXor(args, false, null);
            break;
        case "IF":
            item = new ItemFuncIf(args);
            break;
        case "GET_FORMAT":
            SQLExpr expr = x.getParameters().get(0);
            if (expr instanceof SQLIdentifierExpr) {
                Item arg0 = new ItemString(((SQLIdentifierExpr) expr).getName());
                args.set(0, arg0);
            } else {
                throw new MySQLOutPutException(ErrorCode.ER_OPTIMIZER, "", "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '" + expr.toString() + "'");
            }
            item = ItemCreate.getInstance().createNativeFunc(funcName, args);
            break;
        default:
            if (ItemCreate.getInstance().isNativeFunc(funcName)) {
                item = ItemCreate.getInstance().createNativeFunc(funcName, args);
            } else {
                // unKnownFunction
                item = new ItemFuncUnknown(funcName, args);
            }
            initName(x);
    }
    item.setWithSubQuery(getArgsSubQueryStatus(args));
    item.setCorrelatedSubQuery(getArgsCorrelatedSubQueryStatus(args));
}
Also used : ItemFuncUnknown(com.actiontech.dble.plan.common.item.function.unknown.ItemFuncUnknown) MySqlIntervalExpr(com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlIntervalExpr) SQLSelectOrderByItem(com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem) ItemFuncTimestampDiff(com.actiontech.dble.plan.common.item.function.timefunc.ItemFuncTimestampDiff) TrimTypeEnum(com.actiontech.dble.plan.common.item.function.strfunc.ItemFuncTrim.TrimTypeEnum) ItemFuncTrim(com.actiontech.dble.plan.common.item.function.strfunc.ItemFuncTrim) ItemDateAddInterval(com.actiontech.dble.plan.common.item.function.timefunc.ItemDateAddInterval) ItemFuncChar(com.actiontech.dble.plan.common.item.function.strfunc.ItemFuncChar) ItemFuncConvCharset(com.actiontech.dble.plan.common.item.function.castfunc.ItemFuncConvCharset) ItemFuncIf(com.actiontech.dble.plan.common.item.function.operator.controlfunc.ItemFuncIf) MySQLOutPutException(com.actiontech.dble.plan.common.exception.MySQLOutPutException) ItemFuncOrd(com.actiontech.dble.plan.common.item.function.strfunc.ItemFuncOrd)

Aggregations

MySqlIntervalExpr (com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlIntervalExpr)3 SQLMethodInvokeExpr (com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr)2 MySQLOutPutException (com.actiontech.dble.plan.common.exception.MySQLOutPutException)1 ItemFuncConvCharset (com.actiontech.dble.plan.common.item.function.castfunc.ItemFuncConvCharset)1 ItemFuncIf (com.actiontech.dble.plan.common.item.function.operator.controlfunc.ItemFuncIf)1 ItemFuncChar (com.actiontech.dble.plan.common.item.function.strfunc.ItemFuncChar)1 ItemFuncOrd (com.actiontech.dble.plan.common.item.function.strfunc.ItemFuncOrd)1 ItemFuncTrim (com.actiontech.dble.plan.common.item.function.strfunc.ItemFuncTrim)1 TrimTypeEnum (com.actiontech.dble.plan.common.item.function.strfunc.ItemFuncTrim.TrimTypeEnum)1 ItemDateAddInterval (com.actiontech.dble.plan.common.item.function.timefunc.ItemDateAddInterval)1 ItemFuncTimestampDiff (com.actiontech.dble.plan.common.item.function.timefunc.ItemFuncTimestampDiff)1 ItemFuncUnknown (com.actiontech.dble.plan.common.item.function.unknown.ItemFuncUnknown)1 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)1 SQLSelectOrderByItem (com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem)1 ParserException (com.alibaba.druid.sql.parser.ParserException)1