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.BaseSelectQuery;
23  
24  
25  /**
26   * Base class for queries which represent some form of SELECT statement.
27   * 
28   * @author James Ahlborn
29   */
30  public abstract class BaseSelectQueryImpl extends QueryImpl 
31    implements BaseSelectQuery
32  {
33  
34    protected BaseSelectQueryImpl(String name, List<Row> rows, int objectId, 
35                                  int objectFlag, Type type) {
36      super(name, rows, objectId, objectFlag, type);
37    }
38  
39    protected void toSQLSelectString(StringBuilder builder,
40                                     boolean useSelectPrefix) 
41    {
42      if(useSelectPrefix) {
43        builder.append("SELECT ");
44        String selectType = getSelectType();
45        if(!DEFAULT_TYPE.equals(selectType)) {
46          builder.append(selectType).append(' ');
47        }
48      }
49  
50      builder.append(getSelectColumns());
51      toSelectInto(builder);
52  
53      List<String> fromTables = getFromTables();
54      if(!fromTables.isEmpty()) {
55        builder.append(NEWLINE).append("FROM ").append(fromTables);
56        toRemoteDb(builder, getFromRemoteDbPath(), getFromRemoteDbType());
57      }
58  
59      String whereExpr = getWhereExpression();
60      if(whereExpr != null) {
61        builder.append(NEWLINE).append("WHERE ").append(whereExpr);
62      }
63  
64      List<String> groupings = getGroupings();
65      if(!groupings.isEmpty()) {
66        builder.append(NEWLINE).append("GROUP BY ").append(groupings);
67      }
68  
69      String havingExpr = getHavingExpression();
70      if(havingExpr != null) {
71        builder.append(NEWLINE).append("HAVING ").append(havingExpr);
72      }
73  
74      List<String> orderings = getOrderings();
75      if(!orderings.isEmpty()) {
76        builder.append(NEWLINE).append("ORDER BY ").append(orderings);
77      }
78    }
79  
80    @Override
81    public String getSelectType()
82    {
83      if(hasFlag(DISTINCT_SELECT_TYPE)) {
84        return "DISTINCT";
85      }
86  
87      if(hasFlag(DISTINCT_ROW_SELECT_TYPE)) {
88        return "DISTINCTROW";
89      }
90  
91      if(hasFlag(TOP_SELECT_TYPE)) {
92        StringBuilder builder = new StringBuilder();
93        builder.append("TOP ").append(getFlagRow().name1);
94        if(hasFlag(PERCENT_SELECT_TYPE)) {
95          builder.append(" PERCENT");
96        }
97        return builder.toString();
98      }
99  
100     return DEFAULT_TYPE;
101   }
102 
103   @Override
104   public List<String> getSelectColumns() 
105   {
106     List<String> result = (new RowFormatter(getColumnRows()) {
107         @Override protected void format(StringBuilder builder, Row row) {
108           // note column expression are always quoted appropriately
109           builder.append(row.expression);
110           toAlias(builder, row.name1);
111         }
112       }).format();
113     if(hasFlag(SELECT_STAR_SELECT_TYPE)) {
114       result.add("*");
115     }
116     return result;
117   }
118 
119   protected void toSelectInto(StringBuilder builder)
120   {
121     // base does nothing
122   }
123 
124   @Override
125   public List<String> getFromTables() 
126   {
127     return super.getFromTables();
128   }
129 
130   @Override
131   public String getFromRemoteDbPath() 
132   {
133     return super.getFromRemoteDbPath();
134   }
135 
136   @Override
137   public String getFromRemoteDbType() 
138   {
139     return super.getFromRemoteDbType();
140   }
141 
142   @Override
143   public String getWhereExpression()
144   {
145     return super.getWhereExpression();
146   }
147 
148   @Override
149   public List<String> getGroupings() 
150   {
151     return (new RowFormatter(getGroupByRows()) {
152         @Override protected void format(StringBuilder builder, Row row) {
153           builder.append(row.expression);
154         }
155       }).format();
156   }
157 
158   @Override
159   public String getHavingExpression()
160   {
161     return getHavingRow().expression;
162   }
163 
164   @Override
165   public List<String> getOrderings() 
166   {
167     return super.getOrderings();
168   }
169   
170 }