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    public String getTransformExpression() {
63      Row row = getTransformRow();
64      if(row.expression == null) {
65        return null;
66      }
67      // note column expression are always quoted appropriately
68      StringBuilder builder = new StringBuilder(row.expression);
69      return toAlias(builder, row.name1).toString();
70    }
71  
72    public String getPivotExpression() {
73      return getPivotRow().expression;
74    }
75  
76    @Override
77    protected void toSQLString(StringBuilder builder)
78    {
79      String transformExpr = getTransformExpression();
80      if(transformExpr != null) {
81        builder.append("TRANSFORM ").append(transformExpr).append(NEWLINE);
82      }
83  
84      toSQLSelectString(builder, true);
85  
86      builder.append(NEWLINE).append("PIVOT ")
87        .append(getPivotExpression());
88    }
89  
90  }