Program: 123398
Project: 123398
Buggy source:
/** 
 * ggu Comment method "buildSqlSelect".
 * @param component
 * @param outputTableName
 * @return
 */
public String buildSqlSelect(DbMapComponent component,String outputTableName){
  queryColumnsName="String_Node_Str";
  aliasAlreadyDeclared.clear();
  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());
      }
    }
    sb.append("String_Node_Str");
    sb.append(DbMapSqlConstants.SELECT);
    sb.append(DbMapSqlConstants.NEW_LINE);
    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);
        if (!isPostgres()) {
          expression=addQuoteForSpecialChar(expression,component);
        }
        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();
          sb.append(Messages.getString("String_Node_Str",str));
          sb.append(DbMapSqlConstants.RIGHT_COMMENT);
        }
      }
    }
    sb.append(DbMapSqlConstants.NEW_LINE);
    sb.append(DbMapSqlConstants.FROM);
    List<ExternalDbMapTable> inputTables=data.getInputTables();
    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);
    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);
          }
          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 {
          buildTableDeclaration(component,sb,inputTable,false,false,true);
          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);
            sb.append(Messages.getString("String_Node_Str"));
            sb.append(DbMapSqlConstants.SPACE);
            sb.append(DbMapSqlConstants.RIGHT_COMMENT);
          }
          sb.append(DbMapSqlConstants.SPACE);
          sb.append(DbMapSqlConstants.RIGHT_BRACKET);
        }
      }
    }
    List<String> whereAddition=new ArrayList<String>();
    List<String> byAddition=new ArrayList<String>();
    List<String> containWhereAddition=new ArrayList<String>();
    List<String> originalWhereAddition=new ArrayList<String>();
    if (outputTable != null) {
      List<ExternalDbMapEntry> customConditionsEntries=outputTable.getCustomConditionsEntries();
      if (customConditionsEntries != null) {
        for (        ExternalDbMapEntry entry : customConditionsEntries) {
          String exp=entry.getExpression();
          if (exp != null && !DbMapSqlConstants.EMPTY.equals(exp.trim())) {
            if (containWith(exp,DbMapSqlConstants.GROUP_BY_PATTERN,true) || containWith(exp,DbMapSqlConstants.ORDER_BY_PATTERN,true)) {
              byAddition.add(exp);
            }
 else             if (containWith(exp,DbMapSqlConstants.GROUP_BY_PATTERN,false) || containWith(exp,DbMapSqlConstants.ORDER_BY_PATTERN,false)) {
              containWhereAddition.add(exp);
            }
 else             if (containWith(exp,DbMapSqlConstants.OR,true) || containWith(exp,DbMapSqlConstants.AND,true)) {
              originalWhereAddition.add(exp);
            }
 else {
              whereAddition.add(exp);
            }
          }
        }
      }
    }
    String whereClauses=sbWhere.toString();
    boolean whereFlag=whereClauses.trim().length() > 0;
    boolean whereAddFlag=!whereAddition.isEmpty();
    boolean whereConntainFlag=!containWhereAddition.isEmpty();
    boolean whereOriginalFlag=!originalWhereAddition.isEmpty();
    if (whereFlag || whereAddFlag || whereConntainFlag|| whereOriginalFlag) {
      sb.append(DbMapSqlConstants.NEW_LINE);
      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);
          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 (whereConntainFlag) {
      for (int i=0; i < containWhereAddition.size(); i++) {
        if (i == 0 && (whereFlag || whereAddFlag) || i > 0) {
          sb.append(DbMapSqlConstants.NEW_LINE);
          sb.append(DbMapSqlConstants.SPACE);
          sb.append(DbMapSqlConstants.AND);
        }
        sb.append(DbMapSqlConstants.SPACE);
        sb.append(containWhereAddition.get(i));
      }
    }
    if (!byAddition.isEmpty()) {
      sb.append(DbMapSqlConstants.NEW_LINE);
      for (      String s : byAddition) {
        sb.append(s);
        sb.append(DbMapSqlConstants.NEW_LINE);
      }
    }
  }
  List<String> contextList=getContextList(component);
  String sqlQuery=sb.toString();
  boolean haveReplace=false;
  for (  String context : contextList) {
    if (sqlQuery.contains(context)) {
      sqlQuery=sqlQuery.replace(context,"String_Node_Str" + context + "String_Node_Str");
      haveReplace=true;
    }
    if (queryColumnsName.contains(context)) {
      queryColumnsName=queryColumnsName.replace(context,"String_Node_Str" + context + "String_Node_Str");
    }
  }
  if (!haveReplace) {
    List<String> connContextList=getConnectionContextList(component);
    for (    String context : connContextList) {
      if (sqlQuery.contains(context)) {
        sqlQuery=sqlQuery.replace(context,"String_Node_Str" + context + "String_Node_Str");
      }
      if (queryColumnsName.contains(context)) {
        queryColumnsName=queryColumnsName.replace(context,"String_Node_Str" + context + "String_Node_Str");
      }
    }
  }
  sqlQuery=handleQuery(sqlQuery);
  queryColumnsName=handleQuery(queryColumnsName);
  return sqlQuery;
}

