1 2 12 package com.versant.core.jdbc; 13 14 import com.versant.core.common.*; 15 import com.versant.core.metadata.FetchGroup; 16 import com.versant.core.metadata.FetchGroupField; 17 import com.versant.core.metadata.FieldMetaData; 18 import com.versant.core.metadata.ClassMetaData; 19 import com.versant.core.jdbc.metadata.*; 20 import com.versant.core.server.PersistGraph; 21 22 import java.sql.PreparedStatement ; 23 import java.sql.SQLException ; 24 import java.sql.ResultSet ; 25 26 29 public class JdbcGenericState extends GenericState implements JdbcState { 30 31 public JdbcGenericState() { 32 } 33 34 public JdbcGenericState(ClassMetaData cmd) { 35 super(cmd); 36 } 37 38 49 public int compareToPass1(State state) { 50 JdbcGenericState s = (JdbcGenericState)state; 51 checkCmd(); 52 boolean[] sf = s.filled; 53 for (int i = 0; i < filled.length; i++) { 54 if (!cmd.stateFields[i].primaryField) continue; 55 boolean a = filled[i]; 56 boolean b = sf[i]; 57 if (a && !b) return -1; 58 if (!a && b) return +1; 59 if (a) { 60 JdbcField jdbcField = (JdbcField)cmd.stateFields[i].storeField; 63 if (jdbcField.isOracleStyleLOB()) { 64 a = isNull(i); 65 b = s.isNull(i); 66 if (a && !b) return -1; 67 if (!a && b) return +1; 68 } 69 } 70 } 71 return 0; 72 } 73 74 79 public void copyPass1Fields(ResultSet rs, FetchGroup fetchGroup, 80 int firstCol) { 81 FetchGroupField[] a = fetchGroup.fields; 82 for (int i = 0; i < a.length; i++) { 83 try { 84 FieldMetaData fmd = a[i].fmd; 85 if (!fmd.primaryField) continue; 86 JdbcField f = (JdbcField)fmd.storeField; 87 int fieldNo = f.stateFieldNo; 88 firstCol = getFieldData(f, rs, firstCol, fieldNo); 89 filled[fieldNo] = true; 90 } catch (Exception e) { 91 throw BindingSupportImpl.getInstance().datastore("Error reading field " + a[i].fmd.getQName() + 92 " from ResultSet: " + e, 93 e); 94 } 95 } 96 } 97 98 public void copyPass1Fields(ResultSet rs, JdbcField[] fields) { 99 JdbcField f = null; 100 for (int i = 0; i < fields.length; i++) { 101 try { 102 f = fields[i]; 103 if (f == null) continue; 104 if (f.stateFieldNo < cmd.stateFields.length 106 && cmd.stateFields[f.stateFieldNo] == f.fmd) { 107 getFieldData(f, rs, i + 1, f.stateFieldNo); 108 filled[f.stateFieldNo] = true; 109 } 110 } catch (SQLException e) { 111 throw BindingSupportImpl.getInstance().datastore("Error reading field " 112 + f == null ? "" : f.fmd.getQName() + 113 " from ResultSet: " + e, e); 114 } 115 } 116 } 117 118 private int getFieldData(JdbcField f, ResultSet rs, int firstCol, 119 int fieldNo) throws SQLException { 120 if (f instanceof JdbcSimpleField) { 121 JdbcColumn c = ((JdbcSimpleField)f).col; 122 if (Debug.DEBUG) { 123 if (!c.name.toUpperCase().equals( 124 rs.getMetaData().getColumnName(firstCol).toUpperCase())) { 125 throw BindingSupportImpl.getInstance().internal( 126 "Reading the wrong column: " + firstCol + " \nrs field = " 127 + rs.getMetaData().getColumnName(firstCol) + "\nmetaData field = " + c.name); 128 } 129 } 130 if (c.converter != null) { 131 data[fieldNo] = c.converter.get(rs, firstCol++, c); 132 } else { 133 data[fieldNo] = JdbcUtils.get(rs, firstCol++, c.javaTypeCode, 134 c.scale); 135 if (rs.wasNull()) { 136 data[fieldNo] = null; 137 } 138 } 139 } else if (f instanceof JdbcRefField) { 140 JdbcRefField rf = (JdbcRefField)f; 141 JdbcOID oid = (JdbcOID)rf.targetClass.createOID(false); 142 if (oid.copyKeyFields(rs, firstCol)) { 143 data[fieldNo] = oid; 144 } else { 145 data[fieldNo] = null; 146 } 147 firstCol += rf.cols.length; 148 } else if (f instanceof JdbcPolyRefField) { 149 data[fieldNo] = getPolyRefOID(f, rs, firstCol); 150 firstCol += ((JdbcPolyRefField)f).cols.length; 151 } else { 152 throw BindingSupportImpl.getInstance().internal("not implemented"); 153 } 154 return firstCol; 155 } 156 157 167 public int setParams(PreparedStatement ps, int[] stateFieldNos, 168 int firstFieldNo, int lastFieldNo, int firstParam, 169 PersistGraph pGraph, int tableNo) throws SQLException { 170 JdbcTable table = ((JdbcClass)cmd.storeClass).allTables[tableNo]; 171 for (int i = firstFieldNo; i < lastFieldNo; i++) { 172 int fieldNo = stateFieldNos[i]; 173 if (fieldNo >= 0) { 174 JdbcField field = (JdbcField)cmd.stateFields[fieldNo].storeField; 175 if (field.mainTable == table) { 176 firstParam = setFieldData(field, ps, firstParam, fieldNo); 177 } 178 } 179 } 180 return firstParam; 181 } 182 183 195 public int setParamsChangedAndNotNull(PreparedStatement ps, int[] fieldNos, 196 int firstFieldNo, int lastFieldNo, int firstParam, 197 PersistGraph pGraph, int tableNo) throws SQLException { 198 checkCmd(); 199 JdbcTable table = ((JdbcClass)cmd.storeClass).allTables[tableNo]; 200 for (int i = firstFieldNo; i < lastFieldNo; i++) { 201 int fieldNo = fieldNos[i]; 202 if (fieldNo < 0 || data[fieldNo] == null) continue; 203 JdbcField field = (JdbcField)cmd.stateFields[fieldNo].storeField; 204 if (field.includeForChangedLocking && field.mainTable == table) { 205 firstParam = setFieldData(field, ps, firstParam, fieldNo); 206 } 207 } 208 return firstParam; 209 } 210 211 221 public int setOptimisticLockingParams(PreparedStatement ps, 222 int firstParam) throws SQLException { 223 checkCmd(); 224 JdbcSimpleField f = ((JdbcClass)cmd.storeClass).optimisticLockingField; 225 if (f == null) { 226 throw BindingSupportImpl.getInstance().internal("setOptimisticLockingParams " + 227 "called for non-optimistic locking class: " + cmd); 228 } 229 int fieldNo = f.stateFieldNo; 230 if (!containsField(fieldNo)) { 232 throw BindingSupportImpl.getInstance().internal("setOptimisticLockingParams " + 233 "called for state not containing optimistic locking field: " + 234 cmd.qname + " " + f); 235 } 236 JdbcColumn c = f.col; 237 if (c.converter != null) { 238 c.converter.set(ps, firstParam++, c, data[fieldNo]); 239 } else { 240 JdbcUtils.set(ps, firstParam++, data[fieldNo], c.javaTypeCode, 241 c.jdbcType); 242 } 243 return firstParam; 244 } 245 246 private int setFieldData(JdbcField f, PreparedStatement ps, int firstParam, 247 int fieldNo) throws SQLException { 248 if (f instanceof JdbcSimpleField) { 249 JdbcColumn c = ((JdbcSimpleField)f).col; 250 if (c.isForUpdate()) { 251 if (c.converter != null) { 252 c.converter.set(ps, firstParam++, c, data[fieldNo]); 253 } else { 254 JdbcUtils.set(ps, firstParam++, data[fieldNo], c.javaTypeCode, 255 c.jdbcType); 256 } 257 } 258 } else if (f instanceof JdbcRefField) { 259 OID oid = (OID)data[fieldNo]; 260 if (oid == null || (oid = oid.getRealOID()) == null) { 261 firstParam = setRefFieldToNull(f.mainTableCols, ps, firstParam); 262 } else { 263 firstParam = ((JdbcOID)oid).setParams(ps, firstParam, f.mainTableCols); 264 } 265 } else if (f instanceof JdbcPolyRefField) { 266 firstParam = setPolyRefData(f, (OID)data[fieldNo], cmd, ps, 267 firstParam); 268 } else { 269 throw BindingSupportImpl.getInstance().internal("not implemented"); 270 } 271 return firstParam; 272 } 273 274 282 public void setOracleStyleLOBs(ResultSet rs, int[] stateFieldNos, 283 int numFieldNos, int firstCol) throws SQLException { 284 for (int i = 0; i < numFieldNos; i++) { 285 int fieldNo = stateFieldNos[i]; 286 JdbcField jdbcField = (JdbcField)cmd.stateFields[fieldNo].storeField; 287 JdbcColumn c = ((JdbcSimpleField)jdbcField).col; 288 c.converter.set(rs, firstCol++, c, data[fieldNo]); 289 } 290 } 291 292 private com.versant.core.common.OID getPolyRefOID( 293 com.versant.core.metadata.FieldMetaData fmd, 294 java.sql.ResultSet rs, 295 int firstCol) throws java.sql.SQLException { 296 return getPolyRefOID((JdbcField)fmd.storeField, rs, firstCol); 297 } 298 299 public static com.versant.core.common.OID getPolyRefOID( 300 com.versant.core.jdbc.metadata.JdbcField f, 301 java.sql.ResultSet rs, 302 int firstCol) 303 throws java.sql.SQLException { 304 com.versant.core.jdbc.metadata.JdbcPolyRefField pf = 305 (com.versant.core.jdbc.metadata.JdbcPolyRefField)f; 306 return pf.getData(rs, firstCol); 307 } 308 309 private int setPolyRefData( 310 com.versant.core.metadata.FieldMetaData fmd, 311 com.versant.core.common.OID oid, 312 com.versant.core.metadata.ClassMetaData cmd, 313 java.sql.PreparedStatement ps, 314 int firstParam) throws java.sql.SQLException { 315 return setPolyRefData((JdbcField)fmd.storeField, oid, cmd, ps, firstParam); 316 } 317 318 public static int setPolyRefData( 319 com.versant.core.jdbc.metadata.JdbcField f, 320 com.versant.core.common.OID oid, 321 com.versant.core.metadata.ClassMetaData cmd, 322 java.sql.PreparedStatement ps, 323 int firstParam) throws java.sql.SQLException { 324 com.versant.core.jdbc.metadata.JdbcPolyRefField pf = 325 (com.versant.core.jdbc.metadata.JdbcPolyRefField)f; 326 return pf.setData(ps, firstParam, oid); 327 } 328 329 public static int setRefFieldToNull(JdbcColumn[] cols, 330 PreparedStatement ps, int firstParam) throws SQLException { 331 int nc = cols.length; 332 for (int i = 0; i < nc; i++) { 333 JdbcColumn col = cols[i]; 334 if (col.isForUpdate()) { 335 ps.setNull(firstParam++, col.jdbcType); 336 } 337 } 338 return firstParam; 339 } 340 341 public boolean hasSameNullFields(State state, State mask) { 342 checkCmd(); 343 JdbcClass jc = (JdbcClass)cmd.storeClass; 344 if (jc.optimisticLocking != JdbcClass.OPTIMISTIC_LOCKING_CHANGED) { 345 return true; 346 } 347 JdbcGenericState s = (JdbcGenericState)state; 348 JdbcGenericState ms = (JdbcGenericState)mask; 349 int n = filled.length; 350 for (int i = 0; i < n; i++) { 351 if (ms.filled[i]) { 352 if ((data[i] == null) != (s.data[i] == null)) return false; 353 } 354 } 355 return true; 356 } 357 358 } 359 360 | Popular Tags |