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());
}
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);
}
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);
}
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);
}
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);
}
Aggregations