use of io.seata.sqlparser.druid.postgresql.PostgresqlInsertRecognizer in project seata by seata.
the class PostgresqlInsertRecognizerTest method testGetInsertColumns.
@Test
public void testGetInsertColumns() {
// test for no column
String sql = "insert into t values (?)";
List<SQLRecognizer> sqlRecognizers = SQLVisitorFactory.get(sql, DB_TYPE);
SQLInsertRecognizer recognizer = (SQLInsertRecognizer) sqlRecognizers.get(0);
List<String> insertColumns = recognizer.getInsertColumns();
Assertions.assertNull(insertColumns);
// test for normal
sql = "insert into t(a) values (?)";
recognizer = (SQLInsertRecognizer) SQLVisitorFactory.get(sql, DB_TYPE).get(0);
insertColumns = recognizer.getInsertColumns();
Assertions.assertEquals(1, insertColumns.size());
// test for exception
Assertions.assertThrows(SQLParsingException.class, () -> {
String s = "insert into t(a) values (?)";
List<SQLStatement> sqlStatements = SQLUtils.parseStatements(s, DB_TYPE);
SQLInsertStatement sqlInsertStatement = (SQLInsertStatement) sqlStatements.get(0);
sqlInsertStatement.getColumns().add(new SQLBetweenExpr());
PostgresqlInsertRecognizer postgresqlInsertRecognizer = new PostgresqlInsertRecognizer(s, sqlInsertStatement);
postgresqlInsertRecognizer.getInsertColumns();
});
}
use of io.seata.sqlparser.druid.postgresql.PostgresqlInsertRecognizer in project seata by seata.
the class PostgresqlInsertRecognizerTest method testGetInsertRows.
@Test
public void testGetInsertRows() {
final int pkIndex = 0;
// test for null value
String sql = "insert into t(id, no, name, age, time) values (nextval('id_seq'), null, 'a', ?, now())";
SQLInsertRecognizer recognizer = (SQLInsertRecognizer) SQLVisitorFactory.get(sql, DB_TYPE).get(0);
List<List<Object>> insertRows = recognizer.getInsertRows(Collections.singletonList(pkIndex));
Assertions.assertTrue(insertRows.size() == 1);
// test for exception
Assertions.assertThrows(SQLParsingException.class, () -> {
String s = "insert into t(a) values (?)";
List<SQLStatement> sqlStatements = SQLUtils.parseStatements(s, DB_TYPE);
SQLInsertStatement sqlInsertStatement = (SQLInsertStatement) sqlStatements.get(0);
sqlInsertStatement.getValuesList().get(0).getValues().set(pkIndex, new SQLBetweenExpr());
PostgresqlInsertRecognizer postgresqlInsertRecognizer = new PostgresqlInsertRecognizer(s, sqlInsertStatement);
postgresqlInsertRecognizer.getInsertRows(Collections.singletonList(pkIndex));
});
}
Aggregations