Hi,

heres just a patch that moves DB management from Criterion to BasePeer. 
Previously you went

criterion.setDB( .. )
criterion.someMethodToGenerateSQL()

Instead you now just do

criterion.someMethodToGenerateSQL(db)

which seems easier.

-- 
Cheers,

Pete

--------------------------------------------
 Beer is proof that God loves us and wants 
 us to be happy. -- Benjamin Franklin
--------------------------------------------
Index: Criteria.java
===================================================================
RCS file: /home/cvspublic/jakarta-turbine-torque/src/java/org/apache/torque/util/Criteria.java,v
retrieving revision 1.6
diff -u -r1.6 Criteria.java
--- Criteria.java	2001/10/18 19:28:58	1.6
+++ Criteria.java	2001/10/22 00:41:52
@@ -1025,7 +1025,8 @@
                           Object value,
                           SqlEnum comparison )
     {
-        super.put( column, new Criterion(column, value, comparison) );
+        final Criterion criterion = new Criterion(column, value, comparison);
+        super.put( column, criterion );
         return this;
     }
 
@@ -3169,12 +3170,6 @@
         private boolean ignoreStringCase = false;
 
         /**
-         * The DB adaptor which might be used to get db specific
-         * variations of sql.
-         */
-        private DB db;
-
-        /**
          * Another Criterion connected to this one by an OR clause.
          */
         private Criterion or;
@@ -3315,58 +3310,6 @@
         }
 
         /**
-         * Get the value of db.
-         * The DB adaptor which might be used to get db specific
-         * variations of sql.
-         * @return value of db.
-         */
-        public DB getDb()
-        {
-            DB db = null;
-            if ( this.db == null ) 
-            {
-                // db may not be set if generating preliminary sql for 
-                // debugging.
-                try
-                {
-                    db = Torque.getDB( getDbName() );
-                }
-                catch (Exception e)
-                {
-                    // we are only doing this to allow easier debugging, so
-                    // no need to throw up the exception, just make note of it.
-                    Torque.getCategory().error(
-                       "Could not get a DB adapter, so sql may be wrong");
-                }
-            }
-            else 
-            {
-                db = this.db;
-            }
-            
-            return db;
-        }
-
-        /**
-         * Set the value of db.
-         * The DB adaptor might be used to get db specific
-         * variations of sql.
-         * @param v  Value to assign to db.
-         */
-        public void setDB(DB  v)
-        {
-            this.db = v;
-            if ( and != null )
-            {
-                and.setDB(v);
-            }
-            if ( or != null )
-            {
-                or.setDB(v);
-            }
-        }
-
-        /**
          * Sets ignore case.
          *
          * @param b True if case should be ignored.
@@ -3438,9 +3381,19 @@
 
         /**
          * Appends a representation of the Criterion onto the buffer.
+         * This does not take into account perculiarities of the actual
+         * DB. Use append(StringBuffer,Db) for that functionality.
          */
         public void appendTo(StringBuffer sb)
         {
+            appendTo( sb, null );
+        }
+
+        /**
+         * Appends a representation of the Criterion onto the buffer.
+         */
+        public void appendTo(StringBuffer sb, DB db)
+        {
             //
             // it is alright if value == null
             //
@@ -3473,7 +3426,7 @@
                         .toString();
                 }
                 SqlExpression.build(field, value, comparison,
-                                    ignoreStringCase, getDb(), sb);
+                                    ignoreStringCase, db, sb);
             }
 
             if (or != null)
@@ -3498,15 +3451,13 @@
          * @param params A list to which Prepared Statement parameters
          * will be appended
          */
-        public void appendPsTo(StringBuffer sb, List params)
+        protected void appendPsTo(StringBuffer sb, List params, DB db)
         {
             if (column == null || value == null)
             {
                 return;
             }
 
-            DB db = getDb();
-
             sb.append('(');
             if ( CUSTOM == comparison )
             {
@@ -3587,23 +3538,24 @@
             if (or != null)
             {
                 sb.append(OR);
-                or.appendPsTo(sb,params);
+                or.appendPsTo(sb,params,db);
             }
 
             if (and != null)
             {
                 sb.append(AND);
-                and.appendPsTo(sb,params);
+                and.appendPsTo(sb,params,db);
             }
             sb.append(')');
         }
 
         /**
-         * Build a string representation of the Criterion.
+         * Build a string representation of the Criterion, taking into
+         * account the peculiarities of the database.
          *
          * @return A String with the representation of the Criterion.
          */
-        public String toString()
+        public String toString(DB db)
         {
             //
             // it is alright if value == null
@@ -3614,8 +3566,20 @@
             }
 
             StringBuffer expr = new StringBuffer(25);
-            appendTo(expr);
+            appendTo(expr,db);
             return expr.toString();
+        }
+
+        /**
+         * Build a string representation of the Criterion.
+         *
+         * @return A String with the representation of the Criterion.
+         * @deprecated This method does not construct the SQL for a specicifc
+         *    database. It is recomended that toString(DB) be used instead.
+         */
+        public String toString()
+        {
+            return toString( null );
         }
 
         /**
Index: BasePeer.java
===================================================================
RCS file: /home/cvspublic/jakarta-turbine-torque/src/java/org/apache/torque/util/BasePeer.java,v
retrieving revision 1.15
diff -u -r1.15 BasePeer.java
--- BasePeer.java	2001/10/16 03:58:07	1.15
+++ BasePeer.java	2001/10/22 00:41:53
@@ -1041,8 +1041,7 @@
                 someCriteria[i].setIgnoreCase(ignorCase);
             }
 
-            criterion.setDB(db);
-            whereClause.add( criterion.toString() );
+            whereClause.add( criterion.toString( db ) );
 
         }
 
@@ -2159,9 +2158,8 @@
                 someCriteria[i].setIgnoreCase(ignorCase);
             }
 
-            criterion.setDB(db);
             StringBuffer sb = new StringBuffer();
-            criterion.appendPsTo (sb,params);
+            criterion.appendPsTo (sb,params,db);
             whereClause.add( sb.toString() );
 
         }
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to