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    public String getSelectType()
81    {
82      if(hasFlag(DISTINCT_SELECT_TYPE)) {
83        return "DISTINCT";
84      }
85  
86      if(hasFlag(DISTINCT_ROW_SELECT_TYPE)) {
87        return "DISTINCTROW";
88      }
89  
90      if(hasFlag(TOP_SELECT_TYPE)) {
91        StringBuilder builder = new StringBuilder();
92        builder.append("TOP ").append(getFlagRow().name1);
93        if(hasFlag(PERCENT_SELECT_TYPE)) {
94          builder.append(" PERCENT");
95        }
96        return builder.toString();
97      }
98  
99      return DEFAULT_TYPE;
100   }
101 
102   public List<String> getSelectColumns() 
103   {
104     List<String> result = (new RowFormatter(getColumnRows()) {
105         @Override protected void format(StringBuilder builder, Row row) {
106           // note column expression are always quoted appropriately
107           builder.append(row.expression);
108           toAlias(builder, row.name1);
109         }
110       }).format();
111     if(hasFlag(SELECT_STAR_SELECT_TYPE)) {
112       result.add("*");
113     }
114     return result;
115   }
116 
117   protected void toSelectInto(StringBuilder builder)
118   {
119     // base does nothing
120   }
121 
122   @Override
123   public List<String> getFromTables() 
124   {
125     return super.getFromTables();
126   }
127 
128   @Override
129   public String getFromRemoteDbPath() 
130   {
131     return super.getFromRemoteDbPath();
132   }
133 
134   @Override
135   public String getFromRemoteDbType() 
136   {
137     return super.getFromRemoteDbType();
138   }
139 
140   @Override
141   public String getWhereExpression()
142   {
143     return super.getWhereExpression();
144   }
145 
146   public List<String> getGroupings() 
147   {
148     return (new RowFormatter(getGroupByRows()) {
149         @Override protected void format(StringBuilder builder, Row row) {
150           builder.append(row.expression);
151         }
152       }).format();
153   }
154 
155   public String getHavingExpression()
156   {
157     return getHavingRow().expression;
158   }
159 
160   @Override
161   public List<String> getOrderings() 
162   {
163     return super.getOrderings();
164   }
165   
166 }