db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From b..@apache.org
Subject svn commit: r369195 - in /db/ojb/branches/OJB_1_0_RELEASE/src: java/org/apache/ojb/broker/query/ java/org/apache/ojb/odmg/oql/ test/org/apache/ojb/odmg/
Date Sun, 15 Jan 2006 10:05:02 GMT
Author: brj
Date: Sun Jan 15 02:04:50 2006
New Revision: 369195

URL: http://svn.apache.org/viewcvs?rev=369195&view=rev
Log:
improved OQL-support for InCriteria

Modified:
    db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/query/BetweenCriteria.java
    db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/query/BindableCriterion.java
    db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/query/InCriteria.java
    db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/query/SelectionCriteria.java
    db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/odmg/oql/OQLLexer.java
    db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/odmg/oql/OQLParser.java
    db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/odmg/oql/OQLQueryImpl.java
    db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/odmg/oql/oql-ojb.g
    db/ojb/branches/OJB_1_0_RELEASE/src/test/org/apache/ojb/odmg/OQLTest.java

Modified: db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/query/BetweenCriteria.java
URL: http://svn.apache.org/viewcvs/db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/query/BetweenCriteria.java?rev=369195&r1=369194&r2=369195&view=diff
==============================================================================
--- db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/query/BetweenCriteria.java
(original)
+++ db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/query/BetweenCriteria.java
Sun Jan 15 02:04:50 2006
@@ -53,6 +53,13 @@
     	}
     }
 
+    /**
+     * @see org.apache.ojb.broker.query.BindableCriterion#isMultiBindable()
+     */
+    public boolean isMultiBindable()
+    {
+        return true;
+    }
 
 	/**
 	 * Gets the value2.

Modified: db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/query/BindableCriterion.java
URL: http://svn.apache.org/viewcvs/db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/query/BindableCriterion.java?rev=369195&r1=369194&r2=369195&view=diff
==============================================================================
--- db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/query/BindableCriterion.java
(original)
+++ db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/query/BindableCriterion.java
Sun Jan 15 02:04:50 2006
@@ -35,5 +35,11 @@
      */
     public void bind(Object newValue);
 
+    /**
+     * Indicates whether the Criteria can be bound more than once. 
+     * (ie. BetweenCriteria with 2 Parameters)
+     * @return true if multi bindable
+     */
+    public boolean isMultiBindable();
 }
 

Modified: db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/query/InCriteria.java
URL: http://svn.apache.org/viewcvs/db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/query/InCriteria.java?rev=369195&r1=369194&r2=369195&view=diff
==============================================================================
--- db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/query/InCriteria.java (original)
+++ db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/query/InCriteria.java Sun
Jan 15 02:04:50 2006
@@ -68,7 +68,7 @@
 	}
 	
 	private Object m_attribute;
-	private Collection m_values;
+	private List m_values;
 	private boolean m_translateAttribute;
 	private boolean m_preprocessed = false;
 
@@ -83,7 +83,7 @@
     InCriteria(Object anAttribute, Collection values, boolean negative, boolean translate)
     {
         m_attribute = anAttribute;
-        m_values = values;
+        setValues(values);
         m_translateAttribute = translate;
         setNegative(negative);
     }
@@ -108,15 +108,16 @@
         List result = new ArrayList();
         Collection inCollection = new ArrayList();
         int inLimit = getSqlInLimit(aPb);
+        List values = getValues();
        
