Search in sources :

Example 1 with SQLLiteralExpr

use of com.alibaba.druid.sql.ast.expr.SQLLiteralExpr in project druid by alibaba.

the class MySqlStatementParser method parseLoadDataInFile.

protected MySqlLoadDataInFileStatement parseLoadDataInFile() {
    acceptIdentifier("DATA");
    MySqlLoadDataInFileStatement stmt = new MySqlLoadDataInFileStatement();
    if (identifierEquals(LOW_PRIORITY)) {
        stmt.setLowPriority(true);
        lexer.nextToken();
    }
    if (identifierEquals("CONCURRENT")) {
        stmt.setConcurrent(true);
        lexer.nextToken();
    }
    if (identifierEquals(LOCAL)) {
        stmt.setLocal(true);
        lexer.nextToken();
    }
    acceptIdentifier("INFILE");
    SQLLiteralExpr fileName = (SQLLiteralExpr) exprParser.expr();
    stmt.setFileName(fileName);
    if (lexer.token() == Token.REPLACE) {
        stmt.setReplicate(true);
        lexer.nextToken();
    }
    if (identifierEquals(IGNORE)) {
        stmt.setIgnore(true);
        lexer.nextToken();
    }
    accept(Token.INTO);
    accept(Token.TABLE);
    SQLName tableName = exprParser.name();
    stmt.setTableName(tableName);
    if (identifierEquals(CHARACTER)) {
        lexer.nextToken();
        accept(Token.SET);
        if (lexer.token() != Token.LITERAL_CHARS) {
            throw new ParserException("syntax error, illegal charset");
        }
        String charset = lexer.stringVal();
        lexer.nextToken();
        stmt.setCharset(charset);
    }
    if (identifierEquals("FIELDS") || identifierEquals("COLUMNS")) {
        lexer.nextToken();
        if (identifierEquals("TERMINATED")) {
            lexer.nextToken();
            accept(Token.BY);
            stmt.setColumnsTerminatedBy(new SQLCharExpr(lexer.stringVal()));
            lexer.nextToken();
        }
        if (identifierEquals("OPTIONALLY")) {
            stmt.setColumnsEnclosedOptionally(true);
            lexer.nextToken();
        }
        if (identifierEquals("ENCLOSED")) {
            lexer.nextToken();
            accept(Token.BY);
            stmt.setColumnsEnclosedBy(new SQLCharExpr(lexer.stringVal()));
            lexer.nextToken();
        }
        if (identifierEquals("ESCAPED")) {
            lexer.nextToken();
            accept(Token.BY);
            stmt.setColumnsEscaped(new SQLCharExpr(lexer.stringVal()));
            lexer.nextToken();
        }
    }
    if (identifierEquals("LINES")) {
        lexer.nextToken();
        if (identifierEquals("STARTING")) {
            lexer.nextToken();
            accept(Token.BY);
            stmt.setLinesStartingBy(new SQLCharExpr(lexer.stringVal()));
            lexer.nextToken();
        }
        if (identifierEquals("TERMINATED")) {
            lexer.nextToken();
            accept(Token.BY);
            stmt.setLinesTerminatedBy(new SQLCharExpr(lexer.stringVal()));
            lexer.nextToken();
        }
    }
    if (identifierEquals(IGNORE)) {
        lexer.nextToken();
        stmt.setIgnoreLinesNumber(this.exprParser.expr());
        acceptIdentifier("LINES");
    }
    if (lexer.token() == Token.LPAREN) {
        lexer.nextToken();
        this.exprParser.exprList(stmt.getColumns(), stmt);
        accept(Token.RPAREN);
    }
    if (lexer.token() == Token.SET) {
        lexer.nextToken();
        this.exprParser.exprList(stmt.getSetList(), stmt);
    }
    return stmt;
}
Also used : SQLLiteralExpr(com.alibaba.druid.sql.ast.expr.SQLLiteralExpr) ParserException(com.alibaba.druid.sql.parser.ParserException) SQLCharExpr(com.alibaba.druid.sql.ast.expr.SQLCharExpr) SQLName(com.alibaba.druid.sql.ast.SQLName) MySqlLoadDataInFileStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlLoadDataInFileStatement)

