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 
te::da::Select::operator+
Select & operator+(te::da::Having *h)
te::da::Select::Select
Select(const Fields &fds, const From &f)
te::da::Select::CrossJoin
Select & CrossJoin(FromItem *d2)
te::da::Select::distinct
Distinct & distinct()
te::da::Select::operator()
Select & operator()(const Field &f)
te::da::Select::Select
Select(Fields *fds, From *f=0, Where *w=0, OrderBy *o=0)
te::da::Select::fields
Select & fields(const Fields &f)
te::da::Select::FullOuterJoin
Select & FullOuterJoin(FromItem *d2, JoinCondition *c)
te::da::Select::having
Select & having(const Having &rhs)
te::da::Select::Select
TE_DEFINE_VISITABLE Select()
Default constructor.
te::da::Select::from
Select & from(const FromItem &item)
te::da::Select::Select
Select(Field *f)
te
TerraLib.
Definition: AddressGeocodingOp.h:52
te::da::Select::setDistinct
void setDistinct(Distinct *d)
If Distinct is specified, all duplicate rows are removed from the result set (one row is kept from ea...
te::da::Select::LeftJoin
Select & LeftJoin(const FromItem &d2, const JoinCondition &c)
te::da::Select::from
const From & from() const
te::da::Select::where
Where & where()
te::da::Select::from
Select & from(FromItem *item)
te::da::Distinct
boost::ptr_vector< Expression > Distinct
A class that models a Distinct clause on a query.
Definition: Distinct.h:37
te::da::Select::setGroupBy
void setGroupBy(GroupBy *g)
It sets the list of expressions used to condense the result set.
te::da::Select::where
Select & where(const Where &rhs)
te::da::Select::m_where
std::unique_ptr< Where > m_where
Definition: Select.h:441
te::da::Select::distinct
const Distinct & distinct() const
te::da::Select::Select
Select(Fields *fds, From *f, Where *w, GroupBy *gb, Having *h, OrderBy *o=0)
te::da::Select::InnerJoin
Select & InnerJoin(const FromItem &d2, const JoinCondition &c)
te::da::Select::Select
Select(const Select *rhs)
te::da::SortOrder
SortOrder
Sort order type: asc or desc.
Definition: Enums.h:39
te::da::Select::m_from
std::unique_ptr< From > m_from
Definition: Select.h:440
te::da::Select::Select
Select(const Fields &fds, const From &f, const Where &w, const GroupBy &gb, const OrderBy &o)
te::da::OrderByItem
A class that can be used in an ORDER BY clause to sort the items of a resulting query.
Definition: OrderByItem.h:54
te::da::Select::Select
Select(const Fields &fds, const From &f, const GroupBy &gb, const OrderBy &o)
te::da::Select::m_having
std::unique_ptr< Having > m_having
Definition: Select.h:443
te::da::Select::getHaving
const Having * getHaving() const
It returns the list of expressions used to eliminate group row that doesn't satisfy the condition.
te::da::Select::offset
Select & offset(std::size_t i)
te::da::JoinType
JoinType
The type of join in a query.
Definition: Enums.h:50
te::da::Query
A Query is independent from the data source language/dialect.
Definition: Query.h:47
te::da::Select::orderBy
Select & orderBy(const std::string &propertyName, SortOrder o=ASC)
te::da::Select::setHaving
void setHaving(Having *h)
It sets the list of expressions used to eliminate group row that doesn't satisfy the condition.
te::da::Select::m_distinct
std::unique_ptr< Distinct > m_distinct
Definition: Select.h:445
te::da::Select::Select
Select(const Fields &fds, const From &f, const GroupBy &gb)
te::da::Select::having
Having & having()
te::da::Select::operator+
Select & operator+(te::da::From *f)
Query.h
The base class for queries.
te::da::Fields
boost::ptr_vector< Field > Fields
Fields is just a boost::ptr_vector of Field pointers.
Definition: Fields.h:37
te::da::Select::groupBy
GroupBy & groupBy()
te::da::Select::limit
Select & limit(std::size_t l)
te::da::Select::having
Select & having(Expression *e)
te::da::Select::m_groupBy
std::unique_ptr< GroupBy > m_groupBy
Definition: Select.h:442
te::da::ASC
@ ASC
Definition: Enums.h:41
te::da::Select::NaturalJoin
Select & NaturalJoin(FromItem *d2, JoinType t)
te::da::Select::CrossJoin
Select & CrossJoin(const FromItem &d2)
te::da::Select::NaturalJoin
Select & NaturalJoin(const FromItem &d2, JoinType t)
GroupBy.h
A class that can be used to model a GROUP BY clause.
From.h
It models the FROM clause for a query.
te::da::Select::from
Select & from(const FromItem &i1, const FromItem &i2, const FromItem &i3)
te::da::Select::having
Select & having(Having *h)
te::da::Select::fields
Select & fields(Fields *f)
te::da::Select::setFields
void setFields(Fields *f)
It sets the list of output expressions used to form the result set.
te::da::Select::operator+
Select & operator+(const te::da::Where &w)
te::da::Select::getLimit
std::size_t getLimit() const
It tells the maximum number of rows to return.
te::da::Select::orderBy
Select & orderBy(const OrderBy &rhs)
te::da::GroupByItem
A class that can be used in a GROUP BY clause.
Definition: GroupByItem.h:51
TEDATAACCESSEXPORT
#define TEDATAACCESSEXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:97
te::da::Select::operator+
Select & operator+(const te::da::OrderBy &o)
te::da::Select::operator+
Select & operator+(const te::da::From &f)
te::da::Select::orderBy
const OrderBy & orderBy() const
te::da::Select::orderBy
Select & orderBy(OrderByItem *item)
te::da::Select::Select
Select(const Select &rhs)
te::da::Select::getWhere
Where * getWhere() const
It returns the filter condition.
te::da::Select::fields
Fields & fields()
te::da::Select::m_fields
std::unique_ptr< Fields > m_fields
Definition: Select.h:439
Distinct.h
A class that models a Distinct clause on a query.
te::da::Select::Select
Select(const Fields &fds, const From &f, const Where &w, const GroupBy &gb, const Having &h, const OrderBy &o)
te::da::Select::where
Select & where(const Expression &e)
te::da::OrderBy
boost::ptr_vector< OrderByItem > OrderBy
A class that can be used to model an ORDER BY clause.
Definition: OrderBy.h:37
te::da::Select::setLimit
void setLimit(std::size_t m)
It specifies the maximum number of rows to return.
te::da::Select::m_orderBy
std::unique_ptr< OrderBy > m_orderBy
Definition: Select.h:444
te::da::Select::distinct
Select & distinct(const Distinct &rhs)
te::da::Select::Select
Select(const std::string &propertyName, const std::string &alias)
te::da::Select::LeftJoin
Select & LeftJoin(FromItem *d2, JoinCondition *c)
te::da::Select::operator=
Select & operator=(const Select &rhs)
te::da::Select::getOffset
std::size_t getOffset() const
It tells the number of rows to skip before starting to return rows.
te::da::Expression
This is an abstract class that models a query expression.
Definition: Expression.h:48
te::da::Select::orderBy
Select & orderBy(OrderBy *o)
te::da::Select::Select
Select(Fields *fds, From *f, Where *w, GroupBy *gb, OrderBy *o=0)
te::da::Select::Select
Select(const Fields &fds, const From &f, const OrderBy &o)
te::da::Select::Select
Select(const Fields &fds, const From &f, const Where &w, const OrderBy &o)
te::da::Select::distinct
Select & distinct(Expression *e)
te::da::Select::where
const Where & where() const
te::da::Select::operator+
Select & operator+(te::da::Where *w)
te::da::Select::clone
Query * clone() const
It creates a new copy of this query.
te::da::Select::groupBy
Select & groupBy(const GroupByItem &item)
TE_DEFINE_VISITABLE
#define TE_DEFINE_VISITABLE
Definition: BaseVisitable.h:75
te::da::Field
The Field class can be used to model an expression that takes part of the output items of a SELECT.
Definition: Field.h:51
te::da::Select::operator()
Select & operator()(const std::string &propertyName, const std::string &alias)
te::da::Select::Select
Select(Fields *fds, From *f, OrderBy *o)
te::da::Select::having
const Having & having() const
te::da::Select::getOrderBy
const OrderBy * getOrderBy() const
It returns the list of expressions used to sort the output result.
te::da::Select::from
Select & from(const FromItem &i1, const FromItem &i2)
te::da::Select::fields
const Fields & fields() const
te::da::Select::Join
Select & Join(FromItem *d2, JoinType t, JoinCondition *c)
te::da::Select::operator+
Select & operator+(const te::da::GroupBy &g)
te::da::Select::groupBy
Select & groupBy(const std::string &propertyName)
te::da::Select::having
Select & having(const Expression &e)
te::da::Select::from
Select & from(FromItem *i1, FromItem *i2, FromItem *i3)
OrderBy.h
A class that can be used to model an ORDER BY clause.
te::da::Select::setWhere
void setWhere(Where *w)
It sets the filter codition.
te::da::FromItem
An abstract class that models a source of data in a query.
Definition: FromItem.h:51
te::da::Select::Select
Select(Fields *fds, From *f, GroupBy *gb, OrderBy *o=0)
te::da::Select::m_offset
std::size_t m_offset
Definition: Select.h:447
te::da::Select::groupBy
Select & groupBy(GroupBy *gb)
te::da::Where
A class that can be used to model a filter expression that can be applied to a query.
Definition: Where.h:48
te::da::Select::from
Select & from(const From &rhs)
te::da::Select::Select
Select(const std::string &propertyName)
te::da::Select
A Select models a query to be used when retrieving data from a DataSource.
Definition: Select.h:67
te::da::Select::Select
Select(const Fields &fds, const From &f, const Where &w)
te::da::Select::Select
Select(const Field &f)
te::da::Select::setOrderBy
void setOrderBy(OrderBy *o)
It sets the list of expressions used to sort the output result.
te::da::Select::orderBy
Select & orderBy(const OrderByItem &item)
te::da::Select::setFrom
void setFrom(From *f)
It sets the list of source information.
te::da::Select::getFields
const Fields * getFields() const
It returns the list of output expressions used to form the result set.
te::da::Select::distinct
Select & distinct(const std::string &propertyName)
te::da::JoinCondition
A condition to be used in a Join clause.
Definition: JoinCondition.h:45
te::da::Select::Join
Select & Join(const FromItem &d2, JoinType t, const JoinCondition &c)
te::da::Select::setOffset
void setOffset(std::size_t o)
It specifies the number of rows to skip before starting to return rows.
te::da::Select::getFrom
const From * getFrom() const
It returns the list of source information to be used by the query.
te::da::Select::~Select
~Select()
Destructor.
te::da::Select::Select
Select(const Fields &fds, const From &f, const Where &w, const GroupBy &gb)
te::da::Select::getGroupBy
const GroupBy * getGroupBy() const
It returns the list of expressions used to condense the result set.
te::da::Select::orderBy
OrderBy & orderBy()
te::da::Select::m_limit
std::size_t m_limit
Definition: Select.h:446
te::da::Select::FullOuterJoin
Select & FullOuterJoin(const FromItem &d2, const JoinCondition &c)
te::da::Select::groupBy
Select & groupBy(const GroupBy &rhs)
te::da::Select::from
From & from()
te::da::Select::RightJoin
Select & RightJoin(const FromItem &d2, const JoinCondition &c)
te::da::Select::from
Select & from(const std::string &datasetName)
te::da::Select::distinct
Select & distinct(Distinct *d)
te::da::Select::operator+
Select & operator+(const te::da::Having &h)
te::da::Having
A class that can be used to model a filter expression that can be applied to a query.
Definition: Having.h:48
te::da::GroupBy
boost::ptr_vector< GroupByItem > GroupBy
A class that can be used to model a GROUP BY clause.
Definition: GroupBy.h:37
te::da::Select::operator()
Select & operator()(Field *f)
te::da::Select::distinct
Select & distinct(const Expression &e)
te::da::Select::getDistinct
const Distinct * getDistinct() const
It returns the Distinct modifier.
te::da::Select::operator()
Select & operator()(const std::string &propertyName)
te::da::Select::where
Select & where(Expression *e)
te::da::Select::Select
Select(const Fields &fds)
te::da::Select::InnerJoin
Select & InnerJoin(FromItem *d2, JoinCondition *c)
te::da::Select::operator+
Select & operator+(te::da::OrderBy *o)
te::da::Select::where
Select & where(Where *w)
te::da::Select::orderBy
Select & orderBy(const Expression &e, SortOrder o=ASC)
te::da::Select::RightJoin
Select & RightJoin(FromItem *d2, JoinCondition *c)
Fields.h
The Fields class can be used to model a set of expressions that form the output items of a SELECT.
te::da::From
boost::ptr_vector< FromItem > From
It models the FROM clause for a query.
Definition: From.h:37
te::da::Select::groupBy
const GroupBy & groupBy() const
te::da::Select::from
Select & from(From *f)
te::da::Select::groupBy
Select & groupBy(const Expression &e)
te::da::Select::operator+
Select & operator+(te::da::GroupBy *g)
te::da::Select::groupBy
Select & groupBy(GroupByItem *item)
te::da::Select::from
Select & from(FromItem *i1, FromItem *i2)
te::da::Select::Select
Select(const Fields &fds, const From &f, const Where &w, const GroupBy &gb, const Having &h)