public String insertIntoCollapsed() {
StringWriter sw = new StringWriter(512);
PrintWriter pw = new PrintWriter(sw);
String prefix = " ";
String factTableName = getFactTableName();
SqlQuery sqlQuery = getSqlQuery();
pw.print("INSERT INTO ");
pw.print(makeCollapsedAggregateTableName(getFactTableName()));
pw.println(" (");
for (List<JdbcSchema.Table.Column.Usage> list
: collapsedColumnUsages.values())
{
for (JdbcSchema.Table.Column.Usage usage : list) {
JdbcSchema.Table.Column c = usage.getColumn();
pw.print(prefix);
if (usage.usagePrefix != null) {
pw.print(usage.usagePrefix);
}
pw.print(c.getName());
pw.println(',');
}
}
for (JdbcSchema.Table.Column.Usage usage : measures) {
JdbcSchema.Table.Column c = usage.getColumn();
pw.print(prefix);
String name = getUsageName(usage);
pw.print(name);
//pw.print(c.getName());
pw.println(',');
}
// do fact_count
pw.print(prefix);
pw.print(getFactCount());
pw.println(")");
pw.println("SELECT");
for (List<JdbcSchema.Table.Column.Usage> list
: collapsedColumnUsages.values())
{
for (JdbcSchema.Table.Column.Usage usage : list) {
JdbcSchema.Table.Column c = usage.getColumn();
JdbcSchema.Table t = c.getTable();
pw.print(prefix);
pw.print(
sqlQuery.getDialect().quoteIdentifier(
t.getName(),
c.getName()));
pw.print(" AS ");
String n = (usage.usagePrefix == null)
? c.getName() : usage.usagePrefix + c.getName();
pw.print(sqlQuery.getDialect().quoteIdentifier(n));
pw.println(',');
}
}
for (JdbcSchema.Table.Column.Usage usage : measures) {
JdbcSchema.Table.Column c = usage.getColumn();
JdbcSchema.Table t = c.getTable();
RolapAggregator agg = usage.getAggregator();
pw.print(prefix);
pw.print(
agg.getExpression(sqlQuery.getDialect().quoteIdentifier(
t.getName(), c.getName())));
pw.print(" AS ");
pw.print(sqlQuery.getDialect().quoteIdentifier(c.getName()));
pw.println(',');
}
// do fact_count
pw.print(prefix);
pw.print("COUNT(*) AS ");
pw.println(sqlQuery.getDialect().quoteIdentifier(getFactCount()));
pw.println("FROM ");
pw.print(prefix);
pw.print(sqlQuery.getDialect().quoteIdentifier(factTableName));
pw.print(" ");
pw.print(sqlQuery.getDialect().quoteIdentifier(factTableName));
pw.println(',');
// add dimension tables
int k = 0;
for (RolapStar.Table rt : collapsedColumnUsages.keySet()) {
if (k++ > 0) {
pw.println(',');
}
pw.print(prefix);
pw.print(sqlQuery.getDialect().quoteIdentifier(rt.getAlias()));
pw.print(" AS ");
pw.print(sqlQuery.getDialect().quoteIdentifier(rt.getAlias()));
// walk up tables
if (rt.getParentTable() != null) {
while (rt.getParentTable().getParentTable() != null) {
rt = rt.getParentTable();
pw.println(',');
pw.print(prefix);
pw.print(
sqlQuery.getDialect().quoteIdentifier(rt.getAlias()));
pw.print(" AS ");
pw.print(
sqlQuery.getDialect().quoteIdentifier(rt.getAlias()));
}
}
}
pw.println();
pw.println("WHERE ");
k = 0;
for (RolapStar.Table rt : collapsedColumnUsages.keySet()) {
if (k++ > 0) {
pw.println(" and");
}
RolapStar.Condition cond = rt.getJoinCondition();
if (cond == null) {
continue;
}
pw.print(prefix);
pw.print(cond.toString(sqlQuery));
if (rt.getParentTable() != null) {
while (rt.getParentTable().getParentTable() != null) {
rt = rt.getParentTable();
cond = rt.getJoinCondition();
pw.println(" and");
pw.print(prefix);
pw.print(cond.toString(sqlQuery));
}
}
}
pw.println();
pw.println("GROUP BY ");
k = 0;
for (List<JdbcSchema.Table.Column.Usage> list
: collapsedColumnUsages.values())
{
for (JdbcSchema.Table.Column.Usage usage : list) {
if (k++ > 0) {
pw.println(",");
}
JdbcSchema.Table.Column c = usage.getColumn();
JdbcSchema.Table t = c.getTable();
String n = (usage.usagePrefix == null)
? c.getName() : usage.usagePrefix + c.getName();
pw.print(prefix);
pw.print(sqlQuery.getDialect().quoteIdentifier(t.getName(), n));
}
}
pw.println(';');
return sw.toString();