Example 2 with SQLLiteralExpr

use of com.alibaba.druid.sql.ast.expr.SQLLiteralExpr in project druid by alibaba.

the class ParameterizedOutputVisitorUtils method mergeEqual.

private static boolean mergeEqual(SQLExpr a, SQLExpr b) {
    if (!(a instanceof SQLBinaryOpExpr)) {
        return false;
    }
    if (!(b instanceof SQLBinaryOpExpr)) {
        return false;
    }
    SQLBinaryOpExpr binaryA = (SQLBinaryOpExpr) a;
    SQLBinaryOpExpr binaryB = (SQLBinaryOpExpr) b;
    if (binaryA.getOperator() != SQLBinaryOperator.Equality) {
        return false;
    }
    if (binaryB.getOperator() != SQLBinaryOperator.Equality) {
        return false;
    }
    if (!(binaryA.getRight() instanceof SQLLiteralExpr || binaryA.getRight() instanceof SQLVariantRefExpr)) {
        return false;
    }
    if (!(binaryB.getRight() instanceof SQLLiteralExpr || binaryB.getRight() instanceof SQLVariantRefExpr)) {
        return false;
    }
    return binaryA.getLeft().toString().equals(binaryB.getLeft().toString());
}
Also used : SQLLiteralExpr(com.alibaba.druid.sql.ast.expr.SQLLiteralExpr) SQLVariantRefExpr(com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)

Example 3 with SQLLiteralExpr

use of com.alibaba.druid.sql.ast.expr.SQLLiteralExpr in project druid by alibaba.

the class ParameterizedOutputVisitorUtils method merge.

public static SQLBinaryOpExpr merge(ParameterizedVisitor v, SQLBinaryOpExpr x) {
    SQLExpr left = x.getLeft();
    SQLExpr right = x.getRight();
    SQLObject parent = x.getParent();
    if (left instanceof SQLLiteralExpr && right instanceof SQLLiteralExpr) {
        if (//
        x.getOperator() == SQLBinaryOperator.Equality || x.getOperator() == SQLBinaryOperator.NotEqual) {
            if ((left instanceof SQLIntegerExpr) && (right instanceof SQLIntegerExpr)) {
                if (((SQLIntegerExpr) left).getNumber().intValue() < 100) {
                    left.putAttribute(ATTR_PARAMS_SKIP, true);
                }
                if (((SQLIntegerExpr) right).getNumber().intValue() < 100) {
                    right.putAttribute(ATTR_PARAMS_SKIP, true);
                }
            } else {
                left.putAttribute(ATTR_PARAMS_SKIP, true);
                right.putAttribute(ATTR_PARAMS_SKIP, true);
            }
        }
        return x;
    }
    for (; ; ) {
        if (x.getRight() instanceof SQLBinaryOpExpr) {
            if (x.getLeft() instanceof SQLBinaryOpExpr) {
                SQLBinaryOpExpr leftBinaryExpr = (SQLBinaryOpExpr) x.getLeft();
                if (leftBinaryExpr.getRight().equals(x.getRight())) {
                    x = leftBinaryExpr;
                    v.incrementReplaceCunt();
                    continue;
                }
            }
            SQLExpr mergedRight = merge(v, (SQLBinaryOpExpr) x.getRight());
            if (mergedRight != x.getRight()) {
                x = new SQLBinaryOpExpr(x.getLeft(), x.getOperator(), mergedRight);
                v.incrementReplaceCunt();
            }
            x.setParent(parent);
        }
        break;
    }
    if (x.getLeft() instanceof SQLBinaryOpExpr) {
        SQLExpr mergedLeft = merge(v, (SQLBinaryOpExpr) x.getLeft());
        if (mergedLeft != x.getLeft()) {
            SQLBinaryOpExpr tmp = new SQLBinaryOpExpr(mergedLeft, x.getOperator(), x.getRight());
            tmp.setParent(parent);
            x = tmp;
            v.incrementReplaceCunt();
        }
    }
    // ID = ? OR ID = ? => ID = ?
    if (x.getOperator() == SQLBinaryOperator.BooleanOr) {
        if ((x.getLeft() instanceof SQLBinaryOpExpr) && (x.getRight() instanceof SQLBinaryOpExpr)) {
            SQLBinaryOpExpr leftBinary = (SQLBinaryOpExpr) x.getLeft();
            SQLBinaryOpExpr rightBinary = (SQLBinaryOpExpr) x.getRight();
            if (mergeEqual(leftBinary, rightBinary)) {
                v.incrementReplaceCunt();
                leftBinary.setParent(x.getParent());
                putMergedArribute(leftBinary, rightBinary);
                return leftBinary;
            }
            if (//
            isLiteralExpr(leftBinary.getLeft()) && leftBinary.getOperator() == SQLBinaryOperator.BooleanOr) {
                if (mergeEqual(leftBinary.getRight(), x.getRight())) {
                    v.incrementReplaceCunt();
                    putMergedArribute(leftBinary, rightBinary);
                    return leftBinary;
                }
            }
        }
    }
    return x;
}
Also used : SQLLiteralExpr(com.alibaba.druid.sql.ast.expr.SQLLiteralExpr) SQLObject(com.alibaba.druid.sql.ast.SQLObject) SQLIntegerExpr(com.alibaba.druid.sql.ast.expr.SQLIntegerExpr) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 4 with SQLLiteralExpr

