Search in sources :

Example 1 with MySQLUpdateRecognizer

use of io.seata.sqlparser.druid.mysql.MySQLUpdateRecognizer in project seata by seata.

the class MySQLUpdateRecognizerTest method updateRecognizerTest_0.

/**
 * Update recognizer test 0.
 */
@Test
public void updateRecognizerTest_0() {
    String sql = "UPDATE t1 SET name = 'name1' WHERE id = 'id1'";
    SQLStatement statement = getSQLStatement(sql);
    MySQLUpdateRecognizer mySQLUpdateRecognizer = new MySQLUpdateRecognizer(sql, statement);
    Assertions.assertEquals(sql, mySQLUpdateRecognizer.getOriginalSQL());
    Assertions.assertEquals("t1", mySQLUpdateRecognizer.getTableName());
    Assertions.assertEquals(1, mySQLUpdateRecognizer.getUpdateColumns().size());
    Assertions.assertEquals("name", mySQLUpdateRecognizer.getUpdateColumns().get(0));
    Assertions.assertEquals("name1", mySQLUpdateRecognizer.getUpdateValues().get(0));
    Assertions.assertEquals("id = 'id1'", mySQLUpdateRecognizer.getWhereCondition());
}
Also used : MySQLUpdateRecognizer(io.seata.sqlparser.druid.mysql.MySQLUpdateRecognizer) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) Test(org.junit.jupiter.api.Test)

Example 2 with MySQLUpdateRecognizer

use of io.seata.sqlparser.druid.mysql.MySQLUpdateRecognizer in project seata by seata.

the class MySQLUpdateRecognizerTest method updateRecognizerTest_1.

/**
 * Update recognizer test 1.
 */
@Test
public void updateRecognizerTest_1() {
    String sql = "UPDATE t1 SET name1 = 'name1', name2 = 'name2' WHERE id = 'id1'";
    SQLStatement statement = getSQLStatement(sql);
    MySQLUpdateRecognizer mySQLUpdateRecognizer = new MySQLUpdateRecognizer(sql, statement);
    Assertions.assertEquals(sql, mySQLUpdateRecognizer.getOriginalSQL());
    Assertions.assertEquals("t1", mySQLUpdateRecognizer.getTableName());
    Assertions.assertEquals(2, mySQLUpdateRecognizer.getUpdateColumns().size());
    Assertions.assertEquals("name1", mySQLUpdateRecognizer.getUpdateColumns().get(0));
    Assertions.assertEquals("name1", mySQLUpdateRecognizer.getUpdateValues().get(0));
    Assertions.assertEquals("name2", mySQLUpdateRecognizer.getUpdateColumns().get(1));
    Assertions.assertEquals("name2", mySQLUpdateRecognizer.getUpdateValues().get(1));
    Assertions.assertEquals("id = 'id1'", mySQLUpdateRecognizer.getWhereCondition());
}
Also used : MySQLUpdateRecognizer(io.seata.sqlparser.druid.mysql.MySQLUpdateRecognizer) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) Test(org.junit.jupiter.api.Test)

Example 3 with MySQLUpdateRecognizer

use of io.seata.sqlparser.druid.mysql.MySQLUpdateRecognizer in project seata by seata.

the class MySQLUpdateRecognizerTest method testGetUpdateDatabaseNameColumns.

@Test
public void testGetUpdateDatabaseNameColumns() {
    // test with normal
    String sql = "update d.t set d.t.a = ?, d.t.b = ?, d.t.c = ?";
    List<SQLStatement> asts = SQLUtils.parseStatements(sql, JdbcConstants.MYSQL);
    MySQLUpdateRecognizer recognizer = new MySQLUpdateRecognizer(sql, asts.get(0));
    List<String> updateColumns = recognizer.getUpdateColumns();
    Assertions.assertEquals(updateColumns.size(), 3);
    // test with alias
    sql = "update t set a.a = ?, a.b = ?, a.c = ?";
    asts = SQLUtils.parseStatements(sql, JdbcConstants.MYSQL);
    recognizer = new MySQLUpdateRecognizer(sql, asts.get(0));
    updateColumns = recognizer.getUpdateColumns();
    Assertions.assertEquals(updateColumns.size(), 3);
    // test with error
    Assertions.assertThrows(SQLParsingException.class, () -> {
        String s = "update t set a = a";
        List<SQLStatement> sqlStatements = SQLUtils.parseStatements(s, JdbcConstants.MYSQL);
        SQLUpdateStatement sqlUpdateStatement = (SQLUpdateStatement) sqlStatements.get(0);
        List<SQLUpdateSetItem> updateSetItems = sqlUpdateStatement.getItems();
        for (SQLUpdateSetItem updateSetItem : updateSetItems) {
            updateSetItem.setColumn(new MySqlCharExpr());
        }
        MySQLUpdateRecognizer oracleUpdateRecognizer = new MySQLUpdateRecognizer(s, sqlUpdateStatement);
        oracleUpdateRecognizer.getUpdateColumns();
    });
}
Also used : SQLUpdateSetItem(com.alibaba.druid.sql.ast.statement.SQLUpdateSetItem) SQLUpdateStatement(com.alibaba.druid.sql.ast.statement.SQLUpdateStatement) MySqlCharExpr(com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlCharExpr) MySQLUpdateRecognizer(io.seata.sqlparser.druid.mysql.MySQLUpdateRecognizer) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) Test(org.junit.jupiter.api.Test)

Example 4 with MySQLUpdateRecognizer

use of io.seata.sqlparser.druid.mysql.MySQLUpdateRecognizer in project seata by seata.

the class MySQLUpdateRecognizerTest method updateRecognizerTest_4.

/**
 * Update recognizer test 4.
 */
