You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@turbine.apache.org by jm...@apache.org on 2001/10/18 21:28:58 UTC

cvs commit: jakarta-turbine-torque/src/java/org/apache/torque/util SqlEnum.java Criteria.java SqlExpression.java

jmcnally    01/10/18 12:28:58

  Modified:    src/java/org/apache/torque/util Criteria.java
                        SqlExpression.java
  Added:       src/java/org/apache/torque/util SqlEnum.java
  Log:
  added a typesafe enum to be used unstead of Strings for the sql fragments
  defined in Criteria.  The use of String and the great number of overloaded methods in Criteria lead to inconvenient casting of method arguments.
  
  The change should be transparent to application code.
  
  Revision  Changes    Path
  1.6       +56 -59    jakarta-turbine-torque/src/java/org/apache/torque/util/Criteria.java
  
  Index: Criteria.java
  ===================================================================
  RCS file: /home/cvs/jakarta-turbine-torque/src/java/org/apache/torque/util/Criteria.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- Criteria.java	2001/10/01 20:05:59	1.5
  +++ Criteria.java	2001/10/18 19:28:58	1.6
  @@ -90,103 +90,100 @@
    * @author <a href="mailto:bmclaugh@algx.net">Brett McLaughlin</a>
    * @author <a href="mailto:eric@dobbse.net">Eric Dobbs</a>
    * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
  - * @version $Id: Criteria.java,v 1.5 2001/10/01 20:05:59 jmcnally Exp $
  + * @version $Id: Criteria.java,v 1.6 2001/10/18 19:28:58 jmcnally Exp $
    */
   public class Criteria extends Hashtable
   {
  +
       /**
        * Comparison type.
        */
  -    public static final String EQUAL = "=";
  +    public static final SqlEnum EQUAL = SqlEnum.EQUAL;
   
       /**
        * Comparison type.
        */
  -    public static final String NOT_EQUAL = "<>";
  +    public static final SqlEnum NOT_EQUAL = SqlEnum.NOT_EQUAL;
   
       /**
        * Comparison type.
        */
  -    public static final String ALT_NOT_EQUAL = "!=";
  +    public static final SqlEnum ALT_NOT_EQUAL = SqlEnum.ALT_NOT_EQUAL;
   
       /**
        * Comparison type.
        */
  -    public static final String GREATER_THAN = ">";
  +    public static final SqlEnum GREATER_THAN = SqlEnum.GREATER_THAN;
   
       /**
        * Comparison type.
        */
  -    public static final String LESS_THAN = "<";
  +    public static final SqlEnum LESS_THAN = SqlEnum.LESS_THAN;
   
       /**
        * Comparison type.
        */
  -    public static final String GREATER_EQUAL = ">=";
  +    public static final SqlEnum GREATER_EQUAL = SqlEnum.GREATER_EQUAL;
   
       /**
        * Comparison type.
        */
  -    public static final String LESS_EQUAL = "<=";
  +    public static final SqlEnum LESS_EQUAL = SqlEnum.LESS_EQUAL;
   
       /**
        * Comparison type.
        */
  -    public static final String LIKE = " LIKE ";
  -
  -    // public static final String STRING_EQUAL = "='";
  +    public static final SqlEnum LIKE = SqlEnum.LIKE;
   
       /**
        * Comparison type.
        */
  -    public static final String CUSTOM = "CUSTOM";
  -
  -    // public static final String FOREIGN_KEY = "fk";
  +    public static final SqlEnum CUSTOM = SqlEnum.CUSTOM;
   
       /**
        * Comparison type.
        */
  -    public static final String DISTINCT = "DISTINCT ";
  +    public static final SqlEnum DISTINCT = SqlEnum.DISTINCT;
   
       /**
        * Comparison type.
        */
  -    public static final String IN = " IN ";
  +    public static final SqlEnum IN = SqlEnum.IN;
   
       /**
        * Comparison type.
        */
  -    public static final String NOT_IN = " NOT IN ";
  +    public static final SqlEnum NOT_IN = SqlEnum.NOT_IN;
   
       /**
        * Comparison type.
        */
  -    public static final String ALL = "ALL ";
  +    public static final SqlEnum ALL = SqlEnum.ALL;
   
       /**
        * Comparison type.
        */
  -    public static final String JOIN = "JOIN";
  +    public static final SqlEnum JOIN = SqlEnum.JOIN;
   
       /**
        * "Order by" qualifier - ascending
        */
  -    private static final String ASC = "ASC";
  +    private static final SqlEnum ASC = SqlEnum.ASC;
   
       /**
        * "Order by" qualifier - descending
        */
  -    private static final String DESC = "DESC";
  +    private static final SqlEnum DESC = SqlEnum.DESC;
   
       /**
        * "IS NULL" null comparison
        */
  -    public static final String ISNULL = " IS NULL ";
  +    public static final SqlEnum ISNULL = SqlEnum.ISNULL;
   
       /**
        * "IS NOT NULL" null comparison
        */
  -    public static final String ISNOTNULL = " IS  NOT NULL ";
  +    public static final SqlEnum ISNOTNULL = SqlEnum.ISNOTNULL;
   
       private static final int DEFAULT_CAPACITY = 10;
   
  @@ -450,7 +447,7 @@
        * @return A Criterion.
        */
       public Criterion getNewCriterion(String column,
  -                                     Object value, String comparison)
  +                                     Object value, SqlEnum comparison)
       {
           return new Criterion(column,value,comparison);
       }
  @@ -465,7 +462,7 @@
        * @return A Criterion.
        */
       public Criterion getNewCriterion(String table, String column,
  -                                     Object value, String comparison)
  +                                     Object value, SqlEnum comparison)
       {
           return new Criterion(table,column,value,comparison);
       }
  @@ -516,7 +513,7 @@
        * @param key String name of the key.
        * @return A String with the value of the object at key.
        */
  -    public String getComparison(String key)
  +    public SqlEnum getComparison(String key)
       {
           return getCriterion(key).getComparison();
       }
  @@ -528,7 +525,7 @@
        * @param column String name of column.
        * @return A String with the value of the object at key.
        */
  -    public String getComparison(String table,
  +    public SqlEnum getComparison(String table,
                                   String column)
       {
           return getComparison(
  @@ -1026,7 +1023,7 @@
        */
       public Criteria add ( String column,
                             Object value,
  -                          String comparison )
  +                          SqlEnum comparison )
       {
           super.put( column, new Criterion(column, value, comparison) );
           return this;
  @@ -1086,7 +1083,7 @@
       public Criteria add( String table,
                             String column,
                             Object value,
  -                          String comparison )
  +                          SqlEnum comparison )
       {
           StringBuffer sb = new StringBuffer(table.length()+column.length()+1);
           sb.append(table);
  @@ -1134,7 +1131,7 @@
        */
       public Criteria add( String column,
                             boolean value,
  -                          String comparison )
  +                          SqlEnum comparison )
       {
           add(column, new Boolean(value), comparison );
           return this;
  @@ -1180,7 +1177,7 @@
        */
       public Criteria add( String column,
                             int value,
  -                          String comparison )
  +                          SqlEnum comparison )
       {
           add(column, new Integer(value), comparison );
           return this;
  @@ -1226,7 +1223,7 @@
        */
       public Criteria add( String column,
                           long value,
  -                        String comparison)
  +                        SqlEnum comparison)
       {
           add(column, new Long(value), comparison);
           return this;
  @@ -1272,7 +1269,7 @@
        */
       public Criteria add( String column,
                           float value,
  -                        String comparison)
  +                        SqlEnum comparison)
       {
           add(column, new Float(value), comparison);
           return this;
  @@ -1317,7 +1314,7 @@
        */
       public Criteria add( String column,
                           double value,
  -                        String comparison)
  +                        SqlEnum comparison)
       {
           add(column, new Double(value), comparison);
           return this;
  @@ -1344,7 +1341,7 @@
                                int year,
                                int month,
                                int date,
  -                             String comparison)
  +                             SqlEnum comparison)
       {
           add(column, new GregorianCalendar(year, month, date), comparison );
           return this;
  @@ -1396,7 +1393,7 @@
                                int year,
                                int month,
                                int date,
  -                             String comparison)
  +                             SqlEnum comparison)
       {
           add(column, new GregorianCalendar(year, month, date), comparison );
           return this;
  @@ -1617,7 +1614,7 @@
        */
       public void setAll()
       {
  -        selectModifiers.add(ALL);
  +        selectModifiers.add(ALL.toString());
       }
   
       /**
  @@ -1625,7 +1622,7 @@
        */
       public void setDistinct()
       {
  -        selectModifiers.add(DISTINCT);
  +        selectModifiers.add(DISTINCT.toString());
       }
   
       /**
  @@ -2004,7 +2001,7 @@
        */
       public Criteria and( String column,
                            Object value,
  -                         String comparison )
  +                         SqlEnum comparison )
       {
           Criterion oc = getCriterion(column);
           Criterion nc = new Criterion(column, value, comparison);
  @@ -2074,7 +2071,7 @@
       public Criteria and( String table,
                            String column,
                            Object value,
  -                         String comparison )
  +                         SqlEnum comparison )
       {
           StringBuffer sb = new StringBuffer(table.length()+column.length()+1);
           sb.append(table);
  @@ -2133,7 +2130,7 @@
        */
       public Criteria and( String column,
                            boolean value,
  -                         String comparison )
  +                         SqlEnum comparison )
       {
           and(column, new Boolean(value), comparison );
           return this;
  @@ -2179,7 +2176,7 @@
        */
       public Criteria and( String column,
                            int value,
  -                         String comparison )
  +                         SqlEnum comparison )
       {
           and(column, new Integer(value), comparison );
           return this;
  @@ -2225,7 +2222,7 @@
        */
       public Criteria and( String column,
                            long value,
  -                         String comparison)
  +                         SqlEnum comparison)
       {
           and(column, new Long(value), comparison);
           return this;
  @@ -2271,7 +2268,7 @@
        */
       public Criteria and( String column,
                            float value,
  -                         String comparison)
  +                         SqlEnum comparison)
       {
           and(column, new Float(value), comparison);
           return this;
  @@ -2316,7 +2313,7 @@
        */
       public Criteria and( String column,
                            double value,
  -                         String comparison)
  +                         SqlEnum comparison)
       {
           and(column, new Double(value), comparison);
           return this;
  @@ -2368,7 +2365,7 @@
                                int year,
                                int month,
                                int date,
  -                             String comparison)
  +                             SqlEnum comparison)
       {
           and(column, new GregorianCalendar(year, month, date), comparison );
           return this;
  @@ -2618,7 +2615,7 @@
        */
       public Criteria or( String column,
                           Object value,
  -                        String comparison )
  +                        SqlEnum comparison )
       {
           Criterion oc = getCriterion(column);
           Criterion nc = new Criterion(column, value, comparison);
  @@ -2688,7 +2685,7 @@
       public Criteria or( String table,
                           String column,
                           Object value,
  -                        String comparison )
  +                        SqlEnum comparison )
       {
           StringBuffer sb = new StringBuffer(table.length()+column.length()+1);
           sb.append(table);
  @@ -2746,7 +2743,7 @@
        */
       public Criteria or( String column,
                           boolean value,
  -                        String comparison )
  +                        SqlEnum comparison )
       {
           or(column, new Boolean(value), comparison );
           return this;
  @@ -2792,7 +2789,7 @@
        */
       public Criteria or( String column,
                           int value,
  -                        String comparison )
  +                        SqlEnum comparison )
       {
           or(column, new Integer(value), comparison );
           return this;
  @@ -2838,7 +2835,7 @@
        */
       public Criteria or( String column,
                           long value,
  -                        String comparison)
  +                        SqlEnum comparison)
       {
           or(column, new Long(value), comparison);
           return this;
  @@ -2884,7 +2881,7 @@
        */
       public Criteria or( String column,
                           float value,
  -                        String comparison)
  +                        SqlEnum comparison)
       {
           or(column, new Float(value), comparison);
           return this;
  @@ -2929,7 +2926,7 @@
        */
       public Criteria or( String column,
                           double value,
  -                        String comparison)
  +                        SqlEnum comparison)
       {
           or(column, new Double(value), comparison);
           return this;
  @@ -2981,7 +2978,7 @@
                               int year,
                               int month,
                               int date,
  -                            String comparison)
  +                            SqlEnum comparison)
       {
           or(column, new GregorianCalendar(year, month, date), comparison );
           return this;
  @@ -3160,7 +3157,7 @@
           private Object value;
   
           /** Comparison value. */
  -        private String comparison;
  +        private SqlEnum comparison;
   
           /** Table name. */
           private String table;
  @@ -3190,7 +3187,7 @@
           /**
            * Creates a new instance, initializing a couple members.
            */
  -        private Criterion( Object val, String comp )
  +        private Criterion( Object val, SqlEnum comp )
           {
               this.value = val;
               this.comparison = comp;
  @@ -3207,7 +3204,7 @@
           Criterion( String table,
                           String column,
                           Object val,
  -                        String comp )
  +                        SqlEnum comp )
           {
               this(val, comp);
               this.table = (table == null ? "" : table);
  @@ -3224,7 +3221,7 @@
            */
           Criterion( String tableColumn,
                           Object val,
  -                        String comp )
  +                        SqlEnum comp )
           {
               this(val, comp);
               int dot = tableColumn.indexOf('.');
  @@ -3302,7 +3299,7 @@
            *
            * @return A String with the comparison.
            */
  -        public String getComparison()
  +        public SqlEnum getComparison()
           {
               return this.comparison;
           }
  
  
  
  1.8       +6 -6      jakarta-turbine-torque/src/java/org/apache/torque/util/SqlExpression.java
  
  Index: SqlExpression.java
  ===================================================================
  RCS file: /home/cvs/jakarta-turbine-torque/src/java/org/apache/torque/util/SqlExpression.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- SqlExpression.java	2001/10/15 16:26:51	1.7
  +++ SqlExpression.java	2001/10/18 19:28:58	1.8
  @@ -77,7 +77,7 @@
    * @author <a href="mailto:jmcnally@collab.net">John D. McNally</a>
    * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
    * @author <a href="mailto:fedor@apache.org">Fedor Karpelevitch</a>
  - * @version $Id: SqlExpression.java,v 1.7 2001/10/15 16:26:51 dlr Exp $
  + * @version $Id: SqlExpression.java,v 1.8 2001/10/18 19:28:58 jmcnally Exp $
    */
   public class SqlExpression
   {
  @@ -179,7 +179,7 @@
        */
       public static String build( String columnName,
                                   Object criteria,
  -                                String comparison )
  +                                SqlEnum comparison )
           throws Exception
       {
           // 'db' can be null because 'ignoreCase' is null
  @@ -204,7 +204,7 @@
        */
       public static String build( String columnName,
                                   Object criteria,
  -                                String comparison,
  +                                SqlEnum comparison,
                                   boolean ignoreCase,
                                   DB db )
           throws Exception
  @@ -235,7 +235,7 @@
        */
       public static void build( String columnName,
                                 Object criteria,
  -                              String comparison,
  +                              SqlEnum comparison,
                                 boolean ignoreCase,
                                 DB db,
                                 StringBuffer whereClause)
  @@ -474,7 +474,7 @@
        */
       static String buildIn(String columnName,
                             Object criteria,
  -                          String comparison,
  +                          SqlEnum comparison,
                             boolean ignoreCase,
                             DB db)
       {
  @@ -502,7 +502,7 @@
        */
       static void buildIn(String columnName,
                           Object criteria,
  -                        String comparison,
  +                        SqlEnum comparison,
                           boolean ignoreCase,
                           DB db,
                           StringBuffer whereClause)
  
  
  
  1.1                  jakarta-turbine-torque/src/java/org/apache/torque/util/SqlEnum.java
  
  Index: SqlEnum.java
  ===================================================================
  package org.apache.torque.util;
  
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Turbine" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Turbine", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  
  /**
   * A typesafe enum of sql string fragments.  Used by Criteria and SqlExpression
   * to build queries.  Criteria also makes most of the constants available
   * in order to specify a criterion.
   *
   * @author <a href="mailto:jmcnally@collab,net"></a>
   * @version $Id: SqlEnum.java,v 1.1 2001/10/18 19:28:58 jmcnally Exp $
   * @since 3.0
   */
  class SqlEnum 
  {
      private final String s;
      
      private SqlEnum(String s) 
      { 
          this.s = s;
      }
  
      public final String toString()  
      { 
          return s;
      }
      
      public static final SqlEnum EQUAL = 
          new SqlEnum("=");
      public static final SqlEnum NOT_EQUAL = 
              new SqlEnum("<>");
      public static final SqlEnum ALT_NOT_EQUAL = 
          new SqlEnum("!=");
      public static final SqlEnum GREATER_THAN =
          new SqlEnum(">");
      public static final SqlEnum LESS_THAN =
          new SqlEnum("<");
      public static final SqlEnum GREATER_EQUAL =
          new SqlEnum(">=");
      public static final SqlEnum LESS_EQUAL =
          new SqlEnum("<=");  
      public static final SqlEnum LIKE =
          new SqlEnum(" LIKE ");
      public static final SqlEnum IN =
          new SqlEnum(" IN ");
      public static final SqlEnum NOT_IN =
          new SqlEnum(" NOT IN ");
      public static final SqlEnum CUSTOM =
          new SqlEnum("CUSTOM");
      public static final SqlEnum JOIN =
          new SqlEnum("JOIN");
      public static final SqlEnum DISTINCT =
          new SqlEnum("DISTINCT ");
      public static final SqlEnum ALL =
          new SqlEnum("ALL ");
      public static final SqlEnum ASC =
          new SqlEnum("ASC");
      public static final SqlEnum DESC =
          new SqlEnum("DESC");
      public static final SqlEnum ISNULL =
          new SqlEnum(" IS NULL ");
      public static final SqlEnum ISNOTNULL =
          new SqlEnum(" IS NOT NULL ");
      /*
        public static final SqlEnum  =
              new SqlEnum("");
      */
      }
  
  
  

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