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        appendSelectType(builder);
45      }
46  
47      builder.append(getSelectColumns());
48      toSelectInto(builder);
49  
50      List<String> fromTables = getFromTables();
51      if(!fromTables.isEmpty()) {
52        builder.append(NEWLINE).append("FROM ").append(fromTables);
53        toRemoteDb(builder, getFromRemoteDbPath(), getFromRemoteDbType());
54      }
55  
56      String whereExpr = getWhereExpression();
57      if(whereExpr != null) {
58        builder.append(NEWLINE).append("WHERE ").append(whereExpr);
59      }
60  
61      List<String> groupings = getGroupings();
62      if(!groupings.isEmpty()) {
63        builder.append(NEWLINE).append("GROUP BY ").append(groupings);
64      }
65  
66      String havingExpr = getHavingExpression();
67      if(havingExpr != null) {
68        builder.append(NEWLINE).append("HAVING ").append(havingExpr);
69      }
70  
71      List<String> orderings = getOrderings();
72      if(!orderings.isEmpty()) {
73        builder.append(NEWLINE).append("ORDER BY ").append(orderings);
74      }
75    }
76  
77    @Override
78    public List<String> getSelectColumns() 
79    {
80      List<String> result = (new RowFormatter(getColumnRows()) {
81          @Override protected void format(StringBuilder builder, Row row) {
82            // note column expression are always quoted appropriately
83            builder.append(row.expression);
84            toAlias(builder, row.name1);
85          }
86        }).format();
87      if(hasFlag(SELECT_STAR_SELECT_TYPE)) {
88        result.add("*");
89      }
90      return result;
91    }
92  
93    protected void toSelectInto(StringBuilder builder)
94    {
95      // base does nothing
96    }
97  
98    @Override
99    public List<String> getFromTables() 
100   {
101     return super.getFromTables();
102   }
103 
104   @Override
105   public String getFromRemoteDbPath() 
106   {
107     return super.getFromRemoteDbPath();
108   }
109 
110   @Override
111   public String getFromRemoteDbType() 
112   {
113     return super.getFromRemoteDbType();
114   }
115 
116   @Override
117   public String getWhereExpression()
118   {
119     return super.getWhereExpression();
120   }
121 
122   @Override
123   public List<String> getGroupings() 
124   {
125     return (new RowFormatter(getGroupByRows()) {
126         @Override protected void format(StringBuilder builder, Row row) {
127           builder.append(row.expression);
128         }
129       }).format();
130   }
131 
132   @Override
133   public String getHavingExpression()
134   {
135     return getHavingRow().expression;
136   }
137 
138   @Override
139   public List<String> getOrderings() 
140   {
141     return super.getOrderings();
142   }
143   
144 }