Search in sources :

Example 6 with PreparedStatementProxy

use of io.seata.rm.datasource.PreparedStatementProxy in project seata by seata.

the class MySQLInsertExecutorTest method mockParameters.

private void mockParameters() {
    Map<Integer, ArrayList<Object>> paramters = new HashMap<>(4);
    ArrayList arrayList0 = new ArrayList<>();
    arrayList0.add(PK_VALUE);
    ArrayList arrayList1 = new ArrayList<>();
    arrayList1.add("userId1");
    ArrayList arrayList2 = new ArrayList<>();
    arrayList2.add("userName1");
    ArrayList arrayList3 = new ArrayList<>();
    arrayList3.add("userStatus1");
    paramters.put(1, arrayList0);
    paramters.put(2, arrayList1);
    paramters.put(3, arrayList2);
    paramters.put(4, arrayList3);
    PreparedStatementProxy psp = (PreparedStatementProxy) this.statementProxy;
    when(psp.getParameters()).thenReturn(paramters);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) PreparedStatementProxy(io.seata.rm.datasource.PreparedStatementProxy)

Example 7 with PreparedStatementProxy

use of io.seata.rm.datasource.PreparedStatementProxy in project seata by seata.

the class PostgresqlInsertExecutorTest method mockParametersPkWithDefault.

private void mockParametersPkWithDefault() {
    Map<Integer, ArrayList<Object>> parameters = new HashMap<>(4);
    ArrayList arrayList0 = new ArrayList<>();
    arrayList0.add(SqlDefaultExpr.get());
    ArrayList arrayList1 = new ArrayList<>();
    arrayList1.add("userId1");
    ArrayList arrayList2 = new ArrayList<>();
    arrayList2.add("userName1");
    ArrayList arrayList3 = new ArrayList<>();
    arrayList3.add("userStatus1");
    parameters.put(1, arrayList0);
    parameters.put(2, arrayList1);
    parameters.put(3, arrayList2);
    parameters.put(4, arrayList3);
    PreparedStatementProxy psp = (PreparedStatementProxy) this.statementProxy;
    when(psp.getParameters()).thenReturn(parameters);
}
Also used : PreparedStatementProxy(io.seata.rm.datasource.PreparedStatementProxy)

Example 8 with PreparedStatementProxy

use of io.seata.rm.datasource.PreparedStatementProxy in project seata by seata.

the class MySQLInsertExecutorTest method mockParametersOfOnePk.

private void mockParametersOfOnePk() {
    Map<Integer, ArrayList<Object>> paramters = new HashMap<>(4);
    ArrayList arrayList1 = new ArrayList<>();
    arrayList1.add(PK_VALUE);
    paramters.put(1, arrayList1);
    PreparedStatementProxy psp = (PreparedStatementProxy) this.statementProxy;
    when(psp.getParameters()).thenReturn(paramters);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) PreparedStatementProxy(io.seata.rm.datasource.PreparedStatementProxy)

Example 9 with PreparedStatementProxy

use of io.seata.rm.datasource.PreparedStatementProxy 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

PreparedStatementProxy (io.seata.rm.datasource.PreparedStatementProxy)9 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)6 SQLInsertRecognizer (io.seata.sqlparser.SQLInsertRecognizer)3 List (java.util.List)3 ConnectionContext (io.seata.rm.datasource.ConnectionContext)2 TableMeta (io.seata.rm.datasource.sql.struct.TableMeta)2 NotSupportYetException (io.seata.common.exception.NotSupportYetException)1 ShouldNeverHappenException (io.seata.common.exception.ShouldNeverHappenException)1 ConnectionProxy (io.seata.rm.datasource.ConnectionProxy)1 MySQLInsertExecutor (io.seata.rm.datasource.exec.mysql.MySQLInsertExecutor)1 OracleInsertExecutor (io.seata.rm.datasource.exec.oracle.OracleInsertExecutor)1 TableRecords (io.seata.rm.datasource.sql.struct.TableRecords)1 SqlSequenceExpr (io.seata.sqlparser.struct.SqlSequenceExpr)1 Field (java.lang.reflect.Field)1 Connection (java.sql.Connection)1 Map (java.util.Map)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1 Test (org.junit.jupiter.api.Test)1