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