use of org.talend.designer.dbmap.external.data.ExternalDbMapData in project tdi-studio-se by Talend.
the class OracleGenerationManager method buildSqlSelect.
/**
*
* ggu Comment method "buildSqlSelect".
*
* @param component
* @param outputTableName
* @param tabSpaceString
* @return
*/
@Override
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);
// for (IMetadataColumn column : columns) {
// if (expression != null && column.getLabel().equals(dbMapEntry.getName())) {
// expression = expression.replaceFirst("." + dbMapEntry.getName(), //$NON-NLS-1$
// "." + column.getOriginalDbColumnName()); //$NON-NLS-1$
// break;
// }
// }
boolean added = false;
if (!DEFAULT_TAB_SPACE_STRING.equals(this.tabSpaceString)) {
expression += DbMapSqlConstants.SPACE + DbMapSqlConstants.AS + DbMapSqlConstants.SPACE + getAliasOf(dbMapEntry.getName());
added = true;
}
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);
if (component.getMapperMain() == null) {
component.getExternalEmfData();
}
if (component.getMapperMain() != null) {
MapperManager mapperManager = component.getMapperMain().getMapperManager();
DataMapExpressionParser dataMapExpressionParser = new DataMapExpressionParser(mapperManager.getCurrentLanguage());
TableEntryLocation[] tableEntriesLocationsSources = dataMapExpressionParser.parseTableEntryLocations(expression);
boolean columnChanged = false;
if (tableEntriesLocationsSources.length > 1) {
columnChanged = true;
} else {
for (TableEntryLocation tableEntriesLocationsSource : tableEntriesLocationsSources) {
TableEntryLocation location = tableEntriesLocationsSource;
String entryName = getAliasOf(dbMapEntry.getName());
if (location != null && entryName != null && !entryName.startsWith("_") && !entryName.equals(location.columnName)) {
//$NON-NLS-1$
columnChanged = true;
}
}
}
if (!added && columnChanged) {
sb.append(DbMapSqlConstants.SPACE + DbMapSqlConstants.AS + DbMapSqlConstants.SPACE + getAliasOf(dbMapEntry.getName()));
}
}
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;
}
use of org.talend.designer.dbmap.external.data.ExternalDbMapData in project tdi-studio-se by Talend.
the class PostgresGenerationManager method initExpression.
@Override
protected String initExpression(DbMapComponent component, ExternalDbMapEntry dbMapEntry) {
String expression = super.initExpression(component, dbMapEntry);
// String expression = dbMapEntry.getExpression();
if (expression != null) {
List<? extends IConnection> inputConnections = component.getIncomingConnections();
ExternalDbMapData data = (ExternalDbMapData) component.getExternalData();
if (inputConnections == null) {
return expression;
}
for (ExternalDbMapTable input : data.getInputTables()) {
ExternalDbMapTable inputTable = input;
IConnection connection = null;
for (IConnection iconn : inputConnections) {
if (iconn.getName().equals(input.getTableName())) {
connection = iconn;
break;
}
}
if (connection == null) {
return expression;
}
INode source = connection.getSource();
String schemaStr = "";
String tableNameStr = "";
if (source != null) {
if (isELTDBMap(source)) {
tableNameStr = connection.getName();
} else {
IElementParameter schema = source.getElementParameter("ELT_SCHEMA_NAME");
IElementParameter tableName = source.getElementParameter("ELT_TABLE_NAME");
if (schema != null && schema.getValue() != null) {
schemaStr = TalendTextUtils.removeQuotes(schema.getValue().toString());
}
if (tableName != null && tableName.getValue() != null) {
tableNameStr = TalendTextUtils.removeQuotes(tableName.getValue().toString());
}
}
}
for (IMetadataColumn co : connection.getMetadataTable().getListColumns()) {
String columnLabel = co.getOriginalDbColumnName();
if (columnLabel == null || "".equals(columnLabel)) {
columnLabel = co.getLabel();
}
String[] patternSubs = getPattenSubs(schemaStr, tableNameStr, columnLabel);
MapExpressionParser parser = new MapExpressionParser(patternSubs[0]);
expression = parser.replaceLocation(expression, patternSubs[0], patternSubs[1]);
if (inputTable.getAlias() != null && !"".equals(inputTable.getAlias())) {
patternSubs = getPattenSubs("", inputTable.getAlias(), columnLabel);
parser = new MapExpressionParser(patternSubs[0]);
expression = parser.replaceLocation(expression, patternSubs[0], patternSubs[1]);
}
}
}
}
List<IMetadataTable> metadataList = component.getMetadataList();
if (metadataList != null) {
for (IMetadataTable table : metadataList) {
String tableName = table.getLabel();
for (IMetadataColumn column : table.getListColumns()) {
String columnLabel = column.getOriginalDbColumnName();
if (columnLabel == null || "".equals(columnLabel)) {
columnLabel = column.getLabel();
}
String[] patternSubs = getPattenSubs("", tableName, columnLabel);
MapExpressionParser parser = new MapExpressionParser(patternSubs[0]);
expression = parser.replaceLocation(expression, patternSubs[0], patternSubs[1]);
}
}
}
return expression;
}
use of org.talend.designer.dbmap.external.data.ExternalDbMapData in project tdi-studio-se by Talend.
the class DbGenerationManagerTest method setUp.
@Before
public void setUp() throws Exception {
dbMapComponent = new DbMapComponent();
ExternalDbMapData externalDbMapData = new ExternalDbMapData();
dbMapComponent.setExternalData(externalDbMapData);
List<ExternalDbMapTable> inputTables = new ArrayList<>();
externalDbMapData.setInputTables(inputTables);
ExternalDbMapTable externalDbMapTable = new ExternalDbMapTable();
inputTables.add(externalDbMapTable);
externalDbMapTable.setName("t1");
externalDbMapTable.setAlias("t1");
externalDbMapTable.setTableName("t1");
externalDbMapTable.setJoinType("NO_JOIN");
tableEntries = new ArrayList<>();
externalDbMapTable.setMetadataTableEntries(tableEntries);
extMapEntry = new ExternalDbMapEntry("id", "t1.id");
tableEntries.add(extMapEntry);
List<IConnection> incomingConnections = new ArrayList<IConnection>();
String[] columns = new String[] { "\\\"id\\\"" };
incomingConnections.add(createConnection("t1", "t1", "id", columns));
dbMapComponent.setIncomingConnections(incomingConnections);
dbManager = new GenericDbGenerationManager();
}
use of org.talend.designer.dbmap.external.data.ExternalDbMapData in project tdi-studio-se by Talend.
the class ELTMapSaveToEmfMigrationTask method execute.
/*
* (non-Javadoc)
*
* @see org.talend.core.model.migration.AbstractItemMigrationTask#execute(org.talend.core.model.properties.Item)
*/
@Override
public ExecutionResult execute(Item item) {
IProxyRepositoryFactory factory = CorePlugin.getDefault().getRepositoryService().getProxyRepositoryFactory();
ProcessType processType = getProcessType(item);
boolean modified = false;
if (processType != null) {
for (Object obj : processType.getNode()) {
NodeType nodeType = (NodeType) obj;
StringReader stringReader = null;
if (nodeType.getStringData() != null && !"".equals(nodeType.getStringData())) {
stringReader = new StringReader(nodeType.getStringData());
}
final String componentName = nodeType.getComponentName();
if (stringReader != null) {
if (componentName != null && componentName.startsWith("tELT") && componentName.endsWith("Map")) {
modified = true;
StringBuilder input = null;
Unmarshaller unmarshaller = new Unmarshaller(ExternalDbMapData.class);
unmarshaller.setWhitespacePreserve(true);
try {
BufferedReader r = new BufferedReader(stringReader);
input = new StringBuilder();
String line;
while ((line = r.readLine()) != null) {
input.append(line);
//$NON-NLS-0$
input.append("\r\n");
}
//$NON-NLS-0$ //$NON-NLS-1$
String buf = input.toString().replaceAll("<table-entries .*?>.*?</table-entries>", "");
stringReader.close();
stringReader = new StringReader(buf);
ExternalDbMapData externalData = (ExternalDbMapData) unmarshaller.unmarshal(stringReader);
DBMapData emfMapperData = DbmapFactory.eINSTANCE.createDBMapData();
DBMapHelper.saveDataToEmf(emfMapperData, externalData);
nodeType.setNodeData(emfMapperData);
nodeType.setStringData("");
} catch (MarshalException e) {
ExceptionHandler.process(e);
return ExecutionResult.FAILURE;
} catch (ValidationException e) {
ExceptionHandler.process(e);
return ExecutionResult.FAILURE;
} catch (IOException e) {
ExceptionHandler.process(e);
return ExecutionResult.FAILURE;
} finally {
if (stringReader != null) {
stringReader.close();
}
}
}
}
}
}
try {
if (modified) {
factory.save(item, true);
return ExecutionResult.SUCCESS_WITH_ALERT;
} else {
return ExecutionResult.SUCCESS_NO_ALERT;
}
} catch (Exception e) {
ExceptionHandler.process(e);
return ExecutionResult.FAILURE;
}
}
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