You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-dev@db.apache.org by br...@apache.org on 2003/01/05 10:16:22 UTC

cvs commit: jakarta-ojb/src/java/org/apache/ojb/broker/util SqlHelper.java

brj         2003/01/05 01:16:22

  Modified:    src/java/org/apache/ojb/broker/util SqlHelper.java
  Log:
  new method to split path into column and function
  
  Revision  Changes    Path
  1.2       +110 -7    jakarta-ojb/src/java/org/apache/ojb/broker/util/SqlHelper.java
  
  Index: SqlHelper.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ojb/src/java/org/apache/ojb/broker/util/SqlHelper.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- SqlHelper.java	24 Dec 2002 13:25:26 -0000	1.1
  +++ SqlHelper.java	5 Jan 2003 09:16:22 -0000	1.2
  @@ -1,5 +1,59 @@
   package org.apache.ojb.broker.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 ObjectRelationalBridge" 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 ObjectRelationalBridge", 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/>.
  + */
  +
   /**
    * Helper class for all SQL related stuff.
    *
  @@ -8,31 +62,80 @@
    */
   public class SqlHelper
   {
  +    public static final class PathInfo
  +    {
  +        public String column;
  +        public String function;
  +        
  +        PathInfo(String aColumn, String aFunction)
  +        {
  +            column = aColumn;
  +            function = aFunction;
  +        }
  +    }
  +
       /**
  -     * remove functions and () from path
  -     * ie: avg(amount) -> amount <br>
  -     * ie: sum(accounts.amount) -> accounts.amount
  +     * remove functions and () from path <br>
  +     * ie: avg(amount) -> amount <br> 
  +     * ie: sum (accounts.amount) -> accounts.amount <br>
  +     * ie: count(distinct id) -> id <br>
        * @param aPath the path to the attribute
        */
       public static String cleanPath(String aPath)
       {
  +        PathInfo p = splitPath(aPath);
  +        
  +        if (p != null)
  +        {
  +            return p.column;
  +        }
  +        else
  +        {
  +            return null;
  +        }
  +    }    
  +    
  +
  +    /**
  + 	 * Split a path into column and function, the functionPart contains all info
  + 	 * up to the column<br> 
  +     * ie: avg(amount) -> amount ,avg( <br> 
  +     * ie: sum (accounts.amount) -> accounts.amount , sum( <br> 
  +     * ie: count(distinct id) -> id , count(distinct <br>
  + 	 * @param aPath
  + 	 * @return PathInfo
  + 	 */
  +    public static PathInfo splitPath(String aPath)
  +    {
           int braceBegin;
           int braceEnd;
  +        int colBegin;
  +        String functionName;
  +        String colName;
   
           if (aPath == null)
           {
  -            return aPath;
  +            return null;
           }
   
           braceBegin = aPath.indexOf("(");
           braceEnd = aPath.lastIndexOf(")");
           if (braceBegin >= 0 && braceEnd >= 0)
           {
  -            return aPath.substring(braceBegin + 1, braceEnd).trim();
  +            colBegin = aPath.indexOf(" ", braceBegin);
  +            if (colBegin < 0)
  +            {
  +                colBegin = braceBegin;
  +            }
  +            functionName = aPath.substring(0, colBegin + 1);
  +            colName = aPath.substring(colBegin + 1, braceEnd).trim();
           }
           else
           {
  -            return aPath;
  +            functionName = null;
  +            colName = aPath;
           }
  +        
  +        return new PathInfo(colName, functionName);
       }
   }