Search in sources :

Example 6 with SQLUpdateStatement

use of com.alibaba.druid.sql.ast.statement.SQLUpdateStatement in project druid by alibaba.

the class SQLUpdateBuilderImpl method where.

@Override
public SQLUpdateBuilderImpl where(String expr) {
    SQLUpdateStatement update = getSQLUpdateStatement();
    SQLExpr exprObj = SQLUtils.toSQLExpr(expr, dbType);
    update.setWhere(exprObj);
    return this;
}
Also used : SQLUpdateStatement(com.alibaba.druid.sql.ast.statement.SQLUpdateStatement) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 7 with SQLUpdateStatement

use of com.alibaba.druid.sql.ast.statement.SQLUpdateStatement in project druid by alibaba.

the class Issue1865 method test_for_update_group.

public void test_for_update_group() throws Exception {
    final DbType dbType = JdbcConstants.MYSQL;
    String sql = "update t set val = ? where id = 2 and name = 'wenshao'";
    SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType, SQLParserFeature.EnableSQLBinaryOpExprGroup);
    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());
}
Also used : SQLStatementParser(com.alibaba.druid.sql.parser.SQLStatementParser) SQLUpdateStatement(com.alibaba.druid.sql.ast.statement.SQLUpdateStatement) DbType(com.alibaba.druid.DbType)

Example 8 with SQLUpdateStatement

use of com.alibaba.druid.sql.ast.statement.SQLUpdateStatement in project druid by alibaba.

the class Issue1865 method test_for_update.

public void test_for_update() throws Exception {
    final DbType dbType = JdbcConstants.MYSQL;
    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());
}
Also used : SQLStatementParser(com.alibaba.druid.sql.parser.SQLStatementParser) SQLUpdateStatement(com.alibaba.druid.sql.ast.statement.SQLUpdateStatement) DbType(com.alibaba.druid.DbType)

Example 9 with SQLUpdateStatement

use of com.alibaba.druid.sql.ast.statement.SQLUpdateStatement in project druid by alibaba.

the class MySqlUpdateTest_16 method test_0.

public void test_0() throws Exception {
    String sql = "update am_activity_prize set lock_left_count=lock_left_count-120.0, lock_count=lock_count+0.0 where id=?";
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement stmt = statementList.get(0);
    print(statementList);
    assertEquals(1, statementList.size());
    MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
    stmt.accept(visitor);
    // System.out.println("Tables : " + visitor.getTables());
    // System.out.println("fields : " + visitor.getColumns());
    // System.out.println("coditions : " + visitor.getConditions());
    // System.out.println("orderBy : " + visitor.getOrderByColumns());
    assertEquals(1, visitor.getTables().size());
    assertEquals(3, visitor.getColumns().size());
    // assertEquals(2, visitor.getConditions().size());
    assertTrue(visitor.containsTable("am_activity_prize"));
    assertTrue(visitor.getColumns().contains(new Column("am_activity_prize", "lock_left_count")));
    {
        String output = SQLUtils.toMySqlString(stmt);
        assertEquals("UPDATE am_activity_prize\n" + "SET lock_left_count = lock_left_count - 120.0, lock_count = lock_count + 0.0\n" + // 
        "WHERE id = ?", output);
    }
    {
        String output = SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION);
        assertEquals("update am_activity_prize\n" + "set lock_left_count = lock_left_count - 120.0, lock_count = lock_count + 0.0\n" + // 
        "where id = ?", output);
    }
    assertTrue(WallUtils.isValidateMySql(sql));
    {
        SQLUpdateStatement update = (SQLUpdateStatement) stmt;
        SQLExpr where = update.getWhere();
        assertEquals("id = ?", where.toString());
    }
}
Also used : MySqlSchemaStatVisitor(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor) Column(com.alibaba.druid.stat.TableStat.Column) SQLUpdateStatement(com.alibaba.druid.sql.ast.statement.SQLUpdateStatement) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 10 with SQLUpdateStatement

use of com.alibaba.druid.sql.ast.statement.SQLUpdateStatement in project druid by alibaba.

the class SQLUtils method addCondition.

public static void addCondition(SQLStatement stmt, SQLBinaryOperator op, SQLExpr condition, boolean left) {
    if (stmt instanceof SQLSelectStatement) {
        SQLSelectQuery query = ((SQLSelectStatement) stmt).getSelect().getQuery();
        if (query instanceof SQLSelectQueryBlock) {
            SQLSelectQueryBlock queryBlock = (SQLSelectQueryBlock) query;
            SQLExpr newCondition = buildCondition(op, condition, left, queryBlock.getWhere());
            queryBlock.setWhere(newCondition);
        } else {
            throw new IllegalArgumentException("add condition not support " + stmt.getClass().getName());
        }
        return;
    }
    if (stmt instanceof SQLDeleteStatement) {
        SQLDeleteStatement delete = (SQLDeleteStatement) stmt;
        SQLExpr newCondition = buildCondition(op, condition, left, delete.getWhere());
        delete.setWhere(newCondition);
        return;
    }
    if (stmt instanceof SQLUpdateStatement) {
        SQLUpdateStatement update = (SQLUpdateStatement) stmt;
        SQLExpr newCondition = buildCondition(op, condition, left, update.getWhere());
        update.setWhere(newCondition);
        return;
    }
    throw new IllegalArgumentException("add condition not support " + stmt.getClass().getName());
}
Also used : SQLDeleteStatement(com.alibaba.druid.sql.ast.statement.SQLDeleteStatement) SQLUpdateStatement(com.alibaba.druid.sql.ast.statement.SQLUpdateStatement) SQLSelectQuery(com.alibaba.druid.sql.ast.statement.SQLSelectQuery) SQLSelectStatement(com.alibaba.druid.sql.ast.statement.SQLSelectStatement) SQLSelectQueryBlock(com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Aggregations

SQLUpdateStatement (com.alibaba.druid.sql.ast.statement.SQLUpdateStatement)20 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)8 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)7 SQLDeleteStatement (com.alibaba.druid.sql.ast.statement.SQLDeleteStatement)7 SQLInsertStatement (com.alibaba.druid.sql.ast.statement.SQLInsertStatement)6 SQLExprTableSource (com.alibaba.druid.sql.ast.statement.SQLExprTableSource)4 SQLSelectStatement (com.alibaba.druid.sql.ast.statement.SQLSelectStatement)4 SQLStatementParser (com.alibaba.druid.sql.parser.SQLStatementParser)4 SQLTableSource (com.alibaba.druid.sql.ast.statement.SQLTableSource)3 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)3 MySqlSchemaStatVisitor (com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor)3 Column (com.alibaba.druid.stat.TableStat.Column)3 DbType (com.alibaba.druid.DbType)2 SQLAlterTableDropConstraint (com.alibaba.druid.sql.ast.statement.SQLAlterTableDropConstraint)2 SQLAlterTableDropIndex (com.alibaba.druid.sql.ast.statement.SQLAlterTableDropIndex)2 SQLAlterTableStatement (com.alibaba.druid.sql.ast.statement.SQLAlterTableStatement)2 SQLCreateDatabaseStatement (com.alibaba.druid.sql.ast.statement.SQLCreateDatabaseStatement)2 SQLCreateTableStatement (com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement)2 SQLDropTableStatement (com.alibaba.druid.sql.ast.statement.SQLDropTableStatement)2 SQLSelectQueryBlock (com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock)2