use of org.talend.designer.dbmap.external.data.ExternalDbMapData in project tdi-studio-se by Talend.
the class DbGenerationManager method initExpression.
protected String initExpression(DbMapComponent component, ExternalDbMapEntry dbMapEntry) {
String expression = dbMapEntry.getExpression();
if (expression != null) {
List<Map<String, String>> itemNameList = null;
// MapExpressionParser mapParser = new MapExpressionParser("((\\s*(\\w+)\\s*\\.)*)(\\w+)");
// List<String> parseInTableEntryLocations = mapParser.parseInTableEntryLocations2(expression);
// for (String entryLocation : parseInTableEntryLocations) {
//
// }
// context.schema.context.table.column
// context.schema.table.column
// schema.context.table.column
// schema.table.column
// table.column
// add \\w*#* for oracle 12 , schema name start with C##
//$NON-NLS-1$
MapExpressionParser mapParser1 = new MapExpressionParser("((\\s*(\\w*#*\\w+)\\s*\\.)*)(\\w+)");
itemNameList = mapParser1.parseInTableEntryLocations2(expression);
if (itemNameList == null || itemNameList.isEmpty()) {
//$NON-NLS-1$
MapExpressionParser mapParser2 = new MapExpressionParser("\\s*(\\w+)\\s*\\.\\s*(\\w+)\\s*");
itemNameList = mapParser2.parseInTableEntryLocations(expression);
}
for (Map<String, String> itemNamemap : itemNameList) {
Set<Entry<String, String>> set = itemNamemap.entrySet();
Iterator<Entry<String, String>> ite = set.iterator();
while (ite.hasNext()) {
Entry<String, String> entry = ite.next();
String columnValue = entry.getKey();
String tableValue = entry.getValue();
String tableNameValue = tableValue;
// find original table name if tableValue is alias
String originaltableName = tableValue;
ExternalDbMapData externalData = (ExternalDbMapData) component.getExternalData();
final List<ExternalDbMapTable> inputTables = externalData.getInputTables();
for (ExternalDbMapTable inputTable : inputTables) {
if (inputTable.getAlias() != null && inputTable.getAlias().equals(tableValue)) {
originaltableName = inputTable.getTableName();
tableNameValue = inputTable.getAlias();
}
}
List<IConnection> inputConnections = (List<IConnection>) component.getIncomingConnections();
if (inputConnections == null) {
return expression;
}
for (IConnection iconn : inputConnections) {
IMetadataTable metadataTable = iconn.getMetadataTable();
String tName = iconn.getName();
if ((originaltableName.equals(tName) || tableValue.equals(tName)) && metadataTable != null) {
List<IMetadataColumn> lColumn = metadataTable.getListColumns();
String tableName = metadataTable.getTableName();
String tableColneName = tableName;
tableColneName = MetadataToolHelper.validateTableName(tableColneName);
if (tableValue.contains(".") && tableName != null) {
//$NON-NLS-1$
//$NON-NLS-1$
MapExpressionParser mapParser2 = new MapExpressionParser("\\s*(\\w+)\\s*\\.\\s*(\\w+)\\s*");
List<Map<String, String>> tableNameList = mapParser2.parseInTableEntryLocations(tableValue);
for (Map<String, String> tableNameMap : tableNameList) {
Set<Entry<String, String>> setTable = tableNameMap.entrySet();
Iterator<Entry<String, String>> iteTable = setTable.iterator();
while (iteTable.hasNext()) {
Entry<String, String> tableEntry = iteTable.next();
String tableLabel = tableEntry.getKey();
String schemaValue = tableEntry.getValue();
if (tableLabel.equals(metadataTable.getLabel()) && tableColneName.equals(tableLabel)) {
//$NON-NLS-1$//$NON-NLS-2$
tableName = tableName.replaceAll("\\$", "\\\\\\$");
//$NON-NLS-1$
expression = expression.replaceFirst(tableValue, schemaValue + "." + tableName);
}
}
}
} else if (tableName != null) {
if (tableValue.equals(metadataTable.getLabel()) && tableColneName.equals(tableValue)) {
//$NON-NLS-1$ //$NON-NLS-2$
tableName = tableName.replaceAll("\\$", "\\\\\\$");
expression = expression.replaceFirst(tableValue, tableName);
}
}
for (IMetadataColumn co : lColumn) {
if (columnValue.equals(co.getLabel())) {
String oriName = co.getOriginalDbColumnName();
// if OriginalDbColumn is empty , still use label to generate sql
if (oriName == null || "".equals(oriName)) {
//$NON-NLS-1$
continue;
}
if (expression.trim().equals(tableValue + "." + oriName)) {
continue;
}
if (expression.trim().equals(originaltableName + "." + oriName)) {
continue;
}
// if it is temp delived table, use label to generate sql
if (iconn.getLineStyle() == EConnectionType.TABLE_REF) {
continue;
}
//$NON-NLS-1$ //$NON-NLS-2$
oriName = oriName.replaceAll("\\$", "\\\\\\$");
expression = //$NON-NLS-1$
expression.replaceFirst(//$NON-NLS-1$
"\\." + co.getLabel(), //$NON-NLS-1$
"\\." + oriName);
expression = expression.replace("\"", "\\\"");
}
}
}
}
}
}
}
return expression;
}
use of org.talend.designer.dbmap.external.data.ExternalDbMapData in project tdi-studio-se by Talend.
the class DbMapComponent method buildExternalData.
@Override
public void buildExternalData(AbstractExternalData abstractData) {
externalData = new ExternalDbMapData();
if (abstractData instanceof DBMapData) {
DBMapData mapperData = (DBMapData) abstractData;
List<ExternalDbMapTable> externalTables = new ArrayList<ExternalDbMapTable>();
// input
for (InputTable pTable : mapperData.getInputTables()) {
ExternalDbMapTable externalTable = new ExternalDbMapTable();
externalTable.setName(pTable.getName());
externalTable.setMinimized(pTable.isMinimized());
externalTable.setAlias(pTable.getAlias());
externalTable.setJoinType(pTable.getJoinType());
externalTable.setTableName(pTable.getTableName());
List<ExternalDbMapEntry> entities = new ArrayList<ExternalDbMapEntry>();
for (DBMapperTableEntry pEntity : pTable.getDBMapperTableEntries()) {
ExternalDbMapEntry entity = new ExternalDbMapEntry();
entity.setExpression(pEntity.getExpression());
entity.setJoin(pEntity.isJoin());
entity.setName(pEntity.getName());
entity.setOperator(pEntity.getOperator());
entities.add(entity);
}
externalTable.setMetadataTableEntries(entities);
externalTables.add(externalTable);
}
externalData.setInputTables(externalTables);
// output
externalTables = new ArrayList<ExternalDbMapTable>();
for (OutputTable pTable : mapperData.getOutputTables()) {
ExternalDbMapTable externalTable = new ExternalDbMapTable();
externalTable.setName(pTable.getName());
externalTable.setMinimized(pTable.isMinimized());
externalTable.setTableName(pTable.getTableName());
List<ExternalDbMapEntry> entities = new ArrayList<ExternalDbMapEntry>();
for (DBMapperTableEntry pEntity : pTable.getDBMapperTableEntries()) {
ExternalDbMapEntry entity = new ExternalDbMapEntry();
entity.setExpression(pEntity.getExpression());
entity.setName(pEntity.getName());
entities.add(entity);
}
externalTable.setMetadataTableEntries(entities);
// filters
entities = new ArrayList<ExternalDbMapEntry>();
List<ExternalDbMapEntry> otherFilterEntities = new ArrayList<ExternalDbMapEntry>();
for (FilterEntry pFilter : pTable.getFilterEntries()) {
ExternalDbMapEntry entity = new ExternalDbMapEntry();
entity.setExpression(pFilter.getExpression());
entity.setName(pFilter.getName());
if (FilterTableEntry.OTHER_FILTER.equals(pFilter.getFilterKind())) {
otherFilterEntities.add(entity);
} else {
entities.add(entity);
}
}
externalTable.setCustomWhereConditionsEntries(entities);
externalTable.setCustomOtherConditionsEntries(otherFilterEntities);
externalTables.add(externalTable);
}
externalData.setOutputTables(externalTables);
}
this.setExternalData(externalData);
}
use of org.talend.designer.dbmap.external.data.ExternalDbMapData in project tdi-studio-se by Talend.
the class MapperMain method createModelFromExternalData.
public void createModelFromExternalData(List<IOConnection> inputs, List<IOConnection> outputs, List<IMetadataTable> outputMetadataTables, ExternalDbMapData externalData, boolean checkProblems) {
if (externalData == null) {
externalData = new ExternalDbMapData();
}
mapperManager.getUiManager().setUiProperties(externalData.getUiProperties());
ExternalDataConverter converter = new ExternalDataConverter(mapperManager);
if (this.mapperModel == null) {
this.mapperModel = converter.prepareModel(inputs, outputs, outputMetadataTables, externalData, checkProblems);
}
}
use of org.talend.designer.dbmap.external.data.ExternalDbMapData in project tdi-studio-se by Talend.
the class PostgresGenerationManagerTest method init.
private void init(String schema, String main_table, String lookup_table) {
List<IConnection> incomingConnections = new ArrayList<IConnection>();
String[] mainTableEntities = new String[] { "id", "name", "classNum" };
String[] lookupEndtities = new String[] { "id", "score" };
incomingConnections.add(mockConnection(schema, main_table, mainTableEntities));
incomingConnections.add(mockConnection(schema, lookup_table, lookupEndtities));
component.setIncomingConnections(incomingConnections);
ExternalDbMapData externalData = new ExternalDbMapData();
List<ExternalDbMapTable> inputs = new ArrayList<ExternalDbMapTable>();
List<ExternalDbMapTable> outputs = new ArrayList<ExternalDbMapTable>();
// main table
ExternalDbMapTable inputTable = new ExternalDbMapTable();
inputTable.setTableName(schema + "." + main_table);
List<ExternalDbMapEntry> entities = getMetadataEntities(mainTableEntities, new String[3]);
inputTable.setMetadataTableEntries(entities);
inputs.add(inputTable);
// lookup table
inputTable = new ExternalDbMapTable();
inputTable.setTableName(schema + "." + lookup_table);
entities = getMetadataEntities(lookupEndtities, new String[2]);
inputTable.setMetadataTableEntries(entities);
inputs.add(inputTable);
// output
ExternalDbMapTable outputTable = new ExternalDbMapTable();
outputTable.setName("grade");
List<ExternalDbMapEntry> conditions = new ArrayList<ExternalDbMapEntry>();
conditions.add(new ExternalDbMapEntry(schema + "." + main_table + ".id =3"));
outputTable.setCustomWhereConditionsEntries(conditions);
String[] names = new String[] { "id", "name", "classNum", "score" };
String[] expressions = new String[] { schema + "." + main_table + ".id", schema + "." + main_table + ".name", schema + "." + main_table + ".classNum", schema + "." + lookup_table + ".score" };
outputTable.setMetadataTableEntries(getMetadataEntities(names, expressions));
outputs.add(outputTable);
externalData.setInputTables(inputs);
externalData.setOutputTables(outputs);
component.setExternalData(externalData);
List<IMetadataTable> metadataList = new ArrayList<IMetadataTable>();
MetadataTable metadataTable = getMetadataTable(names);
metadataTable.setLabel("grade");
metadataList.add(metadataTable);
component.setMetadataList(metadataList);
Process process = mock(Process.class);
when(process.getContextManager()).thenReturn(new JobContextManager());
component.setProcess(process);
}
use of org.talend.designer.dbmap.external.data.ExternalDbMapData in project tdi-studio-se by Talend.
the class DbGenerationManager method buildSqlSelect.
/**
*
* ggu Comment method "buildSqlSelect".
*
* @param component
* @param outputTableName
* @param tabSpaceString
* @return
*/
public String buildSqlSelect(DbMapComponent component, String outputTableName, String tabString) {
//$NON-NLS-1$
queryColumnsName = "\"";
aliasAlreadyDeclared.clear();
this.tabSpaceString = tabString;
List<IConnection> outputConnections = (List<IConnection>) component.getOutgoingConnections();
Map<String, IConnection> nameToOutputConnection = new HashMap<String, IConnection>();
for (IConnection connection : outputConnections) {
nameToOutputConnection.put(connection.getUniqueName(), connection);
}
ExternalDbMapData data = (ExternalDbMapData) component.getExternalData();
StringBuilder sb = new StringBuilder();
List<ExternalDbMapTable> outputTables = data.getOutputTables();
int lstOutputTablesSize = outputTables.size();
ExternalDbMapTable outputTable = null;
for (int i = 0; i < lstOutputTablesSize; i++) {
ExternalDbMapTable temp = outputTables.get(i);
if (outputTableName.equals(temp.getName())) {
outputTable = temp;
break;
}
}
if (outputTable != null) {
IConnection connection = nameToOutputConnection.get(outputTable.getName());
List<IMetadataColumn> columns = new ArrayList<IMetadataColumn>();
if (connection != null) {
IMetadataTable metadataTable = connection.getMetadataTable();
if (metadataTable != null) {
columns.addAll(metadataTable.getListColumns());
// call this function seems no use now but only problems
// outputTable = removeUnmatchingEntriesWithColumnsOfMetadataTable(outputTable, metadataTable);
}
}
//$NON-NLS-1$
sb.append("\"");
sb.append(DbMapSqlConstants.SELECT);
sb.append(DbMapSqlConstants.NEW_LINE).append(tabSpaceString);
List<ExternalDbMapEntry> metadataTableEntries = outputTable.getMetadataTableEntries();
if (metadataTableEntries != null) {
int lstSizeOutTableEntries = metadataTableEntries.size();
for (int i = 0; i < lstSizeOutTableEntries; i++) {
ExternalDbMapEntry dbMapEntry = metadataTableEntries.get(i);
String expression = dbMapEntry.getExpression();
expression = initExpression(component, dbMapEntry);
expression = addQuoteForSpecialChar(expression, component);
// }
if (!DEFAULT_TAB_SPACE_STRING.equals(this.tabSpaceString)) {
expression += DbMapSqlConstants.SPACE + DbMapSqlConstants.AS + DbMapSqlConstants.SPACE + getAliasOf(dbMapEntry.getName());
}
if (i > 0) {
sb.append(DbMapSqlConstants.COMMA);
sb.append(DbMapSqlConstants.SPACE);
queryColumnsName += DbMapSqlConstants.COMMA + DbMapSqlConstants.SPACE;
}
if (expression != null && expression.trim().length() > 0) {
sb.append(expression);
queryColumnsName += expression;
} else {
sb.append(DbMapSqlConstants.LEFT_COMMENT);
String str = outputTable.getName() + DbMapSqlConstants.DOT + dbMapEntry.getName();
//$NON-NLS-1$
sb.append(Messages.getString("DbGenerationManager.OuputExpSetMessage", str));
sb.append(DbMapSqlConstants.RIGHT_COMMENT);
}
}
}
sb.append(DbMapSqlConstants.NEW_LINE).append(tabSpaceString);
sb.append(DbMapSqlConstants.FROM);
List<ExternalDbMapTable> inputTables = data.getInputTables();
// load input table in hash
boolean explicitJoin = false;
int lstSizeInputTables = inputTables.size();
Map<String, ExternalDbMapTable> nameToInputTable = new HashMap<String, ExternalDbMapTable>();
for (int i = 0; i < lstSizeInputTables; i++) {
ExternalDbMapTable inputTable = inputTables.get(i);
nameToInputTable.put(inputTable.getName(), inputTable);
IJoinType joinType = language.getJoin(inputTable.getJoinType());
if (!language.unuseWithExplicitJoin().contains(joinType) && i > 0) {
explicitJoin = true;
}
}
StringBuilder sbWhere = new StringBuilder();
boolean isFirstClause = true;
for (int i = 0; i < lstSizeInputTables; i++) {
ExternalDbMapTable inputTable = inputTables.get(i);
if (buildConditions(component, sbWhere, inputTable, false, isFirstClause)) {
isFirstClause = false;
}
}
sb.append(DbMapSqlConstants.NEW_LINE).append(tabSpaceString);
IJoinType previousJoinType = null;
for (int i = 0; i < lstSizeInputTables; i++) {
ExternalDbMapTable inputTable = inputTables.get(i);
IJoinType joinType = null;
if (i == 0) {
joinType = AbstractDbLanguage.JOIN.NO_JOIN;
} else {
joinType = language.getJoin(inputTable.getJoinType());
}
boolean commaCouldBeAdded = !explicitJoin && i > 0;
boolean crCouldBeAdded = false;
if (language.unuseWithExplicitJoin().contains(joinType) && !explicitJoin) {
buildTableDeclaration(component, sb, inputTable, commaCouldBeAdded, crCouldBeAdded, false);
} else if (!language.unuseWithExplicitJoin().contains(joinType) && explicitJoin) {
if (i > 0) {
if (previousJoinType == null) {
buildTableDeclaration(component, sb, inputTables.get(i - 1), commaCouldBeAdded, crCouldBeAdded, true);
previousJoinType = joinType;
} else {
sb.append(DbMapSqlConstants.NEW_LINE).append(tabSpaceString);
}
sb.append(DbMapSqlConstants.SPACE);
}
String labelJoinType = joinType.getLabel();
sb.append(labelJoinType);
sb.append(DbMapSqlConstants.SPACE);
if (joinType == AbstractDbLanguage.JOIN.CROSS_JOIN) {
ExternalDbMapTable nextTable = null;
if (i < lstSizeInputTables) {
nextTable = inputTables.get(i);
buildTableDeclaration(component, sb, nextTable, false, false, true);
}
} else {
// ExternalDbMapTable rightTable = joinLeftToJoinRightTables.get(inputTable.getName());
buildTableDeclaration(component, sb, inputTable, false, false, true);
// if (rightTable != null) {
// } else {
// sb.append(" <!! NO JOIN CLAUSES FOR '" + inputTable.getName() + "' !!> ");
// }
sb.append(DbMapSqlConstants.SPACE);
sb.append(DbMapSqlConstants.ON);
sb.append(DbMapSqlConstants.LEFT_BRACKET);
sb.append(DbMapSqlConstants.SPACE);
if (!buildConditions(component, sb, inputTable, true, true)) {
sb.append(DbMapSqlConstants.LEFT_COMMENT);
sb.append(DbMapSqlConstants.SPACE);
//$NON-NLS-1$
sb.append(Messages.getString("DbGenerationManager.conditionNotSet"));
sb.append(DbMapSqlConstants.SPACE);
sb.append(DbMapSqlConstants.RIGHT_COMMENT);
}
sb.append(DbMapSqlConstants.SPACE);
sb.append(DbMapSqlConstants.RIGHT_BRACKET);
}
}
}
/*
* for addition conditions
*/
// like as input.newcolumn1>100
List<String> whereAddition = new ArrayList<String>();
// olny pure start with group or order, like as order/group by input.newcolumn1
// List<String> byAddition = new ArrayList<String>();
// like as input.newcolumn1>100 group/oder by input.newcolumn1
// List<String> containWhereAddition = new ArrayList<String>();
// like as "OR/AND input.newcolumn1", will keep original
List<String> originalWhereAddition = new ArrayList<String>();
List<String> otherAddition = new ArrayList<String>();
if (outputTable != null) {
List<ExternalDbMapEntry> customWhereConditionsEntries = outputTable.getCustomWhereConditionsEntries();
if (customWhereConditionsEntries != null) {
for (ExternalDbMapEntry entry : customWhereConditionsEntries) {
String exp = initExpression(component, entry);
if (exp != null && !DbMapSqlConstants.EMPTY.equals(exp.trim())) {
// } else
if (containWith(exp, DbMapSqlConstants.OR, true) || containWith(exp, DbMapSqlConstants.AND, true)) {
originalWhereAddition.add(exp);
} else {
whereAddition.add(exp);
}
}
}
}
List<ExternalDbMapEntry> customOtherConditionsEntries = outputTable.getCustomOtherConditionsEntries();
if (customOtherConditionsEntries != null) {
for (ExternalDbMapEntry entry : customOtherConditionsEntries) {
String exp = initExpression(component, entry);
if (exp != null && !DbMapSqlConstants.EMPTY.equals(exp.trim())) {
otherAddition.add(exp);
}
}
}
}
String whereClauses = sbWhere.toString();
boolean whereFlag = whereClauses.trim().length() > 0;
boolean whereAddFlag = !whereAddition.isEmpty();
boolean whereOriginalFlag = !originalWhereAddition.isEmpty();
if (whereFlag || whereAddFlag || whereOriginalFlag) {
sb.append(DbMapSqlConstants.NEW_LINE).append(tabSpaceString);
sb.append(DbMapSqlConstants.WHERE);
}
if (whereFlag) {
sb.append(whereClauses);
}
if (whereAddFlag) {
for (int i = 0; i < whereAddition.size(); i++) {
if (i == 0 && whereFlag || i > 0) {
sb.append(DbMapSqlConstants.NEW_LINE).append(tabSpaceString);
sb.append(DbMapSqlConstants.SPACE);
sb.append(DbMapSqlConstants.AND);
}
sb.append(DbMapSqlConstants.SPACE);
sb.append(whereAddition.get(i));
}
}
if (whereOriginalFlag) {
for (String s : originalWhereAddition) {
sb.append(DbMapSqlConstants.NEW_LINE);
sb.append(DbMapSqlConstants.SPACE);
sb.append(s);
}
}
if (!otherAddition.isEmpty()) {
sb.append(DbMapSqlConstants.NEW_LINE).append(tabSpaceString);
for (String s : otherAddition) {
sb.append(s);
sb.append(DbMapSqlConstants.NEW_LINE).append(tabSpaceString);
}
}
}
String sqlQuery = sb.toString();
if (DEFAULT_TAB_SPACE_STRING.equals(tabSpaceString)) {
List<String> contextList = getContextList(component);
boolean haveReplace = false;
for (String context : contextList) {
if (sqlQuery.contains(context)) {
//$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
sqlQuery = sqlQuery.replaceAll("\\b" + context + "\\b", "\" +" + context + "+ \"");
haveReplace = true;
}
if (queryColumnsName.contains(context)) {
//$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
queryColumnsName = queryColumnsName.replaceAll("\\b" + context + "\\b", "\" +" + context + "+ \"");
}
}
if (!haveReplace) {
List<String> connContextList = getConnectionContextList(component);
for (String context : connContextList) {
if (sqlQuery.contains(context)) {
//$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
sqlQuery = sqlQuery.replaceAll("\\b" + context + "\\b", "\" +" + context + "+ \"");
}
if (queryColumnsName.contains(context)) {
//$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
queryColumnsName = queryColumnsName.replaceAll("\\b" + context + "\\b", "\" +" + context + "+ \"");
}
}
}
}
sqlQuery = handleQuery(sqlQuery);
queryColumnsName = handleQuery(queryColumnsName);
return sqlQuery;
}
Aggregations