db-torque-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tfisc...@apache.org
Subject svn commit: r280407 - in /db/torque/templates/trunk/src/templates/om: BaseManager.vm ObjectWithManager.vm Peer.vm
Date Mon, 12 Sep 2005 20:12:25 GMT
Author: tfischer
Date: Mon Sep 12 13:12:19 2005
New Revision: 280407

URL: http://svn.apache.org/viewcvs?rev=280407&view=rev
Log:
- Added the methods getCachedInstance() and getCachedInstanceImpl() to the BaseManager.vm
template
- Added the method getXXX(Connection) for associated objects to the ObjectWithManager.vm template
- Streamlined code in Peer.vm
Thanks to Thomas Vandahl for the patch.

Modified:
    db/torque/templates/trunk/src/templates/om/BaseManager.vm
    db/torque/templates/trunk/src/templates/om/ObjectWithManager.vm
    db/torque/templates/trunk/src/templates/om/Peer.vm

Modified: db/torque/templates/trunk/src/templates/om/BaseManager.vm
URL: http://svn.apache.org/viewcvs/db/torque/templates/trunk/src/templates/om/BaseManager.vm?rev=280407&r1=280406&r2=280407&view=diff
==============================================================================
--- db/torque/templates/trunk/src/templates/om/BaseManager.vm (original)
+++ db/torque/templates/trunk/src/templates/om/BaseManager.vm Mon Sep 12 13:12:19 2005
@@ -13,6 +13,7 @@
 ## limitations under the License.
 #*
  * author <a href="mailto:jmcnally@collab.net">John McNally</a>
+ * author <a href="mailto:thomas@vandahl.org">Thomas Vandahl</a>
  * version $Id$
  *#
 #set ($interfaceName = $table.JavaName)
