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));
}
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));
}
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));
}
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));
}
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));
}
Aggregations