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