Search in sources :

Example 11 with SQLRecognizer

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);
    });
}
Also used : MySQLDeleteRecognizer(io.seata.sqlparser.druid.mysql.MySQLDeleteRecognizer) OracleInsertRecognizer(io.seata.sqlparser.druid.oracle.OracleInsertRecognizer) SQLRecognizer(io.seata.sqlparser.SQLRecognizer) OracleSelectForUpdateRecognizer(io.seata.sqlparser.druid.oracle.OracleSelectForUpdateRecognizer) MySQLSelectForUpdateRecognizer(io.seata.sqlparser.druid.mysql.MySQLSelectForUpdateRecognizer) OracleDeleteRecognizer(io.seata.sqlparser.druid.oracle.OracleDeleteRecognizer) MySQLInsertRecognizer(io.seata.sqlparser.druid.mysql.MySQLInsertRecognizer) MySQLUpdateRecognizer(io.seata.sqlparser.druid.mysql.MySQLUpdateRecognizer) OracleUpdateRecognizer(io.seata.sqlparser.druid.oracle.OracleUpdateRecognizer) Test(org.junit.jupiter.api.Test)

Example 12 with SQLRecognizer

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);
}
Also used : SQLRecognizer(io.seata.sqlparser.SQLRecognizer) SQLDeleteRecognizer(io.seata.sqlparser.SQLDeleteRecognizer) Test(org.junit.jupiter.api.Test)

Example 13 with SQLRecognizer

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());
}
Also used : SQLRecognizer(io.seata.sqlparser.SQLRecognizer) SQLDeleteRecognizer(io.seata.sqlparser.SQLDeleteRecognizer) Test(org.junit.jupiter.api.Test)

Example 14 with SQLRecognizer

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");
}
Also used : SQLRecognizer(io.seata.sqlparser.SQLRecognizer) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SQLDeleteRecognizer(io.seata.sqlparser.SQLDeleteRecognizer) Test(org.junit.jupiter.api.Test)

Example 15 with SQLRecognizer

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;
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) MySqlParser(io.seata.sqlparser.antlr.mysql.parser.MySqlParser) StatementSqlVisitor(io.seata.sqlparser.antlr.mysql.visit.StatementSqlVisitor) ANTLRNoCaseStringStream(io.seata.sqlparser.antlr.mysql.stream.ANTLRNoCaseStringStream) MySqlLexer(io.seata.sqlparser.antlr.mysql.parser.MySqlLexer) SQLRecognizer(io.seata.sqlparser.SQLRecognizer) SQLOperateRecognizerHolder(io.seata.sqlparser.antlr.SQLOperateRecognizerHolder)

Aggregations

SQLRecognizer (io.seata.sqlparser.SQLRecognizer)23 Test (org.junit.jupiter.api.Test)13 SQLDeleteRecognizer (io.seata.sqlparser.SQLDeleteRecognizer)6 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)4 TableRecords (io.seata.rm.datasource.sql.struct.TableRecords)4 SQLInsertRecognizer (io.seata.sqlparser.SQLInsertRecognizer)4 NotSupportYetException (io.seata.common.exception.NotSupportYetException)3 TableMeta (io.seata.rm.datasource.sql.struct.TableMeta)3 ParametersHolder (io.seata.sqlparser.ParametersHolder)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Map (java.util.Map)3 StringJoiner (java.util.StringJoiner)3 SQLInsertStatement (com.alibaba.druid.sql.ast.statement.SQLInsertStatement)2 StatementProxy (io.seata.rm.datasource.StatementProxy)2 SQLUpdateRecognizer (io.seata.sqlparser.SQLUpdateRecognizer)2 SQLException (java.sql.SQLException)2 HashSet (java.util.HashSet)2 MockStatement (com.alibaba.druid.mock.MockStatement)1 MockStatementBase (com.alibaba.druid.mock.MockStatementBase)1