mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improved sql paser for mysql. issue #2611
This commit is contained in:
@@ -431,6 +431,21 @@ public class SQLColumnDefinition extends SQLObjectImpl implements SQLTableElemen
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean isPrimaryKey() {
|
||||
for (SQLColumnConstraint constraint : constraints) {
|
||||
if (constraint instanceof SQLColumnPrimaryKey) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
if (parent instanceof SQLCreateTableStatement) {
|
||||
return ((SQLCreateTableStatement) parent)
|
||||
.isPrimaryColumn(nameHashCode64());
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return SQLUtils.toSQLString(this, dbType);
|
||||
}
|
||||
|
||||
@@ -1107,4 +1107,35 @@ public class SQLCreateTableStatement extends SQLStatementImpl implements SQLDDLS
|
||||
this.rowFormat = x;
|
||||
}
|
||||
|
||||
public SQLColumnDefinition getColumn(String name) {
|
||||
if (name == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return getColumn(
|
||||
FnvHash.hashCode64(name)
|
||||
);
|
||||
}
|
||||
|
||||
public SQLColumnDefinition getColumn(long nameHash) {
|
||||
for (SQLTableElement element : this.tableElementList) {
|
||||
if (element instanceof SQLColumnDefinition) {
|
||||
SQLColumnDefinition column = (SQLColumnDefinition) element;
|
||||
if (column.nameHashCode64() == nameHash) {
|
||||
return column;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean isPrimaryColumn(long columnNameHash) {
|
||||
SQLPrimaryKey pk = this.findPrimaryKey();
|
||||
if (pk == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return pk.containsColumn(columnNameHash);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -73,6 +73,18 @@ public class SQLUnique extends SQLConstraintImpl implements SQLUniqueConstraint,
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean containsColumn(long columnNameHash) {
|
||||
for (SQLSelectOrderByItem item : columns) {
|
||||
SQLExpr expr = item.getExpr();
|
||||
if (expr instanceof SQLIdentifierExpr) {
|
||||
if (((SQLIdentifierExpr) expr).nameHashCode64() == columnNameHash) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void cloneTo(SQLUnique x) {
|
||||
super.cloneTo(x);
|
||||
|
||||
|
||||
@@ -24,4 +24,5 @@ public interface SQLUniqueConstraint extends SQLConstraint {
|
||||
List<SQLSelectOrderByItem> getColumns();
|
||||
|
||||
boolean containsColumn(String column);
|
||||
boolean containsColumn(long columnNameHash);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user