use of io.seata.sqlparser.SQLRecognizer in project seata by seata.
the class SQLVisitorFactoryTest method testSqlRecognizing.
/**
* Test sql recognizing.
*/
@Test
public void testSqlRecognizing() {
// test for ast was null
Assertions.assertThrows(UnsupportedOperationException.class, () -> SQLVisitorFactory.get("", JdbcConstants.MYSQL));
// test for mysql insert
String sql = "insert into t(id) values (1)";
List<SQLRecognizer> recognizer = SQLVisitorFactory.get(sql, JdbcConstants.MYSQL);
Assertions.assertEquals(recognizer.get(0).getClass().getName(), MySQLInsertRecognizer.class.getName());
// test for mysql delete
sql = "delete from t";
recognizer = SQLVisitorFactory.get(sql, JdbcConstants.MYSQL);
Assertions.assertEquals(recognizer.get(0).getClass().getName(), MySQLDeleteRecognizer.class.getName());
// test for mysql update
sql = "update t set a = a";
recognizer = SQLVisitorFactory.get(sql, JdbcConstants.MYSQL);
Assertions.assertEquals(recognizer.get(0).getClass().getName(), MySQLUpdateRecognizer.class.getName());
// test for mysql select
sql = "select * from t";
recognizer = SQLVisitorFactory.get(sql, JdbcConstants.MYSQL);
Assertions.assertNull(recognizer);
// test for mysql select for update
sql = "select * from t for update";
recognizer = SQLVisitorFactory.get(sql, JdbcConstants.MYSQL);
Assertions.assertEquals(recognizer.get(0).getClass().getName(), MySQLSelectForUpdateRecognizer.class.getName());
// test for oracle insert
sql = "insert into t(id) values (1)";
recognizer = SQLVisitorFactory.get(sql, JdbcConstants.ORACLE);
Assertions.assertEquals(recognizer.get(0).getClass().getName(), OracleInsertRecognizer.class.getName());
// test for oracle delete
sql = "delete from t";
recognizer = SQLVisitorFactory.get(sql, JdbcConstants.ORACLE);
Assertions.assertEquals(recognizer.get(0).getClass().getName(), OracleDeleteRecognizer.class.getName());
// test for oracle update
sql = "update t set a = a";
recognizer = SQLVisitorFactory.get(sql, JdbcConstants.ORACLE);
Assertions.assertEquals(recognizer.get(0).getClass().getName(), OracleUpdateRecognizer.class.getName());
// test for oracle select
sql = "select * from t";
recognizer = SQLVisitorFactory.get(sql, JdbcConstants.ORACLE);
Assertions.assertNull(recognizer);
// test for oracle select for update
sql = "select * from t for update";
recognizer = SQLVisitorFactory.get(sql, JdbcConstants.ORACLE);
Assertions.assertEquals(recognizer.get(0).getClass().getName(), OracleSelectForUpdateRecognizer.class.getName());
// test for do not support db
Assertions.assertThrows(EnhancedServiceNotFoundException.class, () -> {
SQLVisitorFactory.get("select * from t", JdbcConstants.DB2);
});
// TEST FOR Multi-SQL
List<SQLRecognizer> sqlRecognizers = null;
// test for mysql insert
Assertions.assertThrows(UnsupportedOperationException.class, () -> {
SQLVisitorFactory.get("insert into t(id) values (1);insert into t(id) values (2)", JdbcConstants.MYSQL);
});
// test for mysql insert and update
Assertions.assertThrows(UnsupportedOperationException.class, () -> {
SQLVisitorFactory.get("insert into t(id) values (1);update t set a = t;", JdbcConstants.MYSQL);
});
// test for mysql insert and deleted
Assertions.assertThrows(UnsupportedOperationException.class, () -> {
SQLVisitorFactory.get("insert into t(id) values (1);delete from t where id = 1", JdbcConstants.MYSQL);
});
// test for mysql delete
sql = "delete from t where id =1 ; delete from t where id = 2";
sqlRecognizers = SQLVisitorFactory.get(sql, JdbcConstants.MYSQL);
for (SQLRecognizer sqlRecognizer : sqlRecognizers) {
Assertions.assertEquals(sqlRecognizer.getClass().getName(), MySQLDeleteRecognizer.class.getName());
}
// test for mysql update
sql = "update t set a = a;update t set a = c;";
sqlRecognizers = SQLVisitorFactory.get(sql, JdbcConstants.MYSQL);
for (SQLRecognizer sqlRecognizer : sqlRecognizers) {
Assertions.assertEquals(sqlRecognizer.getClass().getName(), MySQLUpdateRecognizer.class.getName());
}
// test for mysql update and deleted
Assertions.assertThrows(UnsupportedOperationException.class, () -> {
SQLVisitorFactory.get("update t set a = a where id =1;update t set a = c where id = 1;delete from t where id =1", JdbcConstants.MYSQL);
});
// test for mysql select
Assertions.assertThrows(UnsupportedOperationException.class, () -> {
SQLVisitorFactory.get("select * from d where id = 1; select * from t where id = 2", JdbcConstants.MYSQL);
});
// test for mysql select for update
Assertions.assertThrows(UnsupportedOperationException.class, () -> {
SQLVisitorFactory.get("select * from t for update; select * from t where id = 2", JdbcConstants.MYSQL);
});
// test for oracle insert
Assertions.assertThrows(UnsupportedOperationException.class, () -> {
SQLVisitorFactory.get("insert into t(id) values (1);insert into t(id) values (2)", JdbcConstants.ORACLE);
});
// test for oracle delete and deleted
sql = "delete from t where id =1 ; delete from t where id = 2";
sqlRecognizers = SQLVisitorFactory.get(sql, JdbcConstants.ORACLE);
for (SQLRecognizer sqlRecognizer : sqlRecognizers) {
Assertions.assertEquals(sqlRecognizer.getClass().getName(), OracleDeleteRecognizer.class.getName());
}
// test for oracle update
sql = "update t set a = b where id =1 ;update t set a = c where id = 1;";
sqlRecognizers = SQLVisitorFactory.get(sql, JdbcConstants.ORACLE);
for (SQLRecognizer sqlRecognizer : sqlRecognizers) {
Assertions.assertEquals(sqlRecognizer.getClass().getName(), OracleUpdateRecognizer.class.getName());
}
// test for oracle select
Assertions.assertThrows(UnsupportedOperationException.class, () -> {
SQLVisitorFactory.get("select * from b ; select * from t where id = 2", JdbcConstants.ORACLE);
});
// test for oracle select for update
// test for mysql select for update
Assertions.assertThrows(UnsupportedOperationException.class, () -> {
SQLVisitorFactory.get("select * from t for update; select * from t where id = 2", JdbcConstants.ORACLE);
});
// test for oracle insert and update
Assertions.assertThrows(UnsupportedOperationException.class, () -> {
SQLVisitorFactory.get("insert into t(id) values (1);update t set a = t;", JdbcConstants.ORACLE);
});
// test for oracle insert and deleted
Assertions.assertThrows(UnsupportedOperationException.class, () -> {
SQLVisitorFactory.get("insert into t(id) values (1);delete from t where id = 1", JdbcConstants.ORACLE);
});
}
use of io.seata.sqlparser.SQLRecognizer in project seata by seata.
the class PostgresqlDeleteRecognizerTest method testGetWhereCondition_1.
@Test
public void testGetWhereCondition_1() {
String sql = "delete from t";
List<SQLRecognizer> sqlRecognizers = SQLVisitorFactory.get(sql, DB_TYPE);
SQLDeleteRecognizer recognizer = (SQLDeleteRecognizer) sqlRecognizers.get(0);
String whereCondition = recognizer.getWhereCondition();
// test for no condition
Assertions.assertEquals("", whereCondition);
}
use of io.seata.sqlparser.SQLRecognizer in project seata by seata.
the class PostgresqlDeleteRecognizerTest method testGetTableAlias.
@Test
public void testGetTableAlias() {
String sql = "delete from t where id = ?";
List<SQLRecognizer> sqlRecognizers = SQLVisitorFactory.get(sql, DB_TYPE);
SQLDeleteRecognizer recognizer = (SQLDeleteRecognizer) sqlRecognizers.get(0);
Assertions.assertNull(recognizer.getTableAlias());
}
use of io.seata.sqlparser.SQLRecognizer in project seata by seata.
the class PostgresqlDeleteRecognizerTest method testGetTableName.
@Test
public void testGetTableName() {
String sql = "delete from t where id = ?";
List<SQLStatement> asts = SQLUtils.parseStatements(sql, DB_TYPE);
List<SQLRecognizer> sqlRecognizers = SQLVisitorFactory.get(sql, DB_TYPE);
SQLDeleteRecognizer recognizer = (SQLDeleteRecognizer) sqlRecognizers.get(0);
Assertions.assertEquals(recognizer.getTableName(), "t");
}
use of io.seata.sqlparser.SQLRecognizer in project seata by seata.
the class AntlrMySQLRecognizerFactory method create.
@Override
public List<SQLRecognizer> create(String sqlData, String dbType) {
MySqlLexer lexer = new MySqlLexer(new ANTLRNoCaseStringStream(sqlData));
CommonTokenStream tokenStream = new CommonTokenStream(lexer);
MySqlParser parser = new MySqlParser(tokenStream);
MySqlParser.SqlStatementsContext sqlStatementsContext = parser.sqlStatements();
List<MySqlParser.SqlStatementContext> sqlStatementContexts = sqlStatementsContext.sqlStatement();
List<SQLRecognizer> recognizers = null;
SQLRecognizer recognizer = null;
for (MySqlParser.SqlStatementContext sql : sqlStatementContexts) {
StatementSqlVisitor visitor = new StatementSqlVisitor();
String originalSQL = visitor.visit(sql).toString();
SQLOperateRecognizerHolder recognizerHolder = SQLOperateRecognizerHolderFactory.getSQLRecognizerHolder(dbType.toLowerCase());
if (sql.dmlStatement().updateStatement() != null) {
recognizer = recognizerHolder.getUpdateRecognizer(originalSQL);
} else if (sql.dmlStatement().insertStatement() != null) {
recognizer = recognizerHolder.getInsertRecognizer(originalSQL);
} else if (sql.dmlStatement().deleteStatement() != null) {
recognizer = recognizerHolder.getDeleteRecognizer(originalSQL);
} else if (sql.dmlStatement().selectStatement() != null) {
recognizer = recognizerHolder.getSelectForUpdateRecognizer(originalSQL);
}
if (recognizer != null) {
if (recognizers == null) {
recognizers = new ArrayList<>();
}
recognizers.add(recognizer);
}
}
return recognizers;
}
Aggregations