Transactor.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/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&,
191  const std::string&,
192  const te::gm::Envelope*,
195  bool,
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&,
202  const std::string&,
203  const te::gm::Geometry*,
206  bool,
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&,
213  const ObjectIdSet*,
215  bool,
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&)
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&)
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  else
391  {
392  TeAttribute attLink;
393  table.attrLink(attLink);
394 
395  std::string attLinkName = attLink.rep_.name_;
396 
397  if(!attLinkName.empty())
398  {
399  pk = new te::da::PrimaryKey(table.name() + "_pk", mainDst.get());
400 
401  te::dt::Property* p = mainDst->getProperty(attLinkName);
402 
403  std::vector<te::dt::Property*> pkProps;
404  pkProps.push_back(p);
405 
406  pk->setProperties(pkProps);
407  }
408  }
409 
410  if(layer)
411  {
412  TeAttrTableVector tables;
413  layer->getAttrTables(tables);
414 
415  if(tables.size() > 1)
416  {
417  for(std::size_t i = 1; i < tables.size(); ++i)
418  {
419  TeTable table = tables[i];
420 
421  std::auto_ptr<te::da::DataSetType> dst(terralib4::Convert2T5(table));
422 
423  std::vector<te::dt::Property*> props = dst->getProperties();
424 
425  for(std::size_t j = 0; j < props.size(); ++j)
426  {
427  te::dt::Property* prop = props[j]->clone();
428  prop->setName(dst->getName() + "_" + prop->getName());
429 
430  mainDst->add(prop);
431  }
432  }
433  }
434 
435  TeRepresPointerVector vec = layer->vectRepres();
436 
437  if(!vec.empty())
438  {
439  TeGeomRep geomRep = vec[0]->geomRep_;
440 
441  int srid = layer->projection()->epsgCode();
442 
443  if(srid == 4979)
444  srid = 4326;
445 
446  te::gm::GeometryProperty* geomProp = new te::gm::GeometryProperty("spatial_data",
447  srid, terralib4::Convert2T5GeomType(geomRep));
448  mainDst->add(geomProp);
449  }
450  }
451 
452  return mainDst;
453 }
454 
455 boost::ptr_vector<te::dt::Property> terralib4::Transactor::getProperties(const std::string& datasetName)
456 {
457  std::auto_ptr<te::da::DataSetType> dt = getDataSetType(datasetName);
458 
459  std::vector<te::dt::Property*> dtProperties = dt->getProperties();
460 
461  boost::ptr_vector<te::dt::Property> properties;
462 
463  for(std::size_t i = 0; i < dtProperties.size(); ++i)
464  properties.push_back(dtProperties[i]->clone());
465 
466  return properties;
467 }
468 
469 std::auto_ptr<te::dt::Property> terralib4::Transactor::getProperty(const std::string& datasetName, const std::string& name)
470 {
471  if(!propertyExists(datasetName, name))
472  throw Exception((boost::format(TE_TR("The dataset \"%1%\" has no property with this name \"%2%\"!")) % datasetName % name).str());
473 
474  std::auto_ptr<te::da::DataSetType> dt(getDataSetType(datasetName));
475 
476  return std::auto_ptr<te::dt::Property>(dt->getProperty(name)->clone());
477 }
478 
479 std::auto_ptr<te::dt::Property> terralib4::Transactor::getProperty(const std::string& datasetName, std::size_t propertyPos)
480 {
481  std::auto_ptr<te::da::DataSetType> dt(getDataSetType(datasetName));
482 
483  assert(propertyPos < dt->size());
484 
485  return std::auto_ptr<te::dt::Property>(dt->getProperty(propertyPos)->clone());
486 }
487 
488 std::vector<std::string> terralib4::Transactor::getPropertyNames(const std::string& datasetName)
489 {
490  std::auto_ptr<te::da::DataSetType> dt(getDataSetType(datasetName));
491 
492  std::vector<std::string> pNames;
493 
494  std::size_t numProperties = dt->size();
495 
496  for(std::size_t i = 0; i < numProperties; ++i)
497  pNames.push_back(dt->getProperty(i)->getName());
498 
499  return pNames;
500 }
501 
502 std::size_t terralib4::Transactor::getNumberOfProperties(const std::string& datasetName)
503 {
504  std::auto_ptr<te::da::DataSetType> dt(getDataSetType(datasetName));
505 
506  return dt->size();
507 }
508 
509 bool terralib4::Transactor::propertyExists(const std::string& datasetName, const std::string& name)
510 {
511  std::auto_ptr<te::da::DataSetType> dt(getDataSetType(datasetName));
512 
513  std::vector<std::string> pNames = getPropertyNames(datasetName);
514 
515  if(std::find(pNames.begin(), pNames.end(), name) != pNames.end())
516  return true;
517 
518  return false;
519 }
520 
521 void terralib4::Transactor::addProperty(const std::string& datasetName, te::dt::Property* p)
522 {
523  std::string name = p->getName();
524  if(propertyExists(datasetName, name))
525  throw Exception((boost::format(TE_TR("The dataset already \"%1%\" has a property with this name \"%2%\"!")) % datasetName % name).str());
526 
527  TeAttributeRep newProperty;
528 
529  newProperty.name_ = name;
530 
531  if(p->getType() != te::dt::GEOMETRY_TYPE)
532  newProperty.type_ = terralib4::Convert2T4(p->getType());
533  else
534  {
535  te::gm::Geometry* geom = dynamic_cast<te::gm::Geometry*>(p);
536  newProperty.type_ = terralib4::Convert2T4GeomType(geom->getGeomTypeId());
537  }
538 
539  std::map<int, TeLayer*>::iterator it = m_layerMap.begin();
540 
541  TeLayer* layer = 0;
542 
543  while(it != m_layerMap.end())
544  {
545  if(it->second->name() == datasetName)
546  {
547  layer = it->second;
548  break;
549  }
550  ++it;
551  }
552 
553  TeAttrTableVector tables;
554  layer->getAttrTables(tables);
555  std::string tableName = tables[0].name();
556 
557  m_db->addColumn(tableName, newProperty);
558 }
559 
560 void terralib4::Transactor::dropProperty(const std::string&, const std::string&)
561 {
562  throw;
563 }
564 
566  const std::string&,
567  const std::string&)
568 {
569  throw Exception(TE_TR("This method is not supported by TerraLib 4.x driver!"));
570 }
571 
572 std::auto_ptr<te::da::PrimaryKey> terralib4::Transactor::getPrimaryKey(const std::string& datasetName)
573 {
574  std::auto_ptr<te::da::DataSetType> dt(getDataSetType(datasetName));
575 
576  return std::auto_ptr<te::da::PrimaryKey>(static_cast<te::da::PrimaryKey*>(dt->getPrimaryKey()->clone()));
577 }
578 
579 bool terralib4::Transactor::primaryKeyExists(const std::string& datasetName, const std::string& name)
580 {
581  std::auto_ptr<te::da::DataSetType> dt(getDataSetType(datasetName));
582 
583  if(dt->getPrimaryKey()->getName() == name)
584  return true;
585 
586  return false;
587 }
588 
590 {
591  throw Exception(TE_TR("This method is not supported by TerraLib 4.x driver!"));
592 }
593 
595 {
596  throw Exception(TE_TR("This method is not supported by TerraLib 4.x driver!"));
597 }
598 
599 std::auto_ptr<te::da::ForeignKey> terralib4::Transactor::getForeignKey(const std::string&, const std::string&)
600 {
601  throw Exception(TE_TR("This method is not supported by TerraLib 4.x driver!"));
602 }
603 
604 std::vector<std::string> terralib4::Transactor::getForeignKeyNames(const std::string&)
605 {
606  throw Exception(TE_TR("This method is not supported by TerraLib 4.x driver!"));
607 }
608 
609 bool terralib4::Transactor::foreignKeyExists(const std::string&, const std::string&)
610 {
611  throw Exception(TE_TR("This method is not supported by TerraLib 4.x driver!"));
612 }
613 
615 {
616  throw Exception(TE_TR("This method is not supported by TerraLib 4.x driver!"));
617 }
618 
619 void terralib4::Transactor::dropForeignKey(const std::string&, const std::string&)
620 {
621  throw Exception(TE_TR("This method is not supported by TerraLib 4.x driver!"));
622 }
623 
624 std::auto_ptr<te::da::UniqueKey> terralib4::Transactor::getUniqueKey(const std::string&, const std::string&)
625 {
626  throw Exception(TE_TR("This method is not supported by TerraLib 4.x driver!"));
627 }
628 
629 std::vector<std::string> terralib4::Transactor::getUniqueKeyNames(const std::string&)
630 {
631  throw Exception(TE_TR("This method is not supported by TerraLib 4.x driver!"));
632 }
633 
634 bool terralib4::Transactor::uniqueKeyExists(const std::string&, const std::string&)
635 {
636  throw Exception(TE_TR("This method is not supported by TerraLib 4.x driver!"));
637 }
638 
640 {
641  throw Exception(TE_TR("This method is not supported by TerraLib 4.x driver!"));
642 }
643 
644 void terralib4::Transactor::dropUniqueKey(const std::string&, const std::string&)
645 {
646  throw Exception(TE_TR("This method is not supported by TerraLib 4.x driver!"));
647 }
648 
649 std::auto_ptr<te::da::CheckConstraint> terralib4::Transactor::getCheckConstraint(const std::string&, const std::string&)
650 {
651  throw Exception(TE_TR("This method is not supported by TerraLib 4.x driver!"));
652 }
653 
654 std::vector<std::string> terralib4::Transactor::getCheckConstraintNames(const std::string&)
655 {
656  throw Exception(TE_TR("This method is not supported by TerraLib 4.x driver!"));
657 }
658 
659 bool terralib4::Transactor::checkConstraintExists(const std::string&, const std::string&)
660 {
661  throw Exception(TE_TR("This method is not supported by TerraLib 4.x driver!"));
662 }
663 
665 {
666  throw Exception(TE_TR("This method is not supported by TerraLib 4.x driver!"));
667 }
668 
669 void terralib4::Transactor::dropCheckConstraint(const std::string&, const std::string&)
670 {
671  throw Exception(TE_TR("This method is not supported by TerraLib 4.x driver!"));
672 }
673 
674 std::auto_ptr<te::da::Index> terralib4::Transactor::getIndex(const std::string&, const std::string&)
675 {
676  throw Exception(TE_TR("This method is not supported by TerraLib 4.x driver!"));
677 }
678 
679 std::vector<std::string> terralib4::Transactor::getIndexNames(const std::string&)
680 {
681  throw Exception(TE_TR("This method is not supported by TerraLib 4.x driver!"));
682 }
683 
684 bool terralib4::Transactor::indexExists(const std::string&, const std::string&)
685 {
686  throw Exception(TE_TR("This method is not supported by TerraLib 4.x driver!"));
687 }
688 
690  const std::map<std::string, std::string>&)
691 {
692  throw Exception(TE_TR("This method is not supported by TerraLib 4.x driver!"));
693 }
694 
695 void terralib4::Transactor::dropIndex(const std::string&, const std::string&)
696 {
697  throw Exception(TE_TR("This method is not supported by TerraLib 4.x driver!"));
698 }
699 
700 std::auto_ptr<te::da::Sequence> terralib4::Transactor::getSequence(const std::string&)
701 {
702  throw Exception(TE_TR("This method is not supported by TerraLib 4.x driver!"));
703 }
704 
705 std::vector<std::string> terralib4::Transactor::getSequenceNames()
706 {
707  throw Exception(TE_TR("This method is not supported by TerraLib 4.x driver!"));
708 }
709 
711 {
712  throw Exception(TE_TR("This method is not supported by TerraLib 4.x driver!"));
713 }
714 
716 {
717  throw Exception(TE_TR("This method is not supported by TerraLib 4.x driver!"));
718 }
719 
720 void terralib4::Transactor::dropSequence(const std::string&)
721 {
722  throw Exception(TE_TR("This method is not supported by TerraLib 4.x driver!"));
723 }
724 
725 std::auto_ptr<te::gm::Envelope> terralib4::Transactor::getExtent(const std::string&,
726  const std::string&)
727 {
728  throw Exception(TE_TR("This method is not supported by TerraLib 4.x driver!"));
729 }
730 
731 std::auto_ptr<te::gm::Envelope> terralib4::Transactor::getExtent(const std::string&,
732  std::size_t)
733 {
734  throw Exception(TE_TR("This method is not supported by TerraLib 4.x driver!"));
735 }
736 
737 std::size_t terralib4::Transactor::getNumberOfItems(const std::string&)
738 {
739  throw Exception(TE_TR("This method is not supported by TerraLib 4.x driver!"));
740 }
741 
743 {
744  throw Exception(TE_TR("This method is not supported by TerraLib 4.x driver!"));
745 }
746 
747 bool terralib4::Transactor::dataSetExists(const std::string& name)
748 {
749  std::vector<std::string> names = getDataSetNames();
750 
751  for(std::size_t i = 0; i < names.size(); ++i)
752  if(names[i] == name)
753  return true;
754 
755  return false;
756 }
757 
758 void terralib4::Transactor::createDataSet(te::da::DataSetType*, const std::map<std::string, std::string>&)
759 {
760  throw Exception(TE_TR("This method is not supported by TerraLib 4.x driver!"));
761 }
762 
763 void terralib4::Transactor::cloneDataSet(const std::string&,
764  const std::string&,
765  const std::map<std::string, std::string>&)
766 {
767  throw Exception(TE_TR("This method is not supported by TerraLib 4.x driver!"));
768 }
769 
770 void terralib4::Transactor::dropDataSet(const std::string&)
771 {
772  throw Exception(TE_TR("This method is not supported by TerraLib 4.x driver!"));
773 }
774 
775 void terralib4::Transactor::renameDataSet(const std::string&, const std::string&)
776 {
777  throw Exception(TE_TR("This method is not supported by TerraLib 4.x driver!"));
778 }
779 
780 void terralib4::Transactor::add(const std::string&,
782  const std::map<std::string, std::string>&,
783  std::size_t)
784 {
785  throw Exception(TE_TR("This method is not supported by TerraLib 4.x driver!"));
786 }
787 
788 void terralib4::Transactor::remove(const std::string&, const te::da::ObjectIdSet*)
789 {
790  throw Exception(TE_TR("This method is not supported by TerraLib 4.x driver!"));
791 }
792 
793 void terralib4::Transactor::update(const std::string&,
795  const std::vector<std::size_t>&,
796  const te::da::ObjectIdSet*,
797  const std::map<std::string, std::string>&,
798  std::size_t)
799 {
800  throw Exception(TE_TR("This method is not supported by TerraLib 4.x driver!"));
801 }
802 
803 void terralib4::Transactor::optimize(const std::map<std::string, std::string>&)
804 {
805  throw Exception(TE_TR("This method is not supported by TerraLib 4.x driver!"));
806 }
807 
808 std::vector<std::string> terralib4::Transactor::getTL4Layers()
809 {
810  std::vector<std::string> layers;
811 
812  std::map<int, TeLayer*>::iterator it = m_layerMap.begin();
813 
814  while(it != m_layerMap.end())
815  {
816  if(!it->second->hasGeometry(TeRASTER))
817  layers.push_back(it->second->name());
818 
819  ++it;
820  }
821 
822  return layers;
823 }
824 
825 std::vector<std::string> terralib4::Transactor::getTL4Tables()
826 {
827  std::vector<std::string> tablesVec;
828 
829  TeAttrTableVector tables;
830  m_db->getAttrTables(tables, TeAttrExternal);
831 
832  for(std::size_t i = 0; i < tables.size(); i++)
833  tablesVec.push_back(tables[i].name());
834 
835  return tablesVec;
836 }
837 
838 std::vector<std::string> terralib4::Transactor::getTL4Rasters()
839 {
840  std::vector<std::string> rasters;
841 
842  std::map<int, TeLayer*>::iterator it = m_layerMap.begin();
843 
844  while(it != m_layerMap.end())
845  {
846  if(it->second->hasGeometry(TeRASTER))
847  {
848  rasters.push_back(it->second->name());
849  }
850  ++it;
851  }
852 
853  return rasters;
854 }
855 
856 std::vector<::terralib4::ThemeInfo> terralib4::Transactor::getTL4Themes()
857 {
858  std::vector<::terralib4::ThemeInfo> themes;
859 
860  TeViewMap& vMap = m_db->viewMap();
861  TeThemeMap& tMap = m_db->themeMap();
862 
863  std::map<int, TeView*>::iterator it = vMap.begin();
864 
865  while(it != vMap.end())
866  {
867  TeView* view = it->second;
868 
869  std::map<int, TeAbstractTheme*>::iterator itT = tMap.begin();
870 
871  while(itT != tMap.end())
872  {
873  TeAbstractTheme* abTheme = itT->second;
874 
875  if(abTheme->type() == TeTHEME)
876  {
877  TeTheme* theme = dynamic_cast<TeTheme*>(abTheme);
878 
879  if(theme->view() == view->id())
880  {
881  ::terralib4::ThemeInfo themeInfo;
882  themeInfo.m_name = theme->name();
883  themeInfo.m_viewName = view->name();
884  themeInfo.m_layerName = theme->layer()->name();
885 
886  themes.push_back(themeInfo);
887  }
888  }
889  ++itT;
890  }
891  ++it;
892  }
893 
894  return themes;
895 }
896 
897 TeTheme* terralib4::Transactor::getTL4Theme(const ::terralib4::ThemeInfo& theme)
898 {
899  std::map<int, TeAbstractTheme*>::iterator it = m_themeMap.begin();
900 
901  while(it != m_themeMap.end())
902  {
903  if(it->second->view() == getViewId(theme.m_viewName, m_viewMap))
904  {
905  TeAbstractTheme* abTheme = it->second;
906 
907  if(abTheme->type() == TeTHEME)
908  {
909  TeTheme* tl4Theme = dynamic_cast<TeTheme*>(abTheme);
910 
911  if(tl4Theme->layer()->name() == theme.m_layerName)
912  {
913  return tl4Theme;
914  }
915  }
916  }
917 
918  ++it;
919  }
920 
921  return 0;
922 }
923 
924 int terralib4::Transactor::getLayerSRID(const std::string & layerName)
925 {
926  std::map<int, TeLayer*>::iterator it = m_layerMap.begin();
927 
928  while(it != m_layerMap.end())
929  {
930  if(it->second->name() == layerName)
931  {
932  int srid = it->second->projection()->epsgCode();
933  if(srid == 4979)
934  srid = 4326;
935 
936  return srid;
937  }
938 
939  ++it;
940  }
941 
942  return 0;
943 }
944 
946 {
947  return te::common::UTF8;
948 }
TeAttrDataType Convert2T4GeomType(te::gm::GeomType type)
Definition: Utils.cpp:329
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:599
std::vector< std::string > getTL4Rasters()
Definition: Transactor.cpp:838
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:684
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:521
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:763
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:579
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:509
std::vector< std::string > getForeignKeyNames(const std::string &datasetName)
It gets the foreign key names of the given dataset.
Definition: Transactor.cpp:604
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:758
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:634
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:695
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:565
bool dataSetExists(const std::string &name)
It checks if a dataset with the given name exists in the data source.
Definition: Transactor.cpp:747
bool hasDataSets()
It checks if the data source has any dataset.
Definition: Transactor.cpp:742
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:803
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:346
std::vector< std::string > getTL4Tables()
Definition: Transactor.cpp:825
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:455
void dropForeignKey(const std::string &datasetName, const std::string &fkName)
It removes the foreign key constraint from the dataset schema.
Definition: Transactor.cpp:619
std::vector< std::string > getPropertyNames(const std::string &datasetName)
It gets the property names of the given dataset.
Definition: Transactor.cpp:488
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
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:788
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:793
std::auto_ptr< te::da::PrimaryKey > getPrimaryKey(const std::string &datasetName)
It retrieves the primary key of the dataset.
Definition: Transactor.cpp:572
void renameDataSet(const std::string &name, const std::string &newName)
It renames a dataset.
Definition: Transactor.cpp:775
void setName(const std::string &name)
It sets the property name.
Definition: Property.h:137
std::vector< std::string > getCheckConstraintNames(const std::string &datasetName)
It gets the check constraint names of the given dataset.
Definition: Transactor.cpp:654
te::gm::GeomType Convert2T5GeomType(TeAttrDataType type)
Definition: Utils.cpp:197
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:55
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:700
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:609
bool sequenceExists(const std::string &name)
It checks if a sequence with the given name exists in the data source.
Definition: Transactor.cpp:710
TeTheme * getTL4Theme(const ::terralib4::ThemeInfo &theme)
Definition: Transactor.cpp:897
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:589
std::size_t getNumberOfProperties(const std::string &datasetName)
It gets the number of properties of the given dataset.
Definition: Transactor.cpp:502
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:649
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:594
std::vector< std::string > getIndexNames(const std::string &datasetName)
It gets the index names of the given dataset.
Definition: Transactor.cpp:679
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:689
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
std::vector< std::string > getUniqueKeyNames(const std::string &datasetName)
It gets the unique key names of the given dataset.
Definition: Transactor.cpp:629
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:737
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:614
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:624
te::common::CharEncoding getEncoding()
It return the DataSource current encoding.
Definition: Transactor.cpp:945
int getType() const
It returns the property data type.
Definition: Property.h:161
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:715
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:469
int getLayerSRID(const std::string &layerName)
Definition: Transactor.cpp:924
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:669
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:674
void dropUniqueKey(const std::string &datasetName, const std::string &name)
It removes the unique key constraint from the dataset.
Definition: Transactor.cpp:644
void addCheckConstraint(const std::string &datasetName, te::da::CheckConstraint *cc)
It adds a check constraint to the dataset.
Definition: Transactor.cpp:664
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:287
std::vector< std::string > getTL4Layers()
Definition: Transactor.cpp:808
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:705
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:780
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:725
void dropDataSet(const std::string &name)
It removes the dataset schema from the data source.
Definition: Transactor.cpp:770
void dropSequence(const std::string &name)
It removes the sequence from the data source.
Definition: Transactor.cpp:720
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:639
std::vector<::terralib4::ThemeInfo > getTL4Themes()
Definition: Transactor.cpp:856
void dropProperty(const std::string &datasetName, const std::string &name)
It removes a property from the given dataset.
Definition: Transactor.cpp:560
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:659
const std::string & getName() const
It returns the property name.
Definition: Property.h:127