Search in sources :

Example 1 with MySQLDeleteRecognizer

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();
    });
}
Also used : MySQLDeleteRecognizer(io.seata.sqlparser.druid.mysql.MySQLDeleteRecognizer) MySqlOrderingExpr(com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlOrderingExpr) SQLDeleteStatement(com.alibaba.druid.sql.ast.statement.SQLDeleteStatement) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) Test(org.junit.jupiter.api.Test)

Example 2 with MySQLDeleteRecognizer

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());
}
Also used : MySQLDeleteRecognizer(io.seata.sqlparser.druid.mysql.MySQLDeleteRecognizer) ParametersHolder(io.seata.sqlparser.ParametersHolder) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) Test(org.junit.jupiter.api.Test)

Example 3 with MySQLDeleteRecognizer

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);
}
Also used : MySQLDeleteRecognizer(io.seata.sqlparser.druid.mysql.MySQLDeleteRecognizer) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) Test(org.junit.jupiter.api.Test)

Example 4 with MySQLDeleteRecognizer

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);
}
Also used : MySQLDeleteRecognizer(io.seata.sqlparser.druid.mysql.MySQLDeleteRecognizer) MockDriver(io.seata.rm.datasource.mock.MockDriver) SQLUtils(com.alibaba.druid.sql.SQLUtils) StatementProxy(io.seata.rm.datasource.StatementProxy) MySQLDeleteRecognizer(io.seata.sqlparser.druid.mysql.MySQLDeleteRecognizer) Field(java.lang.reflect.Field) TableRecords(io.seata.rm.datasource.sql.struct.TableRecords) Test(org.junit.jupiter.api.Test) MockStatementBase(com.alibaba.druid.mock.MockStatementBase) SQLException(java.sql.SQLException) List(java.util.List) Lists(com.google.common.collect.Lists) ConnectionProxy(io.seata.rm.datasource.ConnectionProxy) BeforeAll(org.junit.jupiter.api.BeforeAll) MockStatement(com.alibaba.druid.mock.MockStatement) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) JdbcConstants(com.alibaba.druid.util.JdbcConstants) Assertions(org.junit.jupiter.api.Assertions) DataSourceProxy(io.seata.rm.datasource.DataSourceProxy) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) Types(java.sql.Types) MockDriver(io.seata.rm.datasource.mock.MockDriver) DataSourceProxy(io.seata.rm.datasource.DataSourceProxy) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) ConnectionProxy(io.seata.rm.datasource.ConnectionProxy) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SQLException(java.sql.SQLException) MockStatementBase(com.alibaba.druid.mock.MockStatementBase) Field(java.lang.reflect.Field) StatementProxy(io.seata.rm.datasource.StatementProxy) MockStatement(com.alibaba.druid.mock.MockStatement) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 5 with MySQLDeleteRecognizer

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());
}
Also used : MySQLDeleteRecognizer(io.seata.sqlparser.druid.mysql.MySQLDeleteRecognizer) MockDriver(io.seata.rm.datasource.mock.MockDriver) SQLUtils(com.alibaba.druid.sql.SQLUtils) StatementProxy(io.seata.rm.datasource.StatementProxy) MySQLDeleteRecognizer(io.seata.sqlparser.druid.mysql.MySQLDeleteRecognizer) Field(java.lang.reflect.Field) TableRecords(io.seata.rm.datasource.sql.struct.TableRecords) Test(org.junit.jupiter.api.Test) MockStatementBase(com.alibaba.druid.mock.MockStatementBase) SQLException(java.sql.SQLException) List(java.util.List) Lists(com.google.common.collect.Lists) ConnectionProxy(io.seata.rm.datasource.ConnectionProxy) BeforeAll(org.junit.jupiter.api.BeforeAll) MockStatement(com.alibaba.druid.mock.MockStatement) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) JdbcConstants(com.alibaba.druid.util.JdbcConstants) Assertions(org.junit.jupiter.api.Assertions) DataSourceProxy(io.seata.rm.datasource.DataSourceProxy) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) Types(java.sql.Types) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) Test(org.junit.jupiter.api.Test)

Aggregations

SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)10 MySQLDeleteRecognizer (io.seata.sqlparser.druid.mysql.MySQLDeleteRecognizer)10 Test (org.junit.jupiter.api.Test)10 List (java.util.List)6 ParametersHolder (io.seata.sqlparser.ParametersHolder)5 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)4 Map (java.util.Map)4 MockStatement (com.alibaba.druid.mock.MockStatement)2 MockStatementBase (com.alibaba.druid.mock.MockStatementBase)2 DruidDataSource (com.alibaba.druid.pool.DruidDataSource)2 SQLUtils (com.alibaba.druid.sql.SQLUtils)2 SQLDeleteStatement (com.alibaba.druid.sql.ast.statement.SQLDeleteStatement)2 MySqlOrderingExpr (com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlOrderingExpr)2 JdbcConstants (com.alibaba.druid.util.JdbcConstants)2 Lists (com.google.common.collect.Lists)2 ConnectionProxy (io.seata.rm.datasource.ConnectionProxy)2 DataSourceProxy (io.seata.rm.datasource.DataSourceProxy)2 StatementProxy (io.seata.rm.datasource.StatementProxy)2 MockDriver (io.seata.rm.datasource.mock.MockDriver)2