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/test/org/apache/ojb/broker QueryTest.java
Date Sat, 21 Feb 2004 21:41:45 GMT
brj         2004/02/21 13:41:45

  Modified:    src/java/org/apache/ojb/broker/platforms
                        PlatformDefaultImpl.java Platform.java
                        PlatformMySQLImpl.java
               src/java/org/apache/ojb/broker/query Criteria.java
                        ValueCriteria.java
               src/java/org/apache/ojb/broker/accesslayer/sql
                        SqlQueryStatement.java
               src/test/org/apache/ojb/broker QueryTest.java
  Added:       src/java/org/apache/ojb/broker/query LikeCriteria.java
  Log:
  support for escaping wildcards in LIKE
  
  Revision  Changes    Path
  1.24      +20 -1     db-ojb/src/java/org/apache/ojb/broker/platforms/PlatformDefaultImpl.java
  
  Index: PlatformDefaultImpl.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/platforms/PlatformDefaultImpl.java,v
  retrieving revision 1.23
  retrieving revision 1.24
  diff -u -r1.23 -r1.24
  --- PlatformDefaultImpl.java	14 Feb 2004 20:59:53 -0000	1.23
  +++ PlatformDefaultImpl.java	21 Feb 2004 21:41:45 -0000	1.24
  @@ -57,6 +57,7 @@
   import org.apache.ojb.broker.PersistenceBrokerException;
   import org.apache.ojb.broker.accesslayer.JoinSyntaxTypes;
   import org.apache.ojb.broker.metadata.JdbcConnectionDescriptor;
  +import org.apache.ojb.broker.query.LikeCriteria;
   import org.apache.ojb.broker.util.logging.Logger;
   import org.apache.ojb.broker.util.logging.LoggerFactory;
   
  @@ -399,5 +400,23 @@
           }
   
           return buf.toString();
  +    }
  +    
  +    /**
  +     * @see org.apache.ojb.broker.platforms.Platform#getEscapeClause(org.apache.ojb.broker.query.LikeCriteria)
  +     */
  +    public String getEscapeClause(LikeCriteria aCriteria)
  +    {
  +        String value = (String)aCriteria.getValue();
  +        char escapeChar = LikeCriteria.getEscapeCharacter();
  +        
  +        if (value.indexOf(escapeChar) >= 0)
  +        {
  +            return " ESCAPE '" + escapeChar + "'";
  +        }
  +        else
  +        {
  +            return "";
  +        }
       }
   }
  
  
  
  1.22      +10 -1     db-ojb/src/java/org/apache/ojb/broker/platforms/Platform.java
  
  Index: Platform.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/platforms/Platform.java,v
  retrieving revision 1.21
  retrieving revision 1.22
  diff -u -r1.21 -r1.22
  --- Platform.java	14 Feb 2004 20:59:53 -0000	1.21
  +++ Platform.java	21 Feb 2004 21:41:45 -0000	1.22
  @@ -55,6 +55,7 @@
    */
   
   import org.apache.ojb.broker.metadata.JdbcConnectionDescriptor;
  +import org.apache.ojb.broker.query.LikeCriteria;
   
   import java.sql.CallableStatement;
   import java.sql.Connection;
  @@ -255,4 +256,12 @@
        * @return the concatenated String 
        */
       public String concatenate(String[] theColumns);
  +    
  +    /**
  +     * Answer the Clause used Escape wildcards in LIKE 
  +     * @param aCriteria
  +     * @return
  +     */
  +    public String getEscapeClause(LikeCriteria aCriteria);
  +    
   }
  
  
  
  1.12      +19 -1     db-ojb/src/java/org/apache/ojb/broker/platforms/PlatformMySQLImpl.java
  
  Index: PlatformMySQLImpl.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/platforms/PlatformMySQLImpl.java,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- PlatformMySQLImpl.java	14 Feb 2004 20:59:53 -0000	1.11
  +++ PlatformMySQLImpl.java	21 Feb 2004 21:41:45 -0000	1.12
  @@ -51,6 +51,8 @@
   import java.sql.SQLException;
   import java.sql.Types;
   
  +import org.apache.ojb.broker.query.LikeCriteria;
  +
   /**
    * @version 1.0
    * @author jakob bräuchi
  @@ -172,5 +174,21 @@
   
           buf.append(")");
           return buf.toString();
  +    }    
  +    
  +    /**
  +     * @see org.apache.ojb.broker.platforms.Platform#getEscapeClause(org.apache.ojb.broker.query.LikeCriteria)
  +     */
  +    public String getEscapeClause(LikeCriteria aCriteria)
  +    {
  +        if (LikeCriteria.getEscapeCharacter() != '\\')  
  +        {
  +            // the default escape character is \, so there's no need for an escape clause
  +            return super.getEscapeClause(aCriteria);
  +        }
  +        else
  +        {
  +            return "";
  +        }
       }    
   }
  
  
  
  1.39      +6 -81     db-ojb/src/java/org/apache/ojb/broker/query/Criteria.java
  
  Index: Criteria.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/query/Criteria.java,v
  retrieving revision 1.38
  retrieving revision 1.39
  diff -u -r1.38 -r1.39
  --- Criteria.java	13 Feb 2004 17:02:48 -0000	1.38
  +++ Criteria.java	21 Feb 2004 21:41:45 -0000	1.39
  @@ -339,49 +339,6 @@
       }
   
   	/**
  -	 * Generate a SQL search string from the pattern string passed.
  -	 * The pattern string is a simple pattern string using * as a wildcard.
  -	 * So Ander* would match Anderson and Anderton.
  -	 * The ? character is used to match a single occurence of a character.
  -	 * The '\' is used to escape the wild card characters so that we
  -	 * can search for strings containing * and ?.
  -	 * This method converts the criteria wildcard strings to SQL wildcards.
  -	 *
  -	 * @param pattern a criteria search pattern containing optional wildcards
  -	 * @return a SQL search pattern string with all escape codes processed.
  -	 */
  -	protected String generateSQLSearchPattern(String pattern)
  -	{
  -		StringBuffer sqlpattern = new StringBuffer();
  -
  -		char[] chars = pattern.toCharArray();
  -		for (int i = 0; i < chars.length; i++)
  -		{
  -			if (chars[i] == '\\')
  -			{
  -				// for the escape character add the next char as is.
  -				if (++i < chars.length)
  -				{
  -					sqlpattern.append(chars[i]);
  -				}
  -			}
  -			else if (chars[i] == '*')
  -			{
  -				sqlpattern.append("%");
  -			}
  -			else if (chars[i] == '?')
  -			{
  -				sqlpattern.append("_");
  -			}
  -			else
  -			{
  -				sqlpattern.append(chars[i]);
  -			}
  -		}
  -		return sqlpattern.toString();
  -	}
  -
  -	/**
   	 * Adds and equals (=) criteria for field comparison.
   	 * The field name will be translated into the appropriate columnName by SqlStatement.
   	 * <br>
  @@ -495,58 +452,26 @@
   	 * Adds Like (LIKE) criteria,
   	 * customer_name LIKE "m%ller"
   	 *
  -	 * Where Value is a string simple pattern matching can be used, the
  -	 * character * is a wildcard, e.g.
  -	 * customer_name = Ander*
  -	 * <P>
  -	 * Would match the customers: Anderson and Anderton.  The character ? matches a single
  -	 * occurence of a character.
  -	 *
  -	 * The '\' character is used as an escape character and allow searching for strings
  -	 * containing * or ?.  So in order to search for Ander* - use Ander\*.
  -	 *
  -	 * @param  attribute   The field name to be used
  +     * @see LikeCriteria
  +     * @param  attribute   The field name to be used
   	 * @param  value       An object representing the value of the field
   	 */
   	public void addLike(Object attribute, Object value)
   	{
  -		String pattern;
  -
  -		if (value != null)
  -		{
  -			// substitute our special character for SQL versions
  -			pattern = generateSQLSearchPattern((String) value);
  -		}
  -		else
  -		{
  -			pattern = null;
  -		}
  -
  -		addSelectionCriteria(ValueCriteria.buildLikeCriteria(attribute, pattern, getAlias()));
  +		addSelectionCriteria(ValueCriteria.buildLikeCriteria(attribute, value, getAlias()));
   	}
   
   	/**
   	 * Adds Like (NOT LIKE) criteria,
   	 * customer_id NOT LIKE 10034
   	 *
  +     * @see LikeCriteria
   	 * @param  attribute   The field name to be used
   	 * @param  value       An object representing the value of the field
   	 */
   	public void addNotLike(String attribute, Object value)
   	{
  -		String pattern;
  -
  -		if (value != null)
  -		{
  -			// substitute our special character for SQL versions
  -			pattern = generateSQLSearchPattern((String) value);
  -		}
  -		else
  -		{
  -			pattern = null;
  -		}
  -
  -        addSelectionCriteria(ValueCriteria.buildNotLikeCriteria(attribute, pattern, getAlias()));
  +        addSelectionCriteria(ValueCriteria.buildNotLikeCriteria(attribute, value, getAlias()));
   	}
   
   	/**
  
  
  
  1.5       +2 -2      db-ojb/src/java/org/apache/ojb/broker/query/ValueCriteria.java
  
  Index: ValueCriteria.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/query/ValueCriteria.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- ValueCriteria.java	13 Sep 2003 14:33:57 -0000	1.4
  +++ ValueCriteria.java	21 Feb 2004 21:41:45 -0000	1.5
  @@ -95,7 +95,7 @@
   
       static ValueCriteria buildLikeCriteria(Object anAttribute, Object aValue, String anAlias)
       {
  -        return new ValueCriteria(anAttribute, aValue, LIKE, anAlias);
  +        return new LikeCriteria(anAttribute, aValue, LIKE, anAlias);
       }
   
       static ValueCriteria buildNotLikeCriteria(Object anAttribute, Object aValue, String
anAlias)
  
  
  
  1.5       +117 -33   db-ojb/src/java/org/apache/ojb/broker/query/LikeCriteria.java
  
  
  
  
  1.65      +30 -4     db-ojb/src/java/org/apache/ojb/broker/accesslayer/sql/SqlQueryStatement.java
  
  Index: SqlQueryStatement.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/accesslayer/sql/SqlQueryStatement.java,v
  retrieving revision 1.64
  retrieving revision 1.65
  diff -u -r1.64 -r1.65
  --- SqlQueryStatement.java	20 Feb 2004 18:02:39 -0000	1.64
  +++ SqlQueryStatement.java	21 Feb 2004 21:41:45 -0000	1.65
  @@ -54,7 +54,13 @@
    * <http://www.apache.org/>.
    */
   
  -import java.util.*;
  +import java.util.ArrayList;
  +import java.util.Collection;
  +import java.util.Enumeration;
  +import java.util.HashMap;
  +import java.util.Iterator;
  +import java.util.List;
  +import java.util.Map;
   
   import org.apache.ojb.broker.PersistenceBrokerSQLException;
   import org.apache.ojb.broker.accesslayer.JoinSyntaxTypes;
  @@ -654,7 +660,7 @@
           appendColName(alias, pathInfo, c.isTranslateAttribute(), buf);
           buf.append(c.getClause());
           appendParameter(c.getValue(), buf);
  -        buf.append("AND");
  +        buf.append(" AND ");
           appendParameter(c.getValue2(), buf);
       }
   
  @@ -764,6 +770,22 @@
       }
   
       /**
  +     * Answer the SQL-Clause for a LikeCriteria
  +     *
  +     * @param c
  +     * @param buf
  +     */
  +    private void appendLikeCriteria(TableAlias alias, PathInfo pathInfo, LikeCriteria c,
  +            StringBuffer buf)
  +    {
  +        appendColName(alias, pathInfo, c.isTranslateAttribute(), buf);
  +        buf.append(c.getClause());
  +        appendParameter(c.getValue(), buf);
  +
  +        buf.append(m_platform.getEscapeClause(c));
  +    }
  +
  +    /**
        * Answer the SQL-Clause for a SelectionCriteria
        *
        * @param alias
  @@ -797,6 +819,10 @@
           {
               appendExistsCriteria((ExistsCriteria) c, buf);
           }
  +        else if (c instanceof LikeCriteria)
  +        {
  +            appendLikeCriteria(alias,pathInfo,(LikeCriteria) c, buf);
  +        }
           else
           {
               appendSelectionCriteria(alias,pathInfo,c,buf);
  @@ -875,7 +901,7 @@
           }
           else
           {
  -            buf.append(" ? ");
  +            buf.append("?");
           }
       }
   
  
  
  
  1.51      +66 -1     db-ojb/src/test/org/apache/ojb/broker/QueryTest.java
  
  Index: QueryTest.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/broker/QueryTest.java,v
  retrieving revision 1.50
  retrieving revision 1.51
  diff -u -r1.50 -r1.51
  --- QueryTest.java	14 Feb 2004 19:45:25 -0000	1.50
  +++ QueryTest.java	21 Feb 2004 21:41:45 -0000	1.51
  @@ -13,6 +13,7 @@
   
   import org.apache.ojb.broker.metadata.ClassDescriptor;
   import org.apache.ojb.broker.query.Criteria;
  +import org.apache.ojb.broker.query.LikeCriteria;
   import org.apache.ojb.broker.query.Query;
   import org.apache.ojb.broker.query.QueryByCriteria;
   import org.apache.ojb.broker.query.QueryFactory;
  @@ -253,6 +254,44 @@
       }
   
       /**
  +     * test escaped Like Criteria
  +     */
  +    public void testLikeEscapedCriteria1()
  +    {
  +        Criteria crit = new Criteria();
  +        crit.addLike("firstname", "h%\\%");
  +        Query q = QueryFactory.newQuery(Person.class, crit);
  +
  +        Collection results = broker.getCollectionByQuery(q);
  +        assertNotNull(results);
  +        assertEquals(results.size(), 1);
  +        
  +        // compare with count
  +        int count = broker.getCount(q);
  +        assertEquals(results.size(), count);
  +    }
  +
  +    /**
  +     * test escaped Like Criteria with escape character
  +     */
  +    public void testLikeEscapedCriteria2()
  +    {
  +        LikeCriteria.setEscapeCharacter('|');
  +        
  +        Criteria crit = new Criteria();
  +        crit.addLike("firstname", "h%|%");
  +        Query q = QueryFactory.newQuery(Person.class, crit);
  +
  +        Collection results = broker.getCollectionByQuery(q);
  +        assertNotNull(results);
  +        assertEquals(results.size(), 1);
  +        
  +        // compare with count
  +        int count = broker.getCount(q);
  +        assertEquals(results.size(), count);
  +    }
  +
  +    /**
   	 * test Null Criteria
   	 */
       public void testNullCriteria()
  @@ -416,7 +455,7 @@
           // compare with count
           int count = broker.getCount(q);
           
  -        // assertEquals(results.size(), count); FAILS
  +        assertEquals(results.size(), count); // FAILS
       }
       
       /**
  @@ -536,6 +575,32 @@
   
       }
   
  +    /**
  +     * ReportQuery with pathExpression in columns
  +     */
  +    public void testReportQueryPathExpression()
  +    {
  +        Criteria crit = new Criteria();
  +        Collection results = new Vector();
  +        crit.addLike("articleName", "C%");
  +        ReportQueryByCriteria q = QueryFactory.newReportQuery(Article.class, crit);
  +        q.setColumns(new String[] { "productGroup.groupName","articleId", "articleName",
"price" });
  +
  +        Iterator iter = broker.getReportQueryIteratorByQuery(q);
  +
  +        assertNotNull(iter);
  +        while (iter.hasNext())
  +        {
  +            results.add(iter.next());
  +        }
  +        assertEquals(results.size(), 9);
  +
  +        // compare with count
  +        int count = broker.getCount(q);
  +//        assertEquals(results.size(), count);  
  +        
  +    }
  +    
       /**
   	 * ReportQuery returning rows with some "Liquor" data ordered by price
   	 */
  
  
  

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