All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 "../../common/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(0)
41 {
42 }
43 
45  te::graph::AbstractIterator(g),
46  m_exp(e)
47 {
48 }
49 
51 {
52 }
53 
55 {
56  if(m_graph == 0 || m_graph->getMetadata() == 0)
57  {
58  throw Exception(TE_TR("Invalid graph pointer."));
59  }
60 
61  if(m_exp == 0)
62  {
63  throw Exception(TE_TR("Expression not defined."));
64  }
65 
66  if(m_vertexQuery.get())
67  {
68  if(m_vertexQuery->moveFirst())
69  {
70  int id = m_vertexQuery->getInt32(Globals::sm_tableVertexModelAttrId);
71 
72  return m_graph->getVertex(id);
73  }
74  }
75 
76  //create a query to get eache vertex id
77  std::string tableName = m_graph->getMetadata()->getVertexTableName();
78 
80  te::da::Fields* fields = new te::da::Fields;
81  fields->push_back(f);
82 
83  te::da::FromItem* t = new te::da::DataSetName(tableName);
84  te::da::From* from = new te::da::From;
85  from->push_back(t);
86 
87  te::da::Where* w = new te::da::Where(m_exp);
88 
90  te::da::OrderBy* ob = new te::da::OrderBy();
91  ob->push_back(obItem);
92 
93  te::da::Select select(fields, from, w, ob);
94 
95  m_vertexQuery.reset(0);
96  m_vertexQuery = m_graph->getMetadata()->getDataSource()->query(select);
97 
98  if(m_vertexQuery.get() == 0)
99  {
100  throw Exception(TE_TR("Iterator not initialized."));
101  }
102 
103  int id;
104 
105  if(m_vertexQuery->moveNext())
106  {
107  id = m_vertexQuery->getInt32(Globals::sm_tableVertexModelAttrId);
108  }
109  else
110  {
111  return 0;
112  }
113 
114  return m_graph->getVertex(id);
115 }
116 
118 {
119  if(m_graph == 0 || m_graph->getMetadata() == 0)
120  {
121  throw Exception(TE_TR("Invalid graph pointer."));
122  }
123 
124  if(m_edgeQuery.get())
125  {
126  if(m_edgeQuery->moveFirst())
127  {
128  int id = m_edgeQuery->getInt32(Globals::sm_tableEdgeModelAttrId);
129 
130  return m_graph->getEdge(id);
131  }
132  }
133 
134  //create a query to get eache vertex id
135  std::string edgeTable = m_graph->getMetadata()->getEdgeTableName();
136  std::string vertexAttrTalbe = m_graph->getMetadata()->getVertexTableName();
137 
139  te::da::Fields* fields = new te::da::Fields;
140  fields->push_back(f);
141 
142  //from
143  te::da::From* from = new te::da::From;
144 
145  te::da::FromItem* fi1 = new te::da::DataSetName(edgeTable, "edges");
146  from->push_back(fi1);
147  te::da::FromItem* fi2 = new te::da::DataSetName(vertexAttrTalbe, "v1");
148  from->push_back(fi2);
149  te::da::FromItem* fi3 = new te::da::DataSetName(vertexAttrTalbe, "v2");
150  from->push_back(fi3);
151 
152  std::string edgeIdStr = "edges.";
154 
155  std::string vertexFrom = "edges.";
157 
158  std::string vertexTo = "edges.";
160 
161  std::string v1Id = "v1.";
163 
164  std::string v2Id = "v2.";
166 
167  //where
168  te::da::Field* fvf = new te::da::Field(vertexFrom);
169  te::da::Field* fv1id = new te::da::Field(v1Id);
170  te::da::Expression* exp1 = new te::da::EqualTo(fvf->getExpression(), fv1id->getExpression());
171 
172  te::da::Field* fvt = new te::da::Field(vertexTo);
173  te::da::Field* fv2id = new te::da::Field(v2Id);
174  te::da::Expression* exp2 = new te::da::EqualTo(fvt->getExpression(), fv2id->getExpression());
175 
176  te::da::And* anda = new te::da::And(exp1, exp2);
177 
178  te::da::And* andd = new te::da::And(anda, m_exp);
179 
180  te::da::Where* w = new te::da::Where(andd);
181 
183  te::da::OrderBy* ob = new te::da::OrderBy();
184  ob->push_back(obItem);
185 
186  te::da::Select select(fields, from, w, ob);
187 
188  m_edgeQuery.reset(0);
189  m_edgeQuery = m_graph->getMetadata()->getDataSource()->query(select);
190 
191  if(m_edgeQuery.get() == 0)
192  {
193  throw Exception(TE_TR("Iterator not initialized."));
194  }
195 
196  int id;
197 
198  if(m_edgeQuery->moveNext())
199  {
200  id = m_edgeQuery->getInt32(Globals::sm_tableEdgeModelAttrId);
201  }
202  else
203  {
204  return 0;
205  }
206 
207  return m_graph->getEdge(id);
208 }
209 
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...
Definition: Field.h:50
static const std::string sm_tableVertexModelAttrId
Attribute id.
Definition: Globals.h:97
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.
Definition: PropertyName.h:50
Expression * getExpression() const
It returns the expression set for an output select query.
Definition: Field.cpp:80
virtual ~QueryIterator()
Virtual destructor.
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:347
virtual te::graph::Edge * getFirstEdge()
It returns a pointer to the first edge element of a graph.
Boolean logic operator: AND.
Definition: And.h:46
This is an abstract class that models a query expression.
Definition: Expression.h:47
From the point of view of graph theory, vertices are treated as featureless and indivisible objects...
Definition: Vertex.h:68
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.
Definition: Globals.h:91
Abstract class used to define the main functions of graph struct. All graph implementations must used...
Definition: AbstractGraph.h:55
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
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...
static const std::string sm_tableEdgeModelAttrVFrom
Attribute Vertex From.
Definition: Globals.h:92
A class that can be used in an ORDER BY clause to sort the items of a resulting query.
Definition: OrderByItem.h:53
static const std::string sm_tableEdgeModelAttrVTo
Attribute Vertex To.
Definition: Globals.h:93
This class defines a commun interface to represents a graph iterator class. The main diferency to ano...