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