Search in sources :

Example 6 with MetadataTableRepositoryObject

use of org.talend.sqlbuilder.dbstructure.DBTreeProvider.MetadataTableRepositoryObject in project tdi-studio-se by Talend.

the class EMFRepositoryNodeManager method parseSqlStatement.

@SuppressWarnings("unchecked")
public List<IRepositoryNode> parseSqlStatement(String sql, RepositoryNode currRoot) throws Exception {
    // inital the quote depence on the dbtype
    IRepositoryViewObject rObject = currRoot.getObject();
    DatabaseConnectionItem item = (DatabaseConnectionItem) rObject.getProperty().getItem();
    DatabaseConnection dbConnection = (DatabaseConnection) item.getConnection();
    String dbType = dbConnection.getDatabaseType();
    leftDbQuote = TalendTextUtils.getQuoteByDBType(dbType, true);
    rightDbQuote = TalendTextUtils.getQuoteByDBType(dbType, false);
    sql = initSqlStatement(sql);
    if (sql == null || "".equals(sql) || !sql.startsWith("select ")) {
        //$NON-NLS-1$ //$NON-NLS-2$
        return Collections.EMPTY_LIST;
    }
    List<String> tableNames = new ArrayList<String>();
    List<String> columnsNames = new ArrayList<String>();
    String[] cols = parseSqlToNameList(sql, tableNames, columnsNames);
    List<IRepositoryNode> nodes = new ArrayList<IRepositoryNode>();
    for (IRepositoryNode tableNode : currRoot.getChildren()) {
        for (int i = 0; i < tableNames.size(); i++) {
            //$NON-NLS-1$
            String tableLabel = "";
            if (tableNode.getObject() instanceof MetadataTableRepositoryObject) {
                MetadataTableRepositoryObject object = (MetadataTableRepositoryObject) tableNode.getObject();
                tableLabel = object.getSourceName();
            } else {
                tableLabel = tableNode.getObject().getLabel();
            }
            boolean isNeed = false;
            if (cols.length == 1 && cols[0].equals("*")) {
                //$NON-NLS-1$
                for (String string : tableNames) {
                    if (string.equals(tableLabel.toLowerCase())) {
                        nodes.add(tableNode);
                        isNeed = true;
                    }
                }
            }
            if (tableLabel != null) {
                for (String string : tableNames) {
                    if (string.equals(tableLabel.toLowerCase())) {
                        isNeed = true;
                    }
                }
            }
            if (isNeed) {
                for (IRepositoryNode colNode : tableNode.getChildren()) {
                    //$NON-NLS-1$
                    String collabel = "";
                    if (colNode.getObject() instanceof MetadataColumnRepositoryObject) {
                        MetadataColumnRepositoryObject object2 = (MetadataColumnRepositoryObject) colNode.getObject();
                        collabel = object2.getSourceName();
                    } else {
                        collabel = colNode.getObject().getLabel();
                    }
                    if (collabel != null) {
                        for (String string : columnsNames) {
                            if ((string.replaceAll("\\" + leftDbQuote, "").replaceAll("\\" + rightDbQuote, "")).equals(//$NON-NLS-1$ //$NON-NLS-2$
                            collabel.toLowerCase())) {
                                nodes.add(colNode);
                            }
                            // right bracket
                            if ((string.replaceAll("\\" + leftDbQuote, "").replaceAll("\\" + rightDbQuote, "")).equals(tableLabel.toLowerCase() + "." + collabel.toLowerCase())) {
                                //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ 
                                if (!nodes.contains(colNode)) {
                                    nodes.add(colNode);
                                }
                            }
                            for (int j = 0; j < relations.size(); j++) {
                                String[] pks = relations.get(j);
                                String pk = pks[0];
                                String fk = pks[1];
                                boolean isSet = false;
                                if (pk.equals(collabel.toLowerCase())) {
                                    isSet = true;
                                    //$NON-NLS-1$
                                    pk = tableLabel.toLowerCase() + "." + collabel.toLowerCase();
                                    if (!nodes.contains(colNode)) {
                                        nodes.add(colNode);
                                    }
                                }
                                if (fk.equals(collabel.toLowerCase())) {
                                    isSet = true;
                                    //$NON-NLS-1$
                                    fk = tableLabel.toLowerCase() + "." + collabel.toLowerCase();
                                    if (!nodes.contains(colNode)) {
                                        nodes.add(colNode);
                                    }
                                }
                                if (pk.equals(tableLabel.toLowerCase() + "." + collabel.toLowerCase())) {
                                    //$NON-NLS-1$
                                    if (!nodes.contains(colNode)) {
                                        nodes.add(colNode);
                                    }
                                }
                                if (fk.equals(tableLabel.toLowerCase() + "." + collabel.toLowerCase())) {
                                    //$NON-NLS-1$
                                    if (!nodes.contains(colNode)) {
                                        nodes.add(colNode);
                                    }
                                }
                                if (isSet) {
                                    relations.set(j, new String[] { pk, fk });
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return nodes;
}
Also used : IRepositoryNode(org.talend.repository.model.IRepositoryNode) ArrayList(java.util.ArrayList) DatabaseConnectionItem(org.talend.core.model.properties.DatabaseConnectionItem) MetadataTableRepositoryObject(org.talend.sqlbuilder.dbstructure.DBTreeProvider.MetadataTableRepositoryObject) IRepositoryViewObject(org.talend.core.model.repository.IRepositoryViewObject) DatabaseConnection(org.talend.core.model.metadata.builder.connection.DatabaseConnection) MetadataColumnRepositoryObject(org.talend.sqlbuilder.dbstructure.DBTreeProvider.MetadataColumnRepositoryObject)

Aggregations

MetadataTableRepositoryObject (org.talend.sqlbuilder.dbstructure.DBTreeProvider.MetadataTableRepositoryObject)6 IRepositoryViewObject (org.talend.core.model.repository.IRepositoryViewObject)4 ArrayList (java.util.ArrayList)3 MetadataTable (org.talend.core.model.metadata.builder.connection.MetadataTable)3 MetadataColumnRepositoryObject (org.talend.sqlbuilder.dbstructure.DBTreeProvider.MetadataColumnRepositoryObject)3 DatabaseConnection (org.talend.core.model.metadata.builder.connection.DatabaseConnection)2 RepositoryObject (org.talend.core.model.repository.RepositoryObject)2 IRepositoryNode (org.talend.repository.model.IRepositoryNode)2 SqlBuilderRepositoryObject (org.talend.sqlbuilder.dbstructure.SqlBuilderRepositoryObject)2 List (java.util.List)1 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)1 CTabFolder (org.eclipse.swt.custom.CTabFolder)1 CTabItem (org.eclipse.swt.custom.CTabItem)1 MetadataColumn (org.talend.core.model.metadata.builder.connection.MetadataColumn)1 Query (org.talend.core.model.metadata.builder.connection.Query)1 DatabaseConnectionItem (org.talend.core.model.properties.DatabaseConnectionItem)1 RepositoryNode (org.talend.repository.model.RepositoryNode)1 RepositoryNodeType (org.talend.sqlbuilder.dbstructure.RepositoryNodeType)1 CatalogNode (org.talend.sqlbuilder.dbstructure.nodes.CatalogNode)1 DatabaseNode (org.talend.sqlbuilder.dbstructure.nodes.DatabaseNode)1