Search in sources :

Example 1 with SQLInsertRecognizer

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));
}
Also used : OracleInsertExecutor(io.seata.rm.datasource.exec.oracle.OracleInsertExecutor) ConnectionContext(io.seata.rm.datasource.ConnectionContext) TableMeta(io.seata.rm.datasource.sql.struct.TableMeta) PreparedStatementProxy(io.seata.rm.datasource.PreparedStatementProxy) SQLInsertRecognizer(io.seata.sqlparser.SQLInsertRecognizer) Test(org.junit.jupiter.api.Test)

Example 2 with SQLInsertRecognizer

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);
}
Also used : Connection(java.sql.Connection) ConnectionProxy(io.seata.rm.datasource.ConnectionProxy) TableRecords(io.seata.rm.datasource.sql.struct.TableRecords) Field(java.lang.reflect.Field) MySQLInsertExecutor(io.seata.rm.datasource.exec.mysql.MySQLInsertExecutor) ConnectionContext(io.seata.rm.datasource.ConnectionContext) TableMeta(io.seata.rm.datasource.sql.struct.TableMeta) PreparedStatementProxy(io.seata.rm.datasource.PreparedStatementProxy) SQLInsertRecognizer(io.seata.sqlparser.SQLInsertRecognizer) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 3 with SQLInsertRecognizer

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();
}
Also used : MySQLInsertExecutor(io.seata.rm.datasource.exec.mysql.MySQLInsertExecutor) TableMeta(io.seata.rm.datasource.sql.struct.TableMeta) ConnectionProxy(io.seata.rm.datasource.ConnectionProxy) PreparedStatementProxy(io.seata.rm.datasource.PreparedStatementProxy) SQLInsertRecognizer(io.seata.sqlparser.SQLInsertRecognizer) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 4 with SQLInsertRecognizer

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();
    });
}
Also used : SQLRecognizer(io.seata.sqlparser.SQLRecognizer) PostgresqlInsertRecognizer(io.seata.sqlparser.druid.postgresql.PostgresqlInsertRecognizer) SQLInsertStatement(com.alibaba.druid.sql.ast.statement.SQLInsertStatement) SQLBetweenExpr(com.alibaba.druid.sql.ast.expr.SQLBetweenExpr) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SQLInsertRecognizer(io.seata.sqlparser.SQLInsertRecognizer) Test(org.junit.jupiter.api.Test)

Example 5 with SQLInsertRecognizer

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;
}
Also used : ColumnMeta(io.seata.rm.datasource.sql.struct.ColumnMeta) HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map) SQLInsertRecognizer(io.seata.sqlparser.SQLInsertRecognizer)

Aggregations

SQLInsertRecognizer (io.seata.sqlparser.SQLInsertRecognizer)13 PreparedStatementProxy (io.seata.rm.datasource.PreparedStatementProxy)6 Test (org.junit.jupiter.api.Test)6 TableMeta (io.seata.rm.datasource.sql.struct.TableMeta)5 ConnectionProxy (io.seata.rm.datasource.ConnectionProxy)4 SQLRecognizer (io.seata.sqlparser.SQLRecognizer)4 BeforeEach (org.junit.jupiter.api.BeforeEach)4 MySQLInsertExecutor (io.seata.rm.datasource.exec.mysql.MySQLInsertExecutor)3 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)2 SQLBetweenExpr (com.alibaba.druid.sql.ast.expr.SQLBetweenExpr)2 SQLInsertStatement (com.alibaba.druid.sql.ast.statement.SQLInsertStatement)2 ConnectionContext (io.seata.rm.datasource.ConnectionContext)2 PostgresqlInsertRecognizer (io.seata.sqlparser.druid.postgresql.PostgresqlInsertRecognizer)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 NotSupportYetException (io.seata.common.exception.NotSupportYetException)1 ShouldNeverHappenException (io.seata.common.exception.ShouldNeverHappenException)1 DataSourceProxy (io.seata.rm.datasource.DataSourceProxy)1 OracleInsertExecutor (io.seata.rm.datasource.exec.oracle.OracleInsertExecutor)1