use of com.alibaba.druid.sql.ast.expr.SQLLiteralExpr in project druid by alibaba.

the class WallVisitorUtils method isSimpleConstExpr.

private static boolean isSimpleConstExpr(SQLExpr sqlExpr) {
    List<SQLExpr> parts = getParts(sqlExpr);
    if (parts.isEmpty()) {
        return false;
    }
    for (SQLExpr part : parts) {
        if (isFirst(part)) {
            Object evalValue = part.getAttribute(EVAL_VALUE);
            if (evalValue == null) {
                if (part instanceof SQLBooleanExpr) {
                    evalValue = ((SQLBooleanExpr) part).getValue();
                } else if (part instanceof SQLNumericLiteralExpr) {
                    evalValue = ((SQLNumericLiteralExpr) part).getNumber();
                } else if (part instanceof SQLCharExpr) {
                    evalValue = ((SQLCharExpr) part).getText();
                } else if (part instanceof SQLNCharExpr) {
                    evalValue = ((SQLNCharExpr) part).getText();
                }
            }
            Boolean result = SQLEvalVisitorUtils.castToBoolean(evalValue);
            if (result != null && result) {
                return true;
            }
        }
        boolean isSimpleConstExpr = false;
        if (part == sqlExpr || part instanceof SQLLiteralExpr) {
            isSimpleConstExpr = true;
        } else if (part instanceof SQLBinaryOpExpr) {
            SQLBinaryOpExpr binaryOpExpr = (SQLBinaryOpExpr) part;
            if (binaryOpExpr.getOperator() == SQLBinaryOperator.Equality || binaryOpExpr.getOperator() == SQLBinaryOperator.NotEqual || binaryOpExpr.getOperator() == SQLBinaryOperator.GreaterThan) {
                if (binaryOpExpr.getLeft() instanceof SQLIntegerExpr && binaryOpExpr.getRight() instanceof SQLIntegerExpr) {
                    isSimpleConstExpr = true;
                }
            }
        }
        if (!isSimpleConstExpr) {
            return false;
        }
    }
    return true;
}
Also used : SQLBooleanExpr(com.alibaba.druid.sql.ast.expr.SQLBooleanExpr) SQLNumericLiteralExpr(com.alibaba.druid.sql.ast.expr.SQLNumericLiteralExpr) SQLCharExpr(com.alibaba.druid.sql.ast.expr.SQLCharExpr) SQLLiteralExpr(com.alibaba.druid.sql.ast.expr.SQLLiteralExpr) SQLIntegerExpr(com.alibaba.druid.sql.ast.expr.SQLIntegerExpr) SQLObject(com.alibaba.druid.sql.ast.SQLObject) SQLNCharExpr(com.alibaba.druid.sql.ast.expr.SQLNCharExpr) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 5 with SQLLiteralExpr

