Search in sources :

Example 1 with PostgresqlInsertRecognizer

use of io.seata.sqlparser.druid.postgresql.PostgresqlInsertRecognizer in project seata by seata.

the class PostgresqlInsertRecognizerTest method testGetInsertColumns.

@Test
public void testGetInsertColumns() {
    // test for no column
    String sql = "insert into t values (?)";
    List<SQLRecognizer> sqlRecognizers = SQLVisitorFactory.get(sql, DB_TYPE);
    SQLInsertRecognizer recognizer = (SQLInsertRecognizer) sqlRecognizers.get(0);
    List<String> insertColumns = recognizer.getInsertColumns();
    Assertions.assertNull(insertColumns);
    // test for normal
    sql = "insert into t(a) values (?)";
    recognizer = (SQLInsertRecognizer) SQLVisitorFactory.get(sql, DB_TYPE).get(0);
    insertColumns = recognizer.getInsertColumns();
    Assertions.assertEquals(1, insertColumns.size());
    // test for exception
    Assertions.assertThrows(SQLParsingException.class, () -> {
        String s = "insert into t(a) values (?)";
        List<SQLStatement> sqlStatements = SQLUtils.parseStatements(s, DB_TYPE);
        SQLInsertStatement sqlInsertStatement = (SQLInsertStatement) sqlStatements.get(0);
        sqlInsertStatement.getColumns().add(new SQLBetweenExpr());
        PostgresqlInsertRecognizer postgresqlInsertRecognizer = new PostgresqlInsertRecognizer(s, sqlInsertStatement);
        postgresqlInsertRecognizer.getInsertColumns();
    });
}
Also used : SQLRecognizer(io.seata.sqlparser.SQLRecognizer) PostgresqlInsertRecognizer(io.seata.sqlparser.druid.postgresql.PostgresqlInsertRecognizer) SQLInsertStatement(com.alibaba.druid.sql.ast.statement.SQLInsertStatement) SQLBetweenExpr(com.alibaba.druid.sql.ast.expr.SQLBetweenExpr) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SQLInsertRecognizer(io.seata.sqlparser.SQLInsertRecognizer) Test(org.junit.jupiter.api.Test)

Example 2 with PostgresqlInsertRecognizer

use of io.seata.sqlparser.druid.postgresql.PostgresqlInsertRecognizer in project seata by seata.

the class PostgresqlInsertRecognizerTest method testGetInsertRows.

@Test
public void testGetInsertRows() {
    final int pkIndex = 0;
    // test for null value
    String sql = "insert into t(id, no, name, age, time) values (nextval('id_seq'), null, 'a', ?, now())";
    SQLInsertRecognizer recognizer = (SQLInsertRecognizer) SQLVisitorFactory.get(sql, DB_TYPE).get(0);
    List<List<Object>> insertRows = recognizer.getInsertRows(Collections.singletonList(pkIndex));
    Assertions.assertTrue(insertRows.size() == 1);
    // test for exception
    Assertions.assertThrows(SQLParsingException.class, () -> {
        String s = "insert into t(a) values (?)";
        List<SQLStatement> sqlStatements = SQLUtils.parseStatements(s, DB_TYPE);
        SQLInsertStatement sqlInsertStatement = (SQLInsertStatement) sqlStatements.get(0);
        sqlInsertStatement.getValuesList().get(0).getValues().set(pkIndex, new SQLBetweenExpr());
        PostgresqlInsertRecognizer postgresqlInsertRecognizer = new PostgresqlInsertRecognizer(s, sqlInsertStatement);
        postgresqlInsertRecognizer.getInsertRows(Collections.singletonList(pkIndex));
    });
}
Also used : PostgresqlInsertRecognizer(io.seata.sqlparser.druid.postgresql.PostgresqlInsertRecognizer) SQLInsertStatement(com.alibaba.druid.sql.ast.statement.SQLInsertStatement) SQLBetweenExpr(com.alibaba.druid.sql.ast.expr.SQLBetweenExpr) List(java.util.List) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SQLInsertRecognizer(io.seata.sqlparser.SQLInsertRecognizer) Test(org.junit.jupiter.api.Test)

Aggregations

SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)2 SQLBetweenExpr (com.alibaba.druid.sql.ast.expr.SQLBetweenExpr)2 SQLInsertStatement (com.alibaba.druid.sql.ast.statement.SQLInsertStatement)2 SQLInsertRecognizer (io.seata.sqlparser.SQLInsertRecognizer)2 PostgresqlInsertRecognizer (io.seata.sqlparser.druid.postgresql.PostgresqlInsertRecognizer)2 Test (org.junit.jupiter.api.Test)2 SQLRecognizer (io.seata.sqlparser.SQLRecognizer)1 List (java.util.List)1