Search in sources :

Example 1 with SQLDeleteRecognizer

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

the class PostgresqlDeleteRecognizerTest method testGetWhereCondition_0.

@Test
public void testGetWhereCondition_0() {
    String sql = "delete from t";
    List<SQLRecognizer> sqlRecognizers = SQLVisitorFactory.get(sql, DB_TYPE);
    SQLDeleteRecognizer recognizer = (SQLDeleteRecognizer) sqlRecognizers.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);
}
Also used : ParametersHolder(io.seata.sqlparser.ParametersHolder) SQLRecognizer(io.seata.sqlparser.SQLRecognizer) SQLDeleteRecognizer(io.seata.sqlparser.SQLDeleteRecognizer) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 2 with SQLDeleteRecognizer

use of io.seata.sqlparser.SQLDeleteRecognizer 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 3 with SQLDeleteRecognizer

use of io.seata.sqlparser.SQLDeleteRecognizer 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 4 with SQLDeleteRecognizer

use of io.seata.sqlparser.SQLDeleteRecognizer 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 5 with SQLDeleteRecognizer

use of io.seata.sqlparser.SQLDeleteRecognizer 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)

Aggregations

SQLDeleteRecognizer (io.seata.sqlparser.SQLDeleteRecognizer)7 SQLRecognizer (io.seata.sqlparser.SQLRecognizer)6 Test (org.junit.jupiter.api.Test)5 TableMeta (io.seata.rm.datasource.sql.struct.TableMeta)2 ParametersHolder (io.seata.sqlparser.ParametersHolder)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)1 NotSupportYetException (io.seata.common.exception.NotSupportYetException)1 Map (java.util.Map)1 StringJoiner (java.util.StringJoiner)1