Search in sources :

Example 1 with MySqlDeleteStatement

use of com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlDeleteStatement in project druid by alibaba.

the class MySqlStatementParser method parseDeleteStatement.

public MySqlDeleteStatement parseDeleteStatement() {
    MySqlDeleteStatement deleteStatement = new MySqlDeleteStatement();
    if (lexer.token() == Token.DELETE) {
        lexer.nextToken();
        if (lexer.token() == Token.COMMENT) {
            lexer.nextToken();
        }
        if (lexer.token() == Token.HINT) {
            this.getExprParser().parseHints(deleteStatement.getHints());
        }
        if (identifierEquals(LOW_PRIORITY)) {
            deleteStatement.setLowPriority(true);
            lexer.nextToken();
        }
        if (identifierEquals("QUICK")) {
            deleteStatement.setQuick(true);
            lexer.nextToken();
        }
        if (identifierEquals(IGNORE)) {
            deleteStatement.setIgnore(true);
            lexer.nextToken();
        }
        if (lexer.token() == Token.IDENTIFIER) {
            deleteStatement.setTableSource(createSQLSelectParser().parseTableSource());
            if (lexer.token() == Token.FROM) {
                lexer.nextToken();
                SQLTableSource tableSource = createSQLSelectParser().parseTableSource();
                deleteStatement.setFrom(tableSource);
            }
        } else if (lexer.token() == Token.FROM) {
            lexer.nextToken();
            deleteStatement.setTableSource(createSQLSelectParser().parseTableSource());
        } else {
            throw new ParserException("syntax error");
        }
        if (identifierEquals("USING")) {
            lexer.nextToken();
            SQLTableSource tableSource = createSQLSelectParser().parseTableSource();
            deleteStatement.setUsing(tableSource);
        }
    }
    if (lexer.token() == (Token.WHERE)) {
        lexer.nextToken();
        SQLExpr where = this.exprParser.expr();
        deleteStatement.setWhere(where);
    }
    if (lexer.token() == (Token.ORDER)) {
        SQLOrderBy orderBy = exprParser.parseOrderBy();
        deleteStatement.setOrderBy(orderBy);
    }
    deleteStatement.setLimit(this.exprParser.parseLimit());
    return deleteStatement;
}
Also used : ParserException(com.alibaba.druid.sql.parser.ParserException) MySqlDeleteStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlDeleteStatement) SQLOrderBy(com.alibaba.druid.sql.ast.SQLOrderBy) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 2 with MySqlDeleteStatement

use of com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlDeleteStatement in project druid by alibaba.

the class WallVisitorUtils method checkDelete.

public static void checkDelete(WallVisitor visitor, SQLDeleteStatement x) {
    checkReadOnly(visitor, x.getTableSource());
    WallConfig config = visitor.getConfig();
    if (!config.isDeleteAllow()) {
        addViolation(visitor, ErrorCode.INSERT_NOT_ALLOW, "delete not allow", x);
        return;
    }
    boolean hasUsing = false;
    if (x instanceof MySqlDeleteStatement) {
        hasUsing = ((MySqlDeleteStatement) x).getUsing() != null;
    }
    boolean isJoinTableSource = x.getTableSource() instanceof SQLJoinTableSource;
    if (x.getWhere() == null && (!hasUsing) && !isJoinTableSource) {
        WallContext context = WallContext.current();
        if (context != null) {
            context.incrementDeleteNoneConditionWarnings();
        }
        if (config.isDeleteWhereNoneCheck()) {
            addViolation(visitor, ErrorCode.NONE_CONDITION, "delete none condition not allow", x);
            return;
        }
    }
    SQLExpr where = x.getWhere();
    if (where != null) {
        checkCondition(visitor, where);
        if (Boolean.TRUE == getConditionValue(visitor, where, config.isDeleteWhereAlwayTrueCheck())) {
            if (config.isDeleteWhereAlwayTrueCheck() && visitor.isSqlEndOfComment() && !isSimpleConstExpr(where)) {
                addViolation(visitor, ErrorCode.ALWAYS_TRUE, "delete alway true condition not allow", x);
            }
        }
    }
// checkConditionForMultiTenant(visitor, x.getWhere(), x);
}
Also used : MySqlDeleteStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlDeleteStatement) WallConfig(com.alibaba.druid.wall.WallConfig) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) WallContext(com.alibaba.druid.wall.WallContext)

