Search in sources :

Example 21 with SQLRecognizer

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

the class MultiExecutor method prepareUndoLog.

@Override
protected void prepareUndoLog(TableRecords beforeImage, TableRecords afterImage) throws SQLException {
    if (beforeImagesMap == null || afterImagesMap == null) {
        throw new IllegalStateException("images can not be null");
    }
    SQLRecognizer recognizer;
    for (Map.Entry<SQLRecognizer, TableRecords> entry : beforeImagesMap.entrySet()) {
        sqlRecognizer = recognizer = entry.getKey();
        beforeImage = entry.getValue();
        afterImage = afterImagesMap.get(recognizer);
        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.");
            }
        }
        super.prepareUndoLog(beforeImage, afterImage);
    }
}
Also used : TableRecords(io.seata.rm.datasource.sql.struct.TableRecords) SQLRecognizer(io.seata.sqlparser.SQLRecognizer) ShouldNeverHappenException(io.seata.common.exception.ShouldNeverHappenException) Map(java.util.Map) HashMap(java.util.HashMap)

Example 22 with SQLRecognizer

use of io.seata.sqlparser.SQLRecognizer 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 23 with SQLRecognizer

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

the class DruidSQLRecognizerFactoryTest method testSqlRecognizerCreation.

@Test
public void testSqlRecognizerCreation() {
    SQLRecognizerFactory recognizerFactory = EnhancedServiceLoader.load(SQLRecognizerFactory.class, SqlParserType.SQL_PARSER_TYPE_DRUID);
    Assertions.assertNotNull(recognizerFactory);
    List<SQLRecognizer> recognizers = recognizerFactory.create("delete from t1", JdbcConstants.MYSQL);
    Assertions.assertNotNull(recognizers);
    Assertions.assertEquals(recognizers.size(), 1);
    Assertions.assertEquals(SQLType.DELETE, recognizers.get(0).getSQLType());
}
Also used : SQLRecognizer(io.seata.sqlparser.SQLRecognizer) SQLRecognizerFactory(io.seata.sqlparser.SQLRecognizerFactory) 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