QueryIterator.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 QueryIterator.cpp
22 
23  \brief
24 */
25 
26 // Terralib Includes
27 #include "../../core/translator/Translator.h"
28 #include "../../common/StringUtils.h"
29 #include "../../dataaccess/dataset/DataSet.h"
30 #include "../../dataaccess/datasource/DataSource.h"
31 #include "../../dataaccess/query_h.h"
32 #include "../core/GraphMetadata.h"
33 #include "../Config.h"
34 #include "../Exception.h"
35 #include "../Globals.h"
36 #include "QueryIterator.h"
37 
39  te::graph::AbstractIterator(g),
40  m_exp(nullptr)
41 {
42 }
43 
45  te::graph::AbstractIterator(g),
46  m_exp(e)
47 {
48 }
49 
51 
53 {
54  if(m_graph == nullptr || m_graph->getMetadata() == nullptr)
55  {
56  throw Exception(TE_TR("Invalid graph pointer."));
57  }
58 
59  if(m_exp == nullptr)
60  {
61  throw Exception(TE_TR("Expression not defined."));
62  }
63 
64  if(m_vertexQuery.get())
65  {
66  if(m_vertexQuery->moveFirst())
67  {
69 
70  return m_graph->getVertex(id);
71  }
72  }
73 
74  //create a query to get eache vertex id
75  std::string tableName = m_graph->getMetadata()->getVertexTableName();
76 
78  te::da::Fields* fields = new te::da::Fields;
79  fields->push_back(f);
80 
81  te::da::FromItem* t = new te::da::DataSetName(tableName);
82  te::da::From* from = new te::da::From;
83  from->push_back(t);
84 
86 
88  te::da::OrderBy* ob = new te::da::OrderBy();
89  ob->push_back(obItem);
90 
91  te::da::Select select(fields, from, w, ob);
92 
93  m_vertexQuery.reset(nullptr);
95 
96  if(m_vertexQuery.get() == nullptr)
97  {
98  throw Exception(TE_TR("Iterator not initialized."));
99  }
100 
101  int id;
102 
103  if(m_vertexQuery->moveNext())
104  {
106  }
107  else
108  {
109  return nullptr;
110  }
111 
112  return m_graph->getVertex(id);
113 }
114 
116 {
117  if(m_graph == nullptr || m_graph->getMetadata() == nullptr)
118  {
119  throw Exception(TE_TR("Invalid graph pointer."));
120  }
121 
122  if(m_edgeQuery.get())
123  {
124  if(m_edgeQuery->moveFirst())
125  {
126  int id = m_edgeQuery->getInt32(Globals::sm_tableEdgeModelAttrId);
127 
128  return m_graph->getEdge(id);
129  }
130  }
131 
132  //create a query to get eache vertex id
133  std::string edgeTable = m_graph->getMetadata()->getEdgeTableName();
134  std::string vertexAttrTalbe = m_graph->getMetadata()->getVertexTableName();
135 
137  te::da::Fields* fields = new te::da::Fields;
138  fields->push_back(f);
139 
140  //from
141  te::da::From* from = new te::da::From;
142 
143  te::da::FromItem* fi1 = new te::da::DataSetName(edgeTable, "edges");
144  from->push_back(fi1);
145  te::da::FromItem* fi2 = new te::da::DataSetName(vertexAttrTalbe, "v1");
146  from->push_back(fi2);
147  te::da::FromItem* fi3 = new te::da::DataSetName(vertexAttrTalbe, "v2");
148  from->push_back(fi3);
149 
150  std::string edgeIdStr = "edges.";
152 
153  std::string vertexFrom = "edges.";
155 
156  std::string vertexTo = "edges.";
158 
159  std::string v1Id = "v1.";
161 
162  std::string v2Id = "v2.";
164 
165  //where
166  te::da::Field* fvf = new te::da::Field(vertexFrom);
167  te::da::Field* fv1id = new te::da::Field(v1Id);
168  te::da::Expression* exp1 = new te::da::EqualTo(fvf->getExpression(), fv1id->getExpression());
169 
170  te::da::Field* fvt = new te::da::Field(vertexTo);
171  te::da::Field* fv2id = new te::da::Field(v2Id);
172  te::da::Expression* exp2 = new te::da::EqualTo(fvt->getExpression(), fv2id->getExpression());
173 
174  te::da::And* anda = new te::da::And(exp1, exp2);
175 
176  te::da::And* andd = new te::da::And(anda, m_exp);
177 
178  te::da::Where* w = new te::da::Where(andd);
179 
181  te::da::OrderBy* ob = new te::da::OrderBy();
182  ob->push_back(obItem);
183 
184  te::da::Select select(fields, from, w, ob);
185 
186  m_edgeQuery.reset(nullptr);
188 
189  if(m_edgeQuery.get() == nullptr)
190  {
191  throw Exception(TE_TR("Iterator not initialized."));
192  }
193 
194  int id;
195 
196  if(m_edgeQuery->moveNext())
197  {
199  }
200  else
201  {
202  return nullptr;
203  }
204 
205  return m_graph->getEdge(id);
206 }
207 
te::da::DataSource * getDataSource()
It returns the data source associated with this graph.
te::graph::AbstractGraph * m_graph
Pointer to a abstract graph used to access the elements.
An abstract class that models a source of data in a query.
Definition: FromItem.h:50
The Field class can be used to model an expression that takes part of the output items of a SELECT...
static const std::string sm_tableVertexModelAttrId
Attribute id.
A class that models the name of a dataset used in a From clause.
Definition: DataSetName.h:43
A class that models the name of any property of an object.
Base exception class for plugin module.
Expression * getExpression() const
It returns the expression set for an output select query.
virtual ~QueryIterator()
Virtual destructor.
virtual te::graph::Edge * getEdge(int id)=0
It returns the edge element if it&#39;s exist.
std::unique_ptr< te::da::DataSet > m_vertexQuery
Attribute used to keep the vertex iterator.
boost::ptr_vector< OrderByItem > OrderBy
A class that can be used to model an ORDER BY clause.
Definition: OrderBy.h:37
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:242
virtual te::graph::Edge * getFirstEdge()
It returns a pointer to the first edge element of a graph.
te::da::Expression * m_exp
Boolean logic operator: AND.
This is an abstract class that models a query expression.
From the point of view of graph theory, vertices are treated as featureless and indivisible objects...
Definition: Vertex.h:68
virtual std::unique_ptr< DataSet > query(const Select &q, te::common::TraverseType travType=te::common::FORWARDONLY, const te::common::AccessPolicy accessPolicy=te::common::RAccess)
It executes a query that may return some data using a generic query. This method always returns a dis...
virtual te::graph::GraphMetadata * getMetadata()=0
Function used to access the graph metadata.
Class used to define the edge struct of a graph. Its compose with a identifier, the vertex origin and...
Definition: Edge.h:58
QueryIterator(te::graph::AbstractGraph *g)
Default constructor.
static const std::string sm_tableEdgeModelAttrId
Attribute Id.
Abstract class used to define the main functions of graph struct. All graph implementations must used...
Definition: AbstractGraph.h:55
URI C++ Library.
Definition: Attributes.h:37
boost::ptr_vector< Field > Fields
Fields is just a boost::ptr_vector of Field pointers.
Definition: Fields.h:37
A class that can be used to model a filter expression that can be applied to a query.
Definition: Where.h:47
A Select models a query to be used when retrieving data from a DataSource.
Definition: Select.h:65
boost::ptr_vector< FromItem > From
It models the FROM clause for a query.
Definition: From.h:37
std::unique_ptr< te::da::DataSet > m_edgeQuery
Attribute used to keep the edge iterator.
std::string getEdgeTableName()
It returns the edge table name that contains the vertex elements in data source.
virtual te::graph::Vertex * getFirstVertex()
It returns a pointer to the first vertex element of a graph.
It models the comparison operator.
Definition: EqualTo.h:46
This class defines a commun interface to represents a graph iterator class. The main diferency to ano...
std::string getVertexTableName()
It returns the vertex table name that contains the vertex elements in data source.
static const std::string sm_tableEdgeModelAttrVFrom
Attribute Vertex From.
A class that can be used in an ORDER BY clause to sort the items of a resulting query.
Definition: OrderByItem.h:53
virtual te::graph::Vertex * getVertex(int id)=0
It returns the vertex element if it&#39;s exist.
static const std::string sm_tableEdgeModelAttrVTo
Attribute Vertex To.
This class defines a commun interface to represents a graph iterator class. The main diferency to ano...