Example 3 with MySqlDeleteStatement

use of com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlDeleteStatement in project Mycat_plus by coderczp.

the class DruidDeleteParser method statementParse.

@Override
public void statementParse(SchemaConfig schema, RouteResultset rrs, SQLStatement stmt) throws SQLNonTransientException {
    MySqlDeleteStatement delete = (MySqlDeleteStatement) stmt;
    String tableName = StringUtil.removeBackquote(delete.getTableName().getSimpleName().toUpperCase());
    ctx.addTable(tableName);
}
Also used : MySqlDeleteStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlDeleteStatement)

Example 4 with MySqlDeleteStatement

use of com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlDeleteStatement in project druid by alibaba.

the class MySqlSelectTest_177_hints method testHintAll.

@Test
public void testHintAll() throws Exception {
    String[] hintHead = { "/*+TDDL:node('node_name1')*/" // "/*+ TDDL({ 'extra' :{ 'MERGE_UNION' : 'false' }, 'type' : 'direct', 'vtab' : 'drds_shard', 'dbid' : 'corona_qatest_1', 'realtabs' :[ 'drds_shard_04', 'drds_shard_05', 'drds_shard_06' ]}) */",
    // "/!+TDDL: node('node_name1')*/",
    // "/!TDDL: MASTER()*/",
    // "/*TDDL: MASTER()*/"
    };
    for (int i = 0; i < hintHead.length; i++) {
        String sql1 = hintHead[i] + "delete" + hintHead[i] + " from table_1 where id < 10;";
        String sql2 = hintHead[i] + "insert" + hintHead[i] + " into table_1 values('id < 10');";
        String sql3 = hintHead[i] + "update" + hintHead[i] + " table_1 set id = 1 where id < 10;";
        String sql4 = "explain " + hintHead[i] + "delete" + hintHead[i] + " from table_1 where id < 10;";
        String sql5 = "explain " + hintHead[i] + "insert" + hintHead[i] + " into table_1 values('id < 10');";
        String sql6 = "explain " + hintHead[i] + "update" + hintHead[i] + " table_1 set id = 1 where id < 10;";
        String sql7 = hintHead[i] + "replace" + hintHead[i] + " into table_1 values('id < 10');";
        String[] sql = { sql1, sql2, sql7, sql3, sql4, sql5, sql6 };
        for (int j = 0; j < sql.length; j++) {
            System.out.println(sql[j]);
            String parameterizedSql = ParameterizedOutputVisitorUtils.parameterizeForTDDL(sql[j], DbType.mysql, new ArrayList<Object>(), VisitorFeature.OutputParameterizedQuesUnMergeInList, VisitorFeature.OutputParameterizedUnMergeShardingTable, VisitorFeature.OutputParameterizedQuesUnMergeValuesList, VisitorFeature.OutputParameterizedQuesUnMergeOr);
            MySqlStatementParser parser = new MySqlStatementParser(parameterizedSql, SQLParserFeature.TDDLHint, SQLParserFeature.IgnoreNameQuotes);
            try {
                List<SQLStatement> stmtList = parser.parseStatementList();
                for (SQLStatement statement : stmtList) {
                    if (statement instanceof MySqlInsertStatement) {
                        Assert.assertNotNull(((MySqlInsertStatement) statement).getHeadHintsDirect().size() > 0);
                        Assert.assertNotNull(((MySqlInsertStatement) statement).getHint() != null);
                        System.out.println(((MySqlInsertStatement) statement).getHeadHintsDirect());
                        System.out.println(((MySqlInsertStatement) statement).getHint());
                    } else if (statement instanceof SQLReplaceStatement) {
                        Assert.assertNotNull(((SQLReplaceStatement) statement).getHeadHintsDirect().size() > 0);
                        Assert.assertNotNull(((SQLReplaceStatement) statement).getHints().size() > 0);
                        System.out.println(((SQLReplaceStatement) statement).getHeadHintsDirect());
                        System.out.println(((SQLReplaceStatement) statement).getHints());
                    } else if (statement instanceof MySqlDeleteStatement) {
                        Assert.assertNotNull(((MySqlDeleteStatement) statement).getHeadHintsDirect().size() > 0);
                        Assert.assertNotNull(((MySqlDeleteStatement) statement).getHints().size() > 0);
                        System.out.println(((MySqlDeleteStatement) statement).getHeadHintsDirect());
                        System.out.println(((MySqlDeleteStatement) statement).getHints());
                    } else if (statement instanceof MySqlUpdateStatement) {
                        Assert.assertNotNull(((MySqlUpdateStatement) statement).getHeadHintsDirect().size() > 0);
                        Assert.assertNotNull(((MySqlUpdateStatement) statement).getHints().size() > 0);
                        System.out.println(((MySqlUpdateStatement) statement).getHeadHintsDirect());
                        System.out.println(((MySqlUpdateStatement) statement).getHints());
                    } else if (statement instanceof MySqlExplainStatement) {
                        SQLStatement statement1 = ((MySqlExplainStatement) statement).getStatement();
                        if (statement1 instanceof MySqlInsertStatement) {
                            Assert.assertNotNull(((MySqlInsertStatement) statement1).getHint() != null);
                            System.out.println(((MySqlInsertStatement) statement1).getHeadHintsDirect());
                            System.out.println(((MySqlInsertStatement) statement1).getHint());
                        } else if (statement1 instanceof MySqlDeleteStatement) {
                            Assert.assertNotNull(((MySqlDeleteStatement) statement1).getHints().size() > 0);
                            System.out.println(((MySqlDeleteStatement) statement1).getHeadHintsDirect());
                            System.out.println(((MySqlDeleteStatement) statement1).getHints());
                        } else if (statement1 instanceof MySqlUpdateStatement) {
                            Assert.assertNotNull(((MySqlUpdateStatement) statement1).getHints().size() > 0);
                            System.out.println(((MySqlUpdateStatement) statement1).getHeadHintsDirect());
                            System.out.println(((MySqlUpdateStatement) statement1).getHints());
                        }
                        Assert.assertNotNull(((MySqlExplainStatement) statement).getHints().size() > 0);
                        System.out.println(((MySqlExplainStatement) statement).getHints());
                    }
                }
            } catch (com.alibaba.druid.sql.parser.ParserException e) {
                throw e;
            }
        }
    }
}
Also used : MySqlExplainStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlExplainStatement) MySqlInsertStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) MySqlUpdateStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlUpdateStatement) MySqlDeleteStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlDeleteStatement) SQLReplaceStatement(com.alibaba.druid.sql.ast.statement.SQLReplaceStatement) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) MysqlTest(com.alibaba.druid.sql.MysqlTest) Test(org.junit.Test)

