1 21 22 package org.opensubsystems.core.persist.db.oracle; 23 24 import java.sql.CallableStatement ; 25 import java.sql.SQLException ; 26 import java.sql.Timestamp ; 27 import java.sql.Types ; 28 29 import org.opensubsystems.core.data.BasicDataObject; 30 import org.opensubsystems.core.data.ModifiableDataObject; 31 import org.opensubsystems.core.error.OSSException; 32 import org.opensubsystems.core.error.OSSInconsistentDataException; 33 34 43 public final class OracleDataUtils 44 { 45 47 50 private OracleDataUtils( 51 ) 52 { 53 } 55 56 58 71 public static void insertAndFetchGeneratedValues( 72 CallableStatement insertStatement, 73 int iIndex, 74 BasicDataObject data 75 ) throws SQLException , 76 OSSException 77 { 78 int iGeneratedKey; 79 Timestamp tmTimestamp; 80 81 insertStatement.registerOutParameter(iIndex++, Types.INTEGER); 82 insertStatement.registerOutParameter(iIndex++, Types.TIMESTAMP); 83 insertStatement.executeUpdate(); 84 iGeneratedKey = insertStatement.getInt(iIndex - 2); 85 tmTimestamp = insertStatement.getTimestamp(iIndex - 1); 86 data.setId(iGeneratedKey); 87 data.setCreationTimestamp(tmTimestamp); 88 if (data instanceof ModifiableDataObject) 89 { 90 ((ModifiableDataObject)data).setModificationTimestamp(tmTimestamp); 91 } 92 } 93 94 108 public static void updateAndFetchGeneratedValues( 109 CallableStatement updateStatement, 110 int iIndex, 111 ModifiableDataObject data 112 ) throws SQLException , 113 OSSException 114 { 115 int iUpdateCount; 116 Timestamp tmTimestamp = null; 117 118 updateStatement.registerOutParameter(iIndex++, Types.TIMESTAMP); 119 iUpdateCount = updateStatement.executeUpdate(); 120 if (iUpdateCount > 1) 121 { 122 throw new OSSInconsistentDataException( 123 "Inconsistent database contains multiple (" + iUpdateCount + 124 ") data with the same ID modified at the same time"); 125 } 126 if (iUpdateCount == 1) 127 { 128 tmTimestamp = updateStatement.getTimestamp(iIndex - 1); 129 data.setModificationTimestamp(tmTimestamp); 130 } 131 } 132 } 133 | Popular Tags |