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 
41 namespace te
42 {
43  namespace da
44  {
45 // Forward declaration
46  class DataSetName;
47  class Field;
48  class GroupByItem;
49  class Having;
50  class OrderByItem;
51  class PropertyName;
52  class Where;
53 
54  /*!
55  \class Select
56 
57  \brief A Select models a query to be used when retrieving data from a DataSource.
58 
59  \ingroup dataaccess
60 
61  \sa Query, Fields, From, Expression, GroupBy, Expression, OrderBy, Distinct
62 
63  \todo Ainda falta incluir a possibilidade de construir consultas com operacoes de INTERSECT/UNION/EXCEPT e etc...
64  */
66  {
67  public:
68 
70 
71  /*! \brief Default constructor. */
72  Select();
73 
74  Select(Fields* fds, From* f = 0, Where* w = 0, OrderBy* o = 0);
75 
76  Select(const Fields& fds);
77 
78  Select(const Fields& fds, const From& f);
79 
80  Select(const Fields& fds, const From& f, const Where& w);
81 
82  Select(const Fields& fds, const From& f, const Where& w, const OrderBy& o);
83 
84  Select(Fields* fds, From* f, OrderBy* o);
85 
86  Select(const Fields& fds, const From& f, const OrderBy& o);
87 
88  Select(Fields* fds, From* f, Where* w, GroupBy* gb, OrderBy* o = 0);
89 
90  Select(const Fields& fds, const From& f, const Where& w, const GroupBy& gb);
91 
92  Select(const Fields& fds, const From& f, const Where& w, const GroupBy& gb, const OrderBy& o);
93 
94  Select(Fields* fds, From* f, GroupBy* gb, OrderBy* o = 0);
95 
96  Select(const Fields& fds, const From& f, const GroupBy& gb);
97 
98  Select(const Fields& fds, const From& f, const GroupBy& gb, const OrderBy& o);
99 
100  Select(Fields* fds, From* f, Where* w, GroupBy* gb, Having* h, OrderBy* o = 0);
101 
102  Select(const Fields& fds, const From& f, const Where& w, const GroupBy& gb, const Having& h);
103 
104  Select(const Fields& fds, const From& f, const Where& w, const GroupBy& gb, const Having& h, const OrderBy& o);
105 
106  Select(Field* f);
107 
108  Select(const Field& f);
109 
110  Select(const std::string& propertyName);
111 
112  Select(const std::string& propertyName, const std::string& alias);
113 
114  Select(const Select& rhs);
115 
116  Select(const Select* rhs);
117 
118  /*! \brief Destructor. */
119  ~Select();
120 
121  /*! Assignment operator. */
122  Select& operator=(const Select& rhs);
123 
124  /*! \brief It creates a new copy of this query. */
125  Query* clone() const;
126 
127 // fields
128  Select& fields(const Fields& f);
129 
130  Select& fields(Fields* f);
131 
132  Fields& fields();
133 
134  const Fields& fields() const;
135 
136  Select& operator()(const Field& f);
137 
138  Select& operator()(Field* f);
139 
140  Select& operator()(const std::string& propertyName);
141 
142  Select& operator()(const std::string& propertyName, const std::string& alias);
143 
144 // from / join
145  Select& from(const FromItem& item);
146 
147  Select& from(FromItem* item);
148 
149  Select& from(const FromItem& i1, const FromItem& i2);
150 
151  Select& from(FromItem* i1, FromItem* i2);
152 
153  Select& from(const FromItem& i1, const FromItem& i2, const FromItem& i3);
154 
155  Select& from(FromItem* i1, FromItem* i2, FromItem* i3);
156 
157  Select& from(const std::string& datasetName);
158 
159  Select& from(From* f);
160 
161  Select& from(const From& rhs);
162 
163  From& from();
164 
165  const From& from() const;
166 
167  Select& Join(FromItem* d2, JoinType t, JoinCondition* c);
168 
169  Select& Join(const FromItem& d2, JoinType t, const JoinCondition& c);
170 
171  Select& InnerJoin(FromItem* d2, JoinCondition* c);
172 
173  Select& InnerJoin(const FromItem& d2, const JoinCondition& c);
174 
175  Select& LeftJoin(FromItem* d2, JoinCondition* c);
176 
177  Select& LeftJoin(const FromItem& d2, const JoinCondition& c);
178 
179  Select& RightJoin(FromItem* d2, JoinCondition* c);
180 
181  Select& RightJoin(const FromItem& d2, const JoinCondition& c);
182 
183  Select& FullOuterJoin(FromItem* d2, JoinCondition* c);
184 
185  Select& FullOuterJoin(const FromItem& d2, const JoinCondition& c);
186 
187  Select& CrossJoin(FromItem* d2);
188 
189  Select& CrossJoin(const FromItem& d2);
190 
191  Select& NaturalJoin(FromItem* d2, JoinType t);
192 
193  Select& NaturalJoin(const FromItem& d2, JoinType t);
194 
195 // where
196  Select& where(Expression* e);
197 
198  Select& where(const Expression& e);
199 
200  Select& where(Where* w);
201 
202  Select& where(const Where& rhs);
203 
204  Where& where();
205 
206  const Where& where() const;
207 
208 // group by
209  Select& groupBy(const GroupByItem& item);
210 
211  Select& groupBy(GroupByItem* item);
212 
213  Select& groupBy(const Expression& e);
214 
215  Select& groupBy(const std::string& propertyName);
216 
217  Select& groupBy(GroupBy* gb);
218 
219  Select& groupBy(const GroupBy& rhs);
220 
221  GroupBy& groupBy();
222 
223  const GroupBy& groupBy() const;
224 
225 // having
226 
227  Select& having(Expression* e);
228 
229  Select& having(const Expression& e);
230 
231  Select& having(Having* h);
232 
233  Select& having(const Having& rhs);
234 
235  Having& having();
236 
237  const Having& having() const;
238 
239 // order by
240  Select& orderBy(const OrderByItem& item);
241 
242  Select& orderBy(OrderByItem* item);
243 
244  Select& orderBy(const Expression& e, SortOrder o = ASC);
245 
246  Select& orderBy(const std::string& propertyName, SortOrder o = ASC);
247 
248  Select& orderBy(OrderBy* o);
249 
250  Select& orderBy(const OrderBy& rhs);
251 
252  OrderBy& orderBy();
253 
254  const OrderBy& orderBy() const;
255 
256 // distinct
257  Select& distinct(Expression* e);
258 
259  Select& distinct(const Expression& e);
260 
261  Select& distinct(const std::string& propertyName);
262 
263  Select& distinct(Distinct* d);
264 
265  Select& distinct(const Distinct& rhs);
266 
267  Distinct& distinct();
268 
269  const Distinct& distinct() const;
270 
271 // limit and offset
272  Select& limit(std::size_t l);
273 
274  Select& offset(std::size_t i);
275 
276  /*!
277  \brief It sets the list of output expressions used to form the result set.
278 
279  \param f The list of output expressions used to form the result set.
280 
281  \note Select will take the ownership of Fields.
282  */
283  void setFields(Fields* f);
284 
285  /*!
286  \brief It returns the list of output expressions used to form the result set.
287 
288  \return The list of output expressions used to form the result set.
289  */
290  const Fields* getFields() const;
291 
292  /*!
293  \brief It sets the list of source information.
294 
295  \param f The list of source information.
296 
297  \note Select will take the ownership of From.
298  */
299  void setFrom(From* f);
300 
301  /*!
302  \brief It returns the list of source information to be used by the query.
303 
304  \return The list of source information to be used by the query.
305  */
306  const From* getFrom() const;
307 
308  /*!
309  \brief It sets the filter codition.
310 
311  \param w The filter condition.
312 
313  \note Select will take the ownership of Where.
314  */
315  void setWhere(Where* w);
316 
317  /*!
318  \brief It returns the filter condition.
319 
320  \return The filter condition.
321  */
322  Where* getWhere() const;
323 
324  /*!
325  \brief It sets the list of expressions used to condense the result set.
326 
327  \param g The list of expressions used to condense the result set.
328 
329  \note Select will take the ownership of GroupBy.
330  */
331  void setGroupBy(GroupBy* g);
332 
333  /*!
334  \brief It returns the list of expressions used to condense the result set.
335 
336  \return The list of expressions used to condense the result set.
337  */
338  const GroupBy* getGroupBy() const;
339 
340  /*!
341  \brief It sets the list of expressions used to eliminate group row that doesn't satisfy the condition.
342 
343  \param h The list of expressions used to eliminate group row that doesn't satisfy the condition.
344 
345  \note Select will take the ownership of Having.
346  */
347  void setHaving(Having* h);
348 
349  /*!
350  \brief It returns the list of expressions used to eliminate group row that doesn't satisfy the condition.
351 
352  \return The list of expressions used to eliminate group row that doesn't satisfy the condition.
353  */
354  const Having* getHaving() const;
355 
356  /*!
357  \brief It sets the list of expressions used to sort the output result.
358 
359  \param o The list of expressions used to sort the output result.
360 
361  \note Select will take the ownership of OrderBy.
362  */
363  void setOrderBy(OrderBy* o);
364 
365  /*!
366  \brief It returns the list of expressions used to sort the output result.
367 
368  \return The list of expressions used to sort the output result.
369  */
370  const OrderBy* getOrderBy() const;
371 
372  /*!
373  \brief If Distinct is specified, all duplicate rows are removed from the result set (one row is kept from each group of duplicates).
374 
375  \param d The Distinct modifier.
376 
377  \note Select will take the ownership of Distinct.
378  */
379  void setDistinct(Distinct* d);
380 
381  /*!
382  \brief It returns the Distinct modifier.
383 
384  \return The Distinct modifier.
385  */
386  const Distinct* getDistinct() const;
387 
388  /*!
389  \brief It specifies the maximum number of rows to return.
390 
391  \param m The maximum number of rows to return.
392  */
393  void setLimit(std::size_t m);
394 
395  /*!
396  \brief It tells the maximum number of rows to return.
397 
398  \return The maximum number of rows to return.
399  */
400  std::size_t getLimit() const;
401 
402  /*!
403  \brief It specifies the number of rows to skip before starting to return rows.
404 
405  \param o The number of rows to skip before starting to return rows.
406  */
407  void setOffset(std::size_t o);
408 
409  /*!
410  \brief It tells the number of rows to skip before starting to return rows.
411 
412  \return The number of rows to skip before starting to return rows.
413  */
414  std::size_t getOffset() const;
415 
416  Select& operator+(const te::da::From& f);
417 
418  Select& operator+(te::da::From* f);
419 
420  Select& operator+(const te::da::Where& w) ;
421 
422  Select& operator+(te::da::Where* w) ;
423 
424  Select& operator+(const te::da::GroupBy& g);
425 
426  Select& operator+(te::da::GroupBy* g);
427 
428  Select& operator+(const te::da::Having& h);
429 
430  Select& operator+(te::da::Having* h);
431 
432  Select& operator+(const te::da::OrderBy& o);
433 
434  Select& operator+(te::da::OrderBy* o);
435 
436  private:
437 
438  std::auto_ptr<Fields> m_fields; //!< .
439  std::auto_ptr<From> m_from; //!< .
440  std::auto_ptr<Where> m_where; //!< .
441  std::auto_ptr<GroupBy> m_groupBy; //!< .
442  std::auto_ptr<Having> m_having; //!< .
443  std::auto_ptr<OrderBy> m_orderBy; //!< .
444  std::auto_ptr<Distinct> m_distinct; //!< .
445  std::size_t m_limit; //!< .
446  std::size_t m_offset; //!< .
447  };
448 
449  } // end namespace da
450 } // end namespace te
451 
452 #endif // __TERRALIB_DATAACCESS_INTERNAL_SELECT_H
453 
A class that can be used to model a filter expression that can be applied to a query.
Definition: Having.h:47
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
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:445
The base class for queries.
std::auto_ptr< Where > m_where
Definition: Select.h:440
std::auto_ptr< Having > m_having
Definition: Select.h:442
This is an abstract class that models a query expression.
Definition: Expression.h:47
std::size_t m_offset
Definition: Select.h:446
std::auto_ptr< GroupBy > m_groupBy
Definition: Select.h:441
URI C++ Library.
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
std::auto_ptr< Fields > m_fields
Definition: Select.h:438
#define TE_DEFINE_VISITABLE
Definition: BaseVisitable.h:75
A Select models a query to be used when retrieving data from a DataSource.
Definition: Select.h:65
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::auto_ptr< From > m_from
Definition: Select.h:439
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::auto_ptr< OrderBy > m_orderBy
Definition: Select.h:443
A Query is independent from the data source language/dialect.
Definition: Query.h:46
std::auto_ptr< Distinct > m_distinct
Definition: Select.h:444