src/terralib/ado/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/postgis/SQLVisitor.cpp
22 
23  \brief A visitor for building an SQL statement from a given Query hierarchy.
24 */
25 
26 // TerraLib
27 #include "../dataaccess/query/Field.h"
28 #include "../dataaccess/query/Fields.h"
29 #include "../dataaccess/query/JoinConditionOn.h"
30 #include "../dataaccess/query/LiteralByteArray.h"
31 #include "../dataaccess/query/LiteralDateTime.h"
32 #include "../dataaccess/query/LiteralEnvelope.h"
33 #include "../dataaccess/query/LiteralGeom.h"
34 #include "../dataaccess/query/PropertyName.h"
35 #include "../dataaccess/query/ST_EnvelopeIntersects.h"
36 #include "../geometry/Envelope.h"
37 #include "SQLVisitor.h"
38 #include "Utils.h"
39 
40 // STL
41 #include <cassert>
42 
43 // Boost
44 #include <boost/lexical_cast.hpp>
45 
46 te::ado::SQLVisitor::SQLVisitor(const te::da::SQLDialect& dialect, std::string& sql, _ConnectionPtr conn)
47  : te::da::SQLVisitor(dialect, sql),
48  m_conn(conn)
49 {
50 }
51 
53 {
54  assert(visited.getValue() != 0);
55  assert(false); //TODO
56 }
57 
59 {
60  assert(visited.getValue() != 0);
61  assert(false); //TODO
62 }
63 
65 {
66  assert(visited.getValue() != 0);
67  te::gm::Envelope* env = visited.getValue();
68 
69  m_sql += " lower_x = " + boost::lexical_cast<std::string>(env->getLowerLeftX());
70  m_sql += " upper_x = " + boost::lexical_cast<std::string>(env->getUpperRightX());
71  m_sql += " lower_y = " + boost::lexical_cast<std::string>(env->getLowerLeftY());
72  m_sql += " upper_y = " + boost::lexical_cast<std::string>(env->getUpperRightY());
73 }
74 
76 {
77  assert(visited.getValue() != 0);
78  //Convert2PostGIS(m_conn, static_cast<te::gm::Geometry*>(visited.getValue()), m_sql);
79 }
80 
82 {
83  const te::da::ST_EnvelopeIntersects* envIntersects = dynamic_cast<const te::da::ST_EnvelopeIntersects*>(&visited);
84 
85  if(envIntersects == 0)
86  {
88  return;
89  }
90 
91  te::da::LiteralEnvelope* lenv = dynamic_cast<te::da::LiteralEnvelope*>(envIntersects->getFirst());
92  if(lenv == 0)
93  {
94  lenv = dynamic_cast<te::da::LiteralEnvelope*>(envIntersects->getSecond());
95  if(lenv == 0)
96  {
98  return;
99  }
100  }
101 
102  // Here, we have a LiteralEnvelope
103  assert(lenv);
104 
105  te::gm::Envelope* e = lenv->getValue();
106  assert(e);
107 
108  std::string lowerX = "lower_x";
109  std::string upperX = "upper_x";
110  std::string lowerY = "lower_y";
111  std::string upperY = "upper_y";
112 
113  m_sql += "NOT("+ lowerX +" > " + boost::lexical_cast<std::string>(e->m_urx) + " OR ";
114  m_sql += upperX +" < " + boost::lexical_cast<std::string>(e->m_llx) + " OR ";
115  m_sql += lowerY +" > " + boost::lexical_cast<std::string>(e->m_ury) + " OR ";
116  m_sql += upperY +" < " + boost::lexical_cast<std::string>(e->m_lly) + ")";
117 }
118 
120 {
121  std::vector<std::string> values;
122  te::common::Tokenize(visited.getName(), values, ".");
123 
124  if(values.size() == 1)
125  m_sql += visited.getName();
126  else
127  m_sql += values[values.size() - 1];
128 }
129 
131 {
132  m_sql += "ON ";
133 
134  const te::da::Function* on = dynamic_cast<const te::da::Function*>(visited.getCondition());
135  const te::da::PropertyName* left = dynamic_cast<const te::da::PropertyName*>((*on)[0]);
136  const te::da::PropertyName* right = dynamic_cast<const te::da::PropertyName*>((*on)[1]);
137 
138  m_sql += "(";
139  m_sql += left->getName();
140  m_sql += " ";
141  m_sql += on->getName();
142  m_sql += " ";
143  m_sql += right->getName();
144  m_sql += ")";
145 }
146 
148 {
149  std::size_t size = visited.size();
150 
151  for (size_t i = 0; i < size; ++i)
152  {
153  if (i != 0)
154  m_sql += ", ";
155 
156  visited[i].getExpression()->accept(*this);
157 
158  if (visited[i].getAlias())
159  {
160  std::vector<std::string> values;
161  std::string alias = *(visited[i].getAlias());
162  te::common::Tokenize(alias, values, ".");
163 
164  m_sql += " AS ";
165 
166  for (std::string token : values)
167  {
168  m_sql += token;
169  }
170  }
171  }
172 }
const std::string & getName() const
It returns the property name.
Expression * getFirst() const
It returns the first function argument.
te::da::SQLDialect * dialect
Definition: WFSDialect.h:1
A class that models the name of any property of an object.
Utility functions for ADO.
SQLVisitor(const te::da::SQLDialect &dialect, std::string &sql, _ConnectionPtr conn)
Default constructor.
It represents the SQL query dialect accepted by a given data source.
Definition: SQLDialect.h:55
double m_urx
Upper right corner x-coordinate.
Expression * getCondition() const
It returns a pointer to a join condition.
A class that models a literal for Date and Time values.
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:221
double m_llx
Lower left corner x-coordinate.
An operator that considers the intersection among approximations or envelopes of geometries.
An Envelope defines a 2D rectangular region.
virtual void visit(const Expression &visited)
URI C++ Library.
Definition: Attributes.h:37
A class that models a literal for ByteArray values.
std::string & m_sql
The buffer string where the query will be outputed.
boost::ptr_vector< Field > Fields
Fields is just a boost::ptr_vector of Field pointers.
Definition: Fields.h:37
A visitor for building an SQL statement using ADO dialect.
A class that models a Function expression.
void visit(const te::da::Function &visited)
const std::string & getName() const
It returns the function name.
A visitor for building an SQL statement using ADO dialect.
double m_lly
Lower left corner y-coordinate.
double m_ury
Upper right corner y-coordinate.
A class that models a literal for Envelope values.
JoinConditionOn is a boolean expression and it specifies which items in a join are considered to matc...
Expression * getSecond() const
It returns the second function argument.
te::dt::AbstractData * getValue() const
It returns the value associated to the literal.
te::gm::Envelope * getValue() const
It returns the associated envelope value.
A class that models a literal for Geometry values.
Definition: LiteralGeom.h:46