use of com.alibaba.druid.sql.ast.expr.SQLLiteralExpr in project Mycat_plus by coderczp.

the class ServerLoadDataInfileHandler method buildResultSet.

private RouteResultset buildResultSet(Map<String, LoadData> routeMap) {
    // 强制local
    statement.setLocal(true);
    // 默认druid会过滤掉路径的分隔符,所以这里重新设置下
    SQLLiteralExpr fn = new SQLCharExpr(fileName);
    statement.setFileName(fn);
    String srcStatement = statement.toString();
    RouteResultset rrs = new RouteResultset(srcStatement, ServerParse.LOAD_DATA_INFILE_SQL);
    rrs.setLoadData(true);
    rrs.setStatement(srcStatement);
    rrs.setAutocommit(serverConnection.isAutocommit());
    rrs.setFinishedRoute(true);
    int size = routeMap.size();
    RouteResultsetNode[] routeResultsetNodes = new RouteResultsetNode[size];
    int index = 0;
    for (String dn : routeMap.keySet()) {
        RouteResultsetNode rrNode = new RouteResultsetNode(dn, ServerParse.LOAD_DATA_INFILE_SQL, srcStatement);
        rrNode.setSource(rrs);
        rrNode.setTotalNodeSize(size);
        rrNode.setStatement(srcStatement);
        LoadData newLoadData = new LoadData();
        ObjectUtil.copyProperties(loadData, newLoadData);
        newLoadData.setLocal(true);
        LoadData loadData1 = routeMap.get(dn);
        // if (isHasStoreToFile)
        if (// 此处判断是否有保存分库load的临时文件dn1.txt/dn2.txt,不是判断是否有clientTemp.txt
        loadData1.getFileName() != null) {
            newLoadData.setFileName(loadData1.getFileName());
        } else {
            newLoadData.setData(loadData1.getData());
        }
        rrNode.setLoadData(newLoadData);
        routeResultsetNodes[index] = rrNode;
        index++;
    }
    rrs.setNodes(routeResultsetNodes);
    return rrs;
}
Also used : SQLLiteralExpr(com.alibaba.druid.sql.ast.expr.SQLLiteralExpr) SQLCharExpr(com.alibaba.druid.sql.ast.expr.SQLCharExpr) LoadData(io.mycat.sqlengine.mpp.LoadData) RouteResultsetNode(io.mycat.route.RouteResultsetNode) RouteResultset(io.mycat.route.RouteResultset)

Aggregations

SQLLiteralExpr (com.alibaba.druid.sql.ast.expr.SQLLiteralExpr)11 SQLCharExpr (com.alibaba.druid.sql.ast.expr.SQLCharExpr)8 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)4 SQLName (com.alibaba.druid.sql.ast.SQLName)4 SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)3 SQLObject (com.alibaba.druid.sql.ast.SQLObject)2 SQLIntegerExpr (com.alibaba.druid.sql.ast.expr.SQLIntegerExpr)2 ParserException (com.alibaba.druid.sql.parser.ParserException)2 RouteResultset (io.mycat.route.RouteResultset)2 RouteResultsetNode (io.mycat.route.RouteResultsetNode)2 LoadData (io.mycat.sqlengine.mpp.LoadData)2 RouteResultset (com.actiontech.dble.route.RouteResultset)1 RouteResultsetNode (com.actiontech.dble.route.RouteResultsetNode)1 LoadData (com.actiontech.dble.sqlengine.mpp.LoadData)1 SQLCommentHint (com.alibaba.druid.sql.ast.SQLCommentHint)1 SQLBooleanExpr (com.alibaba.druid.sql.ast.expr.SQLBooleanExpr)1 SQLMethodInvokeExpr (com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr)1 SQLNCharExpr (com.alibaba.druid.sql.ast.expr.SQLNCharExpr)1 SQLNumericLiteralExpr (com.alibaba.druid.sql.ast.expr.SQLNumericLiteralExpr)1 SQLVariantRefExpr (com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr)1