Search in sources :

Example 1 with Column

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;
}
Also used : ModelColumn(com.alibaba.druid.sql.dialect.oracle.ast.clause.ModelClause.ModelColumn) Column(com.alibaba.druid.stat.TableStat.Column) Relationship(com.alibaba.druid.stat.TableStat.Relationship) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 2 with Column

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;
}
Also used : SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLPropertyExpr(com.alibaba.druid.sql.ast.expr.SQLPropertyExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) SQLAllColumnExpr(com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr) Column(com.alibaba.druid.stat.TableStat.Column)

Example 3 with Column

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;
}
Also used : SQLObject(com.alibaba.druid.sql.ast.SQLObject) Column(com.alibaba.druid.stat.TableStat.Column)

Example 4 with Column

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;
}
Also used : SQLCastExpr(com.alibaba.druid.sql.ast.expr.SQLCastExpr) SQLMethodInvokeExpr(com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr) Column(com.alibaba.druid.stat.TableStat.Column) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLPropertyExpr(com.alibaba.druid.sql.ast.expr.SQLPropertyExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 5 with Column

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;
}
Also used : SQLObject(com.alibaba.druid.sql.ast.SQLObject) OdpsValuesTableSource(com.alibaba.druid.sql.dialect.odps.ast.OdpsValuesTableSource) Column(com.alibaba.druid.stat.TableStat.Column)

Aggregations

Column (com.alibaba.druid.stat.TableStat.Column)134 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)122 MySqlSchemaStatVisitor (com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor)74 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)73 OracleStatementParser (com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser)19 OracleSchemaStatVisitor (com.alibaba.druid.sql.dialect.oracle.visitor.OracleSchemaStatVisitor)19 SQLServerStatementParser (com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser)12 SQLServerSchemaStatVisitor (com.alibaba.druid.sql.dialect.sqlserver.visitor.SQLServerSchemaStatVisitor)12 SQLCreateTableStatement (com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement)10 DB2StatementParser (com.alibaba.druid.sql.dialect.db2.parser.DB2StatementParser)8 DB2SchemaStatVisitor (com.alibaba.druid.sql.dialect.db2.visitor.DB2SchemaStatVisitor)8 SchemaStatVisitor (com.alibaba.druid.sql.visitor.SchemaStatVisitor)8 OracleOutputVisitor (com.alibaba.druid.sql.dialect.oracle.visitor.OracleOutputVisitor)6 MysqlTest (com.alibaba.druid.sql.MysqlTest)5 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)5 SQLObject (com.alibaba.druid.sql.ast.SQLObject)5 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)5 Test (org.junit.Test)5 SQLCreateViewStatement (com.alibaba.druid.sql.ast.statement.SQLCreateViewStatement)4 MySqlCreateTableStatement (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement)4