1 2 12 package com.versant.core.jdbc; 13 14 import com.versant.core.common.Debug; 15 import com.versant.core.metadata.FetchGroup; 16 import com.versant.core.metadata.FetchGroupField; 17 import com.versant.core.metadata.FieldMetaData; 18 19 import java.util.ArrayList ; 20 import java.util.List ; 21 22 import com.versant.core.common.BindingSupportImpl; 23 import com.versant.core.jdbc.metadata.JdbcClass; 24 import com.versant.core.jdbc.metadata.JdbcFetchGroup; 25 26 31 public class FgDs { 32 33 public static String INCLUDING_SUBCLASSES = "INCLUDING_SUBCLASSES"; 34 public static String INCLUDING_SUBCLASSES_OUTER = "INCLUDING_SUBCLASSES_OUTER"; 35 public static String EXCLUDING_SUBCLASSES = "EXCLUDING_SUBCLASSES"; 36 public static String EXCLUDING_SUBCLASSES_OUTER = "EXCLUDING_SUBCLASSES_OUTER"; 37 38 public JoinStructure valueJs; 39 public JoinStructure keyJs; 40 43 public FgDs owner; 44 47 public FetchGroup fg; 48 51 private FgDs[] sameTableFgDsArray; 52 55 private List sameTableFgDsCol; 56 59 private List refFgDsCol; 60 64 public int columnSkipCount; 65 private boolean clsIdAdded; 66 67 public String name; 68 69 private boolean finished; 70 71 private JoinStructure rootValueJoinStruct; 72 public FieldMetaData refFmd; 73 74 77 private String sql; 78 private String sqlForUpdate; 79 private boolean refIdFieldsAdded; 80 81 private int opts; 82 83 public FgDs(FgDs owner, FetchGroup fg, String name, int opts) { 84 this.name = name; 85 this.owner = owner; 86 this.fg = fg; 87 columnSkipCount = fg.jdbcTotalCols; 88 89 sameTableFgDsCol = new ArrayList (); 90 refFgDsCol = new ArrayList (); 91 this.opts = opts; 92 } 93 94 public int getOpts() { 95 return opts; 96 } 97 98 public String getSql(boolean forUpdate) { 99 if (forUpdate) return sqlForUpdate; 100 return sql; 101 } 102 103 public void setSql(String sql, boolean forUpdate) { 104 if (forUpdate) { 105 sqlForUpdate = sql; 106 } else { 107 this.sql = sql; 108 } 109 } 110 111 public String toString() { 112 return this.getClass().getName() + "@" + System.identityHashCode(this) + " opts: " + opts; 113 } 114 115 public boolean isJoinOk() { 116 if (owner == null) { 117 return (opts & JdbcFetchGroup.OPT_START_OUTER) == 0; 118 } else { 119 return owner.isJoinOk(); 120 } 121 } 122 123 public boolean isOuter() { 124 return (opts & JdbcFetchGroup.OPT_START_OUTER) != 0; 125 } 126 127 public boolean isIncludingSubs() { 128 return (opts & JdbcFetchGroup.OPT_INC_SUBS) != 0; 129 } 130 131 public JoinStructure getJoinStruct() { 132 if (Debug.DEBUG) { 133 if (owner != null) { 134 throw BindingSupportImpl.getInstance().internal( 135 "This may only be called on the root"); 136 } 137 } 138 if (rootValueJoinStruct == null) { 139 rootValueJoinStruct = new JoinStructure(fg); 140 } 141 return rootValueJoinStruct; 142 } 143 144 public void addRefIdFields() { 145 if (finished) { 146 throw BindingSupportImpl.getInstance().internal("The FgDs is already finished"); 147 } 148 if (refIdFieldsAdded) { 149 throw BindingSupportImpl.getInstance().internal("The refIdFields is already added"); 150 } 151 refIdFieldsAdded = true; 152 columnSkipCount += ((JdbcClass)fg.classMetaData.storeClass).table.pk.length; 153 } 154 155 public boolean isRefIdFieldsAdded() { 156 return refIdFieldsAdded; 157 } 158 159 162 public void addClsIdCount() { 163 if (finished) { 164 throw BindingSupportImpl.getInstance().internal("The FgDs is already finished"); 165 } 166 if (clsIdAdded) { 167 throw BindingSupportImpl.getInstance().internal("ClsId already added"); 168 } 169 clsIdAdded = true; 170 columnSkipCount++; 171 } 172 173 public void addClsIdCount(int amount) { 174 if (finished) { 175 throw BindingSupportImpl.getInstance().internal("The FgDs is already finished"); 176 } 177 if (clsIdAdded) { 178 throw BindingSupportImpl.getInstance().internal("ClsId already added"); 179 } 180 clsIdAdded = true; 181 columnSkipCount += amount; 182 } 183 184 public void addSameTable(FgDs sameTableFgDs) { 185 if (finished) { 186 throw BindingSupportImpl.getInstance().internal("The FgDs is already finished"); 187 } 188 sameTableFgDsCol.add(sameTableFgDs); 189 } 190 191 194 public void updateCounts() { 195 if (finished) { 196 throw BindingSupportImpl.getInstance().internal("The FgDs is already finished"); 197 } 198 finished = true; 199 for (int i = 0; i < sameTableFgDsCol.size(); i++) { 200 FgDs fgDs = (FgDs)sameTableFgDsCol.get(i); 201 fgDs.updateCounts(); 202 columnSkipCount += fgDs.columnSkipCount; 203 } 204 205 for (int i = 0; i < refFgDsCol.size(); i++) { 206 FgDs fgDs = (FgDs)refFgDsCol.get(i); 207 fgDs.updateCounts(); 208 columnSkipCount += fgDs.columnSkipCount; 209 } 210 211 sameTableFgDsArray = new FgDs[sameTableFgDsCol.size()]; 212 sameTableFgDsCol.toArray(sameTableFgDsArray); 213 sameTableFgDsCol = null; 214 if (owner == null) { 215 ((JdbcFetchGroup)fg.storeFetchGroup).setFgDs(this); 216 } 217 if (rootValueJoinStruct != null) rootValueJoinStruct.finish(); 218 } 219 220 public void addReference(FgDs refFgDs, FieldMetaData aFmd) { 221 if (finished) { 222 throw BindingSupportImpl.getInstance().internal("The FgDs is already finished"); 223 } 224 refFgDsCol.add(refFgDs); 225 refFgDs.refFmd = aFmd; 226 } 227 228 public boolean isEmpty() { 229 return refFgDsCol.isEmpty(); 230 } 231 232 238 public FgDs get(int index) { 239 if (index >= refFgDsCol.size()) { 240 return null; 241 } 242 return (FgDs)refFgDsCol.get(index); 243 } 244 245 public void dump(String indent) { 246 if (!Debug.DEBUG) return; 247 System.out.println( 248 indent + "fg name = " + fg + "@" + System.identityHashCode( 249 this) + " class " + fg.classMetaData.cls.getName() + " finished = " + finished); 250 System.out.println(indent + "name = " + name); 251 System.out.println( 252 indent + "TotcolCount " + columnSkipCount + " ownColCount " + fg.jdbcTotalCols); 253 StringBuffer sb = new StringBuffer (); 254 for (int i = 0; i < fg.fields.length; i++) { 255 FetchGroupField field = fg.fields[i]; 256 sb.append(field.fmd.name + ", "); 257 } 258 System.out.println(indent + "fg fields = " + sb.toString()); 259 System.out.println(indent + "refIdFieldsAdded = " + refIdFieldsAdded); 260 System.out.println(indent + "listing children-----"); 261 if (sameTableFgDsCol != null) { 262 System.out.println( 263 indent + sameTableFgDsCol.size() + " same table fgs ----"); 264 for (int i = 0; i < sameTableFgDsCol.size(); i++) { 265 FgDs ds = (FgDs)sameTableFgDsCol.get(i); 266 ds.dump(indent + " "); 267 } 268 } else { 269 System.out.println( 270 indent + sameTableFgDsArray.length + " same table fgs ----"); 271 for (int i = 0; i < sameTableFgDsArray.length; i++) { 272 FgDs fgDs = sameTableFgDsArray[i]; 273 fgDs.dump(indent + " "); 274 } 275 } 276 System.out.println(indent + refFgDsCol.size() + " ref fgs ----"); 277 for (int i = 0; i < refFgDsCol.size(); i++) { 278 com.versant.core.jdbc.FgDs fgDs = (com.versant.core.jdbc.FgDs)refFgDsCol.get( 279 i); 280 fgDs.dump(indent + " "); 281 } 282 System.out.println(indent + "---------------------------------"); 283 } 284 285 public int getChildrenColumnCount() { 286 return columnSkipCount - fg.jdbcTotalCols - (clsIdAdded ? 1 : 0); 287 } 288 289 public FgDs[] getSameTableFgDss() { 290 if (!finished) { 291 throw BindingSupportImpl.getInstance().internal("This fgds was never finised"); 292 } 293 return sameTableFgDsArray; 294 } 295 296 public int getAmountOfRefFgDs() { 297 return refFgDsCol.size(); 298 } 299 300 public FgDs getFirtsFor(FetchGroup afg) { 301 if (!finished) { 302 throw BindingSupportImpl.getInstance().internal("This fgds was never finised"); 303 } 304 for (int i = 0; i < sameTableFgDsCol.size(); i++) { 305 FgDs fgDs = (FgDs)sameTableFgDsCol.get(i); 306 if (fgDs.fg == afg) return fgDs; 307 } 308 return null; 309 } 310 311 public boolean isFinished() { 312 return finished; 313 } 314 315 318 public List getPass2FetchGroupFields() { 319 ArrayList result = new ArrayList (); 320 addPass2Fields(result, fg); 321 322 if (sameTableFgDsArray != null) { 323 for (int i = 0; i < sameTableFgDsArray.length; i++) { 324 addPass2Fields(result, sameTableFgDsArray[i].fg); 325 } 326 } 327 return result; 328 } 329 330 private static void addPass2Fields(ArrayList result, FetchGroup aFg) { 331 FetchGroupField[] fields = aFg.fields; 332 for (int i = 0; i < fields.length; i++) { 333 FetchGroupField field = fields[i]; 334 if (field.fmd.secondaryField) { 335 result.add(field); 336 } 337 } 338 } 339 340 public List getRefFgs() { 341 return refFgDsCol; 342 } 343 } 344 | Popular Tags |