|
|||||||||
| 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.DatabaseReadOperation
public abstract class DatabaseReadOperation
Adapter to simplify writing of database reads, 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 read. Optionally you may want to override one of the handleXXX methods to provide custom error handling. Example of method in factory which reads data using query produced by its schema public DataObject get( final int iDomainId, final int iId ) throws OSSException { DatabaseReadOperation dbop = new DatabaseReadOperation( this, m_schema.getSelectMyDataById(MyDatabaseSchema.MYDATA_COLUMNS), m_schema, dataType) { protected Object performOperation( DatabaseFactoryImpl dbfactory, Connection cntConnection, PreparedStatement pstmQuery ) throws OSSException, SQLException { pstmQuery.setInt(1, iId); pstmQuery.setInt(2, iDomainId); return DatabaseUtils.loadAtMostOneData(dbfactory, pstmQuery, "Multiple records loaded from database for domain ID " + iDomainId + " and ID " + iId); } }; return (DataObject)dbop.executeRead(); }
| 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 | |
|---|---|
DatabaseReadOperation(DatabaseFactoryImpl factory)
Constructor to use when the database read doesn't require any prepared statement. |
|
DatabaseReadOperation(DatabaseFactoryImpl factory,
java.lang.String strQueryToPrepare,
DatabaseSchema schema)
Copy constructor to use when database read doesn't require any prepared statement. |
|
DatabaseReadOperation(DatabaseFactoryImpl factory,
java.lang.String strQueryToPrepare,
DatabaseSchema schema,
int dataType)
Constructor to use when database read doesn't require any prepared statement. |
|
| Method Summary | |
|---|---|
java.lang.Object |
executeRead()
Method to execute database read 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 java.lang.Object |
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 java.sql.PreparedStatement |
prepareQuery(DatabaseFactoryImpl dbfactory,
java.sql.Connection cntConnection,
java.lang.String strQuery)
Prepare the query if it was specified using the provided connection. |
| 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 DatabaseReadOperation(DatabaseFactoryImpl factory)
factory - - factory which is executing this operation
public DatabaseReadOperation(DatabaseFactoryImpl factory,
java.lang.String strQueryToPrepare,
DatabaseSchema schema,
int dataType)
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 operationdataType - - data type used with operation
public DatabaseReadOperation(DatabaseFactoryImpl factory,
java.lang.String strQueryToPrepare,
DatabaseSchema schema)
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 operation| Method Detail |
|---|
public java.lang.Object executeRead()
throws OSSException
OSSException - - an error has occured
protected java.sql.PreparedStatement prepareQuery(DatabaseFactoryImpl dbfactory,
java.sql.Connection cntConnection,
java.lang.String strQuery)
throws OSSException,
java.sql.SQLException
dbfactory - - database factory executing this operationcntConnection - - ready to use connection to perform the database
operation. No need to return this connection.strQuery - - query to prepare, might be null or empty if there is
nothing to prepare
OSSException - - an error has occured
java.sql.SQLException - - an error has occured
protected java.lang.Object performOperation(DatabaseFactoryImpl dbfactory,
java.sql.Connection cntConnection,
java.sql.PreparedStatement pstmStatement)
throws OSSException,
java.sql.SQLException
dbfactory - - database factory executing 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 occured
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 | ||||||||