mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
bug fixed for visitor
This commit is contained in:
@@ -70,6 +70,7 @@ public class SQLInSubQueryExpr extends SQLExprImpl implements Serializable {
|
||||
@Override
|
||||
protected void accept0(SQLASTVisitor visitor) {
|
||||
if (visitor.visit(this)) {
|
||||
acceptChild(visitor,this.expr);
|
||||
acceptChild(visitor, this.subQuery);
|
||||
}
|
||||
|
||||
|
||||
@@ -7,6 +7,14 @@ public class PGCurrentOfExpr extends PGSQLObjectImpl implements SQLExpr {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
private SQLExpr cursor;
|
||||
|
||||
public PGCurrentOfExpr() {
|
||||
|
||||
}
|
||||
|
||||
public PGCurrentOfExpr(SQLExpr cursor) {
|
||||
this.cursor = cursor;
|
||||
}
|
||||
|
||||
public SQLExpr getCursor() {
|
||||
return cursor;
|
||||
|
||||
@@ -14,8 +14,17 @@ public class PGDeleteStatement extends SQLDeleteStatement implements PGSQLStatem
|
||||
private PGWithClause with;
|
||||
private boolean only = false;
|
||||
private List<SQLName> using = new ArrayList<SQLName>(2);
|
||||
private boolean returning;
|
||||
private String alias;
|
||||
|
||||
public boolean isReturning() {
|
||||
return returning;
|
||||
}
|
||||
|
||||
public void setReturning(boolean returning) {
|
||||
this.returning = returning;
|
||||
}
|
||||
|
||||
public String getAlias() {
|
||||
return alias;
|
||||
}
|
||||
@@ -47,7 +56,7 @@ public class PGDeleteStatement extends SQLDeleteStatement implements PGSQLStatem
|
||||
public void setWith(PGWithClause with) {
|
||||
this.with = with;
|
||||
}
|
||||
|
||||
|
||||
protected void accept0(SQLASTVisitor visitor) {
|
||||
accept0((PGASTVisitor) visitor);
|
||||
}
|
||||
|
||||
@@ -105,6 +105,8 @@ public class PGLexer extends Lexer {
|
||||
map.put("CASCADE", Token.CASCADE);
|
||||
map.put("RESTRICT", Token.RESTRICT);
|
||||
map.put("USING", Token.USING);
|
||||
map.put("CURRENT", Token.CURRENT);
|
||||
map.put("RETURNING", Token.RETURNING);
|
||||
|
||||
DEFAULT_MYSQL_KEYWORDS = new Keywords(map);
|
||||
}
|
||||
|
||||
+18
-2
@@ -6,6 +6,7 @@ import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.SQLName;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLDeleteStatement;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.PGCurrentOfExpr;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.PGDeleteStatement;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.PGTruncateStatement;
|
||||
import com.alibaba.druid.sql.parser.Lexer;
|
||||
@@ -59,8 +60,23 @@ public class PGSQLStatementParser extends SQLStatementParser {
|
||||
|
||||
if (lexer.token() == (Token.WHERE)) {
|
||||
lexer.nextToken();
|
||||
SQLExpr where = this.exprParser.expr();
|
||||
deleteStatement.setWhere(where);
|
||||
|
||||
if (lexer.token() == Token.CURRENT) {
|
||||
lexer.nextToken();
|
||||
accept(Token.OF);
|
||||
SQLName cursorName = this.exprParser.name();
|
||||
SQLExpr where = new PGCurrentOfExpr(cursorName);
|
||||
deleteStatement.setWhere(where);
|
||||
} else {
|
||||
SQLExpr where = this.exprParser.expr();
|
||||
deleteStatement.setWhere(where);
|
||||
}
|
||||
}
|
||||
|
||||
if (lexer.token() == Token.RETURNING) {
|
||||
lexer.nextToken();
|
||||
accept(Token.STAR);
|
||||
deleteStatement.setReturning(true);
|
||||
}
|
||||
|
||||
return deleteStatement;
|
||||
|
||||
@@ -303,6 +303,11 @@ public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor
|
||||
print("WHERE ");
|
||||
x.getWhere().accept(this);
|
||||
}
|
||||
|
||||
if (x.isReturning()) {
|
||||
println();
|
||||
print("RETURNING *");
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -125,6 +125,8 @@ public enum Token {
|
||||
CASCADE("CASCADE"),
|
||||
RESTRICT("RESTRICT"),
|
||||
USING("USING"),
|
||||
CURRENT("CURRENT"),
|
||||
RETURNING("RETURNING"),
|
||||
|
||||
// oracle
|
||||
START("START"),
|
||||
|
||||
@@ -11,6 +11,7 @@ import com.alibaba.druid.sql.ast.SQLObject;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLAggregateExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLInSubQueryExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLPropertyExpr;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLDeleteStatement;
|
||||
@@ -364,6 +365,11 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLInSubQueryExpr x) {
|
||||
return true;
|
||||
}
|
||||
|
||||
public void endVisit(SQLDeleteStatement x) {
|
||||
aliasLocal.set(null);
|
||||
|
||||
@@ -30,7 +30,7 @@ public class PGDeleteTest2 extends PGTest {
|
||||
|
||||
Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("tasks")));
|
||||
|
||||
//Assert.assertTrue(visitor.getFields().size() == 0);
|
||||
Assert.assertTrue(visitor.getFields().size() == 0);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.alibaba.druid.bvt.sql.postgresql;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.Assert;
|
||||
|
||||
import com.alibaba.druid.sql.PGTest;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.parser.PGSQLStatementParser;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.visitor.PGSchemaStatVisitor;
|
||||
import com.alibaba.druid.stat.TableStat;
|
||||
|
||||
public class PGDeleteTest3 extends PGTest {
|
||||
|
||||
public void test_0() throws Exception {
|
||||
String sql = "DELETE FROM tasks WHERE status = 'DONE' RETURNING *;";
|
||||
|
||||
PGSQLStatementParser parser = new PGSQLStatementParser(sql);
|
||||
List<SQLStatement> statementList = parser.parseStatementList();
|
||||
SQLStatement statemen = statementList.get(0);
|
||||
System.out.println(output(statementList));
|
||||
|
||||
Assert.assertEquals(1, statementList.size());
|
||||
|
||||
PGSchemaStatVisitor visitor = new PGSchemaStatVisitor();
|
||||
statemen.accept(visitor);
|
||||
|
||||
System.out.println("Tables : " + visitor.getTables());
|
||||
System.out.println("fields : " + visitor.getFields());
|
||||
|
||||
Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("tasks")));
|
||||
|
||||
Assert.assertTrue(visitor.getFields().size() == 1);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package com.alibaba.druid.bvt.sql.postgresql;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.Assert;
|
||||
|
||||
import com.alibaba.druid.sql.PGTest;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.parser.PGSQLStatementParser;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.visitor.PGSchemaStatVisitor;
|
||||
import com.alibaba.druid.stat.TableStat;
|
||||
|
||||
public class PGDeleteTest4 extends PGTest {
|
||||
|
||||
public void test_0() throws Exception {
|
||||
String sql = "DELETE FROM films WHERE producer_id IN (SELECT id FROM producers WHERE name = 'foo');";
|
||||
|
||||
PGSQLStatementParser parser = new PGSQLStatementParser(sql);
|
||||
List<SQLStatement> statementList = parser.parseStatementList();
|
||||
SQLStatement statemen = statementList.get(0);
|
||||
System.out.println(output(statementList));
|
||||
|
||||
Assert.assertEquals(1, statementList.size());
|
||||
|
||||
PGSchemaStatVisitor visitor = new PGSchemaStatVisitor();
|
||||
statemen.accept(visitor);
|
||||
|
||||
System.out.println("Tables : " + visitor.getTables());
|
||||
System.out.println("fields : " + visitor.getFields());
|
||||
|
||||
Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("films")));
|
||||
Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("producers")));
|
||||
|
||||
Assert.assertTrue(visitor.getFields().size() == 3);
|
||||
|
||||
Assert.assertTrue(visitor.getFields().contains(new TableStat.Column("films", "producer_id")));
|
||||
Assert.assertTrue(visitor.getFields().contains(new TableStat.Column("producers", "id")));
|
||||
Assert.assertTrue(visitor.getFields().contains(new TableStat.Column("producers", "name")));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user