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 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/Function.h"
31 #include "../dataaccess/query/Having.h"
32 #include "../dataaccess/query/Join.h"
33 #include "../dataaccess/query/JoinConditionOn.h"
34 #include "../dataaccess/query/LiteralEnvelope.h"
35 #include "../dataaccess/query/PropertyName.h"
36 #include "../dataaccess/query/Select.h"
37 #include "../dataaccess/query/Where.h"
38 #include "../dataaccess/Enums.h"
39 #include "SQLVisitor.h"
40 
41 // STL
42 #include <cassert>
43 #include <string>
44 #include <vector>
45 
47  : te::da::SQLVisitor(dialect, sql),
48  m_bbox(0)
49 {
50 }
51 
53 {
54  m_sql += "\'" + visited.getName() + "\'";
55 }
56 
58 {
59  m_bbox = visited.getValue();
60 
61  m_sql += "BBOX)";
62 }
63 
65 {
66  std::vector<std::string> values;
67  te::common::Tokenize(visited.getName(), values, ".");
68 
69  if(values.size() == 1)
70  m_sql += visited.getName();
71  else
72  m_sql += values[values.size() - 1];
73 }
74 
76 {
77  m_sql += "SELECT FID, ";
78 
79  if(visited.getDistinct())
80  {
81  visitDistinct(*(visited.getDistinct()));
82  m_sql += " ";
83  }
84 
85  if(visited.getFields())
86  {
88  m_sql += " ";
89  }
90 
91  if(visited.getFrom())
92  {
93  te::da::SQLVisitor::visit(*(visited.getFrom()));
94  m_sql += " ";
95  }
96 
97  if(visited.getWhere())
98  {
99  m_sql += "WHERE ";
100  visited.getWhere()->getExp()->accept(*this);
101  m_sql += " ";
102  }
103 
104  if(visited.getGroupBy())
105  {
107  m_sql += " ";
108  }
109 
110  if(visited.getHaving())
111  {
112  m_sql += "HAVING ";
113  visited.getHaving()->getExp()->accept(*this);
114  m_sql += " ";
115  }
116 
117  if(visited.getOrderBy())
119 }
120 
122 {
123  assert(visited.getFirst() && visited.getSecond());
124 
125  visited.getFirst()->accept(*this);
126 
127  if(visited.isNatural())
128  m_sql += " NATURAL ";
129 
130  switch(visited.getType())
131  {
132  case te::da::JOIN :
133  m_sql += " JOIN ";
134  break;
135 
136  case te::da::INNER_JOIN :
137  m_sql += " INNER JOIN ";
138  break;
139 
140  case te::da::LEFT_JOIN :
141  m_sql += " LEFT JOIN ";
142  break;
143 
144  case te::da::RIGHT_JOIN :
145  m_sql += " RIGHT JOIN ";
146  break;
147 
149  m_sql += " FULL OUTER JOIN ";
150  break;
151 
152  case te::da::CROSS_JOIN :
153  m_sql += " CROSS JOIN ";
154  break;
155 
156  case te::da::NATURAL_JOIN :
157  m_sql += " NATURAL JOIN ";
158  break;
159  }
160 
161  visited.getSecond()->accept(*this);
162 
163  if(visited.getCondition())
164  {
165  m_sql += " ";
166  visited.getCondition()->accept(*this);
167  }
168 }
169 
171 {
172  assert(visited.getCondition());
173  m_sql += "ON ";
174 
175  te::da::Function func = dynamic_cast< te::da::Function&>(*visited.getCondition());
176  const std::string& fname = func.getName();
177 
178  assert(func.getNumArgs() == 2);
179  func[0]->accept(*this);
180  m_sql += " ";
181  m_sql += fname;
182  m_sql += " ";
183  func[1]->accept(*this);
184 }
185 
187 {
188  /* TODO: http://www.gdal.org/ogr/ogr_sql.html
189 
190  A special form of the field list uses the DISTINCT keyword. This returns a list of all the distinct values
191  of the named attribute. When the DISTINCT keyword is used, only one attribute may appear in the field list.
192  The DISTINCT keyword may be used against any type of field. Currently the distinctness test against a string value
193  is case insensitive in OGR SQL. The result of a SELECT with a DISTINCT keyword is a layer with one column
194  (named the same as the field operated on), and one feature per distinct value.
195  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. */
196 }
197 
199 {
200  return m_bbox;
201 }
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:198
It represents the SQL query dialect accepted by a given data source.
Definition: SQLDialect.h:55
Expression * getCondition() const
It returns a pointer to a join condition.
JoinType getType() const
It returns the join type.
Definition: Join.cpp:111
void visit(const te::da::DataSetName &visited)
Definition: SQLVisitor.cpp:52
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
bool isNatural() const
It tells if the join is Natural.
Definition: Join.h:141
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
virtual void visit(const Expression &visited)
Definition: SQLVisitor.cpp:67
A visitor for building an SQL statement using OGR dialect.
Expression * getExp() const
Definition: Where.cpp:60
A class that models a Function expression.
Definition: Function.h:47
const Fields * getFields() const
It returns the list of output expressions used to form the result set.
Definition: Select.cpp:932
A Join clause combines two FromItems.
Definition: Join.h:50
Expression * getExp() const
Definition: Having.cpp:60
const std::string & getName() const
It returns the function name.
Definition: Function.h:79
SQLVisitor(const te::da::SQLDialect &dialect, std::string &sql)
Default constructor.
Definition: SQLVisitor.cpp:46
A Select models a query to be used when retrieving data from a DataSource.
Definition: Select.h:65
FromItem * getSecond() const
It returns the second item involved in the join.
Definition: Join.cpp:101
Where * getWhere() const
It returns the filter condition.
Definition: Select.cpp:952
JoinCondition * getCondition() const
It returns the join condition.
Definition: Join.cpp:121
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
JoinConditionOn is a boolean expression and it specifies which items in a join are considered to matc...
std::size_t getNumArgs() const
It returns the number of arguments informed to the function.
Definition: Function.cpp:62
void visitDistinct(const te::da::Distinct &visited)
Definition: SQLVisitor.cpp:186
te::gm::Envelope * getValue() const
It returns the associated envelope value.
FromItem * getFirst() const
It returns the first from item involved in the join.
Definition: Join.cpp:91