Author: tfischer
Date: Wed May 9 13:34:45 2012
New Revision: 1336199
URL: http://svn.apache.org/viewvc?rev=1336199&view=rev
Log:
TORQE-200 generate methods to retrieve at most a single object
Modified:
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/conf/options.properties
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/base/doSelect.vm
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/doSelect.vm
db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/peer/SelectTest.java
Modified:
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/conf/options.properties
URL:
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/conf/options.properties?rev=1336199&r1=1336198&r2=1336199&view=diff
==============================================================================
---
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/conf/options.properties
(original)
+++
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/conf/options.properties
Wed May 9 13:34:45 2012
@@ -71,6 +71,9 @@ torque.om.bean.beanExtendsClass =
# using JCS.
torque.om.useManagers = false
+# Whether to generate methods which select at most a single record
+torque.om.addSelectSingleRecordMethods = true
+
# Whether a save method is generated in the data object classes.
torque.om.addSaveMethods = true
# Whether the save methods should be created in the
Modified:
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/base/doSelect.vm
URL:
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/base/doSelect.vm?rev=1336199&r1=1336198&r2=1336199&view=diff
==============================================================================
---
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/base/doSelect.vm
(original)
+++
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/base/doSelect.vm
Wed May 9 13:34:45 2012
@@ -31,7 +31,9 @@
* Selects ${dbObjectClassName} objects from a database.
*
* @param criteria object used to create the SELECT statement.
- * @return the List of selected Objects, not null.
+ *
+ * @return the list of selected objects, not null.
+ *
* @throws TorqueException Any exceptions caught during processing will be
* rethrown wrapped into a TorqueException.
*/
@@ -46,8 +48,10 @@
* within a transaction.
*
* @param criteria object used to create the SELECT statement.
- * @param connection the connection to use, not null.
- * @return List of selected Objects
+ * @param connection the connection holding the transaction, not null.
+ *
+ * @return The list of selected objects, not null.
+ *
* @throws TorqueException Any exceptions caught during processing will be
* rethrown wrapped into a TorqueException.
*/
@@ -63,6 +67,8 @@
* Selects ${dbObjectClassName} objects from the database which have
* the same content as the passed object.
*
+ * @return The list of selected objects, not null.
+ *
* @throws TorqueException Any exceptions caught during processing will be
* rethrown wrapped into a TorqueException.
*/
@@ -71,3 +77,59 @@
{
return ${peerImplGetter}().doSelect(obj);
}
+#if (${addSelectSingleRecordMethods} == "true")
+
+ /**
+ * Selects at most one ${dbObjectClassName} 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 static ${dbObjectClassName} doSelectSingleRecord(
+ Criteria criteria)
+ throws TorqueException
+ {
+ return ${peerImplGetter}().doSelectSingleRecord(criteria);
+ }
+
+ /**
+ * Selects at most one ${dbObjectClassName} 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 static ${dbObjectClassName} doSelectSingleRecord(
+ Criteria criteria,
+ Connection connection)
+ throws TorqueException
+ {
+ return ${peerImplGetter}().doSelectSingleRecord(criteria, connection);
+ }
+
+ /**
+ * Selects at most one ${dbObjectClassName} object from the database
+ * which has the same content as the passed object.
+ *
+ * @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 static ${dbObjectClassName} doSelectSingleRecord(
+ $dbObjectClassName obj)
+ throws TorqueException
+ {
+ return ${peerImplGetter}().doSelectSingleRecord(obj);
+ }
+#end
+
Modified:
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/doSelect.vm
URL:
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/doSelect.vm?rev=1336199&r1=1336198&r2=1336199&view=diff
==============================================================================
---
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/doSelect.vm
(original)
+++
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/doSelect.vm
Wed May 9 13:34:45 2012
@@ -31,7 +31,9 @@
* Selects ${dbObjectClassName} objects from a database.
*
* @param criteria object used to create the SELECT statement.
- * @return the List of selected Objects, not null.
+ *
+ * @return the list of selected objects, not null.
+ *
* @throws TorqueException Any exceptions caught during processing will be
* rethrown wrapped into a TorqueException.
*/
@@ -56,7 +58,9 @@
*
* @param criteria object used to create the SELECT statement.
* @param connection the connection to use, not null.
- * @return List of selected Objects
+ *
+ * @return the list of selected objects, not null.
+ *
* @throws TorqueException Any exceptions caught during processing will be
* rethrown wrapped into a TorqueException.
*/
@@ -82,6 +86,8 @@
* Selects ${dbObjectClassName} objects from the database which have
* the same content as the passed object.
*
+ * @return The list of selected objects, not null.
+ *
* @throws TorqueException Any exceptions caught during processing will be
* rethrown wrapped into a TorqueException.
*/
@@ -90,3 +96,92 @@
{
return doSelect(buildSelectCriteria(obj));
}
+#if (${addSelectSingleRecordMethods} == "true")
+
+ /**
+ * Selects at most one ${dbObjectClassName} 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 ${dbObjectClassName} doSelectSingleRecord(
+ Criteria criteria)
+ throws TorqueException
+ {
+ List<${dbObjectClassName}> ${field}List = doSelect(criteria);
+ ${dbObjectClassName} ${field} = null;
+ if (${field}List.size() > 1)
+ {
+ throw new TooManyRowsException("Criteria " + criteria
+ + " matched more than one record");
+ }
+ if (!${field}List.isEmpty())
+ {
+ ${field} = ${field}List.get(0);
+ }
+ return ${field};
+ }
+
+ /**
+ * Selects at most one ${dbObjectClassName} 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 ${dbObjectClassName} doSelectSingleRecord(
+ Criteria criteria,
+ Connection connection)
+ throws TorqueException
+ {
+ List<${dbObjectClassName}> ${field}List
+ = doSelect(criteria, connection);
+ ${dbObjectClassName} ${field} = null;
+ if (${field}List.size() > 1)
+ {
+ throw new TooManyRowsException("Criteria " + criteria
+ + " matched more than one record");
+ }
+ if (!${field}List.isEmpty())
+ {
+ ${field} = ${field}List.get(0);
+ }
+ return ${field};
+ }
+
+ /**
+ * Selects at most one ${dbObjectClassName} object from the database
+ * which has the same content as the passed object.
+ *
+ * @return the selected Object, or null if no object was selected.
+ *
+ * @throws TorqueException Any exceptions caught during processing will be
+ * rethrown wrapped into a TorqueException.
+ */
+ public ${dbObjectClassName} doSelectSingleRecord(
+ $dbObjectClassName obj)
+ throws TorqueException
+ {
+ List<${dbObjectClassName}> ${field}List = doSelect(obj);
+ ${dbObjectClassName} ${field} = null;
+ if (${field}List.size() > 1)
+ {
+ throw new TooManyRowsException("Object " + obj
+ + " matched more than one record");
+ }
+ if (!${field}List.isEmpty())
+ {
+ ${field} = ${field}List.get(0);
+ }
+ return ${field};
+ }
+#end
Modified:
db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/peer/SelectTest.java
URL:
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/peer/SelectTest.java?rev=1336199&r1=1336198&r2=1336199&view=diff
==============================================================================
---
db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/peer/SelectTest.java
(original)
+++
db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/peer/SelectTest.java
Wed May 9 13:34:45 2012
@@ -19,9 +19,12 @@ package org.apache.torque.generated.peer
* under the License.
*/
+import java.sql.Connection;
import java.util.List;
import org.apache.torque.BaseDatabaseTestCase;
+import org.apache.torque.TooManyRowsException;
+import org.apache.torque.Torque;
import org.apache.torque.criteria.Criteria;
import org.apache.torque.test.Author;
import org.apache.torque.test.AuthorPeer;
@@ -91,4 +94,99 @@ public class SelectTest extends BaseData
authorList.get(0).getAuthorId(),
author.getAuthorId());
}
+
+ /**
+ * Tests a select by the doSelectSingleRecord method.
+ *
+ * @throws Exception if the test fails
+ */
+ public void testSelectSingleRecord() throws Exception
+ {
+ Criteria criteria = new Criteria().where(
+ AuthorPeer.AUTHOR_ID,
+ authorList.get(0).getAuthorId());
+
+ Author author = AuthorPeer.doSelectSingleRecord(criteria);
+ assertEquals("Expected author with Id "
+ + authorList.get(0).getAuthorId()
+ + " but got "
+ + author.getAuthorId(),
+ authorList.get(0).getAuthorId(),
+ author.getAuthorId());
+ }
+
+ /**
+ * Tests a select by the doSelectSingleRecord method with a connection.
+ *
+ * @throws Exception if the test fails
+ */
+ public void testSelectSingleRecordWithConnection() throws Exception
+ {
+ Criteria criteria = new Criteria().where(
+ AuthorPeer.AUTHOR_ID,
+ authorList.get(0).getAuthorId());
+
+ Connection connection = Torque.getConnection();
+ Author author = AuthorPeer.doSelectSingleRecord(criteria, connection);
+ Torque.closeConnection(connection);
+ assertEquals("Expected author with Id "
+ + authorList.get(0).getAuthorId()
+ + " but got "
+ + author.getAuthorId(),
+ authorList.get(0).getAuthorId(),
+ author.getAuthorId());
+ }
+
+ /**
+ * Tests a select by the doSelectSingleRecord method by passing
+ * a database object.
+ *
+ * @throws Exception if the test fails
+ */
+ public void testSelectSingleRecordWithObject() throws Exception
+ {
+ Author author = AuthorPeer.doSelectSingleRecord(authorList.get(0));
+ assertEquals("Expected author with Id "
+ + authorList.get(0).getAuthorId()
+ + " but got "
+ + author.getAuthorId(),
+ authorList.get(0).getAuthorId(),
+ author.getAuthorId());
+ }
+
+ /**
+ * Tests a select by the doSelectSingleRecord method if no result is found.
+ *
+ * @throws Exception if the test fails
+ */
+ public void testSelectSingleRecordResultNull() throws Exception
+ {
+ Criteria criteria = new Criteria().where(
+ AuthorPeer.AUTHOR_ID,
+ -1);
+
+ Author author = AuthorPeer.doSelectSingleRecord(criteria);
+ assertEquals(null, author);
+ }
+
+ /**
+ * Tests a select by the doSelectSingleRecord method if too many records
+ * are found.
+ *
+ * @throws Exception if the test fails
+ */
+ public void testSelectSingleRecordTooManyRecords() throws Exception
+ {
+ Criteria criteria = new Criteria();
+
+ try
+ {
+ AuthorPeer.doSelectSingleRecord(criteria);
+ fail("Exception expected");
+ }
+ catch (TooManyRowsException e)
+ {
+ // expected
+ }
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]