improved sql parser.

This commit is contained in:
wenshao
2017-03-13 23:07:26 +08:00
parent 190faee6d2
commit 781f4de2b4
12 changed files with 250 additions and 6 deletions
@@ -25,7 +25,7 @@ public class MySqlOutFileExpr extends MySqlObjectImpl implements SQLExpr {
private SQLExpr file;
private String charset;
private SQLLiteralExpr columnsTerminatedBy;
private SQLExpr columnsTerminatedBy;
private boolean columnsEnclosedOptionally = false;
private SQLLiteralExpr columnsEnclosedBy;
private SQLLiteralExpr columnsEscaped;
@@ -66,11 +66,11 @@ public class MySqlOutFileExpr extends MySqlObjectImpl implements SQLExpr {
this.charset = charset;
}
public SQLLiteralExpr getColumnsTerminatedBy() {
public SQLExpr getColumnsTerminatedBy() {
return columnsTerminatedBy;
}
public void setColumnsTerminatedBy(SQLLiteralExpr columnsTerminatedBy) {
public void setColumnsTerminatedBy(SQLExpr columnsTerminatedBy) {
this.columnsTerminatedBy = columnsTerminatedBy;
}
@@ -328,7 +328,7 @@ public class MySqlSelectParser extends SQLSelectParser {
lexer.nextToken();
accept(Token.BY);
}
outFile.setColumnsTerminatedBy((SQLLiteralExpr) expr());
outFile.setColumnsTerminatedBy(expr());
if (identifierEquals("OPTIONALLY")) {
lexer.nextToken();
@@ -250,6 +250,9 @@ public class MySqlSchemaStatVisitor extends SchemaStatVisitor implements MySqlAS
@Override
public boolean visit(MySqlKey x) {
for (SQLObject item : x.getColumns()) {
item.accept(this);
}
return false;
}
@@ -1,3 +1,18 @@
/*
* Copyright 1999-2101 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.odps.ast;
import com.alibaba.druid.sql.ast.SQLName;
@@ -236,7 +236,6 @@ public class OracleOutputVisitor extends SQLASTOutputVisitor implements OracleAS
print0(ucase ? "DELETE " : "delete ");
SQLTableSource tableSource = x.getTableSource();
if (x.getHints().size() > 0) {
printAndAccept(x.getHints(), ", ");
print(' ');
@@ -996,6 +996,13 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
}
}
if (column != null) {
SQLObject parent = x.getParent();
if (parent instanceof SQLPrimaryKey) {
column.setPrimaryKey(true);
} else if (parent instanceof SQLUnique) {
column.setUnique(true);
}
setColumn(x, column);
}
return false;
@@ -1440,6 +1447,14 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
column.setDataType(x.getDataType().getName());
}
for (SQLColumnConstraint item : x.getConstraints()) {
if (item instanceof SQLPrimaryKey) {
column.setPrimaryKey(true);
} else if (item instanceof SQLUnique) {
column.setUnique(true);
}
}
return false;
}
@@ -405,6 +405,9 @@ public class TableStat {
private boolean having;
private boolean join;
private boolean primaryKey; // for ddl
private boolean unique; //
private Map<String, Object> attributes = new HashMap<String, Object>();
private transient String fullName;
@@ -484,6 +487,22 @@ public class TableStat {
this.having = having;
}
public boolean isPrimaryKey() {
return primaryKey;
}
public void setPrimaryKey(boolean primaryKey) {
this.primaryKey = primaryKey;
}
public boolean isUnique() {
return unique;
}
public void setUnique(boolean unique) {
this.unique = unique;
}
public String getName() {
return name;
}
@@ -42,7 +42,8 @@ public class MySqlUtils {
static Constructor<?> MysqlXAConnectionConstructor = null;
public static XAConnection createXAConnection(Driver driver, Connection physicalConn) throws SQLException {
if (driver.getMajorVersion() == 5) {
final int major = driver.getMajorVersion();
if (major == 5) {
if (utilClass == null && !utilClassError) {
try {
utilClass = Class.forName("com.mysql.jdbc.Util");
@@ -1650,6 +1650,10 @@ public class WallVisitorUtils {
return;
}
if (isTopFromDenySchema(visitor, x)) {
return;
}
boolean isShow = x.getParent() instanceof MySqlShowGrantsStatement;
if (isShow) {
return;
@@ -1699,6 +1703,7 @@ public class WallVisitorUtils {
if (!(x.getParent() instanceof SQLSelectItem)) {
return false;
}
SQLSelectItem item = (SQLSelectItem) x.getParent();
if (!(item.getParent() instanceof SQLSelectQueryBlock)) {
@@ -1725,6 +1730,51 @@ public class WallVisitorUtils {
return stmt.getParent() == null;
}
private static boolean isTopFromDenySchema(WallVisitor visitor, SQLMethodInvokeExpr x) {
SQLObject parent = x.getParent();
for (;; ) {
if (parent instanceof SQLExpr || parent instanceof Item || parent instanceof SQLSelectItem) {
parent = parent.getParent();
} else {
break;
}
}
if (parent instanceof SQLSelectQueryBlock) {
SQLSelectQueryBlock queryBlock = (SQLSelectQueryBlock) parent;
if (!(queryBlock.getParent() instanceof SQLSelect)) {
return false;
}
SQLSelect select = (SQLSelect) queryBlock.getParent();
if (!(select.getParent() instanceof SQLSelectStatement)) {
return false;
}
SQLSelectStatement stmt = (SQLSelectStatement) select.getParent();
if (stmt.getParent() != null) {
return false;
}
SQLTableSource from = queryBlock.getFrom();
if (from instanceof SQLExprTableSource) {
SQLExpr fromExpr = ((SQLExprTableSource) from).getExpr();
if (fromExpr instanceof SQLName) {
String fromTableName = fromExpr.toString();
return visitor.isDenyTable(fromTableName);
}
}
return false;
}
return false;
}
private static boolean checkSchema(WallVisitor visitor, SQLExpr x) {
final WallTopStatementContext topStatementContext = wallTopStatementContextLocal.get();
if (topStatementContext != null && (topStatementContext.fromSysSchema || topStatementContext.fromSysTable)) {