/**
  * Returns the "select" SQL statement based on terms and source attributes
  *
  * @param tableName the source name of this relation
  * @return "select" SQL statement
  */
 public ArrayList<String> getSelect(String tableName) {
   // System.out.println("P in getSelect():" + this + "***" +
   // this.hashCode());
   ArrayList<String> select = new ArrayList<String>();
   for (int i = 0; i < source.getTerms().size(); i++) {
     Term t = source.getTerms().get(i);
     if (t.getVar() == null) continue;
     SourceAttribute sourceAttr = source.getSourceAttribute(i);
     // I'm adding a space at then end, so it's easier when I have to
     // search for the var name
     // otherwise if I search for "S" it will return true for any
     // variable that starts with "S"
     select.add(tableName + "." + sourceAttr.getSQLName() + " as " + t.getVar() + " ");
   }
   return select;
 }
 /**
  * Returns the "where" SQL statement based on terms and sourceAttrs. <br>
  * If the term is a constant or it is a bound variable that is satisfied by a constant <br>
  * a comparison is added to the where clause
  *
  * @param tableName the source name of this relation
  * @param child the child of the DataAccessNode that represents this relation
  * @return "where" SQL statement
  * @throws MediatorException
  */
 public ArrayList<String> getWhere(String tableName, SimpleBaseNode child)
     throws MediatorException {
   ArrayList<String> where = new ArrayList<String>();
   for (int i = 0; i < source.getTerms().size(); i++) {
     Term t = source.getTerms().get(i);
     SourceAttribute sourceAttr = source.getSourceAttribute(i);
     boolean isBound = sourceAttr.needsBinding();
     // System.out.println("Pred=" + name + " SourceAttr is bound " +
     // sourceAttr + " " + isBound);
     String bindMarker = "";
     if (isBound) {
       bindMarker = "@";
     }
     if (t instanceof ConstTerm) {
       String val = ((ConstTerm) t).getSqlVal(sourceAttr.isNumber());
       if (val.equals(MediatorConstants.NULL_VALUE))
         where.add(bindMarker + tableName + "." + sourceAttr.getSQLName() + " IS " + val);
       else where.add(bindMarker + tableName + "." + sourceAttr.getSQLName() + " = " + val);
     }
     if (t instanceof FunctionTerm) {
       if (child != null) {
         // I have attribute binding patterns in the function
         where.add(
             bindMarker
                 + tableName
                 + "."
                 + sourceAttr.getSQLName()
                 + " = "
                 + child.getActualName(((FunctionTerm) t).getFunction()));
       } else {
         // the function has only constants as input
         where.add(
             bindMarker
                 + tableName
                 + "."
                 + sourceAttr.getSQLName()
                 + " = "
                 + ((FunctionTerm) t).getFunctionForSQL());
       }
     }
   }
   for (int i = 0; i < source.getTerms().size(); i++) {
     Term t = source.getTerms().get(i);
     SourceAttribute sourceAttr1 = source.getSourceAttribute(i);
     // System.out.println("Find duplicate terms:" + t);
     if (t instanceof VarTerm) {
       // System.out.println("Not Constant...");
       int j = i + 1;
       while (j > 0) {
         j = source.findTerm(t, j);
         if (j > 0) {
           SourceAttribute sourceAttr2 = source.getSourceAttribute(j);
           where.add(
               tableName
                   + "."
                   + sourceAttr1.getSQLName()
                   + " = "
                   + tableName
                   + "."
                   + sourceAttr2.getSQLName());
           j++;
         }
       }
     }
   }
   return where;
 }