mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
sql parser direct support OVER
This commit is contained in:
@@ -1,81 +0,0 @@
|
||||
/*
|
||||
* Copyright 1999-2011 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.alibaba.druid.sql.dialect.postgresql.ast;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLAggregateExpr;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.expr.PGAnalytic;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.visitor.PGASTVisitor;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
|
||||
public class PGAggregateExpr extends SQLAggregateExpr implements PGSQLObject {
|
||||
|
||||
private PGAnalytic over;
|
||||
|
||||
public PGAnalytic getOver() {
|
||||
return over;
|
||||
}
|
||||
|
||||
public void setOver(PGAnalytic over) {
|
||||
this.over = over;
|
||||
}
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public PGAggregateExpr(String methodName, Option option) {
|
||||
super(methodName, option);
|
||||
}
|
||||
|
||||
public PGAggregateExpr(String methodName) {
|
||||
super(methodName);
|
||||
}
|
||||
|
||||
public void output(StringBuffer buf) {
|
||||
buf.append(this.methodName);
|
||||
buf.append("(");
|
||||
int i = 0;
|
||||
for (int size = this.arguments.size(); i < size; ++i) {
|
||||
((SQLExpr) this.arguments.get(i)).output(buf);
|
||||
}
|
||||
buf.append(")");
|
||||
|
||||
if (this.over != null) {
|
||||
buf.append(" ");
|
||||
this.over.output(buf);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void accept0(SQLASTVisitor visitor) {
|
||||
if (visitor instanceof PGASTVisitor) {
|
||||
this.accept0((PGASTVisitor) visitor);
|
||||
} else {
|
||||
if (visitor.visit(this)) {
|
||||
acceptChild(visitor, this.arguments);
|
||||
acceptChild(visitor, this.over);
|
||||
}
|
||||
visitor.endVisit(this);
|
||||
}
|
||||
}
|
||||
|
||||
public void accept0(PGASTVisitor visitor) {
|
||||
if (visitor.visit(this)) {
|
||||
acceptChild(visitor, this.arguments);
|
||||
acceptChild(visitor, this.over);
|
||||
}
|
||||
visitor.endVisit(this);
|
||||
}
|
||||
}
|
||||
@@ -15,16 +15,14 @@
|
||||
*/
|
||||
package com.alibaba.druid.sql.dialect.postgresql.parser;
|
||||
|
||||
import com.alibaba.druid.sql.ast.expr.SQLAggregateExpr;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.PGAggregateExpr;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.PGOrderBy;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.expr.PGAnalytic;
|
||||
import com.alibaba.druid.sql.parser.Lexer;
|
||||
import com.alibaba.druid.sql.parser.SQLExprParser;
|
||||
import com.alibaba.druid.sql.parser.Token;
|
||||
|
||||
public class PGExprParser extends SQLExprParser{
|
||||
public PGExprParser(String sql) {
|
||||
public class PGExprParser extends SQLExprParser {
|
||||
|
||||
public PGExprParser(String sql){
|
||||
super(new PGLexer(sql));
|
||||
this.lexer.nextToken();
|
||||
}
|
||||
@@ -32,63 +30,11 @@ public class PGExprParser extends SQLExprParser{
|
||||
public PGExprParser(Lexer lexer){
|
||||
super(lexer);
|
||||
}
|
||||
|
||||
protected SQLAggregateExpr parseAggregateExpr(String methodName) {
|
||||
methodName = methodName.toUpperCase();
|
||||
|
||||
PGAggregateExpr aggregateExpr;
|
||||
if (lexer.token() == Token.ALL) {
|
||||
aggregateExpr = new PGAggregateExpr(methodName, SQLAggregateExpr.Option.ALL);
|
||||
lexer.nextToken();
|
||||
} else if (lexer.token() == Token.DISTINCT) {
|
||||
aggregateExpr = new PGAggregateExpr(methodName, SQLAggregateExpr.Option.DISTINCT);
|
||||
lexer.nextToken();
|
||||
} else {
|
||||
aggregateExpr = new PGAggregateExpr(methodName);
|
||||
}
|
||||
|
||||
exprList(aggregateExpr.getArguments());
|
||||
|
||||
accept(Token.RPAREN);
|
||||
|
||||
if (lexer.token() == Token.OVER) {
|
||||
lexer.nextToken();
|
||||
PGAnalytic over = new PGAnalytic();
|
||||
accept(Token.LPAREN);
|
||||
|
||||
if (identifierEquals("PARTITION")) {
|
||||
lexer.nextToken();
|
||||
accept(Token.BY);
|
||||
|
||||
if (lexer.token() == (Token.LPAREN)) {
|
||||
lexer.nextToken();
|
||||
exprList(over.getPartitionBy());
|
||||
accept(Token.RPAREN);
|
||||
} else {
|
||||
exprList(over.getPartitionBy());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
over.setOrderBy(parseOrderBy());
|
||||
|
||||
// if (over.getOrderBy() != null) {
|
||||
// //TODO window
|
||||
// }
|
||||
|
||||
accept(Token.RPAREN);
|
||||
aggregateExpr.setOver(over);
|
||||
|
||||
}
|
||||
|
||||
return aggregateExpr;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public PGOrderBy parseOrderBy() {
|
||||
if (lexer.token() == (Token.ORDER)) {
|
||||
PGOrderBy orderBy = new PGOrderBy();
|
||||
PGOrderBy orderBy = new PGOrderBy();
|
||||
lexer.nextToken();
|
||||
|
||||
if (identifierEquals("SIBLINGS")) {
|
||||
|
||||
@@ -47,7 +47,6 @@ public class PGLexer extends Lexer {
|
||||
|
||||
map.put("OFFSET", Token.OFFSET);
|
||||
map.put("ONLY", Token.ONLY);
|
||||
map.put("OVER", Token.OVER);
|
||||
map.put("RECURSIVE", Token.RECURSIVE);
|
||||
map.put("RESTART", Token.RESTART);
|
||||
|
||||
|
||||
@@ -15,7 +15,6 @@
|
||||
*/
|
||||
package com.alibaba.druid.sql.dialect.postgresql.visitor;
|
||||
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.PGAggregateExpr;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.PGWithClause;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.PGWithQuery;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.expr.PGAnalytic;
|
||||
@@ -75,10 +74,6 @@ public interface PGASTVisitor extends SQLASTVisitor {
|
||||
|
||||
boolean visit(PGUpdateStatement x);
|
||||
|
||||
void endVisit(PGAggregateExpr x);
|
||||
|
||||
boolean visit(PGAggregateExpr x);
|
||||
|
||||
void endVisit(PGAnalytic x);
|
||||
|
||||
boolean visit(PGAnalytic x);
|
||||
|
||||
-10
@@ -15,7 +15,6 @@
|
||||
*/
|
||||
package com.alibaba.druid.sql.dialect.postgresql.visitor;
|
||||
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.PGAggregateExpr;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.PGWithClause;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.PGWithQuery;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.expr.PGAnalytic;
|
||||
@@ -151,15 +150,6 @@ public class PGASTVisitorAdapter extends SQLASTVisitorAdapter implements PGASTVi
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(PGAggregateExpr x) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(PGAggregateExpr x) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(PGAnalytic x) {
|
||||
}
|
||||
|
||||
+2
-29
@@ -18,7 +18,6 @@ package com.alibaba.druid.sql.dialect.postgresql.visitor;
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.SQLSetQuantifier;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectStatement;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.PGAggregateExpr;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.PGWithClause;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.PGWithQuery;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.expr.PGAnalytic;
|
||||
@@ -440,32 +439,6 @@ public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(PGAggregateExpr x) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(PGAggregateExpr x) {
|
||||
print(x.getMethodName());
|
||||
|
||||
print("(");
|
||||
|
||||
if (x.getOption() != null) {
|
||||
print(x.getOption().toString());
|
||||
print(' ');
|
||||
}
|
||||
|
||||
printAndAccept(x.getArguments(), ", ");
|
||||
print(")");
|
||||
|
||||
if (x.getOver() != null) {
|
||||
x.getOver().accept(this);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(PGAnalytic x) {
|
||||
|
||||
@@ -513,7 +486,7 @@ public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor
|
||||
print(" AS ");
|
||||
print(x.getAlias());
|
||||
}
|
||||
|
||||
|
||||
if (x.getParameters().size() > 0) {
|
||||
print('(');
|
||||
printAndAccept(x.getParameters(), ", ");
|
||||
@@ -525,7 +498,7 @@ public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor
|
||||
|
||||
@Override
|
||||
public void endVisit(PGFunctionTableSource x) {
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
-11
@@ -22,7 +22,6 @@ import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLTruncateStatement;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.PGAggregateExpr;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.PGWithClause;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.PGWithQuery;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.expr.PGAnalytic;
|
||||
@@ -260,16 +259,6 @@ public class PGSchemaStatVisitor extends SchemaStatVisitor implements PGASTVisit
|
||||
return this.visit((SQLSelectQueryBlock) x);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(PGAggregateExpr x) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(PGAggregateExpr x) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(PGAnalytic x) {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user