1 56 package org.objectstyle.cayenne.map; 57 58 import java.util.ArrayList ; 59 import java.util.HashMap ; 60 import java.util.List ; 61 import java.util.Map ; 62 63 import org.objectstyle.cayenne.CayenneRuntimeException; 64 import org.objectstyle.cayenne.exp.Expression; 65 import org.objectstyle.cayenne.query.Ordering; 66 import org.objectstyle.cayenne.query.Query; 67 68 76 public abstract class QueryBuilder { 77 78 public static final String OBJ_ENTITY_ROOT = "obj-entity"; 79 public static final String DB_ENTITY_ROOT = "db-entity"; 80 public static final String PROCEDURE_ROOT = "procedure"; 81 public static final String DATA_MAP_ROOT = "data-map"; 82 public static final String JAVA_CLASS_ROOT = "java-class"; 83 84 protected String name; 85 protected Map properties; 86 protected List resultColumns; 87 protected String sql; 88 protected Map adapterSql; 89 protected Expression qualifier; 90 protected List orderings; 91 protected List prefetches; 92 protected DataMap dataMap; 93 protected String rootType; 94 protected String rootName; 95 protected String resultType; 96 protected boolean selecting = true; 97 98 101 public abstract Query getQuery(); 102 103 public void setName(String name) { 104 this.name = name; 105 } 106 107 112 protected Object getRoot() { 113 114 if (rootType == null || DATA_MAP_ROOT.equals(rootType)) { 115 return dataMap; 116 } 117 else if (rootName == null) { 118 return null; 119 } 120 else if (OBJ_ENTITY_ROOT.equals(rootType)) { 121 return dataMap.getObjEntity(rootName); 122 } 123 else if (DB_ENTITY_ROOT.equals(rootType)) { 124 return dataMap.getDbEntity(rootName); 125 } 126 else if (PROCEDURE_ROOT.equals(rootType)) { 127 return dataMap.getProcedure(rootName); 128 } 129 else if (JAVA_CLASS_ROOT.equals(rootType)) { 130 return dataMap.getObjEntityForJavaClass(rootName); 133 } 134 135 return null; 136 } 137 138 public void setSelecting(String selecting) { 139 this.selecting = ("false".equalsIgnoreCase(selecting)) ? false : true; 141 } 142 143 public void setResultType(String resultType) { 144 this.resultType = resultType; 145 } 146 147 150 public void setRoot(DataMap dataMap, String rootType, String rootName) { 151 this.dataMap = dataMap; 152 this.rootType = rootType; 153 this.rootName = rootName; 154 } 155 156 160 public void addSql(String sql, String adapterClass) { 161 if (adapterClass == null) { 162 this.sql = sql; 163 } 164 else { 165 if (adapterSql == null) { 166 adapterSql = new HashMap (); 167 } 168 169 adapterSql.put(adapterClass, sql); 170 } 171 } 172 173 public void setQualifier(String qualifier) { 174 if (qualifier == null || qualifier.trim().length() == 0) { 175 this.qualifier = null; 176 } 177 else { 178 this.qualifier = Expression.fromString(qualifier.trim()); 179 } 180 } 181 182 public void addProperty(String name, String value) { 183 if (properties == null) { 184 properties = new HashMap (); 185 } 186 187 properties.put(name, value); 188 } 189 190 public void addResultColumn(String label, String dbType, String objectType) { 191 if (resultColumns == null) { 192 resultColumns = new ArrayList (); 193 } 194 195 resultColumns.add(new ResultColumn(label, dbType, objectType)); 196 } 197 198 public void addOrdering(String path, String descending, String ignoreCase) { 199 if (orderings == null) { 200 orderings = new ArrayList (); 201 } 202 203 if (path != null && path.trim().length() == 0) { 204 path = null; 205 } 206 boolean isDescending = "true".equalsIgnoreCase(descending); 207 boolean isIgnoringCase = "true".equalsIgnoreCase(ignoreCase); 208 orderings.add(new Ordering(path, !isDescending, isIgnoringCase)); 209 } 210 211 public void addPrefetch(String path) { 212 if (path == null || path != null && path.trim().length() == 0) { 213 return; 215 } 216 217 if (prefetches == null) { 218 prefetches = new ArrayList (); 219 } 220 prefetches.add(path.trim()); 221 } 222 223 static class ResultColumn { 224 225 String label; 226 String dbType; 227 String objectType; 228 229 ResultColumn(String label, String dbType, String objectType) { 230 this.label = label; 231 this.dbType = dbType; 232 this.objectType = objectType; 233 } 234 } 235 } | Popular Tags |