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);
}
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);
}
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);
}
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;
}
Aggregations