All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
AttributeRestrictionVisitor.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/dataaccess/query/AttributeRestrictionVisitor.cpp
22 
23  \brief A visitor that retrieves attribute restrictions from a Query hierarchy.
24 */
25 
26 // TerraLib
27 #include "../../common/STLUtils.h"
28 #include "../../common/Translator.h"
29 #include "../Exception.h"
31 #include "Expression.h"
32 #include "Function.h"
33 #include "FunctionNames.h"
34 #include "PropertyName.h"
35 #include "Select.h"
36 #include "Where.h"
37 
38 // STL
39 #include <cassert>
40 
42  : m_index(0),
43  m_op(""),
44  m_function(0)
45 {
46 }
47 
49 {
50 }
51 
53  : m_index(0)
54 {
55  initialize();
56 }
57 
59 {
60  te::common::FreeContents(m_attrRestrictions);
61 }
62 
64 {
65  if(isAttributeRestrictionFunction(visited))
66  {
67  addAttributeRestriction(visited);
68  return;
69  }
70 
71  std::size_t size = visited.getNumArgs();
72  for(std::size_t i = 0; i < size; ++i)
73  {
74  assert(visited[i]);
75  visited[i]->accept(*this);
76  }
77 }
78 
80 {
81  Where* restriction = visited.getWhere();
82 
83  if(!restriction)
84  return;
85 
86  Expression* e = restriction->getExp();
87  if(e)
88  e->accept(*this);
89 }
90 
92 {
93  return !m_attrRestrictions.empty();
94 }
95 
96 const std::vector<te::da::AttributeRestriction*>& te::da::AttributeRestrictionVisitor::getAttributeRestrictions() const
97 {
98  return m_attrRestrictions;
99 }
100 
102 {
103  // Initializes the map of attribute restriction functions
104  m_attrFunctions[FunctionNames::sm_EqualTo ] = "=";
105  m_attrFunctions[FunctionNames::sm_NotEqualTo ] = "<>";
106  m_attrFunctions[FunctionNames::sm_GreaterThan ] = ">";
107  m_attrFunctions[FunctionNames::sm_GreaterThanOrEqualTo] = ">=";
108  m_attrFunctions[FunctionNames::sm_LessThan ] = "<";
109  m_attrFunctions[FunctionNames::sm_LessThanOrEqualTo ] = "<=";
110  // ... more?!
111 }
112 
114 {
115  return m_attrFunctions.find(f.getName()) != m_attrFunctions.end();
116 }
117 
119 {
120  assert(isAttributeRestrictionFunction(f));
121 
122  return m_attrFunctions.find(f.getName())->second;
123 }
124 
126 {
127  // Creates the attribute restriction
128  AttributeRestriction* restriction = new AttributeRestriction;
129  restriction->m_index = m_index++;
130  restriction->m_op = getOperator(f);
131  restriction->m_function = dynamic_cast<const Function*>(&f);
132 
133  // Adds the attribute restriction
134  m_attrRestrictions.push_back(restriction);
135 }
static const std::string sm_LessThan
Definition: FunctionNames.h:59
A class that models a Function expression.
const Function * m_function
The function that represents the attribute restriction.
A class that models the name of any property of an object.
virtual ~AttributeRestrictionVisitor()
Virtual destructor.
static const std::string sm_EqualTo
Definition: FunctionNames.h:55
This is an abstract class that models a query expression.
Definition: Expression.h:47
A struct that represents an attribute restriction.
bool isAttributeRestrictionFunction(const Function &f) const
static const std::string sm_GreaterThanOrEqualTo
Definition: FunctionNames.h:58
std::size_t m_index
Internal index of the attribute restriction.
const std::vector< te::da::AttributeRestriction * > & getAttributeRestrictions() const
virtual void visit(const Expression &visited)
A class that can be used to model a filter expression that can be applied to a query.
Definition: Where.h:47
This is an abstract class that models a query expression.
static const std::string sm_NotEqualTo
Definition: FunctionNames.h:56
std::string m_op
The attribute restriction operator.
Expression * getExp() const
Definition: Where.cpp:60
A class that models a Function expression.
Definition: Function.h:47
A Select models a query to be used when retrieving data from a data source.
std::string getOperator(const Function &f) const
const std::string & getName() const
It returns the function name.
Definition: Function.h:79
A Select models a query to be used when retrieving data from a DataSource.
Definition: Select.h:65
A visitor that retrieves attribute restrictions from a Query hierarchy.
static const std::string sm_LessThanOrEqualTo
Definition: FunctionNames.h:60
A class that can be used to model a filter expression that can be applied to a query.
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.
A static class with global function name definitions.
void FreeContents(boost::unordered_map< K, V * > &m)
This function can be applied to a map of pointers. It will delete each pointer in the map...
Definition: BoostUtils.h:55
static const std::string sm_GreaterThan
Definition: FunctionNames.h:57
std::size_t getNumArgs() const
It returns the number of arguments informed to the function.
Definition: Function.cpp:62