db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From b..@apache.org
Subject cvs commit: db-ojb/src/java/org/apache/ojb/broker/util BrokerHelper.java
Date Sun, 25 Jan 2004 15:48:38 GMT
brj         2004/01/25 07:48:38

  Modified:    src/java/org/apache/ojb/broker/util Tag: OJB_BRANCH_1_0
                        BrokerHelper.java
  Log:
  use count(*) if query is not distinct
  
  Revision  Changes    Path
  No                   revision
  No                   revision
  1.31.2.1  +43 -23    db-ojb/src/java/org/apache/ojb/broker/util/BrokerHelper.java
  
  Index: BrokerHelper.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/util/BrokerHelper.java,v
  retrieving revision 1.31
  retrieving revision 1.31.2.1
  diff -u -r1.31 -r1.31.2.1
  --- BrokerHelper.java	9 Dec 2003 17:19:58 -0000	1.31
  +++ BrokerHelper.java	25 Jan 2004 15:48:38 -0000	1.31.2.1
  @@ -161,7 +161,7 @@
   	 * Answer the real ClassDescriptor for anObj
   	 * ie. aCld may be an Interface of anObj, so the cld for anObj is returned
   	 */
  -	protected ClassDescriptor getRealClassDescriptor(ClassDescriptor aCld, Object anObj)
  +	private ClassDescriptor getRealClassDescriptor(ClassDescriptor aCld, Object anObj)
   	{
   		ClassDescriptor result;
   
  @@ -275,19 +275,31 @@
   	}
   
       /**
  -     * Return true if aValue is regarded as null<br>
  -     * null, Number(0) or empty String
  -     * @param aValue
  -     * @return
  +     * Decide if the given object value represents 'null'.<br/>
  +     *
  +     * - If given value is 'null' itself, true will be returned<br/>
  +     *
  +     * - If given value is instance of Number with value 0 and the field-descriptor
  +     * is a primary key and represents a primitive field, true will be returned<br/>
  +     *
  +     * - If given value is instance of String with length 0 and the field-descriptor
  +     * is a primary key, true will be returned<br/>
        */
  -    private boolean isNull(Object aValue)
  +    public static boolean representsNull(FieldDescriptor fld, Object aValue)
       {
  -        return (
  -            (aValue == null)
  -                || ((aValue instanceof Number) && (((Number) aValue).longValue()
== 0))
  -                || ((aValue instanceof String) && (((String) aValue).length() ==
0)));
  -    }
  +        if(aValue == null) return true;
   
  +        boolean result = false;
  +        if(((aValue instanceof Number) && (((Number) aValue).longValue() == 0)))
  +        {
  +            result = fld.getPersistentField().getType().isPrimitive() && fld.isPrimaryKey();
  +        }
  +        else if((aValue instanceof String) && (((String) aValue).length() == 0))
  +        {
  +            result = fld.isPrimaryKey();
  +        }
  +        return result;
  +    }
   
   	/**
   	 * Get an autoincremented value that has already
  @@ -302,7 +314,7 @@
   	 */
   	protected Object getAutoIncrementValue(FieldDescriptor fd, Object obj, Object cv)
       {
  -        if (isNull(cv))
  +        if (representsNull(fd, cv))
           {
               PersistentField f = fd.getPersistentField();
               try
  @@ -510,8 +522,7 @@
               Class searchClass = aQuery.getSearchClass();
               ReportQueryByCriteria countQuery;
               Criteria countCrit = null;
  -            FieldDescriptor[] pkFields = m_broker.getClassDescriptor(searchClass).getPkFields();
  -            String[] columns = new String[pkFields.length];
  +            String[] columns;
   
               // build a ReportQuery based on query orderby needs to be cleared
               if (aQuery.getCriteria() != null)
  @@ -519,19 +530,28 @@
                   countCrit = aQuery.getCriteria().copy(false, false, false);
               }
   
  -            // BRJ: add a column for each pkField, make it distinct if query is distinct
  -            // TBD check if it really works for multiple keys ?
  -            for (int i = 0; i < pkFields.length; i++)
  +            if (aQuery.isDistinct())
               {
  -                if (aQuery.isDistinct())
  +                //
  +                // BRJ: Count distinct is dbms dependent
  +                // hsql/sapdb: select count (distinct(person_id || project_id)) from person_project
  +                // mysql: select count (distinct person_id,project_id)  from person_project
  +                //
  +                FieldDescriptor[] pkFields = m_broker.getClassDescriptor(searchClass).getPkFields();
  +                columns = new String[pkFields.length];
  +                
  +                for (int i = 0; i < pkFields.length; i++)
                   {
                       columns[i] = "count(distinct " + pkFields[i].getAttributeName() + ")";
                   }
  -                else
  -                {
  -                    columns[i] = "count(" + pkFields[i].getAttributeName() + ")";
  -                }
               }
  +            else
  +            {
  +                // BRJ: use count(*) if query not distinct
  +                columns = new String[1];
  +                columns[0] = "count(*)";
  +            }
  +            
   
               // BRJ: we have to preserve indirection table !
               if (aQuery instanceof MtoNQuery)
  
  
  

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


Mime
View raw message