Example 5 with MySqlDeleteStatement

use of com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlDeleteStatement in project dble by actiontech.

the class DruidDeleteParser method visitorParse.

@Override
public SchemaConfig visitorParse(SchemaConfig schema, RouteResultset rrs, SQLStatement stmt, ServerSchemaStatVisitor visitor, ServerConnection sc) throws SQLException {
    String schemaName = schema == null ? null : schema.getName();
    MySqlDeleteStatement delete = (MySqlDeleteStatement) stmt;
    SQLTableSource tableSource = delete.getTableSource();
    SQLTableSource fromSource = delete.getFrom();
    if (fromSource != null) {
        tableSource = fromSource;
    }
    if (tableSource instanceof SQLJoinTableSource) {
        StringPtr sqlSchema = new StringPtr(null);
        if (!SchemaUtil.isNoSharding(sc, (SQLJoinTableSource) tableSource, stmt, schemaName, sqlSchema)) {
            String msg = "DELETE query with multiple tables is not supported, sql:" + stmt;
            throw new SQLNonTransientException(msg);
        } else {
            if (delete.getWhere() != null && !SchemaUtil.isNoSharding(sc, delete.getWhere(), schemaName, sqlSchema)) {
                String msg = "DELETE query with sub-query is not supported, sql:" + stmt;
                throw new SQLNonTransientException(msg);
            }
            String realSchema = sqlSchema.get() == null ? schemaName : sqlSchema.get();
            SchemaConfig schemaConfig = DbleServer.getInstance().getConfig().getSchemas().get(realSchema);
            rrs.setStatement(RouterUtil.removeSchema(rrs.getStatement(), realSchema));
            RouterUtil.routeToSingleNode(rrs, schemaConfig.getDataNode());
            rrs.setFinishedRoute(true);
            return schema;
        }
    } else {
        SQLExprTableSource deleteTableSource = (SQLExprTableSource) tableSource;
        SchemaInfo schemaInfo = SchemaUtil.getSchemaInfo(sc.getUser(), schemaName, deleteTableSource);
        if (!ServerPrivileges.checkPrivilege(sc, schemaInfo.getSchema(), schemaInfo.getTable(), CheckType.DELETE)) {
            String msg = "The statement DML privilege check is not passed, sql:" + stmt;
            throw new SQLNonTransientException(msg);
        }
        schema = schemaInfo.getSchemaConfig();
        rrs.setStatement(RouterUtil.removeSchema(rrs.getStatement(), schemaInfo.getSchema()));
        if (RouterUtil.isNoSharding(schema, schemaInfo.getTable())) {
            if (delete.getWhere() != null && !SchemaUtil.isNoSharding(sc, delete.getWhere(), schemaName, new StringPtr(schemaInfo.getSchema()))) {
                String msg = "DELETE query with sub-query is not supported, sql:" + stmt;
                throw new SQLNonTransientException(msg);
            }
            RouterUtil.routeToSingleNode(rrs, schema.getDataNode());
            return schema;
        }
        super.visitorParse(schema, rrs, stmt, visitor, sc);
        if (visitor.isHasSubQuery()) {
            String msg = "DELETE query with sub-query  is not supported, sql:" + stmt;
            throw new SQLNonTransientException(msg);
        }
        TableConfig tc = schema.getTables().get(schemaInfo.getTable());
        if (tc != null && tc.isGlobalTable()) {
            RouterUtil.routeToMultiNode(false, rrs, tc.getDataNodes(), tc.isGlobalTable());
            rrs.setFinishedRoute(true);
            return schema;
        }
    }
    return schema;
}
Also used : MySqlDeleteStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlDeleteStatement) SQLNonTransientException(java.sql.SQLNonTransientException) SchemaConfig(com.actiontech.dble.config.model.SchemaConfig) StringPtr(com.actiontech.dble.plan.common.ptr.StringPtr) TableConfig(com.actiontech.dble.config.model.TableConfig) SQLExprTableSource(com.alibaba.druid.sql.ast.statement.SQLExprTableSource) SQLJoinTableSource(com.alibaba.druid.sql.ast.statement.SQLJoinTableSource) SQLTableSource(com.alibaba.druid.sql.ast.statement.SQLTableSource) SchemaInfo(com.actiontech.dble.server.util.SchemaUtil.SchemaInfo)

