Search in sources :

Example 36 with TableMeta

use of io.seata.rm.datasource.sql.struct.TableMeta in project seata by seata.

the class DeleteExecutor method beforeImage.

@Override
protected TableRecords beforeImage() throws SQLException {
    SQLDeleteRecognizer visitor = (SQLDeleteRecognizer) sqlRecognizer;
    TableMeta tmeta = getTableMeta(visitor.getTableName());
    ArrayList<List<Object>> paramAppenderList = new ArrayList<>();
    String selectSQL = buildBeforeImageSQL(visitor, tmeta, paramAppenderList);
    return buildTableRecords(tmeta, selectSQL, paramAppenderList);
}
Also used : 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)

Example 37 with TableMeta

use of io.seata.rm.datasource.sql.struct.TableMeta in project seata by seata.

the class AbstractConnectionProxy method prepareStatement.

@Override
public PreparedStatement prepareStatement(String sql) throws SQLException {
    String dbType = getDbType();
    // support oracle 10.2+
    PreparedStatement targetPreparedStatement = null;
    if (BranchType.AT == RootContext.getBranchType()) {
        List<SQLRecognizer> sqlRecognizers = SQLVisitorFactory.get(sql, dbType);
        if (sqlRecognizers != null && sqlRecognizers.size() == 1) {
            SQLRecognizer sqlRecognizer = sqlRecognizers.get(0);
            if (sqlRecognizer != null && sqlRecognizer.getSQLType() == SQLType.INSERT) {
                TableMeta tableMeta = TableMetaCacheFactory.getTableMetaCache(dbType).getTableMeta(getTargetConnection(), sqlRecognizer.getTableName(), getDataSourceProxy().getResourceId());
                String[] pkNameArray = new String[tableMeta.getPrimaryKeyOnlyName().size()];
                tableMeta.getPrimaryKeyOnlyName().toArray(pkNameArray);
                targetPreparedStatement = getTargetConnection().prepareStatement(sql, pkNameArray);
            }
        }
    }
    if (targetPreparedStatement == null) {
        targetPreparedStatement = getTargetConnection().prepareStatement(sql);
    }
    return new PreparedStatementProxy(this, targetPreparedStatement, sql);
}
Also used : SQLRecognizer(io.seata.sqlparser.SQLRecognizer) PreparedStatement(java.sql.PreparedStatement) TableMeta(io.seata.rm.datasource.sql.struct.TableMeta)

Example 38 with TableMeta

use of io.seata.rm.datasource.sql.struct.TableMeta in project seata by seata.

the class MySQLUndoDeleteExecutorTest method init.

@BeforeAll
public static void init() {
    TableMeta tableMeta = Mockito.mock(TableMeta.class);
    Mockito.when(tableMeta.getPrimaryKeyOnlyName()).thenReturn(Arrays.asList(new String[] { "id" }));
    Mockito.when(tableMeta.getTableName()).thenReturn("table_name");
    TableRecords beforeImage = new TableRecords();
    beforeImage.setTableName("table_name");
    beforeImage.setTableMeta(tableMeta);
    List<Row> beforeRows = new ArrayList<>();
    Row row0 = new Row();
    addField(row0, "id", 1, "12345");
    addField(row0, "age", 1, "1");
    beforeRows.add(row0);
    Row row1 = new Row();
    addField(row1, "id", 1, "12346");
    addField(row1, "age", 1, "1");
    beforeRows.add(row1);
    beforeImage.setRows(beforeRows);
    TableRecords afterImage = new TableRecords();
    afterImage.setTableName("table_name");
    afterImage.setTableMeta(tableMeta);
    List<Row> afterRows = new ArrayList<>();
    Row row2 = new Row();
    addField(row2, "id", 1, "12345");
    addField(row2, "age", 1, "2");
    afterRows.add(row2);
    Row row3 = new Row();
    addField(row3, "id", 1, "12346");
    addField(row3, "age", 1, "2");
    afterRows.add(row3);
    afterImage.setRows(afterRows);
    SQLUndoLog sqlUndoLog = new SQLUndoLog();
    sqlUndoLog.setSqlType(SQLType.UPDATE);
    sqlUndoLog.setTableMeta(tableMeta);
    sqlUndoLog.setTableName("table_name");
    sqlUndoLog.setBeforeImage(beforeImage);
    sqlUndoLog.setAfterImage(afterImage);
    executor = new MySQLUndoDeleteExecutor(sqlUndoLog);
}
Also used : TableRecords(io.seata.rm.datasource.sql.struct.TableRecords) ArrayList(java.util.ArrayList) SQLUndoLog(io.seata.rm.datasource.undo.SQLUndoLog) TableMeta(io.seata.rm.datasource.sql.struct.TableMeta) Row(io.seata.rm.datasource.sql.struct.Row) BeforeAll(org.junit.jupiter.api.BeforeAll)

Aggregations

TableMeta (io.seata.rm.datasource.sql.struct.TableMeta)38 ArrayList (java.util.ArrayList)15 TableRecords (io.seata.rm.datasource.sql.struct.TableRecords)14 Test (org.junit.jupiter.api.Test)12 Row (io.seata.rm.datasource.sql.struct.Row)11 Field (io.seata.rm.datasource.sql.struct.Field)8 ResultSet (java.sql.ResultSet)7 DataSourceProxy (io.seata.rm.datasource.DataSourceProxy)6 SQLUndoLog (io.seata.rm.datasource.undo.SQLUndoLog)6 DruidDataSource (com.alibaba.druid.pool.DruidDataSource)5 ConnectionProxy (io.seata.rm.datasource.ConnectionProxy)5 PreparedStatementProxy (io.seata.rm.datasource.PreparedStatementProxy)5 MockDriver (io.seata.rm.datasource.mock.MockDriver)5 SQLInsertRecognizer (io.seata.sqlparser.SQLInsertRecognizer)5 PreparedStatement (java.sql.PreparedStatement)5 List (java.util.List)5 BeforeEach (org.junit.jupiter.api.BeforeEach)5 ShouldNeverHappenException (io.seata.common.exception.ShouldNeverHappenException)4 ColumnMeta (io.seata.rm.datasource.sql.struct.ColumnMeta)4 MySQLInsertExecutor (io.seata.rm.datasource.exec.mysql.MySQLInsertExecutor)3