Skip to content

SQL_RemoveCondition_demo

高铁 edited this page Oct 3, 2017 · 3 revisions

1. Select Statemennt

final String dbType = JdbcConstants.MYSQL; // ORACLE or POSTGRESQL or SQL_SERVER or HIVE or ODPS

String sql = "select * from t where id = 2 and name = 'wenshao'";
SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType);
SQLSelectStatement stmt = (SQLSelectStatement) parser.parseStatement();
SQLSelectQueryBlock queryBlock = stmt.getSelect().getQueryBlock();

assertTrue(queryBlock.removeCondition("name = 'wenshao'"));

assertEquals("SELECT *\n" +
        "FROM t\n" +
        "WHERE id = 2", stmt.toString());

assertTrue(
        queryBlock.removeCondition("id = 2"));

assertEquals("SELECT *\n" +
        "FROM t", stmt.toString());

2. DELETE Statement

final String dbType = JdbcConstants.MYSQL; // ORACLE or POSTGRESQL or SQL_SERVER or HIVE or ODPS

String sql = "delete from t where id = 2 and name = 'wenshao'";
SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType);
SQLDeleteStatement stmt = (SQLDeleteStatement) parser.parseStatement();

assertTrue(stmt.removeCondition("name = 'wenshao'"));

assertEquals("DELETE FROM t\n" +
        "WHERE id = 2", stmt.toString());

assertTrue(
        stmt.removeCondition("id = 2"));

assertEquals("DELETE FROM t", stmt.toString());

stmt.addCondition("id = 3");
assertEquals("DELETE FROM t\n" +
        "WHERE id = 3", stmt.toString());

3. UPDATE Statemennt

final String dbType = JdbcConstants.MYSQL; // ORACLE or POSTGRESQL or SQL_SERVER or HIVE or ODPS

String sql = "update t set val = ? where id = 2 and name = 'wenshao'";
SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType);
SQLUpdateStatement stmt = (SQLUpdateStatement) parser.parseStatement();

assertTrue(stmt.removeCondition("name = 'wenshao'"));

assertEquals("UPDATE t\n" +
        "SET val = ?\n" +
        "WHERE id = 2", stmt.toString());

assertTrue(
        stmt.removeCondition("id = 2"));

assertEquals("UPDATE t\n" +
        "SET val = ?", stmt.toString());

stmt.addCondition("id = 3");
assertEquals("UPDATE t\n" +
        "SET val = ?\n" +
        "WHERE id = 3", stmt.toString());
Clone this wiki locally