use of io.seata.sqlparser.ParametersHolder 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<>());
});
}
use of io.seata.sqlparser.ParametersHolder in project seata by seata.
the class MySQLSelectForUpdateRecognizerTest method selectForUpdateRecognizerTest_3.
/**
* Select for update recognizer test 3.
*/
@Test
public void selectForUpdateRecognizerTest_3() {
String sql = "SELECT name1, name2 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());
// 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");
Map result = new HashMap();
result.put(1, id1Param);
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_4.
/**
* Select for update recognizer test 4.
*/
@Test
public void selectForUpdateRecognizerTest_4() {
String sql = "SELECT name1, name2 FROM t1 WHERE id IN (?,?) 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 IN (?, ?)", whereCondition);
}
Aggregations