Search in sources :

Example 11 with ShouldNeverHappenException

use of io.seata.common.exception.ShouldNeverHappenException in project seata by seata.

the class OracleUndoDeleteExecutor method buildUndoSQL.

@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.ORACLE));
    // 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.ORACLE)).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)

Example 12 with ShouldNeverHappenException

use of io.seata.common.exception.ShouldNeverHappenException in project seata by seata.

the class OracleUndoInsertExecutor method buildUndoSQL.

@Override
protected String buildUndoSQL() {
    TableRecords afterImage = sqlUndoLog.getAfterImage();
    List<Row> afterImageRows = afterImage.getRows();
    if (CollectionUtils.isEmpty(afterImageRows)) {
        throw new ShouldNeverHappenException("Invalid UNDO LOG");
    }
    return generateDeleteSql(afterImageRows, afterImage);
}
Also used : TableRecords(io.seata.rm.datasource.sql.struct.TableRecords) ShouldNeverHappenException(io.seata.common.exception.ShouldNeverHappenException) Row(io.seata.rm.datasource.sql.struct.Row)

Example 13 with ShouldNeverHappenException

use of io.seata.common.exception.ShouldNeverHappenException in project seata by seata.

the class PostgresqlTableMetaCache method resultSetMetaToSchema.

private TableMeta resultSetMetaToSchema(DatabaseMetaData dbmd, String tableName) throws SQLException {
    TableMeta tm = new TableMeta();
    tm.setTableName(tableName);
    String[] schemaTable = tableName.split("\\.");
    String schemaName = schemaTable.length > 1 ? schemaTable[0] : null;
    tableName = schemaTable.length > 1 ? schemaTable[1] : tableName;
    /*
         * use ResultSetMetaData to get the pure table name
         * can avoid the problem below
         *
         * select * from account_tbl
         * select * from account_TBL
         * select * from account_tbl
         * select * from account.account_tbl
         * select * from "select"
         * select * from "Select"
         * select * from "Sel""ect"
         * select * from "Sel'ect"
         * select * from TEST.test
         * select * from test.TEST
         * select * from "Test".test
         * select * from "Test"."Select"
         */
    if (schemaName != null) {
        if (schemaName.startsWith("\"") && schemaName.endsWith("\"")) {
            schemaName = schemaName.replaceAll("(^\")|(\"$)", "");
        } else {
            schemaName = schemaName.toLowerCase();
        }
    }
    if (tableName.startsWith("\"") && tableName.endsWith("\"")) {
        tableName = tableName.replaceAll("(^\")|(\"$)", "");
    } else {
        tableName = tableName.toLowerCase();
    }
    try (ResultSet rsColumns = dbmd.getColumns(null, schemaName, tableName, "%");
        ResultSet rsIndex = dbmd.getIndexInfo(null, schemaName, tableName, false, true);
        ResultSet rsPrimary = dbmd.getPrimaryKeys(null, schemaName, tableName)) {
        while (rsColumns.next()) {
            ColumnMeta col = new ColumnMeta();
            col.setTableCat(rsColumns.getString("TABLE_CAT"));
            col.setTableSchemaName(rsColumns.getString("TABLE_SCHEM"));
            col.setTableName(rsColumns.getString("TABLE_NAME"));
            col.setColumnName(rsColumns.getString("COLUMN_NAME"));
            col.setDataType(rsColumns.getInt("DATA_TYPE"));
            col.setDataTypeName(rsColumns.getString("TYPE_NAME"));
            col.setColumnSize(rsColumns.getInt("COLUMN_SIZE"));
            col.setDecimalDigits(rsColumns.getInt("DECIMAL_DIGITS"));
            col.setNumPrecRadix(rsColumns.getInt("NUM_PREC_RADIX"));
            col.setNullAble(rsColumns.getInt("NULLABLE"));
            col.setRemarks(rsColumns.getString("REMARKS"));
            col.setColumnDef(rsColumns.getString("COLUMN_DEF"));
            col.setSqlDataType(rsColumns.getInt("SQL_DATA_TYPE"));
            col.setSqlDatetimeSub(rsColumns.getInt("SQL_DATETIME_SUB"));
            col.setCharOctetLength(rsColumns.getObject("CHAR_OCTET_LENGTH"));
            col.setOrdinalPosition(rsColumns.getInt("ORDINAL_POSITION"));
            col.setIsNullAble(rsColumns.getString("IS_NULLABLE"));
            col.setIsAutoincrement(rsColumns.getString("IS_AUTOINCREMENT"));
            tm.getAllColumns().put(col.getColumnName(), col);
        }
        while (rsIndex.next()) {
            String indexName = rsIndex.getString("index_name");
            if (StringUtils.isNullOrEmpty(indexName)) {
                continue;
            }
            String colName = rsIndex.getString("column_name");
            ColumnMeta col = tm.getAllColumns().get(colName);
            if (tm.getAllIndexes().containsKey(indexName)) {
                IndexMeta index = tm.getAllIndexes().get(indexName);
                index.getValues().add(col);
            } else {
                IndexMeta index = new IndexMeta();
                index.setIndexName(indexName);
                index.setNonUnique(rsIndex.getBoolean("non_unique"));
                index.setIndexQualifier(rsIndex.getString("index_qualifier"));
                index.setIndexName(rsIndex.getString("index_name"));
                index.setType(rsIndex.getShort("type"));
                index.setOrdinalPosition(rsIndex.getShort("ordinal_position"));
                index.setAscOrDesc(rsIndex.getString("asc_or_desc"));
                index.setCardinality(rsIndex.getInt("cardinality"));
                index.getValues().add(col);
                if (!index.isNonUnique()) {
                    index.setIndextype(IndexType.UNIQUE);
                } else {
                    index.setIndextype(IndexType.NORMAL);
                }
                tm.getAllIndexes().put(indexName, index);
            }
        }
        while (rsPrimary.next()) {
            String pkIndexName = rsPrimary.getString("pk_name");
            if (tm.getAllIndexes().containsKey(pkIndexName)) {
                IndexMeta index = tm.getAllIndexes().get(pkIndexName);
                index.setIndextype(IndexType.PRIMARY);
            }
        }
        if (tm.getAllIndexes().isEmpty()) {
            throw new ShouldNeverHappenException("Could not found any index in the table: " + tableName);
        }
    }
    return tm;
}
Also used : ColumnMeta(io.seata.rm.datasource.sql.struct.ColumnMeta) ResultSet(java.sql.ResultSet) ShouldNeverHappenException(io.seata.common.exception.ShouldNeverHappenException) IndexMeta(io.seata.rm.datasource.sql.struct.IndexMeta) TableMeta(io.seata.rm.datasource.sql.struct.TableMeta)

