Author: tfischer
Date: Wed Jul 6 14:10:18 2011
New Revision: 1143425
URL: http://svn.apache.org/viewvc?rev=1143425&view=rev
Log:
TORQUE-9
implement doDelete(List<DataObject>) in Peer
Modified:
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/base/buildCriteria.vm
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/base/doDelete.vm
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/buildCriteria.vm
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/doDelete.vm
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/retrieveByPKs.vm
Modified:
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/base/buildCriteria.vm
URL:
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/base/buildCriteria.vm?rev=1143425&r1=1143424&r2=1143425&view=diff
==============================================================================
---
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/base/buildCriteria.vm
(original)
+++
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/base/buildCriteria.vm
Wed Jul 6 14:10:18 2011
@@ -27,6 +27,8 @@
## The options and the attributes of the current source element must be set
## as velocity variables.
##
+#set ( $primaryKeyColumnElements =
$torqueGen.getChild("primary-keys").getChildren("column") )
+#if (!$primaryKeyColumnElements.isEmpty())
/**
* Build a Criteria object which selects all objects which have a given
* primary key.
@@ -39,15 +41,40 @@
}
/**
+ * Build a Criteria object which selects all objects which primary keys
+ * are contained in the passed collection.
+ *
+ * @param pks the primary key values to build the criteria from, not null,
+ * may not contain null.
+ */
+ public static Criteria buildCriteria(Collection<ObjectKey> pks)
+ {
+ return ${peerImplGetter}().buildCriteria(pks);
+ }
+
+ /**
+ * Build a Criteria object which selects all passed objects using their
+ * primary key. Objects which do not yet have a primary key are ignored.
+ *
+ * @param objects the objects to build the criteria from, not null,
+ * may not contain null.
+ */
+ public static Criteria buildPkCriteria(
+ Collection<${dbObjectClassName}> objects)
+ {
+ return ${peerImplGetter}().buildPkCriteria(objects);
+ }
+
+#end
+ /**
* Build a Criteria object from the data object for this peer.
-#set ( $primaryKeyColumnElements =
$torqueGen.getChild("primary-keys").getChildren("column") )
#if (!$idMethod.equals("none") && $primaryKeyColumnElements.size() > 1)
* The primary key columns are only added if the object is not new.
#end
*
* @param obj the object to build the criteria from, not null.
*/
- public static Criteria buildCriteria($dbObjectClassName obj)
+ public static Criteria buildCriteria(${dbObjectClassName} obj)
{
return ${peerImplGetter}().buildCriteria(obj);
}
Modified:
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/base/doDelete.vm
URL:
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/base/doDelete.vm?rev=1143425&r1=1143424&r2=1143425&view=diff
==============================================================================
---
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/base/doDelete.vm
(original)
+++
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/base/doDelete.vm
Wed Jul 6 14:10:18 2011
@@ -43,9 +43,11 @@
}
/**
- * Deletes rows from a table. This method is to be used
- * during a transaction, otherwise use the doDelete(Criteria) method.
- *
+ * Deletes rows from a table.
+ * This method uses the passed connection to delete the rows;
+ * if a transaction is open in the connection, the deletion happens inside
+ * this transaction.
+ *
* @param criteria defines the rows to be deleted, not null.
* @param con the connection to use, not null.
*
@@ -70,15 +72,16 @@
* @throws TorqueException Any exceptions caught during processing will be
* rethrown wrapped into a TorqueException.
*/
- public static int doDelete($dbObjectClassName obj) throws TorqueException
+ public static int doDelete(${dbObjectClassName} obj) throws TorqueException
{
return ${peerImplGetter}().doDelete(obj);
}
/**
* Deletes a data object, i.e. a row in a table, in the database.
- * This method is to be used during a transaction, otherwise use the
- * doDelete($dbObjectClassName) method.
+ * This method uses the passed connection to delete the rows;
+ * if a transaction is open in the connection, the deletion happens inside
+ * this transaction.
*
* @param obj the data object to delete in the database, not null.
* @param con the connection to use, not null.
@@ -88,13 +91,54 @@
* @throws TorqueException Any exceptions caught during processing will be
* rethrown wrapped into a TorqueException.
*/
- public static int doDelete($dbObjectClassName obj, Connection con)
+ public static int doDelete(${dbObjectClassName} obj, Connection con)
throws TorqueException
{
- ## TODO use buildSelectCriteria if no pk is present
return ${peerImplGetter}().doDelete(obj, con);
}
+#set ( $primaryKeyColumnElements =
$torqueGen.getChild("primary-keys").getChildren("column"))
+#if (!$primaryKeyColumnElements.isEmpty())
+ /**
+ * Deletes data objects, i.e. rows in a table, in the database.
+ *
+ * @param objects the datas object to delete in the database, not null,
+ * may not contain null.
+ *
+ * @return the number of deleted rows.
+ *
+ * @throws TorqueException Any exceptions caught during processing will be
+ * rethrown wrapped into a TorqueException.
+ */
+ public static int doDelete(Collection<${dbObjectClassName}> objects)
+ throws TorqueException
+ {
+ return ${peerImplGetter}().doDelete(objects);
+ }
+
+ /**
+ * Deletes data objects, i.e. rows in a table, in the database.
+ * This method uses the passed connection to delete the rows;
+ * if a transaction is open in the connection, the deletion happens inside
+ * this transaction.
+ *
+ * @param objects the datas object to delete in the database, not null,
+ * may not contain null.
+ * @param con the connection to use for deleting, not null.
+ *
+ * @return the number of deleted rows.
+ *
+ * @throws TorqueException Any exceptions caught during processing will be
+ * rethrown wrapped into a TorqueException.
+ */
+ public static int doDelete(
+ Collection<${dbObjectClassName}> objects,
+ Connection con)
+ throws TorqueException
+ {
+ return ${peerImplGetter}().doDelete(objects, con);
+ }
+
/**
* Deletes a row in the database.
*
@@ -112,8 +156,9 @@
/**
* Deletes a row in the database.
- * This method is to be used during a transaction,
- * otherwise use the doDelete(ObjectKey) method.
+ * This method uses the passed connection to delete the rows;
+ * if a transaction is open in the connection, the deletion happens inside
+ * this transaction.
*
* @param pk the ObjectKey that identifies the row to delete.
* @param con the connection to use for deleting, not null.
@@ -128,3 +173,4 @@
{
return ${peerImplGetter}().doDelete(pk, con);
}
+#end
Modified:
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/buildCriteria.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/buildCriteria.vm?rev=1143425&r1=1143424&r2=1143425&view=diff
==============================================================================
---
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/buildCriteria.vm
(original)
+++
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/buildCriteria.vm
Wed Jul 6 14:10:18 2011
@@ -27,6 +27,8 @@
## The options and the attributes of the current source element must be set
## as velocity variables.
##
+#set ( $primaryKeyColumnElements =
$torqueGen.getChild("primary-keys").getChildren("column") )
+#if (!$primaryKeyColumnElements.isEmpty())
/**
* Build a Criteria object which selects all objects which have a given
* primary key.
@@ -36,27 +38,85 @@
public Criteria buildCriteria(ObjectKey pk)
{
Criteria criteria = new Criteria();
-#set ( $primaryKeyColumnElements =
$torqueGen.getChild("primary-keys").getChildren("column") )
-#if ($primaryKeyColumnElements.size() == 1)
- #set ( $primaryKeyColumnElement = $primaryKeyColumnElements.get(0) )
- #set ( $peerColumnName =
$primaryKeyColumnElement.getAttribute("peerColumnName") )
+ #if ($primaryKeyColumnElements.size() == 1)
+ #set ( $primaryKeyColumnElement = $primaryKeyColumnElements.get(0) )
+ #set ( $peerColumnName =
$primaryKeyColumnElement.getAttribute("peerColumnName") )
criteria.add(${peerClassName}.$peerColumnName, pk);
-#else
+ #else
SimpleKey[] keys = (SimpleKey[])pk.getValue();
- #set ( $i = 0 )
- #foreach ($primaryKeyColumnElement in $primaryKeyColumnElements)
- #set ( $peerColumnName =
$primaryKeyColumnElement.getAttribute("peerColumnName") )
+ #set ( $i = 0 )
+ #foreach ($primaryKeyColumnElement in $primaryKeyColumnElements)
+ #set ( $peerColumnName =
$primaryKeyColumnElement.getAttribute("peerColumnName") )
criteria.add(${peerClassName}.$peerColumnName, keys[$i]);
- #set ( $i = $i + 1 )
+ #set ( $i = $i + 1 )
+ #end
+ #end
+ return criteria;
+ }
+
+ /**
+ * Build a Criteria object which selects all objects which primary keys
+ * are contained in the passed collection.
+ *
+ * @param pks the primary key values to build the criteria from, not null,
+ * may not contain null.
+ */
+ public Criteria buildCriteria(Collection<ObjectKey> pks)
+ {
+ Criteria criteria = new Criteria();
+ #if ($primaryKeyColumnElements.size() == 1)
+ #set ( $columnElement = $primaryKeyColumnElements.get(0) )
+ #set ( $peerColumnName = $columnElement.getAttribute("peerColumnName"))
+ criteria.addIn(${peerClassName}.$peerColumnName, pks);
+ #else
+ for (ObjectKey pk : pks)
+ {
+ SimpleKey[] keys = (SimpleKey[])pk.getValue();
+ #set ( $i = 0 )
+ #foreach ($primaryKeyColumnElement in $primaryKeyColumnElements)
+ #set ( $peerColumnName =
$primaryKeyColumnElement.getAttribute("peerColumnName"))
+ Criteria.Criterion c$i = criteria.getNewCriterion(
+ ${peerClassName}.$peerColumnName, keys[$i],
Criteria.EQUAL);
+ #set ( $j = $i - 1 )
+ #if ($i > 0)
+ c${j}.and(c${i});
+ #end
+ #set ( $i = $i + 1 )
+ #end
+ criteria.or(c0);
+ }
#end
-#end
return criteria;
}
+
+ /**
+ * Build a Criteria object which selects all passed objects using their
+ * primary key. Objects which do not yet have a primary key are ignored.
+ *
+ * @param objects the objects to build the criteria from, not null,
+ * may not contain null.
+ */
+ public Criteria buildPkCriteria(
+ Collection<${dbObjectClassName}> objects)
+ {
+ List<ObjectKey> pks = new ArrayList<ObjectKey>(objects.size());
+ for (${dbObjectClassName} object : objects)
+ {
+ ObjectKey pk = object.getPrimaryKey();
+ if (pk != null)
+ {
+ pks.add(pk);
+ }
+ }
+ return buildCriteria(pks);
+ }
+
+#end
/**
* Build a Criteria object from the data object for this peer.
#set ( $primaryKeyColumnElements =
$torqueGen.getChild("primary-keys").getChildren("column") )
-#if (!$idMethod.equals("none") && $primaryKeyColumnElements.size() > 1)
+#if (!$idMethod.equals("none") && $primaryKeyColumnElements.size() >= 1)
* The primary key columns are only added if the object is not new.
#end
*
Modified:
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/doDelete.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/doDelete.vm?rev=1143425&r1=1143424&r2=1143425&view=diff
==============================================================================
---
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/doDelete.vm
(original)
+++
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/doDelete.vm
Wed Jul 6 14:10:18 2011
@@ -93,8 +93,12 @@
*/
public int doDelete($dbObjectClassName obj) throws TorqueException
{
- ## TODO use buildSelectCriteria if no pk is present
+#set ( $primaryKeyColumnElements =
$torqueGen.getChild("primary-keys").getChildren("column"))
+#if ($primaryKeyColumnElements.size() > 0)
return doDelete(buildCriteria(obj.getPrimaryKey()));
+#else
+ return doDelete(buildSelectCriteria(obj));
+#end
}
/**
@@ -113,8 +117,52 @@
public int doDelete($dbObjectClassName obj, Connection con)
throws TorqueException
{
- ## TODO use buildSelectCriteria if no pk is present
+#if ($primaryKeyColumnElements.size() > 0)
return doDelete(buildCriteria(obj.getPrimaryKey()), con);
+#else
+ return doDelete(buildSelectCriteria(obj), con);
+#end
+ }
+
+#if (!$primaryKeyColumnElements.isEmpty())
+ /**
+ * Deletes data objects, i.e. rows in a table, in the database.
+ *
+ * @param objects the data object to delete in the database, not null,
+ * may not contain null.
+ *
+ * @return the number of deleted rows.
+ *
+ * @throws TorqueException Any exceptions caught during processing will be
+ * rethrown wrapped into a TorqueException.
+ */
+ public int doDelete(Collection<${dbObjectClassName}> objects)
+ throws TorqueException
+ {
+ return doDelete(buildPkCriteria(objects));
+ }
+
+ /**
+ * Deletes data objects, i.e. rows in a table, in the database.
+ * This method uses the passed connection to delete the rows;
+ * if a transaction is open in the connection, the deletion happens inside
+ * this transaction.
+ *
+ * @param objects the datas object to delete in the database, not null,
+ * may not contain null.
+ * @param con the connection to use for deleting, 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(
+ Collection<${dbObjectClassName}> objects,
+ Connection con)
+ throws TorqueException
+ {
+ return doDelete(buildPkCriteria(objects), con);
}
/**
@@ -167,3 +215,4 @@
{
return doDelete(buildCriteria(pk), con);
}
+#end
Modified:
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/retrieveByPKs.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/retrieveByPKs.vm?rev=1143425&r1=1143424&r2=1143425&view=diff
==============================================================================
---
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/retrieveByPKs.vm
(original)
+++
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/retrieveByPKs.vm
Wed Jul 6 14:10:18 2011
@@ -61,42 +61,18 @@
* @throws TorqueException Any exceptions caught during processing will be
* rethrown wrapped into a TorqueException.
*/
- public List<${dbObjectClassName}> retrieveByPKs(Collection<ObjectKey> pks,
Connection dbcon)
- throws TorqueException
+ public List<${dbObjectClassName}> retrieveByPKs(
+ Collection<ObjectKey> pks,
+ Connection dbcon)
+ throws TorqueException
{
- List<${dbObjectClassName}> objs = null;
if (pks == null || pks.size() == 0)
{
- objs = new LinkedList<${dbObjectClassName}>();
+ return new ArrayList<${dbObjectClassName}>();
}
- else
- {
- Criteria criteria = new Criteria();
- #if ($primaryKeyColumnElements.size() == 1)
- #set ( $columnElement = $primaryKeyColumnElements.get(0) )
- #set ( $peerColumnName = $columnElement.getAttribute("peerColumnName"))
- criteria.addIn(${peerClassName}.$peerColumnName, pks);
- #else
- for (ObjectKey pk : pks)
- {
- SimpleKey[] keys = (SimpleKey[])pk.getValue();
- #set ( $i = 0 )
- #foreach ($primaryKeyColumnElement in $primaryKeyColumnElements)
- #set ( $peerColumnName =
$primaryKeyColumnElement.getAttribute("peerColumnName"))
- Criteria.Criterion c$i = criteria.getNewCriterion(
- ${peerClassName}.$peerColumnName, keys[$i],
Criteria.EQUAL);
- #set ( $j = $i - 1 )
- #if ($i > 0)
- c${j}.and(c${i});
- #end
- #set ( $i = $i + 1 )
- #end
- criteria.or(c0);
- }
- #end
- objs = doSelect(criteria, dbcon);
- }
- return objs;
+ Criteria criteria = buildCriteria(pks);
+ List<${dbObjectClassName}> result = doSelect(criteria, dbcon);
+ return result;
}
#end
\ No newline at end of file
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]