Search in sources :

Example 16 with ParametersHolder

use of io.seata.sqlparser.ParametersHolder in project seata by seata.

the class MySQLUpdateRecognizerTest method updateRecognizerTest_5.

/**
 * Update recognizer test 5.
 */
@Test
public void updateRecognizerTest_5() {
    String sql = "UPDATE t1 SET name1 = 'name1', name2 = 'name2' WHERE id between ? and ?";
    SQLStatement statement = getSQLStatement(sql);
    MySQLUpdateRecognizer mySQLUpdateRecognizer = new MySQLUpdateRecognizer(sql, statement);
    Assertions.assertEquals(sql, mySQLUpdateRecognizer.getOriginalSQL());
    Assertions.assertEquals("t1", mySQLUpdateRecognizer.getTableName());
    Assertions.assertEquals(2, mySQLUpdateRecognizer.getUpdateColumns().size());
    Assertions.assertEquals("name1", mySQLUpdateRecognizer.getUpdateColumns().get(0));
    Assertions.assertEquals("name1", mySQLUpdateRecognizer.getUpdateValues().get(0));
    Assertions.assertEquals("name2", mySQLUpdateRecognizer.getUpdateColumns().get(1));
    Assertions.assertEquals("name2", mySQLUpdateRecognizer.getUpdateValues().get(1));
    ArrayList<List<Object>> paramAppenderList = new ArrayList<>();
    String whereCondition = mySQLUpdateRecognizer.getWhereCondition(new ParametersHolder() {

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

Example 17 with ParametersHolder

use of io.seata.sqlparser.ParametersHolder in project seata by seata.

the class MySQLUpdateRecognizerTest method updateRecognizerTest_2.

/**
 * Update recognizer test 2.
 */
@Test
public void updateRecognizerTest_2() {
    String sql = "UPDATE t1 SET name1 = 'name1', name2 = 'name2' WHERE id = ?";
    SQLStatement statement = getSQLStatement(sql);
    MySQLUpdateRecognizer mySQLUpdateRecognizer = new MySQLUpdateRecognizer(sql, statement);
    Assertions.assertEquals(sql, mySQLUpdateRecognizer.getOriginalSQL());
    Assertions.assertEquals("t1", mySQLUpdateRecognizer.getTableName());
    Assertions.assertEquals(2, mySQLUpdateRecognizer.getUpdateColumns().size());
    Assertions.assertEquals("name1", mySQLUpdateRecognizer.getUpdateColumns().get(0));
    Assertions.assertEquals("name1", mySQLUpdateRecognizer.getUpdateValues().get(0));
    Assertions.assertEquals("name2", mySQLUpdateRecognizer.getUpdateColumns().get(1));
    Assertions.assertEquals("name2", mySQLUpdateRecognizer.getUpdateValues().get(1));
    ArrayList<List<Object>> paramAppenderList = new ArrayList<>();
    String whereCondition = mySQLUpdateRecognizer.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 : ParametersHolder(io.seata.sqlparser.ParametersHolder) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) MySQLUpdateRecognizer(io.seata.sqlparser.druid.mysql.MySQLUpdateRecognizer) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 18 with ParametersHolder

use of io.seata.sqlparser.ParametersHolder 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 19 with ParametersHolder

use of io.seata.sqlparser.ParametersHolder 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 20 with ParametersHolder

use of io.seata.sqlparser.ParametersHolder 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)

Aggregations

ParametersHolder (io.seata.sqlparser.ParametersHolder)23 Test (org.junit.jupiter.api.Test)19 Map (java.util.Map)18 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)16 ArrayList (java.util.ArrayList)16 List (java.util.List)14 HashMap (java.util.HashMap)13 MySQLDeleteRecognizer (io.seata.sqlparser.druid.mysql.MySQLDeleteRecognizer)5 MySQLSelectForUpdateRecognizer (io.seata.sqlparser.druid.mysql.MySQLSelectForUpdateRecognizer)4 MySQLUpdateRecognizer (io.seata.sqlparser.druid.mysql.MySQLUpdateRecognizer)4 StringJoiner (java.util.StringJoiner)4 SQLRecognizer (io.seata.sqlparser.SQLRecognizer)3 SQLUpdateRecognizer (io.seata.sqlparser.SQLUpdateRecognizer)3 SQLDeleteStatement (com.alibaba.druid.sql.ast.statement.SQLDeleteStatement)2 NotSupportYetException (io.seata.common.exception.NotSupportYetException)2 TableMeta (io.seata.rm.datasource.sql.struct.TableMeta)2 SQLDeleteRecognizer (io.seata.sqlparser.SQLDeleteRecognizer)2 MySqlOrderingExpr (com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlOrderingExpr)1 OracleArgumentExpr (com.alibaba.druid.sql.dialect.oracle.ast.expr.OracleArgumentExpr)1 SQLSelectRecognizer (io.seata.sqlparser.SQLSelectRecognizer)1