Hi,
 
 I'm new to iBatis and am trying to figure out the best way to dynamically
 build a query that requires you to add both entries to the from clause
 and the where clause.  Below is an example of the sort of thing I'm
 trying to accomplish.
 
 Any help would be appreciated.
 
 Thanks, John
 
 import java.util.*;
 public class ItemFinder {
   private List<ItemAttribute> attributeList;
   public ItemFinder() {
     this.attributeList = new ArrayList<ItemAttribute>();
   }
   public void addItemAttribute(ItemAttribute itemAttribute) {
     this.attributeList.add(itemAttribute);
   }
   public List<Item> getItemList() {
     List<Item> itemList = new ArrayList<Item>();
     StringBuffer queryBuf = new StringBuffer();
     StringBuffer fromBuf = new StringBuffer();
     StringBuffer whereBuf = new StringBuffer();
     queryBuf.append("select it.*");
     fromBuf.append(" from item it ");
     whereBuf.append(" where ");
     Iterator<ItemAttribute> it = attributeList.iterator();
     int idx = 0;
     while (it.hasNext()) {
       ItemAttribute itemAttr = it.next();
       if (idx > 0) {
         fromBuf.append(", ");
         whereBuf.append(" and ");
       }
       fromBuf.append(itemAttr.getFrom(idx));
       whereBuf.append(itemAttr.getWhere(idx));
       idx++;
     }
     queryBuf.append(fromBuf.toString() + whereBuf.toString());
     //Get Connection
     //Execute query & build list from result of query
     return itemList;
   }
 }
 
 import java.util.*;
 public class ItemAttribute {
   private String attributeName;
   private float attributeValue;
   public ItemAttribute(String name, float val) {
     this.attributeName = name;
     this.attributeValue = val;
   }
   public String getFrom(int idx) {
     return "attribute attr" + idx + ", item_attribute itemAttr" + idx;
   }
   public String getWhere(int idx) {
     return "attr" + idx + ".attribute_name = '" + this.attributeName + "'" +
            "attr" + idx + ".attribute_id = itemAttr" + idx + ".attribute_id " +
            " and itemAttr" + idx + ".item_id = it.item_id and itemAttr" +
            idx + ".attribute_value = " + this.attributeValue;
   }
 }

____________________________________________________________
FREE 3D EARTH SCREENSAVER - Watch the Earth right on your desktop!
Check it out at http://www.inbox.com/earth

Reply via email to