mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improved sql parser.
This commit is contained in:
@@ -1,43 +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.stmt;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLStatementImpl;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.visitor.PGASTVisitor;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
|
||||
public class PGBeginStatement extends SQLStatementImpl implements PGSQLStatement {
|
||||
public final boolean isBegin;
|
||||
|
||||
public PGBeginStatement(boolean isBegin) {
|
||||
super(JdbcConstants.POSTGRESQL);
|
||||
this.isBegin = isBegin;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void accept0(SQLASTVisitor visitor) {
|
||||
if (visitor instanceof PGASTVisitor) {
|
||||
accept0((PGASTVisitor) visitor);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept0(PGASTVisitor visitor) {
|
||||
visitor.visit(this);
|
||||
visitor.endVisit(this);
|
||||
}
|
||||
}
|
||||
+2
-5
@@ -20,11 +20,8 @@ import com.alibaba.druid.sql.dialect.postgresql.visitor.PGASTVisitor;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
|
||||
// 暂时不支持END命令
|
||||
public class PGCommitStatement extends SQLStatementImpl implements PGSQLStatement {
|
||||
|
||||
// COMMIT
|
||||
public PGCommitStatement() {
|
||||
public class PGStartTransactionStatement extends SQLStatementImpl implements PGSQLStatement {
|
||||
public PGStartTransactionStatement() {
|
||||
super(JdbcConstants.POSTGRESQL);
|
||||
}
|
||||
|
||||
+11
-23
@@ -39,14 +39,12 @@ import com.alibaba.druid.sql.parser.Token;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
public class PGSQLStatementParser extends SQLStatementParser {
|
||||
public static final String TIME_ZONE = "TIME ZONE";
|
||||
public static final String TIME = "TIME";
|
||||
public static final String LOCAL = "LOCAL";
|
||||
public static final String TRANSACTION = "TRANSACTION";
|
||||
|
||||
|
||||
public PGSQLStatementParser(PGExprParser parser) {
|
||||
super(parser);
|
||||
}
|
||||
@@ -222,11 +220,14 @@ public class PGSQLStatementParser extends SQLStatementParser {
|
||||
|
||||
public boolean parseStatementListDialect(List<SQLStatement> statementList) {
|
||||
switch (lexer.token()) {
|
||||
case BEGIN:
|
||||
statementList.add(handleBegin());
|
||||
case START: {
|
||||
lexer.nextToken();
|
||||
acceptIdentifier("TRANSACTION");
|
||||
PGStartTransactionStatement stmt = new PGStartTransactionStatement();
|
||||
statementList.add(stmt);
|
||||
lexer.nextToken();
|
||||
return true;
|
||||
case START:
|
||||
return handleStart(statementList);
|
||||
}
|
||||
case WITH:
|
||||
statementList.add(parseWith());
|
||||
return true;
|
||||
@@ -235,21 +236,7 @@ public class PGSQLStatementParser extends SQLStatementParser {
|
||||
}
|
||||
}
|
||||
|
||||
private SQLStatement handleBegin() {
|
||||
SQLStatement statement = new PGBeginStatement(true);
|
||||
lexer.nextToken();
|
||||
return statement;
|
||||
}
|
||||
|
||||
private boolean handleStart(List<SQLStatement> statementList) {
|
||||
lexer.nextToken();
|
||||
if (lexer.token() == Token.IDENTIFIER && TRANSACTION.equalsIgnoreCase(lexer.stringVal())) {
|
||||
statementList.add(new PGBeginStatement(false));
|
||||
lexer.nextToken();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public PGWithClause parseWithClause() {
|
||||
lexer.nextToken();
|
||||
@@ -417,9 +404,10 @@ public class PGSQLStatementParser extends SQLStatementParser {
|
||||
|
||||
@Override
|
||||
public SQLStatement parseCommit() {
|
||||
SQLStatement statment = new PGCommitStatement();
|
||||
SQLCommitStatement stmt = new SQLCommitStatement();
|
||||
stmt.setDbType(this.dbType);
|
||||
lexer.nextToken();
|
||||
return statment;
|
||||
return stmt;
|
||||
}
|
||||
|
||||
private SQLStatement handleTimeZoneSet(String range) {
|
||||
|
||||
@@ -119,13 +119,9 @@ public interface PGASTVisitor extends SQLASTVisitor {
|
||||
|
||||
boolean visit(PGShowStatement x);
|
||||
|
||||
void endVisit(PGBeginStatement x);
|
||||
void endVisit(PGStartTransactionStatement x);
|
||||
|
||||
boolean visit(PGBeginStatement x);
|
||||
|
||||
void endVisit(PGCommitStatement x);
|
||||
|
||||
boolean visit(PGCommitStatement x);
|
||||
boolean visit(PGStartTransactionStatement x);
|
||||
|
||||
void endVisit(PGSetStatement x);
|
||||
|
||||
|
||||
+2
-12
@@ -282,22 +282,12 @@ public class PGASTVisitorAdapter extends SQLASTVisitorAdapter implements PGASTVi
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(PGBeginStatement x) {
|
||||
public void endVisit(PGStartTransactionStatement x) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(PGBeginStatement x) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(PGCommitStatement x) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(PGCommitStatement x) {
|
||||
public boolean visit(PGStartTransactionStatement x) {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
+3
-14
@@ -637,24 +637,13 @@ public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(PGBeginStatement x) {
|
||||
public void endVisit(PGStartTransactionStatement x) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(PGBeginStatement x) {
|
||||
print0(x.isBegin ? "BEGIN" : "START TRANSACTION");
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(PGCommitStatement x) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(PGCommitStatement x) {
|
||||
print0(ucase ? "COMMIT" : "commit");
|
||||
public boolean visit(PGStartTransactionStatement x) {
|
||||
print0(ucase ? "START TRANSACTION" : "start transaction");
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
+2
-12
@@ -396,22 +396,12 @@ public class PGSchemaStatVisitor extends SchemaStatVisitor implements PGASTVisit
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(PGBeginStatement x) {
|
||||
public void endVisit(PGStartTransactionStatement x) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(PGBeginStatement x) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(PGCommitStatement x) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(PGCommitStatement x) {
|
||||
public boolean visit(PGStartTransactionStatement x) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user