db-torque-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tfisc...@apache.org
Subject svn commit: r355739 - in /db/torque/runtime/trunk: src/java/org/apache/torque/util/Criteria.java src/java/org/apache/torque/util/SqlExpression.java xdocs/changes.xml xdocs/criteria-howto.xml
Date Sat, 10 Dec 2005 11:26:27 GMT
Author: tfischer
Date: Sat Dec 10 03:26:17 2005
New Revision: 355739

URL: http://svn.apache.org/viewcvs?rev=355739&view=rev
Log:
Added support for subqueries including documentation.
This has caused Criterion.appendTo(StringBuffer) to declare throwing a Torque Exception

Modified:
    db/torque/runtime/trunk/src/java/org/apache/torque/util/Criteria.java
    db/torque/runtime/trunk/src/java/org/apache/torque/util/SqlExpression.java
    db/torque/runtime/trunk/xdocs/changes.xml
    db/torque/runtime/trunk/xdocs/criteria-howto.xml

Modified: db/torque/runtime/trunk/src/java/org/apache/torque/util/Criteria.java
URL: http://svn.apache.org/viewcvs/db/torque/runtime/trunk/src/java/org/apache/torque/util/Criteria.java?rev=355739&r1=355738&r2=355739&view=diff
==============================================================================
--- db/torque/runtime/trunk/src/java/org/apache/torque/util/Criteria.java (original)
+++ db/torque/runtime/trunk/src/java/org/apache/torque/util/Criteria.java Sat Dec 10 03:26:17
2005
@@ -38,6 +38,7 @@
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.torque.Torque;
+import org.apache.torque.TorqueException;
 import org.apache.torque.adapter.DB;
 import org.apache.torque.om.DateKey;
 import org.apache.torque.om.ObjectKey;
@@ -3390,7 +3391,7 @@
         /**
          * Appends a representation of the Criterion onto the buffer.
          */
-        public void appendTo(StringBuffer sb)
+        public void appendTo(StringBuffer sb) throws TorqueException
         {
             //
             // it is alright if value == null
@@ -3567,7 +3568,14 @@
             }
 
             StringBuffer expr = new StringBuffer(25);
-            appendTo(expr);
+            try
+            {
+                appendTo(expr);
+            }
+            catch (TorqueException e)
+            {
+                return("Criterion cannot be evaluated");
+            }
             return expr.toString();
         }
 

Modified: db/torque/runtime/trunk/src/java/org/apache/torque/util/SqlExpression.java
URL: http://svn.apache.org/viewcvs/db/torque/runtime/trunk/src/java/org/apache/torque/util/SqlExpression.java?rev=355739&r1=355738&r2=355739&view=diff
==============================================================================
--- db/torque/runtime/trunk/src/java/org/apache/torque/util/SqlExpression.java (original)
+++ db/torque/runtime/trunk/src/java/org/apache/torque/util/SqlExpression.java Sat Dec 10
03:26:17 2005
@@ -195,6 +195,7 @@
                               boolean ignoreCase,
                               DB db,
                               StringBuffer whereClause)
+            throws TorqueException
     {
         // Allow null criteria
         // This will result in queries like
@@ -254,6 +255,30 @@
             {
                 criteria = db.getBooleanString((Boolean) criteria);
             }
+            else if (criteria instanceof Criteria)
+            {
+                 Query subquery = SQLBuilder.buildQueryClause(
+                        (Criteria) criteria, 
+                        null, 
+                        new SQLBuilder.QueryCallback() {
+                            public String process(
+                                    Criteria.Criterion criterion, 
+                                    List params)
+                            {
+                                return criterion.toString();
+                            }
+                });
+                if (comparison.equals(Criteria.IN)
+                        || comparison.equals(Criteria.NOT_IN))
+                {
+                    // code below takes care of adding brackets
+                    criteria = subquery.toString();
+                }
+                else
+                {
+                    criteria = "(" + subquery.toString() + ")";
+                }
+            }
         }
 
         if (comparison.equals(Criteria.LIKE)
@@ -506,6 +531,11 @@
                 // and/or wraps it in UPPER().
                 inClause.add(processInValue(value, ignoreCase, db));
             }
+        }
+        else if (criteria instanceof String)
+        {
+            // subquery
+            inClause.add(criteria);
         }
         else
         {

Modified: db/torque/runtime/trunk/xdocs/changes.xml
URL: http://svn.apache.org/viewcvs/db/torque/runtime/trunk/xdocs/changes.xml?rev=355739&r1=355738&r2=355739&view=diff
==============================================================================
--- db/torque/runtime/trunk/xdocs/changes.xml (original)
+++ db/torque/runtime/trunk/xdocs/changes.xml Sat Dec 10 03:26:17 2005
@@ -26,6 +26,11 @@
   <body>
 
   <release version="3.2.1-dev" date="in SVN">
+    <action type="add" dev="tfischer" issue="TRQS133">
+      Subqueries are now supported.
+      This has caused Criterion.appendTo(StringBuffer) to declare throwing 
+      a Torque Exception.
+    </action>
     <action type="fix" dev="tfischer" issue="TRQS133">
       BaseObject.equals() does not return true anymore if the objects
       do not have the same class.

Modified: db/torque/runtime/trunk/xdocs/criteria-howto.xml
URL: http://svn.apache.org/viewcvs/db/torque/runtime/trunk/xdocs/criteria-howto.xml?rev=355739&r1=355738&r2=355739&view=diff
==============================================================================
--- db/torque/runtime/trunk/xdocs/criteria-howto.xml (original)
+++ db/torque/runtime/trunk/xdocs/criteria-howto.xml Sat Dec 10 03:26:17 2005
@@ -232,6 +232,35 @@
 
   </section>
 
+  <section name="subselects">
+    <p>
+      To use a subselect, simply put a criteria which represents the 
+      subselect at the place where you would normally specify the column
+      value(s) explicitly.
+    </p>
+    
+    <p>
+      Do not forget to specify the selectColumns in the Criteria representing
+      the subselect (usually, the Peer classes do this for you, but not in the 
+      subselect).
+    </p>
+    
+    <p>
+      As an example, assume we want to read the author with the largest authorId
+      from the database. This can be done as follows:
+    </p>
+    
+<source>
+Criteria subquery = new Criteria();
+subquery.addSelectColumn("MAX(" + AuthorPeer.AUTHOR_ID + ")");
+
+Criteria criteria = new Criteria();
+criteria.add(AuthorPeer.AUTHOR_ID, subquery);
+
+List authors = AuthorPeer.doSelect(criteria);
+</source>
+  </section>
+
   <section name="Using the Comparators in Criteria">
 
    <p>



---------------------------------------------------------------------
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