Search in sources :

Example 66 with MetadataTable

use of org.talend.core.model.metadata.builder.connection.MetadataTable in project tdi-studio-se by Talend.

the class EMFRepositoryNodeManager method getPKFromTables.

public List<String[]> getPKFromTables(List<MetadataTable> tables, String selectedContext) {
    List<String[]> fks = new ArrayList<String[]>();
    //$NON-NLS-1$
    String fk = "";
    //$NON-NLS-1$
    String pk = "";
    IMetadataConnection iMetadataConnection = null;
    if (root != null) {
        try {
            DatabaseConnection databaseConnection = (DatabaseConnection) SQLBuilderRepositoryNodeManager.getItem(root).getConnection();
            iMetadataConnection = ConvertionHelper.convert(databaseConnection, false, selectedContext);
            dbMetaData = rnmanager.getDatabaseMetaData(iMetadataConnection);
        } catch (final Exception e) {
            //$NON-NLS-1$
            final String mainMsg = Messages.getString("EMFRepositoryNodeManager.DBConnection.Text");
            Display.getDefault().syncExec(new Runnable() {

                /*
                     * (non-Javadoc)
                     * 
                     * @see java.lang.Runnable#run()
                     */
                @Override
                public void run() {
                    new ErrorDialogWidthDetailArea(new Shell(), SqlBuilderPlugin.PLUGIN_ID, mainMsg, e.getMessage());
                }
            });
        }
    }
    for (MetadataTable table : tables) {
        try {
            if (dbMetaData != null && table.getSourceName() != null) {
                ResultSet resultSet;
                if (dbMetaData.supportsSchemasInDataManipulation() && !"".equals(iMetadataConnection.getSchema())) {
                    // bug 0006949 added
                    if (dbMetaData.getCatalogs() != null) {
                        resultSet = dbMetaData.getExportedKeys(null, iMetadataConnection.getSchema(), table.getSourceName());
                    } else {
                        //$NON-NLS-1$
                        resultSet = dbMetaData.getExportedKeys("", iMetadataConnection.getSchema(), table.getSourceName());
                    }
                } else {
                    if (dbMetaData.getCatalogs() != null) {
                        resultSet = dbMetaData.getExportedKeys(null, iMetadataConnection.getSchema(), table.getSourceName());
                    } else {
                        //$NON-NLS-1$
                        resultSet = dbMetaData.getExportedKeys("", iMetadataConnection.getSchema(), table.getSourceName());
                    }
                }
                if (resultSet != null) {
                    ResultSetMetaData metadata = resultSet.getMetaData();
                    int[] relevantIndeces = new int[metadata.getColumnCount()];
                    for (int i = 1; i <= metadata.getColumnCount(); i++) {
                        relevantIndeces[i - 1] = i;
                    }
                    while (resultSet.next()) {
                        for (int relevantIndece : relevantIndeces) {
                            String key = metadata.getColumnName(relevantIndece);
                            if (key.toUpperCase().equals("FKCOLUMN_NAME")) {
                                //$NON-NLS-1$
                                fk += resultSet.getString(relevantIndece);
                            } else if (key.toUpperCase().equals("FKTABLE_NAME")) {
                                //$NON-NLS-1$
                                //$NON-NLS-1$
                                fk = resultSet.getString(relevantIndece) + ".";
                            } else if (key.toUpperCase().equals("PKCOLUMN_NAME")) {
                                //$NON-NLS-1$
                                //$NON-NLS-1$
                                pk = table.getSourceName() + "." + resultSet.getString(relevantIndece);
                            }
                        }
                        if (!"".equals(fk) && !"".equals(pk)) {
                            //$NON-NLS-1$ //$NON-NLS-2$
                            String[] strs = new String[2];
                            strs[0] = pk;
                            strs[1] = fk;
                            fks.add(strs);
                            //$NON-NLS-1$
                            fk = "";
                            //$NON-NLS-1$
                            pk = "";
                        }
                    }
                    resultSet.close();
                }
            }
        } catch (Exception e) {
            //$NON-NLS-1$
            SqlBuilderPlugin.log(Messages.getString("EMFRepositoryNodeManager.logMessage"), e);
        }
    }
    if (!relations.isEmpty()) {
        fks.addAll(relations);
        relations.clear();
    }
    return fks;
}
Also used : ArrayList(java.util.ArrayList) IMetadataConnection(org.talend.core.model.metadata.IMetadataConnection) ResultSetMetaData(java.sql.ResultSetMetaData) Shell(org.eclipse.swt.widgets.Shell) MetadataTable(org.talend.core.model.metadata.builder.connection.MetadataTable) ResultSet(java.sql.ResultSet) ErrorDialogWidthDetailArea(org.talend.commons.ui.swt.dialogs.ErrorDialogWidthDetailArea) DatabaseConnection(org.talend.core.model.metadata.builder.connection.DatabaseConnection)

