Ok, so perhaps this is the correct format for my Criteria patch:

Index: Criteria.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-turbine-torque/src/java/org/apache/torque/util/Criteria.java,v
retrieving revision 1.20
diff -u -r1.20 Criteria.java
--- Criteria.java    10 Apr 2002 19:41:54 -0000    1.20
+++ Criteria.java    25 Apr 2002 00:14:21 -0000
@@ -68,6 +68,7 @@
 import java.util.Map;
 import java.util.Set;
 import java.util.Vector;
+import java.lang.Character;
 import org.apache.torque.Torque;
 import org.apache.torque.TorqueException;
 import org.apache.torque.adapter.DB;
@@ -3200,6 +3201,309 @@
         blobFlag = (b ? Boolean.TRUE: Boolean.FALSE);
     }
 
+
+    private Hashtable x_table = new Hashtable();
+
+    /**
+     *
+     *
+     */
+    public Criterion parseCriterionString(String p_criterion_string)
+      throws Exception
+    {
+        // find the first closing bracket
+        int x_first_close = p_criterion_string.indexOf(')');
+        if(x_first_close == -1) throw new Exception("no closing brace");
+        int x_latest = -1;
+        int x_matching_open = 0;
+        // find the opening bracket in front
+       
+        while(x_matching_open < x_first_close && x_matching_open != -1)
+        {
+          x_latest = x_matching_open;   
+          x_matching_open = p_criterion_string.indexOf('(',x_latest+1);
+        }
+        if(x_latest == -1) throw new Exception("no opening brace");
+       
+        String x_pair = 
p_criterion_string.substring(x_latest+1,x_first_close);
+        boolean x_or = false;
+        int x_connector_start = x_pair.indexOf(Criterion.AND);
+        int x_connector_end = -1;
+        if(x_connector_start == -1)
+        {
+          x_or = true;
+          x_connector_start = x_pair.indexOf(Criterion.OR);
+          if(x_connector_start == -1) throw new Exception("no 
connecting element: " + x_pair);
+          x_connector_end = x_connector_start + Criterion.OR.length();
+        }
+        else
+          x_connector_end = x_connector_start + Criterion.AND.length();
+         
+        char x_char = '?';
+        int x_former_start = x_latest;
+        int x_incrementer = x_latest;
+        while(x_char == '?')
+        {
+          x_char = p_criterion_string.charAt(++x_incrementer);
+          x_former_start++;
+        }
+            
+        Criterion x_former = (Criterion)(x_table.get(new 
Integer(x_former_start)));
+        if(x_former == null)
+        {
+          x_former = parseCriterion(x_pair.substring(0,x_connector_start));
+          x_table.put(new Integer(x_former_start),x_former);
+        }
+        Criterion x_latter = (Criterion)(x_table.get(new 
Integer(x_latest+1+x_connector_end+1)));
+        if(x_latter == null)
+        {
+          x_latter = parseCriterion(x_pair.substring(x_connector_end));
+          x_table.put(new Integer(x_latest+1+x_connector_end+1),x_latter);
+        }
+       
+        Vector x_v = null;
+       
+        if(x_or == true)
+        {
+          x_former.or(x_latter);
+        }
+        else
+        {
+          x_former.and(x_latter);
+        }
+         
+        // now take original string and remove latter Criterion
+       
+        char[] x_array = new char[p_criterion_string.length()];
+         
+        p_criterion_string.getChars(0, x_latest, x_array, 0);
+        x_array[x_latest] = '?';
+        p_criterion_string.getChars(x_latest+1, 
x_latest+1+x_connector_start, x_array, x_latest+1);
+        for(int i=x_latest+1+x_connector_start+1;i<x_first_close+1;i++)
+          x_array[i] = '?';
+        p_criterion_string.getChars(x_first_close+1, 
p_criterion_string.length(), x_array, x_first_close+1);
+        String x_new_string = new String(x_array);
+        if(x_new_string.indexOf('(') == -1 && x_new_string.indexOf(')') 
== -1)
+          return x_former;
+        else
+          return parseCriterionString(x_new_string); //?
+    }
+   
+    /**
+     *
+     *
+     */
+    public Criterion parseCriterion(String p_criterion_string)
+      throws Exception
+    {
+        String x_table = null;
+       
+        int x_end_table = p_criterion_string.indexOf('.');
+        if(x_end_table!=-1) //throw new Exception("No point in this 
criterion: " + p_criterion_string);
+          x_table = p_criterion_string.substring(0,x_end_table);
+       
+        SqlEnum x_sql_enum = EQUAL;
+       
+        int x_end_column = p_criterion_string.indexOf(EQUAL.toString());
+        if(x_end_column==-1)
+        {
+          x_end_column = p_criterion_string.indexOf(NOT_EQUAL.toString());
+          if(x_end_column==-1)
+          {
+            x_end_column = 
p_criterion_string.indexOf(ALT_NOT_EQUAL.toString());
+            if(x_end_column==-1)
+            {
+              x_end_column = 
p_criterion_string.indexOf(GREATER_THAN.toString());
+              if(x_end_column==-1)
+              {
+                x_end_column = 
p_criterion_string.indexOf(LESS_THAN.toString());
+                if(x_end_column==-1)
+                {
+                  x_end_column = 
p_criterion_string.indexOf(GREATER_EQUAL.toString());
+                  if(x_end_column==-1)
+                  {
+                    x_end_column = 
p_criterion_string.indexOf(LESS_EQUAL.toString());
+                    if(x_end_column==-1)
+                    {
+                      x_end_column = 
p_criterion_string.indexOf(LIKE.toString());
+                      if(x_end_column==-1)
+                      {
+                        x_end_column = 
p_criterion_string.indexOf(NOT_LIKE.toString());
+                        if(x_end_column==-1)
+                        {
+                          x_end_column = 
p_criterion_string.indexOf(IN.toString());
+                          if(x_end_column==-1)
+                          {
+                            x_end_column = 
p_criterion_string.indexOf(NOT_IN.toString());
+                            if(x_end_column==-1)
+                            {
+                              x_end_column = 
p_criterion_string.indexOf(ISNULL.toString());
+                              if(x_end_column==-1)
+                              {
+                                x_end_column = 
p_criterion_string.indexOf(ISNOTNULL.toString());
+                                if(x_end_column==-1)      
+                                  throw new Exception("No recognised 
comparator in this criterion: " + p_criterion_string.toString());
+                                else
+                                  x_sql_enum = ISNOTNULL;    
+                              }
+                              else
+                              {
+                                x_sql_enum = ISNULL;
+                              }
+                            }
+                            else
+                            {
+                              x_sql_enum = NOT_IN;
+                            }
+                          }
+                          else
+                          {
+                            x_sql_enum = IN;
+                          }
+                        }
+                        else
+                        {
+                          x_sql_enum = NOT_LIKE;
+                        }
+                      }
+                      else
+                      {
+                        x_sql_enum = LIKE;
+                      }
+                    }
+                    else
+                    {
+                      x_sql_enum = LESS_EQUAL; 
+                    }             
+                  }
+                  else
+                  {
+                    x_sql_enum = GREATER_EQUAL;              
+                  }
+                }
+                else
+                {
+                  x_sql_enum = LESS_THAN;
+                }
+              }
+              else
+              {
+                x_sql_enum = GREATER_THAN;
+              }
+            }
+            else
+            {
+              x_sql_enum = ALT_NOT_EQUAL;
+            }
+          }
+          else
+          {
+            x_sql_enum = NOT_EQUAL;
+          }
+        }
+        else
+        {
+          x_sql_enum = EQUAL;
+        }
+       
+        if(x_end_table == -1) x_end_table++;
+        String x_column = 
p_criterion_string.substring(x_end_table+1,x_end_column);
+        String x_value = 
p_criterion_string.substring(x_end_column+x_sql_enum.toString().length());
+        x_value = x_value.replace('\'',' ');
+        x_value = x_value.trim();
+             
+        Criterion x_criterion = null;
+        if(x_table != null)
+          x_criterion = new 
Criterion(x_table,x_column,(Object)(x_value),x_sql_enum);
+        else
+          x_criterion = new 
Criterion(x_column,(Object)(x_value),x_sql_enum);
+         
+        return x_criterion;
+    }
+
+
     /**
      * This is an inner class that describes an object in the
      * criteria.
@@ -3217,6 +3521,7 @@
 
         /** Table name. */
         private String table;
+       
 
         /** Column name. */
         private String column;
@@ -3233,14 +3538,20 @@
         /**
          * Another Criterion connected to this one by an OR clause.
          */
-        private Criterion or;
+        //private Criterion or;
 
         /**
          * Another criterion connected to this one by an AND clause.
          */
-        private Criterion and;
+        //private Criterion and;
 
         /**
+         * other connected criteria and their types.
+         */
+        private Vector clauses = new Vector();
+        private Vector types = new Vector();
+       
+        /**
          * Creates a new instance, initializing a couple members.
          */
         private Criterion( Object val, SqlEnum comp )
@@ -3412,13 +3723,10 @@
         public void setDB(DB  v)
         {
             this.db = v;
-            if ( and != null )
-            {
-                and.setDB(v);
-            }
-            if ( or != null )
+           
+            for(int i=0;i<this.clauses.size();i++)
             {
-                or.setDB(v);
+              ((Criterion)(clauses.elementAt(i))).setDB(v);
             }
         }
 
@@ -3447,48 +3755,45 @@
         /**
          *  get the criterion from this Criterion's AND field.
          */
-        public Criterion getAnd()
+        public Vector getClauses()
         {
-            return and;
+            return clauses;
         }
 
         /**
-         * Append a Criteria onto this Criteria's AND field.
+         *  get the criterion from this Criterion's AND field.
+         */
+        public Vector getTypes()
+        {
+            return types;
+        }
+
+        /**
+         * Append an AND Criterion onto this Criterion's list.
          */
         public Criterion and(Criterion criterion)
         {
-            if (this.and == null)
-            {
-                this.and = criterion;
-            }
-            else
-            {
-                this.and.and(criterion);
-            }
+            this.clauses.addElement(criterion);
+            this.types.addElement(AND);
             return this;
         }
 
         /**
          *  get the criterion from this Criterion's AND field.
          */
+         /*
         public Criterion getOr()
         {
             return or;
-        }
+        }*/
 
         /**
-         * Append a Criterion onto this Criterion's OR field.
+         * Append an OR Criterion onto this Criterion's list.
          */
         public Criterion or(Criterion criterion)
         {
-            if (this.or == null)
-            {
-                this.or = criterion;
-            }
-            else
-            {
-                this.or.or(criterion);
-            }
+            this.clauses.addElement(criterion);
+            this.types.addElement(OR);
             return this;
         }
 
@@ -3500,13 +3805,15 @@
             //
             // it is alright if value == null
             //
-
+           
             if (column == null)
             {
                 return;
             }
 
-            sb.append('(');
+            Criterion x_clause = null;
+            for(int j=0;j<this.clauses.size();j++)
+              sb.append('(');
             if ( CUSTOM == comparison )
             {
                 if ( value != null && ! "".equals(value) )
@@ -3532,18 +3839,13 @@
                                     ignoreStringCase, getDb(), sb);
             }
 
-            if (or != null)
-            {
-                sb.append(OR);
-                or.appendTo(sb);
-            }
-
-            if (and != null)
+            for(int i=0;i<this.clauses.size();i++)
             {
-                sb.append(AND);
-                and.appendTo(sb);
+                sb.append(this.types.elementAt(i));
+                x_clause = (Criterion)(this.clauses.elementAt(i));
+                x_clause.appendTo(sb);
+                sb.append(')');
             }
-            sb.append(')');
         }
 
         /**
@@ -3562,8 +3864,10 @@
             }
 
             DB db = getDb();
-
-            sb.append('(');
+           
+            Criterion x_clause = null;
+            for(int j=0;j<this.clauses.size();j++)
+              sb.append('(');
             if ( CUSTOM == comparison )
             {
                 if ( !"".equals(value) )
@@ -3640,18 +3944,15 @@
                 }
             }
 
-            if (or != null)
+            for(int i=0;i<this.clauses.size();i++)
             {
-                sb.append(OR);
-                or.appendPsTo(sb,params);
+                sb.append(this.types.elementAt(i));
+                x_clause = (Criterion)(this.clauses.elementAt(i));
+                x_clause.appendPsTo(sb,params);
+                sb.append(')');
             }
+           
 
-            if (and != null)
-            {
-                sb.append(AND);
-                and.appendPsTo(sb,params);
-            }
-            sb.append(')');
         }
 
         /**
@@ -3717,12 +4018,14 @@
             }
 
             // check chained criterion
-            isEquiv &= (and == null && crit.getAnd() == null )
-                || (and != null && and.equals(crit.getAnd()));
-
-            isEquiv &= (or == null && crit.getOr() == null )
-                || (or != null && or.equals(crit.getOr()));
-
+           
+            isEquiv &= this.clauses.size() == crit.getClauses().size();
+            for(int i=0;i<this.clauses.size();i++)
+            {
+              isEquiv &=  
((String)(types.elementAt(i))).equals((String)(crit.getTypes().elementAt(i))); 

+              isEquiv &=  
((Criterion)(clauses.elementAt(i))).equals((Criterion)(crit.getClauses().elementAt(i)));
 

+            }
+               
             return isEquiv;
         }
 
@@ -3743,14 +4046,9 @@
                 h ^= column.hashCode();
             }
 
-            if (and != null)
+            for(int i=0;i<this.clauses.size();i++)
             {
-                h ^= and.hashCode();
-            }
-
-            if (or != null)
-            {
-                h ^= or.hashCode();
+                h ^= ((Criterion)(clauses.elementAt(i))).hashCode();
             }
 
             return h;
@@ -3768,8 +4066,8 @@
             if ( c != null )
             {
                 s.add(c.getTable());
-                addCriterionTable(c.getAnd(), s);
-                addCriterionTable(c.getOr(), s);
+                for(int i=0;i<c.getClauses().size();i++)
+                  
addCriterionTable((Criterion)(c.getClauses().elementAt(i)), s);
             }
         }
 
@@ -3791,9 +4089,10 @@
             if ( c != null )
             {
                 a.add(c);
-                traverseCriterion(c.getAnd(), a);
-                traverseCriterion(c.getOr(), a);
+                for(int i=0;i<c.getClauses().size();i++)
+                  
traverseCriterion((Criterion)(c.getClauses().elementAt(i)), a);
             }
         }
     }
 }
+



--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to