|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectorg.opensubsystems.core.persist.db.DatabaseOperation
org.opensubsystems.core.persist.db.DatabaseUpdateOperation
public abstract class DatabaseUpdateOperation
Adapter to simplify writing of database updates, which takes care of requesting and returning connections, transaction management and exception handling. To use this adapter you just need to define anonymous class and override method performOperation to provide the actual database update. Optionally you may want to override one of the handleXXX methods to provide custom error handling. Example how to perform the update using query retrieved from schema public ModifiableDataObject save( final ModifiableDataObject data ) throws OSSException { DatabaseUpdateOperation dbop = new DatabaseUpdateOperation( this, m_schema.getQueryToUpdateMyData(data), DatabaseUpdateOperation.DBOP_UPDATE, m_schema, DataConstants.MY_DATA, data ) { protected void updateDatabase( DatabaseFactoryImpl dbfactory, Connection cntConnection, PreparedStatement pstmQuery ) throws OSSException, SQLException { ModifiableDataObject objData = data; int iIndex = ((ModifiableDatabaseFactory)dbfactory).setValuesForUpdate( pstmQuery, objData); DatabaseImpl.getInstance().updatedAndFetchGeneratedValues( m_strDataObjectName, cntConnection, pstmQuery, m_dbschema.isInDomain(), m_dbschema.getTableNames().get( new Integer(m_iDataType)).toString(), iIndex, objData); setReturnData(objData); } }; dbop.executeUpdate(); return (ModifiableDataObject)dbop.getReturnData(); } Example of method in factory which saves data using its schema public ModifiableDataObject save( final ModifiableDataObject data ) throws OSSException { DatabaseUpdateOperation dbop = new DatabaseUpdateOperation( this, DatabaseUpdateOperation.DBOP_UPDATE) { protected void updateDatabase( DatabaseFactoryImpl dbfactory, Connection cntConnection, PreparedStatement pstmQuery ) throws OSSException, SQLException { setReturnData(m_schema.updateData(cntConnection, (MyData)data)); } }; dbop.executeUpdate(); return (ModifiableDataObject)dbop.getReturnData(); }
| Field Summary |
|---|
| Fields inherited from class org.opensubsystems.core.persist.db.DatabaseOperation |
|---|
m_data, m_dbschema, m_factory, m_iDataType, m_returnData, m_strQuery |
| Fields inherited from interface org.opensubsystems.core.persist.db.DatabaseOperations |
|---|
DBOP_DELETE, DBOP_INSERT, DBOP_SELECT, DBOP_UPDATE |
| Constructor Summary | |
|---|---|
DatabaseUpdateOperation(DatabaseFactoryImpl factory,
int iUpdateType)
Constructor to use when database update doesn't require any prepared statement. |
|
DatabaseUpdateOperation(DatabaseFactoryImpl factory,
java.lang.String strQueryToPrepare,
ModifiableDatabaseSchema schema,
int iUpdateType,
java.lang.Object data)
Constructor to use when database update requires prepared statement. |
|
| Method Summary | |
|---|---|
void |
executeUpdate()
Method to execute database update invoking the user defined code in performOperation. |
protected void |
handleKnownError(OSSException exc,
java.sql.Connection cntConnection,
int iOperationType,
int iDataType,
java.lang.Object data)
Override this method to provide any custom error handling for expected error, which were most likely produced by lower layer. |
protected void |
handleSQLException(java.sql.SQLException sqleExc,
java.sql.Connection cntConnection,
int iOperationType,
int iDataType,
java.lang.Object data)
Provide custom handling of SQL Exceptions to usually detect constraint violation. |
protected void |
handleUnknownError(java.lang.Throwable thr,
java.sql.Connection cntConnection,
int iOperationType,
int iDataType,
java.lang.Object data)
Override this method to provide any custom error handling for unexpected error, which weren't handled by lower layer. |
protected void |
performOperation(DatabaseFactoryImpl dbfactory,
java.sql.Connection cntConnection,
java.sql.PreparedStatement pstmStatement)
Define content of this method to perform the database operation using the provided connection and optional prepared statement. |
protected void |
prepareData(DataObject data)
Define content of this method to perform the prepare data (update dataobject attribute). |
protected int |
setValuesForInsert(java.sql.PreparedStatement insertStatement,
DataObject data,
int initialIndex)
Method sets values to the prepared statement for insert of data object. |
protected int |
setValuesForUpdate(java.sql.PreparedStatement updateStatement,
DataObject data,
int initialIndex)
Method sets values to the prepared statement for update of data object. |
| Methods inherited from class org.opensubsystems.core.persist.db.DatabaseOperation |
|---|
getReturnData, setReturnData |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
public DatabaseUpdateOperation(DatabaseFactoryImpl factory,
int iUpdateType)
factory - - factory which is executing this operationiUpdateType - - type of update, one of the constants defined above
public DatabaseUpdateOperation(DatabaseFactoryImpl factory,
java.lang.String strQueryToPrepare,
ModifiableDatabaseSchema schema,
int iUpdateType,
java.lang.Object data)
factory - - factory which is executing this operationstrQueryToPrepare - - query which should be used to construct prepared
statement which will be passed in to executeUpdateschema - - database schema used with this operationiUpdateType - - type of update, one of the constants defined abovedata - - data used for operation| Method Detail |
|---|
public void executeUpdate()
throws OSSException
OSSException - - an error has occured
protected void performOperation(DatabaseFactoryImpl dbfactory,
java.sql.Connection cntConnection,
java.sql.PreparedStatement pstmStatement)
throws OSSException,
java.sql.SQLException
dbfactory - - database factory used for this operationcntConnection - - ready to use connection to perform the database
operation. No need to return this connection.pstmStatement - - prepared statement for query passed in as a
parameter to the constructor. No need to close
this statement. If no query was passed into
constructor, this will be null.
OSSException - - an error has occured
java.sql.SQLException - - an error has occuredprotected void prepareData(DataObject data)
data - - data object the attributes will be updated for
protected int setValuesForInsert(java.sql.PreparedStatement insertStatement,
DataObject data,
int initialIndex)
throws OSSException,
java.sql.SQLException
insertStatement - - prepared statement the values will be set up fordata - - data object to insert, based on the type of the data object
it can be determined what data are we insertinginitialIndex - - initial index for values to be set up into statement
OSSException - - exception during setting values
java.sql.SQLException - - exception during setting values
protected int setValuesForUpdate(java.sql.PreparedStatement updateStatement,
DataObject data,
int initialIndex)
throws OSSException,
java.sql.SQLException
updateStatement - - prepared statement the values will be set up fordata - - data object to update, based on the type of the data object
it can be determined what data are we updatinginitialIndex - - initial index for values to be set up into statement
OSSException - - exception during setting values
java.sql.SQLException - - exception during setting values
protected void handleSQLException(java.sql.SQLException sqleExc,
java.sql.Connection cntConnection,
int iOperationType,
int iDataType,
java.lang.Object data)
throws OSSException
sqleExc - - SQLException to handlecntConnection - - ready to use connection to perform the database
operation. No need to return this connection.iOperationType - - type of the operation that caused the exception,
see DatabaseOperations for possible valuesiDataType - - data type the data object represents (e.g if this is
type user and data is Integer, that means it is id
of user object). This is one of the DataConstant
constants.data - - data object the exception is handled for
OSSException - - properly handled exception
protected void handleKnownError(OSSException exc,
java.sql.Connection cntConnection,
int iOperationType,
int iDataType,
java.lang.Object data)
throws OSSException
exc - - known error which must be handled.cntConnection - - ready to use connection to perform the database
operation. No need to return this connection.iOperationType - - type of the operation that caused the exception,
see DatabaseOperations for possible valuesiDataType - - data type the data object represents (e.g if this is
type user and data is Integer, that means it is id
of user object). This is one of the DataConstant
constants.data - - data object the exception is handled for
OSSException - - properly handled exception
protected void handleUnknownError(java.lang.Throwable thr,
java.sql.Connection cntConnection,
int iOperationType,
int iDataType,
java.lang.Object data)
throws OSSException
thr - - throwable causing this error. This is not OSSException
or a derived class.cntConnection - - ready to use connection to perform the database
operation. No need to return this connection.iOperationType - - type of the operation that caused the exception,
see DatabaseOperations for possible valuesiDataType - - data type the data object represents (e.g if this is
type user and data is Integer, that means it is id
of user object). This is one of the DataConstant
constants.data - - data object the exception is handled for
OSSException - - properly handled exception
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||