use of io.seata.sqlparser.druid.mysql.MySQLDeleteRecognizer in project seata by seata.
the class MySQLDeleteRecognizerTest method testGetWhereCondition_1.
@Test
public void testGetWhereCondition_1() {
String sql = "delete from t";
List<SQLStatement> asts = SQLUtils.parseStatements(sql, JdbcConstants.MYSQL);
MySQLDeleteRecognizer recognizer = new MySQLDeleteRecognizer(sql, asts.get(0));
String whereCondition = recognizer.getWhereCondition();
// test for no condition
Assertions.assertEquals("", whereCondition);
sql = "delete from t where id = 1";
asts = SQLUtils.parseStatements(sql, JdbcConstants.MYSQL);
recognizer = new MySQLDeleteRecognizer(sql, asts.get(0));
whereCondition = recognizer.getWhereCondition();
// test for normal sql
Assertions.assertEquals("id = 1", whereCondition);
sql = "delete from t where id in (1)";
asts = SQLUtils.parseStatements(sql, JdbcConstants.MYSQL);
recognizer = new MySQLDeleteRecognizer(sql, asts.get(0));
whereCondition = recognizer.getWhereCondition();
// test for sql with in
Assertions.assertEquals("id IN (1)", whereCondition);
sql = "delete from t where id between 1 and 2";
asts = SQLUtils.parseStatements(sql, JdbcConstants.MYSQL);
recognizer = new MySQLDeleteRecognizer(sql, asts.get(0));
whereCondition = recognizer.getWhereCondition();
// test for sql with in
Assertions.assertEquals("id BETWEEN 1 AND 2", whereCondition);
// test for exception
Assertions.assertThrows(IllegalArgumentException.class, () -> {
String s = "delete from t where id in (1)";
List<SQLStatement> sqlStatements = SQLUtils.parseStatements(s, JdbcConstants.MYSQL);
SQLDeleteStatement deleteAst = (SQLDeleteStatement) sqlStatements.get(0);
deleteAst.setWhere(new MySqlOrderingExpr());
new MySQLDeleteRecognizer(s, deleteAst).getWhereCondition();
});
}
use of io.seata.sqlparser.druid.mysql.MySQLDeleteRecognizer in project seata by seata.
the class MySQLDeleteRecognizerTest method deleteRecognizerTest_0.
/**
* Delete recognizer test 0.
*/
@Test
public void deleteRecognizerTest_0() {
ParametersHolder parametersHolder = new ParametersHolder() {
@Override
public Map<Integer, ArrayList<Object>> getParameters() {
return Collections.EMPTY_MAP;
}
};
String sql = "DELETE FROM t1 WHERE id = 'id1' order by id asc,name desc limit 1,2";
SQLStatement statement = getSQLStatement(sql);
ArrayList<List<Object>> paramAppenderList = new ArrayList<>();
MySQLDeleteRecognizer mySQLDeleteRecognizer = new MySQLDeleteRecognizer(sql, statement);
String orderBy = mySQLDeleteRecognizer.getOrderBy();
Assertions.assertTrue(orderBy.equalsIgnoreCase(" ORDER BY id asc,name desc"));
Assertions.assertEquals(sql, mySQLDeleteRecognizer.getOriginalSQL());
Assertions.assertEquals("t1", mySQLDeleteRecognizer.getTableName());
Assertions.assertEquals("id = 'id1'", mySQLDeleteRecognizer.getWhereCondition());
String limit = mySQLDeleteRecognizer.getLimit(parametersHolder, paramAppenderList);
Assertions.assertEquals(" LIMIT 1,2", limit);
sql = "DELETE FROM t1 WHERE id > 1 order by id ,name desc limit 1";
statement = getSQLStatement(sql);
mySQLDeleteRecognizer = new MySQLDeleteRecognizer(sql, statement);
orderBy = mySQLDeleteRecognizer.getOrderBy();
Assertions.assertTrue(orderBy.equalsIgnoreCase(" order by id,name desc"));
Assertions.assertEquals(" LIMIT 1", mySQLDeleteRecognizer.getLimit(parametersHolder, paramAppenderList));
sql = "DELETE FROM t1 WHERE id > 1";
statement = getSQLStatement(sql);
mySQLDeleteRecognizer = new MySQLDeleteRecognizer(sql, statement);
Assertions.assertEquals(null, mySQLDeleteRecognizer.getLimit(parametersHolder, paramAppenderList));
orderBy = mySQLDeleteRecognizer.getOrderBy();
Assertions.assertEquals(null, mySQLDeleteRecognizer.getOrderBy());
}
use of io.seata.sqlparser.druid.mysql.MySQLDeleteRecognizer in project seata by seata.
the class MySQLDeleteRecognizerTest method testGetSqlType.
@Test
public void testGetSqlType() {
String sql = "delete from t where id = ?";
List<SQLStatement> asts = SQLUtils.parseStatements(sql, JdbcConstants.MYSQL);
MySQLDeleteRecognizer recognizer = new MySQLDeleteRecognizer(sql, asts.get(0));
Assertions.assertEquals(recognizer.getSQLType(), SQLType.DELETE);
}
use of io.seata.sqlparser.druid.mysql.MySQLDeleteRecognizer in project seata by seata.
the class DeleteExecutorTest method init.
@BeforeAll
public static void init() {
List<String> returnValueColumnLabels = Lists.newArrayList("id", "name");
Object[][] returnValue = new Object[][] { new Object[] { 1, "Tom" }, new Object[] { 2, "Jack" } };
Object[][] columnMetas = new Object[][] { new Object[] { "", "", "table_delete_executor_test", "id", Types.INTEGER, "INTEGER", 64, 0, 10, 1, "", "", 0, 0, 64, 1, "NO", "YES" }, new Object[] { "", "", "table_delete_executor_test", "name", Types.VARCHAR, "VARCHAR", 64, 0, 10, 0, "", "", 0, 0, 64, 2, "YES", "NO" } };
Object[][] indexMetas = new Object[][] { new Object[] { "PRIMARY", "id", false, "", 3, 1, "A", 34 } };
MockDriver mockDriver = new MockDriver(returnValueColumnLabels, returnValue, columnMetas, indexMetas);
DruidDataSource dataSource = new DruidDataSource();
dataSource.setUrl("jdbc:mock:xxx");
dataSource.setDriver(mockDriver);
DataSourceProxy dataSourceProxy = new DataSourceProxy(dataSource);
try {
Field field = dataSourceProxy.getClass().getDeclaredField("dbType");
field.setAccessible(true);
field.set(dataSourceProxy, "mysql");
ConnectionProxy connectionProxy = new ConnectionProxy(dataSourceProxy, dataSource.getConnection().getConnection());
MockStatementBase mockStatement = new MockStatement(dataSource.getConnection().getConnection());
statementProxy = new StatementProxy(connectionProxy, mockStatement);
} catch (Exception e) {
throw new RuntimeException("init failed");
}
String sql = "delete from t where id = 1";
List<SQLStatement> asts = SQLUtils.parseStatements(sql, JdbcConstants.MYSQL);
MySQLDeleteRecognizer recognizer = new MySQLDeleteRecognizer(sql, asts.get(0));
deleteExecutor = new DeleteExecutor(statementProxy, (statement, args) -> {
return null;
}, recognizer);
}
use of io.seata.sqlparser.druid.mysql.MySQLDeleteRecognizer in project seata by seata.
the class DeleteExecutorTest method testBeforeImage.
@Test
public void testBeforeImage() throws SQLException {
Assertions.assertNotNull(deleteExecutor.beforeImage());
String sql = "delete from t";
List<SQLStatement> asts = SQLUtils.parseStatements(sql, JdbcConstants.MYSQL);
MySQLDeleteRecognizer recognizer = new MySQLDeleteRecognizer(sql, asts.get(0));
deleteExecutor = new DeleteExecutor(statementProxy, (statement, args) -> null, recognizer);
Assertions.assertNotNull(deleteExecutor.beforeImage());
}
Aggregations