Search in sources :

Example 41 with MySqlInsertStatement

use of com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement in project druid by alibaba.

the class MySqlInsertTest_14 method test_insert_rollback_on_fail.

public void test_insert_rollback_on_fail() throws Exception {
    String sql = "insert into  test.heartbeat (id ,ts)" + " values ( 1, UNIX_TIMESTAMP())" + " ON DUPLICATE KEY UPDATE ts =  UNIX_TIMESTAMP()";
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    parser.setParseCompleteValues(false);
    parser.setParseValuesSize(3);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement stmt = statementList.get(0);
    MySqlInsertStatement insertStmt = (MySqlInsertStatement) stmt;
    Assert.assertEquals(1, insertStmt.getValuesList().size());
    Assert.assertEquals(2, insertStmt.getValues().getValues().size());
    Assert.assertEquals(2, insertStmt.getColumns().size());
    Assert.assertEquals(1, statementList.size());
    MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
    stmt.accept(visitor);
    assertTrue(visitor.containsTable("test.heartbeat"));
    String formatSql = "INSERT INTO test.heartbeat (id, ts)\n" + "VALUES (1, UNIX_TIMESTAMP())\n" + "ON DUPLICATE KEY UPDATE ts = UNIX_TIMESTAMP()";
    Assert.assertEquals(formatSql, SQLUtils.toMySqlString(insertStmt));
}
Also used : MySqlSchemaStatVisitor(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor) MySqlInsertStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 42 with MySqlInsertStatement

use of com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement in project druid by alibaba.

the class MySqlInsertTest_16_hint method test_insert_rollback_on_fail.

public void test_insert_rollback_on_fail() throws Exception {
    String sql = "insert into mytable /*!(col2, col3, col1) */ values (load_file('sompath'), 'str1', 2);";
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    parser.setParseCompleteValues(false);
    parser.setParseValuesSize(3);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement stmt = statementList.get(0);
    MySqlInsertStatement insertStmt = (MySqlInsertStatement) stmt;
    Assert.assertEquals(1, insertStmt.getValuesList().size());
    Assert.assertEquals(3, insertStmt.getValues().getValues().size());
    Assert.assertEquals(0, insertStmt.getColumns().size());
    Assert.assertEquals(1, statementList.size());
    MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
    stmt.accept(visitor);
    // System.out.println("Tables : " + visitor.getTables());
    // System.out.println("fields : " + visitor.getColumns());
    // System.out.println("coditions : " + visitor.getConditions());
    // System.out.println("relationships : " + visitor.getRelationships());
    // 
    assertTrue(visitor.containsTable("mytable"));
    String formatSql = "INSERT INTO mytable\n" + "VALUES (load_file('sompath'), 'str1', 2);";
    Assert.assertEquals(formatSql, SQLUtils.toMySqlString(insertStmt));
}
Also used : MySqlSchemaStatVisitor(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor) MySqlInsertStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 43 with MySqlInsertStatement

use of com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement in project druid by alibaba.

the class MySqlInsertTest_13 method test_insert_rollback_on_fail.

public void test_insert_rollback_on_fail() throws Exception {
    String sql = "insert into order (id, name) values (?, ?)";
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    parser.setParseCompleteValues(false);
    parser.setParseValuesSize(3);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement stmt = statementList.get(0);
    MySqlInsertStatement insertStmt = (MySqlInsertStatement) stmt;
    Assert.assertEquals(1, insertStmt.getValuesList().size());
    Assert.assertEquals(2, insertStmt.getValues().getValues().size());
    Assert.assertEquals(2, insertStmt.getColumns().size());
    Assert.assertEquals(1, statementList.size());
    MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
    stmt.accept(visitor);
    String formatSql = "INSERT INTO order (id, name)\n" + "VALUES (?, ?)";
    Assert.assertEquals(formatSql, SQLUtils.toMySqlString(insertStmt));
}
Also used : MySqlSchemaStatVisitor(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor) MySqlInsertStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 44 with MySqlInsertStatement