-        if (m_values == null || m_values.isEmpty())
+        if (values == null || values.isEmpty())
         {
             // OQL creates empty Criteria for late binding
-            result.add(buildInCriterion(m_values));
+            result.add(buildInCriterion(values));
         }
         else
         {
-            Iterator iter = m_values.iterator();
+            Iterator iter = values.iterator();
 
             while (iter.hasNext())
             {
@@ -150,6 +151,30 @@
     }
 
     /**
+     * @return the values
+     */
+    private List getValues()
+    {
+        return m_values;
+    }
+
+    /**
+     * @param values the values to set
+     */
+    private void setValues(Collection values)
+    {
+        List newValues = null;
+        
+        if (values != null)
+        {
+            newValues = new ArrayList(values.size());
+            newValues.addAll(values);
+        }
+        
+        m_values = newValues;
+    }
+
+    /**
      * Preprocess the Criteria using a PersistenceBroker.
      * Build list of InCriterion based on SQL-IN-LIMIT.
      * 
@@ -174,16 +199,46 @@
     }
 
     /**
-     * sets the value of the criteria to newValue. Used by the ODMG OQLQuery.bind() operation
+     * @see org.apache.ojb.broker.query.BindableCriterion#bind(java.lang.Object)
      */
     public void bind(Object newValue)
     {
-        m_values = (Collection) newValue;
+        if (newValue instanceof Collection)
+        {
+            setValues((Collection) newValue);
+        }
+        else
+        {
+            bindSingleObject(newValue);
+        }    
     }
 
+    /**
+     * Bind a single Object to the next null position.
+     * @param obj
+     */
+    private void bindSingleObject(Object obj)
+    {
+        int idx = getValues().indexOf(null);
+        
+        getValues().set(idx, obj);
+    }
+    
+    /**
+     * @see org.apache.ojb.broker.query.BindableCriterion#isBound()
+     * @return true if the collection does not contain nulls
+     */
     public boolean isBound()
     {
-        return (m_values != null);
+       return !getValues().contains(null);
+    }
+
+    /**
+     * @see org.apache.ojb.broker.query.BindableCriterion#isMultiBindable()
+     */
+    public boolean isMultiBindable()
+    {
+        return true;
     }
     
     /**

Modified: db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/query/SelectionCriteria.java
URL: http://svn.apache.org/viewcvs/db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/query/SelectionCriteria.java?rev=369195&r1=369194&r2=369195&view=diff
==============================================================================
--- db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/query/SelectionCriteria.java
(original)
+++ db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/query/SelectionCriteria.java
Sun Jan 15 02:04:50 2006
@@ -97,6 +97,14 @@
 		setBound(true);
 	}
 
+    /**
+     * @see org.apache.ojb.broker.query.BindableCriterion#isMultiBindable()
+     */
+    public boolean isMultiBindable()
+    {
+        return false;
+    }
+
 	/**
 	 * Answer the value
 	 */

Modified: db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/odmg/oql/OQLLexer.java
URL: http://svn.apache.org/viewcvs/db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/odmg/oql/OQLLexer.java?rev=369195&r1=369194&r2=369195&view=diff
==============================================================================
--- db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/odmg/oql/OQLLexer.java (original)
+++ db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/odmg/oql/OQLLexer.java Sun Jan
15 02:04:50 2006
@@ -19,24 +19,28 @@
 
 
 import java.io.InputStream;
+import antlr.TokenStreamException;
+import antlr.TokenStreamIOException;
+import antlr.TokenStreamRecognitionException;
+import antlr.CharStreamException;
+import antlr.CharStreamIOException;
+import antlr.ANTLRException;
 import java.io.Reader;
 import java.util.Hashtable;
-
-import antlr.ANTLRHashString;
+import antlr.CharScanner;
+import antlr.InputBuffer;
 import antlr.ByteBuffer;
 import antlr.CharBuffer;
-import antlr.CharStreamException;
-import antlr.CharStreamIOException;
-import antlr.InputBuffer;
-import antlr.LexerSharedInputState;
-import antlr.NoViableAltForCharException;
-import antlr.RecognitionException;
 import antlr.Token;
+import antlr.CommonToken;
+import antlr.RecognitionException;
+import antlr.NoViableAltForCharException;
+import antlr.MismatchedCharException;
 import antlr.TokenStream;
-import antlr.TokenStreamException;
-import antlr.TokenStreamIOException;
-import antlr.TokenStreamRecognitionException;
+import antlr.ANTLRHashString;
+import antlr.LexerSharedInputState;
 import antlr.collections.impl.BitSet;
+import antlr.SemanticException;
 
 /**
  * This OQL grammar has been derived from a OQL sample grammar from the ODMG
@@ -266,7 +270,7 @@
 					}
 				else {
 					if (LA(1)==EOF_CHAR) {uponEOF(); _returnToken = makeToken(Token.EOF_TYPE);}
-				else {throw new NoViableAltForCharException(LA(1), getFilename(), getLine(), getColumn());}
+				else {throw new NoViableAltForCharException((char)LA(1), getFilename(), getLine(), getColumn());}
 				}
 				}
 				if ( _returnToken==null ) continue tryAgain; // found SKIP token
@@ -633,7 +637,7 @@
 		}
 		default:
 		{
-			throw new NoViableAltForCharException(LA(1), getFilename(), getLine(), getColumn());
+			throw new NoViableAltForCharException((char)LA(1), getFilename(), getLine(), getColumn());
 		}
 		}
 		}
@@ -678,7 +682,7 @@
 		}
 		default:
 		{
-			throw new NoViableAltForCharException(LA(1), getFilename(), getLine(), getColumn());
+			throw new NoViableAltForCharException((char)LA(1), getFilename(), getLine(), getColumn());
 		}
 		}
 		}
@@ -754,7 +758,7 @@
 		}
 		default:
 		{
-			throw new NoViableAltForCharException(LA(1), getFilename(), getLine(), getColumn());
+			throw new NoViableAltForCharException((char)LA(1), getFilename(), getLine(), getColumn());
 		}
 		}
 		}
@@ -766,7 +770,7 @@
 				matchRange('0','9');
 			}
 			else {
-				if ( _cnt34>=1 ) { break _loop34; } else {throw new NoViableAltForCharException(LA(1),
getFilename(), getLine(), getColumn());}
+				if ( _cnt34>=1 ) { break _loop34; } else {throw new NoViableAltForCharException((char)LA(1),
getFilename(), getLine(), getColumn());}
 			}
 			
 			_cnt34++;
@@ -796,7 +800,7 @@
 					mTOK_UNSIGNED_INTEGER(false);
 				}
 				else {
-					if ( _cnt37>=1 ) { break _loop37; } else {throw new NoViableAltForCharException(LA(1),
getFilename(), getLine(), getColumn());}
+					if ( _cnt37>=1 ) { break _loop37; } else {throw new NoViableAltForCharException((char)LA(1),
getFilename(), getLine(), getColumn());}
 				}
 				
 				_cnt37++;
@@ -826,7 +830,7 @@
 					mTOK_UNSIGNED_INTEGER(false);
 				}
 				else {
-					if ( _cnt40>=1 ) { break _loop40; } else {throw new NoViableAltForCharException(LA(1),
getFilename(), getLine(), getColumn());}
+					if ( _cnt40>=1 ) { break _loop40; } else {throw new NoViableAltForCharException((char)LA(1),
getFilename(), getLine(), getColumn());}
 				}
 				
 				_cnt40++;
@@ -877,7 +881,7 @@
 		}
 		default:
 		{
-			throw new NoViableAltForCharException(LA(1), getFilename(), getLine(), getColumn());
+			throw new NoViableAltForCharException((char)LA(1), getFilename(), getLine(), getColumn());
 		}
 		}
 		if ( _createToken && _token==null && _ttype!=Token.SKIP ) {
@@ -993,7 +997,7 @@
 		}
 		default:
 		{
-			throw new NoViableAltForCharException(LA(1), getFilename(), getLine(), getColumn());
+			throw new NoViableAltForCharException((char)LA(1), getFilename(), getLine(), getColumn());
 		}
 		}
 		}

Modified: db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/odmg/oql/OQLParser.java
URL: http://svn.apache.org/viewcvs/db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/odmg/oql/OQLParser.java?rev=369195&r1=369194&r2=369195&view=diff
==============================================================================
--- db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/odmg/oql/OQLParser.java (original)
+++ db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/odmg/oql/OQLParser.java Sun Jan
15 02:04:50 2006
@@ -18,6 +18,20 @@
 package org.apache.ojb.odmg.oql;
 
 
+import antlr.TokenBuffer;
+import antlr.TokenStreamException;
+import antlr.TokenStreamIOException;
+import antlr.ANTLRException;
+import antlr.LLkParser;
+import antlr.Token;
+import antlr.TokenStream;
+import antlr.RecognitionException;
+import antlr.NoViableAltException;
+import antlr.MismatchedTokenException;
+import antlr.SemanticException;
+import antlr.ParserSharedInputState;
+import antlr.collections.impl.BitSet;
+
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Vector;
@@ -25,20 +39,11 @@
 import org.apache.ojb.broker.metadata.ClassDescriptor;
 import org.apache.ojb.broker.metadata.MetadataManager;
 import org.apache.ojb.broker.metadata.ObjectReferenceDescriptor;
+import org.apache.ojb.broker.util.ClassHelper;
 import org.apache.ojb.broker.query.Criteria;
 import org.apache.ojb.broker.query.Query;
 import org.apache.ojb.broker.query.QueryByCriteria;
 import org.apache.ojb.broker.query.QueryFactory;
-import org.apache.ojb.broker.util.ClassHelper;
-
-import antlr.NoViableAltException;
-import antlr.ParserSharedInputState;
-import antlr.RecognitionException;
-import antlr.Token;
-import antlr.TokenBuffer;
-import antlr.TokenStream;
-import antlr.TokenStreamException;
-import antlr.collections.impl.BitSet;
 
 public class OQLParser extends antlr.LLkParser       implements OQLLexerTokenTypes
  {
@@ -1089,26 +1094,23 @@
 	}
 	
 	public final Collection  argList() throws RecognitionException, TokenStreamException {
-		Collection coll = null;
+		Collection coll = new ArrayList();
 		
 		
 		try {      // for error handling
-			
-					Collection temp = new Vector();
-				    Object val;
-				
+			Object val;
 			match(TOK_LPAREN);
 			{
 			if ((_tokenSet_16.member(LA(1)))) {
 				val=literal();
-				if (val != null) {temp.add(val);}
+				coll.add(val);
 				{
 				_loop124:
 				do {
 					if ((LA(1)==TOK_COMMA)) {
 						match(TOK_COMMA);
 						val=literal();
-						if (val != null) {temp.add(val);}
+						coll.add(val);
 					}
 					else {
 						break _loop124;
@@ -1125,9 +1127,6 @@
 			
 			}
 			match(TOK_RPAREN);
-			
-					if (!temp.isEmpty()) {coll = temp;}
-					
 		}
 		catch (RecognitionException ex) {
 			reportError(ex);

Modified: db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/odmg/oql/OQLQueryImpl.java
URL: http://svn.apache.org/viewcvs/db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/odmg/oql/OQLQueryImpl.java?rev=369195&r1=369194&r2=369195&view=diff
==============================================================================
--- db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/odmg/oql/OQLQueryImpl.java (original)
+++ db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/odmg/oql/OQLQueryImpl.java Sun
Jan 15 02:04:50 2006
@@ -21,14 +21,11 @@
 import java.util.ListIterator;
 import java.util.Vector;
 
-import org.apache.commons.lang.SystemUtils;
-import org.apache.commons.lang.exception.ExceptionUtils;
 import org.apache.ojb.broker.ManageableCollection;
 import org.apache.ojb.broker.PBKey;
 import org.apache.ojb.broker.PersistenceBroker;
 import org.apache.ojb.broker.PersistenceBrokerFactory;
 import org.apache.ojb.broker.accesslayer.OJBIterator;
-import org.apache.ojb.broker.query.BetweenCriteria;
 import org.apache.ojb.broker.query.BindableCriterion;
 import org.apache.ojb.broker.query.Criteria;
 import org.apache.ojb.broker.query.Query;
@@ -127,17 +124,16 @@
             BindableCriterion crit = (BindableCriterion) getBindIterator().next();
             crit.bind(parameter);
 
-            // BRJ: bind is called twice for between
-            if (crit instanceof BetweenCriteria && !crit.isBound())
+            // BRJ: some criteria can be bound more than once, ie. BetweenCiriteria, InCriteria
+            if (crit.isMultiBindable() && !crit.isBound())
             {
                 getBindIterator().previous();
             }
         }
         catch (Exception e)
         {
-            Throwable t = ExceptionUtils.getRootCause(e);
-            if(t == null) t = e;
-            throw new QueryParameterCountInvalidException("Root exception is:" + SystemUtils.LINE_SEPARATOR
+ ExceptionUtils.getFullStackTrace(t));
+            log.error("Can't bind parameter: " + parameter, e);
+            throw new QueryParameterCountInvalidException(e.getMessage());
         }
     }
 

Modified: db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/odmg/oql/oql-ojb.g
URL: http://svn.apache.org/viewcvs/db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/odmg/oql/oql-ojb.g?rev=369195&r1=369194&r2=369195&view=diff
==============================================================================
--- db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/odmg/oql/oql-ojb.g (original)
+++ db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/odmg/oql/oql-ojb.g Sun Jan 15
02:04:50 2006
@@ -828,28 +828,21 @@
             { value = java.sql.Timestamp.valueOf(tokTs.getText()); }
     ;
 
-argList returns [Collection coll = null] :
-// return a collection of parameters or null if we only have $1,$2...
-	    {
-		Collection temp = new Vector();
-	    Object val;
-	    }
+argList returns [Collection coll = new ArrayList()] :
+// BRJ: return a collection of parameters, or a collection of nulls if we only have $1,$2...
+
+	    { Object val; }
 
         TOK_LPAREN
         (
             val = literal
 
-            // BRJ: do not add null objects
-            {if (val != null) {temp.add(val);} }
+            { coll.add(val); }
             (
                 TOK_COMMA
 	            val = literal
-	            {if (val != null) {temp.add(val);} }
+	            { coll.add(val); }
             )*
         )?
         TOK_RPAREN
-
-		{
-		if (!temp.isEmpty()) {coll = temp;}
-		}
     ;

Modified: db/ojb/branches/OJB_1_0_RELEASE/src/test/org/apache/ojb/odmg/OQLTest.java
URL: http://svn.apache.org/viewcvs/db/ojb/branches/OJB_1_0_RELEASE/src/test/org/apache/ojb/odmg/OQLTest.java?rev=369195&r1=369194&r2=369195&view=diff
==============================================================================
--- db/ojb/branches/OJB_1_0_RELEASE/src/test/org/apache/ojb/odmg/OQLTest.java (original)
+++ db/ojb/branches/OJB_1_0_RELEASE/src/test/org/apache/ojb/odmg/OQLTest.java Sun Jan 15 02:04:50
2006
@@ -203,6 +203,28 @@
 		tx.commit();
 	}
 
+    public void testQueryIn_2() throws Exception
+    {
+        // deleteData(database, odmg, Article.class);
+
+        Transaction tx = odmg.newTransaction();
+        tx.begin();
+
+        OQLQuery query1 = odmg.newOQLQuery();
+        query1.create("select anArticle from " + Article.class.getName() + " where articleId
in(30, 31, 32) order by articleId");
+        List result1 = (List) query1.execute();
+
+
+        OQLQuery query2 = odmg.newOQLQuery();
+        query2.create("select anArticle from " + Article.class.getName() + " where articleId
in($1, $2, $3) order by articleId");
+        query2.bind(new Integer(30));
+        query2.bind(new Integer(31));
+        query2.bind(new Integer(32));
+        List result2 = (List) query2.execute();
+
+        assertEquals(result1.size(), result2.size());
+        tx.commit();
+    }
 	public void testQueryNull() throws Exception
 	{
 		Transaction tx = odmg.newTransaction();



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