Aggregations

MySqlDeleteStatement (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlDeleteStatement)6 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)2 SchemaConfig (com.actiontech.dble.config.model.SchemaConfig)1 TableConfig (com.actiontech.dble.config.model.TableConfig)1 StringPtr (com.actiontech.dble.plan.common.ptr.StringPtr)1 SchemaInfo (com.actiontech.dble.server.util.SchemaUtil.SchemaInfo)1 MysqlTest (com.alibaba.druid.sql.MysqlTest)1 SQLOrderBy (com.alibaba.druid.sql.ast.SQLOrderBy)1 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)1 SQLExprTableSource (com.alibaba.druid.sql.ast.statement.SQLExprTableSource)1 SQLJoinTableSource (com.alibaba.druid.sql.ast.statement.SQLJoinTableSource)1 SQLReplaceStatement (com.alibaba.druid.sql.ast.statement.SQLReplaceStatement)1 SQLTableSource (com.alibaba.druid.sql.ast.statement.SQLTableSource)1 MySqlExplainStatement (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlExplainStatement)1 MySqlInsertStatement (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement)1 MySqlUpdateStatement (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlUpdateStatement)1 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)1 ParserException (com.alibaba.druid.sql.parser.ParserException)1 WallConfig (com.alibaba.druid.wall.WallConfig)1 WallContext (com.alibaba.druid.wall.WallContext)1