use of io.seata.sqlparser.SQLInsertRecognizer in project seata by seata.
the class AbstractDMLBaseExecutorTest method testOnlySupportMysqlWhenUseMultiPk.
@Test
public void testOnlySupportMysqlWhenUseMultiPk() {
Mockito.when(connectionProxy.getContext()).thenReturn(new ConnectionContext());
PreparedStatementProxy statementProxy = Mockito.mock(PreparedStatementProxy.class);
Mockito.when(statementProxy.getConnectionProxy()).thenReturn(connectionProxy);
StatementCallback statementCallback = Mockito.mock(StatementCallback.class);
SQLInsertRecognizer sqlInsertRecognizer = Mockito.mock(SQLInsertRecognizer.class);
TableMeta tableMeta = Mockito.mock(TableMeta.class);
executor = Mockito.spy(new OracleInsertExecutor(statementProxy, statementCallback, sqlInsertRecognizer));
Mockito.when(executor.getDbType()).thenReturn(JdbcConstants.ORACLE);
Mockito.doReturn(tableMeta).when(executor).getTableMeta();
Mockito.when(tableMeta.getPrimaryKeyOnlyName()).thenReturn(Arrays.asList("id", "userCode"));
Assertions.assertThrows(NotSupportYetException.class, () -> executor.executeAutoCommitFalse(null));
}
use of io.seata.sqlparser.SQLInsertRecognizer in project seata by seata.
the class AbstractDMLBaseExecutorTest method initBeforeEach.
@BeforeEach
public void initBeforeEach() throws Exception {
branchRollbackFlagField = ConnectionProxy.LockRetryPolicy.class.getDeclaredField("LOCK_RETRY_POLICY_BRANCH_ROLLBACK_ON_CONFLICT");
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(branchRollbackFlagField, branchRollbackFlagField.getModifiers() & ~Modifier.FINAL);
branchRollbackFlagField.setAccessible(true);
boolean branchRollbackFlag = (boolean) branchRollbackFlagField.get(null);
Assertions.assertTrue(branchRollbackFlag);
Connection targetConnection = Mockito.mock(Connection.class);
connectionProxy = Mockito.mock(ConnectionProxy.class);
Mockito.doThrow(new LockConflictException()).when(connectionProxy).commit();
Mockito.when(connectionProxy.getAutoCommit()).thenReturn(Boolean.TRUE);
Mockito.when(connectionProxy.getTargetConnection()).thenReturn(targetConnection);
Mockito.when(connectionProxy.getContext()).thenReturn(new ConnectionContext());
PreparedStatementProxy statementProxy = Mockito.mock(PreparedStatementProxy.class);
Mockito.when(statementProxy.getConnectionProxy()).thenReturn(connectionProxy);
StatementCallback statementCallback = Mockito.mock(StatementCallback.class);
SQLInsertRecognizer sqlInsertRecognizer = Mockito.mock(SQLInsertRecognizer.class);
TableMeta tableMeta = Mockito.mock(TableMeta.class);
executor = Mockito.spy(new MySQLInsertExecutor(statementProxy, statementCallback, sqlInsertRecognizer));
Mockito.doReturn(tableMeta).when(executor).getTableMeta();
TableRecords tableRecords = new TableRecords();
Mockito.doReturn(tableRecords).when(executor).beforeImage();
Mockito.doReturn(tableRecords).when(executor).afterImage(tableRecords);
}
use of io.seata.sqlparser.SQLInsertRecognizer in project seata by seata.
the class BatchInsertExecutorTest method init.
@BeforeEach
public void init() {
ConnectionProxy connectionProxy = mock(ConnectionProxy.class);
when(connectionProxy.getDbType()).thenReturn(JdbcConstants.MYSQL);
statementProxy = mock(PreparedStatementProxy.class);
when(statementProxy.getConnectionProxy()).thenReturn(connectionProxy);
StatementCallback statementCallback = mock(StatementCallback.class);
sqlInsertRecognizer = mock(SQLInsertRecognizer.class);
tableMeta = mock(TableMeta.class);
insertExecutor = Mockito.spy(new MySQLInsertExecutor(statementProxy, statementCallback, sqlInsertRecognizer));
pkIndexMap = new HashMap() {
{
put(ID_COLUMN, pkIndex);
}
};
doReturn(pkIndexMap).when(insertExecutor).getPkIndex();
}
use of io.seata.sqlparser.SQLInsertRecognizer in project seata by seata.
the class PostgresqlInsertRecognizerTest method testGetInsertColumns.
@Test
public void testGetInsertColumns() {
// test for no column
String sql = "insert into t values (?)";
List<SQLRecognizer> sqlRecognizers = SQLVisitorFactory.get(sql, DB_TYPE);
SQLInsertRecognizer recognizer = (SQLInsertRecognizer) sqlRecognizers.get(0);
List<String> insertColumns = recognizer.getInsertColumns();
Assertions.assertNull(insertColumns);
// test for normal
sql = "insert into t(a) values (?)";
recognizer = (SQLInsertRecognizer) SQLVisitorFactory.get(sql, DB_TYPE).get(0);
insertColumns = recognizer.getInsertColumns();
Assertions.assertEquals(1, insertColumns.size());
// test for exception
Assertions.assertThrows(SQLParsingException.class, () -> {
String s = "insert into t(a) values (?)";
List<SQLStatement> sqlStatements = SQLUtils.parseStatements(s, DB_TYPE);
SQLInsertStatement sqlInsertStatement = (SQLInsertStatement) sqlStatements.get(0);
sqlInsertStatement.getColumns().add(new SQLBetweenExpr());
PostgresqlInsertRecognizer postgresqlInsertRecognizer = new PostgresqlInsertRecognizer(s, sqlInsertStatement);
postgresqlInsertRecognizer.getInsertColumns();
});
}
use of io.seata.sqlparser.SQLInsertRecognizer in project seata by seata.
the class BaseInsertExecutor method getPkIndex.
/**
* get pk index
* @return the key is pk column name and the value is index of the pk column
*/
protected Map<String, Integer> getPkIndex() {
Map<String, Integer> pkIndexMap = new HashMap<>();
SQLInsertRecognizer recognizer = (SQLInsertRecognizer) sqlRecognizer;
List<String> insertColumns = recognizer.getInsertColumns();
if (CollectionUtils.isNotEmpty(insertColumns)) {
final int insertColumnsSize = insertColumns.size();
for (int paramIdx = 0; paramIdx < insertColumnsSize; paramIdx++) {
String sqlColumnName = insertColumns.get(paramIdx);
if (containPK(sqlColumnName)) {
pkIndexMap.put(getStandardPkColumnName(sqlColumnName), paramIdx);
}
}
return pkIndexMap;
}
int pkIndex = -1;
Map<String, ColumnMeta> allColumns = getTableMeta().getAllColumns();
for (Map.Entry<String, ColumnMeta> entry : allColumns.entrySet()) {
pkIndex++;
if (containPK(entry.getValue().getColumnName())) {
pkIndexMap.put(ColumnUtils.delEscape(entry.getValue().getColumnName(), getDbType()), pkIndex);
}
}
return pkIndexMap;
}
Aggregations