Author: tv
Date: Thu Aug 9 18:37:09 2012
New Revision: 1371365
URL: http://svn.apache.org/viewvc?rev=1371365&view=rev
Log:
- Made BasePeerImpl generic
- Added injection of database name, record mapper and table map
- Moved lots of methods from the generated BaseXXXPeerImpl classes to
BasePeerImpl
Modified:
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/BasePeer.java
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/BasePeerImpl.java
Modified:
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/BasePeer.java
URL:
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/BasePeer.java?rev=1371365&r1=1371364&r2=1371365&view=diff
==============================================================================
---
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/BasePeer.java
(original)
+++
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/BasePeer.java
Thu Aug 9 18:37:09 2012
@@ -54,7 +54,7 @@ public abstract class BasePeer
private static final long serialVersionUID = -7702123730779032381L;
/** The BasePeerImpl instance to delegate to. */
- private static BasePeerImpl basePeerImpl;
+ private static BasePeerImpl<?> basePeerImpl;
/**
* Returns an instance of the implementation of the peer.
@@ -62,15 +62,16 @@ public abstract class BasePeer
*
* @return an instance of the implementation of the peer, not null.
*/
- public static BasePeerImpl getBasePeerImpl()
+ @SuppressWarnings("unchecked")
+ public static <T> BasePeerImpl<T> getBasePeerImpl()
{
- BasePeerImpl peerImplInstance = basePeerImpl;
+ BasePeerImpl<?> peerImplInstance = basePeerImpl;
if (peerImplInstance == null)
{
peerImplInstance = new BasePeerImpl();
basePeerImpl = peerImplInstance;
}
- return peerImplInstance;
+ return (BasePeerImpl<T>)peerImplInstance;
}
/**
@@ -79,7 +80,7 @@ public abstract class BasePeer
* @param basePeerImpl the implementation, or null to force creation of a
* new instance.
*/
- public static void setBasePeerImpl(BasePeerImpl basePeerImpl)
+ public static void setBasePeerImpl(BasePeerImpl<?> basePeerImpl)
{
BasePeer.basePeerImpl = basePeerImpl;
}
@@ -417,7 +418,8 @@ public abstract class BasePeer
TableMap defaultTableMap)
throws TorqueException
{
- return getBasePeerImpl().doSelect(criteria, mapper, defaultTableMap);
+ BasePeerImpl<T> bpi = getBasePeerImpl();
+ return bpi.doSelect(criteria, mapper, defaultTableMap);
}
/**
@@ -439,7 +441,8 @@ public abstract class BasePeer
TableMap defaultTableMap)
throws TorqueException
{
- return getBasePeerImpl().doSelect(criteria, mapper, defaultTableMap);
+ BasePeerImpl<T> bpi = getBasePeerImpl();
+ return bpi.doSelect(criteria, mapper, defaultTableMap);
}
/**
@@ -468,7 +471,8 @@ public abstract class BasePeer
Connection connection)
throws TorqueException
{
- return getBasePeerImpl().doSelect(
+ BasePeerImpl<T> bpi = getBasePeerImpl();
+ return bpi.doSelect(
criteria,
mapper,
defaultTableMap,
@@ -496,7 +500,8 @@ public abstract class BasePeer
Connection connection)
throws TorqueException
{
- return getBasePeerImpl().doSelect(
+ BasePeerImpl<T> bpi = getBasePeerImpl();
+ return bpi.doSelect(
criteria,
mapper,
defaultTableMap,
@@ -525,7 +530,8 @@ public abstract class BasePeer
String dbName)
throws TorqueException
{
- return getBasePeerImpl().doSelect(
+ BasePeerImpl<T> bpi = getBasePeerImpl();
+ return bpi.doSelect(
query,
mapper,
defaultTableMap,
@@ -553,7 +559,8 @@ public abstract class BasePeer
Connection connection)
throws TorqueException
{
- return getBasePeerImpl().doSelect(
+ BasePeerImpl<T> bpi = getBasePeerImpl();
+ return bpi.doSelect(
query,
mapper,
defaultTableMap,
@@ -579,7 +586,8 @@ public abstract class BasePeer
TableMap defaultTableMap)
throws TorqueException
{
- return getBasePeerImpl().doSelectSingleRecord(
+ BasePeerImpl<T> bpi = getBasePeerImpl();
+ return bpi.doSelectSingleRecord(
criteria,
mapper,
defaultTableMap);
@@ -606,7 +614,8 @@ public abstract class BasePeer
Connection connection)
throws TorqueException
{
- return getBasePeerImpl().doSelectSingleRecord(
+ BasePeerImpl<T> bpi = getBasePeerImpl();
+ return bpi.doSelectSingleRecord(
criteria,
mapper,
defaultTableMap,
Modified:
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/BasePeerImpl.java
URL:
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/BasePeerImpl.java?rev=1371365&r1=1371364&r2=1371365&view=diff
==============================================================================
---
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/BasePeerImpl.java
(original)
+++
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/BasePeerImpl.java
Thu Aug 9 18:37:09 2012
@@ -72,7 +72,7 @@ import org.apache.torque.sql.SqlBuilder;
* @author <a href="mailto:[email protected]">Henning P. Schmiedehausen</a>
* @version $Id$
*/
-public class BasePeerImpl implements Serializable
+public class BasePeerImpl<T> implements Serializable
{
/**
* Serial Version
@@ -82,14 +82,137 @@ public class BasePeerImpl implements Ser
/** the log */
private static final Log log = LogFactory.getLog(BasePeer.class);
- private static void throwTorqueException(Exception e)
- throws TorqueException
+ /** An injected instance of a record mapper to map JDBC result sets to
objects */
+ private RecordMapper<T> recordMapper = null;
+
+ /** An injected instance of a table map */
+ private TableMap tableMap = null;
+
+ /** An injected instance of the database name */
+ private String databaseName = null;
+
+ /**
+ * Default constructor
+ */
+ public BasePeerImpl()
+ {
+ super();
+ }
+
+ /**
+ * Constructor providing the objects to be injected as parameters
+ *
+ * @param recordMapper a record mapper to map JDBC result sets to objects
+ * @param tableMap the default table map
+ * @param databaseName the name of the database
+ */
+ public BasePeerImpl(RecordMapper<T> recordMapper, TableMap tableMap,
String databaseName)
{
- if (e instanceof TorqueException)
+ this();
+ setRecordMapper(recordMapper);
+ setTableMap(tableMap);
+ setDatabaseName(databaseName);
+ }
+
+ /**
+ * Set the record mapper for this instance
+ *
+ * @param recordMapper the recordMapper to set
+ */
+ public void setRecordMapper(RecordMapper<T> recordMapper)
+ {
+ this.recordMapper = recordMapper;
+ }
+
+ /**
+ * Get the record mapper for this instance.
+ *
+ * @return the recordMapper
+ */
+ public RecordMapper<T> getRecordMapper()
+ {
+ if (recordMapper == null)
{
- throw (TorqueException) e;
+ throw new RuntimeException("No record mapper injected");
}
- else
+
+ return recordMapper;
+ }
+
+ /**
+ * Set the default table map for this instance
+ *
+ * @param tableMap the tableMap to set
+ */
+ public void setTableMap(TableMap tableMap)
+ {
+ this.tableMap = tableMap;
+ }
+
+ /**
+ * Get the default table map for this instance
+ *
+ * @return the tableMap
+ */
+ public TableMap getTableMap()
+ {
+ if (tableMap == null)
+ {
+ throw new RuntimeException("No table map injected");
+ }
+
+ return tableMap;
+ }
+
+ /**
+ * Set the database name for this instance
+ *
+ * @param databaseName the databaseName to set
+ */
+ public void setDatabaseName(String databaseName)
+ {
+ this.databaseName = databaseName;
+ }
+
+ /**
+ * Get the database name for this instance
+ *
+ * @return the databaseName
+ */
+ public String getDatabaseName()
+ {
+ if (databaseName == null)
+ {
+ throw new RuntimeException("No database name injected");
+ }
+
+ return databaseName;
+ }
+
+ /**
+ * Get the list of objects for a ResultSet. Please not that your
+ * resultset MUST return columns in the right order. You can use
+ * getFieldNames() in BaseObject to get the correct sequence.
+ *
+ * @param resultSet the ResultSet
+ * @return the list of objects
+ * @throws TorqueException Any exceptions caught during processing will be
+ * rethrown wrapped into a TorqueException.
+ */
+ public List<T> resultSet2Objects(java.sql.ResultSet resultSet)
+ throws TorqueException
+ {
+ try
+ {
+ List<T> result = new ArrayList<T>();
+ RecordMapper<T> mapper = getRecordMapper();
+ while (resultSet.next())
+ {
+ result.add(mapper.processRow(resultSet, 0));
+ }
+ return result;
+ }
+ catch (SQLException e)
{
throw new TorqueException(e);
}
@@ -222,25 +345,56 @@ public class BasePeerImpl implements Ser
}
/**
- * Method to perform deletes based on conditions in a Criteria.
+ * Deletes rows from a database table.
*
- * @param criteria The criteria to use.
+ * @param criteria defines the rows to be deleted, not null.
*
* @return the number of deleted rows.
*
* @throws TorqueException Any exceptions caught during processing will be
* rethrown wrapped into a TorqueException.
+ */
+ public int doDelete(org.apache.torque.criteria.Criteria criteria) throws
TorqueException
+ {
+ Connection connection = null;
+ try
+ {
+ setDbName(criteria);
+ connection = Transaction.begin(criteria.getDbName());
+ int deletedRows = doDelete(criteria, connection);
+ Transaction.commit(connection);
+ connection = null;
+ return deletedRows;
+ }
+ finally
+ {
+ if (connection != null)
+ {
+ Transaction.safeRollback(connection);
+ }
+ }
+ }
+
+ /**
+ * Deletes rows from a table. This method is to be used
+ * during a transaction, otherwise use the doDelete(Criteria) method.
+ *
+ * @param criteria defines the rows to be deleted, not null.
+ * @param con the connection to use, not null.
*
- * @deprecated This method causes unexpected results when joins are used.
- * Please use doDelete(
- * org.apache.torque.criteria.Criteria, TableMap).
- * This method will be removed in a future version of Torque.
+ * @return the number of deleted rows.
+ *
+ * @throws TorqueException Any exceptions caught during processing will be
+ * rethrown wrapped into a TorqueException.
*/
- @Deprecated
- protected int doDelete(Criteria criteria) throws TorqueException
- {
- return doDelete(criteria, (TableMap) null);
- }
+ public int doDelete(org.apache.torque.criteria.Criteria criteria,
Connection con)
+ throws TorqueException
+ {
+ correctBooleans(criteria);
+ setDbName(criteria);
+
+ return doDelete(criteria, getTableMap(), con);
+ }
/**
* Method to perform deletes based on conditions in a Criteria.
@@ -258,8 +412,7 @@ public class BasePeerImpl implements Ser
* This method will be removed in a future version of Torque.
*/
@Deprecated
- protected int doDelete(org.apache.torque.criteria.Criteria criteria)
- throws TorqueException
+ protected int doDelete(Criteria criteria) throws TorqueException
{
return doDelete(criteria, (TableMap) null);
}
@@ -301,44 +454,6 @@ public class BasePeerImpl implements Ser
}
/**
- * Method to perform deletes based on conditions a Criteria.
- *
- * @param criteria The criteria to use.
- * @param con the Connection to be used for deleting.
- *
- * @return the number of deleted rows.
- *
- * @throws TorqueException Any exceptions caught during processing will be
- * rethrown wrapped into a TorqueException.
- *
- * @deprecated This method causes unexpected results when joins are used.
- * Please use doDelete(
- * org.apache.torque.criteria.Criteria, TableMap,
Connection).
- * This method will be removed in a future version of Torque.
- */
- @Deprecated
- protected int doDelete(
- org.apache.torque.criteria.Criteria criteria,
- Connection con)
- throws TorqueException
- {
- org.apache.torque.criteria.Criterion criterion
- = criteria.getTopLevelCriterion();
- if (criterion == null)
- {
- throw new TorqueException("No conditions found in Criteria");
- }
- TableMap tableMap = MapHelper.getTableMap(
- criterion.getLValue(), criteria, null);
- if (tableMap == null)
- {
- throw new TorqueException("Unqualified column name in criteria"
- + " or table name not found in database map");
- }
- return doDelete(criteria, tableMap, con);
- }
-
- /**
* Method to perform deletes based on conditions in a Criteria.
*
* @param criteria The criteria to use.
@@ -902,12 +1017,151 @@ public class BasePeerImpl implements Ser
}
catch (Exception e)
{
- throwTorqueException(e);
+ if (e instanceof TorqueException)
+ {
+ throw (TorqueException) e;
+ }
+ else
+ {
+ throw new TorqueException(e);
+ }
}
+
return id;
}
/**
+ * Add all the columns needed to create a new object.
+ *
+ * @param criteria object containing the columns to add.
+ * @throws TorqueException Any exceptions caught during processing will be
+ * rethrown wrapped into a TorqueException.
+ */
+ public void addSelectColumns(org.apache.torque.criteria.Criteria criteria)
+ throws TorqueException
+ {
+ ColumnMap[] columns = this.tableMap.getColumns();
+
+ for (ColumnMap c : columns)
+ {
+ criteria.addSelectColumn(c);
+ }
+ }
+
+ /**
+ * Selects objects from a database.
+ *
+ * @param criteria object used to create the SELECT statement.
+ *
+ * @return the list of selected objects, not null.
+ *
+ * @throws TorqueException Any exceptions caught during processing will be
+ * rethrown wrapped into a TorqueException.
+ */
+ public List<T> doSelect(org.apache.torque.criteria.Criteria criteria)
+ throws TorqueException
+ {
+ if (criteria.getSelectColumns().size() == 0)
+ {
+ addSelectColumns(criteria);
+ }
+ setDbName(criteria);
+
+ return doSelect(
+ criteria,
+ getRecordMapper(),
+ getTableMap());
+ }
+
+ /**
+ * Selects objects from a database
+ * within a transaction.
+ *
+ * @param criteria object used to create the SELECT statement.
+ * @param connection the connection to use, not null.
+ *
+ * @return the list of selected objects, not null.
+ *
+ * @throws TorqueException Any exceptions caught during processing will be
+ * rethrown wrapped into a TorqueException.
+ */
+ public List<T> doSelect(
+ org.apache.torque.criteria.Criteria criteria,
+ Connection connection)
+ throws TorqueException
+ {
+ if (criteria.getSelectColumns().size() == 0)
+ {
+ addSelectColumns(criteria);
+ }
+ setDbName(criteria);
+
+ return doSelect(
+ criteria,
+ getRecordMapper(),
+ getTableMap(),
+ connection);
+ }
+
+ /**
+ * Selects at most one object from a database.
+ *
+ * @param criteria object used to create the SELECT statement.
+ *
+ * @return the selected Object, or null if no object was selected.
+ *
+ * @throws TorqueException If more than one record is selected or if
+ * an error occurs when processing the query.
+ */
+ public T doSelectSingleRecord(org.apache.torque.criteria.Criteria criteria)
+ throws TorqueException
+ {
+ List<T> recordList = doSelect(criteria);
+ T record = null;
+ if (recordList.size() > 1)
+ {
+ throw new TooManyRowsException("Criteria " + criteria
+ + " matched more than one record");
+ }
+ if (!recordList.isEmpty())
+ {
+ record = recordList.get(0);
+ }
+ return record;
+ }
+
+ /**
+ * Selects at most one object from a database
+ * within a transaction.
+ *
+ * @param criteria object used to create the SELECT statement.
+ * @param connection the connection holding the transaction, not null.
+ *
+ * @return the selected Object, or null if no object was selected.
+ *
+ * @throws TorqueException If more than one record is selected or if
+ * an error occurs when processing the query.
+ */
+ public T doSelectSingleRecord(
+ org.apache.torque.criteria.Criteria criteria,
+ Connection connection)
+ throws TorqueException
+ {
+ List<T> recordList = doSelect(criteria, connection);
+ T record = null;
+ if (recordList.size() > 1)
+ {
+ throw new TooManyRowsException("Criteria " + criteria
+ + " matched more than one record");
+ }
+ if (!recordList.isEmpty())
+ {
+ record = recordList.get(0);
+ }
+ return record;
+ }
+
+ /**
* Selects rows from a database an maps them to objects.
*
* @param criteria A Criteria specifying the records to select, not null.
@@ -925,9 +1179,9 @@ public class BasePeerImpl implements Ser
* This method will be removed in a future version of Torque.
*/
@Deprecated
- public <T> List<T> doSelect(
+ public <TT> List<TT> doSelect(
Criteria criteria,
- RecordMapper<T> mapper,
+ RecordMapper<TT> mapper,
TableMap defaultTableMap)
throws TorqueException
{
@@ -936,7 +1190,7 @@ public class BasePeerImpl implements Ser
{
connection = Transaction.begin(criteria.getDbName());
- List<T> result = doSelect(
+ List<TT> result = doSelect(
criteria,
mapper,
defaultTableMap,
@@ -968,9 +1222,9 @@ public class BasePeerImpl implements Ser
*
* @throws TorqueException if querying the database fails.
*/
- public <T> List<T> doSelect(
+ public <TT> List<TT> doSelect(
org.apache.torque.criteria.Criteria criteria,
- RecordMapper<T> mapper,
+ RecordMapper<TT> mapper,
TableMap defaultTableMap)
throws TorqueException
{
@@ -979,7 +1233,7 @@ public class BasePeerImpl implements Ser
{
connection = Transaction.begin(criteria.getDbName());
- List<T> result = doSelect(
+ List<TT> result = doSelect(
criteria,
mapper,
defaultTableMap,
@@ -1013,9 +1267,9 @@ public class BasePeerImpl implements Ser
*
* @throws TorqueException if querying the database fails.
*/
- public <T> List<T> doSelect(
+ public <TT> List<TT> doSelect(
String query,
- RecordMapper<T> mapper,
+ RecordMapper<TT> mapper,
TableMap defaultTableMap,
String dbName)
throws TorqueException
@@ -1029,7 +1283,7 @@ public class BasePeerImpl implements Ser
{
connection = Transaction.begin(dbName);
- List<T> result = doSelect(
+ List<TT> result = doSelect(
query,
mapper,
defaultTableMap,
@@ -1062,9 +1316,9 @@ public class BasePeerImpl implements Ser
*
* @throws TorqueException if querying the database fails.
*/
- public <T> List<T> doSelect(
+ public <TT> List<TT> doSelect(
String query,
- RecordMapper<T> mapper,
+ RecordMapper<TT> mapper,
TableMap defaultTableMap,
Connection connection)
throws TorqueException
@@ -1074,7 +1328,7 @@ public class BasePeerImpl implements Ser
throw new NullPointerException("connection is null");
}
- List<T> result = new ArrayList<T>();
+ List<TT> result = new ArrayList<TT>();
Statement statement = null;
ResultSet resultSet = null;
try
@@ -1090,7 +1344,7 @@ public class BasePeerImpl implements Ser
while (resultSet.next())
{
- T rowResult = mapper.processRow(resultSet, 0);
+ TT rowResult = mapper.processRow(resultSet, 0);
result.add(rowResult);
}
long mappingEndTime = System.currentTimeMillis();
@@ -1149,9 +1403,9 @@ public class BasePeerImpl implements Ser
* This method will be removed in a future version of Torque.
*/
@Deprecated
- public <T> List<T> doSelect(
+ public <TT> List<TT> doSelect(
Criteria criteria,
- RecordMapper<T> mapper,
+ RecordMapper<TT> mapper,
TableMap defaultTableMap,
Connection connection)
throws TorqueException
@@ -1223,7 +1477,7 @@ public class BasePeerImpl implements Ser
}
}
- List<T> result = new ArrayList<T>();
+ List<TT> result = new ArrayList<TT>();
int rowNumber = 0;
while (resultSet.next())
{
@@ -1237,7 +1491,7 @@ public class BasePeerImpl implements Ser
break;
}
- T rowResult = mapper.processRow(resultSet, 0);
+ TT rowResult = mapper.processRow(resultSet, 0);
result.add(rowResult);
rowNumber++;
@@ -1300,9 +1554,9 @@ public class BasePeerImpl implements Ser
*
* @throws TorqueException Error performing database query.
*/
- public <T> List<T> doSelect(
+ public <TT> List<TT> doSelect(
org.apache.torque.criteria.Criteria criteria,
- RecordMapper<T> mapper,
+ RecordMapper<TT> mapper,
TableMap defaultTableMap,
Connection connection)
throws TorqueException
@@ -1378,7 +1632,7 @@ public class BasePeerImpl implements Ser
}
}
- List<T> result = new ArrayList<T>();
+ List<TT> result = new ArrayList<TT>();
int rowNumber = 0;
while (resultSet.next())
{
@@ -1392,7 +1646,7 @@ public class BasePeerImpl implements Ser
break;
}
- T rowResult = mapper.processRow(resultSet, 0);
+ TT rowResult = mapper.processRow(resultSet, 0);
result.add(rowResult);
rowNumber++;
@@ -1453,7 +1707,7 @@ public class BasePeerImpl implements Ser
*
* @throws TorqueException if querying the database fails.
*/
- public <T> T doSelectSingleRecord(
+ public T doSelectSingleRecord(
org.apache.torque.criteria.Criteria criteria,
RecordMapper<T> mapper,
TableMap defaultTableMap)
@@ -1487,7 +1741,7 @@ public class BasePeerImpl implements Ser
*
* @throws TorqueException if querying the database fails.
*/
- public <T> T doSelectSingleRecord(
+ public T doSelectSingleRecord(
org.apache.torque.criteria.Criteria criteria,
RecordMapper<T> mapper,
TableMap defaultTableMap,
@@ -2063,6 +2317,19 @@ public class BasePeerImpl implements Ser
return result;
}
+ /**
+ * changes the boolean values in the criteria to the appropriate type,
+ * whenever a booleanchar or booleanint column is involved.
+ * This enables the user to create criteria using Boolean values
+ * for booleanchar or booleanint columns
+ * @param criteria the criteria in which the boolean values should be
corrected
+ * @throws TorqueException if the database map for the criteria cannot be
+ obtained.
+ */
+ public void correctBooleans(org.apache.torque.criteria.Criteria criteria)
throws TorqueException
+ {
+ correctBooleans(criteria, getTableMap());
+ }
/**
* Checks all columns in the criteria to see whether
@@ -2354,4 +2621,22 @@ public class BasePeerImpl implements Ser
}
}
}
+
+ /**
+ * Sets the database name in the passed criteria to the table's default,
+ * if it is not already set.
+ *
+ * @param crit the criteria to set the database name in, not null.
+ */
+ protected void setDbName(org.apache.torque.criteria.Criteria crit) throws
TorqueException
+ {
+ if (!Torque.isInit())
+ {
+ throw new TorqueException("Torque is not initialized");
+ }
+ if (crit.getDbName() == null)
+ {
+ crit.setDbName(getDatabaseName());
+ }
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]