db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From to...@apache.org
Subject cvs commit: db-ojb/src/java/org/apache/ojb/broker/accesslayer StatementsForClassImpl.java
Date Sun, 24 Apr 2005 16:29:10 GMT
tomdz       2005/04/24 09:29:10

  Modified:    src/java/org/apache/ojb/broker/accesslayer/sql
                        SqlGeneratorDefaultImpl.java
               src/java/org/apache/ojb/broker/query QueryFactory.java
                        QueryByExampleImpl.java QueryByExample.java
                        QueryFactoryNew.java QueryFactoryNewImpl.java
               src/java/org/apache/ojb/broker/core
                        QueryReferenceBroker.java
               src/java/org/apache/ojb/broker/metadata
                        ArgumentDescriptor.java ProcedureDescriptor.java
                        RepositoryTags.java RepositoryElements.java
                        ClassDescriptor.java RepositoryXmlHandler.java
               src/java/org/apache/ojb/broker/accesslayer
                        StatementsForClassImpl.java
  Added:       src/java/org/apache/ojb/broker/accesslayer/sql
                        SqlProcedureFKStatement.java
               src/java/org/apache/ojb/broker/metadata
                        SelectByFKProcedureDescriptor.java
                        SelectByPKProcedureDescriptor.java
  Log:
  Applied patch (except to the XDoclet module) supplied by Vadim Gritsenko with issue OJB-16
