brj 2005/02/26 12:02:19
Modified: src/java/org/apache/ojb/broker/accesslayer/sql
SqlQueryStatement.java
Added: src/java/org/apache/ojb/broker/accesslayer/sql
AttributeInfo.java
Log:
Refactoring of SqlQueryStatement
Revision Changes Path
1.98 +14 -83
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.97
retrieving revision 1.98
diff -u -r1.97 -r1.98
--- SqlQueryStatement.java 14 Feb 2005 19:32:13 -0000 1.97
+++ SqlQueryStatement.java 26 Feb 2005 20:02:19 -0000 1.98
@@ -15,7 +15,6 @@
* limitations under the License.
*/
-import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
@@ -25,6 +24,7 @@
import org.apache.ojb.broker.PersistenceBrokerException;
import org.apache.ojb.broker.accesslayer.JoinSyntaxTypes;
+import
org.apache.ojb.broker.accesslayer.sql.AttributeInfo.SingleAttributeInfo;
import org.apache.ojb.broker.accesslayer.sql.TableAliasHandler.Join;
import org.apache.ojb.broker.accesslayer.sql.TableAliasHandler.TableAlias;
import org.apache.ojb.broker.metadata.ClassDescriptor;
@@ -140,11 +140,12 @@
* [0.9, * , ( sai(price), * , sai(stock), )]
* </pre>
* The attribute infos are cached for later use.
+ *
* @param attr the attribute or expression
* @param useOuterJoins
* @param aUserAlias
* @param pathClasses
- * @return the AttributeInfo od the attribute or expression
+ * @return the AttributeInfo of the attribute or expression
*/
protected AttributeInfo getAttributeInfo(String attr, boolean
useOuterJoins, UserAlias aUserAlias, Map pathClasses)
{
@@ -154,28 +155,26 @@
if (result == null)
{
StringTokenizer st = SqlHelper.tokenizeAttribute(attr);
- result = new AttributeInfo();
- List elements = result.elements;
+ result = new AttributeInfo(attr);
while (st.hasMoreTokens())
{
String token = st.nextToken();
if (SqlHelper.isAttribute(token))
{
- elements.add(getSingleAttributeInfo(token,
useOuterJoins, aUserAlias, pathClasses));
+ result.add(getSingleAttributeInfo(token, useOuterJoins,
aUserAlias, pathClasses));
}
else
{
- elements.add(token);
+ result.add(token);
}
}
- result.attribute = attr;
m_attributeCache.put(cacheKey, result);
}
else
{
- result.extentAliasIndex = 0; // Reset extent index
+ result.reset(); // Reset extent index
}
return result;
@@ -215,7 +214,8 @@
* @param pathClasses
* @return
*/
- private SingleAttributeInfo getSingleAttributeInfo(String attrName,
boolean useOuterJoins, UserAlias aUserAlias, Map pathClasses)
+ private SingleAttributeInfo getSingleAttributeInfo(String attrName,
boolean useOuterJoins,
+ UserAlias aUserAlias, Map pathClasses)
{
TableAlias tableAlias = null;
int sp;
@@ -341,7 +341,7 @@
*/
protected boolean appendAttribute(AttributeInfo attrInfo, boolean
translate, StringBuffer buf)
{
- Iterator iter = attrInfo.elements.iterator();
+ Iterator iter = attrInfo.iterator();
while (iter.hasNext())
{
@@ -350,17 +350,8 @@
if (element instanceof SingleAttributeInfo)
{
SingleAttributeInfo sai = (SingleAttributeInfo) element;
- TableAlias alias = sai.tableAlias;
-
- if (sai.tableAlias != null &&
- sai.tableAlias.hasExtents() &&
- attrInfo.extentAliasIndex > 0)
- {
- // use the alias of the extent
- alias = (TableAlias)
sai.tableAlias.extents.get(attrInfo.extentAliasIndex - 1);
- }
-
- appendColumn(getColName(alias, sai.attribute, translate),
buf);
+ TableAlias alias = sai.getTableAlias();
+ appendColumn(getColName(alias, sai.getAttribute(),
translate), buf);
}
else
{
@@ -994,7 +985,7 @@
for (int i = 0;i < alias.extents.size(); i++)
{
buf.append(" OR ");
- attrInfo.extentAliasIndex++; // which extent
+ attrInfo.useNextExtent();
appendCriteria(attrInfo, c, buf);
}
buf.append(")");
@@ -1378,64 +1369,4 @@
return getPlatform().getJoinSyntaxType();
}
-
- //-----------------------------------------------------------------
- // ------------------- Inner classes ------------------------------
- //-----------------------------------------------------------------
-
- /**
- * Helper class containing an 'exploded' attribute or path.
- * The exploded path contains Strings for the tokens and functions and
- * SingleAttributeInfo for the attributes.<br>
- * ie: 0.9 * (price * stock) the attributes price and stock are wrapped
- * in SingleAttributeInfo the whole rest are Strings
- * <pre>
- * [0.9, * , ( sai(price), * , sai(stock), )]
- * </pre>
- */
- static final class AttributeInfo
- {
- String attribute;
- List elements = new ArrayList(); // List of String and
SingleAttributeInfo
- int extentAliasIndex = 0; // which alias to use
-
- /**
- * Answer the TableAlias of the <b>first</b> SingleAttributeInfo.
- * @return TableAlias
- */
- TableAlias getTableAlias()
- {
- TableAlias result = null;
- Iterator iter = elements.iterator();
- while (iter.hasNext())
- {
- Object element = iter.next();
- if (element instanceof SingleAttributeInfo)
- {
- result = ((SingleAttributeInfo)element).tableAlias;
- break;
- }
- }
-
- return result;
- }
- }
-
-
- /**
- * Helper Class containing a TableAlias and an Attribute.
- * ie: A0.emp_firstname
- */
- static final class SingleAttributeInfo
- {
- TableAlias tableAlias;
- String attribute;
-
- SingleAttributeInfo(TableAlias aTableAlias, String anAttribute)
- {
- tableAlias = aTableAlias;
- attribute = anAttribute;
- }
- }
-
}
1.1
db-ojb/src/java/org/apache/ojb/broker/accesslayer/sql/AttributeInfo.java
Index: AttributeInfo.java
===================================================================
package org.apache.ojb.broker.accesslayer.sql;
/* Copyright 2002-2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.ojb.broker.accesslayer.sql.TableAliasHandler.TableAlias;
/**
* Helper class containing an 'exploded' attribute or path.
* The exploded path contains Strings for the tokens and functions and
* SingleAttributeInfo for the attributes.<br>
* ie: 0.9 * (price * stock) the attributes price and stock are wrapped
* in SingleAttributeInfo the whole rest are Strings
* <pre>
* [0.9, * , ( sai(price), * , sai(stock), )]
* </pre>
* <b>All SingleAttributeInfo must belong to the same alias !</b>
*
* @author <a href="mailto:[EMAIL PROTECTED]">Jakob Braeuchi</a>
* @version $Id: AttributeInfo.java,v 1.1 2005/02/26 20:02:19 brj Exp $ *
*/
class AttributeInfo
{
private String m_attribute; // For information only
private List m_elements = new ArrayList(); // List of String and
SingleAttributeInfo
private int m_extentAliasIndex = 0; // which alias to use
AttributeInfo(String anAttribute)
{
m_attribute = anAttribute;
}
/**
* Answer the TableAlias of the <b>first</b> SingleAttributeInfo.
* @return TableAlias
*/
TableAlias getTableAlias()
{
TableAlias result = null;
Iterator iter = m_elements.iterator();
while (iter.hasNext())
{
Object element = iter.next();
if (element instanceof SingleAttributeInfo)
{
result = ((SingleAttributeInfo) element).m_tableAlias;
break;
}
}
return result;
}
/**
* Add a SingleAttributeInfo and set it's parent.
* @param sai SingleAttributeInfo
*/
void add(SingleAttributeInfo sai)
{
sai.m_parent = this;
m_elements.add(sai);
}
/**
* Add a simple String.
* @param str
*/
void add(String str)
{
m_elements.add(str);
}
Iterator iterator()
{
return m_elements.iterator();
}
/**
* Increment the extentIndex.
*/
void useNextExtent()
{
m_extentAliasIndex++;
}
/**
* Reset the extentIndex.
*/
void reset()
{
m_extentAliasIndex = 0;
}
//-----------------------------------------------------------------
// ------------------- Inner classes ------------------------------
//-----------------------------------------------------------------
/**
* Helper Class containing a TableAlias and an Attribute.
* ie: A0.emp_firstname
*/
static final class SingleAttributeInfo
{
private TableAlias m_tableAlias;
private String m_attribute;
private AttributeInfo m_parent;
SingleAttributeInfo(TableAlias aTableAlias, String anAttribute)
{
m_tableAlias = aTableAlias;
m_attribute = anAttribute;
}
/**
* Answer the TableAlias based on extentAliasIndex of the parent
AttributeInfo.
* @return the TableAlias
*/
TableAlias getTableAlias()
{
TableAlias result = m_tableAlias;
int aliasIndex = m_parent.m_extentAliasIndex;
if (aliasIndex > 0)
{
result = (TableAlias) m_tableAlias.extents.get(aliasIndex -
1);
}
return result;
}
String getAttribute()
{
return m_attribute;
}
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]