mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
refactor sql parser. sqlite support limit clause. #1556
This commit is contained in:
-69
@@ -32,7 +32,6 @@ public class PGSelectQueryBlock extends SQLSelectQueryBlock {
|
||||
|
||||
private PGWithClause with;
|
||||
private List<SQLExpr> distinctOn = new ArrayList<SQLExpr>(2);
|
||||
private PGLimit limit;
|
||||
private WindowClause window;
|
||||
|
||||
private SQLOrderBy orderBy;
|
||||
@@ -107,14 +106,6 @@ public class PGSelectQueryBlock extends SQLSelectQueryBlock {
|
||||
this.with = with;
|
||||
}
|
||||
|
||||
public PGLimit getLimit() {
|
||||
return limit;
|
||||
}
|
||||
|
||||
public void setLimit(PGLimit limit) {
|
||||
this.limit = limit;
|
||||
}
|
||||
|
||||
public SQLOrderBy getOrderBy() {
|
||||
return orderBy;
|
||||
}
|
||||
@@ -123,21 +114,6 @@ public class PGSelectQueryBlock extends SQLSelectQueryBlock {
|
||||
this.orderBy = orderBy;
|
||||
}
|
||||
|
||||
public SQLExpr getOffset() {
|
||||
if (limit != null) {
|
||||
return limit.offset;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void setOffset(SQLExpr offset) {
|
||||
if (limit == null) {
|
||||
limit = new PGLimit();
|
||||
limit.setParent(this);
|
||||
}
|
||||
limit.setOffset(offset);
|
||||
}
|
||||
|
||||
public List<SQLExpr> getDistinctOn() {
|
||||
return distinctOn;
|
||||
}
|
||||
@@ -255,51 +231,6 @@ public class PGSelectQueryBlock extends SQLSelectQueryBlock {
|
||||
}
|
||||
}
|
||||
|
||||
public static class PGLimit extends SQLObjectImpl implements SQLExpr, PGSQLObject {
|
||||
|
||||
public PGLimit(){
|
||||
|
||||
}
|
||||
|
||||
private SQLExpr rowCount;
|
||||
private SQLExpr offset;
|
||||
|
||||
public SQLExpr getRowCount() {
|
||||
return rowCount;
|
||||
}
|
||||
|
||||
public void setRowCount(SQLExpr rowCount) {
|
||||
if (rowCount != null) {
|
||||
rowCount.setParent(this);
|
||||
}
|
||||
this.rowCount = rowCount;
|
||||
}
|
||||
|
||||
public SQLExpr getOffset() {
|
||||
return offset;
|
||||
}
|
||||
|
||||
public void setOffset(SQLExpr offset) {
|
||||
if (offset != null) {
|
||||
offset.setParent(this);
|
||||
}
|
||||
this.offset = offset;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void accept0(SQLASTVisitor visitor) {
|
||||
accept0((PGASTVisitor) visitor);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept0(PGASTVisitor visitor) {
|
||||
if (visitor.visit(this)) {
|
||||
acceptChild(visitor, offset);
|
||||
acceptChild(visitor, rowCount);
|
||||
}
|
||||
visitor.endVisit(this);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ package com.alibaba.druid.sql.dialect.postgresql.parser;
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.SQLLimit;
|
||||
import com.alibaba.druid.sql.ast.SQLParameter;
|
||||
import com.alibaba.druid.sql.ast.SQLSetQuantifier;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
|
||||
@@ -28,7 +29,6 @@ import com.alibaba.druid.sql.ast.statement.SQLTableSource;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGFunctionTableSource;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGSelectQueryBlock;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGSelectQueryBlock.IntoOption;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGSelectQueryBlock.PGLimit;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGValuesQuery;
|
||||
import com.alibaba.druid.sql.parser.ParserException;
|
||||
import com.alibaba.druid.sql.parser.SQLExprParser;
|
||||
@@ -159,7 +159,7 @@ public class PGSelectParser extends SQLSelectParser {
|
||||
|
||||
for (;;) {
|
||||
if (lexer.token() == Token.LIMIT) {
|
||||
PGLimit limit = new PGLimit();
|
||||
SQLLimit limit = new SQLLimit();
|
||||
|
||||
lexer.nextToken();
|
||||
if (lexer.token() == Token.ALL) {
|
||||
@@ -171,9 +171,9 @@ public class PGSelectParser extends SQLSelectParser {
|
||||
|
||||
queryBlock.setLimit(limit);
|
||||
} else if (lexer.token() == Token.OFFSET) {
|
||||
PGLimit limit = queryBlock.getLimit();
|
||||
SQLLimit limit = queryBlock.getLimit();
|
||||
if (limit == null) {
|
||||
limit = new PGLimit();
|
||||
limit = new SQLLimit();
|
||||
queryBlock.setLimit(limit);
|
||||
}
|
||||
lexer.nextToken();
|
||||
|
||||
@@ -39,10 +39,6 @@ import com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGValuesQuery;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
|
||||
public interface PGASTVisitor extends SQLASTVisitor {
|
||||
|
||||
boolean visit(PGSelectQueryBlock.PGLimit x);
|
||||
|
||||
void endVisit(PGSelectQueryBlock.PGLimit x);
|
||||
|
||||
void endVisit(PGSelectQueryBlock x);
|
||||
|
||||
|
||||
-11
@@ -34,7 +34,6 @@ import com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGInsertStatement;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGSelectQueryBlock;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGSelectQueryBlock.FetchClause;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGSelectQueryBlock.ForClause;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGSelectQueryBlock.PGLimit;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGSelectQueryBlock.WindowClause;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGSelectStatement;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGShowStatement;
|
||||
@@ -158,16 +157,6 @@ public class PGASTVisitorAdapter extends SQLASTVisitorAdapter implements PGASTVi
|
||||
public boolean visit(PGFunctionTableSource x) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(PGLimit x) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(PGLimit x) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(PGTypeCastExpr x) {
|
||||
|
||||
+14
-17
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
package com.alibaba.druid.sql.dialect.postgresql.visitor;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLLimit;
|
||||
import com.alibaba.druid.sql.ast.SQLSetQuantifier;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLBinaryExpr;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectStatement;
|
||||
@@ -38,7 +39,6 @@ import com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGInsertStatement;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGSelectQueryBlock;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGSelectQueryBlock.FetchClause;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGSelectQueryBlock.ForClause;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGSelectQueryBlock.PGLimit;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGSelectQueryBlock.WindowClause;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGSelectStatement;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGShowStatement;
|
||||
@@ -233,7 +233,6 @@ public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor
|
||||
|
||||
if (x.getLimit() != null) {
|
||||
println();
|
||||
print0(ucase ? "LIMIT " : "limit ");
|
||||
x.getLimit().accept(this);
|
||||
}
|
||||
|
||||
@@ -459,21 +458,6 @@ public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(PGLimit x) {
|
||||
x.getRowCount().accept(this);
|
||||
if (x.getOffset() != null) {
|
||||
print0(ucase ? " OFFSET " : " offset ");
|
||||
x.getOffset().accept(this);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(PGLimit x) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(PGTypeCastExpr x) {
|
||||
|
||||
@@ -643,4 +627,17 @@ public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor
|
||||
x.getExpr().accept(this);
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean visit(SQLLimit x) {
|
||||
print0(ucase ? "LIMIT " : "limit ");
|
||||
|
||||
x.getRowCount().accept(this);
|
||||
|
||||
if (x.getOffset() != null) {
|
||||
print0(ucase ? " OFFSET " : " offset ");
|
||||
x.getOffset().accept(this);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
-11
@@ -40,7 +40,6 @@ import com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGInsertStatement;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGSelectQueryBlock;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGSelectQueryBlock.FetchClause;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGSelectQueryBlock.ForClause;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGSelectQueryBlock.PGLimit;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGSelectQueryBlock.WindowClause;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGSelectStatement;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGShowStatement;
|
||||
@@ -271,16 +270,6 @@ public class PGSchemaStatVisitor extends SchemaStatVisitor implements PGASTVisit
|
||||
public boolean visit(PGFunctionTableSource x) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(PGLimit x) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(PGLimit x) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(PGTypeCastExpr x) {
|
||||
|
||||
Reference in New Issue
Block a user