Search in sources :

Example 6 with SQLRecognizer

use of io.seata.sqlparser.SQLRecognizer in project seata by seata.

the class PostgresqlDeleteRecognizerTest method testGetSqlType.

@Test
public void testGetSqlType() {
    String sql = "delete from t where id = ?";
    List<SQLRecognizer> sqlRecognizers = SQLVisitorFactory.get(sql, DB_TYPE);
    SQLDeleteRecognizer recognizer = (SQLDeleteRecognizer) sqlRecognizers.get(0);
    Assertions.assertEquals(recognizer.getSQLType(), SQLType.DELETE);
}
Also used : SQLRecognizer(io.seata.sqlparser.SQLRecognizer) SQLDeleteRecognizer(io.seata.sqlparser.SQLDeleteRecognizer) Test(org.junit.jupiter.api.Test)

Example 7 with SQLRecognizer

use of io.seata.sqlparser.SQLRecognizer in project seata by seata.

the class MultiDeleteExecutor method beforeImage.

@Override
protected TableRecords beforeImage() throws SQLException {
    if (sqlRecognizers.size() == 1) {
        DeleteExecutor executor = new DeleteExecutor(statementProxy, statementCallback, sqlRecognizers.get(0));
        return executor.beforeImage();
    }
    final TableMeta tmeta = getTableMeta(sqlRecognizers.get(0).getTableName());
    final ArrayList<List<Object>> paramAppenderList = new ArrayList<>();
    StringBuilder whereCondition = new StringBuilder();
    for (SQLRecognizer recognizer : sqlRecognizers) {
        sqlRecognizer = recognizer;
        SQLDeleteRecognizer visitor = (SQLDeleteRecognizer) recognizer;
        ParametersHolder parametersHolder = statementProxy instanceof ParametersHolder ? (ParametersHolder) statementProxy : null;
        if (StringUtils.isNotBlank(visitor.getLimit(parametersHolder, paramAppenderList))) {
            throw new NotSupportYetException("Multi delete SQL with limit condition is not support yet !");
        }
        if (StringUtils.isNotBlank(visitor.getOrderBy())) {
            throw new NotSupportYetException("Multi delete SQL with orderBy condition is not support yet !");
        }
        String whereConditionStr = buildWhereCondition(visitor, paramAppenderList);
        if (StringUtils.isBlank(whereConditionStr)) {
            whereCondition = new StringBuilder();
            paramAppenderList.clear();
            break;
        }
        if (whereCondition.length() > 0) {
            whereCondition.append(" OR ");
        }
        whereCondition.append(whereConditionStr);
    }
    StringBuilder suffix = new StringBuilder(" FROM ").append(getFromTableInSQL());
    if (whereCondition.length() > 0) {
        suffix.append(" WHERE ").append(whereCondition);
    }
    suffix.append(" FOR UPDATE");
    final StringJoiner selectSQLAppender = new StringJoiner(", ", "SELECT ", suffix.toString());
    for (String column : tmeta.getAllColumns().keySet()) {
        selectSQLAppender.add(getColumnNameInSQL(ColumnUtils.addEscape(column, getDbType())));
    }
    return buildTableRecords(tmeta, selectSQLAppender.toString(), paramAppenderList);
}
Also used : ParametersHolder(io.seata.sqlparser.ParametersHolder) SQLRecognizer(io.seata.sqlparser.SQLRecognizer) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) TableMeta(io.seata.rm.datasource.sql.struct.TableMeta) SQLDeleteRecognizer(io.seata.sqlparser.SQLDeleteRecognizer) NotSupportYetException(io.seata.common.exception.NotSupportYetException) StringJoiner(java.util.StringJoiner)

Example 8 with SQLRecognizer

use of io.seata.sqlparser.SQLRecognizer in project seata by seata.

the class PostgresqlInsertRecognizerTest method testGetSqlType.

@Test
public void testGetSqlType() {
    String sql = "insert into t(id) values (?)";
    List<SQLRecognizer> sqlRecognizers = SQLVisitorFactory.get(sql, DB_TYPE);
    SQLInsertRecognizer recognizer = (SQLInsertRecognizer) sqlRecognizers.get(0);
    Assertions.assertEquals(recognizer.getSQLType(), SQLType.INSERT);
}
Also used : SQLRecognizer(io.seata.sqlparser.SQLRecognizer) SQLInsertRecognizer(io.seata.sqlparser.SQLInsertRecognizer) Test(org.junit.jupiter.api.Test)

Example 9 with SQLRecognizer

use of io.seata.sqlparser.SQLRecognizer in project seata by seata.

the class PostgresqlInsertRecognizerTest method testGetTableAlias.

@Test
public void testGetTableAlias() {
    String sql = "insert into t(id) values (?)";
    List<SQLRecognizer> sqlRecognizers = SQLVisitorFactory.get(sql, DB_TYPE);
    SQLInsertRecognizer recognizer = (SQLInsertRecognizer) sqlRecognizers.get(0);
    Assertions.assertNull(recognizer.getTableAlias());
}
Also used : SQLRecognizer(io.seata.sqlparser.SQLRecognizer) SQLInsertRecognizer(io.seata.sqlparser.SQLInsertRecognizer) Test(org.junit.jupiter.api.Test)

Example 10 with SQLRecognizer

use of io.seata.sqlparser.SQLRecognizer in project seata by seata.

the class PostgresqlInsertRecognizerTest method testGetTableName.

@Test
public void testGetTableName() {
    String sql = "insert into t(id) values (?)";
    List<SQLRecognizer> sqlRecognizers = SQLVisitorFactory.get(sql, DB_TYPE);
    SQLInsertRecognizer recognizer = (SQLInsertRecognizer) sqlRecognizers.get(0);
    Assertions.assertEquals(recognizer.getTableName(), "t");
}
Also used : SQLRecognizer(io.seata.sqlparser.SQLRecognizer) SQLInsertRecognizer(io.seata.sqlparser.SQLInsertRecognizer) Test(org.junit.jupiter.api.Test)

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