1 2 12 package com.versant.core.jdbc; 13 14 import com.versant.core.metadata.generator.StateSrcGenerator; 15 import com.versant.core.metadata.ClassMetaData; 16 import com.versant.core.metadata.FieldMetaData; 17 import com.versant.core.metadata.MDStatics; 18 import com.versant.core.common.BindingSupportImpl; 19 import com.versant.core.compiler.ClassSpec; 20 import com.versant.core.jdbc.metadata.*; 21 22 import java.util.List ; 23 import java.util.Iterator ; 24 import java.util.ArrayList ; 25 import java.math.BigInteger ; 26 import java.sql.ResultSet ; 27 import java.sql.PreparedStatement ; 28 import java.sql.SQLException ; 29 30 33 public class JdbcStateGenerator extends StateSrcGenerator { 34 35 public JdbcStateGenerator() { 36 super(); 37 } 38 39 public ClassSpec generateState(ClassMetaData cmd) { 40 ClassSpec spec = super.generateState(cmd); 41 42 spec.addImport(JdbcState.class.getName()); 43 spec.addImport(JdbcOID.class.getName()); 44 spec.addImport(ResultSet .class.getName()); 45 spec.addImport(PreparedStatement .class.getName()); 46 spec.addImport(SQLException .class.getName()); 47 spec.addImport(JdbcColumn.class.getName()); 48 spec.addImport(JdbcUtils.class.getName()); 49 spec.addImport(JdbcField.class.getName()); 50 spec.addImport(JdbcPolyRefField.class.getName()); 51 spec.addImport(JdbcSimpleField.class.getName()); 52 53 spec.addInterface("JdbcState"); 54 55 addJdbcHelpers(); 56 addCopyPass1Fields(); addSetOracleStyleLOBs(); 58 addCopyPass1Fields2(); 59 addSetOptimisticLockingParams(); addSetParams(); addSetParamsChangedAndNotNull(); 63 return spec; 64 } 65 66 protected void addInitStaticsBody(StringBuffer buf) { 67 super.addInitStaticsBody(buf); 68 buf.append("\t\tif (cmd.storeClass == null) return true;\n"); 70 FieldMetaData[] fields = cmd.stateFields; 71 boolean first = true; 72 for (int j = 0; j < fields.length; j++) { 73 JdbcField f = (JdbcField)fields[j].storeField; 74 if (f instanceof JdbcSimpleField) { 75 JdbcSimpleField sf = (JdbcSimpleField)f; 76 if (sf.col.converter != null) { 77 if (first) { 78 buf.append("\t\tFieldMetaData[] fields = cmd.stateFields;\n"); 79 buf.append("\t\tJdbcSimpleField sf;\n"); 80 first = false; 81 } 82 buf.append("\t\tsf = (JdbcSimpleField)fields[" + j + "].storeField;\n"); 83 buf.append("\t\t" + StateSrcGenerator.JDBC_CONVERTER_FIELD_PREFIX + j + 84 " = (" + sf.col.converter.getClass().getName() + ")sf.col.converter;\n"); 85 buf.append("\t\tjdbcCol_" + j + " = sf.col;\n"); 86 } 87 } 88 } 89 } 90 91 private void addJdbcHelpers() { 92 spec.addMethod( 93 "private OID getPolyRefOID(\n" + 94 " FieldMetaData fmd,\n" + 95 " ResultSet rs,\n" + 96 " int firstCol) throws SQLException {\n" + 97 " return getPolyRefOID((JdbcField)fmd.storeField, rs, firstCol);\n" + 98 "}"); 99 spec.addMethod( 100 "private OID getPolyRefOID(\n" + 101 " JdbcField f,\n" + 102 " ResultSet rs,\n" + 103 " int firstCol)\n" + 104 " throws SQLException {\n" + 105 " JdbcPolyRefField pf =\n" + 106 " (JdbcPolyRefField)f;\n" + 107 " return pf.getData(rs, firstCol);\n" + 108 "}"); 109 spec.addMethod( 110 "private int setPolyRefData(\n" + 111 " FieldMetaData fmd,\n" + 112 " OID oid,\n" + 113 " ClassMetaData cmd,\n" + 114 " PreparedStatement ps,\n" + 115 " int firstParam) throws SQLException {\n" + 116 " return setPolyRefData((JdbcField)fmd.storeField, oid, cmd, ps, firstParam);\n" + 117 "}"); 118 spec.addMethod( 119 "public static int setPolyRefData(\n" + 120 " JdbcField f,\n" + 121 " OID oid,\n" + 122 " ClassMetaData cmd,\n" + 123 " PreparedStatement ps,\n" + 124 " int firstParam) throws SQLException {\n" + 125 " JdbcPolyRefField pf =\n" + 126 " (JdbcPolyRefField)f;\n" + 127 " return pf.setData(ps, firstParam, oid);\n" + 128 "}"); 129 } 130 131 132 private void addSetOracleStyleLOBs() { 133 StringBuffer buf = new StringBuffer (); 134 buf.append("\n\tpublic final void setOracleStyleLOBs(ResultSet rs, int stateFieldNos[], int numFieldNos, int firstCol) throws SQLException {\n"); 135 List oracleStyleList = getOracleStyleLOBFieldsMetaData(); 136 if (!oracleStyleList.isEmpty()) { 137 buf.append("\t\tfor (int i = 0; i < numFieldNos; i++) {\n"); 138 buf.append("\t\t\tswitch (stateFieldNos[i]) {\n"); 139 for (Iterator iter = oracleStyleList.iterator();iter.hasNext();) { 140 FieldMetaData fmd = (FieldMetaData) iter.next(); 141 int fieldNo = fmd.stateFieldNo; 142 buf.append("\t\t\t\tcase "+ fieldNo +":\n"); 143 buf.append("\t\t\t\t\tjdbcConverter_"+ fieldNo +".set(rs, firstCol++, jdbcCol_"+ fieldNo +", _"+ fieldNo +");\n"); 144 buf.append("\t\t\t\t\tbreak;\n\n"); 145 } 146 buf.append("\t\t\t}\n"); 147 buf.append("\t\t}\n"); 148 } 149 buf.append("\t}\n"); 150 spec.addMethod(buf.toString()); 151 } 152 153 private void addCopyPass1Fields() { 154 StringBuffer buf = new StringBuffer (); 155 buf.append("\n\tpublic final void copyPass1Fields(ResultSet rs, FetchGroup fetchGroup, int firstCol) throws SQLException {\n"); 156 buf.append("\t\tint fgn[] = fetchGroup.stateFieldNos;\n"); 157 buf.append("\t\tfor (int i = 0; i < fgn.length; i++) {\n"); 158 buf.append("\t\t\tswitch (fgn[i]) {\n"); 159 List pass1List = getPass1FieldsMetaData(); 160 for (Iterator iterator = pass1List.iterator(); iterator.hasNext();) { 161 FieldMetaData fmd = (FieldMetaData) iterator.next(); 162 int fieldNo = fmd.stateFieldNo; 163 buf.append("\t\t\t\tcase "+ fieldNo +":\n"); 164 getCopyPass1FieldfromSQL(fmd,fieldNo, buf); 165 } 166 buf.append("\t\t\t\tdefault:\n"); 167 buf.append("\t\t\t\t\tbreak;\n\n"); 168 buf.append("\t\t\t}\n"); 169 buf.append("\t\t}\n"); 170 buf.append("\t}\n"); 171 spec.addMethod(buf.toString()); 172 } 173 174 175 private void addCopyPass1Fields2() { 176 StringBuffer buf = new StringBuffer (); 177 buf.append("\n\tpublic final void copyPass1Fields(ResultSet rs, JdbcField fields[]) {\n"); 178 List pass1List = getPass1FieldsMetaData(); 179 if (!pass1List.isEmpty()){ 180 buf.append("\t\tJdbcField field = null;\n"); 181 buf.append("\t\ttry {\n"); 182 buf.append("\t\t\tfor (int i = 0; i < fields.length; i++) {\n"); 183 buf.append("\t\t\t\tfield = fields[i];\n"); 184 buf.append("\t\t\t\tif (field != null && cmd.stateFields[field.stateFieldNo] == field.fmd) {\n"); 185 buf.append("\t\t\t\t\tswitch (field.stateFieldNo) {\n"); 186 for (Iterator iter = pass1List.iterator(); iter.hasNext();) { 187 FieldMetaData fmd = (FieldMetaData) iter.next(); 188 int fieldNo = fmd.stateFieldNo; 189 buf.append("\t\t\t\t\t\tcase "+ fieldNo +":\n"); 190 getCopyPass1FieldfromSQL2(fmd, fieldNo, buf); 191 } 192 buf.append("\t\t\t\t\t}\n"); 193 buf.append("\t\t\t\t}\n"); 194 buf.append("\t\t\t}\n"); 195 buf.append("\t\t} catch (SQLException e) {\n"); 196 buf.append("\t\t\tthrow com.versant.core.common.BindingSupportImpl.getInstance().datastore(\"Error reading field \" + field.fmd.getQName() + \" from ResultSet: \" + e, e);\n"); 197 buf.append("\t\t}\n"); 198 } 199 buf.append("\t}\n"); 200 spec.addMethod(buf.toString()); 201 } 202 203 private void getCopyPass1FieldfromSQL2(FieldMetaData fmd, int fieldNo, 204 StringBuffer buf) { 205 JdbcField f = (JdbcField)fmd.storeField; 206 if (f instanceof JdbcSimpleField) { 207 JdbcColumn c = ((JdbcSimpleField) f).col; 208 Class fieldType = c.javaType; 209 boolean isPrim = c.javaType.isPrimitive(); 210 if (c.converter != null) { if (isExternalized(fmd) || isPrimitiveArray(fmd)) { 213 buf.append("\t\t\t\t\t\t\t_" + fieldNo + " = jdbcConverter_" + fieldNo + ".get(rs, (i + 1), jdbcCol_" + fieldNo + ");\n"); 214 } else if (isPrim) { 215 String wrapper = (String ) primativeTypesToWrapper.get(fieldType); 216 String toValue = (String ) wrapperStringToValue.get(wrapper); 217 buf.append("\t\t\t\t\t\t\t_" + fieldNo + " = ((" + wrapper + ")jdbcConverter_" + fieldNo + ".get(rs, (i + 1), jdbcCol_" + fieldNo + "))." + toValue + "();\n"); 218 } else { 219 buf.append("\t\t\t\t\t\t\t_" + fieldNo + " = (" + fieldType.getName() + ")jdbcConverter_" + fieldNo + ".get(rs, (i + 1), jdbcCol_" + fieldNo + ");\n"); 220 } 221 222 } else { Class prim = (Class ) wrapperTypesToPrimative.get(fieldType); 224 if (prim != null) { String rsGet = (String ) typeToResultSetGetField.get(prim); 230 buf.append("\t\t\t\t\t\t\t_" + fieldNo + " = new " + fieldType.getName() + "(rs." + rsGet + "(i + 1));\n"); 231 buf.append("\t\t\t\t\t\t\tif (rs.wasNull()) {\n"); 232 buf.append("\t\t\t\t\t\t\t\t_" + fieldNo + " = null;\n"); 233 buf.append("\t\t\t\t\t\t\t}\n"); 234 } else if (fieldType.equals(java.math.BigInteger .class)) { buf.append("\t\t\t\t\t\t\tjava.math.BigDecimal decimal" + fieldNo + " = rs.getBigDecimal(i + 1);\n"); 242 buf.append("\t\t\t\t\t\t\tif (decimal" + fieldNo + " != null) {\n"); 243 buf.append("\t\t\t\t\t\t\t\t_" + fieldNo + " = decimal" + fieldNo + ".toBigInteger();\n"); 244 buf.append("\t\t\t\t\t\t\t} else {\n"); 245 buf.append("\t\t\t\t\t\t\t\t_" + fieldNo + " = null;\n"); 246 buf.append("\t\t\t\t\t\t\t}\n"); 247 } else { 248 if (isExternalized(fmd)) { 249 buf.append("\t\t\t\t\t\t\t_" + fieldNo + " = rs.getObject(i + 1);\n"); 250 } else if ((String ) typeToResultSetGetField.get(fieldType) == null) { 251 buf.append("\t\t\t\t\t\t\t_" + fieldNo + " = (" + fieldType.getName() + ")rs.getObject(i + 1);\n"); 252 } else { 253 String getField = (String ) typeToResultSetGetField.get(fieldType); 254 buf.append("\t\t\t\t\t\t\t_" + fieldNo + " = rs." + getField + "(i + 1);\n"); 255 } 256 } 257 258 } 259 } else if (f instanceof JdbcPolyRefField) { 260 buf.append("\t\t\t\t\t\t\t_" + fieldNo + " = getPolyRefOID(cmd.stateFields[" + fieldNo + "], rs, (i + 1));\n"); 261 } else if (f instanceof JdbcRefField) { 262 JdbcRefField rf = (JdbcRefField) f; 263 String oidName = rf.targetClass.oidClassName; 264 boolean isInHier = rf.targetClass.isInHeirachy(); 265 if (isInHier) { 266 buf.append("\t\t\t\t\t\t\t_" + fieldNo + " = new " + oidName + "(cmd.jmd.classes[" + rf.targetClass.index + "], false);\n"); 267 } else { 268 buf.append("\t\t\t\t\t\t\t_" + fieldNo + " = new " + oidName + "();\n"); 269 } 270 buf.append("\t\t\t\t\t\t\tif (!((JdbcOID)_" + fieldNo + ").copyKeyFields(rs, (i + 1))) {\n"); 271 buf.append("\t\t\t\t\t\t\t\t_" + fieldNo + " = null;\n"); 272 buf.append("\t\t\t\t\t\t\t}\n"); 273 } 274 buf.append("\t\t\t\t\t\t\t" + getFilledFieldName(fieldNo) + " |= " + getFieldIndex(fieldNo) + ";\n"); 275 buf.append("\t\t\t\t\t\t\tbreak;\n\n"); 276 } 277 278 private String getConverterSet(JdbcSimpleField field) { 279 int fieldNo = field.stateFieldNo; 280 JdbcColumn c = field.col; 281 boolean isPrim = c.javaType.isPrimitive(); 282 String wrapper = null; 283 if (isPrim) { 284 wrapper = (String ) primativeTypesToWrapper.get(c.javaType); 285 } 286 String con = "jdbcConverter_" + fieldNo + ".set(ps, firstParam++, jdbcCol_" + fieldNo + ", " + 287 (isPrim ? "new " + wrapper + "(_" + fieldNo + ")" : "_" + fieldNo + "") + ");"; 288 return con; 289 } 290 291 292 293 private void getCopyPass1FieldfromSQL(FieldMetaData fmd, int fieldNo, 294 StringBuffer buf) { 295 JdbcField f = (JdbcField)fmd.storeField; 296 if (f instanceof JdbcSimpleField) { 297 JdbcColumn c = ((JdbcSimpleField) f).col; 298 Class fieldType = c.javaType; 299 boolean isPrim = c.javaType.isPrimitive(); 300 if (c.converter != null) { if (isExternalized(fmd) || isPrimitiveArray(fmd)) { 303 buf.append("\t\t\t\t\t_"+ fieldNo +" = jdbcConverter_"+ fieldNo +".get(rs, firstCol++, jdbcCol_"+ fieldNo +");\n"); 304 } else if (isPrim) { 305 String wrapper = (String ) primativeTypesToWrapper.get(fieldType); 306 String toValue = (String ) wrapperStringToValue.get(wrapper); 307 buf.append("\t\t\t\t\t_" + fieldNo + " = (("+ wrapper +")jdbcConverter_" + fieldNo + ".get(rs, firstCol++, jdbcCol_" + fieldNo + "))."+ toValue +"();\n"); 308 } else { 309 buf.append("\t\t\t\t\t_" + fieldNo + " = ("+ fieldType.getName() +")jdbcConverter_" + fieldNo + ".get(rs, firstCol++, jdbcCol_" + fieldNo + ");\n"); 310 } 311 312 } else { Class prim = (Class )wrapperTypesToPrimative.get(fieldType); 314 if (prim != null) { String rsGet = (String ) typeToResultSetGetField.get(prim); 320 buf.append("\t\t\t\t\t_"+ fieldNo +" = new "+ fieldType.getName() +"(rs."+ rsGet +"(firstCol++));\n"); 321 buf.append("\t\t\t\t\tif (rs.wasNull()) {\n"); 322 buf.append("\t\t\t\t\t\t_"+ fieldNo +" = null;\n"); 323 buf.append("\t\t\t\t\t}\n"); 324 } else if (fieldType.equals(java.math.BigInteger .class)) { buf.append("\t\t\t\t\tjava.math.BigDecimal decimal"+ fieldNo +" = rs.getBigDecimal(firstCol++);\n"); 332 buf.append("\t\t\t\t\tif (decimal"+ fieldNo +" != null) {\n"); 333 buf.append("\t\t\t\t\t\t_"+ fieldNo +" = decimal"+ fieldNo +".toBigInteger();\n"); 334 buf.append("\t\t\t\t\t} else {\n"); 335 buf.append("\t\t\t\t\t\t_"+ fieldNo +" = null;\n"); 336 buf.append("\t\t\t\t\t}\n"); 337 } else { 338 if (isExternalized(fmd)) { 339 buf.append("\t\t\t\t\t_"+fieldNo+" = rs.getObject(firstCol++);\n"); 340 } else if ((String ) typeToResultSetGetField.get(fieldType) == null) { 341 buf.append("\t\t\t\t\t_" + fieldNo + " = ("+ fieldType.getName() +")rs.getObject(firstCol++);\n"); 342 } else { 343 String getField = (String ) typeToResultSetGetField.get(fieldType); 344 buf.append("\t\t\t\t\t_" + fieldNo + " = rs." + getField + "(firstCol++);\n"); 345 } 346 } 347 348 } 349 } else if (f instanceof JdbcPolyRefField) { 350 buf.append("\t\t\t\t\t_"+fieldNo+" = getPolyRefOID(cmd.stateFields["+fieldNo+"], rs, firstCol);\n"); 353 int inc = ((JdbcPolyRefField) f).cols.length; 354 if (inc == 1){ 355 buf.append("\t\t\t\t\tfirstCol++;\n"); 356 } else { 357 buf.append("\t\t\t\t\tfirstCol += " + inc + ";\n"); 358 } 359 360 361 } else if (f instanceof JdbcRefField) { 362 JdbcRefField rf = (JdbcRefField) f; 363 String oidName = rf.targetClass.oidClassName; 364 boolean isInHier = rf.targetClass.isInHeirachy(); 365 if (isInHier) { 366 buf.append("\t\t\t\t\t_"+fieldNo+" = new "+ oidName +"(cmd.jmd.classes["+ rf.targetClass.index +"], false);\n"); 367 } else { 368 buf.append("\t\t\t\t\t_" + fieldNo + " = new " + oidName + "();\n"); 369 } 370 buf.append("\t\t\t\t\tif (!((JdbcOID)_" + fieldNo + ").copyKeyFields(rs, firstCol)) {\n"); 371 buf.append("\t\t\t\t\t\t_" + fieldNo + " = null;\n"); 372 buf.append("\t\t\t\t\t}\n"); 373 int inc = rf.cols.length; 374 if (inc == 1) { 375 buf.append("\t\t\t\t\tfirstCol++;\n"); 376 } else { 377 buf.append("\t\t\t\t\tfirstCol += " + inc + ";\n"); 378 } 379 } 380 buf.append("\t\t\t\t\t"+ getFilledFieldName(fieldNo) +" |= "+ getFieldIndex(fieldNo) +";\n"); 381 buf.append("\t\t\t\t\tbreak;\n\n"); 382 } 383 384 private List getOracleStyleLOBFieldsMetaData() { 385 ArrayList list = new ArrayList (); 386 FieldMetaData[] fields = cmd.stateFields; 387 list.ensureCapacity(fields.length); 388 for (int i = 0; i < fields.length; i++) { 389 FieldMetaData field = fields[i]; 390 if (field.storeField != null && ((JdbcField)field.storeField).isOracleStyleLOB()) { 391 list.add(field); 392 } 393 } 394 return list; 395 } 396 397 private void addSetOptimisticLockingParams() { 398 StringBuffer buf = new StringBuffer (); 399 406 buf.append("\n\tpublic final int setOptimisticLockingParams(PreparedStatement ps, int firstParam) throws SQLException {\n"); 407 JdbcSimpleField f = ((JdbcClass)cmd.storeClass).optimisticLockingField; 408 if (f != null) { 409 int fieldNo = f.stateFieldNo; 410 JdbcColumn c = f.col; 411 Class classType = c.javaType; 412 if (c.converter != null) { buf.append("\t\t"+getConverterSet(f)+"\n"); 414 } else { if (wrapperTypesToPrimative.containsKey(classType)) { Class primType = (Class ) wrapperTypesToPrimative.get(classType); 417 String psSet = (String )typeToPreparedStatementSetField.get(primType); 418 String toValue = (String )wrapperTypesToValue.get(classType); 419 buf.append("\t\tps."+ psSet +"(firstParam++, _"+ fieldNo +"."+ toValue +"());\n"); 420 } else if (classType.equals(BigInteger .class)) { buf.append("\t\tps.setBigDecimal(firstParam++, new BigDecimal(_"+ fieldNo +"));\n"); 422 } else { 423 String psSet = (String )typeToPreparedStatementSetField.get(classType); 424 if (psSet != null) { 425 buf.append("\t\tps."+ psSet +"(firstParam++, _"+ fieldNo +");\n"); 426 } else { 427 buf.append("\t\tps.setObject(firstParam++, _"+ fieldNo +", "+ c.jdbcType +");\n"); 428 } 429 } 430 } 431 432 } 433 buf.append("\t\treturn firstParam;\n"); 434 buf.append("\t}\n"); 435 spec.addMethod(buf.toString()); 436 } 437 438 protected void addCompareToPass1() { 439 StringBuffer buf = new StringBuffer (); 440 buf.append("\n\tpublic final int compareToPass1(com.versant.core.common.State state) {\n"); 441 buf.append("\t\t" + className + " s = ("+className+") state;\n"); 442 int[] masks = new int[getNumOfControlFields()]; 443 int num = cmd.stateFields.length; 444 for (int i = 0; i < num; i++) { 445 if (cmd.stateFields[i].primaryField) { 446 int fieldNum = cmd.stateFields[i].stateFieldNo; 447 masks[getLocalVarIndex(0, fieldNum)] += getFieldIndex(fieldNum); 448 } 449 } 450 int maskLenght = masks.length; 451 for (int i = 0; i < maskLenght; i++) { 452 if (i == 0) { 453 buf.append("\t\tint ans = ((filled0 & "+ masks[i] +") - (s.filled0 & " + masks[i] + "))"); 454 } else { 455 buf.append(" + ((filled"+i+" & "+ masks[i] +") - (s.filled"+i+" & "+ masks[i] +"))"); 456 } 457 458 } 459 buf.append(";\n"); 460 List list = getOracleStyleLOBFieldsMetaData(); 462 if (!list.isEmpty()) { 463 Iterator iter = list.iterator(); 465 while (iter.hasNext()) { 466 FieldMetaData metaData = (FieldMetaData) iter.next(); 467 int fieldNo = metaData.stateFieldNo; 468 buf.append("\t\tif (ans == 0 && ("+ getFilledFieldName(fieldNo) +" & "+ getFieldIndex(fieldNo) +") != 0) {\n"); 477 buf.append("\t\t\tif (_"+ fieldNo +" == null && s._"+ fieldNo +" != null) {\n"); 478 buf.append("\t\t\t\treturn -1;\n"); 479 buf.append("\t\t\t}\n"); 480 buf.append("\t\t\tif (_"+ fieldNo +" != null && s._"+ fieldNo +" == null) {\n"); 481 buf.append("\t\t\t\treturn 1;\n"); 482 buf.append("\t\t\t}\n"); 483 buf.append("\t\t}\n"); 484 } 485 } 486 buf.append("\t\treturn ans;\n"); 487 buf.append("\t}\n"); 488 spec.addMethod(buf.toString()); 489 } 490 491 protected void addSetParams() { 492 StringBuffer buf = new StringBuffer (); 493 buf.append("\n\tpublic final int setParams(PreparedStatement ps, int fieldNos[], int firstFieldNo, int lastFieldNo, int firstParam, com.versant.core.server.PersistGraph pGraph, int tableNo) throws java.sql.SQLException {\n"); 494 buf.append("\t\tfor (; firstFieldNo < lastFieldNo; firstFieldNo++) {\n"); 495 buf.append("\t\t\tswitch (fieldNos[firstFieldNo]) {\n"); 496 List pass1List = getPass1FieldsMetaData(); 497 for (Iterator iter = pass1List.iterator();iter.hasNext();) { 498 FieldMetaData fmd = (FieldMetaData) iter.next(); 499 buf.append("\t\t\t\tcase "+ fmd.stateFieldNo +":\n"); 500 getSetFieldsToSQL_IL_CheckNull(fmd.stateFieldNo, buf); 501 } 502 buf.append("\t\t\t\tdefault:\n"); 503 buf.append("\t\t\t\t\tbreak;\n\n"); 504 buf.append("\t\t\t}\n"); 505 buf.append("\t\t}\n"); 506 buf.append("\t\treturn firstParam;\n"); 507 buf.append("\t}\n"); 508 spec.addMethod(buf.toString()); 509 } 510 511 protected void addSetParamsChangedAndNotNull() { 512 StringBuffer buf = new StringBuffer (); 513 buf.append("\n\tpublic final int setParamsChangedAndNotNull(PreparedStatement ps, int fieldNos[], int firstFieldNo, int lastFieldNo, int firstParam, com.versant.core.server.PersistGraph pGraph, int tableNo) throws java.sql.SQLException {\n"); 514 List pass1List = getPass1FieldsMetaDataWithChangedLocking(); 515 if (!pass1List.isEmpty()){ 516 buf.append("\t\tfor (; firstFieldNo < lastFieldNo; firstFieldNo++) {\n"); 517 buf.append("\t\t\tif (!isNull(fieldNos[firstFieldNo])) {\n"); 518 buf.append("\t\t\t\tswitch (fieldNos[firstFieldNo]) {\n"); 519 for (Iterator iter = pass1List.iterator();iter.hasNext();) { 520 FieldMetaData fmd = (FieldMetaData) iter.next(); 521 buf.append("\t\t\t\t\tcase "+fmd.stateFieldNo+":\n"); 522 getSetFieldsToSQL_IL(fmd.stateFieldNo, buf); 523 } 524 buf.append("\t\t\t\t\tdefault:\n"); 525 buf.append("\t\t\t\t\t\tbreak;\n\n"); 526 buf.append("\t\t\t\t}\n"); 527 buf.append("\t\t\t}\n"); 528 buf.append("\t\t}\n"); 529 } 530 buf.append("\t\treturn firstParam;\n"); 531 buf.append("\t}\n"); 532 spec.addMethod(buf.toString()); 533 } 534 535 protected void getSetFieldsToSQL_IL_CheckNull(int fieldNo, StringBuffer buf) { 536 FieldMetaData fmd = cmd.stateFields[fieldNo]; 537 JdbcField field = (JdbcField)fmd.storeField; 538 JdbcTable fieldTable = field.mainTable; 539 JdbcTable[] tables = ((JdbcClass)cmd.storeClass).allTables; 540 boolean isMultiTable = false; 541 int tableNo = -1; 542 if (tables.length > 1) { 543 isMultiTable = true; 544 for (int i = 0; i < tables.length; i++) { 545 if (tables[i] == fieldTable) { 546 tableNo = i; 547 } 548 } 549 } 550 if (field instanceof JdbcSimpleField) { 551 JdbcColumn c = ((JdbcSimpleField) field).col; 552 if (c.isForUpdate()) { 553 Class fieldType = c.javaType; 554 boolean isPrimative = c.javaType.isPrimitive(); 555 if (isPrimative) { 556 if (isExternalized(fmd)) { 557 isPrimative = false; 558 } 559 } 560 if (c.converter != null) { boolean isPrim = c.javaType.isPrimitive(); 562 if (isMultiTable) { buf.append("\t\t\t\t\tif (tableNo == "+ tableNo +") {\n"); 564 if (isPrim) { 565 buf.append("\t\t\t\t\t\tjdbcConverter_" + fieldNo + ".set(ps, firstParam++, jdbcCol_" + fieldNo + ", new "+ primativeTypesToWrapper.get(fieldType) +"(_" + fieldNo + "));\n"); 566 } else { 567 buf.append("\t\t\t\t\t\tjdbcConverter_"+ fieldNo +".set(ps, firstParam++, jdbcCol_"+ fieldNo +", _"+ fieldNo +");\n"); 568 } 569 buf.append("\t\t\t\t\t}\n"); 570 } else { 571 if (isPrim) { 572 buf.append("\t\t\t\t\tjdbcConverter_" + fieldNo + ".set(ps, firstParam++, jdbcCol_" + fieldNo + ", new " + primativeTypesToWrapper.get(fieldType) + "(_" + fieldNo + "));\n"); 573 } else { 574 buf.append("\t\t\t\t\tjdbcConverter_" + fieldNo + ".set(ps, firstParam++, jdbcCol_" + fieldNo + ", _" + fieldNo + ");\n"); 575 } 576 } 577 } else { 578 if (isPrimative) { 579 if (isMultiTable) { buf.append("\t\t\t\t\tif (tableNo == " + tableNo + ") {\n"); 582 buf.append("\t\t\t\t\t\tps."+ typeToPreparedStatementSetField.get(fieldType) +"(firstParam++, _"+ fieldNo +");\n"); 583 buf.append("\t\t\t\t\t}\n"); 584 } else { 585 buf.append("\t\t\t\t\tps." + typeToPreparedStatementSetField.get(fieldType) + "(firstParam++, _" + fieldNo + ");\n"); 586 } 587 } else if (wrapperTypesToPrimative.containsKey(fieldType)) { String toVal = (String ) wrapperTypesToValue.get(fieldType); 589 Class primType = (Class ) wrapperTypesToPrimative.get(fieldType); 590 String psSet = (String ) typeToPreparedStatementSetField.get(primType); 591 if (isMultiTable) { 598 buf.append("\t\t\t\t\tif (tableNo == " + tableNo + ") {\n"); 599 buf.append("\t\t\t\t\t\tif (_" + fieldNo + " != null) {\n"); 600 buf.append("\t\t\t\t\t\t\tps."+ psSet +"(firstParam++, _"+ fieldNo +"."+ toVal +"());\n"); 601 buf.append("\t\t\t\t\t\t} else {\n"); 602 buf.append("\t\t\t\t\t\t\tps.setNull(firstParam++, "+ c.jdbcType +");\n"); 603 buf.append("\t\t\t\t\t\t}\n"); 604 buf.append("\t\t\t\t\t}\n"); 605 } else { 606 buf.append("\t\t\t\t\tif (_" + fieldNo + " != null) {\n"); 607 buf.append("\t\t\t\t\t\tps." + psSet + "(firstParam++, _" + fieldNo + "." + toVal + "());\n"); 608 buf.append("\t\t\t\t\t} else {\n"); 609 buf.append("\t\t\t\t\t\tps.setNull(firstParam++, " + c.jdbcType + ");\n"); 610 buf.append("\t\t\t\t\t}\n"); 611 } 612 } else if (fieldType.equals(java.math.BigInteger .class)) { if (isMultiTable) { buf.append("\t\t\t\t\tif (tableNo == " + tableNo + ") {\n"); 620 buf.append("\t\t\t\t\t\tif (_" + fieldNo + " != null) {\n"); 621 buf.append("\t\t\t\t\t\t\tps.setBigDecimal(firstParam++, new java.math.BigDecimal(_"+ fieldNo +"));\n"); 622 buf.append("\t\t\t\t\t\t} else {\n"); 623 buf.append("\t\t\t\t\t\t\tps.setNull(firstParam++, " + c.jdbcType + ");\n"); 624 buf.append("\t\t\t\t\t\t}\n"); 625 buf.append("\t\t\t\t\t}\n"); 626 } else { 627 buf.append("\t\t\t\t\tif (_" + fieldNo + " != null) {\n"); 628 buf.append("\t\t\t\t\t\tps.setBigDecimal(firstParam++, new java.math.BigDecimal(_" + fieldNo + "));\n"); 629 buf.append("\t\t\t\t\t} else {\n"); 630 buf.append("\t\t\t\t\t\tps.setNull(firstParam++, " + c.jdbcType + ");\n"); 631 buf.append("\t\t\t\t\t}\n"); 632 } 633 } else { 634 String psSet = (String ) typeToPreparedStatementSetField.get(fieldType); 640 if (isExternalized(fmd)){ 641 psSet = null; 642 } 643 if (isMultiTable) { buf.append("\t\t\t\t\tif (tableNo == " + tableNo + ") {\n"); 645 buf.append("\t\t\t\t\t\tif (_" + fieldNo + " != null) {\n"); 646 buf.append("\t\t\t\t\t\t\tps."+ (psSet != null ? psSet : "setObject") 647 +"(firstParam++, "+ ((isExternalized(fmd) && psSet != null) ? "(" + fieldType.getName() + ")" : "") 648 +"_" + fieldNo + (psSet != null ? "" : (", " + c.jdbcType))+");\n"); 649 buf.append("\t\t\t\t\t\t} else {\n"); 650 buf.append("\t\t\t\t\t\t\tps.setNull(firstParam++, " + c.jdbcType + ");\n"); 651 buf.append("\t\t\t\t\t\t}\n"); 652 buf.append("\t\t\t\t\t}\n"); 653 } else { 654 buf.append("\t\t\t\t\tif (_" + fieldNo + " != null) {\n"); 655 buf.append("\t\t\t\t\t\tps." + (psSet != null ? psSet : "setObject") 656 + "(firstParam++, " + ((isExternalized(fmd) && psSet != null) ? "(" + fieldType.getName() + ")" : "") 657 + "_" + fieldNo + (psSet != null ? "" : (", " + c.jdbcType)) + ");\n"); 658 buf.append("\t\t\t\t\t} else {\n"); 659 buf.append("\t\t\t\t\t\tps.setNull(firstParam++, " + c.jdbcType + ");\n"); 660 buf.append("\t\t\t\t\t}\n"); 661 } 662 } 663 } 664 } 665 } else if (field instanceof JdbcPolyRefField) { 666 if (isMultiTable) { buf.append("\t\t\t\t\tif (tableNo == " + tableNo + ") {\n"); 669 buf.append("\t\t\t\t\t\tfirstParam = setPolyRefData(cmd.stateFields["+ fieldNo +"], (OID) _"+ fieldNo +", cmd, ps, firstParam);\n"); 670 buf.append("\t\t\t\t\t}\n"); 671 } else { 672 buf.append("\t\t\t\t\t\tfirstParam = setPolyRefData(cmd.stateFields[" + fieldNo + "], (OID) _" + fieldNo + ", cmd, ps, firstParam);\n"); 673 } 674 } else if (field instanceof JdbcRefField) { 675 if (isMultiTable) { buf.append("\t\t\t\t\tif (tableNo == " + tableNo + ") {\n"); 677 } else { 678 buf.append("\t\t\t\t\t{\n"); 679 } 680 buf.append("\t\t\t\t\t\tOID oid = (OID)_" + fieldNo + ";\n"); 681 buf.append("\t\t\t\t\t\tif (oid == null || (oid = oid.getRealOID()) == null) {\n"); 682 JdbcColumn[] cols = field.mainTableCols; 683 int nc = cols.length; 684 for (int j = 0; j < nc; j++) { 685 JdbcColumn col = cols[j]; 686 if (col.isForUpdate()) { 687 buf.append("\t\t\t\t\t\t\tps.setNull(firstParam++, " + col.jdbcType + ");\n"); 688 } 689 } 690 buf.append("\t\t\t\t\t\t} else {\n"); 691 buf.append("\t\t\t\t\t\t\tfirstParam = ((JdbcOID)oid).setParams(ps, firstParam, ((JdbcField)cmd.stateFields["+ fieldNo +"].storeField).mainTableCols);\n"); 692 buf.append("\t\t\t\t\t\t}\n"); 693 buf.append("\t\t\t\t\t}\n"); 694 } 695 buf.append("\t\t\t\t\tbreak;\n\n"); 696 } 697 698 protected void getSetFieldsToSQL_IL(int fieldNo, StringBuffer buf) { 699 FieldMetaData fmd = cmd.stateFields[fieldNo]; 700 JdbcField field = (JdbcField)fmd.storeField; 701 JdbcTable fieldTable = field.mainTable; 702 JdbcTable[] tables = ((JdbcClass)cmd.storeClass).allTables; 703 boolean isMultiTable = false; 704 int tableNo = -1; 705 if (tables.length > 1) { 706 isMultiTable = true; 707 for (int i = 0; i < tables.length; i++) { 708 if (tables[i] == fieldTable) { 709 tableNo = i; 710 } 711 } 712 } 713 if (field instanceof JdbcSimpleField) { 714 JdbcColumn c = ((JdbcSimpleField) field).col; 715 if (c.isForUpdate()) { 716 Class fieldType = c.javaType; 717 boolean isPrimative = c.javaType.isPrimitive(); 718 if (isPrimative){ 719 if (isExternalized(fmd)){ 720 isPrimative = false; 721 } 722 } 723 if (c.converter != null) { boolean isPrim = c.javaType.isPrimitive(); 725 if (isMultiTable) { buf.append("\t\t\t\t\t\tif (tableNo == " + tableNo + ") {\n"); 727 if (isPrim) { 728 buf.append("\t\t\t\t\t\t\tjdbcConverter_" + fieldNo + ".set(ps, firstParam++, jdbcCol_" + fieldNo + ", new " + primativeTypesToWrapper.get(fieldType) + "(_" + fieldNo + "));\n"); 729 } else { 730 buf.append("\t\t\t\t\t\t\tjdbcConverter_" + fieldNo + ".set(ps, firstParam++, jdbcCol_" + fieldNo + ", _" + fieldNo + ");\n"); 731 } 732 buf.append("\t\t\t\t\t\t}\n"); 733 } else { 734 if (isPrim) { 735 buf.append("\t\t\t\t\t\tjdbcConverter_" + fieldNo + ".set(ps, firstParam++, jdbcCol_" + fieldNo + ", new " + primativeTypesToWrapper.get(fieldType) + "(_" + fieldNo + "));\n"); 736 } else { 737 buf.append("\t\t\t\t\t\tjdbcConverter_" + fieldNo + ".set(ps, firstParam++, jdbcCol_" + fieldNo + ", _" + fieldNo + ");\n"); 738 } 739 } 740 } else { 741 if (isPrimative) { 742 if (isMultiTable) { buf.append("\t\t\t\t\t\tif (tableNo == " + tableNo + ") {\n"); 744 buf.append("\t\t\t\t\t\t\tps." + typeToPreparedStatementSetField.get(fieldType) + "(firstParam++, _" + fieldNo + ");\n"); 745 buf.append("\t\t\t\t\t\t}\n"); 746 } else { 747 buf.append("\t\t\t\t\t\tps." + typeToPreparedStatementSetField.get(fieldType) + "(firstParam++, _" + fieldNo + ");\n"); 748 } 749 } else if (wrapperTypesToPrimative.containsKey(fieldType)) { String toVal = (String ) wrapperTypesToValue.get(fieldType); 751 Class primType = (Class ) wrapperTypesToPrimative.get(fieldType); 752 String psSet = (String ) typeToPreparedStatementSetField.get(primType); 753 if (isMultiTable) { buf.append("\t\t\t\t\t\tif (tableNo == " + tableNo + ") {\n"); 756 buf.append("\t\t\t\t\t\t\tps." + psSet + "(firstParam++, _" + fieldNo + "." + toVal + "());\n"); 757 buf.append("\t\t\t\t\t\t}\n"); 758 } else { 759 buf.append("\t\t\t\t\t\tps." + psSet + "(firstParam++, _" + fieldNo + "." + toVal + "());\n"); 760 } 761 } else if (fieldType.equals(java.math.BigInteger .class)) { if (isMultiTable) { buf.append("\t\t\t\t\t\tif (tableNo == " + tableNo + ") {\n"); 765 buf.append("\t\t\t\t\t\t\tps.setBigDecimal(firstParam++, new java.math.BigDecimal(_" + fieldNo + "));\n"); 766 buf.append("\t\t\t\t\t\t}\n"); 767 } else { 768 buf.append("\t\t\t\t\t\tps.setBigDecimal(firstParam++, new java.math.BigDecimal(_" + fieldNo + "));\n"); 769 } 770 } else { 771 String psSet = (String ) typeToPreparedStatementSetField.get(fieldType); 773 if (isExternalized(fmd)){ 774 psSet = null; 775 } 776 if (isMultiTable) { buf.append("\t\t\t\t\t\tif (tableNo == " + tableNo + ") {\n"); 778 buf.append("\t\t\t\t\t\t\tps." + (psSet != null ? psSet : "setObject") 779 + "(firstParam++, " + ((isExternalized(fmd) && psSet != null) ? "(" + fieldType.getName() + ")" : "") 780 + "_" + fieldNo + (psSet != null ? "" : (", " + c.jdbcType)) + ");\n"); 781 buf.append("\t\t\t\t\t\t}\n"); 782 } else { 783 buf.append("\t\t\t\t\t\tps." + (psSet != null ? psSet : "setObject") 784 + "(firstParam++, " + ((isExternalized(fmd) && psSet != null) ? "(" + fieldType.getName() + ")" : "") 785 + "_" + fieldNo + (psSet != null ? "" : (", " + c.jdbcType)) + ");\n"); 786 787 } 788 } 789 } 790 } 791 } else if (field instanceof JdbcPolyRefField) { 792 if (isMultiTable) { buf.append("\t\t\t\t\t\tif (tableNo == " + tableNo + ") {\n"); 795 buf.append("\t\t\t\t\t\t\tfirstParam = setPolyRefData(cmd.stateFields[" + fieldNo + "], (OID) _" + fieldNo + ", cmd, ps, firstParam);\n"); 796 buf.append("\t\t\t\t\t\t}\n"); 797 } else { 798 buf.append("\t\t\t\t\t\t\tfirstParam = setPolyRefData(cmd.stateFields[" + fieldNo + "], (OID) _" + fieldNo + ", cmd, ps, firstParam);\n"); 799 } 800 } else if (field instanceof JdbcRefField) { 801 if (isMultiTable) { buf.append("\t\t\t\t\t\tif (tableNo == " + tableNo + ") {\n"); 804 buf.append("\t\t\t\t\t\t\tfirstParam = ((JdbcOID)_" + fieldNo + ").setParams(ps, firstParam, ((JdbcField)cmd.stateFields[" + fieldNo + "].storeField).mainTableCols);\n"); 805 buf.append("\t\t\t\t\t\t}\n"); 806 } else { 807 buf.append("\t\t\t\t\t\tfirstParam = ((JdbcOID)_" + fieldNo + ").setParams(ps, firstParam, ((JdbcField)cmd.stateFields[" + fieldNo + "].storeField).mainTableCols);\n"); 808 } 809 } 810 buf.append("\t\t\t\t\t\tbreak;\n\n"); 811 } 812 813 protected void addContainsValidAppIdFields() { 814 StringBuffer buf = new StringBuffer (); 815 buf.append("\n\tpublic final boolean containsValidAppIdFields() {\n"); 816 if (cmd.identityType == MDStatics.IDENTITY_TYPE_APPLICATION) { 817 FieldMetaData[] pkf = cmd.pcHeirachy[0].pkFields; 818 for (int i = 0; i < pkf.length; i++) { 819 FieldMetaData fmd = pkf[i]; 820 int pkFieldNum = fmd.stateFieldNo; 821 Class fieldType = fmd.type; 822 boolean isPrimative = fmd.type.isPrimitive(); 823 824 if (isPrimative) { 825 if (fieldType.equals(int.class)) { 826 Integer intt = (Integer ) fmd.getPKDefaultValue(); 827 buf.append("\t\tif ((" + getFilledFieldName(pkFieldNum) + " & " + getFieldIndex(pkFieldNum) + ") == 0 || _" + pkFieldNum + " == "+ intt.intValue() +") {\n"); 828 } else if (fieldType.equals(long.class)) { 829 Long longg = (Long ) fmd.getPKDefaultValue(); 830 buf.append("\t\tif ((" + getFilledFieldName(pkFieldNum) + " & " + getFieldIndex(pkFieldNum) + ") == 0 || _" + pkFieldNum + " == " + longg.longValue() + ") {\n"); 831 } else if (fieldType.equals(boolean.class)) { 832 Boolean bool = (Boolean ) fmd.getPKDefaultValue(); 833 if (bool.booleanValue()) { 834 buf.append("\t\tif ((" + getFilledFieldName(pkFieldNum) + " & " + getFieldIndex(pkFieldNum) + ") == 0 || _" + pkFieldNum + ") {\n"); 835 } else { 836 buf.append("\t\tif (("+ getFilledFieldName(pkFieldNum) +" & "+ getFieldIndex(pkFieldNum) +") == 0 || !_"+ pkFieldNum +") {\n"); 837 } 838 } else if (fieldType.equals(double.class)) { 839 Double doubl = (Double ) fmd.getPKDefaultValue(); 840 buf.append("\t\tif ((" + getFilledFieldName(pkFieldNum) + " & " + getFieldIndex(pkFieldNum) + ") == 0 || _" + pkFieldNum + " == " + doubl.doubleValue() + ") {\n"); 841 } else if (fieldType.equals(float.class)) { 842 Float floatt = (Float ) fmd.getPKDefaultValue(); 843 buf.append("\t\tif ((" + getFilledFieldName(pkFieldNum) + " & " + getFieldIndex(pkFieldNum) + ") == 0 || _" + pkFieldNum + " == " + floatt.doubleValue() + ") {\n"); 844 } else if (fieldType.equals(short.class)) { 845 Short shortt = (Short ) fmd.getPKDefaultValue(); 846 buf.append("\t\tif ((" + getFilledFieldName(pkFieldNum) + " & " + getFieldIndex(pkFieldNum) + ") == 0 || _" + pkFieldNum + " == " + shortt.shortValue() + ") {\n"); 847 } else if (fieldType.equals(char.class)) { 848 Character charr = (Character ) fmd.getPKDefaultValue(); 849 buf.append("\t\tif ((" + getFilledFieldName(pkFieldNum) + " & " + getFieldIndex(pkFieldNum) + ") == 0 || _" + pkFieldNum + " == " + charr.charValue() + ") {\n"); 850 } else if (fieldType.equals(byte.class)) { 851 Byte bytee = (Byte ) fmd.getPKDefaultValue(); 852 buf.append("\t\tif ((" + getFilledFieldName(pkFieldNum) + " & " + getFieldIndex(pkFieldNum) + ") == 0 || _" + pkFieldNum + " == " + bytee.byteValue() + ") {\n"); 853 } else { 854 throw BindingSupportImpl.getInstance().invalidOperation("Unsupported type " + fieldType); 855 } 856 buf.append("\t\t\treturn false;\n"); 857 buf.append("\t\t}\n"); 858 } else { 859 buf.append("\t\tif (("+ getFilledFieldName(pkFieldNum) +" & "+ getFieldIndex(pkFieldNum) +") == 0 || _"+ pkFieldNum +" == null || _" + pkFieldNum + ".equals(cmd.pcHeirachy[0].pkFields["+ i +"].getPKDefaultValue())) {\n"); 860 buf.append("\t\t\treturn false;\n"); 861 buf.append("\t\t}\n"); 862 } 863 } 864 buf.append("\t\treturn true;\n"); 865 } else if (cmd.identityType == MDStatics.IDENTITY_TYPE_DATASTORE) { 866 buf.append("\t\treturn false;\n"); 867 } 868 buf.append("\t}\n"); 869 spec.addMethod(buf.toString()); 870 } 871 872 878 protected void addFields() { 879 super.addFields(); 880 FieldMetaData[] fields = cmd.stateFields; 881 for (int i = 0; i < fields.length; i++) { 882 JdbcField f = (JdbcField)fields[i].storeField; 883 if (f instanceof JdbcSimpleField) { 884 JdbcSimpleField sf = (JdbcSimpleField) f; 885 if (sf.col.converter != null) { 886 spec.addField("public static " + sf.col.converter.getClass().getName() 887 +" "+JDBC_CONVERTER_FIELD_PREFIX + i); 888 spec.addField("public static " + sf.col.getClass().getName() 889 +" "+ "jdbcCol_" + i); 890 } 891 } 892 } 893 } 894 895 protected void addCopyFields() { 896 StringBuffer buf = new StringBuffer (); 897 buf.append("\n\tpublic final void copyFields(OID oid) {\n"); 898 ClassMetaData currentCMD = null; 899 if (cmd.identityType == MDStatics.IDENTITY_TYPE_APPLICATION) { 900 currentCMD = getTopPCSuperClassMetaData(); 901 buf.append("\t\t" + currentCMD.oidClassName + " id = (" + currentCMD.oidClassName + ")oid;\n"); 902 FieldMetaData[] fmds = currentCMD.pkFields; 903 for (int i = 0; i < fmds.length; i++) { 904 FieldMetaData fmd = fmds[i]; 905 int stateFieldNum = fmd.stateFieldNo; 906 buf.append("\t\t_"+ stateFieldNum +" = id._"+ i +";\n"); 907 buf.append("\t\t"+getFilledFieldName(stateFieldNum) +" |= "+ getFieldIndex(stateFieldNum) +";\n"); 908 } 909 } 910 buf.append("\t}\n"); 911 spec.addMethod(buf.toString()); 912 } 913 914 protected void addHasSameNullFields() { 915 StringBuffer buf = new StringBuffer (); 916 buf.append("\n\tpublic final boolean hasSameNullFields(State state, State mask) {\n"); 917 JdbcClass jc = (JdbcClass)cmd.storeClass; 918 if (jc.optimisticLocking != JdbcClass.OPTIMISTIC_LOCKING_CHANGED) { 919 buf.append("\t\treturn true;\n"); 920 } else { 921 buf.append("\t\t"+className+" s = ("+ className +") state;\n"); 922 buf.append("\t\t"+className+" ms = ("+ className +") mask;\n"); 923 int num = getNumOfControlFields(); 924 for (int i = 0; i < num; i++) { 925 if (i == 0) { 926 buf.append("\t\tint filledMask = ms.filled0;\n"); 927 } else { 928 buf.append("\t\tfilledMask = ms.filled" + i + ";\n"); 929 } 930 List fields = getRealObjectFields(i); 931 for (Iterator iter = fields.iterator(); iter.hasNext();) { 932 FieldMetaData fmd = (FieldMetaData) iter.next(); 933 int fieldNum = getFieldNo(fmd); 934 int index = getFieldIndex(fieldNum); 935 buf.append("\t\tif ((filledMask & "+ index +") != 0 && (_"+ fieldNum +" == null) != (s._"+ fieldNum +" == null)) {return false;}\n"); 936 } 937 } 938 buf.append("\t\treturn true;\n"); 939 } 940 buf.append("\t}\n"); 941 spec.addMethod(buf.toString()); 942 } 943 944 protected void addCopyKeyFieldsUpdate() { 945 StringBuffer buf = new StringBuffer (); 946 buf.append("\n\tpublic final void copyKeyFieldsUpdate(OID oid) {\n"); 947 if (cmd.identityType == MDStatics.IDENTITY_TYPE_APPLICATION) { 948 buf.append("\t\t"+ cmd.oidClassName +" other = ("+ cmd.oidClassName +") oid;\n"); 949 FieldMetaData[] pkFields = cmd.pkFields; 950 JdbcColumn[] pkc = ((JdbcClass)cmd.storeClass).table.pk; 951 for (int i = 0; i < pkc.length; i++) { 952 int stateFieldNo = pkFields[i].stateFieldNo; 953 buf.append("\t\tif (containsField("+ stateFieldNo +")) {\n"); 954 buf.append("\t\t\tother._"+i+" = _"+ stateFieldNo +";\n"); 955 buf.append("\t\t}\n"); 956 } 957 } 958 buf.append("\t}\n"); 959 spec.addMethod(buf.toString()); 960 } 961 962 protected void addCopyKeyFieldsFromOID() { 963 StringBuffer buf = new StringBuffer (); 964 buf.append("\n\tpublic final void copyKeyFields(OID oid) {\n"); 965 if (cmd.identityType == MDStatics.IDENTITY_TYPE_APPLICATION) { 966 buf.append("\t\t"+ cmd.oidClassName +" other = ("+ cmd.oidClassName +") oid;\n"); 967 FieldMetaData[] pkFields = cmd.pkFields; 968 JdbcColumn[] pkc = ((JdbcClass)cmd.storeClass).table.pk; 969 for (int i = 0; i < pkc.length; i++) { 970 buf.append("\t\tother._"+i+" = _"+ pkFields[i].stateFieldNo +";\n"); 971 } 972 } 973 buf.append("\t}\n"); 974 spec.addMethod(buf.toString()); 975 } 976 977 protected void addCheckKeyFields() { 978 StringBuffer buf = new StringBuffer (); 979 buf.append("\n\tpublic final boolean checkKeyFields(OID oid) {\n"); 980 if (cmd.identityType == MDStatics.IDENTITY_TYPE_APPLICATION) { 981 buf.append("\t\t" + cmd.oidClassName + " other = (" + cmd.oidClassName + ") oid;\n"); 982 FieldMetaData[] pkFields = cmd.pkFields; 983 JdbcColumn[] pkc = ((JdbcClass)cmd.storeClass).table.pk; 984 for (int i = 0; i < pkc.length; i++) { 985 Class classType = pkc[i].javaType; 986 boolean isPrimitive = classType.isPrimitive(); 987 if (isPrimitive) { 988 buf.append("\t\tif (other._"+i+" != _"+ pkFields[i].stateFieldNo +") {\n"); 989 buf.append("\t\t\treturn false;\n"); 990 buf.append("\t\t}\n"); 991 } else { 992 buf.append("\t\tif (!other._"+i+".equals(_"+ pkFields[i].stateFieldNo +")) {\n"); 993 buf.append("\t\t\treturn false;\n"); 994 buf.append("\t\t}\n"); 995 } 996 } 997 } 998 buf.append("\t\treturn true;\n"); 999 buf.append("\t}\n"); 1000 spec.addMethod(buf.toString()); 1001 } 1002 1003 protected void addClearApplicationIdentityFields() { 1004 StringBuffer buf = new StringBuffer (); 1005 buf.append("\n\tpublic final void clearApplicationIdentityFields() {\n"); 1006 if (cmd.identityType == MDStatics.IDENTITY_TYPE_APPLICATION) { 1007 int[] masks = new int[getNumOfControlFields()]; 1008 FieldMetaData[] pkf = cmd.pcHeirachy[0].pkFields; 1009 for (int i = pkf.length - 1; i >= 0; i--) { 1010 int fieldNum = pkf[i].fieldNo; 1011 masks[getLocalVarIndex(0, fieldNum)] += getFieldIndex(fieldNum); 1012 } 1013 int maskLength = masks.length; 1014 for (int i = 0; i < maskLength; i++) { 1015 if (masks[i] != 0) { 1016 buf.append("\t\tfilled"+i+" = filled"+i+" & "+ (masks[i] ^ 0xFFFFFFFF) +";\n"); 1017 } 1018 } 1019 } 1020 buf.append("\t}\n"); 1021 spec.addMethod(buf.toString()); 1022 } 1023 1024 protected void addContainsApplicationIdentityFields() { 1025 StringBuffer buf = new StringBuffer (); 1026 buf.append("\n\tpublic final boolean containsApplicationIdentityFields() {\n"); 1027 if (cmd.identityType == MDStatics.IDENTITY_TYPE_APPLICATION) { 1028 FieldMetaData[] pkf = cmd.pcHeirachy[0].pkFields; 1029 for (int i = 0; i < pkf.length; i++) { 1030 int pkFieldNum = pkf[i].stateFieldNo; 1031 buf.append("\t\tif (("+ getFilledFieldName(pkFieldNum) +" & "+ getFieldIndex(pkFieldNum) +") == 0) {\n"); 1032 buf.append("\t\t\treturn false;\n"); 1033 buf.append("\t\t}\n"); 1034 } 1035 buf.append("\t\treturn true;\n"); 1036 } else { 1037 buf.append("\t\treturn false;\n"); 1038 } 1039 buf.append("\t}\n"); 1040 spec.addMethod(buf.toString()); 1041 } 1042 1043 protected List getPass1FieldsMetaDataWithChangedLocking() { 1044 ArrayList list = new ArrayList (); 1045 FieldMetaData[] fields = cmd.stateFields; 1046 list.ensureCapacity(fields.length); 1047 for (int i = 0; i < fields.length; i++) { 1048 FieldMetaData field = fields[i]; 1049 if (field.primaryField) { 1050 if (((JdbcField)field.storeField).includeForChangedLocking) { 1051 list.add(field); 1052 } 1053 } 1054 } 1055 return list; 1056 } 1057 1058} 1059 1060 | Popular Tags |