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/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/LiteralByteArray.h"
28 #include "../dataaccess/query/LiteralDateTime.h"
29 #include "../dataaccess/query/LiteralEnvelope.h"
30 #include "../dataaccess/query/LiteralGeom.h"
31 #include "../dataaccess/query/PropertyName.h"
32 #include "../dataaccess/query/ST_EnvelopeIntersects.h"
33 #include "../geometry/Envelope.h"
34 #include "SQLVisitor.h"
35 #include "Utils.h"
36 
37 // STL
38 #include <cassert>
39 
40 // Boost
41 #include <boost/lexical_cast.hpp>
42 
43 te::ado::SQLVisitor::SQLVisitor(const te::da::SQLDialect& dialect, std::string& sql, _ConnectionPtr conn)
44  : te::da::SQLVisitor(dialect, sql),
45  m_conn(conn)
46 {
47 }
48 
50 {
51  assert(visited.getValue() != 0);
52  assert(false); //TODO
53 }
54 
56 {
57  assert(visited.getValue() != 0);
58  assert(false); //TODO
59 }
60 
62 {
63  assert(visited.getValue() != 0);
64  te::gm::Envelope* env = visited.getValue();
65 
66  m_sql += " lower_x = " + boost::lexical_cast<std::string>(env->getLowerLeftX());
67  m_sql += " upper_x = " + boost::lexical_cast<std::string>(env->getUpperRightX());
68  m_sql += " lower_y = " + boost::lexical_cast<std::string>(env->getLowerLeftY());
69  m_sql += " upper_y = " + boost::lexical_cast<std::string>(env->getUpperRightY());
70 }
71 
73 {
74  assert(visited.getValue() != 0);
75  //Convert2PostGIS(m_conn, static_cast<te::gm::Geometry*>(visited.getValue()), m_sql);
76 }
77 
79 {
80  const te::da::ST_EnvelopeIntersects* envIntersects = dynamic_cast<const te::da::ST_EnvelopeIntersects*>(&visited);
81 
82  if(envIntersects == 0)
83  {
85  return;
86  }
87 
88  te::da::LiteralEnvelope* lenv = dynamic_cast<te::da::LiteralEnvelope*>(envIntersects->getFirst());
89  if(lenv == 0)
90  {
91  lenv = dynamic_cast<te::da::LiteralEnvelope*>(envIntersects->getSecond());
92  if(lenv == 0)
93  {
95  return;
96  }
97  }
98 
99  // Here, we have a LiteralEnvelope
100  assert(lenv);
101 
102  te::gm::Envelope* e = lenv->getValue();
103  assert(e);
104 
105  std::string lowerX = "lower_x";
106  std::string upperX = "upper_x";
107  std::string lowerY = "lower_y";
108  std::string upperY = "upper_y";
109 
110  m_sql += "NOT("+ lowerX +" > " + boost::lexical_cast<std::string>(e->m_urx) + " OR ";
111  m_sql += upperX +" < " + boost::lexical_cast<std::string>(e->m_llx) + " OR ";
112  m_sql += lowerY +" > " + boost::lexical_cast<std::string>(e->m_ury) + " OR ";
113  m_sql += upperY +" < " + boost::lexical_cast<std::string>(e->m_lly) + ")";
114 }
115 
117 {
118  std::vector<std::string> values;
119  te::common::Tokenize(visited.getName(), values, ".");
120 
121  if(values.size() == 1)
122  m_sql += visited.getName();
123  else
124  m_sql += values[values.size() - 1];
125 }
const std::string & getName() const
It returns the property name.
Definition: PropertyName.h:80
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.
Definition: PropertyName.h:50
SQLVisitor(const te::da::SQLDialect &dialect, std::string &sql, _ConnectionPtr conn)
Default constructor.
Definition: SQLVisitor.cpp:43
It represents the SQL query dialect accepted by a given data source.
Definition: SQLDialect.h:55
double m_urx
Upper right corner x-coordinate.
Definition: Envelope.h:346
A visitor for building an SQL statement using ADO dialect.
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:216
double m_llx
Lower left corner x-coordinate.
Definition: Envelope.h:344
An operator that considers the intersection among approximations or envelopes of geometries.
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
virtual void visit(const Expression &visited)
Definition: SQLVisitor.cpp:67
A class that models a literal for ByteArray values.
A class that models a Function expression.
Definition: Function.h:47
void visit(const te::da::Function &visited)
Definition: SQLVisitor.cpp:78
A visitor for building an SQL statement using ADO dialect.
Definition: SQLVisitor.h:49
double m_lly
Lower left corner y-coordinate.
Definition: Envelope.h:345
Utility functions for ADO.
double m_ury
Upper right corner y-coordinate.
Definition: Envelope.h:347
A class that models a literal for Envelope values.
Expression * getSecond() const
It returns the second function argument.
te::dt::AbstractData * getValue() const
It returns the value associated to the literal.
Definition: Literal.cpp:65
te::gm::Envelope * getValue() const
It returns the associated envelope value.
A class that models a literal for Geometry values.
Definition: LiteralGeom.h:46