use of org.talend.sqlbuilder.dbstructure.DBTreeProvider.MetadataColumnRepositoryObject 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;
}
use of org.talend.sqlbuilder.dbstructure.DBTreeProvider.MetadataColumnRepositoryObject 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.sqlbuilder.dbstructure.DBTreeProvider.MetadataColumnRepositoryObject in project tdi-studio-se by Talend.
the class MetadataColumnComparator method modifyOldRepositoryNode.
/**
* dev Comment method "modifyOldRepositoryNode".
*
* @param connection
* @param iMetadataConnection
*/
@SuppressWarnings("unchecked")
public void modifyOldRepositoryNode(DatabaseConnection connection, IMetadataConnection iMetadataConnection, RepositoryNode oldNode) throws Exception {
boolean status = new ManagerConnection().check(iMetadataConnection);
connection.setDivergency(!status);
if (status) {
// /Get MetadataTable From DB
List<MetadataTable> tablesFromDB = ExtractMetaDataFromDataBase.returnMetaTablesFormConnection(iMetadataConnection);
ExtractMetaDataUtils.getInstance().setReconnect(false);
// Get MetadataTable From EMF(Old RepositoryNode)
Set<MetadataTable> tablesetFromEMF = ConnectionHelper.getTables(connection);
List<MetadataTable> tablesFromEMF = new ArrayList<MetadataTable>();
tablesFromEMF.addAll(tablesetFromEMF);
if (oldNode.getProperties(EProperties.CONTENT_TYPE) == RepositoryNodeType.DATABASE) {
if (tablesFromEMF.size() < tablesFromDB.size()) {
modifyOldConnection(tablesFromEMF, iMetadataConnection, tablesFromDB, oldNode);
}
restoreConnection(connection, tablesFromEMF);
} else if (oldNode.getProperties(EProperties.CONTENT_TYPE) == RepositoryNodeType.TABLE) {
MetadataTable metadataTable = ((MetadataTableRepositoryObject) oldNode.getObject()).getTable();
modifyOldOneTableFromDB(tablesFromDB, metadataTable);
MetadataTable tableFromDB = null;
for (MetadataTable table : tablesFromDB) {
if (table.getSourceName().equals(metadataTable.getSourceName())) {
tableFromDB = table;
}
}
if (tableFromDB != null) {
List<MetadataColumn> columnsFromDB = new ArrayList<MetadataColumn>();
columnsFromDB.addAll(ExtractMetaDataFromDataBase.returnMetadataColumnsFormTable(iMetadataConnection, tableFromDB.getSourceName()));
fixedColumns(columnsFromDB, metadataTable.getColumns());
}
// for (MetadataColumn metadataColumn : (List<MetadataColumn>) metadataTable.getColumns()) {
// modifyOneColumnFromDB(iMetadataConnection, tablesFromDB, metadataColumn);
// }
restoreConnection(connection, tablesFromEMF);
} else if (oldNode.getProperties(EProperties.CONTENT_TYPE) == RepositoryNodeType.COLUMN) {
MetadataColumn metadataColumn = ((MetadataColumnRepositoryObject) oldNode.getObject()).getColumn();
modifyOneColumnFromDB(iMetadataConnection, tablesFromDB, metadataColumn);
}
} else {
Set<MetadataTable> tableset = ConnectionHelper.getTables(connection);
List<MetadataTable> tablesFromEMF = new ArrayList<MetadataTable>();
tablesFromEMF.addAll(tableset);
for (MetadataTable tableFromEMF : tablesFromEMF) {
List<MetadataColumn> columnsFromEMF = tableFromEMF.getColumns();
for (MetadataColumn column : columnsFromEMF) {
//$NON-NLS-1$
column.setOriginalField(" ");
column.setDivergency(true);
column.setSynchronised(false);
}
//$NON-NLS-1$
tableFromEMF.setSourceName(" ");
tableFromEMF.setDivergency(true);
}
}
ExtractMetaDataUtils.getInstance().setReconnect(true);
}
Aggregations