Skip to content

Commit

Permalink
MONDRIAN: avoid duplicate WHERE-clauses when
Browse files Browse the repository at this point in the history
"members in level" are gotten for a "join".

[git-p4: depot-paths = "//open/mondrian/": change = 2733]
  • Loading branch information
hhaas committed Sep 27, 2004
1 parent c4f3212 commit e49c0d0
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions src/main/mondrian/rolap/sql/SqlQuery.java
Expand Up @@ -301,16 +301,17 @@ public String chooseQuery(MondrianDef.SQL[] sqls) {

/**
* @pre alias != null
* @return true if query *was* added
*/
public void addFromQuery(
public boolean addFromQuery(
String query, String alias, boolean failIfExists) {
Util.assertPrecondition(alias != null);
if (fromAliases.contains(alias)) {
if (failIfExists) {
throw Util.newInternal(
"query already contains alias '" + alias + "'");
} else {
return;
return false;
}
}
if (fromCount++ == 0) {
Expand All @@ -331,6 +332,7 @@ public void addFromQuery(
from.append(quoteIdentifier(alias));
fromAliases.add(alias);
}
return true;
}

/**
Expand All @@ -341,15 +343,16 @@ public void addFromQuery(
* @param alias table alias, may not be null
*
* @pre alias != null
* @return true if table *was* added
*/
private void addFromTable(
private boolean addFromTable(
String schema, String table, String alias, boolean failIfExists) {
if (fromAliases.contains(alias)) {
if (failIfExists) {
throw Util.newInternal(
"query already contains alias '" + alias + "'");
} else {
return;
return false;
}
}
if (fromCount++ == 0) {
Expand All @@ -368,33 +371,41 @@ private void addFromTable(
from.append(quoteIdentifier(alias));
fromAliases.add(alias);
}
return true;
}

public void addFrom(SqlQuery sqlQuery, String alias, boolean failIfExists)
{
addFromQuery(sqlQuery.toString(), alias, failIfExists);
}

public void addFrom(MondrianDef.Relation relation, boolean failIfExists) {
/**
* @return true, if relation *was* added to query
*/
public boolean addFrom(MondrianDef.Relation relation, boolean failIfExists) {
if (relation instanceof MondrianDef.View) {
MondrianDef.View view = (MondrianDef.View) relation;
String sqlString = chooseQuery(view.selects);
String alias = view.alias;
if (!fromAliases.contains(alias)) {
addFromQuery(sqlString, alias, failIfExists);
return addFromQuery(sqlString, alias, failIfExists);
}
return false;
} else if (relation instanceof MondrianDef.Table) {
MondrianDef.Table table = (MondrianDef.Table) relation;
addFromTable(
return addFromTable(
table.schema, table.name, table.getAlias(), failIfExists);
} else if (relation instanceof MondrianDef.Join) {
MondrianDef.Join join = (MondrianDef.Join) relation;
addFrom(join.left, failIfExists);
addFrom(join.right, failIfExists);
addWhere(
boolean added = false;
added = added || addFrom(join.left, failIfExists);
added = added || addFrom(join.right, failIfExists);
if (added)
addWhere(
quoteIdentifier(join.getLeftAlias(), join.leftKey) +
" = " +
quoteIdentifier(join.getRightAlias(), join.rightKey));
return added;
} else {
throw Util.newInternal("bad relation type " + relation);
}
Expand Down

0 comments on commit e49c0d0

Please sign in to comment.