Fixed version (ground truth):
/** 
 * ggu Comment method "buildSqlSelect".
 * @param component
 * @param outputTableName
 * @return
 */
public String buildSqlSelect(DbMapComponent component,String outputTableName){
  queryColumnsName="String_Node_Str";
  aliasAlreadyDeclared.clear();
  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());
      }
    }
    sb.append("String_Node_Str");
    sb.append(DbMapSqlConstants.SELECT);
    sb.append(DbMapSqlConstants.NEW_LINE);
    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);
        if (isOracle()) {
          expression=addQuoteForSpecialChar(expression,component);
        }
        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();
          sb.append(Messages.getString("String_Node_Str",str));
          sb.append(DbMapSqlConstants.RIGHT_COMMENT);
        }
      }
    }
    sb.append(DbMapSqlConstants.NEW_LINE);
    sb.append(DbMapSqlConstants.FROM);
    List<ExternalDbMapTable> inputTables=data.getInputTables();
    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);
    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);
          }
          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 {
          buildTableDeclaration(component,sb,inputTable,false,false,true);
          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);
            sb.append(Messages.getString("String_Node_Str"));
            sb.append(DbMapSqlConstants.SPACE);
            sb.append(DbMapSqlConstants.RIGHT_COMMENT);
          }
          sb.append(DbMapSqlConstants.SPACE);
          sb.append(DbMapSqlConstants.RIGHT_BRACKET);
        }
      }
    }
    List<String> whereAddition=new ArrayList<String>();
    List<String> byAddition=new ArrayList<String>();
    List<String> containWhereAddition=new ArrayList<String>();
    List<String> originalWhereAddition=new ArrayList<String>();
    if (outputTable != null) {
      List<ExternalDbMapEntry> customConditionsEntries=outputTable.getCustomConditionsEntries();
      if (customConditionsEntries != null) {
        for (        ExternalDbMapEntry entry : customConditionsEntries) {
          String exp=entry.getExpression();
          if (exp != null && !DbMapSqlConstants.EMPTY.equals(exp.trim())) {
            if (containWith(exp,DbMapSqlConstants.GROUP_BY_PATTERN,true) || containWith(exp,DbMapSqlConstants.ORDER_BY_PATTERN,true)) {
              byAddition.add(exp);
            }
 else             if (containWith(exp,DbMapSqlConstants.GROUP_BY_PATTERN,false) || containWith(exp,DbMapSqlConstants.ORDER_BY_PATTERN,false)) {
              containWhereAddition.add(exp);
            }
 else             if (containWith(exp,DbMapSqlConstants.OR,true) || containWith(exp,DbMapSqlConstants.AND,true)) {
              originalWhereAddition.add(exp);
            }
 else {
              whereAddition.add(exp);
            }
          }
        }
      }
    }
    String whereClauses=sbWhere.toString();
    boolean whereFlag=whereClauses.trim().length() > 0;
    boolean whereAddFlag=!whereAddition.isEmpty();
    boolean whereConntainFlag=!containWhereAddition.isEmpty();
    boolean whereOriginalFlag=!originalWhereAddition.isEmpty();
    if (whereFlag || whereAddFlag || whereConntainFlag|| whereOriginalFlag) {
      sb.append(DbMapSqlConstants.NEW_LINE);
      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);
          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 (whereConntainFlag) {
      for (int i=0; i < containWhereAddition.size(); i++) {
        if (i == 0 && (whereFlag || whereAddFlag) || i > 0) {
          sb.append(DbMapSqlConstants.NEW_LINE);
          sb.append(DbMapSqlConstants.SPACE);
          sb.append(DbMapSqlConstants.AND);
        }
        sb.append(DbMapSqlConstants.SPACE);
        sb.append(containWhereAddition.get(i));
      }
    }
    if (!byAddition.isEmpty()) {
      sb.append(DbMapSqlConstants.NEW_LINE);
      for (      String s : byAddition) {
        sb.append(s);
        sb.append(DbMapSqlConstants.NEW_LINE);
      }
    }
  }
  List<String> contextList=getContextList(component);
  String sqlQuery=sb.toString();
  boolean haveReplace=false;
  for (  String context : contextList) {
    if (sqlQuery.contains(context)) {
      sqlQuery=sqlQuery.replace(context,"String_Node_Str" + context + "String_Node_Str");
      haveReplace=true;
    }
    if (queryColumnsName.contains(context)) {
      queryColumnsName=queryColumnsName.replace(context,"String_Node_Str" + context + "String_Node_Str");
    }
  }
  if (!haveReplace) {
    List<String> connContextList=getConnectionContextList(component);
    for (    String context : connContextList) {
      if (sqlQuery.contains(context)) {
        sqlQuery=sqlQuery.replace(context,"String_Node_Str" + context + "String_Node_Str");
      }
      if (queryColumnsName.contains(context)) {
        queryColumnsName=queryColumnsName.replace(context,"String_Node_Str" + context + "String_Node_Str");
      }
    }
  }
  sqlQuery=handleQuery(sqlQuery);
  queryColumnsName=handleQuery(queryColumnsName);
  return sqlQuery;
}