Search in sources :

Example 6 with MySQLDeleteRecognizer

use of io.seata.sqlparser.druid.mysql.MySQLDeleteRecognizer in project seata by seata.

the class MySQLDeleteRecognizerTest method testGetTableAlias.

@Test
public void testGetTableAlias() {
    String sql = "delete from t where id = ?";
    List<SQLStatement> asts = SQLUtils.parseStatements(sql, JdbcConstants.MYSQL);
    MySQLDeleteRecognizer recognizer = new MySQLDeleteRecognizer(sql, asts.get(0));
    Assertions.assertNull(recognizer.getTableAlias());
}
Also used : MySQLDeleteRecognizer(io.seata.sqlparser.druid.mysql.MySQLDeleteRecognizer) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) Test(org.junit.jupiter.api.Test)

Example 7 with MySQLDeleteRecognizer

use of io.seata.sqlparser.druid.mysql.MySQLDeleteRecognizer in project seata by seata.

the class MySQLDeleteRecognizerTest method deleteRecognizerTest_3.

/**
 * Delete recognizer test 3.
 */
@Test
public void deleteRecognizerTest_3() {
    String sql = "DELETE FROM t1 WHERE id between ? AND ?";
    SQLStatement statement = getSQLStatement(sql);
    MySQLDeleteRecognizer mySQLDeleteRecognizer = new MySQLDeleteRecognizer(sql, statement);
    Assertions.assertEquals(sql, mySQLDeleteRecognizer.getOriginalSQL());
    Assertions.assertEquals("t1", mySQLDeleteRecognizer.getTableName());
    // test overflow parameters
    ArrayList<List<Object>> paramAppenderList = new ArrayList<>();
    String whereCondition = mySQLDeleteRecognizer.getWhereCondition(new ParametersHolder() {

        @Override
        public Map<Integer, ArrayList<Object>> getParameters() {
            ArrayList<Object> idParam = new ArrayList<>();
            idParam.add("id1");
            ArrayList<Object> id2Param = new ArrayList<>();
            id2Param.add("id2");
            Map result = new HashMap();
            result.put(1, idParam);
            result.put(2, id2Param);
            return result;
        }
    }, paramAppenderList);
    Assertions.assertEquals(Collections.singletonList(Arrays.asList("id1", "id2")), paramAppenderList);
    Assertions.assertEquals("id BETWEEN ? AND ?", whereCondition);
}
Also used : MySQLDeleteRecognizer(io.seata.sqlparser.druid.mysql.MySQLDeleteRecognizer) ParametersHolder(io.seata.sqlparser.ParametersHolder) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 8 with MySQLDeleteRecognizer

use of io.seata.sqlparser.druid.mysql.MySQLDeleteRecognizer in project seata by seata.

the class MySQLDeleteRecognizerTest method deleteRecognizerTest_1.

/**
 * Delete recognizer test 1.
 */
@Test
public void deleteRecognizerTest_1() {
    String sql = "DELETE FROM t1 WHERE id = ?";
    SQLStatement statement = getSQLStatement(sql);
    MySQLDeleteRecognizer mySQLDeleteRecognizer = new MySQLDeleteRecognizer(sql, statement);
    Assertions.assertEquals(sql, mySQLDeleteRecognizer.getOriginalSQL());
    Assertions.assertEquals("t1", mySQLDeleteRecognizer.getTableName());
    // test overflow parameters
    ArrayList<List<Object>> paramAppenderList = new ArrayList<>();
    String whereCondition = mySQLDeleteRecognizer.getWhereCondition(new ParametersHolder() {

        @Override
        public Map<Integer, ArrayList<Object>> getParameters() {
            ArrayList<Object> idParam = new ArrayList<>();
            idParam.add("id1");
            Map result = new HashMap<>();
            result.put(1, idParam);
            return result;
        }
    }, paramAppenderList);
    Assertions.assertEquals(Collections.singletonList(Collections.singletonList("id1")), paramAppenderList);
    Assertions.assertEquals("id = ?", whereCondition);
}
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) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 9 with MySQLDeleteRecognizer

use of io.seata.sqlparser.druid.mysql.MySQLDeleteRecognizer in project seata by seata.

the class MySQLDeleteRecognizerTest method deleteRecognizerTest_2.

/**
 * Delete recognizer test 2.
 */
