All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Transactor.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008-2013 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/Transactor.cpp
22 
23  \brief DataSourceTransactor implementation for TerraLib 4.x API.
24 */
25 
26 // TerraLib
27 #include "../common/StringUtils.h"
28 #include "../common/Translator.h"
29 #include "../dataaccess/dataset/CheckConstraint.h"
30 #include "../dataaccess/dataset/DataSet.h"
31 #include "../dataaccess/dataset/ForeignKey.h"
32 #include "../dataaccess/dataset/Index.h"
33 #include "../dataaccess/dataset/ObjectIdSet.h"
34 #include "../dataaccess/dataset/PrimaryKey.h"
35 #include "../dataaccess/dataset/Sequence.h"
36 #include "../dataaccess/dataset/UniqueKey.h"
37 #include "../dataaccess/datasource/DataSourceCatalog.h"
38 #include "../dataaccess/datasource/ScopedTransaction.h"
39 #include "../dataaccess/query/Select.h"
40 #include "../dataaccess/query/SQLDialect.h"
41 #include "../dataaccess/utils/Utils.h"
42 #include "../datatype/Array.h"
43 #include "../datatype/Date.h"
44 #include "../datatype/DateTimeProperty.h"
45 #include "../datatype/Property.h"
46 #include "../datatype/SimpleData.h"
47 #include "../datatype/StringProperty.h"
48 #include "../geometry/Envelope.h"
49 #include "../geometry/GeometryProperty.h"
50 #include "../geometry/Utils.h"
51 #include "../geometry/Geometry.h"
52 #include "../memory/DataSet.h"
53 #include "../raster/RasterProperty.h"
54 #include "VectorDataSet.h"
55 #include "DataSource.h"
56 #include "Exception.h"
57 #include "Globals.h"
58 #include "RasterDataSet.h"
59 #include "TableDataSet.h"
60 #include "ThemeInfo.h"
61 #include "Transactor.h"
62 #include "Utils.h"
63 
64 // Terralib 4.x
65 #include <terralib4/kernel/TeDatabase.h>
66 #include <terralib4/kernel/TeLayer.h>
67 #include <terralib4/kernel/TeTable.h>
68 #include <terralib4/kernel/TeTheme.h>
69 #include <terralib4/kernel/TeRasterTransform.h>
70 #include <terralib4/kernel/TeVisual.h>
71 
72 // STL
73 #include <cassert>
74 //#include <cstring>
75 #include <memory>
76 #include <iostream>
77 
78 // Boost
79 //#include <boost/algorithm/string/case_conv.hpp>
80 #include <boost/format.hpp>
81 #include <boost/lexical_cast.hpp>
82 //#include <boost/thread.hpp>
83 
84 int getViewId(const std::string& viewName, TeViewMap& viewMap)
85 {
86  std::map<int, TeView*>::iterator it = viewMap.begin();
87 
88  while(it != viewMap.end())
89  {
90  if(it->second->name() == viewName)
91  return it->second->id();
92 
93  ++it;
94  }
95 
96  return -1;
97 }
98 
100  : m_ds(ds),
101  m_db(db),
102  m_isInTransaction(false),
103  m_layerMap(m_db->layerMap()),
104  m_viewMap(m_db->viewMap()),
105  m_themeMap(m_db->themeMap())
106 {
107 }
108 
110 {
111 }
112 
114 {
115  return m_ds;
116 }
117 
119 {
120  m_db->beginTransaction();
121  m_isInTransaction = true;
122 }
123 
125 {
126  m_db->commitTransaction();
127  m_isInTransaction = false;
128 }
129 
131 {
132  m_db->rollbackTransaction();
133  m_isInTransaction = false;
134 }
135 
137 {
138  return m_isInTransaction;
139 }
140 
141 std::auto_ptr<te::da::DataSet> terralib4::Transactor::getDataSet(const std::string& name,
142  te::common::TraverseType /*travType*/,
143  bool /*connected*/,
144  const te::common::AccessPolicy /*accessPolicy*/)
145 {
146  TeLayer* layer = 0;
147 
148  if(m_db->layerExist(name))
149  {
150  std::map<int, TeLayer*>::iterator it = m_layerMap.begin();
151 
152  while(it != m_layerMap.end())
153  {
154  if(it->second->name() == name)
155  {
156  layer = it->second;
157  break;
158  }
159  ++it;
160  }
161 
162  if(layer->hasGeometry(TeRASTER))
163  {
164  return std::auto_ptr<te::da::DataSet>(new RasterDataSet(layer->raster()));
165  }
166  else
167  return std::auto_ptr<te::da::DataSet>(new VectorDataSet(layer));
168  }
169  else
170  {
171  TeAttrTableVector tables;
172  m_db->getAttrTables(tables);
173 
174  TeTable table;
175 
176  for(std::size_t i = 0; i < tables.size(); ++i)
177  {
178  if(tables[i].tableType() == TeAttrExternal && tables[i].name() == name)
179  {
180  table = tables[i];
181  break;
182  }
183  }
184 
185 
186  return std::auto_ptr<te::da::DataSet>(new TableDataSet(m_db, table));
187  }
188 }
189 
190 std::auto_ptr<te::da::DataSet> terralib4::Transactor::getDataSet(const std::string& name,
191  const std::string& propertyName,
192  const te::gm::Envelope* e,
194  te::common::TraverseType travType,
195  bool /*connected*/,
196  const te::common::AccessPolicy /*accessPolicy*/)
197 {
198  throw Exception(TE_TR("This method is not supported by TerraLib 4.x driver!"));
199 }
200 
201 std::auto_ptr<te::da::DataSet> terralib4::Transactor::getDataSet(const std::string& name,
202  const std::string& propertyName,
203  const te::gm::Geometry* g,
205  te::common::TraverseType travType,
206  bool /*connected*/,
207  const te::common::AccessPolicy /*accessPolicy*/)
208 {
209  throw Exception(TE_TR("This method is not supported by TerraLib 4.x driver!"));
210 }
211 
212 std::auto_ptr<te::da::DataSet> terralib4::Transactor::getDataSet(const std::string& name,
213  const ObjectIdSet* oids,
214  te::common::TraverseType travType,
215  bool /*connected*/,
216  const te::common::AccessPolicy /*accessPolicy*/)
217 {
218  throw Exception(TE_TR("This method is not supported by TerraLib 4.x driver!"));
219 }
220 
221 
222 
223 std::auto_ptr<te::da::DataSet> terralib4::Transactor::query(const te::da::Select& /*q*/,
224  te::common::TraverseType /*travType*/,
225  bool /*connected*/,
226  const te::common::AccessPolicy /*accessPolicy*/)
227 {
228  throw Exception(TE_TR("TerraLib 4.x driver doesn't support queries!"));
229 }
230 
231 std::auto_ptr<te::da::DataSet> terralib4::Transactor::query(const std::string& /*query*/,
232  te::common::TraverseType /*travType*/,
233  bool /*connected*/,
234  const te::common::AccessPolicy /*accessPolicy*/)
235 {
236  throw Exception(TE_TR("TerraLib 4.x driver doesn't support queries!"));
237 }
238 
240 {
241  throw Exception(TE_TR("TerraLib 4.x driver doesn't support command execution!"));
242 }
243 
244 void terralib4::Transactor::execute(const std::string& /*command*/)
245 {
246  throw Exception(TE_TR("TerraLib 4.x driver doesn't support command execution!"));
247 }
248 
249 std::auto_ptr<te::da::PreparedQuery> terralib4::Transactor::getPrepared(const std::string& qName)
250 {
251  throw Exception(TE_TR("TerraLib 4.x driver doesn't support prepared queries!"));
252 }
253 
254 std::auto_ptr<te::da::BatchExecutor> terralib4::Transactor::getBatchExecutor()
255 {
256  throw Exception(TE_TR("TerraLib 4.x driver doesn't support prepared batch executors!"));
257 }
258 
260 {
261 }
262 
264 {
265  throw Exception(TE_TR("TerraLib 4.x driver is read-only!"));
266 }
267 
268 std::string terralib4::Transactor::escape(const std::string& value)
269 {
270  throw Exception(TE_TR("This method is not supported by TerraLib 4.x driver!"));
271 }
272 
273 bool terralib4::Transactor::isDataSetNameValid(const std::string& /*datasetName*/)
274 {
275  throw Exception(TE_TR("TerraLib 4.x driver is read-only!"));
276 }
277 
278 bool terralib4::Transactor::isPropertyNameValid(const std::string& /*propertyName*/)
279 {
280  throw Exception(TE_TR("TerraLib 4.x driver is read-only!"));
281 }
282 
283 std::vector<std::string> terralib4::Transactor::getDataSetNames()
284 {
285  std::map<int, TeLayer*>::iterator it = m_layerMap.begin();
286 
287  std::vector<std::string> dataSets;
288 
289  while(it != m_layerMap.end())
290  {
291  dataSets.push_back(it->second->name());
292 
293  ++it;
294  }
295 
296  TeAttrTableVector tableVector;
297  m_db->getAttrTables(tableVector);
298 
299  for(std::size_t i = 0; i < tableVector.size(); ++i)
300  {
301  if(tableVector[i].tableType() == TeAttrExternal)
302  dataSets.push_back(tableVector[i].name());
303  }
304 
305  return dataSets;
306 }
307 
309 {
310  return getDataSetNames().size();
311 }
312 
313 std::auto_ptr<te::da::DataSetType> terralib4::Transactor::getDataSetType(const std::string& name)
314 {
315  TeLayer* layer = 0;
316 
317  if(m_db->layerExist(name))
318  {
319  std::map<int, TeLayer*>::iterator it = m_layerMap.begin();
320 
321  while(it != m_layerMap.end())
322  {
323  if(it->second->name() == name)
324  {
325  layer = it->second;
326  break;
327  }
328  ++it;
329  }
330  }
331 
332  TeTable table;
333 
334  if(!layer)
335  {
336  TeAttrTableVector tables;
337  m_db->getAttrTables(tables);
338 
339  for(std::size_t i = 0; i < tables.size(); i++)
340  {
341  if(tables[i].tableType() == TeAttrExternal && name == tables[i].name())
342  {
343  table = tables[i];
344  break;
345  }
346  }
347  }
348  else
349  {
350 
351 // Is a layer
352 
353  if(layer->hasGeometry(TeRASTER))
354  {
355  std::auto_ptr<te::da::DataSetType> dst(new te::da::DataSetType(layer->name(), 0));
356 
357  // TODO: handle rasters with multiple objectid!
358  te::rst::RasterProperty* prop = Convert2T5(layer->raster()->params());
359  dst->add(prop);
360  return dst;
361  }
362 
363  TeAttrTableVector tables;
364  layer->getAttrTables(tables);
365 
366  table = tables[0];
367  }
368 
369  std::auto_ptr<te::da::DataSetType> mainDst(terralib4::Convert2T5(table));
370  mainDst->setTitle(table.name());
371 
372  std::vector<std::string> pkey;
373  table.primaryKeys(pkey);
374 
375  te::da::PrimaryKey* pk = 0;
376 
377  if(!pkey.empty())
378  {
379  pk = new te::da::PrimaryKey(table.name() + "_pk", mainDst.get());
380 
381  std::vector<te::dt::Property*> pkProps;
382  for(std::size_t i = 0; i < pkey.size(); ++i)
383  {
384  te::dt::Property* p = mainDst->getProperty(pkey[i]);
385  pkProps.push_back(p);
386  }
387 
388  pk->setProperties(pkProps);
389  }
390 
391  if(layer)
392  {
393  TeAttrTableVector tables;
394  layer->getAttrTables(tables);
395 
396  if(tables.size() > 1)
397  {
398  for(std::size_t i = 1; i < tables.size(); ++i)
399  {
400  TeTable table = tables[i];
401 
402  std::auto_ptr<te::da::DataSetType> dst(terralib4::Convert2T5(table));
403 
404  std::vector<te::dt::Property*> props = dst->getProperties();
405 
406  for(std::size_t j = 0; j < props.size(); ++j)
407  {
408  te::dt::Property* prop = props[j]->clone();
409  prop->setName(dst->getName() + "_" + prop->getName());
410 
411  mainDst->add(prop);
412  }
413  }
414  }
415 
416  TeRepresPointerVector vec = layer->vectRepres();
417 
418  if(!vec.empty())
419  {
420  TeGeomRep geomRep = vec[0]->geomRep_;
421 
422  int srid = layer->projection()->epsgCode();
423 
424  if(srid == 4979)
425  srid = 4326;
426 
427  te::gm::GeometryProperty* geomProp = new te::gm::GeometryProperty("spatial_data",
428  srid, terralib4::Convert2T5GeomType(geomRep));
429  mainDst->add(geomProp);
430  }
431  }
432 
433  return mainDst;
434 }
435 
436 boost::ptr_vector<te::dt::Property> terralib4::Transactor::getProperties(const std::string& datasetName)
437 {
438  std::auto_ptr<te::da::DataSetType> dt = getDataSetType(datasetName);
439 
440  std::vector<te::dt::Property*> dtProperties = dt->getProperties();
441 
442  boost::ptr_vector<te::dt::Property> properties;
443 
444  for(std::size_t i = 0; i < dtProperties.size(); ++i)
445  properties.push_back(dtProperties[i]->clone());
446 
447  return properties;
448 }
449 
450 std::auto_ptr<te::dt::Property> terralib4::Transactor::getProperty(const std::string& datasetName, const std::string& name)
451 {
452  if(!propertyExists(datasetName, name))
453  throw Exception((boost::format(TE_TR("The dataset \"%1%\" has no property with this name \"%2%\"!")) % datasetName % name).str());
454 
455  std::auto_ptr<te::da::DataSetType> dt(getDataSetType(datasetName));
456 
457  return std::auto_ptr<te::dt::Property>(dt->getProperty(name)->clone());
458 }
459 
460 std::auto_ptr<te::dt::Property> terralib4::Transactor::getProperty(const std::string& datasetName, std::size_t propertyPos)
461 {
462  std::auto_ptr<te::da::DataSetType> dt(getDataSetType(datasetName));
463 
464  assert(propertyPos < dt->size());
465 
466  return std::auto_ptr<te::dt::Property>(dt->getProperty(propertyPos)->clone());
467 }
468 
469 std::vector<std::string> terralib4::Transactor::getPropertyNames(const std::string& datasetName)
470 {
471  std::auto_ptr<te::da::DataSetType> dt(getDataSetType(datasetName));
472 
473  std::vector<std::string> pNames;
474 
475  std::size_t numProperties = dt->size();
476 
477  for(std::size_t i = 0; i < numProperties; ++i)
478  pNames.push_back(dt->getProperty(i)->getName());
479 
480  return pNames;
481 }
482 
483 std::size_t terralib4::Transactor::getNumberOfProperties(const std::string& datasetName)
484 {
485  std::auto_ptr<te::da::DataSetType> dt(getDataSetType(datasetName));
486 
487  return dt->size();
488 }
489 
490 bool terralib4::Transactor::propertyExists(const std::string& datasetName, const std::string& name)
491 {
492  std::auto_ptr<te::da::DataSetType> dt(getDataSetType(datasetName));
493 
494  std::vector<std::string> pNames = getPropertyNames(datasetName);
495 
496  if(std::find(pNames.begin(), pNames.end(), name) != pNames.end())
497  return true;
498 
499  return false;
500 }
501 
502 void terralib4::Transactor::addProperty(const std::string& datasetName, te::dt::Property* p)
503 {
504  std::string name = p->getName();
505  if(propertyExists(datasetName, name))
506  throw Exception((boost::format(TE_TR("The dataset already \"%1%\" has a property with this name \"%2%\"!")) % datasetName % name).str());
507 
508  int pType = p->getType();
509 
510  TeAttributeRep newProperty;
511 
512  newProperty.name_ = name;
513 
514  if(p->getType() != te::dt::GEOMETRY_TYPE)
515  newProperty.type_ = terralib4::Convert2T4(p->getType());
516  else
517  {
518  te::gm::Geometry* geom = dynamic_cast<te::gm::Geometry*>(p);
519  newProperty.type_ = terralib4::Convert2T4GeomType(geom->getGeomTypeId());
520  }
521 
522  std::map<int, TeLayer*>::iterator it = m_layerMap.begin();
523 
524  TeLayer* layer = 0;
525 
526  while(it != m_layerMap.end())
527  {
528  if(it->second->name() == datasetName)
529  {
530  layer = it->second;
531  break;
532  }
533  ++it;
534  }
535 
536  TeAttrTableVector tables;
537  layer->getAttrTables(tables);
538  std::string tableName = tables[0].name();
539 
540  m_db->addColumn(tableName, newProperty);
541 }
542 
543 void terralib4::Transactor::dropProperty(const std::string& datasetName, const std::string& name)
544 {
545  throw;
546 }
547 
548 void terralib4::Transactor::renameProperty(const std::string& datasetName,
549  const std::string& propertyName,
550  const std::string& newPropertyName)
551 {
552  throw Exception(TE_TR("This method is not supported by TerraLib 4.x driver!"));
553 }
554 
555 std::auto_ptr<te::da::PrimaryKey> terralib4::Transactor::getPrimaryKey(const std::string& datasetName)
556 {
557  std::auto_ptr<te::da::DataSetType> dt(getDataSetType(datasetName));
558 
559  return std::auto_ptr<te::da::PrimaryKey>(static_cast<te::da::PrimaryKey*>(dt->getPrimaryKey()->clone()));
560 }
561 
562 bool terralib4::Transactor::primaryKeyExists(const std::string& datasetName, const std::string& name)
563 {
564  std::auto_ptr<te::da::DataSetType> dt(getDataSetType(datasetName));
565 
566  if(dt->getPrimaryKey()->getName() == name)
567  return true;
568 
569  return false;
570 }
571 
572 void terralib4::Transactor::addPrimaryKey(const std::string& datasetName, te::da::PrimaryKey* pk)
573 {
574  throw Exception(TE_TR("This method is not supported by TerraLib 4.x driver!"));
575 }
576 
577 void terralib4::Transactor::dropPrimaryKey(const std::string& datasetName)
578 {
579  throw Exception(TE_TR("This method is not supported by TerraLib 4.x driver!"));
580 }
581 
582 std::auto_ptr<te::da::ForeignKey> terralib4::Transactor::getForeignKey(const std::string& datasetName, const std::string& name)
583 {
584  throw Exception(TE_TR("This method is not supported by TerraLib 4.x driver!"));
585 }
586 
587 std::vector<std::string> terralib4::Transactor::getForeignKeyNames(const std::string& datasetName)
588 {
589  throw Exception(TE_TR("This method is not supported by TerraLib 4.x driver!"));
590 }
591 
592 bool terralib4::Transactor::foreignKeyExists(const std::string& datasetName, const std::string& name)
593 {
594  throw Exception(TE_TR("This method is not supported by TerraLib 4.x driver!"));
595 }
596 
597 void terralib4::Transactor::addForeignKey(const std::string& datasetName, te::da::ForeignKey* fk)
598 {
599  throw Exception(TE_TR("This method is not supported by TerraLib 4.x driver!"));
600 }
601 
602 void terralib4::Transactor::dropForeignKey(const std::string& datasetName, const std::string& fkName)
603 {
604  throw Exception(TE_TR("This method is not supported by TerraLib 4.x driver!"));
605 }
606 
607 std::auto_ptr<te::da::UniqueKey> terralib4::Transactor::getUniqueKey(const std::string& datasetName, const std::string& name)
608 {
609  throw Exception(TE_TR("This method is not supported by TerraLib 4.x driver!"));
610 }
611 
612 std::vector<std::string> terralib4::Transactor::getUniqueKeyNames(const std::string& datasetName)
613 {
614  throw Exception(TE_TR("This method is not supported by TerraLib 4.x driver!"));
615 }
616 
617 bool terralib4::Transactor::uniqueKeyExists(const std::string& datasetName, const std::string& name)
618 {
619  throw Exception(TE_TR("This method is not supported by TerraLib 4.x driver!"));
620 }
621 
622 void terralib4::Transactor::addUniqueKey(const std::string& datasetName, te::da::UniqueKey* uk)
623 {
624  throw Exception(TE_TR("This method is not supported by TerraLib 4.x driver!"));
625 }
626 
627 void terralib4::Transactor::dropUniqueKey(const std::string& datasetName, const std::string& name)
628 {
629  throw Exception(TE_TR("This method is not supported by TerraLib 4.x driver!"));
630 }
631 
632 std::auto_ptr<te::da::CheckConstraint> terralib4::Transactor::getCheckConstraint(const std::string& datasetName, const std::string& name)
633 {
634  throw Exception(TE_TR("This method is not supported by TerraLib 4.x driver!"));
635 }
636 
637 std::vector<std::string> terralib4::Transactor::getCheckConstraintNames(const std::string& datasetName)
638 {
639  throw Exception(TE_TR("This method is not supported by TerraLib 4.x driver!"));
640 }
641 
642 bool terralib4::Transactor::checkConstraintExists(const std::string& datasetName, const std::string& name)
643 {
644  throw Exception(TE_TR("This method is not supported by TerraLib 4.x driver!"));
645 }
646 
648 {
649  throw Exception(TE_TR("This method is not supported by TerraLib 4.x driver!"));
650 }
651 
652 void terralib4::Transactor::dropCheckConstraint(const std::string& datasetName, const std::string& name)
653 {
654  throw Exception(TE_TR("This method is not supported by TerraLib 4.x driver!"));
655 }
656 
657 std::auto_ptr<te::da::Index> terralib4::Transactor::getIndex(const std::string& datasetName, const std::string& name)
658 {
659  throw Exception(TE_TR("This method is not supported by TerraLib 4.x driver!"));
660 }
661 
662 std::vector<std::string> terralib4::Transactor::getIndexNames(const std::string& datasetName)
663 {
664  throw Exception(TE_TR("This method is not supported by TerraLib 4.x driver!"));
665 }
666 
667 bool terralib4::Transactor::indexExists(const std::string& datasetName, const std::string& name)
668 {
669  throw Exception(TE_TR("This method is not supported by TerraLib 4.x driver!"));
670 }
671 
672 void terralib4::Transactor::addIndex(const std::string& datasetName, te::da::Index* idx,
673  const std::map<std::string, std::string>& options)
674 {
675  throw Exception(TE_TR("This method is not supported by TerraLib 4.x driver!"));
676 }
677 
678 void terralib4::Transactor::dropIndex(const std::string& datasetName, const std::string& idxName)
679 {
680  throw Exception(TE_TR("This method is not supported by TerraLib 4.x driver!"));
681 }
682 
683 std::auto_ptr<te::da::Sequence> terralib4::Transactor::getSequence(const std::string& name)
684 {
685  throw Exception(TE_TR("This method is not supported by TerraLib 4.x driver!"));
686 }
687 
688 std::vector<std::string> terralib4::Transactor::getSequenceNames()
689 {
690  throw Exception(TE_TR("This method is not supported by TerraLib 4.x driver!"));
691 }
692 
693 bool terralib4::Transactor::sequenceExists(const std::string& name)
694 {
695  throw Exception(TE_TR("This method is not supported by TerraLib 4.x driver!"));
696 }
697 
699 {
700  throw Exception(TE_TR("This method is not supported by TerraLib 4.x driver!"));
701 }
702 
703 void terralib4::Transactor::dropSequence(const std::string& name)
704 {
705  throw Exception(TE_TR("This method is not supported by TerraLib 4.x driver!"));
706 }
707 
708 std::auto_ptr<te::gm::Envelope> terralib4::Transactor::getExtent(const std::string& datasetName,
709  const std::string& /*propertyName*/)
710 {
711  throw Exception(TE_TR("This method is not supported by TerraLib 4.x driver!"));
712 }
713 
714 std::auto_ptr<te::gm::Envelope> terralib4::Transactor::getExtent(const std::string& datasetName,
715  std::size_t /*propertyPos*/)
716 {
717  throw Exception(TE_TR("This method is not supported by TerraLib 4.x driver!"));
718 }
719 
720 std::size_t terralib4::Transactor::getNumberOfItems(const std::string& datasetName)
721 {
722  throw Exception(TE_TR("This method is not supported by TerraLib 4.x driver!"));
723 }
724 
726 {
727  throw Exception(TE_TR("This method is not supported by TerraLib 4.x driver!"));
728 }
729 
730 bool terralib4::Transactor::dataSetExists(const std::string& name)
731 {
732  std::vector<std::string> names = getDataSetNames();
733 
734  for(std::size_t i = 0; i < names.size(); ++i)
735  if(names[i] == name)
736  return true;
737 
738  return false;
739 }
740 
741 void terralib4::Transactor::createDataSet(te::da::DataSetType* dt, const std::map<std::string, std::string>& options)
742 {
743  throw Exception(TE_TR("This method is not supported by TerraLib 4.x driver!"));
744 }
745 
746 void terralib4::Transactor::cloneDataSet(const std::string& name,
747  const std::string& cloneName,
748  const std::map<std::string, std::string>& options)
749 {
750  throw Exception(TE_TR("This method is not supported by TerraLib 4.x driver!"));
751 }
752 
753 void terralib4::Transactor::dropDataSet(const std::string& name)
754 {
755  throw Exception(TE_TR("This method is not supported by TerraLib 4.x driver!"));
756 }
757 
758 void terralib4::Transactor::renameDataSet(const std::string& name, const std::string& newName)
759 {
760  throw Exception(TE_TR("This method is not supported by TerraLib 4.x driver!"));
761 }
762 
763 void terralib4::Transactor::add(const std::string& datasetName,
764  te::da::DataSet* d,
765  const std::map<std::string, std::string>& options,
766  std::size_t limit)
767 {
768  throw Exception(TE_TR("This method is not supported by TerraLib 4.x driver!"));
769 }
770 
771 void terralib4::Transactor::remove(const std::string& datasetName, const te::da::ObjectIdSet* oids)
772 {
773  throw Exception(TE_TR("This method is not supported by TerraLib 4.x driver!"));
774 }
775 
776 void terralib4::Transactor::update(const std::string& datasetName,
777  te::da::DataSet* dataset,
778  const std::vector<std::size_t>& properties,
779  const te::da::ObjectIdSet* oids,
780  const std::map<std::string, std::string>& options,
781  std::size_t limit)
782 {
783  throw Exception(TE_TR("This method is not supported by TerraLib 4.x driver!"));
784 }
785 
786 void terralib4::Transactor::optimize(const std::map<std::string, std::string>& opInfo)
787 {
788  throw Exception(TE_TR("This method is not supported by TerraLib 4.x driver!"));
789 }
790 
791 std::vector<std::string> terralib4::Transactor::getTL4Layers()
792 {
793  std::vector<std::string> layers;
794 
795  std::map<int, TeLayer*>::iterator it = m_layerMap.begin();
796 
797  while(it != m_layerMap.end())
798  {
799  layers.push_back(it->second->name());
800  ++it;
801  }
802 
803  return layers;
804 }
805 
806 std::vector<std::string> terralib4::Transactor::getTL4Tables()
807 {
808  std::vector<std::string> tablesVec;
809 
810  TeAttrTableVector tables;
811  m_db->getAttrTables(tables, TeAttrExternal);
812 
813  for(std::size_t i = 0; i < tables.size(); i++)
814  tablesVec.push_back(tables[i].name());
815 
816  return tablesVec;
817 }
818 
819 std::vector<::terralib4::ThemeInfo> terralib4::Transactor::getTL4Themes()
820 {
821  std::vector<::terralib4::ThemeInfo> themes;
822 
823  TeViewMap& vMap = m_db->viewMap();
824  TeThemeMap& tMap = m_db->themeMap();
825 
826  std::map<int, TeView*>::iterator it = vMap.begin();
827 
828  while(it != vMap.end())
829  {
830  TeView* view = it->second;
831 
832  std::map<int, TeAbstractTheme*>::iterator itT = tMap.begin();
833 
834  while(itT != tMap.end())
835  {
836  TeAbstractTheme* abTheme = itT->second;
837 
838  if(abTheme->type() == TeTHEME)
839  {
840  TeTheme* theme = dynamic_cast<TeTheme*>(abTheme);
841 
842  if(theme->view() == view->id())
843  {
844  ::terralib4::ThemeInfo themeInfo;
845  themeInfo.m_name = theme->name();
846  themeInfo.m_viewName = view->name();
847  themeInfo.m_layerName = theme->layer()->name();
848 
849  themes.push_back(themeInfo);
850  }
851  }
852  ++itT;
853  }
854  ++it;
855  }
856 
857  return themes;
858 }
859 
860 TeTheme* terralib4::Transactor::getTL4Theme(const ::terralib4::ThemeInfo theme)
861 {
862  std::map<int, TeAbstractTheme*>::iterator it = m_themeMap.begin();
863 
864  while(it != m_themeMap.end())
865  {
866  if(it->second->view() == getViewId(theme.m_viewName, m_viewMap))
867  {
868  TeAbstractTheme* abTheme = it->second;
869 
870  if(abTheme->type() == TeTHEME)
871  {
872  TeTheme* tl4Theme = dynamic_cast<TeTheme*>(abTheme);
873 
874  if(tl4Theme->layer()->name() == theme.m_layerName)
875  {
876  return tl4Theme;
877  }
878  }
879  }
880 
881  ++it;
882  }
883 
884  return 0;
885 }
886 
887 int terralib4::Transactor::getLayerSRID(const std::string & layerName)
888 {
889  std::map<int, TeLayer*>::iterator it = m_layerMap.begin();
890 
891  while(it != m_layerMap.end())
892  {
893  if(it->second->name() == layerName)
894  {
895  return it->second->projection()->epsgCode();
896  }
897 
898  ++it;
899  }
900 
901  return 0;
902 }
903 
905 {
906  return te::common::UTF8;
907 }
TeAttrDataType Convert2T4GeomType(te::gm::GeomType type)
Definition: Utils.cpp:325
std::auto_ptr< te::da::ForeignKey > getForeignKey(const std::string &datasetName, const std::string &name)
It retrieves the foreign key from the given dataset.
Definition: Transactor.cpp:582
bool isInTransaction() const
It returns true if a transaction is in progress, otherwise, it returns false.
Definition: Transactor.cpp:136
Geometric property.
bool indexExists(const std::string &datasetName, const std::string &name)
It checks if an index with the given name exists in the dataset.
Definition: Transactor.cpp:667
Utility functions for the data access module.
void addProperty(const std::string &datasetName, te::dt::Property *p)
It adds a new property to the dataset schema.
Definition: Transactor.cpp:502
std::string m_layerName
Definition: ThemeInfo.h:41
boost::int64_t getLastGeneratedId()
It returns the last id generated by an insertion command.
Definition: Transactor.cpp:263
CharEncoding
Supported charsets (character encoding).
void cloneDataSet(const std::string &name, const std::string &cloneName, const std::map< std::string, std::string > &options)
It clones the dataset in the data source.
Definition: Transactor.cpp:746
DataSourceTransactor implementation for TerraLib 4.x API.
An static class with global definitions.
bool primaryKeyExists(const std::string &datasetName, const std::string &name)
It checks if a primary key exists in the dataset.
Definition: Transactor.cpp:562
std::vector< std::string > getDataSetNames()
It It gets the dataset names available in the data source.
Definition: Transactor.cpp:283
A class that models the description of a dataset.
Definition: DataSetType.h:72
int getViewId(const std::string &viewName, TeViewMap &viewMap)
Definition: Transactor.cpp:84
bool propertyExists(const std::string &datasetName, const std::string &name)
It checks if a property with the given name exists in the dataset.
Definition: Transactor.cpp:490
std::vector< std::string > getForeignKeyNames(const std::string &datasetName)
It gets the foreign key names of the given dataset.
Definition: Transactor.cpp:587
void createDataSet(te::da::DataSetType *dt, const std::map< std::string, std::string > &options)
It creates the dataset schema definition in the target data source.
Definition: Transactor.cpp:741
virtual Property * clone() const =0
It returns a clone of the object.
bool uniqueKeyExists(const std::string &datasetName, const std::string &name)
It checks if a unique key with the given name exists in the dataset.
Definition: Transactor.cpp:617
Implements the DataSource class for the TerraLib 4.x Data Access Driver.
void dropIndex(const std::string &datasetName, const std::string &idxName)
It removes the index from the dataset schema.
Definition: Transactor.cpp:678
SpatialRelation
Spatial relations between geometric objects.
Definition: Enums.h:122
It describes a sequence (a number generator).
Definition: Sequence.h:56
void renameProperty(const std::string &datasetName, const std::string &propertyName, const std::string &newPropertyName)
It renames a property of the given dataset.
Definition: Transactor.cpp:548
bool dataSetExists(const std::string &name)
It checks if a dataset with the given name exists in the data source.
Definition: Transactor.cpp:730
bool hasDataSets()
It checks if the data source has any dataset.
Definition: Transactor.cpp:725
void optimize(const std::map< std::string, std::string > &opInfo)
For some data access drivers, this method will perform some operations to optimize the data storage...
Definition: Transactor.cpp:786
A class that describes a check constraint.
std::string escape(const std::string &value)
It escapes a string for using in commands and queries.
Definition: Transactor.cpp:268
An abstract class for data providers like a DBMS, Web Services or a regular file. ...
Definition: DataSource.h:118
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:345
std::vector< std::string > getTL4Tables()
Definition: Transactor.cpp:806
Transactor(DataSource *ds, TeDatabase *db)
Definition: Transactor.cpp:99
void cancel()
It requests that the data source stop the processing of the current command.
Definition: Transactor.cpp:259
std::auto_ptr< te::da::BatchExecutor > getBatchExecutor()
It creates a batch command executor.
Definition: Transactor.cpp:254
It models a property definition.
Definition: Property.h:59
Raster property.
boost::ptr_vector< te::dt::Property > getProperties(const std::string &datasetName)
It retrieves the properties of the dataset.
Definition: Transactor.cpp:436
void dropForeignKey(const std::string &datasetName, const std::string &fkName)
It removes the foreign key constraint from the dataset schema.
Definition: Transactor.cpp:602
std::vector< std::string > getPropertyNames(const std::string &datasetName)
It gets the property names of the given dataset.
Definition: Transactor.cpp:469
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:52
The basic information about a Terralib 4.x Theme.
void rollBack()
It aborts the transaction. Any changes will be rolled-back.
Definition: Transactor.cpp:130
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
void remove(const std::string &datasetName, const te::da::ObjectIdSet *oids=0)
It removes all the informed items from the dataset.
Definition: Transactor.cpp:771
void update(const std::string &datasetName, te::da::DataSet *dataset, const std::vector< std::size_t > &properties, const te::da::ObjectIdSet *oids, const std::map< std::string, std::string > &options, std::size_t limit=0)
It updates the contents of a dataset for the set of data items.
Definition: Transactor.cpp:776
std::auto_ptr< te::da::PrimaryKey > getPrimaryKey(const std::string &datasetName)
It retrieves the primary key of the dataset.
Definition: Transactor.cpp:555
void renameDataSet(const std::string &name, const std::string &newName)
It renames a dataset.
Definition: Transactor.cpp:758
void setName(const std::string &name)
It sets the property name.
Definition: Property.h:136
std::vector< std::string > getCheckConstraintNames(const std::string &datasetName)
It gets the check constraint names of the given dataset.
Definition: Transactor.cpp:637
te::gm::GeomType Convert2T5GeomType(TeAttrDataType type)
Definition: Utils.cpp:193
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
This class represents a set of unique ids created in the same context. i.e. from the same data set...
Definition: ObjectIdSet.h:53
std::auto_ptr< te::da::Sequence > getSequence(const std::string &name)
It gets the sequence with the given name in the data source.
Definition: Transactor.cpp:683
bool foreignKeyExists(const std::string &datasetName, const std::string &name)
It checks if a foreign key with the given name exists in the data source.
Definition: Transactor.cpp:592
bool sequenceExists(const std::string &name)
It checks if a sequence with the given name exists in the data source.
Definition: Transactor.cpp:693
Implementation of a dataset for the TerraLib 4 driver.
Definition: TableDataSet.h:61
void addPrimaryKey(const std::string &datasetName, te::da::PrimaryKey *pk)
It adds a primary key constraint to the dataset schema.
Definition: Transactor.cpp:572
std::size_t getNumberOfProperties(const std::string &datasetName)
It gets the number of properties of the given dataset.
Definition: Transactor.cpp:483
std::auto_ptr< te::da::CheckConstraint > getCheckConstraint(const std::string &datasetName, const std::string &name)
It gets the check constraint of the dataset with the given name.
Definition: Transactor.cpp:632
void setProperties(const std::vector< te::dt::Property * > &properties)
It sets the properties that form the primary key.
Definition: PrimaryKey.h:116
void dropPrimaryKey(const std::string &datasetName)
It removes the primary key constraint from the dataset schema.
Definition: Transactor.cpp:577
std::vector< std::string > getIndexNames(const std::string &datasetName)
It gets the index names of the given dataset.
Definition: Transactor.cpp:662
void addIndex(const std::string &datasetName, te::da::Index *idx, const std::map< std::string, std::string > &options)
It adds an index to the dataset.
Definition: Transactor.cpp:672
std::string m_name
Definition: ThemeInfo.h:39
void execute(const te::da::Query &command)
It executes the specified command using a generic query representation.
Definition: Transactor.cpp:239
It models a foreign key constraint for a DataSetType.
Definition: ForeignKey.h:50
TeTheme * getTL4Theme(const ::terralib4::ThemeInfo theme)
Definition: Transactor.cpp:860
std::vector< std::string > getUniqueKeyNames(const std::string &datasetName)
It gets the unique key names of the given dataset.
Definition: Transactor.cpp:612
It describes a unique key (uk) constraint.
Definition: UniqueKey.h:53
std::size_t getNumberOfItems(const std::string &datasetName)
It retrieves the number of items of the given dataset.
Definition: Transactor.cpp:720
bool isPropertyNameValid(const std::string &propertyName)
It checks if the given property name is valid.
Definition: Transactor.cpp:278
GeomType getGeomTypeId() const
It returns the geometry subclass type identifier.
Definition: Geometry.h:178
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
Definition: Geometry.h:73
A Select models a query to be used when retrieving data from a DataSource.
Definition: Select.h:65
void addForeignKey(const std::string &datasetName, te::da::ForeignKey *fk)
It adds a foreign key constraint to a dataset.
Definition: Transactor.cpp:597
std::auto_ptr< te::da::UniqueKey > getUniqueKey(const std::string &datasetName, const std::string &name)
It gets the unique key in the dataset with the given name.
Definition: Transactor.cpp:607
te::common::CharEncoding getEncoding()
It return the DataSource current encoding.
Definition: Transactor.cpp:904
int getType() const
It returns the property data type.
Definition: Property.h:143
A dataset is the unit of information manipulated by the data access module of TerraLib.
Definition: DataSet.h:112
std::auto_ptr< te::da::DataSetType > getDataSetType(const std::string &name)
It gets information about the given dataset.
Definition: Transactor.cpp:313
void addSequence(te::da::Sequence *sequence)
It creates a new sequence in the data source.
Definition: Transactor.cpp:698
std::auto_ptr< te::dt::Property > getProperty(const std::string &datasetName, const std::string &name)
It retrieves the property with the given name from the dataset.
Definition: Transactor.cpp:450
int getLayerSRID(const std::string &layerName)
Definition: Transactor.cpp:887
Implementation of a dataset for the TerraLib 4 driver.
Definition: VectorDataSet.h:61
It describes a primary key (pk) constraint.
Definition: PrimaryKey.h:52
void dropCheckConstraint(const std::string &datasetName, const std::string &name)
It removes the check constraint from the dataset.
Definition: Transactor.cpp:652
std::auto_ptr< te::da::DataSet > getDataSet(const std::string &name, te::common::TraverseType travType=te::common::FORWARDONLY, bool connected=false, const te::common::AccessPolicy accessPolicy=te::common::RAccess)
It gets the dataset identified by the given name. A dataset can be connected or disconnected. A connected dataset, after its creation through the data source transactor, continues to depend on the connection given by its associated data source. Differently, a disconnected dataset, after its creation, no more depends of the connection given by the data source, and it continues to live after the connection has been released to the data source.
Definition: Transactor.cpp:141
std::auto_ptr< te::da::Index > getIndex(const std::string &datasetName, const std::string &name)
It gets the index with the given name from the dataset.
Definition: Transactor.cpp:657
void dropUniqueKey(const std::string &datasetName, const std::string &name)
It removes the unique key constraint from the dataset.
Definition: Transactor.cpp:627
void addCheckConstraint(const std::string &datasetName, te::da::CheckConstraint *cc)
It adds a check constraint to the dataset.
Definition: Transactor.cpp:647
void commit()
It commits the transaction.
Definition: Transactor.cpp:124
std::size_t getNumberOfDataSets()
It retrieves the number of data sets available in the data source.
Definition: Transactor.cpp:308
void begin()
It starts a new transaction.
Definition: Transactor.cpp:118
std::auto_ptr< te::da::DataSet > query(const te::da::Select &q, te::common::TraverseType travType=te::common::FORWARDONLY, bool connected=false, const te::common::AccessPolicy accessPolicy=te::common::RAccess)
It executes a query that may return some data using a generic query. A dataset can be connected or di...
Definition: Transactor.cpp:223
std::auto_ptr< te::da::PreparedQuery > getPrepared(const std::string &qName=std::string(""))
It creates a prepared query object that may be used for query commands (select, insert, update and delete) that are used repeatedly.
Definition: Transactor.cpp:249
TeAttrDataType Convert2T4(int type)
It converts a Terralib 5 data type to Terralib 4.x data type.
Definition: Utils.cpp:283
std::vector< std::string > getTL4Layers()
Definition: Transactor.cpp:791
bool isDataSetNameValid(const std::string &datasetName)
It returns true if the given string is a valid dataset name.
Definition: Transactor.cpp:273
std::vector< std::string > getSequenceNames()
It gets the sequence names available in the data source.
Definition: Transactor.cpp:688
std::string m_viewName
Definition: ThemeInfo.h:40
void add(const std::string &datasetName, te::da::DataSet *d, const std::map< std::string, std::string > &options, std::size_t limit=0)
It adds data items to the dataset in the data source.
Definition: Transactor.cpp:763
std::auto_ptr< te::gm::Envelope > getExtent(const std::string &datasetName, const std::string &propertyName)
It retrieves the bounding rectangle of the spatial property for the given dataset.
Definition: Transactor.cpp:708
void dropDataSet(const std::string &name)
It removes the dataset schema from the data source.
Definition: Transactor.cpp:753
void dropSequence(const std::string &name)
It removes the sequence from the data source.
Definition: Transactor.cpp:703
A Query is independent from the data source language/dialect.
Definition: Query.h:46
It describes an index associated to a DataSetType.
Definition: Index.h:54
te::da::DataSource * getDataSource() const
It returns the parent data source of the transactor.
Definition: Transactor.cpp:113
void addUniqueKey(const std::string &datasetName, te::da::UniqueKey *uk)
It adds a unique key constraint to the dataset.
Definition: Transactor.cpp:622
std::vector<::terralib4::ThemeInfo > getTL4Themes()
Definition: Transactor.cpp:819
void dropProperty(const std::string &datasetName, const std::string &name)
It removes a property from the given dataset.
Definition: Transactor.cpp:543
bool checkConstraintExists(const std::string &datasetName, const std::string &name)
It checks if a check-constraint with the given name exists in the data source.
Definition: Transactor.cpp:642
const std::string & getName() const
It returns the property name.
Definition: Property.h:126