KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mondrian > rolap > agg > AbstractQuerySpec


1 /*
2 // $Id: //open/mondrian/src/main/mondrian/rolap/agg/AbstractQuerySpec.java#11 $
3 // This software is subject to the terms of the Common Public License
4 // Agreement, available at the following URL:
5 // http://www.opensource.org/licenses/cpl.html.
6 // Copyright (C) 2005-2007 Julian Hyde and others
7 // All Rights Reserved.
8 // You must accept the terms of that agreement to use this software.
9 */

10
11 package mondrian.rolap.agg;
12
13 import mondrian.rolap.RolapStar;
14 import mondrian.rolap.StarColumnPredicate;
15 import mondrian.rolap.sql.SqlQuery;
16
17 /**
18  * Base class for {@link QuerySpec} implementations.
19  *
20  * @author jhyde
21  * @author Richard M. Emberson
22  * @version $Id: //open/mondrian/src/main/mondrian/rolap/agg/AbstractQuerySpec.java#11 $
23  */

24 public abstract class AbstractQuerySpec implements QuerySpec {
25     private final RolapStar star;
26
27     protected AbstractQuerySpec(final RolapStar star) {
28         this.star = star;
29     }
30
31     protected SqlQuery newSqlQuery() {
32         return getStar().getSqlQuery();
33     }
34
35     public RolapStar getStar() {
36         return star;
37     }
38
39     protected abstract void addMeasure(final int i, final SqlQuery sqlQuery);
40     protected abstract boolean isAggregate();
41
42     protected void nonDistinctGenerateSql(
43         final SqlQuery sqlQuery, boolean ordered, boolean countOnly)
44     {
45         // add constraining dimensions
46
RolapStar.Column[] columns = getColumns();
47         int arity = columns.length;
48         if (countOnly) {
49             sqlQuery.addSelect("count(*)");
50         }
51         for (int i = 0; i < arity; i++) {
52             RolapStar.Column column = columns[i];
53             RolapStar.Table table = column.getTable();
54             if (table.isFunky()) {
55                 // this is a funky dimension -- ignore for now
56
continue;
57             }
58             table.addToFrom(sqlQuery, false, true);
59
60             String JavaDoc expr = column.generateExprString(sqlQuery);
61
62             StarColumnPredicate predicate = getColumnPredicate(i);
63             final String JavaDoc where = RolapStar.Column.createInExpr(
64                 expr,
65                 predicate,
66                 column.getDatatype(),
67                 sqlQuery.getDialect());
68             if (!where.equals("true")) {
69                 sqlQuery.addWhere(where);
70             }
71
72             if (countOnly) {
73                 continue;
74             }
75
76             // some DB2 (AS400) versions throw an error, if a column alias is
77
// there and *not* used in a subsequent order by/group by
78
final SqlQuery.Dialect dialect = sqlQuery.getDialect();
79             if (dialect.isAS400()) {
80                 sqlQuery.addSelect(expr, null);
81             } else {
82                 sqlQuery.addSelect(expr, getColumnAlias(i));
83             }
84
85             if (isAggregate()) {
86                 sqlQuery.addGroupBy(expr);
87             }
88
89             // Add ORDER BY clause to make the results deterministic.
90
// Derby has a bug with ORDER BY, so ignore it.
91
if (ordered) {
92                 sqlQuery.addOrderBy(expr, true, false, false);
93             }
94         }
95
96         // add measures
97
if (!countOnly) {
98             for (int i = 0, count = getMeasureCount(); i < count; i++) {
99                 addMeasure(i, sqlQuery);
100             }
101         }
102     }
103 }
104
105
106 // End AbstractQuerySpec.java
107
Popular Tags