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);
}
}
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);
}
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());
}
Aggregations