use of com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement in project druid by alibaba.
the class MySqlInsertTest_4 method test_0.
public void test_0() throws Exception {
String sql = "INSERT INTO tbl_name (a,b,c) VALUES(1,2,3),(4,5,6),(7,8,9);";
MySqlStatementParser parser = new MySqlStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement stmt = statementList.get(0);
MySqlInsertStatement insertStmt = (MySqlInsertStatement) stmt;
Assert.assertEquals(3, insertStmt.getColumns().size());
Assert.assertEquals(3, insertStmt.getValuesList().size());
Assert.assertEquals(3, insertStmt.getValuesList().get(0).getValues().size());
Assert.assertEquals(3, insertStmt.getValuesList().get(1).getValues().size());
Assert.assertEquals(3, insertStmt.getValuesList().get(2).getValues().size());
Assert.assertEquals(1, statementList.size());
MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
stmt.accept(visitor);
Assert.assertEquals(//
"INSERT INTO tbl_name (a, b, c)" + "\nVALUES (1, 2, 3)," + "\n\t(4, 5, 6)," + "\n\t(7, 8, 9)", SQLUtils.toMySqlString(insertStmt));
}
use of com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement in project druid by alibaba.
the class MySqlInsertTest_5 method test_0.
public void test_0() throws Exception {
String sql = "INSERT INTO tbl_name (a,b,c) VALUES(1,2,3,4,5,6,7,8,9);";
MySqlStatementParser parser = new MySqlStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement stmt = statementList.get(0);
MySqlInsertStatement insertStmt = (MySqlInsertStatement) stmt;
Assert.assertEquals(3, insertStmt.getColumns().size());
Assert.assertEquals(1, insertStmt.getValuesList().size());
Assert.assertEquals(9, insertStmt.getValuesList().get(0).getValues().size());
Assert.assertEquals(1, statementList.size());
MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
stmt.accept(visitor);
Assert.assertEquals(//
"INSERT INTO tbl_name (a, b, c)" + //
"\nVALUES (1, 2, 3, 4, 5" + "\n\t, 6, 7, 8, 9)", SQLUtils.toMySqlString(insertStmt));
}
use of com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement in project druid by alibaba.
the class MySqlInsertTest_6 method test_0.
public void test_0() throws Exception {
String sql = //
"insert into document(the_key,the_namespace,Gmt_create,Gmt_modify,Expired_time,the_value) " + //
"values (?,?,now(),now(),date_add(now(),interval ? second),?) " + "on duplicate key update Gmt_modify = values(Gmt_modify),Expired_time = values(Expired_time),the_value = values(the_value)";
MySqlStatementParser parser = new MySqlStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement stmt = statementList.get(0);
MySqlInsertStatement insertStmt = (MySqlInsertStatement) stmt;
Assert.assertEquals(6, insertStmt.getColumns().size());
Assert.assertEquals(1, insertStmt.getValuesList().size());
Assert.assertEquals(6, insertStmt.getValuesList().get(0).getValues().size());
Assert.assertEquals(1, statementList.size());
MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
stmt.accept(visitor);
Assert.assertEquals(//
"INSERT INTO document (the_key, the_namespace, Gmt_create, Gmt_modify, Expired_time" + //
"\n\t, the_value)" + //
"\nVALUES (?, ?, now(), now(), date_add(now(), INTERVAL ? SECOND)" + //
"\n\t, ?)" + "\nON DUPLICATE KEY UPDATE Gmt_modify = VALUES(Gmt_modify), Expired_time = VALUES(Expired_time), the_value = VALUES(the_value)", SQLUtils.toMySqlString(insertStmt));
Assert.assertEquals(//
"insert into document (the_key, the_namespace, Gmt_create, Gmt_modify, Expired_time" + //
"\n\t, the_value)" + //
"\nvalues (?, ?, now(), now(), date_add(now(), interval ? second)" + //
"\n\t, ?)" + "\non duplicate key update Gmt_modify = VALUES(Gmt_modify), Expired_time = VALUES(Expired_time), the_value = VALUES(the_value)", SQLUtils.toMySqlString(insertStmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
}
use of com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement in project druid by alibaba.
the class SQLMergeTest method merge2.
private String merge2() {
String sql = "INSERT INTO T (F1, F2, F3, F4, F5) VALUES (?, ?, ?, ?, ?), (?, ?, ?, ?, ?), (?, ?, ?, ?, ?)";
StringBuilder out = new StringBuilder();
MySqlOutputVisitor visitor = new MySqlOutputVisitor(out) {
public boolean visit(SQLInListExpr x) {
x.getExpr().accept(this);
if (x.isNot()) {
print(" NOT IN (##)");
} else {
print(" IN (##)");
}
return false;
}
@Override
public boolean visit(MySqlInsertStatement x) {
print("INSERT ");
if (x.isLowPriority()) {
print("LOW_PRIORITY ");
}
if (x.isDelayed()) {
print("DELAYED ");
}
if (x.isHighPriority()) {
print("HIGH_PRIORITY ");
}
if (x.isIgnore()) {
print("IGNORE ");
}
print("INTO ");
x.getTableSource().accept(this);
if (x.getColumns().size() > 0) {
print(" (");
for (int i = 0, size = x.getColumns().size(); i < size; ++i) {
if (i != 0) {
print(", ");
}
x.getColumns().get(i).accept(this);
}
print(")");
}
if (x.getValuesList().size() != 0) {
print(" VALUES ");
int size = x.getValuesList().size();
if (size == 0) {
print("()");
} else {
for (int i = 0; i < 1; ++i) {
if (i != 0) {
print(", ");
}
x.getValuesList().get(i).accept(this);
}
}
}
if (x.getQuery() != null) {
print(" ");
x.getQuery().accept(this);
}
if (x.getDuplicateKeyUpdate().size() != 0) {
print(" ON DUPLICATE KEY UPDATE ");
printAndAccept(x.getDuplicateKeyUpdate(), ", ");
}
return false;
}
};
MySqlStatementParser parser = new MySqlStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
for (SQLStatement statement : statementList) {
statement.accept(visitor);
visitor.println();
}
return out.toString();
}
Aggregations