Example 67 with MetadataTable

use of org.talend.core.model.metadata.builder.connection.MetadataTable in project tdi-studio-se by Talend.

the class EMFRepositoryNodeManager method getTables.

@SuppressWarnings("unchecked")
public List<MetadataTable> getTables(List<IRepositoryNode> nodes, List<MetadataColumn> selectedColumns) {
    List<MetadataTable> tables = new ArrayList<MetadataTable>();
    for (IRepositoryNode node : nodes) {
        RepositoryNodeType type = SQLBuilderRepositoryNodeManager.getRepositoryType((RepositoryNode) node);
        if (type == RepositoryNodeType.DATABASE) {
            root = (RepositoryNode) node;
            DatabaseConnection connection = (DatabaseConnection) SQLBuilderRepositoryNodeManager.getItem((RepositoryNode) node).getConnection();
            for (MetadataTable table : ConnectionHelper.getTables(connection)) {
                if (!tables.contains(table)) {
                    tables.add(table);
                    selectedColumns.addAll(table.getColumns());
                }
            }
            // if database is selected , It does not need to check others.
            break;
        } else if (type == RepositoryNodeType.TABLE) {
            MetadataTable table = ((MetadataTableRepositoryObject) node.getObject()).getTable();
            if (!tables.contains(table)) {
                tables.add(table);
                selectedColumns.addAll(table.getColumns());
            }
            root = SQLBuilderRepositoryNodeManager.getRoot((RepositoryNode) node);
        } else if (type == RepositoryNodeType.COLUMN) {
            MetadataColumn column = ((MetadataColumnRepositoryObject) node.getObject()).getColumn();
            if (!selectedColumns.contains(column)) {
                selectedColumns.add(column);
            }
            MetadataTable table = column.getTable();
            if (!tables.contains(table)) {
                tables.add(table);
            }
            root = SQLBuilderRepositoryNodeManager.getRoot((RepositoryNode) node);
        }
        setRoot(null);
    }
    return tables;
}
Also used : MetadataColumn(org.talend.core.model.metadata.builder.connection.MetadataColumn) IRepositoryNode(org.talend.repository.model.IRepositoryNode) RepositoryNodeType(org.talend.sqlbuilder.dbstructure.RepositoryNodeType) ArrayList(java.util.ArrayList) MetadataTable(org.talend.core.model.metadata.builder.connection.MetadataTable) DatabaseConnection(org.talend.core.model.metadata.builder.connection.DatabaseConnection) MetadataColumnRepositoryObject(org.talend.sqlbuilder.dbstructure.DBTreeProvider.MetadataColumnRepositoryObject) RepositoryNode(org.talend.repository.model.RepositoryNode) IRepositoryNode(org.talend.repository.model.IRepositoryNode)

Example 68 with MetadataTable

use of org.talend.core.model.metadata.builder.connection.MetadataTable in project tdi-studio-se by Talend.

