mysql parser, updateStatement support join tableSource

This commit is contained in:
wenshao
2011-11-17 09:43:14 +08:00
parent f353cb1794
commit f2efb27d86
5 changed files with 24 additions and 10 deletions
@@ -27,6 +27,8 @@ public class SQLExprTableSource extends SQLTableSource {
public SQLExprTableSource(){
}
public SQLExpr getExpr() {
return this.expr;
@@ -27,21 +27,29 @@ public class SQLUpdateStatement extends SQLStatementImpl {
private static final long serialVersionUID = 1L;
private SQLName tableName;
private final List<SQLUpdateSetItem> items = new ArrayList<SQLUpdateSetItem>();
private SQLExpr where;
private SQLTableSource tableSource;
public SQLUpdateStatement(){
}
public SQLName getTableName() {
return tableName;
public SQLTableSource getTableSource() {
return tableSource;
}
public void setTableName(SQLName tableName) {
this.tableName = tableName;
public void setTableSource(SQLTableSource tableSource) {
this.tableSource = tableSource;
}
public SQLName getTableName() {
if (tableSource instanceof SQLExprTableSource) {
SQLExprTableSource exprTableSource = (SQLExprTableSource) tableSource;
return (SQLName) exprTableSource.getExpr();
}
return null;
}
public SQLExpr getWhere() {
@@ -60,7 +68,7 @@ public class SQLUpdateStatement extends SQLStatementImpl {
public void output(StringBuffer buf) {
buf.append("UPDATE ");
this.tableName.output(buf);
this.tableSource.output(buf);
buf.append(" SET ");
for (int i = 0, size = items.size(); i < size; ++i) {
@@ -79,7 +87,7 @@ public class SQLUpdateStatement extends SQLStatementImpl {
@Override
protected void accept0(SQLASTVisitor visitor) {
if (visitor.visit(this)) {
acceptChild(visitor, tableName);
acceptChild(visitor, tableSource);
acceptChild(visitor, items);
acceptChild(visitor, where);
}
@@ -30,6 +30,7 @@ import com.alibaba.druid.sql.ast.statement.SQLDropTableStatement;
import com.alibaba.druid.sql.ast.statement.SQLInsertStatement;
import com.alibaba.druid.sql.ast.statement.SQLSelectStatement;
import com.alibaba.druid.sql.ast.statement.SQLSetStatement;
import com.alibaba.druid.sql.ast.statement.SQLTableSource;
import com.alibaba.druid.sql.ast.statement.SQLUpdateSetItem;
import com.alibaba.druid.sql.ast.statement.SQLUpdateStatement;
@@ -236,7 +237,8 @@ public class SQLStatementParser extends SQLParser {
SQLUpdateStatement udpateStatement = new SQLUpdateStatement();
udpateStatement.setTableName(this.exprParser.name());
SQLTableSource tableSource = this.exprParser.createSelectParser().parseTableSource();
udpateStatement.setTableSource(tableSource);
accept(Token.SET);
@@ -620,7 +620,7 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter {
public boolean visit(SQLUpdateStatement x) {
print("UPDATE ");
x.getTableName().accept(this);
x.getTableSource().accept(this);
print(" SET ");
for (int i = 0, size = x.getItems().size(); i < size; ++i) {
@@ -25,6 +25,8 @@ public class MySqlParameterizedOutputVisitorTest extends TestCase {
"SELECT * FROM T WHERE 1 = 0 OR a.id = ?; ");
validateOracle("SELECT * FROM T WHERE A.ID = ? OR A.ID = ?", "SELECT * FROM T WHERE A.ID = ?; ");
validate("INSERT INTO T (F1, F2) VALUES(?, ?), (?, ?), (?, ?)", "INSERT INTO T (F1, F2) VALUES (?, ?)");
validate("update net_device d, sys_user u set d.resp_user_id=u.id where d.resp_user_login_name=u.username and d.id in (42354)", //
"UPDATE net_device d, sys_user u SET d.resp_user_id = u.id WHERE d.resp_user_login_name = u.username AND d.id IN (?)");
// System.out.println("SELECT * FORM A WHERE ID = (##)".replaceAll("##", "?"));
}