Search in sources :

Example 1 with SqlSequenceExpr

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;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) SqlSequenceExpr(io.seata.sqlparser.struct.SqlSequenceExpr) PreparedStatementProxy(io.seata.rm.datasource.PreparedStatementProxy)

Example 2 with SqlSequenceExpr

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));
}
Also used : ArrayList(java.util.ArrayList) SqlSequenceExpr(io.seata.sqlparser.struct.SqlSequenceExpr) Test(org.junit.jupiter.api.Test)

Example 3 with SqlSequenceExpr

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;
}
Also used : SQLMethodInvokeExpr(com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr) ArrayList(java.util.ArrayList) SQLNullExpr(com.alibaba.druid.sql.ast.expr.SQLNullExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) SQLValuableExpr(com.alibaba.druid.sql.ast.expr.SQLValuableExpr) SQLParsingException(io.seata.sqlparser.SQLParsingException) SQLInsertStatement(com.alibaba.druid.sql.ast.statement.SQLInsertStatement) SQLVariantRefExpr(com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr) ArrayList(java.util.ArrayList) List(java.util.List) SQLDefaultExpr(com.alibaba.druid.sql.ast.expr.SQLDefaultExpr) SQLSequenceExpr(com.alibaba.druid.sql.ast.expr.SQLSequenceExpr) SqlSequenceExpr(io.seata.sqlparser.struct.SqlSequenceExpr)

Example 4 with SqlSequenceExpr

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);
}
Also used : ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) SqlSequenceExpr(io.seata.sqlparser.struct.SqlSequenceExpr) Test(org.junit.jupiter.api.Test)

Example 5 with SqlSequenceExpr

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));
}
Also used : ColumnMeta(io.seata.rm.datasource.sql.struct.ColumnMeta) ShouldNeverHappenException(io.seata.common.exception.ShouldNeverHappenException) SqlSequenceExpr(io.seata.sqlparser.struct.SqlSequenceExpr)

Aggregations

SqlSequenceExpr (io.seata.sqlparser.struct.SqlSequenceExpr)6 ArrayList (java.util.ArrayList)5 List (java.util.List)4 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)2 SQLMethodInvokeExpr (com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr)2 SQLNullExpr (com.alibaba.druid.sql.ast.expr.SQLNullExpr)2 SQLSequenceExpr (com.alibaba.druid.sql.ast.expr.SQLSequenceExpr)2 SQLValuableExpr (com.alibaba.druid.sql.ast.expr.SQLValuableExpr)2 SQLVariantRefExpr (com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr)2 SQLInsertStatement (com.alibaba.druid.sql.ast.statement.SQLInsertStatement)2 SQLParsingException (io.seata.sqlparser.SQLParsingException)2 Test (org.junit.jupiter.api.Test)2 SQLDefaultExpr (com.alibaba.druid.sql.ast.expr.SQLDefaultExpr)1 ShouldNeverHappenException (io.seata.common.exception.ShouldNeverHappenException)1 PreparedStatementProxy (io.seata.rm.datasource.PreparedStatementProxy)1 ColumnMeta (io.seata.rm.datasource.sql.struct.ColumnMeta)1 HashMap (java.util.HashMap)1