1 23 24 package org.objectweb.jorm.mapper.rdb.generator; 25 26 import org.objectweb.jorm.type.api.PType; 27 import org.objectweb.jorm.api.PException; 28 import org.objectweb.jorm.metainfo.api.PrimitiveElement; 29 import org.objectweb.jorm.mapper.rdb.adapter.api.RdbAdapter; 30 31 import java.util.List ; 32 33 38 public class RdbGenColumn { 39 42 public String fieldName = null; 43 46 public boolean hiddenField = true; 47 48 52 public String constant; 53 54 57 public String columnName = null; 58 62 public int columnSize = RdbAdapter.NOSIZE; 63 public int columnScale = RdbAdapter.NOSIZE; 64 67 public PType columnType = null; 68 71 public boolean columnNotNull = false; 72 75 public boolean columnInPK = false; 76 79 public RdbGenTable table = null; 80 81 85 public String columnSqlType = null; 86 87 91 public RdbGenColumn joinCol = null; 92 93 98 public List joins = null; 99 public List pes = null; 100 101 public RdbGenColumn getJoinCol() { 102 return joinCol; 103 } 104 105 public String getColumnSqlType() { 106 return columnSqlType; 107 } 108 109 public String getFieldName() { 110 return fieldName; 111 } 112 113 public String getFieldName(RdbGenJoin rgj) throws PException { 114 if (rgj != null && joins != null) { 115 int idx = joins.indexOf(rgj); 116 if (idx == -1) { 117 throw new PException("No field found for the column " 118 + columnName + " with the join " + rgj); 119 } 120 return ((PrimitiveElement) pes.get(idx)).getName(); 121 } 122 return fieldName; 123 } 124 125 public String getConstant() { 126 return constant; 127 } 128 129 public List getJoins() { 130 return joins; 131 } 132 133 public boolean isHiddenField() { 134 return hiddenField; 135 } 136 137 public String getColumnName() { 138 return columnName; 139 } 140 141 public PType getColumnType() { 142 return columnType; 143 } 144 145 public boolean isColumnNotNull() { 146 return columnNotNull; 147 } 148 149 public boolean isColumnInPK() { 150 return columnInPK; 151 } 152 153 public RdbGenTable getTable() { 154 return table; 155 } 156 157 public String getColumnNotNull() { 158 return "" + columnNotNull; 159 } 160 161 public String getColumnInPK() { 162 return "" + columnInPK(); 163 } 164 165 public boolean columnInPK() { 166 if (table.isMainTable) { 167 return table.genInfos.colInGenId(this); 168 } else if (table.colocatedTable) { 169 return false; 170 } else { 171 return this.joinCol != null; 172 } 173 } 174 175 181 public String getSqlType(String adapter) throws PException { 182 if (columnSqlType != null) { 183 return "\"" + columnSqlType + "\""; 184 } else { 185 StringBuffer sb = new StringBuffer (adapter); 186 sb.append(".getSqlType("); 187 sb.append(columnType.getProgName()); 188 sb.append(".getTypeCode(), "); 189 sb.append(columnInPK()); 190 sb.append(", "); 191 sb.append(columnSize); 192 sb.append(", "); 193 sb.append(columnScale); 194 sb.append(")"); 195 return sb.toString(); 196 } 197 } 198 199 214 public String getSqlGet(String adapter, 215 String resultset, 216 String pos, 217 String nullValue) throws PException { 218 StringBuffer sb = new StringBuffer (adapter); 219 sb.append("."); 220 sb.append(getAdapterGet(columnType)); 221 sb.append("("); 222 sb.append(resultset); 223 sb.append(", "); 224 sb.append(pos); 225 switch (columnType.getTypeCode()) { 226 case PType.TYPECODE_DATE: 227 sb.append(", "); 228 if (columnSqlType != null && columnSqlType.length() > 0) { 229 sb.append("\""); 230 } 231 sb.append(columnSqlType); 232 if (columnSqlType != null && columnSqlType.length() > 0) { 233 sb.append("\""); 234 } 235 break; 236 } 237 sb.append(", "); 238 if (nullValue != null && nullValue.length()>0) { 239 sb.append(nullValue); 240 } else { 241 sb.append(getNullValue()); 242 } 243 sb.append(")"); 244 return sb.toString(); 245 } 246 247 public String getNullValue() { 248 switch (columnType.getTypeCode()) { 249 case PType.TYPECODE_BOOLEAN: 250 return "false"; 251 case PType.TYPECODE_CHAR: 252 return "((" + columnType.getJavaName() + ") 0)"; 253 case PType.TYPECODE_BYTE: 254 case PType.TYPECODE_SHORT: 255 case PType.TYPECODE_INT: 256 case PType.TYPECODE_LONG: 257 case PType.TYPECODE_FLOAT: 258 case PType.TYPECODE_DOUBLE: 259 return "((" + columnType.getJavaName() + ") -1)"; 260 default: 261 return "null"; 262 } 263 } 264 265 266 284 public String getSqlSet(String adapter, 285 String pstmt, 286 String value, 287 int pos) throws PException { 288 StringBuffer sb = new StringBuffer (adapter); 289 sb.append("."); 290 sb.append(getAdapterSet(columnType)); 291 sb.append("("); 292 sb.append(pstmt); 293 sb.append(", "); 294 sb.append(pos); 295 sb.append(", "); 296 sb.append(value); 297 switch (columnType.getTypeCode()) { 298 case PType.TYPECODE_DATE: 299 sb.append(", "); 300 if (columnSqlType != null && columnSqlType.length() > 0) { 301 sb.append("\""); 302 } 303 sb.append(columnSqlType); 304 if (columnSqlType != null && columnSqlType.length() > 0) { 305 sb.append("\""); 306 } 307 break; 308 } 309 sb.append(")"); 310 return sb.toString(); 311 } 312 313 public String getAdapterGet(PType type) { 314 return "get" + getAdapterSuffix(type); 315 } 316 public String getAdapterSet(PType type) { 317 return "set" + getAdapterSuffix(type); 318 } 319 320 private String getAdapterSuffix(PType type) { 321 switch (type.getTypeCode()) { 322 case PType.TYPECODE_BYTEARRAY: 323 return "ByteArray"; 324 case PType.TYPECODE_BOOLEAN: 325 return "Boolean"; 326 case PType.TYPECODE_OBJBOOLEAN: 327 return "Oboolean"; 328 case PType.TYPECODE_FLOAT: 329 return "Float"; 330 case PType.TYPECODE_OBJFLOAT: 331 return "Ofloat"; 332 case PType.TYPECODE_DOUBLE: 333 return "Double"; 334 case PType.TYPECODE_OBJDOUBLE: 335 return "Odouble"; 336 case PType.TYPECODE_SERIALIZED: 337 return "Serialized"; 338 default: 339 return type.getCodingName(); 340 } 341 } 342 343 public String getSqlTypeCode(PType pt) throws PException { 344 StringBuffer sb = new StringBuffer ("adapter.getSqlTypeCode("); 345 sb.append(pt.getProgName()); 346 sb.append(".getTypeCode(), "); 347 if (columnSqlType != null && columnSqlType.length() > 0) { 348 sb.append("\""); 349 } 350 sb.append(columnSqlType); 351 if (columnSqlType != null && columnSqlType.length() > 0) { 352 sb.append("\""); 353 } 354 sb.append(")"); 355 return sb.toString(); 356 } 357 358 public String getAccessorValue(RdbGenInfos genInfos, 359 boolean isSpecific) throws Exception { 360 return getAccessorValue(genInfos, isSpecific, null); 361 } 362 363 public String getAccessorValue(RdbGenInfos genInfos, 364 boolean isSpecific, RdbGenJoin rgj) throws Exception { 365 366 if (hiddenField) { 367 if (genInfos.colInGenId(this)) { 368 throw new PException("unmanaged case"); 369 } else { 370 RdbGenRef gen_ref = genInfos.getGenRefOfColumn(this, rgj); 371 if (isSpecific) { 372 return "paccessor.paGet" 373 + RdbGenInfos.commonHelper.upperFL(gen_ref.fieldName) 374 + "(conn)"; 375 } else { 376 return "paccessorGen.paGetRefField(" 377 + gen_ref.fieldName + "_NAME, conn)"; 378 } 379 } 380 } else { 381 if (isSpecific) { 382 return "paccessor.paGet" 383 + RdbGenInfos.commonHelper.upperFL(getFieldName(rgj)) 384 + "()"; 385 } else { 386 return "paccessorGen." + 387 RdbGenInfos.commonHelper.getPaGetMethod(columnType) 388 + "(" + getFieldName(rgj) + "_NAME)"; 389 } 390 } 391 } 392 393 public String getRefValue(RdbGenInfos genInfos, 394 boolean isSpecific) throws Exception { 395 return getRefValue(genInfos, isSpecific, null); 396 } 397 398 public String getRefValue(RdbGenInfos genInfos, 399 boolean isSpecific, RdbGenJoin rgj) throws Exception { 400 try { 401 if (!hiddenField) { 402 throw new PException("unmanaged case"); 404 } 405 RdbGenRef gen_ref = null; 406 if (genInfos.colInGenId(this)) { 407 gen_ref = genInfos.genId; 409 if (gen_ref.refColumn != null) { 410 return "pname.encode" + columnType.getCodingName() + "()"; 412 } else { 413 return "_pngId.pnGet" + 416 RdbGenInfos.commonHelper.upperFL(gen_ref.getFieldName(this)) 417 + "(null)"; 418 } 419 } else { 420 gen_ref = genInfos.getGenRefOfColumn(this, rgj); 423 if (gen_ref == null) { 424 System.out.println("Pb in getRefValue of column " + getColumnName()); 425 return "JORM_GENERATION_ERROR"; 426 } 427 if (gen_ref.refColumn != null) { 429 return gen_ref.fieldName + "Pnc.encode" + columnType.getCodingName() 433 + "(" + getAccessorValue(genInfos, isSpecific, rgj) + ")"; 434 } else { 435 return "png" + gen_ref.cnId + ".pnGet" 438 + RdbGenInfos.commonHelper.upperFL( 439 gen_ref.getFieldName(this)) 440 + "(null)"; 441 } 442 443 } 444 } catch (Exception e) { 445 e.printStackTrace(); 446 System.exit(-1); 447 throw e; 448 } 449 } 450 451 public String getColumnName(String tableSuffix) { 452 return table.tableName 453 + (tableSuffix == null ? "" : tableSuffix) 454 + "." + columnName; 455 } 456 457 public boolean isInJoin(RdbGenJoin rgj) { 458 return (joins != null && joins.contains(rgj)) 459 || rgj.getJoinColumnsInExt().contains(this); 460 461 } 462 463 public boolean equals(RdbGenColumn rgc) { 464 if (rgc == null) 465 return false; 466 return ( (columnName == null ? rgc.columnName == null : columnName.equals(rgc.columnName)) 467 && (table == null ? rgc.table == null : table.tableName.equals(rgc.table.tableName)) 468 && (columnSqlType == null ? rgc.columnSqlType == null : columnSqlType.equals(rgc.columnSqlType)) 469 && (columnType == rgc.columnType) 470 && (fieldName == null ? rgc.fieldName == null : fieldName.equals(rgc.fieldName)) 471 && (hiddenField == rgc.hiddenField) 472 && (joinCol == null ? rgc.joinCol == null : joinCol.equals(rgc.joinCol)) 473 && (columnInPK == rgc.columnInPK) 474 && (columnNotNull == rgc.columnNotNull) 475 && (columnScale == rgc.columnScale) 476 && (constant == null ? rgc.constant == null : constant.equals(rgc.constant))); 477 } 478 } 479 | Popular Tags |