Search in sources :

Example 6 with SQLInsertRecognizer

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

the class BaseInsertExecutor method containsPK.

protected boolean containsPK() {
    SQLInsertRecognizer recognizer = (SQLInsertRecognizer) sqlRecognizer;
    List<String> insertColumns = recognizer.getInsertColumns();
    if (CollectionUtils.isEmpty(insertColumns)) {
        return false;
    }
    return containsPK(insertColumns);
}
Also used : SQLInsertRecognizer(io.seata.sqlparser.SQLInsertRecognizer)

Example 7 with SQLInsertRecognizer

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

the class PostgresqlInsertExecutorTest method init.

@BeforeEach
public void init() {
    ConnectionProxy connectionProxy = mock(ConnectionProxy.class);
    when(connectionProxy.getDbType()).thenReturn(JdbcConstants.POSTGRESQL);
    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 PostgresqlInsertExecutor(statementProxy, statementCallback, sqlInsertRecognizer));
    pkIndexMap = new HashMap<String, Integer>() {

        {
            put(ID_COLUMN, pkIndex);
        }
    };
}
Also used : PostgresqlInsertExecutor(io.seata.rm.datasource.exec.postgresql.PostgresqlInsertExecutor) 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 8 with SQLInsertRecognizer

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

the class MySQLInsertExecutorTest method init.

@BeforeEach
public void init() throws SQLException {
    ConnectionProxy connectionProxy = mock(ConnectionProxy.class);
    when(connectionProxy.getDbType()).thenReturn(JdbcConstants.MYSQL);
    DataSourceProxy dataSourceProxy = new DataSourceProxy(new MockDataSource());
    when(connectionProxy.getDataSourceProxy()).thenReturn(dataSourceProxy);
    statementProxy = mock(PreparedStatementProxy.class);
    when(statementProxy.getConnectionProxy()).thenReturn(connectionProxy);
    when(statementProxy.getTargetStatement()).thenReturn(statementProxy);
    MockResultSet resultSet = new MockResultSet(statementProxy);
    resultSet.mockResultSet(Arrays.asList("Variable_name", "Value"), new Object[][] { { "auto_increment_increment", "1" } });
    when(statementProxy.getTargetStatement().executeQuery("SHOW VARIABLES LIKE 'auto_increment_increment'")).thenReturn(resultSet);
    StatementCallback statementCallback = mock(StatementCallback.class);
    sqlInsertRecognizer = mock(SQLInsertRecognizer.class);
    tableMeta = mock(TableMeta.class);
    insertExecutor = Mockito.spy(new MySQLInsertExecutor(statementProxy, statementCallback, sqlInsertRecognizer));
    pkIndexMap = new HashMap<String, Integer>() {

        {
            put(ID_COLUMN, pkIndex);
        }
    };
}
Also used : DataSourceProxy(io.seata.rm.datasource.DataSourceProxy) MySQLInsertExecutor(io.seata.rm.datasource.exec.mysql.MySQLInsertExecutor) MockDataSource(io.seata.rm.datasource.mock.MockDataSource) MockResultSet(io.seata.rm.datasource.mock.MockResultSet) TableMeta(io.seata.rm.datasource.sql.struct.TableMeta) Mockito.anyString(org.mockito.Mockito.anyString) ConnectionProxy(io.seata.rm.datasource.ConnectionProxy) PreparedStatementProxy(io.seata.rm.datasource.PreparedStatementProxy) SQLInsertRecognizer(io.seata.sqlparser.SQLInsertRecognizer) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 9 with SQLInsertRecognizer

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

the class PostgresqlInsertRecognizerTest method testGetInsertRows.

@Test
public void testGetInsertRows() {
    final int pkIndex = 0;
    // test for null value
    String sql = "insert into t(id, no, name, age, time) values (nextval('id_seq'), null, 'a', ?, now())";
    SQLInsertRecognizer recognizer = (SQLInsertRecognizer) SQLVisitorFactory.get(sql, DB_TYPE).get(0);
    List<List<Object>> insertRows = recognizer.getInsertRows(Collections.singletonList(pkIndex));
    Assertions.assertTrue(insertRows.size() == 1);
    // 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.getValuesList().get(0).getValues().set(pkIndex, new SQLBetweenExpr());
        PostgresqlInsertRecognizer postgresqlInsertRecognizer = new PostgresqlInsertRecognizer(s, sqlInsertStatement);
        postgresqlInsertRecognizer.getInsertRows(Collections.singletonList(pkIndex));
    });
}
Also used : PostgresqlInsertRecognizer(io.seata.sqlparser.druid.postgresql.PostgresqlInsertRecognizer) SQLInsertStatement(com.alibaba.druid.sql.ast.statement.SQLInsertStatement) SQLBetweenExpr(com.alibaba.druid.sql.ast.expr.SQLBetweenExpr) List(java.util.List) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SQLInsertRecognizer(io.seata.sqlparser.SQLInsertRecognizer) Test(org.junit.jupiter.api.Test)

Example 10 with SQLInsertRecognizer

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

the class PostgresqlInsertRecognizerTest method testGetSqlType.

@Test
public void testGetSqlType() {
    String sql = "insert into t(id) values (?)";
    List<SQLRecognizer> sqlRecognizers = SQLVisitorFactory.get(sql, DB_TYPE);
    SQLInsertRecognizer recognizer = (SQLInsertRecognizer) sqlRecognizers.get(0);
    Assertions.assertEquals(recognizer.getSQLType(), SQLType.INSERT);
}
Also used : SQLRecognizer(io.seata.sqlparser.SQLRecognizer) SQLInsertRecognizer(io.seata.sqlparser.SQLInsertRecognizer) Test(org.junit.jupiter.api.Test)

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