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;
}
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;
}
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;
}
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);
}
}
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;
}
Aggregations