Example 14 with ShouldNeverHappenException

use of io.seata.common.exception.ShouldNeverHappenException in project seata by seata.

the class BaseTransactionalExecutor method prepareUndoLog.

/**
 * prepare undo log.
 *
 * @param beforeImage the before image
 * @param afterImage  the after image
 * @throws SQLException the sql exception
 */
protected void prepareUndoLog(TableRecords beforeImage, TableRecords afterImage) throws SQLException {
    if (beforeImage.getRows().isEmpty() && afterImage.getRows().isEmpty()) {
        return;
    }
    if (SQLType.UPDATE == sqlRecognizer.getSQLType()) {
        if (beforeImage.getRows().size() != afterImage.getRows().size()) {
            throw new ShouldNeverHappenException("Before image size is not equaled to after image size, probably because you updated the primary keys.");
        }
    }
    ConnectionProxy connectionProxy = statementProxy.getConnectionProxy();
    TableRecords lockKeyRecords = sqlRecognizer.getSQLType() == SQLType.DELETE ? beforeImage : afterImage;
    String lockKeys = buildLockKey(lockKeyRecords);
    if (null != lockKeys) {
        connectionProxy.appendLockKey(lockKeys);
        SQLUndoLog sqlUndoLog = buildUndoItem(beforeImage, afterImage);
        connectionProxy.appendUndoLog(sqlUndoLog);
    }
}
Also used : TableRecords(io.seata.rm.datasource.sql.struct.TableRecords) ShouldNeverHappenException(io.seata.common.exception.ShouldNeverHappenException) SQLUndoLog(io.seata.rm.datasource.undo.SQLUndoLog) ConnectionProxy(io.seata.rm.datasource.ConnectionProxy)

Example 15 with ShouldNeverHappenException

use of io.seata.common.exception.ShouldNeverHappenException 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)40 TableRecords (io.seata.rm.datasource.sql.struct.TableRecords)11 Row (io.seata.rm.datasource.sql.struct.Row)9 ArrayList (java.util.ArrayList)9 List (java.util.List)8 TransactionException (io.seata.core.exception.TransactionException)7 SQLUndoLog (io.seata.rm.datasource.undo.SQLUndoLog)7 CollectionUtils (io.seata.common.util.CollectionUtils)6 ColumnUtils (io.seata.rm.datasource.ColumnUtils)6 Field (io.seata.rm.datasource.sql.struct.Field)6 AbstractUndoExecutor (io.seata.rm.datasource.undo.AbstractUndoExecutor)6 JdbcConstants (io.seata.sqlparser.util.JdbcConstants)6 Collectors (java.util.stream.Collectors)6 ColumnMeta (io.seata.rm.datasource.sql.struct.ColumnMeta)5 TableMeta (io.seata.rm.datasource.sql.struct.TableMeta)4 Method (java.lang.reflect.Method)4 ResultSet (java.sql.ResultSet)4 SqlGenerateUtils (io.seata.rm.datasource.SqlGenerateUtils)3 IndexMeta (io.seata.rm.datasource.sql.struct.IndexMeta)3 HashMap (java.util.HashMap)3