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/08/02 10:27:22 UTC
cvs commit: db-ojb/src/test/org/apache/ojb/odmg ContractVersionEffectivenessOQLTest.java
olegnitz 2003/08/02 01:27:22
Modified: src/java/org/apache/ojb/broker/accesslayer RsIterator.java
src/java/org/apache/ojb/broker/accesslayer/sql
SqlDeleteByQuery.java SqlQueryStatement.java
SqlSelectStatement.java
src/java/org/apache/ojb/broker/core
PersistenceBrokerImpl.java
src/java/org/apache/ojb/broker/query AbstractQueryImpl.java
Query.java QueryByCriteria.java
src/java/org/apache/ojb/odmg/oql OQLLexer.java
OQLLexerTokenTypes.java OQLParser.java oql-ojb.g
src/test/org/apache/ojb/odmg
ContractVersionEffectivenessOQLTest.java
Log:
Small refactoring to allow queries like 'SELECT ref1.ref2.ref3 ..." returning objects.
QueryByCriteria now has two different notions: baseClass and searchClass,
in the above example baseClass.ref1.ref2.ref3 represents the value of the searchClass
Revision Changes Path
1.40 +4 -9 db-ojb/src/java/org/apache/ojb/broker/accesslayer/RsIterator.java
Index: RsIterator.java
===================================================================
RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/accesslayer/RsIterator.java,v
retrieving revision 1.39
retrieving revision 1.40
diff -u -r1.39 -r1.40
--- RsIterator.java 28 Jul 2003 08:14:50 -0000 1.39
+++ RsIterator.java 2 Aug 2003 08:27:22 -0000 1.40
@@ -63,6 +63,7 @@
import org.apache.ojb.broker.metadata.DescriptorRepository;
import org.apache.ojb.broker.metadata.FieldDescriptor;
import org.apache.ojb.broker.metadata.JdbcConnectionDescriptor;
+import org.apache.ojb.broker.metadata.ObjectReferenceDescriptor;
import org.apache.ojb.broker.query.Query;
import org.apache.ojb.broker.query.QueryByCriteria;
import org.apache.ojb.broker.core.PersistenceBrokerImpl;
@@ -71,8 +72,10 @@
import java.sql.ResultSet;
import java.sql.SQLException;
+import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
+import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
@@ -251,14 +254,6 @@
if (hasNext)
{
Object obj = getObjectFromResultSet();
- if (m_query instanceof QueryByCriteria)
- {
- if (((QueryByCriteria)m_query).getObjectProjectionAttribute() != null)
- {
- obj = m_cld.getObjectReferenceDescriptorByName(((QueryByCriteria)m_query).getObjectProjectionAttribute()).getPersistentField().get(obj);
- }
-
- }
m_current_row++;
// Invoke events on PersistenceBrokerAware instances and listeners
1.11 +2 -2 db-ojb/src/java/org/apache/ojb/broker/accesslayer/sql/SqlDeleteByQuery.java
Index: SqlDeleteByQuery.java
===================================================================
RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/accesslayer/sql/SqlDeleteByQuery.java,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -r1.10 -r1.11
--- SqlDeleteByQuery.java 21 Mar 2003 19:50:59 -0000 1.10
+++ SqlDeleteByQuery.java 2 Aug 2003 08:27:22 -0000 1.11
@@ -91,7 +91,7 @@
Criteria crit = this.getQuery().getCriteria();
stmt.append("DELETE FROM ");
- stmt.append(getClassDescriptor().getFullTableName());
+ stmt.append(getSearchClassDescriptor().getFullTableName());
appendWhereClause(where, crit, stmt);
return stmt.toString();
1.47 +18 -10 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.46
retrieving revision 1.47
diff -u -r1.46 -r1.47
--- SqlQueryStatement.java 27 Jul 2003 00:22:51 -0000 1.46
+++ SqlQueryStatement.java 2 Aug 2003 08:27:22 -0000 1.47
@@ -99,7 +99,9 @@
private HashMap m_joinTreeToCriteria = new HashMap();
private Platform pf;
- private ClassDescriptor m_cld;
+ private ClassDescriptor m_baseCld;
+ private ClassDescriptor m_searchCld;
+
/**
* Constructor for SqlCriteriaStatement.
@@ -107,10 +109,11 @@
public SqlQueryStatement(Platform pf, ClassDescriptor cld, String[] columns, Query query, Logger logger)
{
this.m_root = createTableAlias(cld, null,"");
- this.m_query = (QueryByCriteria)query;
+ this.m_query = (QueryByCriteria) query;
this.m_columns = columns;
this.pf = pf;
- this.m_cld = cld;
+ this.m_searchCld = cld;
+ this.m_baseCld = cld.getRepository().getDescriptorFor(query.getBaseClass());
// In some cases it is necessary to split the query criteria
// and then to generate UNION of several SELECTs
@@ -121,15 +124,20 @@
}
}
- protected ClassDescriptor getClassDescriptor()
+ protected ClassDescriptor getBaseClassDescriptor()
+ {
+ return m_baseCld;
+ }
+
+ protected ClassDescriptor getSearchClassDescriptor()
{
- return m_cld;
+ return m_searchCld;
}
/**
* Return the TableAlias and the PathInfo for an Attribute name<br>
* field names in functions (ie: sum(name) ) are tried to resolve ie: name
- * from FIELDDESCRIPTOR , UPPER(name_test) from Criteria<br>
+ * from FIELDDESCRIPTOR , UPPER(name_test) from Criteria<br>
* also resolve pathExpression adress.city or owner.konti.saldo
* @param attr
* @param useOuterJoins
@@ -144,9 +152,9 @@
String colName = pathInfo.column;
int sp;
-
- sp = colName.lastIndexOf(".");
-
+
+ sp = colName.lastIndexOf(".");
+
if (sp == -1)
{
// BRJ : ignore alias for simple attributes ??
1.13 +5 -5 db-ojb/src/java/org/apache/ojb/broker/accesslayer/sql/SqlSelectStatement.java
Index: SqlSelectStatement.java
===================================================================
RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/accesslayer/sql/SqlSelectStatement.java,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -r1.12 -r1.13
--- SqlSelectStatement.java 24 Apr 2003 20:09:30 -0000 1.12
+++ SqlSelectStatement.java 2 Aug 2003 08:27:22 -0000 1.13
@@ -100,9 +100,9 @@
* @return list of column names for the set of all unique columns for multiple classes mapped to the
* same table.
*/
- protected List appendListOfColumnsForSelect(TableAlias alias, StringBuffer buf)
+ protected List appendListOfColumnsForSelect(ClassDescriptor cld, StringBuffer buf)
{
- FieldDescriptor[] fieldDescriptors = alias.cld.getRepository().getFieldDescriptorsForMultiMappedTable(alias.cld);
+ FieldDescriptor[] fieldDescriptors = cld.getRepository().getFieldDescriptorsForMultiMappedTable(cld);
int fieldDescriptorLength = fieldDescriptors.length;
ArrayList columnList = new ArrayList();
int i = 0;
@@ -210,7 +210,7 @@
* will allow us to load objects with unique mapping fields that are mapped
* to the same table.
*/
- columnList = appendListOfColumnsForSelect(getRoot(), stmt);
+ columnList = appendListOfColumnsForSelect(getSearchClassDescriptor(), stmt);
}
else
{
@@ -234,7 +234,7 @@
* going to map superclass tables here, not sure about the columns, just using all columns for now
*
*/
- ClassDescriptor cld = super.getClassDescriptor();
+ ClassDescriptor cld = super.getBaseClassDescriptor();
ClassDescriptor cldSuper = null;
if(cld.getSuperClass() != null)
{
1.31 +1 -9 db-ojb/src/java/org/apache/ojb/broker/core/PersistenceBrokerImpl.java
Index: PersistenceBrokerImpl.java
===================================================================
RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/core/PersistenceBrokerImpl.java,v
retrieving revision 1.30
retrieving revision 1.31
diff -u -r1.30 -r1.31
--- PersistenceBrokerImpl.java 28 Jul 2003 08:14:43 -0000 1.30
+++ PersistenceBrokerImpl.java 2 Aug 2003 08:27:22 -0000 1.31
@@ -1387,14 +1387,6 @@
result.ojbAdd(candidate);
retrievedCount++;
}
- else if (query instanceof QueryByCriteria)
- {
- if (((QueryByCriteria)query).getObjectProjectionAttribute()!=null)
- {
- result.ojbAdd(candidate);
- retrievedCount++;
- }
- }
else
{
//warn the user
1.6 +46 -36 db-ojb/src/java/org/apache/ojb/broker/query/AbstractQueryImpl.java
Index: AbstractQueryImpl.java
===================================================================
RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/query/AbstractQueryImpl.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- AbstractQueryImpl.java 15 Mar 2003 15:42:26 -0000 1.5
+++ AbstractQueryImpl.java 2 Aug 2003 08:27:22 -0000 1.6
@@ -59,61 +59,71 @@
/**
* Abstract implemenation of Query interface
- *
+ *
* @author ???
* @version $Id$
*/
public abstract class AbstractQueryImpl implements Query, Serializable
{
- private int m_startAtIndex = Query.NO_START_AT_INDEX;
- private int m_endAtIndex = Query.NO_END_AT_INDEX;
- private int m_fullSize = 0;
- private Class m_clazz;
+ private int m_startAtIndex = Query.NO_START_AT_INDEX;
+ private int m_endAtIndex = Query.NO_END_AT_INDEX;
+ private int m_fullSize = 0;
+ protected Class m_searchClass;
+ private Class m_baseClass;
public AbstractQueryImpl()
{
}
-
+
public AbstractQueryImpl(Class aSearchClass)
{
- m_clazz = aSearchClass;
+ m_searchClass = aSearchClass;
+ m_baseClass = aSearchClass;
+ }
+
+ public int getStartAtIndex()
+ {
+ return m_startAtIndex;
}
-
- public int getStartAtIndex()
- {
- return m_startAtIndex;
- }
-
+
public void setStartAtIndex(int startAtIndex)
- {
- m_startAtIndex = startAtIndex;
- }
-
- public int getEndAtIndex()
- {
- return m_endAtIndex;
- }
- public void setEndAtIndex(int endAtIndex)
- {
- m_endAtIndex = endAtIndex;
- }
- public void fullSize(int size)
- {
- m_fullSize = size;
- }
- public int fullSize()
- {
- return m_fullSize;
- }
+ {
+ m_startAtIndex = startAtIndex;
+ }
+
+ public int getEndAtIndex()
+ {
+ return m_endAtIndex;
+ }
+ public void setEndAtIndex(int endAtIndex)
+ {
+ m_endAtIndex = endAtIndex;
+ }
+ public void fullSize(int size)
+ {
+ m_fullSize = size;
+ }
+ public int fullSize()
+ {
+ return m_fullSize;
+ }
/*
* @see Query#getSearchClass()
*/
public Class getSearchClass()
{
- return m_clazz;
+ return m_searchClass;
}
-
+
+ /*
+ * @see Query#getBaseClass()
+ */
+ public Class getBaseClass()
+ {
+ return m_baseClass;
+ }
+
/* (non-Javadoc)
* @see org.apache.ojb.broker.query.Query#getGroupBy()
*/
1.10 +78 -73 db-ojb/src/java/org/apache/ojb/broker/query/Query.java
Index: Query.java
===================================================================
RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/query/Query.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- Query.java 15 Mar 2003 15:42:26 -0000 1.9
+++ Query.java 2 Aug 2003 08:27:22 -0000 1.10
@@ -83,83 +83,88 @@
*/
public interface Query extends java.io.Serializable
{
- public static final int NO_START_AT_INDEX = 0;
- public static final int NO_END_AT_INDEX = 0;
- public static final boolean SCROLLABLE = true;
- public static final boolean NOT_SCROLLABLE = false;
-
- /**
- * return the criteria of the query if present or null.
- */
- public abstract Criteria getCriteria();
-
- /**
- * return the criteria of the query if present or null.
- */
- public abstract Criteria getHavingCriteria();
-
- /**
- * return the template Object if present or null
- */
- public abstract Object getExampleObject();
-
- /**
- * return the target class, representing the extend to be searched
- */
- public abstract Class getSearchClass();
-
- /**
- * return true if select DISTINCT should be used
- */
- public boolean isDistinct();
-
- /**
- * Answer the orderBy of all Criteria and Sub Criteria the elements are of
- * class FieldHelper
- * @return List of FieldHelper
- */
- public List getOrderBy();
-
- /**
- * Gets the groupby for ReportQueries of all Criteria and Sub Criteria
- * the elements are of class FieldHelper
- * @return List of FieldHelper
- */
- public List getGroupBy();
-
- /**
- *
- * @return the row at which the query should start retrieving results. If the start at index is -1, then ignore all
- * cursor control.
- */
- int getStartAtIndex();
-
- /**
- * Set the row at which the query should start retrieving results, inclusive.
- * @param startAtIndex starting index, inclusive.
- */
- void setStartAtIndex(int startAtIndex);
-
- /**
- *
- * @return the row at which the query should stop retrieving results. If the end at index is -1, ignore all cursor
- * control
- */
- int getEndAtIndex();
-
- /**
- * Set the row at which the query should stop retieving results, exlusive.
- * @param endAtIndex ending index, exclusive
- */
- void setEndAtIndex(int endAtIndex);
+ public static final int NO_START_AT_INDEX = 0;
+ public static final int NO_END_AT_INDEX = 0;
+ public static final boolean SCROLLABLE = true;
+ public static final boolean NOT_SCROLLABLE = false;
+
+ /**
+ * return the criteria of the query if present or null.
+ */
+ public abstract Criteria getCriteria();
+
+ /**
+ * return the criteria of the query if present or null.
+ */
+ public abstract Criteria getHavingCriteria();
+
+ /**
+ * return the template Object if present or null
+ */
+ public abstract Object getExampleObject();
+
+ /**
+ * return the target class, representing the extend to be searched
+ */
+ public abstract Class getSearchClass();
+
+ /**
+ * return the base class, with respect to which all paths are done
+ */
+ public abstract Class getBaseClass();
+
+ /**
+ * return true if select DISTINCT should be used
+ */
+ public boolean isDistinct();
+
+ /**
+ * Answer the orderBy of all Criteria and Sub Criteria the elements are of
+ * class FieldHelper
+ * @return List of FieldHelper
+ */
+ public List getOrderBy();
+
+ /**
+ * Gets the groupby for ReportQueries of all Criteria and Sub Criteria
+ * the elements are of class FieldHelper
+ * @return List of FieldHelper
+ */
+ public List getGroupBy();
+
+ /**
+ *
+ * @return the row at which the query should start retrieving results. If the start at index is -1, then ignore all
+ * cursor control.
+ */
+ int getStartAtIndex();
+
+ /**
+ * Set the row at which the query should start retrieving results, inclusive.
+ * @param startAtIndex starting index, inclusive.
+ */
+ void setStartAtIndex(int startAtIndex);
+
+ /**
+ *
+ * @return the row at which the query should stop retrieving results. If the end at index is -1, ignore all cursor
+ * control
+ */
+ int getEndAtIndex();
+
+ /**
+ * Set the row at which the query should stop retieving results, exlusive.
+ * @param endAtIndex ending index, exclusive
+ */
+ void setEndAtIndex(int endAtIndex);
/**
* Returns the names of Relationships to be prefetched
* @return List of Strings
*/
public List getPrefetchedRelationships();
-
- void fullSize(int size);
- int fullSize();
+
+ void fullSize(int size);
+ int fullSize();
}
1.19 +16 -14 db-ojb/src/java/org/apache/ojb/broker/query/QueryByCriteria.java
Index: QueryByCriteria.java
===================================================================
RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/query/QueryByCriteria.java,v
retrieving revision 1.18
retrieving revision 1.19
diff -u -r1.18 -r1.19
--- QueryByCriteria.java 1 Jul 2003 17:41:26 -0000 1.18
+++ QueryByCriteria.java 2 Aug 2003 08:27:22 -0000 1.19
@@ -329,7 +329,7 @@
*/
public String toString()
{
- return "Query from " + getSearchClass() + " where " + m_criteria;
+ return "Query from " + getBaseClass() + " where " + m_criteria;
}
/**
@@ -538,20 +538,22 @@
}
return temp;
- }
+ }
- protected Map getPathOuterJoin()
- {
- return m_pathOuterJoin;
- }
+ protected Map getPathOuterJoin()
+ {
+ return m_pathOuterJoin;
+ }
- public String getObjectProjectionAttribute()
- {
- return m_objectProjectionAttribute;
- }
+ public String getObjectProjectionAttribute()
+ {
+ return m_objectProjectionAttribute;
+ }
- public void setObjectProjectionAttribute(String objectProjectionAttribute)
- {
- m_objectProjectionAttribute = objectProjectionAttribute;
+ public void setObjectProjectionAttribute(String objectProjectionAttribute,
+ Class objectProjectionClass)
+ {
+ m_objectProjectionAttribute = objectProjectionAttribute;
+ m_searchClass = objectProjectionClass;
}
}
1.10 +9 -20 db-ojb/src/java/org/apache/ojb/odmg/oql/OQLLexer.java
Index: OQLLexer.java
===================================================================
RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/odmg/oql/OQLLexer.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- OQLLexer.java 29 Jun 2003 15:08:03 -0000 1.9
+++ OQLLexer.java 2 Aug 2003 08:27:22 -0000 1.10
@@ -1,26 +1,6 @@
// $ANTLR 2.7.1: "oql-ojb.g" -> "OQLLexer.java"$
/*
- * ObJectBridge - Bridging Java Objects and Relational Databases
- * http://objectbridge.sourceforge.net
- * Copyright (C) 2000, 2001 Thomas Mahler, et al.
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- */
-
-/*
* This OQL grammar has been derived from a OQL sample grammar from the ODMG
* WebSite. The original grammar is copyright protected by MicroData Base
* Systems: Copyright (c) 1999 Micro Data Base Systems, Inc. All rights
@@ -40,6 +20,10 @@
*/
package org.apache.ojb.odmg.oql;
+import org.apache.ojb.broker.query.*;
+import org.apache.ojb.broker.metadata.*;
+import org.apache.ojb.broker.*;
+import java.util.*;
import java.io.InputStream;
import antlr.TokenStreamException;
@@ -47,18 +31,23 @@
import antlr.TokenStreamRecognitionException;
import antlr.CharStreamException;
import antlr.CharStreamIOException;
+import antlr.ANTLRException;
import java.io.Reader;
import java.util.Hashtable;
+import antlr.CharScanner;
import antlr.InputBuffer;
import antlr.ByteBuffer;
import antlr.CharBuffer;
import antlr.Token;
+import antlr.CommonToken;
import antlr.RecognitionException;
import antlr.NoViableAltForCharException;
+import antlr.MismatchedCharException;
import antlr.TokenStream;
import antlr.ANTLRHashString;
import antlr.LexerSharedInputState;
import antlr.collections.impl.BitSet;
+import antlr.SemanticException;
public class OQLLexer extends antlr.CharScanner implements OQLLexerTokenTypes, TokenStream
{
1.6 +4 -21 db-ojb/src/java/org/apache/ojb/odmg/oql/OQLLexerTokenTypes.java
Index: OQLLexerTokenTypes.java
===================================================================
RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/odmg/oql/OQLLexerTokenTypes.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- OQLLexerTokenTypes.java 29 Jun 2003 15:08:02 -0000 1.5
+++ OQLLexerTokenTypes.java 2 Aug 2003 08:27:22 -0000 1.6
@@ -1,26 +1,6 @@
// $ANTLR 2.7.1: "oql-ojb.g" -> "OQLParser.java"$
/*
- * ObJectBridge - Bridging Java Objects and Relational Databases
- * http://objectbridge.sourceforge.net
- * Copyright (C) 2000, 2001 Thomas Mahler, et al.
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- */
-
-/*
* This OQL grammar has been derived from a OQL sample grammar from the ODMG
* WebSite. The original grammar is copyright protected by MicroData Base
* Systems: Copyright (c) 1999 Micro Data Base Systems, Inc. All rights
@@ -40,7 +20,10 @@
*/
package org.apache.ojb.odmg.oql;
-
+import org.apache.ojb.broker.query.*;
+import org.apache.ojb.broker.metadata.*;
+import org.apache.ojb.broker.*;
+import java.util.*;
public interface OQLLexerTokenTypes {
int EOF = 1;
1.15 +37 -31 db-ojb/src/java/org/apache/ojb/odmg/oql/OQLParser.java
Index: OQLParser.java
===================================================================
RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/odmg/oql/OQLParser.java,v
retrieving revision 1.14
retrieving revision 1.15
diff -u -r1.14 -r1.15
--- OQLParser.java 29 Jun 2003 15:08:03 -0000 1.14
+++ OQLParser.java 2 Aug 2003 08:27:22 -0000 1.15
@@ -1,26 +1,6 @@
// $ANTLR 2.7.1: "oql-ojb.g" -> "OQLParser.java"$
/*
- * ObJectBridge - Bridging Java Objects and Relational Databases
- * http://objectbridge.sourceforge.net
- * Copyright (C) 2000, 2001 Thomas Mahler, et al.
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- */
-
-/*
* This OQL grammar has been derived from a OQL sample grammar from the ODMG
* WebSite. The original grammar is copyright protected by MicroData Base
* Systems: Copyright (c) 1999 Micro Data Base Systems, Inc. All rights
@@ -41,16 +21,26 @@
package org.apache.ojb.odmg.oql;
import org.apache.ojb.broker.query.*;
+import org.apache.ojb.broker.metadata.*;
+import org.apache.ojb.broker.*;
import java.util.*;
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 antlr.collections.AST;
+import antlr.ASTPair;
+import antlr.collections.impl.ASTArray;
public class OQLParser extends antlr.LLkParser
implements OQLLexerTokenTypes
@@ -111,17 +101,17 @@
try { // for error handling
- Class clazz = null;
- Criteria criteria = new Criteria();
- String[] projectionAttrs;
- boolean distinct = false;
+ Class clazz = null;
+ Criteria criteria = new Criteria();
+ String[] projectionAttrs;
+ boolean distinct = false;
match(LITERAL_select);
{
if ((LA(1)==LITERAL_distinct)) {
match(LITERAL_distinct);
- distinct = true;
+ distinct = true;
}
else if ((LA(1)==TOK_STAR||LA(1)==Identifier)) {
@@ -147,19 +137,35 @@
}
- if (clazz != null) {
+ if (clazz != null)
+ {
if (projectionAttrs[0].indexOf('.') < 0)
{
query = QueryFactory.newQuery(clazz, criteria, distinct);
}
else
{
- for (int i = 0; i < projectionAttrs.length; i++) {
- projectionAttrs[i] = projectionAttrs[i].substring(
- projectionAttrs[i].indexOf('.') + 1);
+ ClassDescriptor cld = PersistenceBrokerFactory.defaultPersistenceBroker().getClassDescriptor(clazz);
+ for (int i = 0; i < projectionAttrs.length; i++)
+ {
+ projectionAttrs[i] = projectionAttrs[i].substring(projectionAttrs[i].indexOf('.') + 1);
+ }
+
+ ArrayList descs = cld.getAttributeDescriptorsForPath(projectionAttrs[0]);
+ int pathLen = descs.size();
+
+ if ((pathLen > 0) && (descs.get(pathLen - 1) instanceof ObjectReferenceDescriptor))
+ {
+ ObjectReferenceDescriptor ord =
+ ((ObjectReferenceDescriptor) descs.get(pathLen - 1));
+ query = QueryFactory.newQuery(clazz, criteria, distinct);
+ query.setObjectProjectionAttribute(projectionAttrs[0],
+ ord.getItemClass());
+ }
+ else
+ {
+ query = QueryFactory.newReportQuery(clazz, projectionAttrs, criteria, distinct);
}
- query = QueryFactory.newReportQuery(clazz, projectionAttrs,
- criteria, distinct);
}
}
1.18 +37 -36 db-ojb/src/java/org/apache/ojb/odmg/oql/oql-ojb.g
Index: oql-ojb.g
===================================================================
RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/odmg/oql/oql-ojb.g,v
retrieving revision 1.17
retrieving revision 1.18
diff -u -r1.17 -r1.18
--- oql-ojb.g 27 Jul 2003 11:17:12 -0000 1.17
+++ oql-ojb.g 2 Aug 2003 08:27:22 -0000 1.18
@@ -357,43 +357,44 @@
}
)?
- projectionAttrs = projectionAttributes
+ projectionAttrs = projectionAttributes
- "from" clazz = fromClause
- ( "where" whereClause[criteria] )?
- {
- if (clazz != null)
- {
- if (projectionAttrs[0].indexOf('.') < 0)
- {
- query = QueryFactory.newQuery(clazz, criteria, distinct);
- }
- else
- {
+ "from" clazz = fromClause
+ ( "where" whereClause[criteria] )?
+ {
+ if (clazz != null)
+ {
+ if (projectionAttrs[0].indexOf('.') < 0)
+ {
+ query = QueryFactory.newQuery(clazz, criteria, distinct);
+ }
+ else
+ {
ClassDescriptor cld = PersistenceBrokerFactory.defaultPersistenceBroker().getClassDescriptor(clazz);
- String objectProjectionAttribute = null;
- for (int i = 0; i < projectionAttrs.length; i++)
- {
- projectionAttrs[i] = projectionAttrs[i].substring(projectionAttrs[i].indexOf('.') + 1);
- if (cld.getObjectReferenceDescriptorByName(projectionAttrs[i]) != null)
- {
- objectProjectionAttribute = projectionAttrs[i];
- break;
- }
- }
- if (objectProjectionAttribute != null)
- {
- query = QueryFactory.newQuery(clazz, criteria, distinct);
- query.setObjectProjectionAttribute(objectProjectionAttribute);
- }
- else
- {
- query = QueryFactory.newReportQuery(clazz, projectionAttrs, criteria, distinct);
- }
- }
- }
- }
- ( "order" "by" orderClause[query] )?
+ for (int i = 0; i < projectionAttrs.length; i++)
+ {
+ projectionAttrs[i] = projectionAttrs[i].substring(projectionAttrs[i].indexOf('.') + 1);
+ }
+
+ ArrayList descs = cld.getAttributeDescriptorsForPath(projectionAttrs[0]);
+ int pathLen = descs.size();
+
+ if ((pathLen > 0) && (descs.get(pathLen - 1) instanceof ObjectReferenceDescriptor))
+ {
+ ObjectReferenceDescriptor ord =
+ ((ObjectReferenceDescriptor) descs.get(pathLen - 1));
+ query = QueryFactory.newQuery(clazz, criteria, distinct);
+ query.setObjectProjectionAttribute(projectionAttrs[0],
+ ord.getItemClass());
+ }
+ else
+ {
+ query = QueryFactory.newReportQuery(clazz, projectionAttrs, criteria, distinct);
+ }
+ }
+ }
+ }
+ ( "order" "by" orderClause[query] )?
( "prefetch" prefetchClause[query] )?
@@ -807,4 +808,4 @@
{
if (!temp.isEmpty()) {coll = temp;}
}
- ;
\ No newline at end of file
+ ;
1.13 +9 -8 db-ojb/src/test/org/apache/ojb/odmg/ContractVersionEffectivenessOQLTest.java
Index: ContractVersionEffectivenessOQLTest.java
===================================================================
RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/odmg/ContractVersionEffectivenessOQLTest.java,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -r1.12 -r1.13
--- ContractVersionEffectivenessOQLTest.java 27 Jul 2003 11:18:00 -0000 1.12
+++ ContractVersionEffectivenessOQLTest.java 2 Aug 2003 08:27:22 -0000 1.13
@@ -299,19 +299,20 @@
{
createData(db, odmg);
OQLQuery query = odmg.newOQLQuery();
- int i = 0;
query.create("select effectiveness.version from " + Effectiveness.class.getName() + " where is_defined(effectiveness.version.versionValue1)");
ManageableCollection all = (ManageableCollection) query.execute();
java.util.Iterator it = all.ojbIterator();
- Object temp1 = null;
- Version temp = null;
while (it.hasNext())
{
- temp1 = it.next();
-
- //thma: the following cast can not work. The OQL parser uses
- //ReportQueries if a "." is detected in the projection attribute.
- //temp = (Version)temp1;
+ assertTrue("Selected item is Version", (it.next() instanceof Version));
+ }
+
+ query.create("select effectiveness.version.contract from " + Effectiveness.class.getName() + " where is_defined(effectiveness.version.versionValue1)");
+ all = (ManageableCollection) query.execute();
+ it = all.ojbIterator();
+ while (it.hasNext())
+ {
+ assertTrue("Selected item is Contract", (it.next() instanceof Contract));
}
}
finally
---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org