@Test
public void deleteRecognizerTest_2() {
    String sql = "DELETE FROM t1 WHERE id IN (?, ?)";
    SQLStatement statement = getSQLStatement(sql);
    MySQLDeleteRecognizer mySQLDeleteRecognizer = new MySQLDeleteRecognizer(sql, statement);
    Assertions.assertEquals(sql, mySQLDeleteRecognizer.getOriginalSQL());
    Assertions.assertEquals("t1", mySQLDeleteRecognizer.getTableName());
    // test overflow parameters
    ArrayList<List<Object>> paramAppenderList = new ArrayList<>();
    String whereCondition = mySQLDeleteRecognizer.getWhereCondition(new ParametersHolder() {

        @Override
        public Map<Integer, ArrayList<Object>> getParameters() {
            ArrayList<Object> idParam = new ArrayList<>();
            idParam.add("id1");
            ArrayList<Object> id2Param = new ArrayList<>();
            id2Param.add("id2");
            Map result = new HashMap();
            result.put(1, idParam);
            result.put(2, id2Param);
            return result;
        }
    }, paramAppenderList);
    Assertions.assertEquals(Arrays.asList(Arrays.asList("id1", "id2")), paramAppenderList);
    Assertions.assertEquals("id IN (?, ?)", whereCondition);
}
Also used : MySQLDeleteRecognizer(io.seata.sqlparser.druid.mysql.MySQLDeleteRecognizer) ParametersHolder(io.seata.sqlparser.ParametersHolder) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 10 with MySQLDeleteRecognizer

use of io.seata.sqlparser.druid.mysql.MySQLDeleteRecognizer in project seata by seata.

the class MySQLDeleteRecognizerTest method testGetWhereCondition_0.

@Test
public void testGetWhereCondition_0() {
    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(new ParametersHolder() {

        @Override
        public Map<Integer, ArrayList<Object>> getParameters() {
            return null;
        }
    }, new ArrayList<>());
    // test for no condition
    Assertions.assertEquals("", whereCondition);
    sql = "delete from t where id = ?";
    asts = SQLUtils.parseStatements(sql, JdbcConstants.MYSQL);
    recognizer = new MySQLDeleteRecognizer(sql, asts.get(0));
    whereCondition = recognizer.getWhereCondition(new ParametersHolder() {

        @Override
        public Map<Integer, ArrayList<Object>> getParameters() {
            ArrayList<Object> idParam = new ArrayList<>();
            idParam.add(1);
            Map result = new HashMap();
            result.put(1, idParam);
            return result;
        }
    }, new ArrayList<>());
    // test for normal sql
    Assertions.assertEquals("id = ?", whereCondition);
    sql = "delete from t where id in (?)";
    asts = SQLUtils.parseStatements(sql, JdbcConstants.MYSQL);
    recognizer = new MySQLDeleteRecognizer(sql, asts.get(0));
    whereCondition = recognizer.getWhereCondition(new ParametersHolder() {

        @Override
        public Map<Integer, ArrayList<Object>> getParameters() {
            ArrayList<Object> idParam = new ArrayList<>();
            idParam.add(1);
            Map result = new HashMap();
            result.put(1, idParam);
            return result;
        }
    }, new ArrayList<>());
    // test for sql with in
    Assertions.assertEquals("id IN (?)", whereCondition);
    sql = "delete from t where id between ? and ?";
    asts = SQLUtils.parseStatements(sql, JdbcConstants.MYSQL);
    recognizer = new MySQLDeleteRecognizer(sql, asts.get(0));
    whereCondition = recognizer.getWhereCondition(new ParametersHolder() {

        @Override
        public Map<Integer, ArrayList<Object>> getParameters() {
            ArrayList<Object> idParam = new ArrayList<>();
            idParam.add(1);
            ArrayList<Object> idParam2 = new ArrayList<>();
            idParam.add(2);
            Map result = new HashMap();
            result.put(1, idParam);
            result.put(2, idParam2);
            return result;
        }
    }, new ArrayList<>());
    // test for sql with in
    Assertions.assertEquals("id BETWEEN ? AND ?", whereCondition);
    // test for exception
    Assertions.assertThrows(IllegalArgumentException.class, () -> {
        String s = "delete from t where id in (?)";
        List<SQLStatement> sqlStatements = SQLUtils.parseStatements(s, JdbcConstants.MYSQL);
        SQLDeleteStatement deleteAst = (SQLDeleteStatement) sqlStatements.get(0);
        deleteAst.setWhere(new MySqlOrderingExpr());
        new MySQLDeleteRecognizer(s, deleteAst).getWhereCondition(new ParametersHolder() {

            @Override
            public Map<Integer, ArrayList<Object>> getParameters() {
                return new HashMap();
            }
        }, new ArrayList<>());
    });
}
Also used : MySQLDeleteRecognizer(io.seata.sqlparser.druid.mysql.MySQLDeleteRecognizer) MySqlOrderingExpr(com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlOrderingExpr) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) ParametersHolder(io.seata.sqlparser.ParametersHolder) SQLDeleteStatement(com.alibaba.druid.sql.ast.statement.SQLDeleteStatement) HashMap(java.util.HashMap) Map(java.util.Map) 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