Search in sources :

Example 11 with SQLInsertRecognizer

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

the class PostgresqlInsertRecognizerTest method testGetTableAlias.

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

Example 12 with SQLInsertRecognizer

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

the class PostgresqlInsertRecognizerTest method testGetTableName.

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

Example 13 with SQLInsertRecognizer

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

the class BaseInsertExecutor method parsePkValuesFromStatement.

/**
 * parse primary key value from statement.
 * @return
 */
protected Map<String, List<Object>> parsePkValuesFromStatement() {
    // insert values including PK
    SQLInsertRecognizer recognizer = (SQLInsertRecognizer) sqlRecognizer;
    final Map<String, Integer> pkIndexMap = getPkIndex();
    if (pkIndexMap.isEmpty()) {
        throw new ShouldNeverHappenException("pkIndex is not found");
    }
    Map<String, List<Object>> pkValuesMap = new HashMap<>();
    boolean ps = true;
    if (statementProxy instanceof PreparedStatementProxy) {
        PreparedStatementProxy preparedStatementProxy = (PreparedStatementProxy) statementProxy;
        List<List<Object>> insertRows = recognizer.getInsertRows(pkIndexMap.values());
        if (insertRows != null && !insertRows.isEmpty()) {
            Map<Integer, ArrayList<Object>> parameters = preparedStatementProxy.getParameters();
            final int rowSize = insertRows.size();
            int totalPlaceholderNum = -1;
            for (List<Object> row : insertRows) {
                // insert parameter count will than the actual +1
                if (row.isEmpty()) {
                    continue;
                }
                int currentRowPlaceholderNum = -1;
                for (Object r : row) {
                    if (PLACEHOLDER.equals(r)) {
                        totalPlaceholderNum += 1;
                        currentRowPlaceholderNum += 1;
                    }
                }
                String pkKey;
                int pkIndex;
                List<Object> pkValues;
                for (Map.Entry<String, Integer> entry : pkIndexMap.entrySet()) {
                    pkKey = entry.getKey();
                    pkValues = pkValuesMap.get(pkKey);
                    if (Objects.isNull(pkValues)) {
                        pkValues = new ArrayList<>(rowSize);
                    }
                    pkIndex = entry.getValue();
                    Object pkValue = row.get(pkIndex);
                    if (PLACEHOLDER.equals(pkValue)) {
                        int currentRowNotPlaceholderNumBeforePkIndex = 0;
                        for (int n = 0, len = row.size(); n < len; n++) {
                            Object r = row.get(n);
                            if (n < pkIndex && !PLACEHOLDER.equals(r)) {
                                currentRowNotPlaceholderNumBeforePkIndex++;
                            }
                        }
                        int idx = totalPlaceholderNum - currentRowPlaceholderNum + pkIndex - currentRowNotPlaceholderNumBeforePkIndex;
                        ArrayList<Object> parameter = parameters.get(idx + 1);
                        pkValues.addAll(parameter);
                    } else {
                        pkValues.add(pkValue);
                    }
                    if (!pkValuesMap.containsKey(ColumnUtils.delEscape(pkKey, getDbType()))) {
                        pkValuesMap.put(ColumnUtils.delEscape(pkKey, getDbType()), pkValues);
                    }
                }
            }
        }
    } else {
        ps = false;
        List<List<Object>> insertRows = recognizer.getInsertRows(pkIndexMap.values());
        for (List<Object> row : insertRows) {
            pkIndexMap.forEach((pkKey, pkIndex) -> {
                List<Object> pkValues = pkValuesMap.get(pkKey);
                if (Objects.isNull(pkValues)) {
                    pkValuesMap.put(ColumnUtils.delEscape(pkKey, getDbType()), Lists.newArrayList(row.get(pkIndex)));
                } else {
                    pkValues.add(row.get(pkIndex));
                }
            });
        }
    }
    if (pkValuesMap.isEmpty()) {
        throw new ShouldNeverHappenException();
    }
    boolean b = this.checkPkValues(pkValuesMap, ps);
    if (!b) {
        throw new NotSupportYetException(String.format("not support sql [%s]", sqlRecognizer.getOriginalSQL()));
    }
    return pkValuesMap;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ShouldNeverHappenException(io.seata.common.exception.ShouldNeverHappenException) ArrayList(java.util.ArrayList) List(java.util.List) PreparedStatementProxy(io.seata.rm.datasource.PreparedStatementProxy) HashMap(java.util.HashMap) Map(java.util.Map) NotSupportYetException(io.seata.common.exception.NotSupportYetException) 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