@@ -63,10 +64,10 @@
     extends AbstractBaseManager
 {
     /** The name of the manager */
-    protected static String MANAGED_CLASS = "${package}.${interfaceName}";
+    protected static final String MANAGED_CLASS = "${package}.${interfaceName}";
 
     /** The name of our class to pass to Torque as the default manager. */
-    protected static String DEFAULT_MANAGER_CLASS
+    protected static final String DEFAULT_MANAGER_CLASS
         = "${package}.${interfaceName}Manager";
 
     /**
@@ -108,6 +109,21 @@
     }
 
     /**
+     * Static accessor for the @see #getCachedInstanceImpl(ObjectKey).
+     * Loads <code>${interfaceName}</code> from cache, returns 
+     * <code>null</code>, if instance is not in cache
+     *
+     * @param id an <code>ObjectKey</code> value
+     * @return a <code>${interfaceName}</code> value
+     * @exception TorqueException if an error occurs
+     */
+    public static ${interfaceName} getCachedInstance(ObjectKey id)
+        throws TorqueException
+    {
+        return getManager().getCachedInstanceImpl(id);
+    }
+
+    /**
      * Static accessor for the @see #getInstanceImpl(ObjectKey, boolean).
      *
      * @param id an <code>ObjectKey</code> value
@@ -239,7 +255,6 @@
         return obj;
     }
 
-
     /**
      * Get a ${interfaceName} with the given id.
      *
@@ -249,6 +264,18 @@
         throws TorqueException
     {
         return (${interfaceName}) getOMInstance(id);
+    }
+
+    /**
+     * Get a ${interfaceName} with the given id from the cache. Returns 
+     * <code>null</code> if instance is not in cache
+     *
+     * @param id <code>ObjectKey</code> value
+     */
+    protected ${interfaceName} getCachedInstanceImpl(ObjectKey id)
+        throws TorqueException
+    {
+        return (${interfaceName}) cacheGet(id);
     }
 
     /**

Modified: db/torque/templates/trunk/src/templates/om/ObjectWithManager.vm
URL: http://svn.apache.org/viewcvs/db/torque/templates/trunk/src/templates/om/ObjectWithManager.vm?rev=280407&r1=280406&r2=280407&view=diff
==============================================================================
--- db/torque/templates/trunk/src/templates/om/ObjectWithManager.vm (original)
+++ db/torque/templates/trunk/src/templates/om/ObjectWithManager.vm Mon Sep 12 13:12:19 2005
@@ -364,15 +364,60 @@
         {
         #if ($argsize > 1)
             SimpleKey[] keys = { $arglist };
-            return ${interfaceName}Manager.getInstance(new ComboKey(keys));
+            $varName = ${interfaceName}Manager.getInstance(new ComboKey(keys));
         #else
-            return ${interfaceName}Manager.getInstance($arglist);
+            $varName = ${interfaceName}Manager.getInstance($arglist);
         #end
         }
       #end
         return $varName;
     }
 
+    /**
+     * Return the associated $interfaceName object
+     * If it was not retrieved before, the object is retrieved from
+     * the database using the passed connection
+     *
+     * @param connection the connection used to retrieve the associated object
+     *        from the database, if it was not retrieved before
+     * @return the associated $interfaceName object
+     * @throws TorqueException
+     */
+    public $interfaceName get${pVarName}(Connection connection)
+        throws TorqueException
+    {
+        if ($varName == null && ($conditional))
+        {
+        #if ($argsize > 1)
+            SimpleKey[] keys = { $arglist };
+            ComboKey comboid = new ComboKey(keys);
+
+            $varName = ${interfaceName}Manager.getCachedInstance(comboid);
+            if ($varName == null)
+            {
+          #if ($tblFK.isAlias())
+                $varName = ${className}Peer.retrieve${className}ByPK(comboid, connection);
+          #else
+                $varName = ${className}Peer.retrieveByPK(comboid, connection);
+          #end
+                ${interfaceName}Manager.putInstance($varName);
+            }
+        #else
+            $varName = ${interfaceName}Manager.getCachedInstance($arglist);
+            if ($varName == null)
+            {
+          #if ($tblFK.isAlias())
+                $varName = ${className}Peer.retrieve${className}ByPK($arglist, connection);
+          #else
+                $varName = ${className}Peer.retrieveByPK($arglist, connection);
+          #end
+                ${interfaceName}Manager.putInstance($varName);
+            }
+        #end
+        }
+        return $varName;
+    }
+    
     /**
      * Provides convenient way to set a relationship based on a
      * ObjectKey, for example

Modified: db/torque/templates/trunk/src/templates/om/Peer.vm
URL: http://svn.apache.org/viewcvs/db/torque/templates/trunk/src/templates/om/Peer.vm?rev=280407&r1=280406&r2=280407&view=diff
==============================================================================
--- db/torque/templates/trunk/src/templates/om/Peer.vm (original)
+++ db/torque/templates/trunk/src/templates/om/Peer.vm Mon Sep 12 13:12:19 2005
@@ -594,7 +594,7 @@
         throws TorqueException
     {
         Criteria selectCriteria = new Criteria(DATABASE_NAME, 2);
-		correctBooleans(criteria);
+        correctBooleans(criteria);
 
   #foreach ($col in $table.Columns)
     #set ( $cup=$col.Name.toUpperCase() )
@@ -832,10 +832,14 @@
     #set ( $cjtype = $col.JavaNative )
     #if ($col.isPrimaryKey() && !$table.IdMethod.equals("none"))
         if (!obj.isNew())
+        {
     #end
     #if ( $cjtype != "byte[]" )
             criteria.add($cup, obj.${col.GetterName}());
     #end
+    #if ($col.isPrimaryKey() && !$table.IdMethod.equals("none"))
+        }
+    #end
   #end
         return criteria;
     }
@@ -906,7 +910,7 @@
         {
             Torque.closeConnection(db);
         }
-        return(retVal);
+        return retVal;
     }
 
     /**
@@ -959,7 +963,7 @@
         {
             Torque.closeConnection(db);
         }
-        return(retVal);
+        return retVal;
     }
 
     /**
@@ -1048,7 +1052,7 @@
         {
             Torque.closeConnection(db);
         }
-        return(retVal);
+        return retVal;
     }
 
   #set ( $comma = false )
@@ -1079,13 +1083,13 @@
         criteria.add($cup, $clo);
   #end
         List v = doSelect(criteria, con);
-        if (v.size() != 1)
+        if (v.size() == 1)
         {
-            throw new TorqueException("Failed to select one and only one row.");
+            return ($table.JavaName) v.get(0);
         }
         else
         {
-            return ($table.JavaName) v.get(0);
+            throw new TorqueException("Failed to select one and only one row.");
         }
     }
 #end
@@ -1151,8 +1155,8 @@
     protected static List doSelectJoin${joinColumnId}(Criteria criteria)
         throws TorqueException
     {
-		return doSelectJoin${joinColumnId}(criteria, null);
-	}
+        return doSelectJoin${joinColumnId}(criteria, null);
+    }
 
     /**
      * selects a collection of $className objects pre-filled with their
@@ -1185,11 +1189,15 @@
 
         correctBooleans(criteria);
 
-		List rows;
-		if( conn == null )
-	        rows = BasePeer.doSelect(criteria);
-	    else
-	    	rows = BasePeer.doSelect(criteria,conn);
+        List rows;
+        if (conn == null)
+        {
+            rows = BasePeer.doSelect(criteria);
+        }
+        else
+        {
+            rows = BasePeer.doSelect(criteria,conn);
+        }
 
         List results = new ArrayList();
 
@@ -1295,7 +1303,7 @@
     protected static List doSelectJoinAllExcept${excludeString}(Criteria criteria)
         throws TorqueException
     {
-    	return doSelectJoinAllExcept${excludeString}(criteria, null);
+        return doSelectJoinAllExcept${excludeString}(criteria, null);
     }
 
     /**
@@ -1335,10 +1343,14 @@
         correctBooleans(criteria);
 
         List rows;
-        if( conn == null )
-	        rows = BasePeer.doSelect(criteria);
-	    else
-	    	rows = BasePeer.doSelect(criteria,conn);
+        if (conn == null)
+        {
+            rows = BasePeer.doSelect(criteria);
+        }
+        else
+        {
+            rows = BasePeer.doSelect(criteria,conn);
+        }
 
         List results = new ArrayList();
 



---------------------------------------------------------------------
To unsubscribe, e-mail: torque-dev-unsubscribe@db.apache.org
For additional commands, e-mail: torque-dev-help@db.apache.org


Mime
View raw message