with small changes to account for changes in the 1.1 branch
  
  Revision  Changes    Path
  1.29      +29 -6     db-ojb/src/java/org/apache/ojb/broker/accesslayer/sql/SqlGeneratorDefaultImpl.java
  
  Index: SqlGeneratorDefaultImpl.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/accesslayer/sql/SqlGeneratorDefaultImpl.java,v
  retrieving revision 1.28
  retrieving revision 1.29
  diff -u -r1.28 -r1.29
  --- SqlGeneratorDefaultImpl.java	2 Dec 2004 21:37:46 -0000	1.28
  +++ SqlGeneratorDefaultImpl.java	24 Apr 2005 16:29:10 -0000	1.29
  @@ -28,6 +28,7 @@
   import org.apache.ojb.broker.query.InCriterion;
   import org.apache.ojb.broker.query.NullCriteria;
   import org.apache.ojb.broker.query.Query;
  +import org.apache.ojb.broker.query.QueryByExample;
   import org.apache.ojb.broker.query.SelectionCriteria;
   import org.apache.ojb.broker.query.SqlCriteria;
   import org.apache.ojb.broker.util.logging.Logger;
  @@ -185,11 +186,19 @@
        */
       public String getPreparedSelectByPkStatement(ClassDescriptor cld)
       {
  -        SqlStatement sql;
  -        String result;
  +        ProcedureDescriptor pd = cld.getSelectByPKProcedure();
  +        SqlStatement        sql;
   
  -        sql = new SqlSelectByPkStatement(m_platform, m_logger, cld);
  -        result = sql.getStatement();
  +        if (pd == null)
  +        {
  +            sql = new SqlSelectByPkStatement(m_platform, m_logger, cld);
  +        }
  +        else
  +        {
  +            sql = new SqlProcedureStatement(pd, m_logger);
  +        }
  +
  +        String result = sql.getStatement();
   
           if (m_logger.isDebugEnabled())
           {
  @@ -205,7 +214,21 @@
        */
       public String getPreparedSelectStatement(Query query, ClassDescriptor cld)
       {
  -        return new SqlSelectStatement(m_platform, m_logger, cld, query).getStatement();
  +        ProcedureDescriptor pd = cld.getSelectByFKProcedure();
  +        SqlStatement        sql;
  +
  +        if ((query instanceof QueryByExample) && (pd != null))
  +        {
  +            // Special case for QueryByExample and SelectByFKProcedure
  +            sql = new SqlProcedureFKStatement(pd, (QueryByExample)query, m_logger);
  +        }
  +        else
  +         {
  +            // All other queries
  +            sql = new SqlSelectStatement(m_platform, m_logger, cld, query);
  +        }
  +
  +        return sql.getStatement();
       }
   
       /**
  
  
  
  1.1                  db-ojb/src/java/org/apache/ojb/broker/accesslayer/sql/SqlProcedureFKStatement.java
  
  Index: SqlProcedureFKStatement.java
  ===================================================================
  package org.apache.ojb.broker.accesslayer.sql;
  
  /* Copyright 2003-2004 The Apache Software Foundation
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  import java.util.Collection;
  import java.util.Enumeration;
  import java.util.HashMap;
  import java.util.Iterator;
  
  import org.apache.ojb.broker.metadata.ArgumentDescriptor;
  import org.apache.ojb.broker.metadata.ProcedureDescriptor;
  import org.apache.ojb.broker.query.QueryByExample;
  import org.apache.ojb.broker.query.SelectionCriteria;
  import org.apache.ojb.broker.util.logging.Logger;
  
  /**
   * Model a call to a stored procedure based on ProcedureDescriptors
   *
   * @see org.apache.ojb.broker.metadata.ProcedureDescriptor
   * @author <a href="mailto:rburt3@mchsi.com">Randall Burt</a>
   * @author <a href="mailto:rgallagh@bellsouth.net">Ron Gallagher</a>
   * @author <a href="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
   * @version $Id: SqlProcedureFKStatement.java,v 1.1 2005/04/24 16:29:10 tomdz Exp $
   */
  public class SqlProcedureFKStatement implements SqlStatement
  {
  
      /**
       * The descriptor that defines the procedure to invoke.
       */
      private ProcedureDescriptor procedureDescriptor;
  
      /**
       * Query
       */
      private QueryByExample query;
  
      /**
       * The logger to utilize
       */
      private Logger logger;
  
      /**
       * Create an instance of this object.
       *
       * @param procedureDescriptor the descriptor that defines the procedure to invoke.
       * @param logger the logger to utilize
       */
      public SqlProcedureFKStatement(ProcedureDescriptor procedureDescriptor, QueryByExample
query, Logger logger)
      {
          // Save the values.
          this.procedureDescriptor = procedureDescriptor;
          this.query = query;
          this.logger = logger;
      }
  
      /**
       * Get the syntax that is required to invoke the procedure that is defined
       * by the <code>ProcedureDescriptor</code> that was passed to the
       * constructor of this class.
       *
       * @see SqlStatement#getStatement()
       */
      public String getStatement()
      {
          // Collect criteria name/values
          HashMap values = new HashMap();
          for (Enumeration e = query.getCriteria().getElements(); e.hasMoreElements();) {
              SelectionCriteria c = (SelectionCriteria) e.nextElement();
              if (c.getValue() != null) {
                  values.put(c.getAttribute(), c.getValue());
              }
          }
  
          // Build statement
          StringBuffer sb = new StringBuffer(512);
          if (this.procedureDescriptor.hasReturnValue())
          {
              sb.append("{ ?= call ");
          }
          else
          {
              sb.append("{ call ");
          }
          sb.append(this.procedureDescriptor.getName());
          sb.append("(");
  
          Collection arguments = this.procedureDescriptor.getArguments();
          for (Iterator i = arguments.iterator(); i.hasNext();)
          {
              ArgumentDescriptor argument = (ArgumentDescriptor) i.next();
              Object value = values.get(argument.getFieldRefName());
              if (value == null) {
                  sb.append("NULL");
              } else {
                  sb.append("?");
              }
  
              if (i.hasNext()) {
                  sb.append(',');
              }
          }
          sb.append(") }");
          return sb.toString();
      }
  }
  
  
  
  1.23      +10 -1     db-ojb/src/java/org/apache/ojb/broker/query/QueryFactory.java
  
  Index: QueryFactory.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/query/QueryFactory.java,v
  retrieving revision 1.22
  retrieving revision 1.23
  diff -u -r1.22 -r1.23
  --- QueryFactory.java	13 Apr 2005 18:44:17 -0000	1.22
  +++ QueryFactory.java	24 Apr 2005 16:29:10 -0000	1.23
  @@ -120,6 +120,15 @@
       }
   
       /**
  +     * Return a QueryByExample for name-value pairs
  +     * @return QueryByExample
  +     */
  +    public static QueryByExample newQueryByExample(Class classToSearchFrom, String[] names,
Object[] values, boolean distinct)
  +    {
  +        return theFactory.newQueryByExample(classToSearchFrom, names, values, distinct);
  +    }
  +
  +    /**
        * create a new QueryByMtoNCriteria
        * @param classToSearchFrom
        * @param indirectionTable
  
  
  
  1.2       +82 -39    db-ojb/src/java/org/apache/ojb/broker/query/QueryByExampleImpl.java
  
  Index: QueryByExampleImpl.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/query/QueryByExampleImpl.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- QueryByExampleImpl.java	1 Apr 2005 20:28:08 -0000	1.1
  +++ QueryByExampleImpl.java	24 Apr 2005 16:29:10 -0000	1.2
  @@ -33,40 +33,59 @@
   {
       private static final long serialVersionUID = -7982652379502961071L;
   
  -    private Object m_exampleObject;
  -    private boolean m_ignoreZero = false; // numeric 0 are treated as null
  +    private Object   exampleObject;
  +    private String[] fieldNames;
  +    private Object[] values;
  +    private boolean  ignoreZero = false; // numeric 0 are treated as null
   
       /**
        * @param anExampleObject
        */
       QueryByExampleImpl(Object anExampleObject)
       {
  -        this (anExampleObject, false);
  +        this(anExampleObject, false);
       }
  -    
  +
       /**
        * @param anExampleObject
        * @param distinct
        */
  -    QueryByExampleImpl(Object anExampleObject, boolean distinct)
  +    QueryByExampleImpl(Object exampleObject, boolean distinct)
       {
  -        m_exampleObject = anExampleObject;
  +        this.exampleObject = exampleObject;
  +        this.fieldNames    = null;
  +        this.values        = null;
   
  -        setSearchClass(anExampleObject.getClass());
  -        setBaseClass(anExampleObject.getClass());
  -    
  +        setSearchClass(exampleObject.getClass());
  +        setBaseClass(exampleObject.getClass());
  +        setDistinct(distinct);
  +    }
  +
  +    /**
  +     * Build a Query based on name/value pairs<br>
  +     * all non null values are used as EqualToCriteria
  +     */
  +    QueryByExampleImpl(Class classToSearchFrom, String[] names, Object[] values, boolean
distinct)
  +    {
  +        this.exampleObject = null;
  +        this.fieldNames    = names;
  +        this.values        = values;
  +
  +        setSearchClass(classToSearchFrom);
  +        setBaseClass(classToSearchFrom);
           setDistinct(distinct);
       }
   
       /**
        * Copy Constructor
  -     * @param aQuery
  +     * @param source
        */
  -    QueryByExampleImpl(QueryByExampleImpl aQuery)
  +    QueryByExampleImpl(QueryByExampleImpl source)
       {
  -        super(aQuery);
  -        
  -        m_exampleObject = aQuery.getExampleObject();
  +        super(source);
  +        exampleObject = source.exampleObject;
  +        fieldNames    = source.fieldNames;
  +        values        = source.values;
       }
   
       /**
  @@ -74,7 +93,7 @@
        */
       public Object getExampleObject()
       {
  -        return m_exampleObject;
  +        return exampleObject;
       }
       
       /**
  @@ -86,51 +105,75 @@
        */
       protected void buildCriteria(PersistenceBroker aPb)
       {
  -        Object example = getExampleObject();
  -        DescriptorRepository repository = aPb.getConfiguration().getModel();
  -        ClassDescriptor cld = repository.getDescriptorFor(example.getClass());
  -        FieldDescriptor[] fds = cld.getFieldDescriptions();
           Criteria criteria = new Criteria();
   
  -        for (int i = 0; i < fds.length; i++)
  +        if (exampleObject != null)
           {
  -            try
  +            Object               example    = getExampleObject();
  +            DescriptorRepository repository = aPb.getConfiguration().getModel();
  +            ClassDescriptor      cld        = repository.getDescriptorFor(example.getClass());
  +            FieldDescriptor[]    fds        = cld.getFieldDescriptions();
  +    
  +            for (int idx = 0; idx < fds.length; idx++)
               {
  -                PersistentField f = fds[i].getPersistentField();
  -                Object value = f.get(example);
  -                
  -                if (value != null)
  +                try
                   {
  -                    if (value instanceof Number && isIgnoreZero())
  +                    PersistentField f     = fds[idx].getPersistentField();
  +                    Object          value = f.get(example);
  +                    
  +                    if (value != null)
                       {
  -                        Number numVal = (Number)value;
  -                        if (numVal.floatValue() != 0f)
  +                        if (isIgnoreZero() && (value instanceof Number))
                           {
  -                            criteria.addEqualTo(f.getName(), value);                  
         
  +                            Number numVal = (Number)value;
  +    
  +                            if (numVal.floatValue() != 0f)
  +                            {
  +                                criteria.addEqualTo(f.getName(), value);              
             
  +                            }
                           }
  +                        else
  +                        {
  +                            criteria.addEqualTo(f.getName(), value);
  +                        }    
                       }
  -                    else
  -                    {
  -                        criteria.addEqualTo(f.getName(), value);
  -                    }    
  +                }
  +                catch (Throwable ex)
  +                {
  +                    LoggerFactory.getDefaultLogger().error(ex);
                   }
               }
  -            catch (Throwable ex)
  +        }
  +        else
  +        {
  +            for (int idx = 0; idx < fieldNames.length; idx++)
               {
  -                LoggerFactory.getDefaultLogger().error(ex);
  +                if (isIgnoreZero() && (values[idx]  instanceof Number))
  +                {
  +                    Number numVal = (Number)values[idx] ;
  +
  +                    if (numVal.floatValue() != 0f)
  +                    {
  +                        criteria.addEqualTo(fieldNames[idx], values[idx] );           
                
  +                    }
  +                }
  +                else
  +                {
  +                    criteria.addEqualTo(fieldNames[idx], values[idx]);
  +                }    
               }
           }
   
           setCriteria(criteria);
       }
  -    
  +
       /**
        * if true numeric 0 is not included in the criteria.
        * @return Returns the ignoreZero.
        */
       public boolean isIgnoreZero()
       {
  -        return m_ignoreZero;
  +        return ignoreZero;
       }
       
       /**
  @@ -139,7 +182,7 @@
        */
       public void setIgnoreZero(boolean ignoreZero)
       {
  -        m_ignoreZero = ignoreZero;
  +        this.ignoreZero = ignoreZero;
       }
       
       /**
  
  
  
  1.9       +2 -2      db-ojb/src/java/org/apache/ojb/broker/query/QueryByExample.java
  
  Index: QueryByExample.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/query/QueryByExample.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- QueryByExample.java	1 Apr 2005 20:28:08 -0000	1.8
  +++ QueryByExample.java	24 Apr 2005 16:29:10 -0000	1.9
  @@ -22,7 +22,7 @@
    * @author <a href="mailto:jbraeuchi@gmx.ch">Jakob Braeuchi</a>
    * @version $Id$
    */
  -public interface QueryByExample extends Query
  +public interface QueryByExample extends QueryByCriteria
   {
       /**
        * if true numeric 0 is not included in the criteria.
  
  
  
  1.2       +12 -1     db-ojb/src/java/org/apache/ojb/broker/query/QueryFactoryNew.java
  
  Index: QueryFactoryNew.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/query/QueryFactoryNew.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- QueryFactoryNew.java	7 Apr 2005 19:55:11 -0000	1.1
  +++ QueryFactoryNew.java	24 Apr 2005 16:29:10 -0000	1.2
  @@ -90,6 +90,17 @@
       QueryByExample newQueryByExample(Object example);
   
       /**
  +     * Return a QueryByCriteria for example.
  +     * <br>Use with care because building of Query is not foolproof !!!
  +     * @param classToSearchFrom
  +     * @param names
  +     * @param values
  +     * @param distinct
  +     * @return QueryByExample
  +     */
  +    QueryByExample newQueryByExample(Class classToSearchFrom, String[] names, Object[]
values, boolean distinct);
  +
  +    /**
        * create a new QueryByMtoNCriteria.
        * @param classToSearchFrom
        * @param indirectionTable
  
  
  
  1.2       +15 -1     db-ojb/src/java/org/apache/ojb/broker/query/QueryFactoryNewImpl.java
  
  Index: QueryFactoryNewImpl.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/query/QueryFactoryNewImpl.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- QueryFactoryNewImpl.java	7 Apr 2005 19:55:11 -0000	1.1
  +++ QueryFactoryNewImpl.java	24 Apr 2005 16:29:10 -0000	1.2
  @@ -116,6 +116,20 @@
       }
   
       /**
  +     * Return a QueryByCriteria for example.
  +     * <br>Use with care because building of Query is not foolproof !!!
  +     * @param classToSearchFrom
  +     * @param names
  +     * @param values
  +     * @param distinct
  +     * @return QueryByExample
  +     */
  +    public QueryByExample newQueryByExample(Class classToSearchFrom, String[] names, Object[]
values, boolean distinct)
  +    {
  +        return new QueryByExampleImpl(classToSearchFrom, names, values, distinct);
  +    }
  +
  +    /**
        * create a new QueryByMtoNCriteria.
        * @param classToSearchFrom
        * @param indirectionTable
  
  
  
  1.36      +28 -10    db-ojb/src/java/org/apache/ojb/broker/core/QueryReferenceBroker.java
  
  Index: QueryReferenceBroker.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/core/QueryReferenceBroker.java,v
  retrieving revision 1.35
  retrieving revision 1.36
  diff -u -r1.35 -r1.36
  --- QueryReferenceBroker.java	23 Apr 2005 14:39:51 -0000	1.35
  +++ QueryReferenceBroker.java	24 Apr 2005 16:29:10 -0000	1.36
  @@ -362,7 +362,8 @@
                   }
                   else if ((m_retrievalTasks != null)
                           && !rds.isLazy()
  -                        && (rds.getItemProxyClass() == null))
  +                        && (rds.getItemProxyClass() == null)
  +                        && (pb.getClassDescriptor(rds.getItemClass()).getSelectByFKProcedure()
== null))
                   {
                       addRetrievalTask(obj, rds);
                       isRefObjDefined = false;
  @@ -585,7 +586,8 @@
           {
               if ((m_retrievalTasks != null) && !cds.isLazy()
                       && !cds.hasProxyItems()
  -                    && (cds.getQueryCustomizer() == null))
  +                    && (cds.getQueryCustomizer() == null)
  +                    && (pb.getClassDescriptor(cds.getItemClass()).getSelectByFKProcedure()
== null))
               {
                   addRetrievalTask(obj, cds);
               }
  @@ -732,18 +734,34 @@
        */
       private QueryByCriteria getFKQuery1toN(Object obj, CollectionDescriptor cod)
       {
  -        ValueContainer[] values = pb.serviceBrokerHelper().getFkTargetValuesForObject(cod,
obj ,false);
  -        ClassDescriptor refCld = cod.getItemClassDescriptor();
  +        ValueContainer[]  container = pb.serviceBrokerHelper().getFkTargetValuesForObject(cod,
obj ,false);
  +        ClassDescriptor   refCld = cod.getItemClassDescriptor();
           FieldDescriptor[] fields = cod.getForeignKeyFieldDescriptors(refCld);
  -        Criteria criteria = new Criteria();
   
  -        for (int i = 0; i < fields.length; i++)
  +        if (refCld.getSelectByFKProcedure() != null)
           {
  -            FieldDescriptor fld = fields[i];
  -            criteria.addEqualTo(fld.getAttributeName(), values[i].getValue());
  +            String[]          names  = new String[fields.length];
  +            Object[]          values = new Object[fields.length];
  +    
  +            for (int idx = 0; idx < fields.length; idx++)
  +            {
  +                names[idx]  = fields[idx].getAttributeName();
  +                values[idx] = container[idx].getValue();
  +            }
  +    
  +            return pb.getQueryFactory().newQueryByExample(refCld.getClassOfObject(), names,
values, false);
           }
  +        else
  +        {
  +            Criteria criteria = new Criteria();
  +
  +            for (int idx = 0; idx < fields.length; idx++)
  +            {
  +                criteria.addEqualTo(fields[idx].getAttributeName(), container[idx].getValue());
  +            }
   
  -        return pb.getQueryFactory().newQuery(refCld.getClassOfObject(), criteria);
  +            return pb.getQueryFactory().newQuery(refCld.getClassOfObject(), criteria);
  +        }
       }
   
       /**
  
  
  
  1.10      +10 -1     db-ojb/src/java/org/apache/ojb/broker/metadata/ArgumentDescriptor.java
  
  Index: ArgumentDescriptor.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/metadata/ArgumentDescriptor.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- ArgumentDescriptor.java	11 Aug 2004 00:41:46 -0000	1.9
  +++ ArgumentDescriptor.java	24 Apr 2005 16:29:10 -0000	1.10
  @@ -187,6 +187,15 @@
           }
       }
   
  +    /**
  +     * Retrieve the field name that this argument is related to.
  +     * @return The current value
  +     */
  +    public final String getFieldRefName()
  +    {
  +        return this.fieldRefName;
  +    }
  +
       //---------------------------------------------------------------
       /**
        * Retrieve the field descriptor that this argument is related to.
  
  
  
  1.8       +2 -2      db-ojb/src/java/org/apache/ojb/broker/metadata/ProcedureDescriptor.java
  
  Index: ProcedureDescriptor.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/metadata/ProcedureDescriptor.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- ProcedureDescriptor.java	4 Apr 2004 23:53:34 -0000	1.7
  +++ ProcedureDescriptor.java	24 Apr 2005 16:29:10 -0000	1.8
  @@ -124,7 +124,7 @@
        * @return <code>true</code> if there is a return value for this
        * procedure.
        */
  -    public final boolean hasReturnValue()
  +    public boolean hasReturnValue()
       {
           return (this.returnValueFieldRef != null);
       }
  
  
  
  1.41      +2 -0      db-ojb/src/java/org/apache/ojb/broker/metadata/RepositoryTags.java
  
  Index: RepositoryTags.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/metadata/RepositoryTags.java,v
  retrieving revision 1.40
  retrieving revision 1.41
  diff -u -r1.40 -r1.41
  --- RepositoryTags.java	6 Jan 2005 21:00:02 -0000	1.40
  +++ RepositoryTags.java	24 Apr 2005 16:29:10 -0000	1.41
  @@ -165,6 +165,8 @@
           table.put("insert-procedure", new Integer(INSERT_PROCEDURE));
           table.put("update-procedure", new Integer(UPDATE_PROCEDURE));
           table.put("delete-procedure", new Integer(DELETE_PROCEDURE));
  +        table.put("select-by-pk-procedure", new Integer(SELECTBYPK_PROCEDURE));
  +        table.put("select-by-fk-procedure", new Integer(SELECTBYFK_PROCEDURE));
           table.put("constant-argument", new Integer(CONSTANT_ARGUMENT));
           table.put("runtime-argument", new Integer(RUNTIME_ARGUMENT));
           table.put("return", new Integer(RETURN));
  
  
  
  1.41      +4 -1      db-ojb/src/java/org/apache/ojb/broker/metadata/RepositoryElements.java
  
  Index: RepositoryElements.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/metadata/RepositoryElements.java,v
  retrieving revision 1.40
  retrieving revision 1.41
  diff -u -r1.40 -r1.41
  --- RepositoryElements.java	6 Jan 2005 21:00:02 -0000	1.40
  +++ RepositoryElements.java	24 Apr 2005 16:29:10 -0000	1.41
  @@ -148,8 +148,11 @@
   
   	public static final int USE_OBJECT_FACTORY = 121;
   
  +    public static final int SELECTBYPK_PROCEDURE = 123;
  +    public static final int SELECTBYFK_PROCEDURE = 124;
  +
       // maintain a next id to keep track where we are
  -    static final int _NEXT = 123;
  +    static final int _NEXT = 125;
   
       // String constants
       public static final String TAG_ACCESS = "access";
  
  
  
  1.106     +61 -2     db-ojb/src/java/org/apache/ojb/broker/metadata/ClassDescriptor.java
  
  Index: ClassDescriptor.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/metadata/ClassDescriptor.java,v
  retrieving revision 1.105
  retrieving revision 1.106
  diff -u -r1.105 -r1.106
  --- ClassDescriptor.java	2 Apr 2005 09:44:39 -0000	1.105
  +++ ClassDescriptor.java	24 Apr 2005 16:29:10 -0000	1.106
  @@ -77,6 +77,17 @@
        */
       private DeleteProcedureDescriptor deleteProcedure;
   
  +    /**
  +     * The descriptor for the select by PK procedure/function.
  +     */
  +    private SelectByPKProcedureDescriptor selectByPKProcedure;
  +
  +    //---------------------------------------------------------------
  +    /**
  +     * The descriptor for the select by FK procedure/function.
  +     */
  +    private SelectByFKProcedureDescriptor selectByFKProcedure;
  +
       //---------------------------------------------------------------
       // transient fields, to make this class serializable we have to declare
       // some transient fields and some associated string fields to reinitialze
  @@ -248,6 +259,7 @@
       {
           return baseClass;
       }
  +
       public void setBaseClass(String baseClass)
       {
           this.baseClass = baseClass;
  @@ -257,6 +269,7 @@
       {
           this.superPersistentFieldDescriptors = superPersistentFieldDescriptors;
       }
  +
       public Vector getSuperPersistentFieldDescriptors()
       {
           return superPersistentFieldDescriptors;
  @@ -1688,7 +1701,53 @@
        */
       public DeleteProcedureDescriptor getDeleteProcedure()
       {
  -        return this.deleteProcedure;
  +        return deleteProcedure;
  +    }
  +
  +    /**
  +     * Change the descriptor for the select by PK procedure/function.
  +     *
  +     * @param newValue the new value.
  +     */
  +    public void setSelectByPKProcedure(SelectByPKProcedureDescriptor newValue)
  +    {
  +        selectByPKProcedure = newValue;
  +    }
  +
  +    /**
  +     * Change the descriptor for the select by FK procedure/function.
  +     *
  +     * @param newValue the new value.
  +     */
  +    public void setSelectByFKProcedure(SelectByFKProcedureDescriptor newValue)
  +    {
  +        selectByFKProcedure = newValue;
  +        if (selectByFKProcedure != null)
  +        {
  +            selectByFKProcedure.initArguments();
  +        }
  +    }
  +
  +    //---------------------------------------------------------------
  +    /**
  +     * Retrieve the descriptor for the select by PK procedure/function.
  +     *
  +     * @return The current value
  +     */
  +    public SelectByFKProcedureDescriptor getSelectByFKProcedure()
  +    {
  +        return selectByFKProcedure;
  +    }
  +
  +    //---------------------------------------------------------------
  +    /**
  +     * Retrieve the descriptor for the select by PK procedure/function.
  +     *
  +     * @return The current value
  +     */
  +    public SelectByPKProcedureDescriptor getSelectByPKProcedure()
  +    {
  +        return this.selectByPKProcedure;
       }
   
       /**
  
  
  
  1.68      +65 -1     db-ojb/src/java/org/apache/ojb/broker/metadata/RepositoryXmlHandler.java
  
  Index: RepositoryXmlHandler.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/metadata/RepositoryXmlHandler.java,v
  retrieving revision 1.67
  retrieving revision 1.68
  diff -u -r1.67 -r1.68
  --- RepositoryXmlHandler.java	6 Jan 2005 21:00:02 -0000	1.67
  +++ RepositoryXmlHandler.java	24 Apr 2005 16:29:10 -0000	1.68
  @@ -947,6 +947,56 @@
                           break;
   
                       }
  +                case SELECTBYPK_PROCEDURE:
  +                    {
  +                        if (isDebug) logger.debug("    > " + RepositoryTags.getTagById(SELECTBYPK_PROCEDURE));
  +
  +                        // Get the proc name
  +                        String procName = atts.getValue(RepositoryTags.getTagById(NAME));
  +                        if (isDebug) logger.debug("     " + RepositoryTags.getTagById(NAME)
+
  +                                                  ": " + procName);
  +
  +                        // create the procedure descriptor
  +                        SelectByPKProcedureDescriptor proc =
  +                            new SelectByPKProcedureDescriptor(m_CurrentCLD,
  +                                                          procName);
  +                        m_CurrentProcedure = proc;
  +
  +                        // Get the name of the field ref that will receive the
  +                        // return value.
  +                        String returnFieldRefName = atts.getValue(RepositoryTags.getTagById(RETURN_FIELD_REF));
  +                        if (isDebug) logger.debug("     " + RepositoryTags.getTagById(RETURN_FIELD_REF)
+
  +                                                  ": " + returnFieldRefName);
  +                        proc.setReturnValueFieldRef(returnFieldRefName);
  +
  +                        break;
  +
  +                    }
  +                case SELECTBYFK_PROCEDURE:
  +                    {
  +                        if (isDebug) logger.debug("    > " + RepositoryTags.getTagById(SELECTBYFK_PROCEDURE));
  +
  +                        // Get the proc name
  +                        String procName = atts.getValue(RepositoryTags.getTagById(NAME));
  +                        if (isDebug) logger.debug("     " + RepositoryTags.getTagById(NAME)
+
  +                                                  ": " + procName);
  +
  +                        // create the procedure descriptor
  +                        SelectByFKProcedureDescriptor proc =
  +                            new SelectByFKProcedureDescriptor(m_CurrentCLD,
  +                                                          procName);
  +                        m_CurrentProcedure = proc;
  +
  +                        // Get the name of the field ref that will receive the
  +                        // return value.
  +                        String returnFieldRefName = atts.getValue(RepositoryTags.getTagById(RETURN_FIELD_REF));
  +                        if (isDebug) logger.debug("     " + RepositoryTags.getTagById(RETURN_FIELD_REF)
+
  +                                                  ": " + returnFieldRefName);
  +                        proc.setReturnValueFieldRef(returnFieldRefName);
  +
  +                        break;
  +
  +                    }
                   case CONSTANT_ARGUMENT:
                       {
                           String value = null;
  @@ -1140,6 +1190,20 @@
                           m_CurrentProcedure = null;
                           break;
                       }
  +                case SELECTBYPK_PROCEDURE:
  +                    {
  +                        if (isDebug) logger.debug("    < " + RepositoryTags.getTagById(SELECTBYPK_PROCEDURE));
  +                        m_CurrentCLD.setSelectByPKProcedure((SelectByPKProcedureDescriptor)m_CurrentProcedure);
  +                        m_CurrentProcedure = null;
  +                        break;
  +                    }
  +                case SELECTBYFK_PROCEDURE:
  +                    {
  +                        if (isDebug) logger.debug("    < " + RepositoryTags.getTagById(SELECTBYFK_PROCEDURE));
  +                        m_CurrentCLD.setSelectByFKProcedure((SelectByFKProcedureDescriptor)m_CurrentProcedure);
  +                        m_CurrentProcedure = null;
  +                        break;
  +                    }
                   case CONSTANT_ARGUMENT:
                       {
                           if (isDebug) logger.debug("    < " + RepositoryTags.getTagById(CONSTANT_ARGUMENT));
  
  
  
  1.1                  db-ojb/src/java/org/apache/ojb/broker/metadata/SelectByFKProcedureDescriptor.java
  
  Index: SelectByFKProcedureDescriptor.java
  ===================================================================
  package org.apache.ojb.broker.metadata;
  
  /* Copyright 2004-2004 The Apache Software Foundation
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  import org.apache.commons.lang.builder.ToStringBuilder;
  import org.apache.commons.lang.builder.ToStringStyle;
  
  import java.io.Serializable;
  import java.util.Vector;
  
  /**
   * An SelectByFKProcedureDescriptor contains information that is related to the
   * procedure/function that is used to handle selecting of the record by somebody's FK.
   * <br>
   * Note: Be careful when use SelectByFKProcedureDescriptor variables or caching
   * SelectByFKProcedureDescriptor instances, because instances could become invalid
   * during runtime (see {@link MetadataManager}).
   *
   * @author <a href="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
   * @version $Id: SelectByFKProcedureDescriptor.java,v 1.1 2005/04/24 16:29:10 tomdz Exp
$
   */
  public class SelectByFKProcedureDescriptor extends ProcedureDescriptor
      implements Serializable, XmlCapable
  {
      private static final long serialVersionUID = 3834306215823355958L;
  
      //---------------------------------------------------------------
      /**
       * Constructor declaration
       */
      public SelectByFKProcedureDescriptor(
          ClassDescriptor classDescriptor,
          String name)
      {
          super(classDescriptor, name);
          addArguments(getClassDescriptor().getFieldDescriptions());
      }
  
      /**
       * SelectByFK procedure always returns one value, ResultSet
       */
      public boolean hasReturnValue()
      {
          return true;
      }
  
      /*
       * @see XmlCapable#toXML()
       */
      public String toXML()
      {
          String eol = System.getProperty( "line.separator" );
  
          // The result
          StringBuffer result = new StringBuffer( 1024 );
          result.append( eol );
          result.append( "   " );
  
          // Opening tag and attributes
          result.append( " " );
          result.append( RepositoryTags.getOpeningTagNonClosingById( SELECTBYFK_PROCEDURE
) );
          result.append( " " );
          result.append( RepositoryTags.getAttribute( NAME, this.getName() ) );
          result.append( ">" );
          result.append( eol );
  
          // Closing tag
          result.append( "    " );
          result.append( RepositoryTags.getClosingTagById( SELECTBYFK_PROCEDURE ) );
          result.append( eol );
          return result.toString();
      }
  
      //---------------------------------------------------------------
      /**
       * Provide a string representation of this object
       *
       * @return a string representation of this object
       */
      public String toString()
      {
          ToStringBuilder buf = new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE);
          buf.append("name", this.getName());
          return buf.toString();
      }
  
      public void initArguments() {
          Vector refs = getClassDescriptor().getSuperPersistentFieldDescriptors();
          if (refs != null) {
              // Re-init arguments collection
              getArguments().clear();
  
              // Add super arguments
              addSuperArguments(getClassDescriptor().getBaseClass());
  
              // Add my arguments
              addArguments(getClassDescriptor().getFieldDescriptions());
          }
      }
  
      private void addSuperArguments(String baseClass) {
          if (baseClass != null) {
              ClassDescriptor baseCld = getClassDescriptor().getRepository().getDescriptorFor(baseClass);
              addSuperArguments(baseCld.getBaseClass());
              addArguments(baseCld.getFieldDescriptions());
          }
      }
  }
  
  
  
  1.1                  db-ojb/src/java/org/apache/ojb/broker/metadata/SelectByPKProcedureDescriptor.java
  
  Index: SelectByPKProcedureDescriptor.java
  ===================================================================
  package org.apache.ojb.broker.metadata;
  
  /* Copyright 2005 The Apache Software Foundation
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  import org.apache.commons.lang.builder.ToStringBuilder;
  import org.apache.commons.lang.builder.ToStringStyle;
  import java.io.Serializable;
  
  /**
   * An SelectByPKProcedureDescriptor contains information that is related to the
   * procedure/function that is used to handle selecting of the record by its PK.
   * <br>
   * Note: Be careful when use SelectByPKProcedureDescriptor variables or caching
   * SelectByPKProcedureDescriptor instances, because instances could become invalid
   * during runtime (see {@link MetadataManager}).
   *
   * @author <a href="mailto:vgritsenko@apache.org">Vadim Gritsenko<a>
   * @version $Id: SelectByPKProcedureDescriptor.java,v 1.1 2005/04/24 16:29:10 tomdz Exp
$
   */
  public class SelectByPKProcedureDescriptor extends ProcedureDescriptor
      implements Serializable, XmlCapable
  {
      private static final long serialVersionUID = 3257566204813063731L;
  
      //---------------------------------------------------------------
      /**
       * Constructor declaration
       */
      public SelectByPKProcedureDescriptor(
          ClassDescriptor classDescriptor,
          String name)
      {
          super(classDescriptor, name);
          this.addArguments(this.getClassDescriptor().getPkFields());
          this.addArguments(this.getClassDescriptor().getLockingFields());
      }
  
      /**
       * SelectByPK procedure always returns one value, ResultSet
       */
      public boolean hasReturnValue()
      {
          return true;
      }
  
      /*
       * @see XmlCapable#toXML()
       */
      public String toXML()
      {
          String eol = System.getProperty( "line.separator" );
  
          // The result
          StringBuffer result = new StringBuffer( 1024 );
          result.append( eol );
          result.append( "   " );
  
          // Opening tag and attributes
          result.append( " " );
          result.append( RepositoryTags.getOpeningTagNonClosingById( SELECTBYPK_PROCEDURE
) );
          result.append( " " );
          result.append( RepositoryTags.getAttribute( NAME, this.getName() ) );
          result.append( ">" );
          result.append( eol );
  
          // Closing tag
          result.append( "    " );
          result.append( RepositoryTags.getClosingTagById( SELECTBYPK_PROCEDURE ) );
          result.append( eol );
          return result.toString();
      }
  
      //---------------------------------------------------------------
      /**
       * Provide a string representation of this object
       *
       * @return a string representation of this object
       */
      public String toString()
      {
          ToStringBuilder buf = new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE);
          buf.append("name", this.getName());
          return buf.toString();
      }
  }
  
  
  
  1.28      +9 -2      db-ojb/src/java/org/apache/ojb/broker/accesslayer/StatementsForClassImpl.java
  
  Index: StatementsForClassImpl.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/accesslayer/StatementsForClassImpl.java,v
  retrieving revision 1.27
  retrieving revision 1.28
  diff -u -r1.27 -r1.28
  --- StatementsForClassImpl.java	6 Apr 2005 14:45:42 -0000	1.27
  +++ StatementsForClassImpl.java	24 Apr 2005 16:29:10 -0000	1.28
  @@ -184,6 +184,12 @@
           return stmt;
       }
   
  +    protected boolean usePreparedSelectByPKStatement()
  +    {
  +        return !(classDescriptor.getSelectByPKProcedure() != null &&
  +                classDescriptor.getSelectByPKProcedure().hasReturnValues());
  +    }
  +
       public PreparedStatement getSelectByPKStmt(Connection con) throws SQLException
       {
           if (selectByPKSql == null)
  @@ -192,7 +198,8 @@
           }
           try
           {
  -            return prepareStatement(con, selectByPKSql, Query.NOT_SCROLLABLE, true);
  +            return prepareStatement(con, selectByPKSql, Query.NOT_SCROLLABLE,
  +                                    usePreparedSelectByPKStatement());
           }
           catch (SQLException ex)
           {
  
  
  

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