Search in sources :

Example 1 with ParametersHolder

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

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

the class MySQLSelectForUpdateRecognizerTest method selectForUpdateRecognizerTest_1.

/**
 * Select for update recognizer test 1.
 */
@Test
public void selectForUpdateRecognizerTest_1() {
    String sql = "SELECT name FROM t1 WHERE id = ? FOR UPDATE";
    SQLStatement statement = getSQLStatement(sql);
    MySQLSelectForUpdateRecognizer mySQLUpdateRecognizer = new MySQLSelectForUpdateRecognizer(sql, statement);
    Assertions.assertEquals(sql, mySQLUpdateRecognizer.getOriginalSQL());
    Assertions.assertEquals("t1", mySQLUpdateRecognizer.getTableName());
    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) MySQLSelectForUpdateRecognizer(io.seata.sqlparser.druid.mysql.MySQLSelectForUpdateRecognizer) 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 3 with ParametersHolder

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

the class MySQLSelectForUpdateRecognizerTest method selectForUpdateRecognizerTest_5.

/**
 * Select for update recognizer test 5.
 */
@Test
public void selectForUpdateRecognizerTest_5() {
    String sql = "SELECT name1, name2 FROM t1 WHERE id between ? and ? FOR UPDATE";
    SQLStatement statement = getSQLStatement(sql);
    MySQLSelectForUpdateRecognizer mySQLUpdateRecognizer = new MySQLSelectForUpdateRecognizer(sql, statement);
    Assertions.assertEquals(sql, mySQLUpdateRecognizer.getOriginalSQL());
    Assertions.assertEquals("t1", mySQLUpdateRecognizer.getTableName());
    // test overflow parameters
    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) MySQLSelectForUpdateRecognizer(io.seata.sqlparser.druid.mysql.MySQLSelectForUpdateRecognizer) 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 4 with ParametersHolder

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

the class MySQLUpdateRecognizerTest method updateRecognizerTest_4.

/**
 * Update recognizer test 4.
 */
@Test
public void updateRecognizerTest_4() {
    String sql = "UPDATE t1 SET name1 = 'name1', name2 = 'name2' WHERE id in (?, ?) and name1 = ?";
    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");
            ArrayList<Object> name1Param = new ArrayList<>();
            name1Param.add("name");
            Map result = new HashMap();
            result.put(1, id1Param);
            result.put(2, id2Param);
            result.put(3, name1Param);
            return result;
        }
    }, paramAppenderList);
    Assertions.assertEquals(Collections.singletonList(Arrays.asList("id1", "id2", "name")), paramAppenderList);
    Assertions.assertEquals("id IN (?, ?)\nAND name1 = ?", 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 5 with ParametersHolder

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

the class MySQLUpdateRecognizerTest method updateRecognizerTest_3.

/**
 * Update recognizer test 3.
 */
@Test
public void updateRecognizerTest_3() {
    String sql = "UPDATE t1 SET name1 = 'name1', name2 = 'name2' WHERE id in (?, ?)";
    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 IN (?, ?)", 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)

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