the class MetadataColumnComparator method sortTableColumn.

/**
     * dev Comment method "sortTableColumn".
     * 
     * @param set
     * @return
     */
@SuppressWarnings("unchecked")
private static List<MetadataTable> sortTableColumn(Collection<MetadataTable> set) {
    List<MetadataTable> sysTables = new ArrayList<MetadataTable>();
    List<MetadataTable> divTables = new ArrayList<MetadataTable>();
    List<MetadataTable> grayTables = new ArrayList<MetadataTable>();
    List<MetadataTable> norTables = new ArrayList<MetadataTable>();
    List<MetadataTable> sortTables = new ArrayList<MetadataTable>();
    for (MetadataTable object : set) {
        boolean isTableNormal = true;
        if (object.isDivergency()) {
            divTables.add(object);
            isTableNormal = false;
            continue;
        }
        if (object.getLabel() == null || "".equals(object.getLabel())) {
            //$NON-NLS-1$
            grayTables.add(object);
            isTableNormal = false;
            continue;
        }
        List<MetadataColumn> columns = sortColumn(object.getColumns());
        object.getColumns().clear();
        object.getColumns().addAll(columns);
        for (MetadataColumn column : columns) {
            if (column.isSynchronised()) {
                sysTables.add(object);
                isTableNormal = false;
                break;
            }
            if (column.isDivergency()) {
                divTables.add(object);
                isTableNormal = false;
                break;
            }
            if (column.getLabel() == null || "".equals(column.getLabel())) {
                //$NON-NLS-1$
                grayTables.add(object);
                isTableNormal = false;
                break;
            }
        }
        if (isTableNormal) {
            norTables.add(object);
        }
    }
    MetadataTableComparator metadataTableComparator = new MetadataTableComparator();
    Collections.sort(norTables, metadataTableComparator);
    Collections.sort(sysTables, metadataTableComparator);
    Collections.sort(divTables, metadataTableComparator);
    Collections.sort(grayTables, metadataTableComparator);
    sortTables.addAll(norTables);
    sortTables.addAll(sysTables);
    sortTables.addAll(divTables);
    sortTables.addAll(grayTables);
    return sortTables;
}
Also used : MetadataColumn(org.talend.core.model.metadata.builder.connection.MetadataColumn) ArrayList(java.util.ArrayList) MetadataTable(org.talend.core.model.metadata.builder.connection.MetadataTable)

Example 69 with MetadataTable

use of org.talend.core.model.metadata.builder.connection.MetadataTable in project tdi-studio-se by Talend.

the class MetadataColumnComparator method modifyOldOneTableFromEMF.

/**
     * dev Comment method "modifyOldOneTableFromEMF".
     * 
     * @param metaFromEMF
     * @param iMetadataConnection
     * @param db
     */
@SuppressWarnings("unchecked")
private void modifyOldOneTableFromEMF(List<MetadataTable> metaFromEMF, IMetadataConnection iMetadataConnection, MetadataTable db) {
    boolean flag = true;
    for (MetadataTable emf : metaFromEMF) {
        if (db.getSourceName().equals(emf.getSourceName())) {
            flag = false;
            break;
        }
    }
    if (flag) {
        MetadataTable table = ConnectionFactory.eINSTANCE.createMetadataTable();
        table.setSourceName(db.getSourceName());
        //$NON-NLS-1$
        table.setLabel("");
        // List<TdColumn> columns = ExtractMetaDataFromDataBase.returnMetadataColumnsFormTable(iMetadataConnection,
        // table.getSourceName());
        // for (MetadataColumn column : columns) {
        // MetadataColumn column1 = ConnectionFactory.eINSTANCE.createMetadataColumn();
        // column1.setOriginalField(column.getOriginalField());
        // column1.setLabel(""); //$NON-NLS-1$
        // table.getColumns().add(column1);
        // }
        metaFromEMF.add(table);
    }
}
Also used : MetadataTable(org.talend.core.model.metadata.builder.connection.MetadataTable)

