1 10 11 package com.triactive.jdo.store; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 16 17 class FetchStatement 18 { 19 protected final ClassBaseTable mainTable; 20 protected final ArrayList selected = new ArrayList (); 21 protected final ArrayList supertables = new ArrayList (); 22 23 protected StringBuffer conditionList = new StringBuffer (); 24 25 public FetchStatement(ClassBaseTable mainTable) 26 { 27 this.mainTable = mainTable; 28 } 29 30 public int select(Column col) 31 { 32 String columnID = referenceColumn(col); 33 34 if (!selected.contains(columnID)) 35 selected.add(columnID); 36 37 return selected.indexOf(columnID) + 1; 38 } 39 40 public String referenceColumn(Column col) 41 { 42 ClassBaseTable t = (ClassBaseTable)col.getTable(); 43 44 if (!t.equals(mainTable) && !supertables.contains(t)) 45 supertables.add(t); 46 47 return t.getName().toString() + '.' + col.getName().toString(); 48 } 49 50 public void andCondition(String condition) 51 { 52 if (conditionList.length() > 0) 53 conditionList.append(" AND "); 54 55 conditionList.append('(').append(condition).append(')'); 56 } 57 58 public String toString() 59 { 60 StringBuffer stmt = new StringBuffer ("SELECT "); 61 62 Iterator i = selected.iterator(); 63 64 while (i.hasNext()) 65 { 66 stmt.append(i.next()); 67 68 if (i.hasNext()) 69 stmt.append(','); 70 } 71 72 SQLIdentifier mainTableName = mainTable.getName(); 73 String mainTableColumnID = referenceColumn(mainTable.getIDMapping().getColumn()); 74 StringBuffer joinConditions = new StringBuffer (); 75 76 stmt.append(" FROM ").append(mainTableName); 77 78 i = supertables.iterator(); 79 80 while (i.hasNext()) 81 { 82 ClassBaseTable supertable = (ClassBaseTable)i.next(); 83 String supertableColumnID = referenceColumn(supertable.getIDMapping().getColumn()); 84 85 stmt.append(',').append(supertable.getName()); 86 87 if (joinConditions.length() > 0) 88 joinConditions.append(" AND "); 89 90 joinConditions.append(mainTableColumnID).append(" = ").append(supertableColumnID); 91 } 92 93 if (joinConditions.length() > 0) 94 { 95 if (conditionList.length() > 0) 96 conditionList = joinConditions.append(" AND (").append(conditionList).append(')'); 97 else 98 conditionList = joinConditions; 99 } 100 101 if (conditionList.length() > 0) 102 stmt.append(" WHERE ").append(conditionList); 103 104 return stmt.toString(); 105 } 106 } 107 | Popular Tags |