use of io.seata.sqlparser.struct.SqlSequenceExpr in project seata by seata.
the class OracleInsertExecutorTest method mockParametersPkWithSeq.
private SqlSequenceExpr mockParametersPkWithSeq() {
SqlSequenceExpr expr = new SqlSequenceExpr("seq", "nextval");
Map<Integer, ArrayList<Object>> paramters = new HashMap(4);
ArrayList arrayList0 = new ArrayList<>();
arrayList0.add(expr);
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);
List<List<Object>> rows = new ArrayList<>();
rows.add(Arrays.asList("?", "?", "?"));
when(sqlInsertRecognizer.getInsertRows(pkIndexMap.values())).thenReturn(rows);
return expr;
}
use of io.seata.sqlparser.struct.SqlSequenceExpr in project seata by seata.
the class MySQLInsertExecutorTest method test_checkPkValues.
@Test
public void test_checkPkValues() {
// ps = true
List<Object> pkValues = new ArrayList<>();
pkValues.add(Null.get());
Assertions.assertTrue(insertExecutor.checkPkValuesForSinglePk(pkValues, true));
pkValues = new ArrayList<>();
pkValues.add(Null.get());
pkValues.add(Null.get());
Assertions.assertTrue(insertExecutor.checkPkValuesForSinglePk(pkValues, true));
pkValues = new ArrayList<>();
pkValues.add(1);
Assertions.assertTrue(insertExecutor.checkPkValuesForSinglePk(pkValues, true));
pkValues = new ArrayList<>();
pkValues.add(1);
pkValues.add(2);
Assertions.assertTrue(insertExecutor.checkPkValuesForSinglePk(pkValues, true));
pkValues = new ArrayList<>();
pkValues.add(SqlMethodExpr.get());
Assertions.assertTrue(insertExecutor.checkPkValuesForSinglePk(pkValues, true));
pkValues = new ArrayList<>();
pkValues.add(SqlMethodExpr.get());
pkValues.add(SqlMethodExpr.get());
Assertions.assertTrue(insertExecutor.checkPkValuesForSinglePk(pkValues, true));
pkValues = new ArrayList<>();
pkValues.add(new SqlSequenceExpr());
Assertions.assertTrue(insertExecutor.checkPkValuesForSinglePk(pkValues, true));
pkValues = new ArrayList<>();
pkValues.add(new SqlSequenceExpr());
pkValues.add(new SqlSequenceExpr());
Assertions.assertTrue(insertExecutor.checkPkValuesForSinglePk(pkValues, true));
pkValues = new ArrayList<>();
pkValues.add(SqlDefaultExpr.get());
Assertions.assertTrue(insertExecutor.checkPkValuesForSinglePk(pkValues, true));
pkValues = new ArrayList<>();
pkValues.add(SqlDefaultExpr.get());
pkValues.add(SqlDefaultExpr.get());
Assertions.assertTrue(insertExecutor.checkPkValuesForSinglePk(pkValues, true));
// ps = false
pkValues = new ArrayList<>();
pkValues.add(Null.get());
Assertions.assertTrue(insertExecutor.checkPkValuesForSinglePk(pkValues, false));
pkValues = new ArrayList<>();
pkValues.add(Null.get());
pkValues.add(Null.get());
Assertions.assertFalse(insertExecutor.checkPkValuesForSinglePk(pkValues, false));
pkValues = new ArrayList<>();
pkValues.add(1);
Assertions.assertTrue(insertExecutor.checkPkValuesForSinglePk(pkValues, false));
pkValues = new ArrayList<>();
pkValues.add(1);
pkValues.add(2);
Assertions.assertTrue(insertExecutor.checkPkValuesForSinglePk(pkValues, false));
pkValues = new ArrayList<>();
pkValues.add(SqlMethodExpr.get());
Assertions.assertFalse(insertExecutor.checkPkValuesForSinglePk(pkValues, false));
pkValues = new ArrayList<>();
pkValues.add(SqlMethodExpr.get());
pkValues.add(SqlMethodExpr.get());
Assertions.assertFalse(insertExecutor.checkPkValuesForSinglePk(pkValues, false));
pkValues = new ArrayList<>();
pkValues.add(new SqlSequenceExpr());
Assertions.assertTrue(insertExecutor.checkPkValuesForSinglePk(pkValues, false));
pkValues = new ArrayList<>();
pkValues.add(new SqlSequenceExpr());
pkValues.add(new SqlSequenceExpr());
Assertions.assertFalse(insertExecutor.checkPkValuesForSinglePk(pkValues, false));
pkValues = new ArrayList<>();
pkValues.add(SqlDefaultExpr.get());
Assertions.assertTrue(insertExecutor.checkPkValuesForSinglePk(pkValues, false));
pkValues = new ArrayList<>();
pkValues.add(SqlDefaultExpr.get());
pkValues.add(SqlDefaultExpr.get());
Assertions.assertFalse(insertExecutor.checkPkValuesForSinglePk(pkValues, false));
// not support.
pkValues = new ArrayList<>();
pkValues.add(1);
pkValues.add(Null.get());
Assertions.assertFalse(insertExecutor.checkPkValuesForSinglePk(pkValues, true));
pkValues = new ArrayList<>();
pkValues.add(1);
pkValues.add(Null.get());
Assertions.assertFalse(insertExecutor.checkPkValuesForSinglePk(pkValues, false));
pkValues = new ArrayList<>();
pkValues.add(1);
pkValues.add(SqlMethodExpr.get());
Assertions.assertFalse(insertExecutor.checkPkValuesForSinglePk(pkValues, true));
pkValues = new ArrayList<>();
pkValues.add(1);
pkValues.add(SqlMethodExpr.get());
Assertions.assertFalse(insertExecutor.checkPkValuesForSinglePk(pkValues, false));
pkValues = new ArrayList<>();
pkValues.add(1);
pkValues.add(new SqlSequenceExpr());
Assertions.assertFalse(insertExecutor.checkPkValuesForSinglePk(pkValues, true));
pkValues = new ArrayList<>();
pkValues.add(1);
pkValues.add(new SqlSequenceExpr());
Assertions.assertFalse(insertExecutor.checkPkValuesForSinglePk(pkValues, false));
pkValues = new ArrayList<>();
pkValues.add(1);
pkValues.add(SqlDefaultExpr.get());
Assertions.assertFalse(insertExecutor.checkPkValuesForSinglePk(pkValues, true));
pkValues = new ArrayList<>();
pkValues.add(1);
pkValues.add(SqlDefaultExpr.get());
Assertions.assertFalse(insertExecutor.checkPkValuesForSinglePk(pkValues, false));
pkValues = new ArrayList<>();
pkValues.add(Null.get());
pkValues.add(SqlMethodExpr.get());
Assertions.assertFalse(insertExecutor.checkPkValuesForSinglePk(pkValues, true));
pkValues = new ArrayList<>();
pkValues.add(Null.get());
pkValues.add(SqlMethodExpr.get());
Assertions.assertFalse(insertExecutor.checkPkValuesForSinglePk(pkValues, false));
pkValues = new ArrayList<>();
pkValues.add(Null.get());
pkValues.add(new SqlSequenceExpr());
Assertions.assertFalse(insertExecutor.checkPkValuesForSinglePk(pkValues, true));
pkValues = new ArrayList<>();
pkValues.add(Null.get());
pkValues.add(new SqlSequenceExpr());
Assertions.assertFalse(insertExecutor.checkPkValuesForSinglePk(pkValues, false));
pkValues = new ArrayList<>();
pkValues.add(Null.get());
pkValues.add(SqlDefaultExpr.get());
Assertions.assertFalse(insertExecutor.checkPkValuesForSinglePk(pkValues, true));
pkValues = new ArrayList<>();
pkValues.add(Null.get());
pkValues.add(SqlDefaultExpr.get());
Assertions.assertFalse(insertExecutor.checkPkValuesForSinglePk(pkValues, false));
pkValues = new ArrayList<>();
pkValues.add(SqlMethodExpr.get());
pkValues.add(new SqlSequenceExpr());
Assertions.assertFalse(insertExecutor.checkPkValuesForSinglePk(pkValues, true));
pkValues = new ArrayList<>();
pkValues.add(SqlMethodExpr.get());
pkValues.add(new SqlSequenceExpr());
Assertions.assertFalse(insertExecutor.checkPkValuesForSinglePk(pkValues, false));
pkValues = new ArrayList<>();
pkValues.add(SqlMethodExpr.get());
pkValues.add(SqlDefaultExpr.get());
Assertions.assertFalse(insertExecutor.checkPkValuesForSinglePk(pkValues, true));
pkValues = new ArrayList<>();
pkValues.add(SqlMethodExpr.get());
pkValues.add(SqlDefaultExpr.get());
Assertions.assertFalse(insertExecutor.checkPkValuesForSinglePk(pkValues, false));
pkValues = new ArrayList<>();
pkValues.add(new SqlSequenceExpr());
pkValues.add(SqlDefaultExpr.get());
Assertions.assertFalse(insertExecutor.checkPkValuesForSinglePk(pkValues, true));
pkValues = new ArrayList<>();
pkValues.add(SqlMethodExpr.get());
pkValues.add(new SqlSequenceExpr());
pkValues.add(SqlDefaultExpr.get());
Assertions.assertFalse(insertExecutor.checkPkValuesForSinglePk(pkValues, false));
}
use of io.seata.sqlparser.struct.SqlSequenceExpr in project seata by seata.
the class PostgresqlInsertRecognizer method getInsertRows.
@Override
public List<List<Object>> getInsertRows(Collection<Integer> primaryKeyIndex) {
List<SQLInsertStatement.ValuesClause> valuesClauses = ast.getValuesList();
List<List<Object>> rows = new ArrayList<>(valuesClauses.size());
for (SQLInsertStatement.ValuesClause valuesClause : valuesClauses) {
List<SQLExpr> exprs = valuesClause.getValues();
List<Object> row = new ArrayList<>(exprs.size());
rows.add(row);
for (int i = 0, len = exprs.size(); i < len; i++) {
SQLExpr expr = exprs.get(i);
if (expr instanceof SQLNullExpr) {
row.add(Null.get());
} else if (expr instanceof SQLValuableExpr) {
row.add(((SQLValuableExpr) expr).getValue());
} else if (expr instanceof SQLVariantRefExpr) {
row.add(((SQLVariantRefExpr) expr).getName());
} else if (expr instanceof SQLMethodInvokeExpr) {
SQLMethodInvokeExpr sqlMethodInvokeExpr = (SQLMethodInvokeExpr) expr;
String function = sqlMethodInvokeExpr.getMethodName();
if (StringUtils.equalsIgnoreCase(function, "nextval")) {
String sequence = sqlMethodInvokeExpr.getParameters().get(0).toString();
row.add(new SqlSequenceExpr(sequence, function));
} else {
row.add(SqlMethodExpr.get());
}
} else if (expr instanceof SQLSequenceExpr) {
SQLSequenceExpr sequenceExpr = (SQLSequenceExpr) expr;
String sequence = sequenceExpr.getSequence().getSimpleName();
String function = sequenceExpr.getFunction().name;
row.add(new SqlSequenceExpr(sequence, function));
} else if (expr instanceof SQLDefaultExpr) {
row.add(SqlDefaultExpr.get());
} else {
if (primaryKeyIndex.contains(i)) {
throw new SQLParsingException("Unknown SQLExpr: " + expr.getClass() + " " + expr);
}
row.add(NotPlaceholderExpr.get());
}
}
}
return rows;
}
use of io.seata.sqlparser.struct.SqlSequenceExpr in project seata by seata.
the class OracleInsertExecutorTest method testPkValue_sequence.
@Test
public void testPkValue_sequence() throws Exception {
mockInsertColumns();
SqlSequenceExpr expr = mockParametersPkWithSeq();
doReturn(tableMeta).when(insertExecutor).getTableMeta();
when(tableMeta.getPrimaryKeyOnlyName()).thenReturn(Arrays.asList(new String[] { ID_COLUMN }));
List<Object> pkValuesSeq = new ArrayList<>();
pkValuesSeq.add(PK_VALUE);
doReturn(pkValuesSeq).when(insertExecutor).getPkValuesBySequence(expr);
doReturn(pkIndexMap).when(insertExecutor).getPkIndex();
Map<String, List<Object>> pkValuesByColumn = insertExecutor.getPkValuesByColumn();
verify(insertExecutor).getPkValuesBySequence(expr);
Assertions.assertEquals(pkValuesByColumn.get(ID_COLUMN), pkValuesSeq);
}
use of io.seata.sqlparser.struct.SqlSequenceExpr in project seata by seata.
the class PostgresqlInsertExecutor method getPkValuesByDefault.
/**
* get primary key values by default
* @return
* @throws SQLException
*/
@Override
public List<Object> getPkValuesByDefault() throws SQLException {
// current version 1.2 only support postgresql.
Map<String, ColumnMeta> pkMetaMap = getTableMeta().getPrimaryKeyMap();
ColumnMeta pkMeta = pkMetaMap.values().iterator().next();
String columnDef = pkMeta.getColumnDef();
// sample: nextval('test_id_seq'::regclass)
String seq = org.apache.commons.lang.StringUtils.substringBetween(columnDef, "'", "'");
String function = org.apache.commons.lang.StringUtils.substringBetween(columnDef, "", "(");
if (StringUtils.isBlank(seq)) {
throw new ShouldNeverHappenException("get primary key value failed, cause columnDef is " + columnDef);
}
return getPkValuesBySequence(new SqlSequenceExpr("'" + seq + "'", function));
}
Aggregations