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:
@@ -19,8 +19,10 @@ import com.alibaba.druid.sql.ast.SQLDataType;
|
||||
import com.alibaba.druid.sql.ast.SQLDataTypeImpl;
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.*;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLCharacterDataType;
|
||||
import com.alibaba.druid.sql.ast.statement.*;
|
||||
import com.alibaba.druid.sql.dialect.oracle.ast.stmt.*;
|
||||
import com.alibaba.druid.util.FnvHash;
|
||||
import oracle.jdbc.rowset.OracleJoinable;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -629,8 +631,58 @@ public class SQLTransformUtils {
|
||||
long nameHashCode64 = x.methodNameHashCode64();
|
||||
if (nameHashCode64 == FnvHash.Constants.SYS_GUID) {
|
||||
SQLMethodInvokeExpr uuid_generate_v4 = new SQLMethodInvokeExpr("uuid_generate_v4");
|
||||
|
||||
uuid_generate_v4.setParent(x.getParent());
|
||||
return uuid_generate_v4;
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
public static SQLTableSource transformOracleToPostgresql(SQLTableSource x) {
|
||||
if (x instanceof OracleSelectTableReference) {
|
||||
OracleSelectTableReference xx = (OracleSelectTableReference) x;
|
||||
SQLExprTableSource y = new SQLExprTableSource();
|
||||
xx.cloneTo(y);
|
||||
|
||||
y.setParent(x.getParent());
|
||||
return y;
|
||||
}
|
||||
|
||||
if (x instanceof OracleSelectJoin) {
|
||||
OracleSelectJoin xx = (OracleSelectJoin) x;
|
||||
SQLJoinTableSource y = new SQLJoinTableSource();
|
||||
xx.cloneTo(y);
|
||||
|
||||
y.setLeft(transformOracleToPostgresql(y.getLeft()));
|
||||
y.setRight(transformOracleToPostgresql(y.getRight()));
|
||||
|
||||
y.setParent(x.getParent());
|
||||
return y;
|
||||
}
|
||||
|
||||
if (x instanceof OracleSelectSubqueryTableSource) {
|
||||
OracleSelectSubqueryTableSource xx = (OracleSelectSubqueryTableSource) x;
|
||||
SQLSubqueryTableSource y = new SQLSubqueryTableSource();
|
||||
xx.cloneTo(y);
|
||||
|
||||
y.setParent(x.getParent());
|
||||
return y;
|
||||
}
|
||||
|
||||
return x;
|
||||
}
|
||||
|
||||
public static SQLSelectQueryBlock transformOracleToPostgresql(SQLSelectQueryBlock x) {
|
||||
if (x instanceof OracleSelectQueryBlock) {
|
||||
OracleSelectQueryBlock xx = (OracleSelectQueryBlock) x;
|
||||
SQLSelectQueryBlock y = new SQLSelectQueryBlock();
|
||||
xx.cloneTo(y);
|
||||
y.setFrom(transformOracleToPostgresql(y.getFrom()));
|
||||
|
||||
y.setParent(x.getParent());
|
||||
return y;
|
||||
}
|
||||
|
||||
return x;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
* 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.ast.statement;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.SQLName;
|
||||
import com.alibaba.druid.sql.ast.SQLStatementImpl;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
|
||||
public class SQLCreateUserStatement extends SQLStatementImpl {
|
||||
private SQLName user;
|
||||
private SQLExpr password;
|
||||
|
||||
// oracle
|
||||
private SQLName defaultTableSpace;
|
||||
|
||||
public SQLCreateUserStatement() {
|
||||
|
||||
}
|
||||
|
||||
public SQLName getUser() {
|
||||
return user;
|
||||
}
|
||||
|
||||
public void setUser(SQLName user) {
|
||||
if (user != null) {
|
||||
user.setParent(this);
|
||||
}
|
||||
this.user = user;
|
||||
}
|
||||
|
||||
public SQLExpr getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(SQLExpr password) {
|
||||
if (password != null) {
|
||||
password.setParent(this);
|
||||
}
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void accept0(SQLASTVisitor visitor) {
|
||||
if (visitor.visit(this)) {
|
||||
acceptChild(visitor, user);
|
||||
acceptChild(visitor, password);
|
||||
}
|
||||
visitor.endVisit(this);
|
||||
}
|
||||
}
|
||||
@@ -129,6 +129,7 @@ public class SQLSetStatement extends SQLStatementImpl {
|
||||
public static enum Option {
|
||||
IDENTITY_INSERT,
|
||||
PASSWORD, // mysql
|
||||
GLOBAL,
|
||||
SESSION,
|
||||
LOCAL
|
||||
}
|
||||
|
||||
@@ -67,14 +67,17 @@ public class SQLSubqueryTableSource extends SQLTableSourceImpl {
|
||||
buf.append(")");
|
||||
}
|
||||
|
||||
public SQLSubqueryTableSource clone() {
|
||||
SQLSubqueryTableSource x = new SQLSubqueryTableSource();
|
||||
|
||||
public void cloneTo(SQLSubqueryTableSource x) {
|
||||
x.alias = alias;
|
||||
|
||||
if (select != null) {
|
||||
x.select = select.clone();
|
||||
}
|
||||
}
|
||||
|
||||
public SQLSubqueryTableSource clone() {
|
||||
SQLSubqueryTableSource x = new SQLSubqueryTableSource();
|
||||
cloneTo(x);
|
||||
return x;
|
||||
}
|
||||
|
||||
|
||||
+11
@@ -68,4 +68,15 @@ public class OracleSelectSubqueryTableSource extends SQLSubqueryTableSource impl
|
||||
public String toString () {
|
||||
return SQLUtils.toOracleString(this);
|
||||
}
|
||||
|
||||
public OracleSelectSubqueryTableSource clone() {
|
||||
OracleSelectSubqueryTableSource x = new OracleSelectSubqueryTableSource();
|
||||
cloneTo(x);
|
||||
|
||||
if (pivot != null) {
|
||||
setParent(pivot.clone());
|
||||
}
|
||||
|
||||
return x;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@ import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLListExpr;
|
||||
import com.alibaba.druid.sql.ast.statement.*;
|
||||
import com.alibaba.druid.sql.dialect.oracle.ast.expr.OracleSysdateExpr;
|
||||
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;
|
||||
@@ -44,7 +45,10 @@ import com.alibaba.druid.sql.visitor.SQLASTOutputVisitor;
|
||||
import com.alibaba.druid.util.FnvHash;
|
||||
import com.alibaba.druid.util.StringUtils;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor {
|
||||
|
||||
@@ -646,4 +650,80 @@ public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLCreateUserStatement x) {
|
||||
print0(ucase ? "CREATE USER " : "create user ");
|
||||
x.getUser().accept(this);
|
||||
print0(ucase ? " PASSWORD " : " password ");
|
||||
|
||||
SQLExpr passoword = x.getPassword();
|
||||
|
||||
if (passoword instanceof SQLIdentifierExpr) {
|
||||
print('\'');
|
||||
passoword.accept(this);
|
||||
print('\'');
|
||||
} else {
|
||||
passoword.accept(this);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
protected void printGrantPrivileges(SQLGrantStatement x) {
|
||||
List<SQLExpr> privileges = x.getPrivileges();
|
||||
int i = 0;
|
||||
for (SQLExpr privilege : privileges) {
|
||||
if (i != 0) {
|
||||
print(", ");
|
||||
}
|
||||
|
||||
if (privilege instanceof SQLIdentifierExpr) {
|
||||
String name = ((SQLIdentifierExpr) privilege).getName();
|
||||
if ("RESOURCE".equalsIgnoreCase(name)) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
privilege.accept(this);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean visit(SQLGrantStatement x) {
|
||||
if (x.getOn() == null) {
|
||||
print("ALTER ROLE ");
|
||||
x.getTo().accept(this);
|
||||
print(' ');
|
||||
Set<SQLIdentifierExpr> pgPrivilegs = new LinkedHashSet<SQLIdentifierExpr>();
|
||||
for (SQLExpr privilege : x.getPrivileges()) {
|
||||
if (privilege instanceof SQLIdentifierExpr) {
|
||||
String name = ((SQLIdentifierExpr) privilege).getName();
|
||||
if (name.equalsIgnoreCase("CONNECT")) {
|
||||
pgPrivilegs.add(new SQLIdentifierExpr("LOGIN"));
|
||||
}
|
||||
if (name.toLowerCase().startsWith("create ")) {
|
||||
pgPrivilegs.add(new SQLIdentifierExpr("CREATEDB"));
|
||||
}
|
||||
}
|
||||
}
|
||||
int i = 0;
|
||||
for (SQLExpr privilege : pgPrivilegs) {
|
||||
if (i != 0) {
|
||||
print(' ');
|
||||
}
|
||||
privilege.accept(this);
|
||||
i++;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
return super.visit(x);
|
||||
}
|
||||
|
||||
// for oracle to postsql
|
||||
public boolean visit(OracleSysdateExpr x) {
|
||||
print0(ucase ? "CURRENT_TIMESTAMP" : "CURRENT_TIMESTAMP");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
+19
@@ -600,4 +600,23 @@ public class SQLServerOutputVisitor extends SQLASTOutputVisitor implements SQLSe
|
||||
print0(ucase ? "GO" : "go");
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLCreateUserStatement x) {
|
||||
print0(ucase ? "CREATE USER " : "create user ");
|
||||
x.getUser().accept(this);
|
||||
print0(ucase ? " WITH PASSWORD = " : " with password = ");
|
||||
|
||||
SQLExpr passoword = x.getPassword();
|
||||
|
||||
if (passoword instanceof SQLIdentifierExpr) {
|
||||
print('\'');
|
||||
passoword.accept(this);
|
||||
print('\'');
|
||||
} else {
|
||||
passoword.accept(this);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -700,6 +700,12 @@ public class SQLStatementParser extends SQLParser {
|
||||
} else if (lexer.token == Token.VIEW) {
|
||||
privilege = "CREATE VIEW";
|
||||
lexer.nextToken();
|
||||
} else if (lexer.token == Token.PROCEDURE) {
|
||||
privilege = "CREATE PROCEDURE";
|
||||
lexer.nextToken();
|
||||
} else if (lexer.token == Token.SEQUENCE) {
|
||||
privilege = "CREATE SEQUENCE";
|
||||
lexer.nextToken();
|
||||
} else if (lexer.token == Token.ANY) {
|
||||
lexer.nextToken();
|
||||
|
||||
@@ -816,6 +822,12 @@ public class SQLStatementParser extends SQLParser {
|
||||
} else if (lexer.identifierEquals("RELOAD")) {
|
||||
lexer.nextToken();
|
||||
privilege = "RELOAD";
|
||||
} else if (lexer.identifierEquals("RESOURCE")) {
|
||||
lexer.nextToken();
|
||||
privilege = "RESOURCE";
|
||||
} else if (lexer.token == Token.CONNECT) {
|
||||
lexer.nextToken();
|
||||
privilege = "CONNECT";
|
||||
} else if (lexer.identifierEquals("REPLICATION")) {
|
||||
lexer.nextToken();
|
||||
if (lexer.identifierEquals("SLAVE")) {
|
||||
@@ -1862,7 +1874,7 @@ public class SQLStatementParser extends SQLParser {
|
||||
return parseCreateTrigger();
|
||||
}
|
||||
|
||||
if (lexer.identifierEquals("PACKAGE")) {
|
||||
if (lexer.identifierEquals(FnvHash.Constants.PACKAGE)) {
|
||||
lexer.reset(markBp, markChar, Token.CREATE);
|
||||
return parseCreatePackage();
|
||||
}
|
||||
@@ -1878,7 +1890,10 @@ public class SQLStatementParser extends SQLParser {
|
||||
|
||||
lexer.reset(markBp, markChar, Token.CREATE);
|
||||
return parseCreateDatabase();
|
||||
} else if (lexer.identifierEquals("PUBLIC")) {
|
||||
} else if (lexer.token == Token.USER) {
|
||||
lexer.reset(markBp, markChar, Token.CREATE);
|
||||
return parseCreateUser();
|
||||
} else if (lexer.identifierEquals(FnvHash.Constants.PUBLIC)) {
|
||||
lexer.nextToken();
|
||||
if (lexer.identifierEquals("SYNONYM")) {
|
||||
lexer.reset(markBp, markChar, Token.CREATE);
|
||||
@@ -1906,10 +1921,10 @@ public class SQLStatementParser extends SQLParser {
|
||||
lexer.reset(markBp, markChar, Token.CREATE);
|
||||
SQLStatement stmt = this.parseCreateFunction();
|
||||
return stmt;
|
||||
} else if (lexer.identifierEquals("BITMAP")) {
|
||||
} else if (lexer.identifierEquals(FnvHash.Constants.BITMAP)) {
|
||||
lexer.reset(markBp, markChar, Token.CREATE);
|
||||
return parseCreateIndex(true);
|
||||
} else if (lexer.identifierEquals("MATERIALIZED")) {
|
||||
} else if (lexer.identifierEquals(FnvHash.Constants.MATERIALIZED)) {
|
||||
lexer.reset(markBp, markChar, Token.CREATE);
|
||||
return parseCreateMaterializedView();
|
||||
}
|
||||
@@ -1917,6 +1932,20 @@ public class SQLStatementParser extends SQLParser {
|
||||
throw new ParserException("TODO " + lexer.token);
|
||||
}
|
||||
|
||||
public SQLStatement parseCreateUser() {
|
||||
accept(Token.CREATE);
|
||||
accept(Token.USER);
|
||||
|
||||
SQLCreateUserStatement stmt = new SQLCreateUserStatement();
|
||||
stmt.setUser(this.exprParser.name());
|
||||
|
||||
accept(Token.IDENTIFIED);
|
||||
accept(Token.BY);
|
||||
stmt.setPassword(this.exprParser.primary());
|
||||
|
||||
return stmt;
|
||||
}
|
||||
|
||||
public SQLCreateFunctionStatement parseCreateFunction() {
|
||||
throw new ParserException("TODO " + lexer.token);
|
||||
}
|
||||
|
||||
@@ -3584,6 +3584,10 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
|
||||
return false;
|
||||
}
|
||||
|
||||
protected void printGrantPrivileges(SQLGrantStatement x) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLGrantStatement x) {
|
||||
print0(ucase ? "GRANT " : "grant ");
|
||||
@@ -5265,6 +5269,14 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean visit(SQLCreateUserStatement x) {
|
||||
print0(ucase ? "CREATE USER " : "create user ");
|
||||
x.getUser().accept(this);
|
||||
print0(ucase ? " IDENTIFIED BY " : " identified by ");
|
||||
x.getPassword().accept(this);
|
||||
return false;
|
||||
}
|
||||
|
||||
public Boolean getPrintStatementAfterSemi() {
|
||||
return printStatementAfterSemi;
|
||||
}
|
||||
|
||||
@@ -746,4 +746,8 @@ public interface SQLASTVisitor {
|
||||
boolean visit(SQLReplaceStatement x);
|
||||
|
||||
void endVisit(SQLReplaceStatement x);
|
||||
|
||||
boolean visit(SQLCreateUserStatement x);
|
||||
|
||||
void endVisit(SQLCreateUserStatement x);
|
||||
}
|
||||
|
||||
@@ -1709,6 +1709,16 @@ public class SQLASTVisitorAdapter implements SQLASTVisitor {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLCreateUserStatement x) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(SQLCreateUserStatement x) {
|
||||
|
||||
}
|
||||
|
||||
public final boolean isEnabled(VisitorFeature feature) {
|
||||
return VisitorFeature.isEnabled(this.features, feature);
|
||||
}
|
||||
|
||||
@@ -496,5 +496,9 @@ public final class FnvHash {
|
||||
long OFF = fnv1a_64_lower("OFF");
|
||||
long IDENTITY_INSERT = fnv1a_64_lower("IDENTITY_INSERT");
|
||||
long PASSWORD = fnv1a_64_lower("PASSWORD");
|
||||
long PUBLIC = fnv1a_64_lower("PUBLIC");
|
||||
long MATERIALIZED = fnv1a_64_lower("MATERIALIZED");
|
||||
long BITMAP = fnv1a_64_lower("BITMAP");
|
||||
long PACKAGE = fnv1a_64_lower("PACKAGE");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user