1 23 24 package org.objectweb.jorm.mapper.rdb.generator; 25 26 import org.objectweb.jorm.type.api.PTypeSpace; 27 import org.objectweb.jorm.metainfo.api.PrimitiveElement; 28 import org.objectweb.jorm.metainfo.api.ScalarField; 29 import org.objectweb.jorm.metainfo.api.Class; 30 import org.objectweb.jorm.api.PException; 31 import org.objectweb.jorm.mapper.rdb.metainfo.*; 32 33 import java.util.ArrayList ; 34 import java.util.Iterator ; 35 import java.util.List ; 36 import java.util.HashMap ; 37 import java.util.Map ; 38 39 45 public class RdbGenTable { 46 public RdbGenInfos genInfos = null; 47 50 public String tableName = null; 51 54 public List columns = new ArrayList (); 55 56 59 public List inheritedColumns = new ArrayList (); 60 61 64 66 71 public List colocatedColumns = null; 72 75 public boolean colocatedTable = false; 76 77 public List joins = null; 78 public Map jn2join = null; 79 80 83 public boolean readOnly = false; 84 85 89 public boolean mustGenerateUpdate = true; 90 91 94 public boolean isMainTable; 95 96 public RdbGenTable() { 97 } 98 99 106 public RdbGenTable(RdbExternalTable extTable, 107 RdbGenTable mainTable, 108 Class clazz, 109 RdbGenInfos genInfos) throws PException { 110 111 constructFromTable(extTable, clazz, genInfos); 112 isMainTable = false; 113 readOnly = extTable.isReadOnly(); 114 joins = new ArrayList (); 115 jn2join = new HashMap (); 116 int joinIdx = 0; 117 for (Iterator itj = extTable.getRdbJoins().iterator(); itj.hasNext();) { 118 RdbJoin j = (RdbJoin) itj.next(); 119 RdbGenJoin rgj = new RdbGenJoin(mainTable, this, j, joinIdx); 120 joinIdx++; 121 jn2join.put(j.getName(), rgj); 122 joins.add(rgj); 123 } 124 if ( extTable.getPrimitiveElementMappings() != null) { 125 for (Iterator it = extTable.getPrimitiveElementMappings().iterator(); it.hasNext();) { 126 RdbPrimitiveElementMapping rpem = (RdbPrimitiveElementMapping) it.next(); 127 Iterator entryIt = rpem.getPrimitiveElementByRdbJoin().entrySet().iterator(); 128 if (!entryIt.hasNext()) { 129 continue; 130 } 131 if (rpem.getName() == null) { 132 throw new PException("Primitive element " 133 + ((PrimitiveElement) rpem.getLinkedMO()).getName() 134 + " has a null column name"); 135 } 136 RdbGenColumn rgc = getColumn(rpem.getName()); 137 rgc.pes = new ArrayList (); 138 rgc.joins = new ArrayList (); 139 PrimitiveElement pe = null; 140 while (entryIt.hasNext()) { 141 Map.Entry me = (Map.Entry ) entryIt.next(); 142 RdbJoin j = (RdbJoin) me.getKey(); 143 pe = (PrimitiveElement) me.getValue(); 144 rgc.pes.add(pe); 145 rgc.joins.add(jn2join.get(j.getName())); 146 } 147 rgc.hiddenField = pe instanceof ScalarField; 148 rgc.columnType = pe.getType(); 149 } 150 } 151 } 152 153 159 public RdbGenTable(RdbTable mainTable, Class clazz, RdbGenInfos genInfos) throws PException { 160 constructFromTable(mainTable, clazz, genInfos); 161 isMainTable = true; 162 } 163 164 165 private void constructFromTable(RdbTable table, Class clazz, 166 RdbGenInfos genInfos) throws PException { 167 this.genInfos = genInfos; 168 tableName = table.getName(); 169 colocatedTable = table.isColocated() && !table.isColocatedMaster(); 170 171 for (Iterator it = table.getPrimitiveElementMappings().iterator(); it.hasNext();) { 172 RdbPrimitiveElementMapping rpem = (RdbPrimitiveElementMapping) it.next(); 173 RdbGenColumn rgc = new RdbGenColumn(); 174 rgc.columnName = rpem.getName(); 175 if (rgc.columnName == null) { 176 throw new PException("Primitive element " 177 + ((PrimitiveElement) rpem.getLinkedMO()).getName() 178 + " has a null column name"); 179 } 180 rgc.columnNotNull = rpem.isNotNull(); 181 rgc.table = this; 182 PrimitiveElement pe = (PrimitiveElement) rpem.getLinkedMO(); 183 if (pe != null) { 184 rgc.columnType = pe.getType(); 185 rgc.columnSize = pe.getSize(); 186 rgc.columnScale = pe.getScale(); 187 rgc.hiddenField = pe instanceof ScalarField; 188 if (pe.isConstant()) { 189 rgc.constant = clazz.getConstantValue(pe.getName()); 191 } 192 rgc.fieldName = pe.getName(); 193 } 194 rgc.columnSqlType = rpem.getType(); 195 if (rgc.columnSqlType != null && rgc.columnSqlType.length() == 0) { 196 rgc.columnSqlType = null; 197 } 198 columns.add(rgc); 199 } 200 201 } 202 209 public void fixMustGenerateUpdate() { 210 mustGenerateUpdate = false; 211 for (int i = 0; i < columns.size() && !mustGenerateUpdate; i++) { 213 RdbGenColumn rgc = (RdbGenColumn) columns.get(i); 214 215 if (rgc.joinCol != null) { 217 continue; 218 } 219 220 if (joins == null && genInfos.colInGenId(rgc)) { 222 continue; 223 } 224 mustGenerateUpdate = true; 226 } 227 } 228 229 public boolean getMustGenerateUpdate() { 230 return mustGenerateUpdate; 231 } 232 233 public boolean getReadOnly() { 234 return readOnly; 235 } 236 237 public List getJoins() { 238 return joins; 239 } 240 241 public RdbGenInfos getGenInfos() { 242 return genInfos; 243 } 244 245 public String getTableName() { 246 return tableName; 247 } 248 249 public String getTableNameNoDot() { 250 return tableName.replace('.', '_'); 251 } 252 253 public List getColumns() { 254 return columns; 255 } 256 257 public List getInheritedColumns() { 258 return inheritedColumns; 259 } 260 261 public List getColocatedColumns() { 262 return colocatedColumns; 263 } 264 265 public boolean getColocatedTable() { 266 return colocatedTable; 267 } 268 269 public boolean isMainTable() { 270 return isMainTable; 271 } 272 273 276 private Boolean psMand = null; 277 278 283 public boolean preparedStatementMandatory() { 284 if (psMand == null) { 285 for (int i = 0; i < columns.size(); i++) { 286 if (((RdbGenColumn) columns.get(i)).columnType == PTypeSpace.SERIALIZED) { 287 psMand = new Boolean (true); 288 return true; 289 } 290 } 291 psMand = new Boolean (false); 292 } 293 return psMand.booleanValue(); 294 } 295 296 303 boolean colocatedColumn(RdbGenColumn rgc) { 304 if (colocatedColumns != null) { 305 for (int i = 0; i < colocatedColumns.size(); i++) { 306 if (colocatedColumns.get(i) == rgc) { 307 return true; 308 } 309 } 310 } 311 return false; 312 } 313 320 public RdbGenColumn getColumn(String columnName) { 321 RdbGenColumn rgc = null; 322 for (int i = 0; i < columns.size(); i++) { 323 rgc = (RdbGenColumn) columns.get(i); 324 if (rgc.columnName.equals(columnName)) { 325 return rgc; 326 } 327 } 328 for (int i = 0; i < inheritedColumns.size(); i++) { 329 rgc = (RdbGenColumn) inheritedColumns.get(i); 330 if (rgc.columnName.equals(columnName)) { 331 return rgc; 332 } 333 } 334 335 return null; 336 } 337 } 338 | Popular Tags |