@Test
public void updateRecognizerTest_4() {
    String sql = "UPDATE t1 SET name1 = 'name1', name2 = 'name2' WHERE id in (?, ?) and name1 = ?";
    SQLStatement statement = getSQLStatement(sql);
    MySQLUpdateRecognizer mySQLUpdateRecognizer = new MySQLUpdateRecognizer(sql, statement);
    Assertions.assertEquals(sql, mySQLUpdateRecognizer.getOriginalSQL());
    Assertions.assertEquals("t1", mySQLUpdateRecognizer.getTableName());
    Assertions.assertEquals(2, mySQLUpdateRecognizer.getUpdateColumns().size());
    Assertions.assertEquals("name1", mySQLUpdateRecognizer.getUpdateColumns().get(0));
    Assertions.assertEquals("name1", mySQLUpdateRecognizer.getUpdateValues().get(0));
    Assertions.assertEquals("name2", mySQLUpdateRecognizer.getUpdateColumns().get(1));
    Assertions.assertEquals("name2", mySQLUpdateRecognizer.getUpdateValues().get(1));
    ArrayList<List<Object>> paramAppenderList = new ArrayList<>();
    String whereCondition = mySQLUpdateRecognizer.getWhereCondition(new ParametersHolder() {

        @Override
        public Map<Integer, ArrayList<Object>> getParameters() {
            ArrayList<Object> id1Param = new ArrayList<>();
            id1Param.add("id1");
            ArrayList<Object> id2Param = new ArrayList<>();
            id2Param.add("id2");
            ArrayList<Object> name1Param = new ArrayList<>();
            name1Param.add("name");
            Map result = new HashMap();
            result.put(1, id1Param);
            result.put(2, id2Param);
            result.put(3, name1Param);
            return result;
        }
    }, paramAppenderList);
    Assertions.assertEquals(Collections.singletonList(Arrays.asList("id1", "id2", "name")), paramAppenderList);
    Assertions.assertEquals("id IN (?, ?)\nAND name1 = ?", whereCondition);
}
Also used : ParametersHolder(io.seata.sqlparser.ParametersHolder) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) MySQLUpdateRecognizer(io.seata.sqlparser.druid.mysql.MySQLUpdateRecognizer) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 5 with MySQLUpdateRecognizer

use of io.seata.sqlparser.druid.mysql.MySQLUpdateRecognizer in project seata by seata.

the class MySQLUpdateRecognizerTest method updateRecognizerTest_3.

/**
 * Update recognizer test 3.
 */
@Test
public void updateRecognizerTest_3() {
    String sql = "UPDATE t1 SET name1 = 'name1', name2 = 'name2' WHERE id in (?, ?)";
    SQLStatement statement = getSQLStatement(sql);
    MySQLUpdateRecognizer mySQLUpdateRecognizer = new MySQLUpdateRecognizer(sql, statement);
    Assertions.assertEquals(sql, mySQLUpdateRecognizer.getOriginalSQL());
    Assertions.assertEquals("t1", mySQLUpdateRecognizer.getTableName());
    Assertions.assertEquals(2, mySQLUpdateRecognizer.getUpdateColumns().size());
    Assertions.assertEquals("name1", mySQLUpdateRecognizer.getUpdateColumns().get(0));
    Assertions.assertEquals("name1", mySQLUpdateRecognizer.getUpdateValues().get(0));
    Assertions.assertEquals("name2", mySQLUpdateRecognizer.getUpdateColumns().get(1));
    Assertions.assertEquals("name2", mySQLUpdateRecognizer.getUpdateValues().get(1));
    ArrayList<List<Object>> paramAppenderList = new ArrayList<>();
    String whereCondition = mySQLUpdateRecognizer.getWhereCondition(new ParametersHolder() {

        @Override
        public Map<Integer, ArrayList<Object>> getParameters() {
            ArrayList<Object> id1Param = new ArrayList<>();
            id1Param.add("id1");
            ArrayList<Object> id2Param = new ArrayList<>();
            id2Param.add("id2");
            Map result = new HashMap();
            result.put(1, id1Param);
            result.put(2, id2Param);
            return result;
        }
    }, paramAppenderList);
    Assertions.assertEquals(Collections.singletonList(Arrays.asList("id1", "id2")), paramAppenderList);
    Assertions.assertEquals("id IN (?, ?)", whereCondition);
}
Also used : ParametersHolder(io.seata.sqlparser.ParametersHolder) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) MySQLUpdateRecognizer(io.seata.sqlparser.druid.mysql.MySQLUpdateRecognizer) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Aggregations

SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)13 MySQLUpdateRecognizer (io.seata.sqlparser.druid.mysql.MySQLUpdateRecognizer)13 Test (org.junit.jupiter.api.Test)13 List (java.util.List)6 ParametersHolder (io.seata.sqlparser.ParametersHolder)4 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 Map (java.util.Map)4 SQLUpdateSetItem (com.alibaba.druid.sql.ast.statement.SQLUpdateSetItem)3 SQLUpdateStatement (com.alibaba.druid.sql.ast.statement.SQLUpdateStatement)3 MockStatement (com.alibaba.druid.mock.MockStatement)2 MockStatementBase (com.alibaba.druid.mock.MockStatementBase)2 DruidDataSource (com.alibaba.druid.pool.DruidDataSource)2 SQLUtils (com.alibaba.druid.sql.SQLUtils)2 MySqlCharExpr (com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlCharExpr)2 JdbcConstants (com.alibaba.druid.util.JdbcConstants)2 Lists (com.google.common.collect.Lists)2 ConnectionProxy (io.seata.rm.datasource.ConnectionProxy)2 DataSourceProxy (io.seata.rm.datasource.DataSourceProxy)2 StatementProxy (io.seata.rm.datasource.StatementProxy)2