bug fixed for sql parser. for issue #1910

This commit is contained in:
wenshao
2017-08-16 14:19:02 +08:00
parent 4b7093dc6a
commit 97ed996c05
22 changed files with 440 additions and 64 deletions
@@ -791,6 +791,10 @@ public class SQLUtils {
if (MySqlUtils.isKeyword(normalizeName)) {
return name;
}
} else if (JdbcConstants.POSTGRESQL.equals(dbType)) {
if (PGUtils.isKeyword(normalizeName)) {
return name;
}
}
return normalizeName;
@@ -16,6 +16,7 @@
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.SQLStatement;
import com.alibaba.druid.sql.ast.SQLStatementImpl;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
@@ -26,7 +27,7 @@ public class SQLCommentStatement extends SQLStatementImpl {
TABLE, COLUMN
}
private SQLExpr on;
private SQLName on;
private Type type;
private SQLExpr comment;
@@ -46,11 +47,11 @@ public class SQLCommentStatement extends SQLStatementImpl {
this.type = type;
}
public SQLExpr getOn() {
public SQLName getOn() {
return on;
}
public void setOn(SQLExpr on) {
public void setOn(SQLName on) {
this.on = on;
}
@@ -33,16 +33,18 @@ import com.alibaba.druid.util.lang.Consumer;
public class SQLCreateTableStatement extends SQLStatementImpl implements SQLDDLStatement {
protected boolean ifNotExiists = false;
protected Type type;
protected SQLExprTableSource tableSource;
protected boolean ifNotExiists = false;
protected Type type;
protected SQLExprTableSource tableSource;
protected List<SQLTableElement> tableElementList = new ArrayList<SQLTableElement>();
// for postgresql
private SQLExprTableSource inherits;
private SQLExprTableSource inherits;
protected SQLSelect select;
protected SQLSelect select;
protected SQLExpr comment;
public SQLCreateTableStatement(){
@@ -52,6 +54,17 @@ public class SQLCreateTableStatement extends SQLStatementImpl implements SQLDDLS
super(dbType);
}
public SQLExpr getComment() {
return comment;
}
public void setComment(SQLExpr comment) {
if (comment != null) {
comment.setParent(this);
}
this.comment = comment;
}
public SQLName getName() {
if (tableSource == null) {
return null;
@@ -586,6 +599,38 @@ public class SQLCreateTableStatement extends SQLStatementImpl implements SQLDDLS
return false;
}
public boolean apply(SQLCommentStatement x) {
SQLName on = x.getOn();
SQLExpr comment = x.getComment();
if (comment == null) {
return false;
}
SQLCommentStatement.Type type = x.getType();
if (type == SQLCommentStatement.Type.TABLE) {
if (!SQLUtils.nameEquals(getName(), on)) {
return false;
}
setComment(comment.clone());
return true;
} else if (type == SQLCommentStatement.Type.COLUMN) {
SQLPropertyExpr propertyExpr = (SQLPropertyExpr) on;
if (!SQLUtils.nameEquals(getName(), (SQLName) propertyExpr.getOwner())) {
return false;
}
SQLColumnDefinition column = this.findColumn(propertyExpr.getName());
if (column != null) {
column.setComment(comment.clone());
}
return true;
}
return false;
}
public boolean apply(SQLAlterTableStatement alter) {
if (!SQLUtils.nameEquals(alter.getName(), this.getName())) {
return false;
@@ -842,6 +887,9 @@ public class SQLCreateTableStatement extends SQLStatementImpl implements SQLDDLS
if (select != null) {
x.setSelect(select.clone());
}
if (comment != null) {
x.setComment(comment.clone());
}
}
public SQLCreateTableStatement clone() {
@@ -241,7 +241,7 @@ public class MySqlCreateTableParser extends SQLCreateTableParser {
if (lexer.token() == Token.EQ) {
lexer.nextToken();
}
stmt.getTableOptions().put("COMMENT", this.exprParser.expr());
stmt.setComment(this.exprParser.expr());
continue;
}
@@ -536,6 +536,12 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
option.getValue().accept(this);
}
SQLExpr comment = x.getComment();
if (comment != null) {
print0(ucase ? " COMMENT " : " comment ");
comment.accept(this);
}
if (x.getPartitioning() != null) {
println();
x.getPartitioning().accept(this);
@@ -31,8 +31,6 @@ public class OdpsCreateTableStatement extends SQLCreateTableStatement {
private SQLExprTableSource like;
protected SQLExpr comment;
protected List<SQLColumnDefinition> partitionColumns = new ArrayList<SQLColumnDefinition>(2);
protected final List<SQLName> clusteredBy = new ArrayList<SQLName>();
@@ -58,14 +56,6 @@ public class OdpsCreateTableStatement extends SQLCreateTableStatement {
this.like = like;
}
public SQLExpr getComment() {
return comment;
}
public void setComment(SQLExpr comment) {
this.comment = comment;
}
public List<SQLColumnDefinition> getPartitionColumns() {
return partitionColumns;
}
@@ -1764,11 +1764,7 @@ public class OracleOutputVisitor extends SQLASTOutputVisitor implements OracleAS
print0(ucase ? "COMPUTE STATISTICS" : "compute statistics");
}
if (x.getTablespace() != null) {
println();
print0(ucase ? "TABLESPACE " : "tablespace ");
x.getTablespace().accept(this);
}
this.printOracleSegmentAttributes(x);
if (x.isOnline()) {
print0(ucase ? " ONLINE" : " online");
@@ -1790,8 +1786,6 @@ public class OracleOutputVisitor extends SQLASTOutputVisitor implements OracleAS
}
}
this.printOracleSegmentAttributes(x);
if (x.getLocalPartitions().size() > 0) {
println();
print0(ucase ? "LOCAL (" : "local (");
@@ -849,7 +849,7 @@ public class SchemaRepository {
}
}
x.setSchema(null);
x1.setSchema(null);
String name = x1.computeName();
SchemaObject table = schema.findTableOrView(name);
@@ -20,7 +20,9 @@ import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.sql.XAConnection;
@@ -52,4 +54,23 @@ public class PGUtils {
return tables;
}
private static Set<String> keywords;
public static boolean isKeyword(String name) {
if (name == null) {
return false;
}
String name_lower = name.toLowerCase();
Set<String> words = keywords;
if (words == null) {
words = new HashSet<String>();
Utils.loadFromFile("META-INF/druid/parser/postgresql/keywords", words);
keywords = words;
}
return words.contains(name_lower);
}
}