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 
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. */
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 
132 
134 
135  const Fields& fields() const;
136 
137  Select& operator()(const Field& f);
138 
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 
149 
150  Select& from(const FromItem& i1, const FromItem& i2);
151 
153 
154  Select& from(const FromItem& i1, const FromItem& i2, const FromItem& i3);
155 
157 
158  Select& from(const std::string& datasetName);
159 
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 
173 
174  Select& InnerJoin(const FromItem& d2, const JoinCondition& c);
175 
177 
178  Select& LeftJoin(const FromItem& d2, const JoinCondition& c);
179 
181 
182  Select& RightJoin(const FromItem& d2, const JoinCondition& c);
183 
185 
187 
189 
190  Select& CrossJoin(const FromItem& d2);
191 
193 
195 
196 // where
198 
199  Select& where(const Expression& e);
200 
202 
203  Select& where(const Where& rhs);
204 
206 
207  const Where& where() const;
208 
209 // group by
210  Select& groupBy(const GroupByItem& item);
211 
213 
215 
216  Select& groupBy(const std::string& propertyName);
217 
219 
220  Select& groupBy(const GroupBy& rhs);
221 
223 
224  const GroupBy& groupBy() const;
225 
226 // having
227 
229 
231 
233 
234  Select& having(const Having& rhs);
235 
237 
238  const Having& having() const;
239 
240 // order by
241  Select& orderBy(const OrderByItem& item);
242 
244 
246 
247  Select& orderBy(const std::string& propertyName, SortOrder o = ASC);
248 
250 
251  Select& orderBy(const OrderBy& rhs);
252 
254 
255  const OrderBy& orderBy() const;
256 
257 // distinct
259 
261 
262  Select& distinct(const std::string& propertyName);
263 
265 
266  Select& distinct(const Distinct& rhs);
267 
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  */
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 
418 
420 
422 
424 
426 
428 
430 
432 
434 
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 
#define TE_DEFINE_VISITABLE
Definition: BaseVisitable.h:75
A class that models a Distinct clause on a query.
The Fields class can be used to model a set of expressions that form the output items of a SELECT.
It models the FROM clause for a query.
A class that can be used to model a GROUP BY clause.
A class that can be used to model an ORDER BY clause.
The base class for queries.
This is an abstract class that models a query expression.
Definition: Expression.h:48
The Field class can be used to model an expression that takes part of the output items of a SELECT.
Definition: Field.h:51
An abstract class that models a source of data in a query.
Definition: FromItem.h:51
A class that can be used in a GROUP BY clause.
Definition: GroupByItem.h:51
A class that can be used to model a filter expression that can be applied to a query.
Definition: Having.h:48
A condition to be used in a Join clause.
Definition: JoinCondition.h:45
A class that can be used in an ORDER BY clause to sort the items of a resulting query.
Definition: OrderByItem.h:54
A Query is independent from the data source language/dialect.
Definition: Query.h:47
A Select models a query to be used when retrieving data from a DataSource.
Definition: Select.h:67
Select & groupBy(GroupByItem *item)
void setOffset(std::size_t o)
It specifies the number of rows to skip before starting to return rows.
Select & groupBy(GroupBy *gb)
Where * getWhere() const
It returns the filter condition.
Select & groupBy(const std::string &propertyName)
Select & distinct(Distinct *d)
const From * getFrom() const
It returns the list of source information to be used by the query.
Select & LeftJoin(FromItem *d2, JoinCondition *c)
Select & groupBy(const GroupByItem &item)
OrderBy & orderBy()
Select(const Fields &fds, const From &f, const Where &w)
Select(const Fields &fds, const From &f, const Where &w, const OrderBy &o)
std::size_t getLimit() const
It tells the maximum number of rows to return.
Select & groupBy(const GroupBy &rhs)
std::size_t m_limit
Definition: Select.h:446
Select & distinct(const Distinct &rhs)
const GroupBy & groupBy() const
Select & from(const FromItem &i1, const FromItem &i2)
Select(const Fields &fds)
Select(const Fields &fds, const From &f, const Where &w, const GroupBy &gb)
Select(const Fields &fds, const From &f, const GroupBy &gb)
std::unique_ptr< Distinct > m_distinct
Definition: Select.h:445
std::size_t getOffset() const
It tells the number of rows to skip before starting to return rows.
Select(const Select *rhs)
Select & operator+(te::da::From *f)
Select & operator+(const te::da::GroupBy &g)
void setWhere(Where *w)
It sets the filter codition.
Select & operator()(Field *f)
Select(const Fields &fds, const From &f)
Select & groupBy(const Expression &e)
void setFields(Fields *f)
It sets the list of output expressions used to form the result set.
const GroupBy * getGroupBy() const
It returns the list of expressions used to condense the result set.
std::unique_ptr< Where > m_where
Definition: Select.h:441
Select & from(const FromItem &i1, const FromItem &i2, const FromItem &i3)
Select(Fields *fds, From *f, GroupBy *gb, OrderBy *o=0)
Select & InnerJoin(const FromItem &d2, const JoinCondition &c)
TE_DEFINE_VISITABLE Select()
Default constructor.
Select & distinct(Expression *e)
Select & operator+(te::da::Having *h)
void setDistinct(Distinct *d)
If Distinct is specified, all duplicate rows are removed from the result set (one row is kept from ea...
Select & operator+(const te::da::Having &h)
Select & FullOuterJoin(const FromItem &d2, const JoinCondition &c)
std::unique_ptr< GroupBy > m_groupBy
Definition: Select.h:442
Select & InnerJoin(FromItem *d2, JoinCondition *c)
Select & RightJoin(const FromItem &d2, const JoinCondition &c)
Select & limit(std::size_t l)
Select & operator+(const te::da::OrderBy &o)
Select(const Fields &fds, const From &f, const GroupBy &gb, const OrderBy &o)
const Fields & fields() const
Select & where(Expression *e)
const Having & having() const
Select(const Select &rhs)
std::unique_ptr< From > m_from
Definition: Select.h:440
Select & having(const Having &rhs)
const OrderBy & orderBy() const
std::unique_ptr< OrderBy > m_orderBy
Definition: Select.h:444
Select & where(const Expression &e)
Select & orderBy(OrderBy *o)
Select & orderBy(OrderByItem *item)
void setGroupBy(GroupBy *g)
It sets the list of expressions used to condense the result set.
Select & having(Expression *e)
Select & operator+(te::da::Where *w)
Select(Field *f)
Select & where(const Where &rhs)
Select & operator+(const te::da::Where &w)
Having & having()
Select & offset(std::size_t i)
Select(const Fields &fds, const From &f, const Where &w, const GroupBy &gb, const OrderBy &o)
Query * clone() const
It creates a new copy of this query.
Fields & fields()
Select & from(FromItem *item)
std::size_t m_offset
Definition: Select.h:447
Select & Join(FromItem *d2, JoinType t, JoinCondition *c)
GroupBy & groupBy()
Select & CrossJoin(FromItem *d2)
Select & fields(const Fields &f)
Select & FullOuterJoin(FromItem *d2, JoinCondition *c)
Select & orderBy(const OrderBy &rhs)
~Select()
Destructor.
Select(Fields *fds, From *f, Where *w, GroupBy *gb, OrderBy *o=0)
const OrderBy * getOrderBy() const
It returns the list of expressions used to sort the output result.
Select & from(FromItem *i1, FromItem *i2, FromItem *i3)
Select & fields(Fields *f)
Select & operator+(te::da::GroupBy *g)
Select(const Fields &fds, const From &f, const Where &w, const GroupBy &gb, const Having &h)
Select(const Field &f)
Select & operator=(const Select &rhs)
void setHaving(Having *h)
It sets the list of expressions used to eliminate group row that doesn't satisfy the condition.
Select & operator+(const te::da::From &f)
Select & from(const From &rhs)
Select(const Fields &fds, const From &f, const Where &w, const GroupBy &gb, const Having &h, const OrderBy &o)
Select & NaturalJoin(const FromItem &d2, JoinType t)
Select & distinct(const std::string &propertyName)
Select(Fields *fds, From *f, Where *w, GroupBy *gb, Having *h, OrderBy *o=0)
const Where & where() const
Select & from(FromItem *i1, FromItem *i2)
Distinct & distinct()
Select & operator()(const std::string &propertyName, const std::string &alias)
Select & distinct(const Expression &e)
Select & operator()(const std::string &propertyName)
const Fields * getFields() const
It returns the list of output expressions used to form the result set.
Select & from(From *f)
Select & LeftJoin(const FromItem &d2, const JoinCondition &c)
Select & NaturalJoin(FromItem *d2, JoinType t)
Select(const Fields &fds, const From &f, const OrderBy &o)
Select(const std::string &propertyName)
Select & orderBy(const Expression &e, SortOrder o=ASC)
Select(const std::string &propertyName, const std::string &alias)
const Having * getHaving() const
It returns the list of expressions used to eliminate group row that doesn't satisfy the condition.
Select & having(const Expression &e)
Select & orderBy(const OrderByItem &item)
Select & from(const FromItem &item)
const Distinct & distinct() const
void setFrom(From *f)
It sets the list of source information.
Select(Fields *fds, From *f, OrderBy *o)
Select & Join(const FromItem &d2, JoinType t, const JoinCondition &c)
Select & operator+(te::da::OrderBy *o)
Select(Fields *fds, From *f=0, Where *w=0, OrderBy *o=0)
const From & from() const
Select & operator()(const Field &f)
Select & where(Where *w)
const Distinct * getDistinct() const
It returns the Distinct modifier.
Select & CrossJoin(const FromItem &d2)
Select & orderBy(const std::string &propertyName, SortOrder o=ASC)
Select & RightJoin(FromItem *d2, JoinCondition *c)
std::unique_ptr< Fields > m_fields
Definition: Select.h:439
void setOrderBy(OrderBy *o)
It sets the list of expressions used to sort the output result.
Select & from(const std::string &datasetName)
std::unique_ptr< Having > m_having
Definition: Select.h:443
Where & where()
Select & having(Having *h)
void setLimit(std::size_t m)
It specifies the maximum number of rows to return.
A class that can be used to model a filter expression that can be applied to a query.
Definition: Where.h:48
boost::ptr_vector< Field > Fields
Fields is just a boost::ptr_vector of Field pointers.
Definition: Fields.h:37
boost::ptr_vector< GroupByItem > GroupBy
A class that can be used to model a GROUP BY clause.
Definition: GroupBy.h:37
boost::ptr_vector< FromItem > From
It models the FROM clause for a query.
Definition: From.h:37
JoinType
The type of join in a query.
Definition: Enums.h:50
SortOrder
Sort order type: asc or desc.
Definition: Enums.h:39
@ ASC
Definition: Enums.h:41
boost::ptr_vector< OrderByItem > OrderBy
A class that can be used to model an ORDER BY clause.
Definition: OrderBy.h:37
boost::ptr_vector< Expression > Distinct
A class that models a Distinct clause on a query.
Definition: Distinct.h:37
TerraLib.
#define TEDATAACCESSEXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:97