use of com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement in project druid by alibaba.

the class MySqlInsertTest_15 method test_insert_rollback_on_fail.

public void test_insert_rollback_on_fail() throws Exception {
    String sql = "insert into t.t1 (id,id2) values (1,2);";
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    parser.setParseCompleteValues(false);
    parser.setParseValuesSize(3);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement stmt = statementList.get(0);
    MySqlInsertStatement insertStmt = (MySqlInsertStatement) stmt;
    Assert.assertEquals(1, insertStmt.getValuesList().size());
    Assert.assertEquals(2, insertStmt.getValues().getValues().size());
    Assert.assertEquals(2, insertStmt.getColumns().size());
    Assert.assertEquals(1, statementList.size());
    MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
    stmt.accept(visitor);
    // System.out.println("Tables : " + visitor.getTables());
    // System.out.println("fields : " + visitor.getColumns());
    // System.out.println("coditions : " + visitor.getConditions());
    // System.out.println("relationships : " + visitor.getRelationships());
    // 
    assertTrue(visitor.containsTable("t.t1"));
    String formatSql = "INSERT INTO t.t1 (id, id2)\n" + "VALUES (1, 2);";
    Assert.assertEquals(formatSql, SQLUtils.toMySqlString(insertStmt));
}
Also used : MySqlSchemaStatVisitor(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor) MySqlInsertStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 45 with MySqlInsertStatement

use of com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement in project druid by alibaba.

the class MySqlInsertTest_26_special method test_insert_timestamp.

public void test_insert_timestamp() throws Exception {
    String sql = "INSERT INTO SUNTEST(ID,NAME) VALUES(1,'\\_ASDFASDF')";
    {
        List<Object> outParameters = new ArrayList<Object>();
        String psql = ParameterizedOutputVisitorUtils.parameterize(sql, DbType.mysql, outParameters);
        assertEquals("INSERT INTO SUNTEST(ID, NAME)\n" + "VALUES (?, ?)", psql);
        assertEquals(2, outParameters.size());
        String rsql = ParameterizedOutputVisitorUtils.restore(psql, DbType.mysql, outParameters);
        assertEquals("INSERT INTO SUNTEST (ID, NAME)\n" + "VALUES (1, '\\_ASDFASDF')", rsql);
    }
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement stmt = statementList.get(0);
    MySqlInsertStatement insertStmt = (MySqlInsertStatement) stmt;
    assertEquals("INSERT INTO SUNTEST (ID, NAME)\n" + "VALUES (1, '\\_ASDFASDF')", SQLUtils.toMySqlString(insertStmt));
}
Also used : List(java.util.List) ArrayList(java.util.ArrayList) MySqlInsertStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Aggregations

MySqlInsertStatement (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement)47 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)39 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)33 MySqlSchemaStatVisitor (com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor)14 SQLStatementParser (com.alibaba.druid.sql.parser.SQLStatementParser)8 TableConfig (io.mycat.config.model.TableConfig)8 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)7 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)7 ArrayList (java.util.ArrayList)7 List (java.util.List)7 SQLNonTransientException (java.sql.SQLNonTransientException)6 RouteResultset (io.mycat.route.RouteResultset)5 SQLSyntaxErrorException (java.sql.SQLSyntaxErrorException)5 SQLIntegerExpr (com.alibaba.druid.sql.ast.expr.SQLIntegerExpr)4 ValuesClause (com.alibaba.druid.sql.ast.statement.SQLInsertStatement.ValuesClause)4 RuleConfig (io.mycat.config.model.rule.RuleConfig)3 RouteResultsetNode (io.mycat.route.RouteResultsetNode)3 SlotFunction (io.mycat.route.function.SlotFunction)3 MycatStatementParser (io.mycat.route.parser.druid.MycatStatementParser)3 RouteCalculateUnit (io.mycat.route.parser.druid.RouteCalculateUnit)3