All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
SQLVisitor.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008-2011 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/ogr/SQLVisitor.cpp
22 
23  \brief A visitor for building an SQL statement using OGR dialect.
24 */
25 
26 // TerraLib
27 #include "../common/StringUtils.h"
28 #include "../geometry/Envelope.h"
29 #include "../dataaccess/query/DataSetName.h"
30 #include "../dataaccess/query/Having.h"
31 #include "../dataaccess/query/LiteralEnvelope.h"
32 #include "../dataaccess/query/PropertyName.h"
33 #include "../dataaccess/query/Select.h"
34 #include "../dataaccess/query/Where.h"
35 #include "SQLVisitor.h"
36 
37 // STL
38 #include <cassert>
39 #include <string>
40 #include <vector>
41 
43  : te::da::SQLVisitor(dialect, sql),
44  m_bbox(0)
45 {
46 }
47 
49 {
50  m_sql += "\'" + visited.getName() + "\'";
51 }
52 
54 {
55  m_bbox = visited.getValue();
56 
57  m_sql += "BBOX)";
58 }
59 
61 {
62  std::vector<std::string> values;
63  te::common::Tokenize(visited.getName(), values, ".");
64 
65  if(values.size() == 1)
66  m_sql += visited.getName();
67  else
68  m_sql += values[values.size() - 1];
69 }
70 
72 {
73  m_sql += "SELECT FID, ";
74 
75  if(visited.getDistinct())
76  {
77  visitDistinct(*(visited.getDistinct()));
78  m_sql += " ";
79  }
80 
81  if(visited.getFields())
82  {
84  m_sql += " ";
85  }
86 
87  if(visited.getFrom())
88  {
89  te::da::SQLVisitor::visit(*(visited.getFrom()));
90  m_sql += " ";
91  }
92 
93  if(visited.getWhere())
94  {
95  m_sql += "WHERE ";
96  visited.getWhere()->getExp()->accept(*this);
97  m_sql += " ";
98  }
99 
100  if(visited.getGroupBy())
101  {
103  m_sql += " ";
104  }
105 
106  if(visited.getHaving())
107  {
108  m_sql += "HAVING ";
109  visited.getHaving()->getExp()->accept(*this);
110  m_sql += " ";
111  }
112 
113  if(visited.getOrderBy())
115 }
116 
118 {
119  /* TODO: http://www.gdal.org/ogr/ogr_sql.html
120 
121  A special form of the field list uses the DISTINCT keyword. This returns a list of all the distinct values
122  of the named attribute. When the DISTINCT keyword is used, only one attribute may appear in the field list.
123  The DISTINCT keyword may be used against any type of field. Currently the distinctness test against a string value
124  is case insensitive in OGR SQL. The result of a SELECT with a DISTINCT keyword is a layer with one column
125  (named the same as the field operated on), and one feature per distinct value.
126  Geometries are discarded. The distinct values are assembled in memory, so alot of memory may be used for datasets with a large number of distinct values. */
127 }
128 
130 {
131  return m_bbox;
132 }
const Distinct * getDistinct() const
It returns the Distinct modifier.
Definition: Select.cpp:992
const std::string & getName() const
It returns the property name.
Definition: PropertyName.h:80
te::da::SQLDialect * dialect
Definition: WFSDialect.h:1
const OrderBy * getOrderBy() const
It returns the list of expressions used to sort the output result.
Definition: Select.cpp:982
A class that models the name of a dataset used in a From clause.
Definition: DataSetName.h:43
const GroupBy * getGroupBy() const
It returns the list of expressions used to condense the result set.
Definition: Select.cpp:962
boost::ptr_vector< Expression > Distinct
A class that models a Distinct clause on a query.
Definition: Distinct.h:37
A class that models the name of any property of an object.
Definition: PropertyName.h:50
te::gm::Envelope * getMBR()
Definition: SQLVisitor.cpp:129
It represents the SQL query dialect accepted by a given data source.
Definition: SQLDialect.h:55
void visit(const te::da::DataSetName &visited)
Definition: SQLVisitor.cpp:48
const From * getFrom() const
It returns the list of source information to be used by the query.
Definition: Select.cpp:942
void Tokenize(const std::string &str, std::vector< std::string > &tokens, const std::string &delimiters=" ")
It tokenizes a given string with a delimiter of your own choice.
Definition: StringUtils.h:216
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
virtual void visit(const Expression &visited)
Definition: SQLVisitor.cpp:66
A visitor for building an SQL statement using OGR dialect.
Expression * getExp() const
Definition: Where.cpp:60
const Fields * getFields() const
It returns the list of output expressions used to form the result set.
Definition: Select.cpp:932
Expression * getExp() const
Definition: Having.cpp:60
SQLVisitor(const te::da::SQLDialect &dialect, std::string &sql)
Default constructor.
Definition: SQLVisitor.cpp:42
A Select models a query to be used when retrieving data from a DataSource.
Definition: Select.h:65
Where * getWhere() const
It returns the filter condition.
Definition: Select.cpp:952
virtual ReturnType accept(VisitorType &guest) const =0
It call the visit method from the guest object.
const Having * getHaving() const
It returns the list of expressions used to eliminate group row that doesn't satisfy the condition...
Definition: Select.cpp:972
A class that models a literal for Envelope values.
const std::string & getName() const
It returns the dataset name.
Definition: DataSetName.cpp:57
void visitDistinct(const te::da::Distinct &visited)
Definition: SQLVisitor.cpp:117
te::gm::Envelope * getValue() const
It returns the associated envelope value.