Search in sources :

Example 16 with MySqlInsertStatement

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

the class MySqlInsertTest_10 method test_parseCompleteValues_true.

public void test_parseCompleteValues_true() throws Exception {
    String sql = "insert into t(a,b) values ('a1','b1'),('a2','b2'),('a3','b3'),('a4','b4');";
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    parser.setParseCompleteValues(true);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement stmt = statementList.get(0);
    MySqlInsertStatement insertStmt = (MySqlInsertStatement) stmt;
    Assert.assertEquals(4, 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 t (a, b)" + //
    "\nVALUES ('a1', 'b1')," + //
    "\n\t('a2', 'b2')," + //
    "\n\t('a3', 'b3')," + "\n\t('a4', 'b4')";
    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 17 with MySqlInsertStatement

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

the class MySqlInsertTest_11 method test_insert_rollback_on_fail.

public void test_insert_rollback_on_fail() throws Exception {
    String sql = "insert ROLLBACK_ON_FAIL into t(a,b) values ('a1','b1')";
    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 ROLLBACK_ON_FAIL INTO t (a, b)" + "\nVALUES ('a1', 'b1')";
    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 18 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 19 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 20 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)

Aggregations

MySqlInsertStatement (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement)24 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)19 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)16 MySqlSchemaStatVisitor (com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor)12 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)4 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)4 ValuesClause (com.alibaba.druid.sql.ast.statement.SQLInsertStatement.ValuesClause)4 TableConfig (io.mycat.config.model.TableConfig)3 SQLNonTransientException (java.sql.SQLNonTransientException)3 SQLSyntaxErrorException (java.sql.SQLSyntaxErrorException)3 SQLIntegerExpr (com.alibaba.druid.sql.ast.expr.SQLIntegerExpr)2 SQLStatementParser (com.alibaba.druid.sql.parser.SQLStatementParser)2 RouteResultset (io.mycat.route.RouteResultset)2 SQLCommentHint (com.alibaba.druid.sql.ast.SQLCommentHint)1 SQLLimit (com.alibaba.druid.sql.ast.SQLLimit)1 SQLName (com.alibaba.druid.sql.ast.SQLName)1 SQLBinaryExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryExpr)1 SQLBooleanExpr (com.alibaba.druid.sql.ast.expr.SQLBooleanExpr)1 SQLInListExpr (com.alibaba.druid.sql.ast.expr.SQLInListExpr)1 SQLPropertyExpr (com.alibaba.druid.sql.ast.expr.SQLPropertyExpr)1