Example 70 with MetadataTable

use of org.talend.core.model.metadata.builder.connection.MetadataTable in project tdi-studio-se by Talend.

the class MetadataColumnComparator method getAllNamesByRepositoryNode.

/**
     * method "getTableNamesByRepositoryNode" get All Table Names in current RepositoryNode's DatabaseConnectionItem.
     * 
     * @param node current RepositoryNode
     * @return List :all Table Names.
     */
@SuppressWarnings("unchecked")
public static Map<String, List<String>> getAllNamesByRepositoryNode(RepositoryNode node) {
    Map<String, List<String>> allNames = new HashMap<String, List<String>>();
    DatabaseConnectionItem item = getItem(getRoot(node));
    DatabaseConnection connection = (DatabaseConnection) item.getConnection();
    Set<MetadataTable> tablesFromEMF = ConnectionHelper.getTables(connection);
    boolean isOdbc = connection.getSID() == null || connection.getSID().length() == 0;
    String sid = isOdbc ? connection.getDatasourceName() : connection.getSID();
    for (MetadataTable table : tablesFromEMF) {
        String tableName = table.getSourceName();
        if (tableName != null && !"".equals(tableName)) {
            //$NON-NLS-1$
            List<String> columnNames = new ArrayList<String>();
            //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
            tableName = "\"" + sid + "\".\"" + tableName + "\"";
            List<MetadataColumn> columns = table.getColumns();
            for (MetadataColumn column : columns) {
                String columnName = column.getOriginalField();
                if (columnName != null && !"".equals(columnName)) {
                    //$NON-NLS-1$
                    //$NON-NLS-1$ //$NON-NLS-2$
                    columnName = tableName + ".\"" + columnName + "\"";
                    columnNames.add(columnName);
                }
            }
            allNames.put(tableName, columnNames);
        }
    }
    return allNames;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) DatabaseConnectionItem(org.talend.core.model.properties.DatabaseConnectionItem) MetadataColumn(org.talend.core.model.metadata.builder.connection.MetadataColumn) MetadataTable(org.talend.core.model.metadata.builder.connection.MetadataTable) List(java.util.List) ArrayList(java.util.ArrayList) DatabaseConnection(org.talend.core.model.metadata.builder.connection.DatabaseConnection)

Aggregations

MetadataTable (org.talend.core.model.metadata.builder.connection.MetadataTable)76 ArrayList (java.util.ArrayList)26 IMetadataTable (org.talend.core.model.metadata.IMetadataTable)24 MetadataColumn (org.talend.core.model.metadata.builder.connection.MetadataColumn)22 IRepositoryViewObject (org.talend.core.model.repository.IRepositoryViewObject)18 List (java.util.List)14 DatabaseConnection (org.talend.core.model.metadata.builder.connection.DatabaseConnection)13 PersistenceException (org.talend.commons.exception.PersistenceException)11 ConnectionItem (org.talend.core.model.properties.ConnectionItem)10 MetadataTableRepositoryObject (org.talend.core.repository.model.repositoryObject.MetadataTableRepositoryObject)10 RepositoryNode (org.talend.repository.model.RepositoryNode)10 DatabaseConnectionItem (org.talend.core.model.properties.DatabaseConnectionItem)9 TaggedValue (orgomg.cwm.objectmodel.core.TaggedValue)9 HashMap (java.util.HashMap)8 Schema (org.apache.avro.Schema)7 ComponentProperties (org.talend.components.api.properties.ComponentProperties)7 SAPFunctionUnit (org.talend.core.model.metadata.builder.connection.SAPFunctionUnit)7 IElementParameter (org.talend.core.model.process.IElementParameter)7 Map (java.util.Map)6 IProxyRepositoryFactory (org.talend.repository.model.IProxyRepositoryFactory)6