1 25 26 package org.objectweb.jorm.mapper.rdb.lib; 27 28 import java.util.ArrayList ; 29 import java.util.Arrays ; 30 import java.util.Collection ; 31 import java.util.Comparator ; 32 import java.util.HashSet ; 33 import java.util.Iterator ; 34 35 import org.objectweb.jorm.api.PClassMapping; 36 import org.objectweb.jorm.mapper.rdb.adapter.api.RdbAdapter; 37 import org.objectweb.jorm.mapper.rdb.metainfo.RdbJoin; 38 import org.objectweb.jorm.mapper.rdb.metainfo.RdbTable; 39 import org.objectweb.jorm.metainfo.api.Class; 40 import org.objectweb.util.monolog.api.Logger; 41 42 47 public class RdbExtentMappingInfos { 48 49 public static final String NULL_COLUMN = "NULL"; 50 51 public static Logger logger; 52 public static boolean debug = false; 53 54 Class clazz; 56 57 private RdbColumnInfo[] columns; 59 60 HashSet joins = new HashSet (); RdbTable mainTable; 64 65 HashSet filters = new HashSet (); boolean addFilters = false; 71 boolean hasUnmappedFields = false; 72 private boolean emptyEMI = true; 73 74 private String name; 76 77 RdbExtentMappingInfos(Class clazz, String name, int nbColumns) { 78 this.clazz = clazz; 79 this.name = name; 80 this.columns = new RdbColumnInfo[nbColumns]; 81 } 83 84 87 public String getName() { 88 return name; 89 } 90 91 94 public void setName(String name) { 95 this.name = name; 96 } 97 98 public HashSet getFilters() { 100 return filters; 101 } 102 103 public RdbTable getMainTable() { 104 return mainTable; 105 } 106 107 108 public void setMainTable(RdbTable mainTable) { 109 this.mainTable = mainTable; 110 } 111 112 public HashSet getJoins() { 113 return joins; 114 } 115 116 public void addJoin(RdbJoin join){ 117 joins.add(join); 118 } 119 120 public boolean isEmpty() { 121 return emptyEMI; 122 } 123 124 public void addProjection(String tableName, String columnName, 125 String aliasName, String className, int position) { 126 if (columns[position] != null && columns[position].getName() != null && !columns[position].getName().equals(NULL_COLUMN)) { 127 if (debug) { 128 String newExpr = tableName + "." + columnName; 129 if (!columns[position].getName().equals(newExpr)) { 130 throw new InternalError ("expr in select definition (" + newExpr 131 + ") not equal to old one (" + columns[position].getName() + ")"); 132 } 133 if (!columns[position].getAlias().equals(aliasName)) { 134 throw new InternalError ("alias in select definition (" + aliasName 135 + ") not equal to old one (" + columns[position].getAlias() + ")"); 136 } 137 } 138 } else { 139 emptyEMI = false; 140 columns[position] = new RdbColumnInfo(columnName.equals(NULL_COLUMN)?columnName:tableName + "." + columnName, 143 aliasName, 144 className); 145 } 146 } 147 148 156 public String getSelectParameter(String adapterName) { 157 StringBuffer sb = new StringBuffer (); 158 for (int pos = 0; pos < columns.length; pos++) { 160 sb.append("\"" + columns[pos].getName() + "\" + " + 161 adapterName +".getColumnAliasExpr(\"" + columns[pos].getAlias() + "\")"); 162 if (pos < columns.length - 1) { 163 sb.append(" + \",\" + "); 164 } 165 } 166 return sb.toString(); 167 } 168 169 174 public String getSelectPrefetch(RdbAdapter adapter, Class cl) { 175 RdbColumnInfo[] columnsClone = new RdbColumnInfo[columns.length]; 176 for(int i = 0; i < columns.length; i++){ 177 columnsClone[i] = columns[i]; 178 } 179 if (cl.isPolymorphic()) { 181 Arrays.sort(columnsClone); 183 } else { 184 Arrays.sort(columnsClone, new Comparator () { 186 public int compare(Object o1, Object o2) { 187 return ((RdbColumnInfo) o1).getName().compareTo( 188 ((RdbColumnInfo) o2).getName()); 189 } 190 }); 191 } 192 StringBuffer sb = new StringBuffer (); 193 for (int pos = 0; pos < columnsClone.length; pos++) { 195 sb.append(columnsClone[pos].getName() + " " + 196 adapter.getColumnAliasExpr(columnsClone[pos].getAlias())); 197 if (pos < columnsClone.length - 1) { 198 sb.append(" , "); 199 } 200 } 201 return sb.toString(); 202 } 203 204 211 public String getSelectPKOnly(RdbAdapter adapter, String primaryKey) { 212 RdbColumnInfo[] columnsClone = new RdbColumnInfo[columns.length]; 213 for(int i = 0; i < columns.length; i++){ 214 columnsClone[i] = columns[i]; 215 } 216 Arrays.sort(columnsClone); 218 StringBuffer sb = new StringBuffer (); 219 for (int pos = 0; pos < columnsClone.length; pos++) { 221 if(primaryKey.indexOf(columnsClone[pos].getName()) != -1){ 222 sb.append(columnsClone[pos].getName() + " " + 223 adapter.getColumnAliasExpr(columnsClone[pos].getAlias())); 224 if (pos < columnsClone.length - 1) { 225 sb.append(" , "); 226 } 227 } 228 } 229 return sb.toString(); 230 } 231 232 236 public String getSelectPKAndFields(RdbAdapter adapter, Class cl) { 237 RdbColumnInfo[] columnsClone = new RdbColumnInfo[columns.length]; 238 for(int i = 0; i < columns.length; i++){ 239 columnsClone[i] = columns[i]; 240 } 241 if (cl.isPolymorphic()) { 243 Arrays.sort(columnsClone); 244 } else { 245 Arrays.sort(columnsClone, new Comparator () { 246 public int compare(Object o1, Object o2) { 247 return ((RdbColumnInfo) o1).getName().compareTo( 248 ((RdbColumnInfo) o2).getName()); 249 } 250 }); 251 } 252 StringBuffer sb = new StringBuffer (); 253 Collection superClasses = new ArrayList (); 254 superClasses.add(cl.getFQName()); 257 Iterator it = cl.getAncestors().iterator(); 259 while(it.hasNext()){ 261 Class c = (Class ) it.next(); 262 superClasses.add(c.getFQName()); 263 } 264 for (int pos = 0; pos < columnsClone.length; pos++) { 265 if (superClasses.contains(columnsClone[pos].getClassName())) { 268 sb.append(columnsClone[pos].getName() + " " + 269 adapter.getColumnAliasExpr(columnsClone[pos].getAlias())); 270 if (pos < columnsClone.length - 1) { 271 sb.append(" , "); 272 } 273 } 274 } 275 return sb.toString(); 276 } 277 278 public String getWhereParameter(PClassMapping pcm) { 279 StringBuffer sb = new StringBuffer (); 280 if(pcm != null && pcm instanceof RdbPPolymorphicClass){ 281 if (this.hasJoins()) { 282 sb.append(" AND ("); 283 ((RdbPPolymorphicClass)pcm).appendExtentFilters(this.name, sb); 284 sb.append(")"); 285 } else { 286 sb.append(" WHERE "); 287 ((RdbPPolymorphicClass)pcm).appendExtentFilters(this.name, sb); 288 } 289 } 290 return sb.toString(); 291 } 292 293 public String getAliasName(int position){ 294 if (position >= 0 && position < columns.length && columns[position] != null) 295 return columns[position].getAlias(); 296 else 297 return null; 298 } 299 300 310 public Collection getColumnAliases(boolean prefetch, String primaryKey, Class cl){ 311 if (prefetch) { 312 return getColumnAliasesPrefetch(); 313 } else { 314 if(cl == null) 315 return getColumnAliasesPKOnly(primaryKey); 316 else 317 return getColumnAliasesPKAndFields(cl); 318 } 319 } 320 321 private Collection getColumnAliasesPrefetch(){ 322 RdbColumnInfo[] columnsClone = new RdbColumnInfo[columns.length]; 323 for(int i = 0; i < columns.length; i++){ 324 columnsClone[i] = columns[i]; 325 } 326 Arrays.sort(columnsClone); 328 Collection c = new ArrayList (); 329 for(int i=0; i< columnsClone.length; i++) 330 c.add(columnsClone[i].getAlias()); 331 return c; 332 } 333 334 private Collection getColumnAliasesPKOnly(String primaryKey){ 335 RdbColumnInfo[] columnsClone = new RdbColumnInfo[columns.length]; 336 for(int i = 0; i < columns.length; i++){ 337 columnsClone[i] = columns[i]; 338 } 339 Arrays.sort(columnsClone); 341 Collection c = new ArrayList (); 342 for(int i=0; i< columnsClone.length; i++) 343 if(primaryKey.indexOf(columnsClone[i].getName()) != -1) 344 c.add(columnsClone[i].getAlias()); 345 return c; 346 } 347 348 public Collection getColumnAliasesPKAndFields(Class cl){ 349 RdbColumnInfo[] columnsClone = new RdbColumnInfo[columns.length]; 350 for(int i = 0; i < columns.length; i++){ 351 columnsClone[i] = columns[i]; 352 } 353 Arrays.sort(columnsClone); 355 Collection c = new ArrayList (); 356 Collection superClasses = new ArrayList (); 357 superClasses.add(cl.getFQName()); 360 Iterator it = cl.getAncestors().iterator(); 362 while(it.hasNext()){ 364 Class currentClass = (Class ) it.next(); 365 superClasses.add(currentClass.getFQName()); 366 } 367 for (int pos = 0; pos < columnsClone.length; pos++) { 368 if (superClasses.contains(columnsClone[pos].getClassName())) { 371 c.add(columnsClone[pos].getAlias()); 372 } 373 } 374 return c; 375 } 376 377 public Collection getColumnClasses(){ 378 RdbColumnInfo[] columnsClone = new RdbColumnInfo[columns.length]; 379 for(int i = 0; i < columns.length; i++){ 380 columnsClone[i] = columns[i]; 381 } 382 Arrays.sort(columnsClone); 384 ArrayList columnClasses = new ArrayList (); 385 for(int i = 0; i < columnsClone.length; i++){ 386 columnClasses.add(columnsClone[i].getClassName()); 387 } 388 return columnClasses; 389 } 390 391 public boolean mappingDone(int pos) { 392 return (columns[pos] != null && columns[pos].getName() != null); 393 } 394 395 public boolean mappingNull(int pos) { 396 return (columns[pos] == null || columns[pos].getName().equals(NULL_COLUMN)); 397 } 398 399 public boolean hasJoins() { 400 return (!joins.isEmpty()); 401 } 402 } | Popular Tags |