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 br...@apache.org on 2003/03/01 21:19:56 UTC
cvs commit: db-ojb/src/java/org/apache/ojb/broker/accesslayer/sql SqlQueryStatement.java SqlSelectStatement.java
brj 2003/03/01 12:19:56
Modified: src/java/org/apache/ojb/broker/accesslayer/sql
SqlQueryStatement.java SqlSelectStatement.java
Log:
allow user defined alias to support multiple joins on the same table.
fix by Roger Janssen ( mailto:roger.janssen@ibanx.nl )
Revision Changes Path
1.24 +162 -51 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.23
retrieving revision 1.24
diff -u -r1.23 -r1.24
--- SqlQueryStatement.java 24 Feb 2003 19:03:34 -0000 1.23
+++ SqlQueryStatement.java 1 Mar 2003 20:19:56 -0000 1.24
@@ -132,24 +132,57 @@
* also resolve pathExpression adress.city or owner.konti.saldo
* @param attr
* @param useOuterJoins
+ * @param aUserAlias
* @return ColumnInfo
*/
- protected AttributeInfo getAttributeInfo(String attr, boolean useOuterJoins)
+ protected AttributeInfo getAttributeInfo(String attr, boolean useOuterJoins, String aUserAlias)
{
AttributeInfo result = new AttributeInfo();
- TableAlias alias = getRoot();
+ TableAlias tableAlias;
SqlHelper.PathInfo pathInfo = SqlHelper.splitPath(attr);
String colName = pathInfo.column;
int sp;
- sp = colName.lastIndexOf(".");
- if (sp >= 0)
+
+ sp = colName.lastIndexOf(".");
+
+ if (sp == -1)
+ {
+ if (aUserAlias == null)
+ {
+ tableAlias = getRoot();
+ }
+ else
+ {
+ tableAlias = getTableAlias(colName, useOuterJoins, aUserAlias);
+ if (tableAlias == null)
+ {
+ tableAlias = getRoot();
+ }
+ }
+ }
+ else
{
String pathName = colName.substring(0, sp);
String fieldName = colName.substring(sp + 1);;
- alias = getTableAlias(pathName, useOuterJoins);
- if (alias != null)
+ tableAlias = getTableAlias(pathName, useOuterJoins, aUserAlias);
+ /**
+ * if we have not found an alias by the pathName or
+ * aliasName (if given), try again because pathName
+ * may be an aliasname. it can be only and only if it is not
+ * a path, which means there may be no path separators (,)
+ * in the pathName.
+ */
+ if ((tableAlias==null) && (colName.lastIndexOf(".")==-1))
+ {
+ /**
+ * pathName might be an alias, so check this first
+ */
+ tableAlias = getTableAlias(null, useOuterJoins, pathName);
+ }
+
+ if (tableAlias != null)
{
// correct column name to match the alias
// productGroup.groupName -> groupName
@@ -157,7 +190,7 @@
}
}
- result.tableAlias = alias;
+ result.tableAlias = tableAlias;
result.pathInfo = pathInfo;
return result;
}
@@ -166,15 +199,15 @@
* Add the Column to the StringBuffer <br>
* if a FieldDescriptor is found for the Criteria the colName is taken from
* there otherwise its taken from Criteria. <br>
- * @param alias
- * @param pathInfo
+ * @param aTableAlias
+ * @param aPathInfo
* @param buf
*/
- protected void appendColName(TableAlias alias, PathInfo pathInfo, StringBuffer buf)
+ protected void appendColName(TableAlias aTableAlias, PathInfo aPathInfo, StringBuffer buf)
{
- FieldDescriptor fld = getField(alias, pathInfo);
- String functionName = pathInfo.function;
- String colName = pathInfo.column;
+ FieldDescriptor fld = getField(aTableAlias, aPathInfo);
+ String functionName = aPathInfo.function;
+ String colName = aPathInfo.column;
if (functionName != null) // rebuild function contains (
@@ -184,7 +217,7 @@
if (fld != null)
{
- buf.append(alias.alias);
+ buf.append(aTableAlias.alias);
buf.append(".");
buf.append(fld.getColumnName());
}
@@ -201,27 +234,27 @@
/**
* Get the FieldDescriptor for the PathInfo
- * @param alias
- * @param pathInfo
+ * @param aTableAlias
+ * @param aPathInfo
* @return FieldDescriptor
*/
- protected FieldDescriptor getField(TableAlias alias, PathInfo pathInfo)
+ protected FieldDescriptor getField(TableAlias aTableAlias, PathInfo aPathInfo)
{
FieldDescriptor fld = null;
- String colName = pathInfo.column;
+ String colName = aPathInfo.column;
- if (alias != null)
+ if (aTableAlias != null)
{
- fld = alias.cld.getFieldDescriptorByName(colName);
+ fld = aTableAlias.cld.getFieldDescriptorByName(colName);
if (fld == null)
{
- ObjectReferenceDescriptor ord = alias.cld.getObjectReferenceDescriptorByName(colName);
+ ObjectReferenceDescriptor ord = aTableAlias.cld.getObjectReferenceDescriptorByName(colName);
if (ord != null)
{
- if (alias == getRoot())
+ if (aTableAlias == getRoot())
{
// no path expression
- FieldDescriptor[] fk = ord.getForeignKeyFieldDescriptors(alias.cld);
+ FieldDescriptor[] fk = ord.getForeignKeyFieldDescriptors(aTableAlias.cld);
if (fk.length > 0)
{
fld = fk[0];
@@ -235,10 +268,10 @@
* potentially people are referring to objects, not to the object's primary key, and then we need to take the
* primary key attribute of the referenced object to help them out.
*/
- ClassDescriptor cld = alias.cld.getRepository().getDescriptorFor(ord.getItemClass());
+ ClassDescriptor cld = aTableAlias.cld.getRepository().getDescriptorFor(ord.getItemClass());
if (cld != null)
{
- fld = alias.cld.getFieldDescriptorByName(cld.getPkFields()[0].getPersistentField().getName());
+ fld = aTableAlias.cld.getFieldDescriptorByName(cld.getPkFields()[0].getPersistentField().getName());
}
}
}
@@ -256,12 +289,12 @@
* ie: name from FIELDDESCRIPTOR , UPPER(name_test) from Criteria<br>
* also resolve pathExpression adress.city or owner.konti.saldo
*/
- protected void appendColName(String attr, boolean useOuterJoins, StringBuffer buf)
+ protected void appendColName(String attr, boolean useOuterJoins, String aUserAlias, StringBuffer buf)
{
- AttributeInfo attrInfo = getAttributeInfo(attr, useOuterJoins);
- TableAlias alias = attrInfo.tableAlias;
+ AttributeInfo attrInfo = getAttributeInfo(attr, useOuterJoins, aUserAlias);
+ TableAlias tableAlias = attrInfo.tableAlias;
- appendColName(alias, attrInfo.pathInfo, buf);
+ appendColName(tableAlias, attrInfo.pathInfo, buf);
}
/**
@@ -430,7 +463,7 @@
{
appendColName(alias, pathInfo, buf);
buf.append(c.getClause());
- appendColName((String) c.getValue(), false, buf);
+ appendColName((String) c.getValue(), false, c.getAlias(), buf);
}
/**
@@ -476,7 +509,7 @@
*/
private void appendNullCriteria(TableAlias alias, PathInfo pathInfo, NullCriteria c, StringBuffer buf)
{
- appendColName(alias, pathInfo,buf);
+ appendColName(alias, pathInfo, buf);
buf.append(c.getClause());
}
@@ -547,7 +580,7 @@
*/
protected void appendSQLClause(SelectionCriteria c, StringBuffer buf)
{
- AttributeInfo attrInfo = getAttributeInfo(c.getAttribute(), false);
+ AttributeInfo attrInfo = getAttributeInfo(c.getAttribute(), false, c.getAlias());
TableAlias alias = attrInfo.tableAlias;
appendSelectionCriteria(alias, attrInfo.pathInfo, c, buf);
@@ -603,7 +636,7 @@
* @param useOuterJoins use outer join to join this table with the previous
* table in the path.
*/
- private TableAlias getTableAlias(String path, boolean useOuterJoins)
+ private TableAlias getTableAlias(String aPath, boolean useOuterJoins, String aUserAlias)
{
TableAlias curr, prev, indirect;
String attr, attrPath = null;
@@ -615,10 +648,11 @@
Join join;
ArrayList descriptors;
- curr = (TableAlias) m_pathToAlias.get(path);
+
+ curr = getTableAliasForPath(aPath, aUserAlias);
if (curr == null)
{
- descriptors = getRoot().cld.getAttributeDescriptorsForPath(path);
+ descriptors = getRoot().cld.getAttributeDescriptorsForPath(aPath);
prev = getRoot();
for (int i = 0; i < descriptors.size(); i++)
@@ -654,10 +688,10 @@
else
{
String mnAttrPath = attrPath + "*";
- indirect = getTableAlias(mnAttrPath);
+ indirect = getTableAliasForPath(mnAttrPath, aUserAlias, aPath);
if (indirect == null)
{
- indirect = createTableAlias(cod.getIndirectionTable(), mnAttrPath);
+ indirect = createTableAlias(cod.getIndirectionTable(), mnAttrPath, aUserAlias, aPath);
}
// we need two Joins for m:n
@@ -681,10 +715,10 @@
keys = cld.getPkFields();
}
- curr = getTableAlias(attrPath);
+ curr = getTableAliasForPath(attrPath, aUserAlias, aPath);
if (curr == null)
{
- curr = createTableAlias(cld, attrPath);
+ curr = createTableAlias(cld, attrPath, aUserAlias, aPath);
// BRJ : use OUTER join for same table
boolean outer = (curr.cld == prev.cld) || useOuterJoins;
@@ -708,6 +742,26 @@
}
/**
+ * Create a TableAlias for path or userAlias
+ * @param aCld
+ * @param aPath
+ * @param aUserAlias
+ * @param anOriginalPath
+ * @return TableAlias
+ */
+ private TableAlias createTableAlias(ClassDescriptor aCld, String aPath, String aUserAlias, String anOriginalPath)
+ {
+ if (aUserAlias == null || !anOriginalPath.equals(aPath))
+ {
+ return createTableAlias(aCld, aPath);
+ }
+ else
+ {
+ return createTableAlias(aCld, aUserAlias);
+ }
+ }
+
+ /**
* Create new TableAlias for path
* @param cld the class descriptor for the TableAlias
* @param path the path from the target table of the query to this TableAlias.
@@ -733,6 +787,26 @@
}
/**
+ * Create a TableAlias for path or userAlias
+ * @param aTable
+ * @param aPath
+ * @param aUserAlias
+ * @param anOriginalPath
+ * @return TableAlias
+ */
+ private TableAlias createTableAlias(String aTable, String aPath, String aUserAlias, String anOriginalPath)
+ {
+ if (aUserAlias == null || !anOriginalPath.equals(aPath))
+ {
+ return createTableAlias(aTable, aPath);
+ }
+ else
+ {
+ return createTableAlias(aTable, aUserAlias);
+ }
+ }
+
+ /**
* Create new TableAlias for path
* @param table the table name
* @param path the path from the target table of the query to this TableAlias.
@@ -748,16 +822,53 @@
}
/**
- * Answer the TableAlias for aPath
+ * Answer the TableAlias for aPath
* @param aPath
* @return TableAlias, null if none
*/
- private TableAlias getTableAlias(String aPath)
+ private TableAlias getTableAliasForPath(String aPath)
{
return (TableAlias) m_pathToAlias.get(aPath);
}
/**
+ * Answer the TableAlias for aPath or aUserAlias
+ * @param aPath
+ * @param aUserAlias
+ * @return TableAlias, null if none
+ */
+ private TableAlias getTableAliasForPath(String aPath, String aUserAlias)
+ {
+ if (aUserAlias == null)
+ {
+ return getTableAliasForPath(aPath);
+ }
+ else
+ {
+ return getTableAliasForPath(aUserAlias);
+ }
+ }
+
+ /**
+ * Answer the TableAlias for aPath or aUserAlias
+ * @param aPath
+ * @param aUserAlias
+ * @param anOriginalPath
+ * @return TableAlias, null if none
+ */
+ private TableAlias getTableAliasForPath(String aPath, String aUserAlias, String anOriginalPath)
+ {
+ if (aUserAlias == null || !anOriginalPath.equals(aPath))
+ {
+ return getTableAliasForPath(aPath);
+ }
+ else
+ {
+ return getTableAliasForPath(aUserAlias);
+ }
+ }
+
+ /**
* answer the ClassDescriptor for itemClass for an ObjectReferenceDescriptor
* check optional hint;
*/
@@ -827,7 +938,7 @@
{
buf.append(",");
}
- appendColName(cf.name, true, buf);
+ appendColName(cf.name, true, null, buf);
}
}
@@ -971,7 +1082,7 @@
if (columnNumbers[i] == -1)
{
buf.append(",");
- appendColName(cf.name, true, buf);
+ appendColName(cf.name, true, null, buf);
columnNumbers[i] = existingColumns.size();
existingColumns.add(cf.name);
}
@@ -999,20 +1110,20 @@
SelectionCriteria c = (SelectionCriteria) o;
if (c.getAttribute() != null)
{
- buildJoinTreeForColumn(c.getAttribute());
+ buildJoinTreeForColumn(c.getAttribute(), crit.getAlias());
}
if (c instanceof FieldCriteria)
{
FieldCriteria cc = (FieldCriteria) c;
- buildJoinTreeForColumn((String) cc.getValue());
+ buildJoinTreeForColumn((String) cc.getValue(), crit.getAlias());
}
else if (c instanceof ColumnCriteria)
{
ColumnCriteria cc = (ColumnCriteria) c;
- buildJoinTreeForColumn((String) cc.getValue());
+ buildJoinTreeForColumn((String) cc.getValue(), crit.getAlias());
}
}
- } // while
+ }
}
/**
@@ -1020,14 +1131,14 @@
* functions and the last segment are removed
* ie: avg(accounts.amount) -> accounts
*/
- private void buildJoinTreeForColumn(String name)
+ private void buildJoinTreeForColumn(String aColName, String aUserAlias)
{
- String pathName = SqlHelper.cleanPath(name);
+ String pathName = SqlHelper.cleanPath(aColName);
int sepPos = pathName.lastIndexOf(".");
if (sepPos >= 0)
{
- getTableAlias(pathName.substring(0, sepPos), false);
+ getTableAlias(pathName.substring(0, sepPos), false, aUserAlias);
}
}
1.9 +2 -2 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.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- SqlSelectStatement.java 24 Dec 2002 12:57:51 -0000 1.8
+++ SqlSelectStatement.java 1 Mar 2003 20:19:56 -0000 1.9
@@ -143,7 +143,7 @@
{
buf.append(",");
}
- appendColName(columns[i], true, buf);
+ appendColName(columns[i], true, null, buf);
columnList.add(columns[i]);
}
return columnList;