mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
refactor sql parser.
This commit is contained in:
@@ -1,62 +0,0 @@
|
||||
/*
|
||||
* Copyright 1999-2017 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 java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.druid.sql.dialect.postgresql.visitor.PGASTVisitor;
|
||||
|
||||
public class PGWithClause extends PGSQLObjectImpl {
|
||||
|
||||
private boolean recursive = false;
|
||||
private List<PGWithQuery> withQuery = new ArrayList<PGWithQuery>(2);
|
||||
|
||||
public boolean isRecursive() {
|
||||
return recursive;
|
||||
}
|
||||
|
||||
public void setRecursive(boolean recursive) {
|
||||
this.recursive = recursive;
|
||||
}
|
||||
|
||||
public List<PGWithQuery> getWithQuery() {
|
||||
return withQuery;
|
||||
}
|
||||
|
||||
public void setWithQuery(List<PGWithQuery> withQuery) {
|
||||
this.withQuery = withQuery;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept0(PGASTVisitor visitor) {
|
||||
if (visitor.visit(this)) {
|
||||
acceptChild(visitor, withQuery);
|
||||
}
|
||||
visitor.endVisit(this);
|
||||
}
|
||||
|
||||
public PGWithClause clone() {
|
||||
PGWithClause x = new PGWithClause();
|
||||
x.recursive = recursive;
|
||||
for (PGWithQuery w : withQuery) {
|
||||
PGWithQuery w2 = w.clone();
|
||||
w2.setParent(x);
|
||||
x.withQuery.add(w2);
|
||||
}
|
||||
return x;
|
||||
}
|
||||
}
|
||||
@@ -1,85 +0,0 @@
|
||||
/*
|
||||
* Copyright 1999-2017 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 java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.visitor.PGASTVisitor;
|
||||
|
||||
public class PGWithQuery extends PGSQLObjectImpl {
|
||||
|
||||
private SQLExpr name;
|
||||
private final List<SQLExpr> columns = new ArrayList<SQLExpr>();
|
||||
private SQLStatement query;
|
||||
|
||||
public SQLExpr getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(SQLExpr name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public SQLStatement getQuery() {
|
||||
return query;
|
||||
}
|
||||
|
||||
public void setQuery(SQLStatement query) {
|
||||
this.query = query;
|
||||
}
|
||||
|
||||
public List<SQLExpr> getColumns() {
|
||||
return columns;
|
||||
}
|
||||
|
||||
public void addColumn(SQLExpr column) {
|
||||
if (column != null) {
|
||||
column.setParent(this);
|
||||
}
|
||||
this.columns.add(column);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept0(PGASTVisitor visitor) {
|
||||
if (visitor.visit(this)) {
|
||||
acceptChild(visitor, name);
|
||||
acceptChild(visitor, columns);
|
||||
acceptChild(visitor, query);
|
||||
}
|
||||
visitor.endVisit(this);
|
||||
}
|
||||
|
||||
public PGWithQuery clone() {
|
||||
PGWithQuery x = new PGWithQuery();
|
||||
|
||||
if (name != null) {
|
||||
x.setName(name.clone());
|
||||
}
|
||||
for (SQLExpr c : columns) {
|
||||
SQLExpr c2 = c.clone();
|
||||
c2.setParent(x);
|
||||
x.columns.add(c);
|
||||
}
|
||||
if (query != null) {
|
||||
x.setQuery(query.clone());
|
||||
}
|
||||
|
||||
return x;
|
||||
}
|
||||
}
|
||||
+4
-5
@@ -20,15 +20,14 @@ import java.util.List;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLName;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLDeleteStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlDeleteStatement;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.PGWithClause;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLWithSubqueryClause;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.visitor.PGASTVisitor;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
|
||||
public class PGDeleteStatement extends SQLDeleteStatement implements PGSQLStatement {
|
||||
|
||||
private PGWithClause with;
|
||||
private SQLWithSubqueryClause with;
|
||||
private List<SQLName> using = new ArrayList<SQLName>(2);
|
||||
private boolean returning;
|
||||
private String alias;
|
||||
@@ -63,11 +62,11 @@ public class PGDeleteStatement extends SQLDeleteStatement implements PGSQLStatem
|
||||
|
||||
|
||||
|
||||
public PGWithClause getWith() {
|
||||
public SQLWithSubqueryClause getWith() {
|
||||
return with;
|
||||
}
|
||||
|
||||
public void setWith(PGWithClause with) {
|
||||
public void setWith(SQLWithSubqueryClause with) {
|
||||
this.with = with;
|
||||
}
|
||||
|
||||
|
||||
+4
-4
@@ -20,13 +20,13 @@ import java.util.List;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLInsertStatement;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.PGWithClause;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLWithSubqueryClause;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.visitor.PGASTVisitor;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
|
||||
public class PGInsertStatement extends SQLInsertStatement implements PGSQLStatement {
|
||||
|
||||
private PGWithClause with;
|
||||
private SQLWithSubqueryClause with;
|
||||
private List<ValuesClause> valuesList = new ArrayList<ValuesClause>();
|
||||
private SQLExpr returning;
|
||||
private boolean defaultValues = false;
|
||||
@@ -55,11 +55,11 @@ public class PGInsertStatement extends SQLInsertStatement implements PGSQLStatem
|
||||
this.returning = returning;
|
||||
}
|
||||
|
||||
public PGWithClause getWith() {
|
||||
public SQLWithSubqueryClause getWith() {
|
||||
return with;
|
||||
}
|
||||
|
||||
public void setWith(PGWithClause with) {
|
||||
public void setWith(SQLWithSubqueryClause with) {
|
||||
this.with = with;
|
||||
}
|
||||
|
||||
|
||||
-12
@@ -19,18 +19,15 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.SQLObjectImpl;
|
||||
import com.alibaba.druid.sql.ast.SQLOrderBy;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.PGSQLObject;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.PGSQLObjectImpl;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.PGWithClause;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.visitor.PGASTVisitor;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
|
||||
public class PGSelectQueryBlock extends SQLSelectQueryBlock implements PGSQLObject{
|
||||
|
||||
private PGWithClause with;
|
||||
private List<SQLExpr> distinctOn = new ArrayList<SQLExpr>(2);
|
||||
private WindowClause window;
|
||||
|
||||
@@ -59,7 +56,6 @@ public class PGSelectQueryBlock extends SQLSelectQueryBlock implements PGSQLObje
|
||||
@Override
|
||||
public void accept0(PGASTVisitor visitor) {
|
||||
if (visitor.visit(this)) {
|
||||
acceptChild(visitor, this.with);
|
||||
acceptChild(visitor, this.distinctOn);
|
||||
acceptChild(visitor, this.selectList);
|
||||
acceptChild(visitor, this.into);
|
||||
@@ -99,14 +95,6 @@ public class PGSelectQueryBlock extends SQLSelectQueryBlock implements PGSQLObje
|
||||
this.window = window;
|
||||
}
|
||||
|
||||
public PGWithClause getWith() {
|
||||
return with;
|
||||
}
|
||||
|
||||
public void setWith(PGWithClause with) {
|
||||
this.with = with;
|
||||
}
|
||||
|
||||
public SQLOrderBy getOrderBy() {
|
||||
return orderBy;
|
||||
}
|
||||
|
||||
-12
@@ -17,15 +17,12 @@ package com.alibaba.druid.sql.dialect.postgresql.ast.stmt;
|
||||
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelect;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectStatement;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.PGWithClause;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.visitor.PGASTVisitor;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
|
||||
public class PGSelectStatement extends SQLSelectStatement implements PGSQLStatement {
|
||||
|
||||
private PGWithClause with;
|
||||
|
||||
public PGSelectStatement(){
|
||||
super(JdbcConstants.POSTGRESQL);
|
||||
}
|
||||
@@ -34,21 +31,12 @@ public class PGSelectStatement extends SQLSelectStatement implements PGSQLStatem
|
||||
super(select, JdbcConstants.POSTGRESQL);
|
||||
}
|
||||
|
||||
public PGWithClause getWith() {
|
||||
return with;
|
||||
}
|
||||
|
||||
public void setWith(PGWithClause with) {
|
||||
this.with = with;
|
||||
}
|
||||
|
||||
protected void accept0(SQLASTVisitor visitor) {
|
||||
accept0((PGASTVisitor) visitor);
|
||||
}
|
||||
|
||||
public void accept0(PGASTVisitor visitor) {
|
||||
if (visitor.visit(this)) {
|
||||
acceptChild(visitor, this.with);
|
||||
acceptChild(visitor, this.select);
|
||||
}
|
||||
visitor.endVisit(this);
|
||||
|
||||
+4
-9
@@ -15,13 +15,8 @@
|
||||
*/
|
||||
package com.alibaba.druid.sql.dialect.postgresql.ast.stmt;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLTableSource;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLUpdateStatement;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.PGWithClause;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLWithSubqueryClause;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.visitor.PGASTVisitor;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
@@ -30,7 +25,7 @@ public class PGUpdateStatement extends SQLUpdateStatement implements PGSQLStatem
|
||||
|
||||
private boolean only = false;
|
||||
|
||||
private PGWithClause with;
|
||||
private SQLWithSubqueryClause with;
|
||||
|
||||
public PGUpdateStatement(){
|
||||
super (JdbcConstants.POSTGRESQL);
|
||||
@@ -44,11 +39,11 @@ public class PGUpdateStatement extends SQLUpdateStatement implements PGSQLStatem
|
||||
this.only = only;
|
||||
}
|
||||
|
||||
public PGWithClause getWith() {
|
||||
public SQLWithSubqueryClause getWith() {
|
||||
return with;
|
||||
}
|
||||
|
||||
public void setWith(PGWithClause with) {
|
||||
public void setWith(SQLWithSubqueryClause with) {
|
||||
this.with = with;
|
||||
}
|
||||
|
||||
|
||||
@@ -39,6 +39,7 @@ import com.alibaba.druid.sql.dialect.postgresql.ast.expr.PGPolygonExpr;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.expr.PGTypeCastExpr;
|
||||
import com.alibaba.druid.sql.parser.Lexer;
|
||||
import com.alibaba.druid.sql.parser.SQLExprParser;
|
||||
import com.alibaba.druid.sql.parser.SQLParserFeature;
|
||||
import com.alibaba.druid.sql.parser.Token;
|
||||
import com.alibaba.druid.util.FNVUtils;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
@@ -68,6 +69,12 @@ public class PGExprParser extends SQLExprParser {
|
||||
this.dbType = JdbcConstants.POSTGRESQL;
|
||||
}
|
||||
|
||||
public PGExprParser(String sql, SQLParserFeature... features){
|
||||
this(new PGLexer(sql));
|
||||
this.lexer.nextToken();
|
||||
this.dbType = JdbcConstants.POSTGRESQL;
|
||||
}
|
||||
|
||||
public PGExprParser(Lexer lexer){
|
||||
super(lexer);
|
||||
this.aggregateFunctions = AGGREGATE_FUNCTIONS;
|
||||
|
||||
@@ -22,6 +22,7 @@ import java.util.Map;
|
||||
|
||||
import com.alibaba.druid.sql.parser.Keywords;
|
||||
import com.alibaba.druid.sql.parser.Lexer;
|
||||
import com.alibaba.druid.sql.parser.SQLParserFeature;
|
||||
import com.alibaba.druid.sql.parser.Token;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
|
||||
@@ -73,10 +74,13 @@ public class PGLexer extends Lexer {
|
||||
DEFAULT_PG_KEYWORDS = new Keywords(map);
|
||||
}
|
||||
|
||||
public PGLexer(String input){
|
||||
public PGLexer(String input, SQLParserFeature... features){
|
||||
super(input);
|
||||
super.keywods = DEFAULT_PG_KEYWORDS;
|
||||
super.dbType = JdbcConstants.POSTGRESQL;
|
||||
for (SQLParserFeature feature : features) {
|
||||
config(feature, true);
|
||||
}
|
||||
}
|
||||
|
||||
protected void scanString() {
|
||||
|
||||
+9
-84
@@ -23,8 +23,6 @@ import com.alibaba.druid.sql.ast.expr.SQLCurrentOfCursorExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLQueryExpr;
|
||||
import com.alibaba.druid.sql.ast.statement.*;
|
||||
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.stmt.PGDeleteStatement;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGInsertStatement;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGSelectStatement;
|
||||
@@ -53,6 +51,10 @@ public class PGSQLStatementParser extends SQLStatementParser {
|
||||
super(new PGExprParser(sql));
|
||||
}
|
||||
|
||||
public PGSQLStatementParser(String sql, SQLParserFeature... features){
|
||||
super(new PGExprParser(sql, features));
|
||||
}
|
||||
|
||||
public PGSQLStatementParser(Lexer lexer){
|
||||
super(new PGExprParser(lexer));
|
||||
}
|
||||
@@ -236,86 +238,6 @@ public class PGSQLStatementParser extends SQLStatementParser {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public PGWithClause parseWithClause() {
|
||||
lexer.nextToken();
|
||||
|
||||
PGWithClause withClause = new PGWithClause();
|
||||
|
||||
if (lexer.token() == Token.RECURSIVE) {
|
||||
lexer.nextToken();
|
||||
withClause.setRecursive(true);
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
PGWithQuery withQuery = withQuery();
|
||||
withClause.getWithQuery().add(withQuery);
|
||||
if (lexer.token() == Token.COMMA) {
|
||||
lexer.nextToken();
|
||||
continue;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return withClause;
|
||||
}
|
||||
|
||||
private PGWithQuery withQuery() {
|
||||
PGWithQuery withQuery = new PGWithQuery();
|
||||
|
||||
if (lexer.token() == Token.LITERAL_ALIAS) {
|
||||
withQuery.setName(new SQLIdentifierExpr("\"" + lexer.stringVal()
|
||||
+ "\""));
|
||||
} else {
|
||||
withQuery.setName(new SQLIdentifierExpr(lexer.stringVal()));
|
||||
}
|
||||
lexer.nextToken();
|
||||
|
||||
if (lexer.token() == Token.LPAREN) {
|
||||
lexer.nextToken();
|
||||
|
||||
for (;;) {
|
||||
SQLExpr expr = this.exprParser.expr();
|
||||
withQuery.addColumn(expr);
|
||||
if (lexer.token() == Token.COMMA) {
|
||||
lexer.nextToken();
|
||||
continue;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
accept(Token.RPAREN);
|
||||
}
|
||||
|
||||
accept(Token.AS);
|
||||
|
||||
if (lexer.token() == Token.LPAREN) {
|
||||
lexer.nextToken();
|
||||
|
||||
SQLStatement query;
|
||||
if (lexer.token() == Token.SELECT) {
|
||||
query = this.parseSelect();
|
||||
} else if (lexer.token() == Token.INSERT) {
|
||||
query = this.parseInsert();
|
||||
} else if (lexer.token() == Token.UPDATE) {
|
||||
query = this.parseUpdateStatement();
|
||||
} else if (lexer.token() == Token.DELETE) {
|
||||
query = this.parseDeleteStatement();
|
||||
} else if (lexer.token() == Token.VALUES) {
|
||||
query = this.parseSelect();
|
||||
} else {
|
||||
throw new ParserException("syntax error, support token '" + lexer.token() + "', " + lexer.info());
|
||||
}
|
||||
withQuery.setQuery(query);
|
||||
|
||||
accept(Token.RPAREN);
|
||||
}
|
||||
|
||||
return withQuery;
|
||||
}
|
||||
|
||||
public PGSelectStatement parseSelect() {
|
||||
PGSelectParser selectParser = createSQLSelectParser();
|
||||
SQLSelect select = selectParser.select();
|
||||
@@ -323,7 +245,8 @@ public class PGSQLStatementParser extends SQLStatementParser {
|
||||
}
|
||||
|
||||
public SQLStatement parseWith() {
|
||||
PGWithClause with = this.parseWithClause();
|
||||
SQLWithSubqueryClause with = this.parseWithQuery();
|
||||
// PGWithClause with = this.parseWithClause();
|
||||
if (lexer.token() == Token.INSERT) {
|
||||
PGInsertStatement stmt = this.parseInsert();
|
||||
stmt.setWith(with);
|
||||
@@ -332,7 +255,7 @@ public class PGSQLStatementParser extends SQLStatementParser {
|
||||
|
||||
if (lexer.token() == Token.SELECT) {
|
||||
PGSelectStatement stmt = this.parseSelect();
|
||||
stmt.setWith(with);
|
||||
stmt.getSelect().setWithSubQuery(with);
|
||||
return stmt;
|
||||
}
|
||||
|
||||
@@ -347,6 +270,7 @@ public class PGSQLStatementParser extends SQLStatementParser {
|
||||
stmt.setWith(with);
|
||||
return stmt;
|
||||
}
|
||||
|
||||
throw new ParserException("TODO. " + lexer.info());
|
||||
}
|
||||
|
||||
@@ -463,4 +387,5 @@ public class PGSQLStatementParser extends SQLStatementParser {
|
||||
return new PGSetStatement(range, parameter, values);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -15,8 +15,6 @@
|
||||
*/
|
||||
package com.alibaba.druid.sql.dialect.postgresql.visitor;
|
||||
|
||||
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.*;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.stmt.*;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
@@ -39,14 +37,6 @@ public interface PGASTVisitor extends SQLASTVisitor {
|
||||
|
||||
boolean visit(PGSelectQueryBlock.ForClause x);
|
||||
|
||||
void endVisit(PGWithQuery x);
|
||||
|
||||
boolean visit(PGWithQuery x);
|
||||
|
||||
void endVisit(PGWithClause x);
|
||||
|
||||
boolean visit(PGWithClause x);
|
||||
|
||||
void endVisit(PGDeleteStatement x);
|
||||
|
||||
boolean visit(PGDeleteStatement x);
|
||||
|
||||
-23
@@ -15,8 +15,6 @@
|
||||
*/
|
||||
package com.alibaba.druid.sql.dialect.postgresql.visitor;
|
||||
|
||||
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.PGBoxExpr;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.expr.PGCidrExpr;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.expr.PGCircleExpr;
|
||||
@@ -69,27 +67,6 @@ public class PGASTVisitorAdapter extends SQLASTVisitorAdapter implements PGASTVi
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(PGWithQuery x) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(PGWithQuery x) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(PGWithClause x) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(PGWithClause x) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(PGDeleteStatement x) {
|
||||
|
||||
|
||||
+23
-73
@@ -15,13 +15,11 @@
|
||||
*/
|
||||
package com.alibaba.druid.sql.dialect.postgresql.visitor;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
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;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLTruncateStatement;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.PGWithClause;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.PGWithQuery;
|
||||
import com.alibaba.druid.sql.ast.statement.*;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.expr.PGBoxExpr;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.expr.PGCidrExpr;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.expr.PGCircleExpr;
|
||||
@@ -42,6 +40,8 @@ import com.alibaba.druid.sql.parser.Token;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTOutputVisitor;
|
||||
import com.alibaba.druid.util.StringUtils;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor {
|
||||
|
||||
public PGOutputVisitor(Appendable appender){
|
||||
@@ -121,58 +121,8 @@ public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(PGWithQuery x) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(PGWithQuery x) {
|
||||
x.getName().accept(this);
|
||||
|
||||
if (x.getColumns().size() > 0) {
|
||||
print0(" (");
|
||||
printAndAccept(x.getColumns(), ", ");
|
||||
print(')');
|
||||
}
|
||||
println();
|
||||
print0(ucase ? "AS" : "as");
|
||||
println();
|
||||
print('(');
|
||||
incrementIndent();
|
||||
println();
|
||||
x.getQuery().accept(this);
|
||||
decrementIndent();
|
||||
println();
|
||||
print(')');
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(PGWithClause x) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(PGWithClause x) {
|
||||
print0(ucase ? "WITH" : "with");
|
||||
if (x.isRecursive()) {
|
||||
print0(ucase ? " RECURSIVE " : " recursive ");
|
||||
}
|
||||
incrementIndent();
|
||||
println();
|
||||
printlnAndAccept(x.getWithQuery(), ", ");
|
||||
decrementIndent();
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean visit(PGSelectQueryBlock x) {
|
||||
if (x.getWith() != null) {
|
||||
x.getWith().accept(this);
|
||||
println();
|
||||
}
|
||||
|
||||
print0(ucase ? "SELECT " : "select ");
|
||||
|
||||
if (SQLSetQuantifier.ALL == x.getDistionOption()) {
|
||||
@@ -305,9 +255,9 @@ public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor
|
||||
if (x.getWhere() != null) {
|
||||
println();
|
||||
print0(ucase ? "WHERE " : "where ");
|
||||
incrementIndent();
|
||||
this.indentCount++;
|
||||
x.getWhere().accept(this);
|
||||
decrementIndent();
|
||||
this.indentCount--;
|
||||
}
|
||||
|
||||
if (x.isReturning()) {
|
||||
@@ -363,11 +313,6 @@ public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor
|
||||
|
||||
@Override
|
||||
public boolean visit(PGSelectStatement x) {
|
||||
if (x.getWith() != null) {
|
||||
x.getWith().accept(this);
|
||||
println();
|
||||
}
|
||||
|
||||
return visit((SQLSelectStatement) x);
|
||||
}
|
||||
|
||||
@@ -378,8 +323,9 @@ public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor
|
||||
|
||||
@Override
|
||||
public boolean visit(PGUpdateStatement x) {
|
||||
if (x.getWith() != null) {
|
||||
x.getWith().accept(this);
|
||||
SQLWithSubqueryClause with = x.getWith();
|
||||
if (with != null) {
|
||||
visit(with);
|
||||
println();
|
||||
}
|
||||
|
||||
@@ -389,7 +335,7 @@ public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor
|
||||
print0(ucase ? "ONLY " : "only ");
|
||||
}
|
||||
|
||||
x.getTableSource().accept(this);
|
||||
printTableSource(x.getTableSource());
|
||||
|
||||
println();
|
||||
print0(ucase ? "SET " : "set ");
|
||||
@@ -397,27 +343,31 @@ public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor
|
||||
if (i != 0) {
|
||||
print0(", ");
|
||||
}
|
||||
x.getItems().get(i).accept(this);
|
||||
SQLUpdateSetItem item = x.getItems().get(i);
|
||||
visit(item);
|
||||
}
|
||||
|
||||
if (x.getFrom() != null) {
|
||||
SQLTableSource from = x.getFrom();
|
||||
if (from != null) {
|
||||
println();
|
||||
print0(ucase ? "FROM " : "from ");
|
||||
x.getFrom().accept(this);
|
||||
printTableSource(from);
|
||||
}
|
||||
|
||||
if (x.getWhere() != null) {
|
||||
SQLExpr where = x.getWhere();
|
||||
if (where != null) {
|
||||
println();
|
||||
indentCount++;
|
||||
print0(ucase ? "WHERE " : "where ");
|
||||
incrementIndent();
|
||||
x.getWhere().accept(this);
|
||||
decrementIndent();
|
||||
printExpr(where);
|
||||
indentCount--;
|
||||
}
|
||||
|
||||
if (x.getReturning().size() > 0) {
|
||||
List<SQLExpr> returning = x.getReturning();
|
||||
if (returning.size() > 0) {
|
||||
println();
|
||||
print0(ucase ? "RETURNING " : "returning ");
|
||||
printAndAccept(x.getReturning(), ", ");
|
||||
printAndAccept(returning, ", ");
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
+8
-42
@@ -21,9 +21,6 @@ import com.alibaba.druid.sql.ast.SQLName;
|
||||
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.dialect.mysql.ast.statement.MySqlSelectQueryBlock;
|
||||
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.PGBoxExpr;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.expr.PGCidrExpr;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.expr.PGCircleExpr;
|
||||
@@ -42,9 +39,13 @@ import com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGSelectQueryBlock.Wind
|
||||
import com.alibaba.druid.sql.visitor.SchemaStatVisitor;
|
||||
import com.alibaba.druid.stat.TableStat;
|
||||
import com.alibaba.druid.stat.TableStat.Mode;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
import com.alibaba.druid.util.JdbcUtils;
|
||||
|
||||
public class PGSchemaStatVisitor extends SchemaStatVisitor implements PGASTVisitor {
|
||||
public PGSchemaStatVisitor() {
|
||||
super(JdbcConstants.POSTGRESQL);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDbType() {
|
||||
@@ -82,41 +83,6 @@ public class PGSchemaStatVisitor extends SchemaStatVisitor implements PGASTVisit
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(PGWithQuery x) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(PGWithQuery x) {
|
||||
x.getQuery().accept(this);
|
||||
|
||||
Map<String, String> aliasMap = getAliasMap();
|
||||
if (aliasMap != null) {
|
||||
String alias = null;
|
||||
if (x.getName() != null) {
|
||||
alias = x.getName().toString();
|
||||
}
|
||||
|
||||
if (alias != null) {
|
||||
aliasMap.put(alias, null);
|
||||
addSubQuery(alias, x.getQuery());
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(PGWithClause x) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(PGWithClause x) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(PGDeleteStatement x) {
|
||||
|
||||
@@ -124,6 +90,10 @@ public class PGSchemaStatVisitor extends SchemaStatVisitor implements PGASTVisit
|
||||
|
||||
@Override
|
||||
public boolean visit(PGDeleteStatement x) {
|
||||
if (repository != null) {
|
||||
repository.resolve(x);
|
||||
}
|
||||
|
||||
if (x.getWith() != null) {
|
||||
x.getWith().accept(this);
|
||||
}
|
||||
@@ -204,10 +174,6 @@ public class PGSchemaStatVisitor extends SchemaStatVisitor implements PGASTVisit
|
||||
|
||||
@Override
|
||||
public boolean visit(PGSelectStatement x) {
|
||||
if (x.getWith() != null) {
|
||||
x.getWith().accept(this);
|
||||
}
|
||||
|
||||
return visit((SQLSelectStatement) x);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user