use of com.alibaba.druid.stat.TableStat.Column in project druid by alibaba.
the class OracleSchemaStatVisitor method visit.
@Override
public boolean visit(OracleSelectJoin x) {
super.visit(x);
for (SQLExpr item : x.getUsing()) {
if (item instanceof SQLIdentifierExpr) {
String columnName = ((SQLIdentifierExpr) item).getName();
String leftTable = (String) x.getLeft().getAttribute(ATTR_TABLE);
String rightTable = (String) x.getRight().getAttribute(ATTR_TABLE);
if (leftTable != null && rightTable != null) {
Relationship relationship = new Relationship();
relationship.setLeft(new Column(leftTable, columnName));
relationship.setRight(new Column(rightTable, columnName));
relationship.setOperator("USING");
relationships.add(relationship);
}
if (leftTable != null) {
addColumn(leftTable, columnName);
}
if (rightTable != null) {
addColumn(rightTable, columnName);
}
}
}
return false;
}
use of com.alibaba.druid.stat.TableStat.Column in project druid by alibaba.
the class SchemaStatVisitor method handleSubQueryColumn.
protected Column handleSubQueryColumn(String alias, SQLObject query) {
if (query instanceof SQLSelect) {
query = ((SQLSelect) query).getQuery();
}
SQLSelectQueryBlock queryBlock = null;
List<SQLSelectItem> selectList = null;
if (query instanceof SQLSelectQueryBlock) {
queryBlock = (SQLSelectQueryBlock) query;
if (queryBlock.getGroupBy() != null) {
return null;
}
selectList = queryBlock.getSelectList();
}
if (selectList != null) {
boolean allColumn = false;
String allColumnOwner = null;
for (SQLSelectItem item : selectList) {
if (!item.getClass().equals(SQLSelectItem.class)) {
continue;
}
String itemAlias = item.getAlias();
SQLExpr itemExpr = item.getExpr();
String ident = itemAlias;
if (itemAlias == null) {
if (itemExpr instanceof SQLIdentifierExpr) {
ident = itemAlias = itemExpr.toString();
} else if (itemExpr instanceof SQLPropertyExpr) {
itemAlias = ((SQLPropertyExpr) itemExpr).getName();
ident = itemExpr.toString();
}
}
if (alias.equalsIgnoreCase(itemAlias)) {
Column column = (Column) itemExpr.getAttribute(ATTR_COLUMN);
if (column != null) {
return column;
} else {
SQLTableSource from = queryBlock.getFrom();
if (from instanceof SQLSubqueryTableSource) {
SQLSelect select = ((SQLSubqueryTableSource) from).getSelect();
Column subQueryColumn = handleSubQueryColumn(ident, select);
return subQueryColumn;
}
}
}
if (itemExpr instanceof SQLAllColumnExpr) {
allColumn = true;
} else if (itemExpr instanceof SQLPropertyExpr) {
SQLPropertyExpr propertyExpr = (SQLPropertyExpr) itemExpr;
if (propertyExpr.getName().equals("*")) {
SQLExpr owner = propertyExpr.getOwner();
if (owner instanceof SQLIdentifierExpr) {
allColumnOwner = ((SQLIdentifierExpr) owner).getName();
allColumn = true;
}
}
}
}
if (allColumn) {
SQLTableSource from = queryBlock.getFrom();
String tableName = getTable(from, allColumnOwner);
if (tableName != null) {
return new Column(tableName, alias);
}
}
}
return null;
}
use of com.alibaba.druid.stat.TableStat.Column in project druid by alibaba.
the class SchemaStatVisitor method visit.
public boolean visit(SQLColumnDefinition x) {
String tableName = null;
{
SQLObject parent = x.getParent();
if (parent instanceof SQLCreateTableStatement) {
tableName = ((SQLCreateTableStatement) parent).getName().toString();
}
}
if (tableName == null) {
return true;
}
String columnName = x.getName().toString();
Column column = addColumn(tableName, columnName);
if (x.getDataType() != null) {
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;
}
use of com.alibaba.druid.stat.TableStat.Column in project druid by alibaba.
the class SchemaStatVisitor method getColumn.
protected Column getColumn(SQLExpr expr) {
Map<String, String> aliasMap = getAliasMap();
if (aliasMap == null) {
return null;
}
if (expr instanceof SQLMethodInvokeExpr) {
SQLMethodInvokeExpr methodInvokeExp = (SQLMethodInvokeExpr) expr;
if (methodInvokeExp.getParameters().size() == 1) {
SQLExpr firstExpr = methodInvokeExp.getParameters().get(0);
return getColumn(firstExpr);
}
}
if (expr instanceof SQLCastExpr) {
expr = ((SQLCastExpr) expr).getExpr();
}
if (expr instanceof SQLPropertyExpr) {
SQLExpr owner = ((SQLPropertyExpr) expr).getOwner();
String column = ((SQLPropertyExpr) expr).getName();
if (owner instanceof SQLIdentifierExpr) {
String tableName = ((SQLIdentifierExpr) owner).getName();
String table = tableName;
String tableNameLower = tableName.toLowerCase();
if (aliasMap.containsKey(tableNameLower)) {
table = aliasMap.get(tableNameLower);
}
if (containsSubQuery(tableNameLower)) {
table = null;
}
if (variants.containsKey(table)) {
return null;
}
if (table != null) {
return new Column(table, column);
}
return handleSubQueryColumn(tableName, column);
}
return null;
}
if (expr instanceof SQLIdentifierExpr) {
Column attrColumn = (Column) expr.getAttribute(ATTR_COLUMN);
if (attrColumn != null) {
return attrColumn;
}
String column = ((SQLIdentifierExpr) expr).getName();
String table = getCurrentTable();
if (table != null && aliasMap.containsKey(table)) {
table = aliasMap.get(table);
if (table == null) {
return null;
}
}
if (table != null) {
return new Column(table, column);
}
if (variants.containsKey(column)) {
return null;
}
return new Column("UNKNOWN", column);
}
return null;
}
use of com.alibaba.druid.stat.TableStat.Column in project druid by alibaba.
the class SchemaStatVisitor method visit.
public boolean visit(SQLIdentifierExpr x) {
String currentTable = getCurrentTable();
if (containsSubQuery(currentTable)) {
return false;
}
String ident = x.toString();
if (variants.containsKey(ident)) {
return false;
}
Column column = null;
if (currentTable != null) {
column = addColumn(currentTable, ident);
if (column != null && isParentGroupBy(x)) {
this.groupByColumns.add(column);
}
x.putAttribute(ATTR_COLUMN, column);
} else {
boolean skip = false;
for (SQLObject parent = x.getParent(); parent != null; parent = parent.getParent()) {
if (parent instanceof SQLSelectQueryBlock) {
SQLTableSource from = ((SQLSelectQueryBlock) parent).getFrom();
if (from instanceof OdpsValuesTableSource) {
skip = true;
break;
}
} else if (parent instanceof SQLSelectQuery) {
break;
}
}
if (!skip) {
column = handleUnkownColumn(ident);
}
if (column != null) {
x.putAttribute(ATTR_COLUMN, column);
}
}
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;
}
Aggregations