1 package org.apache.ojb.broker.util.sequence; 2 16 import java.math.BigDecimal ; 17 import java.sql.Date ; 18 import java.sql.SQLException ; 19 import java.sql.Time ; 20 import java.sql.Timestamp ; 21 import java.sql.Types ; 22 import java.util.Iterator ; 23 import java.util.Vector ; 24 25 import org.apache.ojb.broker.PersistenceBroker; 26 import org.apache.ojb.broker.PersistenceBrokerException; 27 import org.apache.ojb.broker.accesslayer.JdbcAccess; 28 import org.apache.ojb.broker.accesslayer.ResultSetAndStatement; 29 import org.apache.ojb.broker.core.proxy.ProxyHelper; 30 import org.apache.ojb.broker.metadata.ClassDescriptor; 31 import org.apache.ojb.broker.metadata.FieldDescriptor; 32 import org.apache.ojb.broker.metadata.ObjectReferenceDescriptor; 33 import org.apache.ojb.broker.metadata.fieldaccess.PersistentField; 34 import org.apache.ojb.broker.query.Query; 35 import org.apache.ojb.broker.util.logging.Logger; 36 import org.apache.ojb.broker.util.logging.LoggerFactory; 37 38 72 public class SequenceManagerMySQLImpl implements SequenceManager 73 { 74 private Logger log = LoggerFactory.getLogger(SequenceManagerMySQLImpl.class); 75 76 79 protected PersistenceBroker broker; 80 81 public SequenceManagerMySQLImpl(PersistenceBroker broker) 82 { 83 this.broker = broker; 84 } 85 86 94 private synchronized int getNextId(FieldDescriptor field) 95 { 96 return 0; 97 } 98 99 103 public int getUniqueId(FieldDescriptor field) 104 { 105 return getNextId(field); 106 } 107 108 113 protected String getUniqueString(FieldDescriptor field) 114 { 115 return Integer.toString(getUniqueId(field)); 116 } 117 118 122 protected long getUniqueLong(FieldDescriptor field) 123 { 124 return getUniqueId(field); 125 } 126 127 131 protected Object getUniqueObject(FieldDescriptor field) 132 { 133 return null; } 135 136 public void afterStore(JdbcAccess dbAccess, ClassDescriptor cld, Object obj) throws SequenceManagerException 137 { 138 FieldDescriptor[] fldArr = cld.getAutoIncrementFields(); 139 FieldDescriptor fd = fldArr.length > 0 ? fldArr[0] : null; 140 if(fd != null) 141 { ResultSetAndStatement rsStmt = null; 143 try 144 { 145 rsStmt = dbAccess.executeSQL("SELECT LAST_INSERT_ID() as newid FROM " + cld.getFullTableName(), cld, Query.NOT_SCROLLABLE); 146 int newid = 0; 147 if (rsStmt.m_rs.next()) 148 { 149 newid = rsStmt.m_rs.getInt("newid"); 150 } 151 rsStmt.m_rs.close(); 152 if(log.isDebugEnabled()) log.debug("After store - newid=" + newid); 153 154 PersistentField pf = fd.getPersistentField(); 155 pf.set(obj, new Integer (newid)); 156 } 157 catch (PersistenceBrokerException e) 158 { 159 throw new SequenceManagerException(e); 160 } 161 catch (SQLException e) 162 { 163 throw new SequenceManagerException(e); 164 } 165 finally 166 { 167 rsStmt.close(); 168 } 169 } 170 171 } 172 173 public void setReferenceFKs(Object obj, ClassDescriptor cld) throws SequenceManagerException 174 { 175 176 Vector objectReferenceDescriptors = cld.getObjectReferenceDescriptors(); 177 Iterator i = objectReferenceDescriptors.iterator(); 179 while (i.hasNext()) 180 { 181 ObjectReferenceDescriptor rds = 182 (ObjectReferenceDescriptor) i.next(); 183 Object ref = rds.getPersistentField().get(obj); 184 if(ref != null){ assertFkAssignment(obj, cld, ref, rds); 186 } 187 } 188 } 189 190 197 private void assertFkAssignment(Object obj, ClassDescriptor cld, Object ref, ObjectReferenceDescriptor rds) 198 { 199 Class c = ProxyHelper.getRealClass(ref); 201 202 ClassDescriptor refCld = 204 broker.getDescriptorRepository().getDescriptorFor(c); 205 ClassDescriptor objCld = cld; 206 207 FieldDescriptor[] objFkFields = 209 rds.getForeignKeyFieldDescriptors(objCld); 210 if (objFkFields != null) 211 { 212 FieldDescriptor refFld = refCld.getAutoIncrementFields()[0]; 213 FieldDescriptor fld = null; 214 for (int i = 0; i < objFkFields.length; i++) 215 { 216 fld = objFkFields[i]; 217 fld.getPersistentField().set(obj, refFld.getPersistentField().get(ref)); 218 } 219 } 220 } 221 222 229 public Object getUniqueValue(FieldDescriptor field) throws SequenceManagerException 230 { 231 Object result = null; 232 switch (field.getJdbcType().getType()) 233 { 234 case Types.ARRAY: 235 { 236 Object [] arr = {getUniqueString(field)}; 237 result = arr; 238 break; 239 } 240 case Types.BIGINT: 241 { 242 result = new Long (getUniqueLong(field)); 243 break; 244 } 245 case Types.BINARY: 246 { 247 result = getUniqueString(field).getBytes(); 248 break; 249 } 250 case Types.CHAR: 251 { 252 result = getUniqueString(field); 253 break; 254 } 255 case Types.DATE: 256 { 257 result = new Date (getUniqueLong(field)); 258 break; 259 } 260 case Types.DECIMAL: 261 { 262 result = new BigDecimal (getUniqueLong(field)); 263 break; 264 } 265 case Types.DOUBLE: 266 { 267 result = new Double (getUniqueLong(field)); 268 break; 269 } 270 case Types.FLOAT: 271 { 272 result = new Double (getUniqueLong(field)); 273 break; 274 } 275 case Types.INTEGER: 276 { 277 result = new Integer (getUniqueId(field)); 278 break; 279 } 280 case Types.JAVA_OBJECT: 281 { 282 result = getUniqueObject(field); 283 break; 284 } 285 case Types.LONGVARBINARY: 286 { 287 result = getUniqueString(field).getBytes(); 288 break; 289 } 290 case Types.LONGVARCHAR: 291 { 292 result = getUniqueString(field); 293 break; 294 } 295 case Types.NUMERIC: 296 { 297 result = new BigDecimal (getUniqueLong(field)); 298 break; 299 } 300 case Types.REAL: 301 { 302 result = new Float (getUniqueLong(field)); 303 break; 304 } 305 case Types.SMALLINT: 306 { 307 result = new Short ((short)getUniqueId(field)); 308 break; 309 } 310 case Types.TIME: 311 { 312 result = new Time (getUniqueLong(field)); 313 break; 314 } 315 case Types.TIMESTAMP: 316 { 317 result = new Timestamp (getUniqueLong(field)); 318 break; 319 } 320 case Types.TINYINT: 321 { 322 result = new Byte ((byte)getUniqueId(field)); 323 break; 324 } 325 case Types.VARBINARY: 326 { 327 result = getUniqueString(field).getBytes(); 328 break; 329 } 330 case Types.VARCHAR: 331 { 332 result = getUniqueString(field); 333 break; 334 } 335 default: 336 { 337 result = getUniqueString(field); 338 break; 339 } 340 } 341 result = field.getFieldConversion().sqlToJava(result); 342 return result; 343 } 344 } 345 | Popular Tags |