Search in sources :

Example 1 with AbstractUndoExecutor

use of io.seata.rm.datasource.undo.AbstractUndoExecutor in project seata by seata.

the class MySQLUndoDeleteExecutor method buildUndoSQL.

/**
 * Undo delete.
 *
 * Notice: PK is at last one.
 * @see AbstractUndoExecutor#undoPrepare
 *
 * @return sql
 */
@Override
protected String buildUndoSQL() {
    TableRecords beforeImage = sqlUndoLog.getBeforeImage();
    List<Row> beforeImageRows = beforeImage.getRows();
    if (CollectionUtils.isEmpty(beforeImageRows)) {
        throw new ShouldNeverHappenException("Invalid UNDO LOG");
    }
    Row row = beforeImageRows.get(0);
    List<Field> fields = new ArrayList<>(row.nonPrimaryKeys());
    fields.addAll(getOrderedPkList(beforeImage, row, JdbcConstants.MYSQL));
    // delete sql undo log before image all field come from table meta, need add escape.
    // see BaseTransactionalExecutor#buildTableRecords
    String insertColumns = fields.stream().map(field -> ColumnUtils.addEscape(field.getName(), JdbcConstants.MYSQL)).collect(Collectors.joining(", "));
    String insertValues = fields.stream().map(field -> "?").collect(Collectors.joining(", "));
    return String.format(INSERT_SQL_TEMPLATE, sqlUndoLog.getTableName(), insertColumns, insertValues);
}
Also used : TableRecords(io.seata.rm.datasource.sql.struct.TableRecords) ColumnUtils(io.seata.rm.datasource.ColumnUtils) List(java.util.List) Field(io.seata.rm.datasource.sql.struct.Field) AbstractUndoExecutor(io.seata.rm.datasource.undo.AbstractUndoExecutor) CollectionUtils(io.seata.common.util.CollectionUtils) Row(io.seata.rm.datasource.sql.struct.Row) JdbcConstants(io.seata.sqlparser.util.JdbcConstants) Collectors(java.util.stream.Collectors) ShouldNeverHappenException(io.seata.common.exception.ShouldNeverHappenException) TableRecords(io.seata.rm.datasource.sql.struct.TableRecords) SQLUndoLog(io.seata.rm.datasource.undo.SQLUndoLog) ArrayList(java.util.ArrayList) Field(io.seata.rm.datasource.sql.struct.Field) ShouldNeverHappenException(io.seata.common.exception.ShouldNeverHappenException) ArrayList(java.util.ArrayList) Row(io.seata.rm.datasource.sql.struct.Row)

Aggregations

ShouldNeverHappenException (io.seata.common.exception.ShouldNeverHappenException)1 CollectionUtils (io.seata.common.util.CollectionUtils)1 ColumnUtils (io.seata.rm.datasource.ColumnUtils)1 Field (io.seata.rm.datasource.sql.struct.Field)1 Row (io.seata.rm.datasource.sql.struct.Row)1 TableRecords (io.seata.rm.datasource.sql.struct.TableRecords)1 AbstractUndoExecutor (io.seata.rm.datasource.undo.AbstractUndoExecutor)1 SQLUndoLog (io.seata.rm.datasource.undo.SQLUndoLog)1 JdbcConstants (io.seata.sqlparser.util.JdbcConstants)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1