View Javadoc
1   /*
2   Copyright (c) 2008 Health Market Science, Inc.
3   
4   Licensed under the Apache License, Version 2.0 (the "License");
5   you may not use this file except in compliance with the License.
6   You may obtain a copy of the License at
7   
8       http://www.apache.org/licenses/LICENSE-2.0
9   
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15  */
16  
17  package com.healthmarketscience.jackcess.impl.query;
18  
19  import java.util.List;
20  
21  import static com.healthmarketscience.jackcess.impl.query.QueryFormat.*;
22  import com.healthmarketscience.jackcess.query.CrossTabQuery;
23  
24  
25  /**
26   * Concrete Query subclass which represents a crosstab/pivot query, e.g.:
27   * {@code TRANSFORM <expr> SELECT <query> PIVOT <expr>}
28   * 
29   * @author James Ahlborn
30   */
31  public class CrossTabQueryImpl extends BaseSelectQueryImpl 
32    implements CrossTabQuery
33  {
34  
35    public CrossTabQueryImpl(String name, List<Row> rows, int objectId, 
36                             int objectFlag) {
37      super(name, rows, objectId, objectFlag, Type.CROSS_TAB);
38    }
39  
40    protected Row getTransformRow() {
41      return getUniqueRow(
42          filterRowsByNotFlag(super.getColumnRows(), 
43                              (short)(CROSSTAB_PIVOT_FLAG | 
44                                      CROSSTAB_NORMAL_FLAG)));
45    }
46  
47    @Override
48    protected List<Row> getColumnRows() {
49      return filterRowsByFlag(super.getColumnRows(), CROSSTAB_NORMAL_FLAG);
50    }
51  
52    @Override
53    protected List<Row> getGroupByRows() {
54      return filterRowsByFlag(super.getGroupByRows(), CROSSTAB_NORMAL_FLAG);
55    }
56  
57    protected Row getPivotRow() {
58      return getUniqueRow(filterRowsByFlag(super.getColumnRows(), 
59                                           CROSSTAB_PIVOT_FLAG));
60    }
61  
62    @Override
63    public String getTransformExpression() {
64      Row row = getTransformRow();
65      if(row.expression == null) {
66        return null;
67      }
68      // note column expression are always quoted appropriately
69      StringBuilder builder = new StringBuilder(row.expression);
70      return toAlias(builder, row.name1).toString();
71    }
72  
73    @Override
74    public String getPivotExpression() {
75      return getPivotRow().expression;
76    }
77  
78    /**
79     * Ms access stores the select type of a crosstab query (a stored TOP still
80     * applies when the query runs), but does not write it in the sql text.
81     */
82    @Override
83    protected void appendSelectType(StringBuilder builder)
84    {
85      // nothing to write
86    }
87  
88    @Override
89    protected void toSQLString(StringBuilder builder)
90    {
91      String transformExpr = getTransformExpression();
92      if(transformExpr != null) {
93        builder.append("TRANSFORM ").append(transformExpr).append(NEWLINE);
94      }
95  
96      toSQLSelectString(builder, true);
97  
98      builder.append(NEWLINE).append("PIVOT ")
99        .append(getPivotExpression());
100   }
101 
102 }