All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 "../common/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 = 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::auto_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  portal->query("SELECT * FROM " + m_table.name());
90 
91  m_size = portal->numRows();
92 
93  while(portal->fetchRow())
94  {
96 
97  for(int i = 0; i != m_nCols; ++i)
98  {
99  int pType = m_table.attributeList()[i].rep_.type_;
100 
101  switch(pType)
102  {
103  case TeSTRING:
104  item->setString(i, portal->getData(i));
105  break;
106 
107  case TeREAL:
108  item->setDouble(i, portal->getDouble(i));//Numeric(i, portal->getData(i));
109  break;
110 
111  case TeINT:
112  item->setInt32(i, portal->getInt(i));
113  break;
114 
115  case TeDATETIME:
116  {
117  TeTime date = portal->getDate(i);
118 
119  int y = date.year();
120  int m = date.month();
121  int d = date.day();
122  int h = date.hour();
123  int min = date.minute();
124  int s = date.second();
125 
127  te::dt::DateTimeType subType = static_cast<te::dt::DateTimeProperty*>(p)->getSubType();
128  te::dt::DateTime* dateTime = 0;
129 
130  if(subType == te::dt::DATE)
131  {
132  dateTime = new te::dt::Date((unsigned short)y, (unsigned short)m, (unsigned short)d);
133  }
134  else if(subType == te::dt::TIME_DURATION)
135  {
136  dateTime = new te::dt::TimeDuration(h, min, s);
137  }
138  else if(subType == te::dt::TIME_INSTANT)
139  {
140  te::dt::Date dt(y, m, d);
141  te::dt::TimeDuration td(h, min, s);
142  dateTime = new te::dt::TimeInstant(dt, td);
143  }
144 
145  item->setDateTime(i, dateTime);
146 
147  }
148 
149  case TeBLOB:
150  {
151  unsigned char* data;
152  long size;
153  portal->getBlob(m_dt->getProperty(i)->getName(), data, size);
154  item->setByteArray(i, new te::dt::ByteArray((char*)data, (std::size_t)size));
155  break;
156  }
157 
158  case TeCHARACTER:
159  item->setChar(i, (char)portal->getData(i));
160  break;
161 
162  case TeUNSIGNEDINT:
163  item->setInt32(i, portal->getInt(i));
164  break;
165 
166  case TeBOOLEAN:
167  item->setBool(i, portal->getBool(i));
168  break;
169 
170  case TeUNKNOWN:
171  case TeOBJECT:
172  case TeTEXTTYPE:
173  case TeTEXTSETTYPE:
174  default:
175  throw;
176  }
177  }
178 
179  ((te::mem::DataSet*)m_data)->add(item);
180  }
181 }
182 
184 {
185  delete m_dt;
186 }
187 
189 {
190  throw;
191 }
192 
194 {
195  throw;
196 }
197 
198 std::auto_ptr<te::gm::Envelope> terralib4::TableDataSet::getExtent(std::size_t i)
199 {
200  throw;
201 }
202 
204 {
205  return m_nCols;
206 }
207 
209 {
210  return m_dt->getProperty(i)->getType();
211 }
212 
213 std::string terralib4::TableDataSet::getPropertyName(std::size_t i) const
214 {
215  //return m_dt->getProperty(i)->getName();
216  throw;
217 }
218 
220 {
221  return te::common::UNKNOWN_CHAR_ENCODING; // TODO
222 }
223 
225 {
226  throw;
227 }
228 
230 {
231  return !(size() > 0);
232 }
233 
235 {
236  throw;
237 }
238 
239 std::size_t terralib4::TableDataSet::size() const
240 {
241  return m_size;
242 }
243 
245 {
246  return m_data->moveNext();
247 }
248 
250 {
251  return false;
252 }
253 
255 {
256  return false;
257 }
258 
260 {
261  return false;
262 }
263 
265 {
266  return false;
267 }
268 
269 bool terralib4::TableDataSet::move(std::size_t /*i*/)
270 {
271  return false;
272 }
273 
275 {
276  return false;
277 }
278 
280 {
281  return false;
282 }
283 
285 {
286  return false;
287 }
288 
290 {
291  return false;
292 }
293 
294 char terralib4::TableDataSet::getChar(std::size_t i) const
295 {
296  throw;
297 }
298 
299 unsigned char terralib4::TableDataSet::getUChar(std::size_t i) const
300 {
301  throw;
302 }
303 
304 boost::int16_t terralib4::TableDataSet::getInt16(std::size_t i) const
305 {
306  return m_data->getInt16(i);
307 }
308 
309 boost::int32_t terralib4::TableDataSet::getInt32(std::size_t i) const
310 {
311  return m_data->getInt32(i);
312 }
313 
314 boost::int64_t terralib4::TableDataSet::getInt64(std::size_t i) const
315 {
316  return m_data->getInt64(i);
317 }
318 
319 bool terralib4::TableDataSet::getBool(std::size_t i) const
320 {
321  return m_portal->getBool(i);
322 }
323 
324 float terralib4::TableDataSet::getFloat(std::size_t i) const
325 {
326  return m_data->getFloat(i);
327 }
328 
329 double terralib4::TableDataSet::getDouble(std::size_t i) const
330 {
331  return m_data->getDouble(i);
332 }
333 
334 std::string terralib4::TableDataSet::getNumeric(std::size_t i) const
335 {
336  return m_data->getNumeric(i);
337 }
338 
339 std::string terralib4::TableDataSet::getString(std::size_t i) const
340 {
341  return m_data->getString(i);
342 }
343 
344 std::auto_ptr<te::dt::ByteArray> terralib4::TableDataSet::getByteArray(std::size_t i) const
345 {
346  /*unsigned char * data;
347  long size;
348  m_result->getBlob(m_result->getAttribute(i).rep_.name_, data, size);
349 
350  return std::auto_ptr<te::dt::ByteArray>(new te::dt::ByteArray((char*)data, size));*/
351  throw;
352 }
353 
354 std::auto_ptr<te::gm::Geometry> terralib4::TableDataSet::getGeometry(std::size_t i) const
355 {
356  throw;
357 }
358 
359 std::auto_ptr<te::rst::Raster> terralib4::TableDataSet::getRaster(std::size_t i) const
360 {
361  throw;
362 }
363 
364 std::auto_ptr<te::dt::DateTime> terralib4::TableDataSet::getDateTime(std::size_t i) const
365 {
366  //TeTime time = m_result->getDate(i);
367  //time.
368  throw;
369 }
370 
371 std::auto_ptr<te::dt::Array> terralib4::TableDataSet::getArray(std::size_t i) const
372 {
373  return std::auto_ptr<te::dt::Array>(0);
374 }
375 
376 bool terralib4::TableDataSet::isNull(std::size_t i) const
377 {
378 
379  return false;
380  std::string val = m_portal->getData(i);
381 
382  if(val.empty())
383  return true;
384 
385  return false;
386 }
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
void setDouble(std::size_t i, double value)
It sets the value of the i-th property.
CharEncoding
Supported charsets (character encoding).
bool isAtBegin() const
It tells if the dataset internal pointer is on the first element of the collection or not...
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
std::auto_ptr< te::gm::Envelope > getExtent(std::size_t i)
It computes the bounding rectangle for a spatial property of the dataset.
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...
Implements the DataSource class for the TerraLib 4.x Data Access Driver.
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.
std::auto_ptr< te::dt::DateTime > getDateTime(std::size_t i) const
Method for retrieving a date and time attribute value.
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.
bool moveLast()
It sets the dataset internal pointer to the last item in the collection.
A class to represent time instant.
Definition: TimeInstant.h:55
std::auto_ptr< te::dt::Property > Convert2T5(const TeAttributeRep &attRep)
It creates a valid TerraLib 5 property given a valid TerraLib 4.x attribute representation.
Definition: Utils.cpp:53
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.
AccessPolicy
Supported data access policies (can be used as bitfield).
Definition: Enums.h:40
TraverseType
A dataset can be traversed in two ways:
Definition: Enums.h:53
Implementation of a random-access dataset class for the TerraLib In-Memory Data Access driver...
Definition: DataSet.h:65
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).
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.
std::auto_ptr< te::dt::Array > getArray(std::size_t i) const
Method for retrieving an array.
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.
void add(Constraint *c)
It adds a new constraint.
An implementation of the DatasetItem class for the TerraLib In-Memory Data Access driver...
Definition: DataSetItem.h:56
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.
std::auto_ptr< te::rst::Raster > getRaster(std::size_t i) const
Method for retrieving a raster attribute value.
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::auto_ptr< te::gm::Geometry > getGeometry(std::size_t i) const
Method for retrieving a geometric attribute value.
std::string getString(std::size_t i) const
Method for retrieving a string value attribute.
te::common::CharEncoding getPropertyCharEncoding(std::size_t i) const
It returns the property character encoding at position pos.
DateTimeType
The subtype of date and time type, based on ISO 8621.
Definition: Enums.h:45
void setString(std::size_t i, const std::string &value)
It sets the value of the i-th property.
std::auto_ptr< te::dt::ByteArray > getByteArray(std::size_t i) const
Method for retrieving a byte array.
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...
Utilitary functions for dealing with TerraLib 5 and 4.x conversion.
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.
A class for representing binary data.
Definition: ByteArray.h:51
std::size_t size() const
It returns the collection size, if it is known.
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.