Select.h
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file terralib/dataaccess/query/Select.h
22 
23  \brief A Select models a query to be used when retrieving data from a data source.
24 */
25 
26 #ifndef __TERRALIB_DATAACCESS_INTERNAL_SELECT_H
27 #define __TERRALIB_DATAACCESS_INTERNAL_SELECT_H
28 
29 // TerraLib
30 #include "../Enums.h"
31 #include "Distinct.h"
32 #include "Fields.h"
33 #include "From.h"
34 #include "GroupBy.h"
35 #include "OrderBy.h"
36 #include "Query.h"
37 
38 // STL
39 #include <memory>
40 #include <string>
41 
42 namespace te
43 {
44  namespace da
45  {
46 // Forward declaration
47  class DataSetName;
48  class Field;
49  class GroupByItem;
50  class Having;
51  class OrderByItem;
52  class PropertyName;
53  class Where;
54 
55  /*!
56  \class Select
57 
58  \brief A Select models a query to be used when retrieving data from a DataSource.
59 
60  \ingroup dataaccess
61 
62  \sa Query, Fields, From, Expression, GroupBy, Expression, OrderBy, Distinct
63 
64  \todo Ainda falta incluir a possibilidade de construir consultas com operacoes de INTERSECT/UNION/EXCEPT e etc...
65  */
67  {
68  public:
69 
71 
72  /*! \brief Default constructor. */
73  Select();
74 
75  Select(Fields* fds, From* f = 0, Where* w = 0, OrderBy* o = 0);
76 
77  Select(const Fields& fds);
78 
79  Select(const Fields& fds, const From& f);
80 
81  Select(const Fields& fds, const From& f, const Where& w);
82 
83  Select(const Fields& fds, const From& f, const Where& w, const OrderBy& o);
84 
85  Select(Fields* fds, From* f, OrderBy* o);
86 
87  Select(const Fields& fds, const From& f, const OrderBy& o);
88 
89  Select(Fields* fds, From* f, Where* w, GroupBy* gb, OrderBy* o = 0);
90 
91  Select(const Fields& fds, const From& f, const Where& w, const GroupBy& gb);
92 
93  Select(const Fields& fds, const From& f, const Where& w, const GroupBy& gb, const OrderBy& o);
94 
95  Select(Fields* fds, From* f, GroupBy* gb, OrderBy* o = 0);
96 
97  Select(const Fields& fds, const From& f, const GroupBy& gb);
98 
99  Select(const Fields& fds, const From& f, const GroupBy& gb, const OrderBy& o);
100 
101  Select(Fields* fds, From* f, Where* w, GroupBy* gb, Having* h, OrderBy* o = 0);
102 
103  Select(const Fields& fds, const From& f, const Where& w, const GroupBy& gb, const Having& h);
104 
105  Select(const Fields& fds, const From& f, const Where& w, const GroupBy& gb, const Having& h, const OrderBy& o);
106 
107  Select(Field* f);
108 
109  Select(const Field& f);
110 
111  Select(const std::string& propertyName);
112 
113  Select(const std::string& propertyName, const std::string& alias);
114 
115  Select(const Select& rhs);
116 
117  Select(const Select* rhs);
118 
119  /*! \brief Destructor. */
120  ~Select();
121 
122  /*! Assignment operator. */
123  Select& operator=(const Select& rhs);
124 
125  /*! \brief It creates a new copy of this query. */
126  Query* clone() const;
127 
128 // fields
129  Select& fields(const Fields& f);
130 
131  Select& fields(Fields* f);
132 
133  Fields& fields();
134 
135  const Fields& fields() const;
136 
137  Select& operator()(const Field& f);
138 
139  Select& operator()(Field* f);
140 
141  Select& operator()(const std::string& propertyName);
142 
143  Select& operator()(const std::string& propertyName, const std::string& alias);
144 
145 // from / join
146  Select& from(const FromItem& item);
147 
148  Select& from(FromItem* item);
149 
150  Select& from(const FromItem& i1, const FromItem& i2);
151 
152  Select& from(FromItem* i1, FromItem* i2);
153 
154  Select& from(const FromItem& i1, const FromItem& i2, const FromItem& i3);
155 
156  Select& from(FromItem* i1, FromItem* i2, FromItem* i3);
157 
158  Select& from(const std::string& datasetName);
159 
160  Select& from(From* f);
161 
162  Select& from(const From& rhs);
163 
164  From& from();
165 
166  const From& from() const;
167 
169 
170  Select& Join(const FromItem& d2, JoinType t, const JoinCondition& c);
171 
172  Select& InnerJoin(FromItem* d2, JoinCondition* c);
173 
174  Select& InnerJoin(const FromItem& d2, const JoinCondition& c);
175 
176  Select& LeftJoin(FromItem* d2, JoinCondition* c);
177 
178  Select& LeftJoin(const FromItem& d2, const JoinCondition& c);
179 
180  Select& RightJoin(FromItem* d2, JoinCondition* c);
181 
182  Select& RightJoin(const FromItem& d2, const JoinCondition& c);
183 
184  Select& FullOuterJoin(FromItem* d2, JoinCondition* c);
185 
186  Select& FullOuterJoin(const FromItem& d2, const JoinCondition& c);
187 
188  Select& CrossJoin(FromItem* d2);
189 
190  Select& CrossJoin(const FromItem& d2);
191 
192  Select& NaturalJoin(FromItem* d2, JoinType t);
193 
194  Select& NaturalJoin(const FromItem& d2, JoinType t);
195 
196 // where
197  Select& where(Expression* e);
198 
199  Select& where(const Expression& e);
200 
201  Select& where(Where* w);
202 
203  Select& where(const Where& rhs);
204 
205  Where& where();
206 
207  const Where& where() const;
208 
209 // group by
210  Select& groupBy(const GroupByItem& item);
211 
212  Select& groupBy(GroupByItem* item);
213 
214  Select& groupBy(const Expression& e);
215 
216  Select& groupBy(const std::string& propertyName);
217 
218  Select& groupBy(GroupBy* gb);
219 
220  Select& groupBy(const GroupBy& rhs);
221 
222  GroupBy& groupBy();
223 
224  const GroupBy& groupBy() const;
225 
226 // having
227 
228  Select& having(Expression* e);
229 
230  Select& having(const Expression& e);
231 
232  Select& having(Having* h);
233 
234  Select& having(const Having& rhs);
235 
236  Having& having();
237 
238  const Having& having() const;
239 
240 // order by
241  Select& orderBy(const OrderByItem& item);
242 
243  Select& orderBy(OrderByItem* item);
244 
245  Select& orderBy(const Expression& e, SortOrder o = ASC);
246 
247  Select& orderBy(const std::string& propertyName, SortOrder o = ASC);
248 
249  Select& orderBy(OrderBy* o);
250 
251  Select& orderBy(const OrderBy& rhs);
252 
253  OrderBy& orderBy();
254 
255  const OrderBy& orderBy() const;
256 
257 // distinct
258  Select& distinct(Expression* e);
259 
260  Select& distinct(const Expression& e);
261 
262  Select& distinct(const std::string& propertyName);
263 
264  Select& distinct(Distinct* d);
265 
266  Select& distinct(const Distinct& rhs);
267 
268  Distinct& distinct();
269 
270  const Distinct& distinct() const;
271 
272 // limit and offset
273  Select& limit(std::size_t l);
274 
275  Select& offset(std::size_t i);
276 
277  /*!
278  \brief It sets the list of output expressions used to form the result set.
279 
280  \param f The list of output expressions used to form the result set.
281 
282  \note Select will take the ownership of Fields.
283  */
284  void setFields(Fields* f);
285 
286  /*!
287  \brief It returns the list of output expressions used to form the result set.
288 
289  \return The list of output expressions used to form the result set.
290  */
291  const Fields* getFields() const;
292 
293  /*!
294  \brief It sets the list of source information.
295 
296  \param f The list of source information.
297 
298  \note Select will take the ownership of From.
299  */
300  void setFrom(From* f);
301 
302  /*!
303  \brief It returns the list of source information to be used by the query.
304 
305  \return The list of source information to be used by the query.
306  */
307  const From* getFrom() const;
308 
309  /*!
310  \brief It sets the filter codition.
311 
312  \param w The filter condition.
313 
314  \note Select will take the ownership of Where.
315  */
316  void setWhere(Where* w);
317 
318  /*!
319  \brief It returns the filter condition.
320 
321  \return The filter condition.
322  */
323  Where* getWhere() const;
324 
325  /*!
326  \brief It sets the list of expressions used to condense the result set.
327 
328  \param g The list of expressions used to condense the result set.
329 
330  \note Select will take the ownership of GroupBy.
331  */
332  void setGroupBy(GroupBy* g);
333 
334  /*!
335  \brief It returns the list of expressions used to condense the result set.
336 
337  \return The list of expressions used to condense the result set.
338  */
339  const GroupBy* getGroupBy() const;
340 
341  /*!
342  \brief It sets the list of expressions used to eliminate group row that doesn't satisfy the condition.
343 
344  \param h The list of expressions used to eliminate group row that doesn't satisfy the condition.
345 
346  \note Select will take the ownership of Having.
347  */
348  void setHaving(Having* h);
349 
350  /*!
351  \brief It returns the list of expressions used to eliminate group row that doesn't satisfy the condition.
352 
353  \return The list of expressions used to eliminate group row that doesn't satisfy the condition.
354  */
355  const Having* getHaving() const;
356 
357  /*!
358  \brief It sets the list of expressions used to sort the output result.
359 
360  \param o The list of expressions used to sort the output result.
361 
362  \note Select will take the ownership of OrderBy.
363  */
364  void setOrderBy(OrderBy* o);
365 
366  /*!
367  \brief It returns the list of expressions used to sort the output result.
368 
369  \return The list of expressions used to sort the output result.
370  */
371  const OrderBy* getOrderBy() const;
372 
373  /*!
374  \brief If Distinct is specified, all duplicate rows are removed from the result set (one row is kept from each group of duplicates).
375 
376  \param d The Distinct modifier.
377 
378  \note Select will take the ownership of Distinct.
379  */
380  void setDistinct(Distinct* d);
381 
382  /*!
383  \brief It returns the Distinct modifier.
384 
385  \return The Distinct modifier.
386  */
387  const Distinct* getDistinct() const;
388 
389  /*!
390  \brief It specifies the maximum number of rows to return.
391 
392  \param m The maximum number of rows to return.
393  */
394  void setLimit(std::size_t m);
395 
396  /*!
397  \brief It tells the maximum number of rows to return.
398 
399  \return The maximum number of rows to return.
400  */
401  std::size_t getLimit() const;
402 
403  /*!
404  \brief It specifies the number of rows to skip before starting to return rows.
405 
406  \param o The number of rows to skip before starting to return rows.
407  */
408  void setOffset(std::size_t o);
409 
410  /*!
411  \brief It tells the number of rows to skip before starting to return rows.
412 
413  \return The number of rows to skip before starting to return rows.
414  */
415  std::size_t getOffset() const;
416 
417  Select& operator+(const te::da::From& f);
418 
419  Select& operator+(te::da::From* f);
420 
421  Select& operator+(const te::da::Where& w) ;
422 
423  Select& operator+(te::da::Where* w) ;
424 
425  Select& operator+(const te::da::GroupBy& g);
426 
427  Select& operator+(te::da::GroupBy* g);
428 
429  Select& operator+(const te::da::Having& h);
430 
431  Select& operator+(te::da::Having* h);
432 
433  Select& operator+(const te::da::OrderBy& o);
434 
435  Select& operator+(te::da::OrderBy* o);
436 
437  private:
438 
439  std::unique_ptr<Fields> m_fields; //!< .
440  std::unique_ptr<From> m_from; //!< .
441  std::unique_ptr<Where> m_where; //!< .
442  std::unique_ptr<GroupBy> m_groupBy; //!< .
443  std::unique_ptr<Having> m_having; //!< .
444  std::unique_ptr<OrderBy> m_orderBy; //!< .
445  std::unique_ptr<Distinct> m_distinct; //!< .
446  std::size_t m_limit; //!< .
447  std::size_t m_offset; //!< .
448  };
449 
450  } // end namespace da
451 } // end namespace te
452 
453 #endif // __TERRALIB_DATAACCESS_INTERNAL_SELECT_H
454 
A class that can be used to model a filter expression that can be applied to a query.
Definition: Having.h:47
std::unique_ptr< Having > m_having
Definition: Select.h:443
boost::ptr_vector< GroupByItem > GroupBy
A class that can be used to model a GROUP BY clause.
Definition: GroupBy.h:37
A class that can be used in a GROUP BY clause.
Definition: GroupByItem.h:50
An abstract class that models a source of data in a query.
Definition: FromItem.h:50
The Field class can be used to model an expression that takes part of the output items of a SELECT...
Definition: Field.h:50
std::unique_ptr< Distinct > m_distinct
Definition: Select.h:445
boost::ptr_vector< Expression > Distinct
A class that models a Distinct clause on a query.
Definition: Distinct.h:37
A class that models a Distinct clause on a query.
boost::ptr_vector< OrderByItem > OrderBy
A class that can be used to model an ORDER BY clause.
Definition: OrderBy.h:37
std::size_t m_limit
Definition: Select.h:446
The base class for queries.
This is an abstract class that models a query expression.
Definition: Expression.h:47
std::size_t m_offset
Definition: Select.h:447
TerraLib.
A condition to be used in a Join clause.
Definition: JoinCondition.h:44
boost::ptr_vector< Field > Fields
Fields is just a boost::ptr_vector of Field pointers.
Definition: Fields.h:37
JoinType
The type of join in a query.
Definition: Enums.h:49
A class that can be used to model a filter expression that can be applied to a query.
Definition: Where.h:47
A Join clause combines two FromItems.
Definition: Join.h:50
SortOrder
Sort order type: asc or desc.
Definition: Enums.h:38
#define TE_DEFINE_VISITABLE
Definition: BaseVisitable.h:75
std::unique_ptr< Fields > m_fields
Definition: Select.h:439
A Select models a query to be used when retrieving data from a DataSource.
Definition: Select.h:66
A class that can be used to model a GROUP BY clause.
boost::ptr_vector< FromItem > From
It models the FROM clause for a query.
Definition: From.h:37
std::unique_ptr< From > m_from
Definition: Select.h:440
std::unique_ptr< OrderBy > m_orderBy
Definition: Select.h:444
std::unique_ptr< Where > m_where
Definition: Select.h:441
A class that can be used to model an ORDER BY clause.
It models the FROM clause for a query.
The Fields class can be used to model a set of expressions that form the output items of a SELECT...
#define TEDATAACCESSEXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:97
A class that can be used in an ORDER BY clause to sort the items of a resulting query.
Definition: OrderByItem.h:53
std::unique_ptr< GroupBy > m_groupBy
Definition: Select.h:442
A Query is independent from the data source language/dialect.
Definition: Query.h:46