TableDataSet.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/terralib4/TableDataSet.cpp
22 
23  \brief Implementation of a dataset for the TerraLib 4 driver.
24 */
25 
26 // TerraLib
27 #include "../common/ByteSwapUtils.h"
28 #include "../common/Globals.h"
29 #include "../common/StringUtils.h"
30 #include "../core/translator/Translator.h"
31 #include "../dataaccess/dataset/DataSetType.h"
32 #include "../datatype/Array.h"
33 #include "../datatype/ByteArray.h"
34 #include "../datatype/DateTimeInstant.h"
35 #include "../datatype/DateTimeProperty.h"
36 #include "../datatype/SimpleData.h"
37 #include "../datatype/TimeInstant.h"
38 #include "../memory/DataSet.h"
39 #include "../memory/DataSetItem.h"
40 #include "../geometry/Envelope.h"
41 #include "../geometry/Geometry.h"
42 #include "../geometry/GeometryProperty.h"
43 #include "../geometry/WKBReader.h"
44 #include "TableDataSet.h"
45 #include "DataSource.h"
46 #include "Exception.h"
47 #include "GeomReader.h"
48 #include "Utils.h"
49 
50 // Terralib 4.x
51 #include <terralib4/kernel/TeAttribute.h>
52 #include <terralib4/kernel/TeDatabase.h>
53 #include <terralib4/kernel/TeLayer.h>
54 
55 // STL
56 #include <cassert>
57 #include <memory>
58 
59 // Boost
60 #include <boost/lexical_cast.hpp>
61 
62 terralib4::TableDataSet::TableDataSet( TeDatabase* db, TeTable table)
63  : m_dt(0),
64  m_table(table),
65  m_i(-1),
66  m_size(-1),
67  m_nCols(-1),
68  m_data(0),
69  m_db(db)
70 {
71 
72  m_nCols = (int)m_table.attributeList().size();
73 
74  m_dt = new te::da::DataSetType(m_table.name());
75 
76  for(int i = 0; i != m_nCols; ++i)
77  {
78  TeAttributeList attrList = table.attributeList();
79  TeAttribute attr = attrList[i];
80 
81  std::unique_ptr<te::dt::Property> p(terralib4::Convert2T5(attr.rep_));
82 
83  m_dt->add(p.release());
84  }
85 
87 
88  TeDatabasePortal* portal = m_db->getPortal();
89  std::string queryLatin1 = terralib4::Convert2Latin1(std::string("SELECT * FROM "));
90  portal->query(queryLatin1 + m_table.name());
91 
92  m_size = portal->numRows();
93 
94  while(portal->fetchRow())
95  {
97 
98  for(int i = 0; i != m_nCols; ++i)
99  {
100  int pType = m_table.attributeList()[i].rep_.type_;
101 
102  switch(pType)
103  {
104  case TeSTRING:
105  {
106  std::string aux(portal->getData(i));
107  item->setString(i, terralib4::Convert2Utf8(aux));
108  break;
109  }
110 
111  case TeREAL:
112  item->setDouble(i, portal->getDouble(i));//Numeric(i, portal->getData(i));
113  break;
114 
115  case TeINT:
116  item->setInt32(i, portal->getInt(i));
117  break;
118 
119  case TeDATETIME:
120  {
121  TeTime date = portal->getDate(i);
122 
123  int y = date.year();
124  int m = date.month();
125  int d = date.day();
126  int h = date.hour();
127  int min = date.minute();
128  int s = date.second();
129 
131  te::dt::DateTimeType subType = static_cast<te::dt::DateTimeProperty*>(p)->getSubType();
132  te::dt::DateTime* dateTime = 0;
133 
134  if(subType == te::dt::DATE)
135  {
136  dateTime = new te::dt::Date((unsigned short)y, (unsigned short)m, (unsigned short)d);
137  }
138  else if(subType == te::dt::TIME_DURATION)
139  {
140  dateTime = new te::dt::TimeDuration(h, min, s);
141  }
142  else if(subType == te::dt::TIME_INSTANT)
143  {
144  te::dt::Date dt((unsigned short)y, (unsigned short)m, (unsigned short)d);
145  te::dt::TimeDuration td(h, min, s);
146  dateTime = new te::dt::TimeInstant(dt, td);
147  }
148 
149  item->setDateTime(i, dateTime);
150 
151  }
152 
153  case TeBLOB:
154  {
155  unsigned char* data;
156  long size;
157  portal->getBlob(m_dt->getProperty(i)->getName(), data, size);
158  item->setByteArray(i, new te::dt::ByteArray((char*)data, (std::size_t)size));
159  break;
160  }
161 
162  case TeCHARACTER:
163  item->setChar(i, (char)portal->getData(i));
164  break;
165 
166  case TeUNSIGNEDINT:
167  item->setInt32(i, portal->getInt(i));
168  break;
169 
170  case TeBOOLEAN:
171  item->setBool(i, portal->getBool(i));
172  break;
173 
174  case TeUNKNOWN:
175  case TeOBJECT:
176  case TeTEXTTYPE:
177  case TeTEXTSETTYPE:
178  default:
179  throw;
180  }
181  }
182 
183  ((te::mem::DataSet*)m_data)->add(item);
184  }
185 }
186 
188 {
189  delete m_dt;
190 }
191 
193 {
194  throw;
195 }
196 
198 {
199  throw;
200 }
201 
202 std::unique_ptr<te::gm::Envelope> terralib4::TableDataSet::getExtent(std::size_t)
203 {
204  throw;
205 }
206 
208 {
209  return m_nCols;
210 }
211 
213 {
214  return m_dt->getProperty(i)->getType();
215 }
216 
217 std::string terralib4::TableDataSet::getPropertyName(std::size_t) const
218 {
219  //return m_dt->getProperty(i)->getName();
220  throw;
221 }
222 
224 {
225  throw;
226 }
227 
229 {
230  return !(size() > 0);
231 }
232 
234 {
235  throw;
236 }
237 
238 std::size_t terralib4::TableDataSet::size() const
239 {
240  return m_size;
241 }
242 
244 {
245  return m_data->moveNext();
246 }
247 
249 {
250  return false;
251 }
252 
254 {
255  return false;
256 }
257 
259 {
260  return false;
261 }
262 
264 {
265  return false;
266 }
267 
268 bool terralib4::TableDataSet::move(std::size_t /*i*/)
269 {
270  return false;
271 }
272 
274 {
275  return false;
276 }
277 
279 {
280  return false;
281 }
282 
284 {
285  return false;
286 }
287 
289 {
290  return false;
291 }
292 
294 {
295  return false;
296 }
297 
298 char terralib4::TableDataSet::getChar(std::size_t) const
299 {
300  throw;
301 }
302 
303 unsigned char terralib4::TableDataSet::getUChar(std::size_t) const
304 {
305  throw;
306 }
307 
308 boost::int16_t terralib4::TableDataSet::getInt16(std::size_t i) const
309 {
310  return m_data->getInt16(i);
311 }
312 
313 boost::int32_t terralib4::TableDataSet::getInt32(std::size_t i) const
314 {
315  return m_data->getInt32(i);
316 }
317 
318 boost::int64_t terralib4::TableDataSet::getInt64(std::size_t i) const
319 {
320  return m_data->getInt64(i);
321 }
322 
323 bool terralib4::TableDataSet::getBool(std::size_t i) const
324 {
325  return m_portal->getBool((int)i);
326 }
327 
328 float terralib4::TableDataSet::getFloat(std::size_t i) const
329 {
330  return m_data->getFloat(i);
331 }
332 
333 double terralib4::TableDataSet::getDouble(std::size_t i) const
334 {
335  return m_data->getDouble(i);
336 }
337 
338 std::string terralib4::TableDataSet::getNumeric(std::size_t i) const
339 {
340  return m_data->getNumeric(i);
341 }
342 
343 std::string terralib4::TableDataSet::getString(std::size_t i) const
344 {
345  return m_data->getString(i);
346 }
347 
348 std::unique_ptr<te::dt::ByteArray> terralib4::TableDataSet::getByteArray(std::size_t) const
349 {
350  /*unsigned char * data;
351  long size;
352  m_result->getBlob(m_result->getAttribute(i).rep_.name_, data, size);
353 
354  return std::unique_ptr<te::dt::ByteArray>(new te::dt::ByteArray((char*)data, size));*/
355  throw;
356 }
357 
358 std::unique_ptr<te::gm::Geometry> terralib4::TableDataSet::getGeometry(std::size_t) const
359 {
360  throw;
361 }
362 
363 std::unique_ptr<te::rst::Raster> terralib4::TableDataSet::getRaster(std::size_t) const
364 {
365  throw;
366 }
367 
368 std::unique_ptr<te::dt::DateTime> terralib4::TableDataSet::getDateTime(std::size_t) const
369 {
370  //TeTime time = m_result->getDate(i);
371  //time.
372  throw;
373 }
374 
375 std::unique_ptr<te::dt::Array> terralib4::TableDataSet::getArray(std::size_t) const
376 {
377  return std::unique_ptr<te::dt::Array>();
378 }
379 
380 bool terralib4::TableDataSet::isNull(std::size_t) const
381 {
382 
383  return false;
384  /*std::string val = m_portal->getData((int)i);
385 
386  if(val.empty())
387  return true;
388 
389  return false;*/
390 }
static te::dt::TimeDuration td(20, 30, 50, 11)
Property * getProperty(std::size_t i) const
It returns the i-th property.
boost::int64_t getInt64(std::size_t i) const
Method for retrieving a 64-bit integer attribute value (8 bytes long).
te::da::DataSetType * m_dt
Definition: TableDataSet.h:145
virtual double getDouble(std::size_t i) const =0
Method for retrieving a double attribute value.
void setDouble(std::size_t i, double value)
It sets the value of the i-th property.
bool isAtBegin() const
It tells if the dataset internal pointer is on the first element of the collection or not...
TETERRALIB4EXPORT std::string Convert2Utf8(const std::string &str)
bool isBeforeBegin() const
It tells if the dataset internal pointer is in a position before the first element of the collection ...
A class that models the description of a dataset.
Definition: DataSetType.h:72
virtual std::string getNumeric(std::size_t i) const =0
Method for retrieving a numeric attribute value.
virtual float getFloat(std::size_t i) const =0
Method for retrieving a float attribute value.
double getDouble(std::size_t i) const
Method for retrieving a double attribute value.
bool isConnected() const
It returns true if the dataset is connected and false if it is disconnected. A dataset can be connect...
bool isAtEnd() const
It tells if the dataset internal pointer is on the last element of the collection.
void setChar(std::size_t i, char value)
It sets the value of the i-th property.
te::da::DataSet * m_data
Definition: TableDataSet.h:150
std::size_t getNumProperties() const
It returns the number of properties that composes an item of the dataset.
std::string getNumeric(std::size_t i) const
Method for retrieving a numeric attribute value.
It models a property definition.
Definition: Property.h:59
te::common::TraverseType getTraverseType() const
It returns the traverse type associated to the dataset.
std::unique_ptr< te::gm::Geometry > getGeometry(std::size_t i) const
Method for retrieving a geometric attribute value.
bool moveLast()
It sets the dataset internal pointer to the last item in the collection.
std::unique_ptr< te::dt::Property > Convert2T5(const TeAttributeRep &attRep)
It creates a valid TerraLib 5 property given a valid TerraLib 4.x attribute representation.
A class to represent time instant.
Definition: TimeInstant.h:55
bool isNull(std::size_t i) const
It checks if the attribute value is NULL.
boost::int32_t getInt32(std::size_t i) const
Method for retrieving a 32-bit integer attribute value (4 bytes long).
void setInt32(std::size_t i, boost::int32_t value)
It sets the value of the i-th property.
bool isEmpty() const
It returns true if the collection is empty.
std::unique_ptr< te::rst::Raster > getRaster(std::size_t i) const
Method for retrieving a raster attribute value.
virtual bool moveNext()=0
It moves the internal pointer to the next item of the collection.
AccessPolicy
Supported data access policies (can be used as bitfield).
TraverseType
A dataset can be traversed in two ways:
Implementation of a random-access dataset class for the TerraLib In-Memory Data Access driver...
bool moveBeforeFirst()
It moves the internal pointer to a position before the first item in the collection.
unsigned char getUChar(std::size_t i) const
Method for retrieving an unsigned character attribute value (1 byte long).
virtual boost::int16_t getInt16(std::size_t i) const =0
Method for retrieving a 16-bit integer attribute value (2 bytes long).
A base class for date data types.
Definition: Date.h:53
void setDateTime(std::size_t i, te::dt::DateTime *value)
It sets the value of the i-th property.
Implements the DataSource class for the TerraLib 4.x Data Access Driver.
virtual boost::int32_t getInt32(std::size_t i) const =0
Method for retrieving a 32-bit integer attribute value (4 bytes long).
void DataSet()
static te::dt::DateTime d(2010, 8, 9, 15, 58, 39)
static te::dt::TimeDuration dt(20, 30, 50, 11)
std::unique_ptr< te::dt::DateTime > getDateTime(std::size_t i) const
Method for retrieving a date and time attribute value.
std::unique_ptr< te::dt::Array > getArray(std::size_t i) const
Method for retrieving an array.
te::gm::Polygon * p
boost::int16_t getInt16(std::size_t i) const
Method for retrieving a 16-bit integer attribute value (2 bytes long).
std::string getPropertyName(std::size_t i) const
It returns the property name at position pos.
bool movePrevious()
It moves the internal pointer to the previous item of the collection.
bool moveNext()
It moves the internal pointer to the next item of the collection.
TETERRALIB4EXPORT std::string Convert2Latin1(const std::string &str)
bool isPositionValid() const
It tells if the dataset internal pointer is on a valid position.
int getType() const
It returns the property data type.
Definition: Property.h:161
void add(Constraint *c)
It adds a new constraint.
An implementation of the DatasetItem class for the TerraLib In-Memory Data Access driver...
void setByteArray(std::size_t i, te::dt::ByteArray *value)
It sets the value of the i-th property.
bool getBool(std::size_t i) const
Method for retrieving a boolean attribute value.
TeDatabasePortal * m_portal
Definition: TableDataSet.h:152
A class to represent time duration with nano-second/micro-second resolution.
Definition: TimeDuration.h:51
bool move(std::size_t i)
It moves the dataset internal pointer to a given position.
The type for date and time types: date, date period, date duration, time duration, time instant, time period, time instant with time zone or time period with time zone.
int getPropertyDataType(std::size_t i) const
It returns the underlying data type of the property at position pos.
void setBool(std::size_t i, bool value)
It sets the value of the i-th property.
std::string getString(std::size_t i) const
Method for retrieving a string value attribute.
std::unique_ptr< te::dt::ByteArray > getByteArray(std::size_t i) const
Method for retrieving a byte array.
Utilitary functions for dealing with TerraLib 5 and 4.x conversion.
DateTimeType
The subtype of date and time type, based on ISO 8621.
virtual boost::int64_t getInt64(std::size_t i) const =0
Method for retrieving a 64-bit integer attribute value (8 bytes long).
void setString(std::size_t i, const std::string &value)
It sets the value of the i-th property.
An utility class for converting a TerraLib 4.x geometry to a TerraLib 5.
bool isAfterEnd() const
It tells if the dataset internal pointer is on the sentinel position after the last element of the co...
float getFloat(std::size_t i) const
Method for retrieving a float attribute value.
TableDataSet(TeDatabase *db, TeTable table)
char getChar(std::size_t i) const
Method for retrieving a signed character attribute value (1 byte long).
std::string getDatasetNameOfProperty(std::size_t i) const
It returns the underlying dataset name of the property at position pos.
bool moveFirst()
It moves the internal pointer to the first item in the collection.
virtual std::string getString(std::size_t i) const =0
Method for retrieving a string value attribute.
A class for representing binary data.
Definition: ByteArray.h:51
std::size_t size() const
It returns the collection size, if it is known.
std::unique_ptr< te::gm::Envelope > getExtent(std::size_t i)
It computes the bounding rectangle for a spatial property of the dataset.
const std::string & getName() const
It returns the property name.
Definition: Property.h:127
te::common::AccessPolicy getAccessPolicy() const
It returns the read and write permission associated to the dataset.