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 ol...@apache.org on 2003/01/05 15:19:56 UTC
cvs commit: jakarta-ojb/src/test/org/apache/ojb/odmg ProjectionAttributeTest.java
olegnitz 2003/01/05 06:19:56
Modified: src/java/org/apache/ojb/broker/query AbstractQueryImpl.java
Query.java
src/java/org/apache/ojb/broker/singlevm
PersistenceBrokerImpl.java
src/java/org/apache/ojb/odmg/oql OQLParser.java
OQLQueryImpl.java oql-ojb.g
src/test/org/apache/ojb/odmg ProjectionAttributeTest.java
Log:
Changed the implementation of OQL queries with attributes projections
Revision Changes Path
1.4 +0 -10 jakarta-ojb/src/java/org/apache/ojb/broker/query/AbstractQueryImpl.java
Index: AbstractQueryImpl.java
===================================================================
RCS file: /home/cvs/jakarta-ojb/src/java/org/apache/ojb/broker/query/AbstractQueryImpl.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- AbstractQueryImpl.java 1 Aug 2002 09:45:39 -0000 1.3
+++ AbstractQueryImpl.java 5 Jan 2003 14:19:55 -0000 1.4
@@ -61,7 +61,6 @@
private int m_startAtIndex = Query.NO_START_AT_INDEX;
private int m_endAtIndex = Query.NO_END_AT_INDEX;
private int m_fullSize = 0;
- private String m_requestedAttribute;
public int getStartAtIndex()
{
return m_startAtIndex;
@@ -88,13 +87,4 @@
return m_fullSize;
}
- public String getRequestedAttribute()
- {
- return m_requestedAttribute;
- }
-
- public void setRequestedAttribute(String attr)
- {
- m_requestedAttribute = attr;
- }
}
1.5 +0 -3 jakarta-ojb/src/java/org/apache/ojb/broker/query/Query.java
Index: Query.java
===================================================================
RCS file: /home/cvs/jakarta-ojb/src/java/org/apache/ojb/broker/query/Query.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- Query.java 8 Oct 2002 06:12:08 -0000 1.4
+++ Query.java 5 Jan 2003 14:19:55 -0000 1.5
@@ -134,7 +134,4 @@
void fullSize(int size);
int fullSize();
- String getRequestedAttribute();
- void setRequestedAttribute(String attr);
-
}
1.105 +3 -52 jakarta-ojb/src/java/org/apache/ojb/broker/singlevm/PersistenceBrokerImpl.java
Index: PersistenceBrokerImpl.java
===================================================================
RCS file: /home/cvs/jakarta-ojb/src/java/org/apache/ojb/broker/singlevm/PersistenceBrokerImpl.java,v
retrieving revision 1.104
retrieving revision 1.105
diff -u -r1.104 -r1.105
--- PersistenceBrokerImpl.java 3 Jan 2003 21:37:25 -0000 1.104
+++ PersistenceBrokerImpl.java 5 Jan 2003 14:19:55 -0000 1.105
@@ -1262,57 +1262,8 @@
|| (Proxy.isProxyClass(candidate.getClass()))
|| (candidate instanceof VirtualProxy))
{
- boolean added = false;
- if ((query.getRequestedAttribute() != null) && (hasProjectionAttribute))
- {
- /**
- * someone requested a nested attribute, say from an OQL statement like:
- * select x.y.z.pkField from com.someclass, so we need to walk the path and
- * get the attribute as requested.
- */
- int indexOfDot = query.getRequestedAttribute().indexOf(".");
- if (indexOfDot != -1)
- {
- String attr = query.getRequestedAttribute().substring(indexOfDot + 1);
- try
- {
- /**
- * this will fail if the attribute requested is invalid.
- */
- Object projectedAttribute = BeanUtils.getNestedProperty(candidate, attr);
- /**
- * MBAIRD:
- * When we are getting projection attributes, we need to use a collections
- * class that doesn't try to get an identity or lock the object, since there
- * is a chance it is not persistence capable.
- */
- if (!descriptorRepository.hasDescriptorFor(projectedAttribute.getClass())
- && (!(result instanceof ManageableArrayList)))
- {
- result = new ManageableArrayList();
- }
- result.ojbAdd(projectedAttribute);
- retrievedCount++;
- added = true;
- }
- catch (Exception e)
- {
- /**
- * probably an invalid projection attribute, so let's just ignore it.
- */
- hasProjectionAttribute = false;
- }
- }
- else
- hasProjectionAttribute = false;
- }
- if (!added)
- {
- if (result == null)
- result = (ManageableCollection) collectionClass.newInstance();
- result.ojbAdd(candidate);
- retrievedCount++;
- }
+ result.ojbAdd(candidate);
+ retrievedCount++;
}
}
}
1.8 +57 -18 jakarta-ojb/src/java/org/apache/ojb/odmg/oql/OQLParser.java
Index: OQLParser.java
===================================================================
RCS file: /home/cvs/jakarta-ojb/src/java/org/apache/ojb/odmg/oql/OQLParser.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- OQLParser.java 3 Nov 2002 13:38:36 -0000 1.7
+++ OQLParser.java 5 Jan 2003 14:19:55 -0000 1.8
@@ -121,12 +121,16 @@
Class clazz = null;
Criteria criteria = new Criteria();
- String projectionAttrs;
+ String[] projectionAttrs;
+ boolean distinct = false;
match(LITERAL_select);
{
if ((LA(1)==LITERAL_distinct)) {
match(LITERAL_distinct);
+
+ distinct = true;
+
}
else if ((LA(1)==TOK_STAR||LA(1)==Identifier)) {
}
@@ -165,13 +169,21 @@
}
if (clazz != null) {
+ if (projectionAttrs[0].indexOf('.') < 0)
+ {
query = QueryFactory.newQuery(clazz, criteria);
}
- if ((query != null) && (projectionAttrs != null) && (projectionAttrs.indexOf(".") > 0))
+ else
{
- query.setRequestedAttribute(projectionAttrs);
+ for (int i = 0; i < projectionAttrs.length; i++) {
+ projectionAttrs[i] = projectionAttrs[i].substring(
+ projectionAttrs[i].indexOf('.') + 1);
}
-
+ query = QueryFactory.newReportQuery(clazz, projectionAttrs,
+ criteria, distinct);
+ }
+ }
+
}
catch (RecognitionException ex) {
reportError(ex);
@@ -181,30 +193,46 @@
return query;
}
- public final String projectionAttributes() throws RecognitionException, TokenStreamException {
- String projectionAttrs = null;
+ public final String[] projectionAttributes() throws RecognitionException, TokenStreamException {
+ String[] projectionAttrs = null;
Token id = null;
+ Token id1 = null;
try { // for error handling
+
+ String first = null;
+ ArrayList list = null;
+
{
if ((LA(1)==Identifier)) {
id = LT(1);
match(Identifier);
{
- projectionAttrs = id.getText();
-
+ first = id.getText();
+
}
{
- _loop77:
+ _loop78:
do {
if ((LA(1)==TOK_COMMA)) {
match(TOK_COMMA);
+ id1 = LT(1);
match(Identifier);
+ {
+
+ if (list == null)
+ {
+ list = new ArrayList();
+ list.add(first);
+ }
+ list.add(id1.getText());
+
+ }
}
else {
- break _loop77;
+ break _loop78;
}
} while (true);
@@ -218,6 +246,17 @@
}
}
+
+ if (list == null)
+ {
+ projectionAttrs = new String[] {first};
+ }
+ else
+ {
+ projectionAttrs = (String[]) list.toArray(new String[list.size()]);
+ }
+
+
}
catch (RecognitionException ex) {
reportError(ex);
@@ -274,14 +313,14 @@
try { // for error handling
sortCriterion(criteria);
{
- _loop80:
+ _loop81:
do {
if ((LA(1)==TOK_COMMA)) {
match(TOK_COMMA);
sortCriterion(criteria);
}
else {
- break _loop80;
+ break _loop81;
}
} while (true);
@@ -341,7 +380,7 @@
try { // for error handling
andExpr(criteria);
{
- _loop86:
+ _loop87:
do {
if ((LA(1)==LITERAL_or) && (_tokenSet_6.member(LA(2))) && (_tokenSet_7.member(LA(3)))) {
match(LITERAL_or);
@@ -350,7 +389,7 @@
criteria.addOrCriteria(orCriteria);
}
else {
- break _loop86;
+ break _loop87;
}
} while (true);
@@ -420,7 +459,7 @@
try { // for error handling
quantifierExpr(criteria);
{
- _loop89:
+ _loop90:
do {
if ((LA(1)==LITERAL_and) && (_tokenSet_6.member(LA(2))) && (_tokenSet_7.member(LA(3)))) {
match(LITERAL_and);
@@ -429,7 +468,7 @@
criteria.addAndCriteria(andCriteria);
}
else {
- break _loop89;
+ break _loop90;
}
} while (true);
@@ -960,7 +999,7 @@
val=literal();
if (val != null) {temp.add(val);}
{
- _loop112:
+ _loop113:
do {
if ((LA(1)==TOK_COMMA)) {
match(TOK_COMMA);
@@ -968,7 +1007,7 @@
if (val != null) {temp.add(val);}
}
else {
- break _loop112;
+ break _loop113;
}
} while (true);
1.11 +50 -4 jakarta-ojb/src/java/org/apache/ojb/odmg/oql/OQLQueryImpl.java
Index: OQLQueryImpl.java
===================================================================
RCS file: /home/cvs/jakarta-ojb/src/java/org/apache/ojb/odmg/oql/OQLQueryImpl.java,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -r1.10 -r1.11
--- OQLQueryImpl.java 24 Dec 2002 13:34:49 -0000 1.10
+++ OQLQueryImpl.java 5 Jan 2003 14:19:55 -0000 1.11
@@ -64,10 +64,13 @@
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.Criteria;
import org.apache.ojb.broker.query.Query;
+import org.apache.ojb.broker.query.ReportQuery;
import org.apache.ojb.broker.query.SelectionCriteria;
+import org.apache.ojb.broker.util.collections.ManageableArrayList;
import org.apache.ojb.broker.util.configuration.Configurable;
import org.apache.ojb.broker.util.configuration.Configuration;
import org.apache.ojb.broker.util.configuration.ConfigurationException;
@@ -272,10 +275,53 @@
// ask the broker to perfom the query.
// the concrete result type is configurable
- ManageableCollection result =
- (ManageableCollection) broker.getCollectionByQuery(this.getCollectionClass(), query);
+ ManageableCollection result;
- performLockingIfRequired(tx, broker, result);
+ if (!(query instanceof ReportQuery))
+ {
+ result = (ManageableCollection) broker.getCollectionByQuery(this.getCollectionClass(), query);
+ performLockingIfRequired(tx, broker, result);
+ }
+ else
+ {
+ Iterator iter = null;
+ result = new ManageableArrayList();
+ iter = broker.getReportQueryIteratorByQuery(query);
+ try
+ {
+ while (iter.hasNext())
+ {
+ Object[] res = (Object[]) iter.next();
+
+ if (res.length == 1)
+ {
+ if (res[0] != null) // skip null values
+ {
+ result.ojbAdd(res[0]);
+ }
+ }
+ else
+ {
+ // skip null tuples
+ for (int i = 0; i < res.length; i++)
+ {
+ if (res[i] != null)
+ {
+ result.ojbAdd(res);
+ break;
+ }
+ }
+ }
+ }
+ }
+ finally
+ {
+ if (iter instanceof OJBIterator)
+ {
+ ((OJBIterator) iter).releaseDbResources();
+ }
+ }
+ }
capsule.destroy();
return result;
@@ -371,4 +417,4 @@
{
return this.query.fullSize();
}
-}
\ No newline at end of file
+}
1.9 +60 -23 jakarta-ojb/src/java/org/apache/ojb/odmg/oql/oql-ojb.g
Index: oql-ojb.g
===================================================================
RCS file: /home/cvs/jakarta-ojb/src/java/org/apache/ojb/odmg/oql/oql-ojb.g,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- oql-ojb.g 3 Nov 2002 13:38:36 -0000 1.8
+++ oql-ojb.g 5 Jan 2003 14:19:55 -0000 1.9
@@ -357,21 +357,23 @@
{
Class clazz = null;
Criteria criteria = new Criteria();
- String projectionAttrs;
+ String[] projectionAttrs;
+ boolean distinct = false;
}
"select"
- // "distinct" is effectively ignored as we ALWAYS select distinct
(
options {
warnWhenFollowAmbig = false;
} :
"distinct"
+ {
+ distinct = true;
+ }
)?
- // We also ignore the projection
projectionAttrs = projectionAttributes
"from" clazz = fromClause
@@ -379,13 +381,21 @@
( "order" "by" orderClause[criteria] )?
{
if (clazz != null) {
- query = QueryFactory.newQuery(clazz, criteria);
- }
- if ((query != null) && (projectionAttrs != null) && (projectionAttrs.indexOf(".") > 0))
- {
- query.setRequestedAttribute(projectionAttrs);
+ if (projectionAttrs[0].indexOf('.') < 0)
+ {
+ query = QueryFactory.newQuery(clazz, criteria);
+ }
+ else
+ {
+ for (int i = 0; i < projectionAttrs.length; i++) {
+ projectionAttrs[i] = projectionAttrs[i].substring(
+ projectionAttrs[i].indexOf('.') + 1);
+ }
+ query = QueryFactory.newReportQuery(clazz, projectionAttrs,
+ criteria, distinct);
+ }
}
- }
+ }
;
existsQuery returns [Query query = null] :
@@ -426,20 +436,47 @@
orExpr[criteria]
;
-projectionAttributes returns [String projectionAttrs = null] :
- (
- id:Identifier
- (
- {
- projectionAttrs = id.getText();
- }
- )
- (
- TOK_COMMA
- Identifier
- )*
+projectionAttributes returns [String[] projectionAttrs = null] :
+ {
+ String first = null;
+ ArrayList list = null;
+ }
+
+ (
+ id:Identifier
+ (
+ {
+ first = id.getText();
+ }
+ )
+ (
+ TOK_COMMA
+ id1:Identifier
+ (
+ {
+ if (list == null)
+ {
+ list = new ArrayList();
+ list.add(first);
+ }
+ list.add(id1.getText());
+ }
+ )
+ )*
| TOK_STAR
- )
+ )
+
+ {
+ if (list == null)
+ {
+ projectionAttrs = new String[] {first};
+ }
+ else
+ {
+ projectionAttrs = (String[]) list.toArray(new String[list.size()]);
+ }
+
+ }
;
orderClause[Criteria criteria] :
@@ -741,4 +778,4 @@
{
if (!temp.isEmpty()) {coll = temp;}
}
- ;
\ No newline at end of file
+ ;
1.4 +15 -7 jakarta-ojb/src/test/org/apache/ojb/odmg/ProjectionAttributeTest.java
Index: ProjectionAttributeTest.java
===================================================================
RCS file: /home/cvs/jakarta-ojb/src/test/org/apache/ojb/odmg/ProjectionAttributeTest.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- ProjectionAttributeTest.java 24 Dec 2002 14:07:40 -0000 1.3
+++ ProjectionAttributeTest.java 5 Jan 2003 14:19:56 -0000 1.4
@@ -8,6 +8,7 @@
*/
package org.apache.ojb.odmg;
+import java.util.List;
import junit.framework.TestCase;
import org.apache.ojb.broker.ManageableCollection;
import org.apache.ojb.broker.PersistenceBrokerFactory;
@@ -89,24 +90,31 @@
tx.begin();
OQLQuery query = odmg.newOQLQuery();
- String sql = "select allPersons.firstname from " + org.apache.ojb.broker.Person.class.getName();
+ String sql = "select aPerson.firstname, aPerson.lastname from " + org.apache.ojb.broker.Person.class.getName();
query.create(sql);
- ManageableCollection allPersons = (ManageableCollection) query.execute();
+ ManageableCollection result = (ManageableCollection) query.execute();
// Iterator over the restricted articles objects
- java.util.Iterator it = allPersons.ojbIterator();
+ java.util.Iterator it = result.ojbIterator();
int i = 0;
while (it.hasNext())
{
- /**
- * just make sure it's a string.
- */
- String value = (String) it.next();
+ Object[] res = (Object[]) it.next();
+ String firstname = (String) res[0];
+ String lastname = (String) res[1];
i++;
}
if (i < COUNT)
fail("Should have found at least " + COUNT + " items");
+
+ OQLQuery query1 = odmg.newOQLQuery();
+ query1.create("select distinct anArticle.productGroup.groupId from " + Article.class.getName());
+ List result1 = (List) query1.execute();
+ for (it = result1.iterator(); it.hasNext(); )
+ {
+ Integer value = (Integer) it.next();
+ }
tx.commit();
}
catch (Throwable t)