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;
}
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());
}
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());
}
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());
}
}
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());
}
Aggregations