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 // TerraLib
21 #include "../common/Translator.h"
22 #include "../dataaccess/dataset/ObjectId.h"
23 #include "../dataaccess/dataset/ObjectIdSet.h"
24 #include "../dataaccess/query/DataSetName.h"
25 #include "../dataaccess/query/Query.h"
26 #include "../dataaccess/query/Select.h"
27 #include "../dataaccess/utils/Utils.h"
28 #include "../datatype/ByteArray.h"
29 #include "../datatype/Date.h"
30 #include "../datatype/TimeInstant.h"
31 #include "../geometry/Envelope.h"
32 #include "../geometry/GeometryProperty.h"
33 #include "../srs/SpatialReferenceSystemManager.h"
34 #include "../srs/Config.h"
35 #include "DataSource.h"
36 #include "DataSet.h"
37 #include "SQLVisitor.h"
38 #include "Transactor.h"
39 #include "Utils.h"
40 
41 // OGR
42 #include <ogrsf_frmts.h>
43 
44 OGRFieldType GetOGRType(int te_type)
45 {
46  switch (te_type)
47  {
48  case te::dt::CHAR_TYPE:
49  case te::dt::UCHAR_TYPE:
51  return OFTString;
52  break;
53 
54  case te::dt::INT16_TYPE:
56  case te::dt::INT32_TYPE:
58  case te::dt::INT64_TYPE:
60  return OFTInteger;
61  break;
62 
63  case te::dt::FLOAT_TYPE:
66  return OFTReal;
67  break;
68 
70  return OFTDateTime;
71  break;
72  };
73 
74  return OFTInteger;
75 }
76 
78  : te::da::DataSourceTransactor(),
79  m_ogrDs(ds)
80 {
81 }
82 
84 {
85 }
86 
88 {
89  return 0;
90 }
91 
93 {
94 }
95 
97 {
98  if (!m_ogrDs->getOGRDataSource())
99  return;
100 
101  // we have to reopen datasource so pending data gets synched to disk!
102  m_ogrDs->open();
103 }
104 
106 {
107 }
108 
110 {
111  return false;
112 }
113 
114 std::auto_ptr<te::da::DataSet> te::ogr::Transactor::getDataSet(const std::string& name,
115  te::common::TraverseType /*travType*/,
116  bool /*connected*/,
118 {
119  if (!m_ogrDs->getOGRDataSource())
120  return std::auto_ptr<te::da::DataSet>();
121 
122  OGRDataSource* ds = m_ogrDs->getOGRDataSource();
123 // OGRDataSource* ds = OGRSFDriverRegistrar::Open(m_ogrDs->getOGRDataSource()->GetName());
124 
125  std::string sql = "SELECT FID, * FROM \'" + name + "\'";
126  OGRLayer* layer = ds->ExecuteSQL(sql.c_str(), 0, 0);
127 
128  if(layer == 0)
129  throw Exception(TE_TR("The informed data set could not be found in the data source."));
130 
131  return std::auto_ptr<te::da::DataSet>(new DataSet(ds, layer));
132 }
133 
134 std::auto_ptr<te::da::DataSet> te::ogr::Transactor::getDataSet(const std::string& name,
135  const std::string& /*propertyName*/,
136  const te::gm::Envelope* e,
138  te::common::TraverseType /*travType*/,
139  bool /*connected*/,
141 {
142  if (!m_ogrDs->getOGRDataSource())
143  return std::auto_ptr<te::da::DataSet>();
144 
145  OGRDataSource* ds = m_ogrDs->getOGRDataSource();
146 // OGRDataSource* ds = OGRSFDriverRegistrar::Open(m_ogrDs->getOGRDataSource()->GetName());
147 
148  std::string sql = "SELECT FID, * FROM \'" + name + "\'";
149  OGRLayer* layer = ds->ExecuteSQL(sql.c_str(), 0, 0);
150 
151  if(layer == 0)
152  throw Exception(TE_TR("The informed data set could not be found in the data source."));
153 
154  layer->SetSpatialFilterRect(e->m_llx, e->m_lly, e->m_urx, e->m_ury);
155 
156  return std::auto_ptr<te::da::DataSet>(new DataSet(ds, layer));
157 }
158 
159 std::auto_ptr<te::da::DataSet> te::ogr::Transactor::getDataSet(const std::string& name,
160  const std::string& /*propertyName*/,
161  const te::gm::Geometry* g,
163  te::common::TraverseType /*travType*/,
164  bool /*connected*/,
166 {
167  if (!m_ogrDs->getOGRDataSource())
168  return std::auto_ptr<te::da::DataSet>();
169 
170  OGRDataSource* ds = m_ogrDs->getOGRDataSource();
171 // OGRDataSource* ds = OGRSFDriverRegistrar::Open(m_ogrDs->getOGRDataSource()->GetName());
172 
173  std::string sql = "SELECT FID, * FROM \'" + name + "\'";
174  OGRLayer* layer = ds->ExecuteSQL(sql.c_str(), 0, 0);
175 
176  if(layer == 0)
177  throw Exception(TE_TR("The informed data set could not be found in the data source."));
178 
179  OGRGeometry* ogrg = Convert2OGR(g);
180 
181  layer->SetSpatialFilter(ogrg);
182 
183  OGRGeometryFactory::destroyGeometry(ogrg);
184 
185  return std::auto_ptr<te::da::DataSet>(new DataSet(ds, layer));
186 }
187 
188 std::auto_ptr<te::da::DataSet> te::ogr::Transactor::query(const te::da::Select& q,
189  te::common::TraverseType /*travType*/,
190  bool /*connected*/,
192 {
193  if (!m_ogrDs->getOGRDataSource())
194  return std::auto_ptr<te::da::DataSet>();
195 
196  OGRDataSource* ds = OGRSFDriverRegistrar::Open(m_ogrDs->getOGRDataSource()->GetName());
197 
198  std::string sql;
199 
200  SQLVisitor visitor(*m_ogrDs->getDialect(), sql);
201 
202  q.accept(visitor);
203 
204  sql = RemoveSpatialSql(sql);
205 
206  OGRLayer* layer = ds->ExecuteSQL(sql.c_str(), 0, 0);
207 
208  if(layer == 0)
209  throw Exception(TE_TR("Could not retrieve the DataSet from data source."));
210 
211  te::gm::Envelope* e = visitor.getMBR();
212 
213  if(e != 0)
214  layer->SetSpatialFilterRect(e->m_llx, e->m_lly, e->m_urx, e->m_ury);
215 
216  return std::auto_ptr<te::da::DataSet>(new DataSet(ds, layer));
217 }
218 
219 std::auto_ptr<te::da::DataSet> te::ogr::Transactor::query(const std::string& query,
220  te::common::TraverseType /*travType*/,
221  bool /*connected*/,
223 {
224  if (!m_ogrDs->getOGRDataSource())
225  return std::auto_ptr<te::da::DataSet>();
226 
227  OGRDataSource* ds = OGRSFDriverRegistrar::Open(m_ogrDs->getOGRDataSource()->GetName());
228 
229  // Adding FID attribute case "SELECT *"
230  std::string queryCopy = query;
231  std::size_t pos = queryCopy.find("*");
232  if(pos != std::string::npos)
233  {
234  std::string fid = "FID, *";
235  queryCopy.replace(pos, 1, fid);
236  }
237 
238  OGRLayer* layer = ds->ExecuteSQL(queryCopy.c_str(), 0, 0);
239 
240  if(layer == 0)
241  throw Exception(TE_TR("Could not retrieve the DataSet from data source."));
242 
243  return std::auto_ptr<te::da::DataSet>(new DataSet(ds, layer));
244 }
245 
247 {
248  std::string sql;
249  SQLVisitor v(*m_ogrDs->getDialect(), sql);
250 
251  command.accept(v);
252 
253  execute(sql);
254 }
255 
256 void te::ogr::Transactor::execute(const std::string& command)
257 {
258  if (!m_ogrDs->getOGRDataSource())
259  return;
260 
261  OGRLayer* layer = m_ogrDs->getOGRDataSource()->ExecuteSQL(command.c_str(), 0, "");
262 
263  if(layer != 0)
264  m_ogrDs->getOGRDataSource()->ReleaseResultSet(layer);
265 }
266 
267 std::auto_ptr<te::da::PreparedQuery> te::ogr::Transactor::getPrepared(const std::string& /*qName*/)
268 {
269  return std::auto_ptr<te::da::PreparedQuery>(0);
270 }
271 
272 std::auto_ptr<te::da::BatchExecutor> te::ogr::Transactor::getBatchExecutor()
273 {
274  return std::auto_ptr<te::da::BatchExecutor>(0);
275 }
276 
278 {
279 }
280 
282 {
283  return 0;
284 }
285 
286 std::string te::ogr::Transactor::escape(const std::string& value)
287 {
288  return value;
289 }
290 
291 std::vector<std::string> te::ogr::Transactor::getDataSetNames()
292 {
293  std::vector<std::string> names;
294 
295  if (!m_ogrDs->getOGRDataSource())
296  return names;
297 
298  for(int i=0; i<m_ogrDs->getOGRDataSource()->GetLayerCount(); i++)
299  names.push_back(m_ogrDs->getOGRDataSource()->GetLayer(i)->GetName());
300 
301  return names;
302 }
303 
305 {
306  if (!m_ogrDs->getOGRDataSource())
307  return 0;
308 
309  return m_ogrDs->getOGRDataSource()->GetLayerCount();
310 }
311 
312 std::auto_ptr<te::da::DataSetType> te::ogr::Transactor::getDataSetType(const std::string& name)
313 {
314  if (!m_ogrDs->getOGRDataSource())
315  return std::auto_ptr<te::da::DataSetType>();
316 
317  std::string sql("SELECT FID, * FROM \'");
318  sql += name + "\'";
319 
320  OGRLayer* l = m_ogrDs->getOGRDataSource()->ExecuteSQL(sql.c_str(), 0, 0);
321 
322  if(l==0)
323  return std::auto_ptr<te::da::DataSetType>();
324 
325  std::auto_ptr<te::da::DataSetType> type(Convert2TerraLib(l->GetLayerDefn()));
326 
327  type->setName(name);
328 
329  const char* colIdName = l->GetFIDColumn();
330 
331  if(colIdName == 0 || colIdName[0] == '\0')
332  colIdName = "FID";
333 
334  int pos = l->GetLayerDefn()->GetFieldIndex(colIdName);
335  if(pos >= 0)
336  {
337  te::da::PrimaryKey* pk = new te::da::PrimaryKey(colIdName, type.get());
338  pk->add(type->getProperty(pos));
339  }
340 
341  int srs = te::ogr::Convert2TerraLibProjection(l->GetSpatialRef());
342 
344 
345  if(gp != 0)
346  gp->setSRID(srs);
347 
348  m_ogrDs->getOGRDataSource()->ReleaseResultSet(l);
349 
350  return type;
351 }
352 
353 std::auto_ptr<te::da::DataSetTypeCapabilities> te::ogr::Transactor::getCapabilities(const std::string &name)
354 {
355  std::auto_ptr<te::da::DataSetTypeCapabilities> cap(new te::da::DataSetTypeCapabilities);
356 
357  OGRLayer* l = m_ogrDs->getOGRDataSource()->GetLayerByName(name.c_str());
358 
359  if(l != 0)
360  {
361  cap->setSupportAddColumn((l->TestCapability(OLCCreateField) == 0) ? false : true);
362  cap->setSupportRemoveColumn((l->TestCapability(OLCDeleteField) == 0) ? false : true);
363  cap->setSupportDataEdition((l->TestCapability(OLCRandomWrite) == 0) ? false : true);
364  }
365 
366  return cap;
367 }
368 
369 boost::ptr_vector<te::dt::Property> te::ogr::Transactor::getProperties(const std::string& datasetName)
370 {
371  boost::ptr_vector<te::dt::Property> ps;
372 
373  if (!m_ogrDs->getOGRDataSource())
374  return ps;
375 
376  std::string sql("SELECT FID, * FROM \'");
377  sql += datasetName + "\'";
378 
379  OGRLayer* l = m_ogrDs->getOGRDataSource()->ExecuteSQL(sql.c_str(), 0, 0);
380 
381  if(l!=0)
382  {
383  int srs = te::ogr::Convert2TerraLibProjection(l->GetSpatialRef());
384  std::auto_ptr<te::da::DataSetType> dt(Convert2TerraLib(l->GetLayerDefn(),srs));
385  std::vector<te::dt::Property*> props = dt->getProperties();
386  std::vector<te::dt::Property*>::iterator it;
387 
388  for(it=props.begin(); it!=props.end(); ++it)
389  ps.push_back((*it)->clone());
390  }
391 
392  m_ogrDs->getOGRDataSource()->ReleaseResultSet(l);
393 
394  return ps;
395 }
396 
397 std::auto_ptr<te::dt::Property> te::ogr::Transactor::getProperty(const std::string& datasetName, const std::string& name)
398 {
399  if (!m_ogrDs->getOGRDataSource())
400  return std::auto_ptr<te::dt::Property>();
401 
402  int idx = -1;
403  std::string sql("SELECT FID, * FROM \'");
404  sql += datasetName + "\'";
405 
406  OGRLayer* l = m_ogrDs->getOGRDataSource()->ExecuteSQL(sql.c_str(), 0, 0);
407 
408  if(l != 0)
409  idx = l->GetLayerDefn()->GetFieldIndex(name.c_str());
410 
411  m_ogrDs->getOGRDataSource()->ReleaseResultSet(l);
412 
413  return getProperty(datasetName, idx);
414 }
415 
416 std::auto_ptr<te::dt::Property> te::ogr::Transactor::getProperty(const std::string& datasetName, std::size_t propertyPos)
417 {
418  OGRDataSource* ogrds = m_ogrDs->getOGRDataSource();
419  if (!ogrds)
420  return std::auto_ptr<te::dt::Property>();
421 
422  std::auto_ptr<te::dt::Property> res;
423  std::string sql ("SELECT FID, * FROM \'");
424  sql += datasetName + "\'";
425 
426  OGRLayer* l = ogrds->ExecuteSQL(sql.c_str(), 0, 0);
427 
428  if(l != 0)
429  {
430  OGRFeatureDefn* def = l->GetLayerDefn();
431  OGRFieldDefn* fdef = def->GetFieldDefn(propertyPos);
432 
433  if(fdef != 0)
434  res.reset(Convert2TerraLib(fdef));
435  }
436 
437  m_ogrDs->getOGRDataSource()->ReleaseResultSet(l);
438 
439  return res;
440 }
441 
442 std::vector<std::string> te::ogr::Transactor::getPropertyNames(const std::string& datasetName)
443 {
444  OGRDataSource* ogrds = m_ogrDs->getOGRDataSource();
445  if (!ogrds)
446  return std::vector<std::string>();
447 
448  std::vector<std::string> res;
449  std::string sql ("SELECT FID, * FROM \'");
450  sql += datasetName + "\'";
451 
452  OGRLayer* l = ogrds->ExecuteSQL(sql.c_str(), 0, 0);
453 
454  if(l != 0)
455  {
456  OGRFeatureDefn* def = l->GetLayerDefn();
457 
458  for(int i=0; i<def->GetFieldCount(); i++)
459  res.push_back(def->GetFieldDefn(i)->GetNameRef());
460  }
461 
462  ogrds->ReleaseResultSet(l);
463 
464  return res;
465 }
466 
467 std::size_t te::ogr::Transactor::getNumberOfProperties(const std::string& datasetName)
468 {
469  OGRDataSource* ogrds = m_ogrDs->getOGRDataSource();
470  if (!ogrds)
471  return 0;
472 
473  std::string sql("SELECT FID, * FROM \'");
474  sql += datasetName + "\'";
475 
476  OGRLayer* l = ogrds->ExecuteSQL(sql.c_str(), 0, 0);
477 
478  int res = 0;
479 
480  if(l != 0)
481  {
482  res = l->GetLayerDefn()->GetFieldCount();
483  ogrds->ReleaseResultSet(l);
484  }
485 
486  return res;
487 }
488 
489 bool te::ogr::Transactor::propertyExists(const std::string& datasetName, const std::string& name)
490 {
491  OGRDataSource* ogrds = m_ogrDs->getOGRDataSource();
492  if (!ogrds)
493  return false;
494 
495  std::string sql("SELECT FID, * FROM \'");
496  sql += datasetName + "\'";
497  bool res = false;
498 
499  OGRLayer* l = ogrds->ExecuteSQL(sql.c_str(), 0, 0);
500 
501  if(l != 0)
502  {
503  res = (l->GetLayerDefn()->GetFieldIndex(name.c_str()) != -1);
504  ogrds->ReleaseResultSet(l);
505  }
506 
507  return res;
508 }
509 
510 void te::ogr::Transactor::addProperty(const std::string& datasetName, te::dt::Property* p)
511 {
512  if (!m_ogrDs->getOGRDataSource())
513  return;
514 
515  OGRLayer* l = m_ogrDs->getOGRDataSource()->GetLayerByName(datasetName.c_str());
516 
517  if(l != 0)
518  {
519  if(p->getType() != te::dt::GEOMETRY_TYPE)
520  {
521 // if(!l->TestCapability(OLCCreateField))
522 // throw Exception(TE_TR("This dataset do not support add fields operation."));
523 
524  OGRFieldDefn* nField = Convert2OGR(p);
525  OGRErr error = l->CreateField(nField);
526 
527  delete nField;
528 
529  if(error != OGRERR_NONE)
530  throw Exception(TE_TR("Error when attempting add the property: " + p->getName() + "."));
531 
532  error = l->SyncToDisk();
533 
534  if(error != OGRERR_NONE)
535  throw Exception(TE_TR("Error saving changes on the file."));
536  }
537  }
538 }
539 
540 void te::ogr::Transactor::dropProperty(const std::string& datasetName, const std::string& name)
541 {
542  OGRLayer* l = m_ogrDs->getOGRDataSource()->GetLayerByName(datasetName.c_str());
543 
544  if(l != 0)
545  {
546  if(!l->TestCapability(OLCDeleteField))
547  throw Exception(TE_TR("This dataset do not support remove properties operation."));
548 
549  int fPos = l->GetLayerDefn()->GetFieldIndex(name.c_str());
550 
551  if(fPos < 0)
552  throw Exception(TE_TR("Field not found."));
553 
554  OGRErr error = l->DeleteField(fPos);
555 
556  if(error != OGRERR_NONE)
557  throw Exception(TE_TR("Error when attempting remove the property."));
558 
559  error = l->SyncToDisk();
560 
561  if(error != OGRERR_NONE)
562  throw Exception(TE_TR("Error saving changes on the file."));
563  }
564 }
565 
566 void te::ogr::Transactor::renameProperty(const std::string& datasetName,
567  const std::string& propertyName,
568  const std::string& newPropertyName)
569 {
570  if (!m_ogrDs->getOGRDataSource())
571  return;
572 
573  OGRLayer* l = m_ogrDs->getOGRDataSource()->GetLayerByName(datasetName.c_str());
574 
575  if(l != 0)
576  {
577  int idx = l->GetLayerDefn()->GetFieldIndex(propertyName.c_str());
578 
579  if(idx == -1)
580  throw Exception(TE_TR("Field to be renamed does not exists."));
581 
582  OGRFieldDefn* df = l->GetLayerDefn()->GetFieldDefn(idx);
583 
584  OGRFieldDefn* dfn = new OGRFieldDefn(df);
585 
586  dfn->SetName(newPropertyName.c_str());
587 
588  OGRErr err = l->AlterFieldDefn(idx, dfn, ALTER_NAME_FLAG);
589 
590  if(err != OGRERR_NONE)
591  throw Exception(TE_TR("Fail to rename field."));
592  }
593 }
594 
595 void te::ogr::Transactor::changePropertyDefinition(const std::string& datasetName, const std::string& propName, te::dt::Property* newProp)
596 {
597  if (!m_ogrDs->getOGRDataSource())
598  return;
599 
600  std::auto_ptr<te::dt::Property> p;
601 
602  p.reset(newProp);
603 
604  OGRLayer* l = m_ogrDs->getOGRDataSource()->GetLayerByName(datasetName.c_str());
605 
606  if(l != 0)
607  {
608  if(!l->TestCapability(OLCAlterFieldDefn))
609  throw Exception(TE_TR("This data source do not support the operation of alter columns type."));
610 
611  int idx = l->GetLayerDefn()->GetFieldIndex(propName.c_str());
612 
613  if(idx == -1)
614  throw Exception(TE_TR("Field to be renamed does not exists."));
615 
616  OGRFieldDefn* dfn = new OGRFieldDefn(l->GetLayerDefn()->GetFieldDefn(idx));
617 
618  dfn->SetType(GetOGRType(newProp->getType()));
619 
620  OGRErr err = l->AlterFieldDefn(idx, dfn, ALTER_TYPE_FLAG);
621 
622  if(err != OGRERR_NONE)
623  throw Exception(TE_TR("Fail to to change field type."));
624 
625  std::string name = m_ogrDs->getOGRDataSource()->GetName();
626 
627  err = l->SyncToDisk();
628  }
629 }
630 
631 std::auto_ptr<te::da::PrimaryKey> te::ogr::Transactor::getPrimaryKey(const std::string& datasetName)
632 {
633  if (!m_ogrDs->getOGRDataSource())
634  return std::auto_ptr<te::da::PrimaryKey>();
635 
636  std::auto_ptr<te::da::PrimaryKey> res;
637  std::string sql("SELECT FID, * FROM \'");
638  sql += datasetName + "\'";
639 
640  OGRLayer* layer = m_ogrDs->getOGRDataSource()->ExecuteSQL(sql.c_str(), 0, 0);
641 
642  if(layer != 0)
643  {
644  const char* colIdName = layer->GetFIDColumn();
645 
646  if(colIdName == 0 || colIdName[0] == '\0')
647  colIdName = "FID";
648 
649  int pos = layer->GetLayerDefn()->GetFieldIndex(colIdName);
650 
651  if(pos >= 0)
652  {
653  res.reset(new te::da::PrimaryKey);
654  res->add(getProperty(datasetName, pos).get());
655  }
656  }
657 
658  m_ogrDs->getOGRDataSource()->ReleaseResultSet(layer);
659 
660  return res;
661 }
662 
663 bool te::ogr::Transactor::primaryKeyExists(const std::string& /*datasetName*/, const std::string& /*name*/)
664 {
665  return false;
666 }
667 
668 void te::ogr::Transactor::addPrimaryKey(const std::string& /*datasetName*/, te::da::PrimaryKey* /*pk*/)
669 {
670 }
671 
672 void te::ogr::Transactor::dropPrimaryKey(const std::string& /*datasetName*/)
673 {
674 }
675 
676 std::auto_ptr<te::da::ForeignKey> te::ogr::Transactor::getForeignKey(const std::string& /*datasetName*/, const std::string& /*name*/)
677 {
678  return std::auto_ptr<te::da::ForeignKey>();
679 }
680 
681 std::vector<std::string> te::ogr::Transactor::getForeignKeyNames(const std::string& /*datasetName*/)
682 {
683  return std::vector<std::string>();
684 }
685 
686 bool te::ogr::Transactor::foreignKeyExists(const std::string& /*datasetName*/, const std::string& /*name*/)
687 {
688  return false;
689 }
690 
691 void te::ogr::Transactor::addForeignKey(const std::string& /*datasetName*/, te::da::ForeignKey* /*fk*/)
692 {
693 }
694 
695 void te::ogr::Transactor::dropForeignKey(const std::string& /*datasetName*/, const std::string& /*fkName*/)
696 {
697 }
698 
699 std::auto_ptr<te::da::UniqueKey> te::ogr::Transactor::getUniqueKey(const std::string& /*datasetName*/, const std::string& /*name*/)
700 {
701  return std::auto_ptr<te::da::UniqueKey>();
702 }
703 
704 std::vector<std::string> te::ogr::Transactor::getUniqueKeyNames(const std::string& /*datasetName*/)
705 {
706  return std::vector<std::string>();
707 }
708 
709 bool te::ogr::Transactor::uniqueKeyExists(const std::string& /*datasetName*/, const std::string& /*name*/)
710 {
711  return false;
712 }
713 
714 void te::ogr::Transactor::addUniqueKey(const std::string& /*datasetName*/, te::da::UniqueKey* /*uk*/)
715 {
716 }
717 
718 void te::ogr::Transactor::dropUniqueKey(const std::string& /*datasetName*/, const std::string& /*name*/)
719 {
720 }
721 
722 std::auto_ptr<te::da::CheckConstraint> te::ogr::Transactor::getCheckConstraint(const std::string& /*datasetName*/, const std::string& /*name*/)
723 {
724  return std::auto_ptr<te::da::CheckConstraint>();
725 }
726 
727 std::vector<std::string> te::ogr::Transactor::getCheckConstraintNames(const std::string& /*datasetName*/)
728 {
729  return std::vector<std::string>();
730 }
731 
732 bool te::ogr::Transactor::checkConstraintExists(const std::string& /*datasetName*/, const std::string& /*name*/)
733 {
734  return false;
735 }
736 
737 void te::ogr::Transactor::addCheckConstraint(const std::string& /*datasetName*/, te::da::CheckConstraint* /*cc*/)
738 {
739 }
740 
741 void te::ogr::Transactor::dropCheckConstraint(const std::string& /*datasetName*/, const std::string& /*name*/)
742 {
743 }
744 
745 std::auto_ptr<te::da::Index> te::ogr::Transactor::getIndex(const std::string& /*datasetName*/, const std::string& /*name*/)
746 {
747  return std::auto_ptr<te::da::Index>();
748 }
749 
750 std::vector<std::string> te::ogr::Transactor::getIndexNames(const std::string& /*datasetName*/)
751 {
752  return std::vector<std::string>();
753 }
754 
755 bool te::ogr::Transactor::indexExists(const std::string& /*datasetName*/, const std::string& /*name*/)
756 {
757  return false;
758 }
759 
760 void te::ogr::Transactor::addIndex(const std::string& /*datasetName*/, te::da::Index* /*idx*/,
761  const std::map<std::string, std::string>& /*options*/)
762 {
763 }
764 
765 void te::ogr::Transactor::dropIndex(const std::string& /*datasetName*/, const std::string& /*idxName*/)
766 {
767 }
768 
769 std::auto_ptr<te::da::Sequence> te::ogr::Transactor::getSequence(const std::string& /*name*/)
770 {
771  return std::auto_ptr<te::da::Sequence>();
772 }
773 
774 std::vector<std::string> te::ogr::Transactor::getSequenceNames()
775 {
776  return std::vector<std::string>();
777 }
778 
779 bool te::ogr::Transactor::sequenceExists(const std::string& /*name*/)
780 {
781  return false;
782 }
783 
785 {
786 }
787 
788 void te::ogr::Transactor::dropSequence(const std::string& /*name*/)
789 {
790 }
791 
792 std::auto_ptr<te::gm::Envelope> te::ogr::Transactor::getExtent(const std::string& datasetName,
793  const std::string& propertyName)
794 {
795  if (!m_ogrDs->getOGRDataSource())
796  return std::auto_ptr<te::gm::Envelope>();
797 
798  std::auto_ptr<te::gm::Envelope> res;
799  std::string sql("SELECT ");
800  sql += propertyName + " FROM \'";
801  sql += datasetName + "\'";
802 
803  OGRLayer* l = m_ogrDs->getOGRDataSource()->ExecuteSQL(sql.c_str(), 0, 0);
804 
805  if(l != 0)
806  {
807  std::auto_ptr<OGREnvelope> env(new OGREnvelope);
808 
809  if(l->GetExtent(env.get()) != OGRERR_NONE)
810  {
811  m_ogrDs->getOGRDataSource()->ReleaseResultSet(l);
812  throw Exception(TE_TR("Error when attempting get extent."));
813  }
814 
815  res.reset(Convert2TerraLib(env.get()));
816 
817  m_ogrDs->getOGRDataSource()->ReleaseResultSet(l);
818  }
819 
820  return res;
821 }
822 
823 std::auto_ptr<te::gm::Envelope> te::ogr::Transactor::getExtent(const std::string& datasetName,
824  std::size_t /*propertyPos*/)
825 {
826  return getExtent(datasetName, "OGR_GEOMETRY");
827 }
828 
829 std::size_t te::ogr::Transactor::getNumberOfItems(const std::string& datasetName)
830 {
831  if (!m_ogrDs->getOGRDataSource())
832  return 0;
833 
834  OGRLayer* l = m_ogrDs->getOGRDataSource()->GetLayerByName(datasetName.c_str());
835 
836  if(l != 0)
837  return l->GetFeatureCount();
838 
839  return 0;
840 }
841 
843 {
844  if (!m_ogrDs->getOGRDataSource())
845  return false;
846 
847  return (m_ogrDs->getOGRDataSource()->GetLayerCount() > 0);
848 }
849 
850 bool te::ogr::Transactor::dataSetExists(const std::string& name)
851 {
852  if (!m_ogrDs->getOGRDataSource())
853  return false;
854 
855  return (m_ogrDs->getOGRDataSource()->GetLayerByName(name.c_str()) != 0);
856 }
857 
858 void te::ogr::Transactor::createDataSet(te::da::DataSetType* dt, const std::map<std::string, std::string>& /*options*/)
859 {
860  if (!m_ogrDs->getOGRDataSource())
861  return;
862 
863  if(!m_ogrDs->getOGRDataSource()->TestCapability(ODsCCreateLayer))
864  throw Exception(TE_TR("This driver does not support dataset creation."));
865 
866  OGRwkbGeometryType geomType = wkbUnknown;
867  OGRSpatialReference* srs = 0;
868  if(dt->hasGeom())
869  {
870  geomType = Convert2OGR(te::da::GetFirstGeomProperty(dt)->getGeometryType());
871  int srid = te::da::GetFirstGeomProperty(dt)->getSRID();
872  if (srid != TE_UNKNOWN_SRS)
873  {
874  srs = Convert2OGRProjection( srid );
875  }
876  }
877 
878  char** papszOptions = 0;
879  std::map<std::string, std::string>::const_iterator it = m_ogrDs->getConnectionInfo().begin();
880  while(it != m_ogrDs->getConnectionInfo().end())
881  {
882  if(it->first == "URI" || it->first == "SOURCE" || it->first == "DRIVER")
883  {
884  ++it;
885  continue;
886  }
887  papszOptions = CSLSetNameValue(papszOptions, it->first.c_str(), it->second.c_str());
888  ++it;
889  }
890 
891  OGRLayer* newLayer = m_ogrDs->getOGRDataSource()->CreateLayer(dt->getName().c_str(), srs, geomType, papszOptions);
892 
893  if(papszOptions)
894  CSLDestroy(papszOptions);
895 
896  if(newLayer == 0)
897  throw Exception(TE_TR("Error when attempting create the dataset type."));
898 
899  dt->setName(newLayer->GetName());
900 
901 // add the properties
902  for(size_t i = 0; i < dt->size(); ++i)
903  addProperty(dt->getName(), dt->getProperty(i));
904 }
905 
906 void te::ogr::Transactor::cloneDataSet(const std::string& name,
907  const std::string& cloneName,
908  const std::map<std::string, std::string>& /*options*/)
909 {
910  if (!m_ogrDs->getOGRDataSource())
911  return;
912 
913  if(!m_ogrDs->getOGRDataSource()->TestCapability(ODsCCreateLayer))
914  throw Exception(TE_TR("This driver does not support creates a dataset."));
915 
916  OGRLayer* l = m_ogrDs->getOGRDataSource()->GetLayerByName(name.c_str());
917 
918  if(l == 0)
919  throw Exception(TE_TR("Could not retrieve the DataSet from data source."));
920 
921  OGRLayer* cl = m_ogrDs->getOGRDataSource()->CopyLayer(l, cloneName.c_str());
922 
923  if(cl == 0)
924  throw Exception(TE_TR("Error when attempting clone the dataset."));
925 }
926 
927 void te::ogr::Transactor::dropDataSet(const std::string& name)
928 {
929  if (!m_ogrDs->getOGRDataSource())
930  return;
931 
932  if(!m_ogrDs->getOGRDataSource()->TestCapability(ODsCDeleteLayer))
933  throw Exception(TE_TR("This driver does not support remove a dataset."));
934 
935  int i=0;
936 
937  for(; i<m_ogrDs->getOGRDataSource()->GetLayerCount(); i++)
938  if(name.compare(m_ogrDs->getOGRDataSource()->GetLayer(i)->GetName()) == 0)
939  break;
940 
941  if(i == m_ogrDs->getOGRDataSource()->GetLayerCount())
942  throw Exception(TE_TR("Could not retrieve the DataSet from data source."));
943 
944  if(m_ogrDs->getOGRDataSource()->DeleteLayer(i) != OGRERR_NONE)
945  throw Exception(TE_TR("Error when attempting to remove the dataset."));
946 }
947 
948 void te::ogr::Transactor::renameDataSet(const std::string& /*name*/, const std::string& /*newName*/)
949 {
950 }
951 
952 void te::ogr::Transactor::add(const std::string& datasetName,
953  te::da::DataSet* d,
954  const std::map<std::string, std::string>& options,
955  std::size_t limit)
956 {
957  if(limit == 0)
958  limit = std::string::npos;
959 
960  if (!m_ogrDs->getOGRDataSource())
961  return;
962 
963  OGRLayer* layer = m_ogrDs->getOGRDataSource()->GetLayerByName(datasetName.c_str());
964 
965  if(layer == 0)
966  throw Exception(TE_TR("Could not retrieve the DataSet from data source."));
967 
968  try
969  {
970  begin();
971 
972  std::size_t nproperties = d->getNumProperties();
973 
974  std::size_t nProcessedRows = 0;
975 
976  while(d->moveNext() && (nProcessedRows != limit))
977  {
978  OGRFeature* feat = OGRFeature::CreateFeature(layer->GetLayerDefn());
979 
980  std::size_t currfield = 0;
981 
982  for(std::size_t i = 0; i != nproperties; ++i)
983  {
984  if(d->isNull(i))
985  {
987  ++currfield;
988 
989  continue;
990  }
991 
992  switch(d->getPropertyDataType(i))
993  {
994  case te::dt::INT16_TYPE:
995  feat->SetField(currfield, d->getInt16(i));
996  ++currfield;
997  break;
998 
999  case te::dt::INT32_TYPE:
1000  feat->SetField(currfield, d->getInt32(i));
1001  ++currfield;
1002  break;
1003 
1004  case te::dt::STRING_TYPE:
1005  feat->SetField(currfield, d->getAsString(i).c_str());
1006  ++currfield;
1007  break;
1008 
1009  case te::dt::DOUBLE_TYPE:
1010  feat->SetField(currfield, d->getDouble(i));
1011  ++currfield;
1012  break;
1013 
1014  case te::dt::NUMERIC_TYPE:
1015  feat->SetField(currfield, atof(d->getNumeric(i).c_str()));
1016  ++currfield;
1017  break;
1018 
1020  {
1021  std::auto_ptr<te::dt::ByteArray> ba(d->getByteArray(i));
1022  feat->SetField(currfield, ba->bytesUsed(), reinterpret_cast<unsigned char*>(ba->getData()));
1023  ++currfield;
1024  }
1025  break;
1026 
1027  case te::dt::DATETIME_TYPE:
1028  {
1029  std::auto_ptr<te::dt::DateTime> dtm(d->getDateTime(i));
1030 
1031  te::dt::Date* dtime = dynamic_cast<te::dt::Date*>(dtm.get());
1032 
1033  if(dtime)
1034  {
1035  feat->SetField(currfield,
1036  static_cast<int>(dtime->getYear()),
1037  static_cast<int>(dtime->getMonth()),
1038  static_cast<int>(dtime->getDay()));
1039  ++currfield;
1040  break;
1041  }
1042 
1043  te::dt::TimeDuration* tduration = dynamic_cast<te::dt::TimeDuration*>(dtm.get());
1044 
1045  if(tduration)
1046  {
1047  feat->SetField(currfield, 0, 0, 0,
1048  static_cast<int>(tduration->getHours()),
1049  static_cast<int>(tduration->getMinutes()),
1050  static_cast<int>(tduration->getSeconds()));
1051  ++currfield;
1052  break;
1053  }
1054 
1055  te::dt::TimeInstant* tinst = dynamic_cast<te::dt::TimeInstant*>(dtm.get());
1056 
1057  if(tinst)
1058  {
1059  feat->SetField(currfield,
1060  static_cast<int>(dtime->getYear()),
1061  static_cast<int>(dtime->getMonth()),
1062  static_cast<int>(dtime->getDay()),
1063  static_cast<int>(tduration->getHours()),
1064  static_cast<int>(tduration->getMinutes()),
1065  static_cast<int>(tduration->getSeconds()));
1066  ++currfield;
1067  break;
1068  }
1069 
1070  throw Exception (TE_TR("Unsupported date and time type by OGR."));
1071  }
1072  break;
1073 
1074  case te::dt::GEOMETRY_TYPE:
1075  {
1076  std::auto_ptr<te::gm::Geometry> geom(d->getGeometry(i));
1077  OGRGeometry* OGRgeom = Convert2OGR(geom.get());
1078  feat->SetGeometryDirectly(OGRgeom);
1079  }
1080  break;
1081 
1082  default:
1083  throw Exception(TE_TR("Unsupported data type by OGR."));
1084  }
1085  }
1086 
1087  if(layer->CreateFeature(feat) != OGRERR_NONE)
1088  {
1089  OGRFeature::DestroyFeature(feat);
1090  throw Exception(TE_TR("Fail to insert dataset item."));
1091  }
1092 
1093  OGRFeature::DestroyFeature(feat);
1094  nProcessedRows++;
1095  }
1096 
1097  commit();
1098  }
1099  catch(Exception& e)
1100  {
1101  rollBack();
1102  throw e;
1103  }
1104 }
1105 
1106 void te::ogr::Transactor::remove(const std::string& datasetName, const te::da::ObjectIdSet* oids)
1107 {
1108  if(!m_ogrDs->getOGRDataSource())
1109  return;
1110 
1111  OGRLayer* l = m_ogrDs->getOGRDataSource()->GetLayerByName(datasetName.c_str());
1112 
1113  if(l == 0)
1114  throw Exception(TE_TR("Could not retrieve the DataSet from data source."));
1115 
1116  if(!l->TestCapability(OLCDeleteFeature))
1117  throw Exception(TE_TR("Driver does not support removal of features."));
1118 
1119  std::set<te::da::ObjectId*, te::common::LessCmp<te::da::ObjectId*> >::const_iterator it = oids->begin();
1120 
1121  while(it != oids->end())
1122  {
1123  begin();
1124 
1125  if(l->DeleteFeature(atoi((*it)->getValue()[0].toString().c_str())) != OGRERR_NONE)
1126  {
1127  rollBack();
1128  throw Exception(TE_TR("Error when attempting to remove the feature."));
1129  }
1130 
1131  ++it;
1132  }
1133 
1134  commit();
1135 }
1136 
1137 
1138 void te::ogr::Transactor::update(const std::string& datasetName,
1139  te::da::DataSet* /*dataset*/,
1140  const std::vector<std::size_t>& properties,
1141  const te::da::ObjectIdSet* /*oids*/,
1142  const std::map<std::string, std::string>& /*options*/,
1143  std::size_t /*limit*/)
1144 {
1145 }
1146 
1147 void te::ogr::Transactor::update(const std::string &datasetName, te::da::DataSet *dataset, const std::vector< std::set<int> >& properties,
1148  const std::vector<size_t>& ids)
1149 {
1150  if(m_ogrDs->getOGRDataSource() == 0)
1151  throw Exception(TE_TR("Data source failure"));
1152 
1153  OGRLayer* l = m_ogrDs->getOGRDataSource()->GetLayerByName(datasetName.c_str());
1154 
1155  if(l == 0)
1156  throw Exception(TE_TR("Could not retrieve dataset"));
1157 
1158  dataset->moveFirst();
1159  int i = 0;
1160 
1161  do
1162  {
1163  size_t id_pos = ids[0];
1164  int id = dataset->getInt32(id_pos);
1165 
1166  OGRFeature* feat = l->GetFeature(id)->Clone();
1167 
1168  std::set<int> ls = properties[i];
1169  std::set<int>::iterator it;
1170 
1171  for(it = ls.begin(); it != ls.end(); ++it)
1172  {
1173  int fpos = *it;
1174  int fpos_o = fpos - 1;
1175 
1176  switch(dataset->getPropertyDataType(fpos))
1177  {
1178  case te::dt::INT32_TYPE:
1179  feat->SetField(fpos_o, dataset->getInt32(fpos));
1180  break;
1181 
1182  case te::dt::DOUBLE_TYPE:
1183  case te::dt::NUMERIC_TYPE:
1184  feat->SetField(fpos_o, dataset->getDouble(fpos));
1185  break;
1186 
1187  case te::dt::STRING_TYPE:
1188  feat->SetField(fpos_o, dataset->getString(fpos).c_str());
1189  break;
1190 
1191  case te::dt::GEOMETRY_TYPE:
1192  {
1193  std::auto_ptr<te::gm::Geometry> gm = dataset->getGeometry(fpos);
1194  feat->SetGeometry(te::ogr::Convert2OGR(gm.get()));
1195  }
1196  }
1197  }
1198 
1199  l->SetFeature(feat);
1200 
1201  i++;
1202  } while (dataset->moveNext());
1203 
1204  l->SyncToDisk();
1205 }
1206 
1207 void te::ogr::Transactor::optimize(const std::map<std::string, std::string>& /*opInfo*/)
1208 {
1209 }
1210 
1212 {
1213  return te::common::LATIN1;
1214 }
Property * getProperty(std::size_t i) const
It returns the i-th property.
std::vector< std::string > getUniqueKeyNames(const std::string &datasetName)
It gets the unique key names of the given dataset.
Definition: Transactor.cpp:704
te::common::CharEncoding getEncoding()
It return the DataSource current encoding.
void dropProperty(const std::string &datasetName, const std::string &name)
It removes a property from the given dataset.
Definition: Transactor.cpp:540
virtual std::auto_ptr< te::dt::DateTime > getDateTime(std::size_t i) const =0
Method for retrieving a date and time attribute value.
virtual boost::int16_t getInt16(std::size_t i) const =0
Method for retrieving a 16-bit integer attribute value (2 bytes long).
Geometric property.
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:792
void add(te::dt::Property *p)
It adds a property to the list of properties of the primary key.
Definition: PrimaryKey.h:123
void addForeignKey(const std::string &datasetName, te::da::ForeignKey *fk)
It adds a foreign key constraint to a dataset.
Definition: Transactor.cpp:691
Utility functions for the data access module.
void dropCheckConstraint(const std::string &datasetName, const std::string &name)
It removes the check constraint from the dataset.
Definition: Transactor.cpp:741
boost::int64_t getLastGeneratedId()
It returns the last id generated by an insertion command.
Definition: Transactor.cpp:281
A class that informs what kind of constraint and index is supported by a given data source...
TEOGREXPORT OGRGeometry * Convert2OGR(const te::gm::Geometry *teGeom)
It converts the TerraLib Geometry to OGR Geometry.
Definition: Utils.cpp:80
void setSRID(int srid)
It sets the spatial reference system identifier associated to this property.
virtual boost::int32_t getInt32(std::size_t i) const =0
Method for retrieving a 32-bit integer attribute value (4 bytes long).
CharEncoding
Supported charsets (character encoding).
A class for data providers of OGR.
bool isInTransaction() const
It returns true if a transaction is in progress, otherwise, it returns false.
Definition: Transactor.cpp:109
void commit()
It commits the transaction.
Definition: Transactor.cpp:96
bool hasGeom() const
It returns true if the DataSetType has at least one geometry property; otherwise, it returns false...
Definition: DataSetType.h:655
boost::ptr_vector< te::dt::Property > getProperties(const std::string &datasetName)
It retrieves the properties of the dataset.
Definition: Transactor.cpp:369
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:769
std::auto_ptr< te::da::DataSetTypeCapabilities > getCapabilities(const std::string &name)
It gets capabilities about a data set.
Definition: Transactor.cpp:353
bool sequenceExists(const std::string &name)
It checks if a sequence with the given name exists in the data source.
Definition: Transactor.cpp:779
te::da::DataSource * getDataSource() const
It returns the parent data source of the transactor.
Definition: Transactor.cpp:87
A class that models the description of a dataset.
Definition: DataSetType.h:72
void addProperty(const std::string &datasetName, te::dt::Property *p)
It adds a new property to the dataset schema.
Definition: Transactor.cpp:510
long getSeconds() const
It returns the seconds of a minute - from 0 to 59.
Definition: TimeDuration.h:105
std::size_t getNumberOfProperties(const std::string &datasetName)
It gets the number of properties of the given dataset.
Definition: Transactor.cpp:467
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:745
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:732
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:906
void dropForeignKey(const std::string &datasetName, const std::string &fkName)
It removes the foreign key constraint from the dataset schema.
Definition: Transactor.cpp:695
virtual std::auto_ptr< te::dt::ByteArray > getByteArray(std::size_t i) const =0
Method for retrieving a byte array.
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:755
double m_urx
Upper right corner x-coordinate.
Definition: Envelope.h:346
void renameDataSet(const std::string &name, const std::string &newName)
It renames a dataset.
Definition: Transactor.cpp:948
SpatialRelation
Spatial relations between geometric objects.
Definition: Enums.h:122
It describes a sequence (a number generator).
Definition: Sequence.h:56
std::auto_ptr< te::da::PrimaryKey > getPrimaryKey(const std::string &datasetName)
It retrieves the primary key of the dataset.
Definition: Transactor.cpp:631
bool dataSetExists(const std::string &name)
It checks if a dataset with the given name exists in the data source.
Definition: Transactor.cpp:850
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:722
Transactor(DataSource *ds)
Definition: Transactor.cpp:77
A class that describes a check constraint.
An abstract class for data providers like a DBMS, Web Services or a regular file. ...
Definition: DataSource.h:118
virtual std::string getNumeric(std::size_t i) const =0
Method for retrieving a numeric attribute value.
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:346
TEOGREXPORT std::string RemoveSpatialSql(const std::string &sql)
Definition: Utils.cpp:637
std::auto_ptr< te::da::BatchExecutor > getBatchExecutor()
It creates a batch command executor.
Definition: Transactor.cpp:272
void dropUniqueKey(const std::string &datasetName, const std::string &name)
It removes the unique key constraint from the dataset.
Definition: Transactor.cpp:718
void dropDataSet(const std::string &name)
It removes the dataset schema from the data source.
Definition: Transactor.cpp:927
Implementation of a DataSet for OGR data provider.
Definition: DataSet.h:59
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.
It models a property definition.
Definition: Property.h:59
void cancel()
It requests that the data source stop the processing of the current command.
Definition: Transactor.cpp:277
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:686
void remove(const std::string &datasetName, const te::da::ObjectIdSet *oids=0)
It removes all the informed items from the dataset.
virtual double getDouble(std::size_t i) const =0
Method for retrieving a double attribute value.
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...
A class to represent time instant.
Definition: TimeInstant.h:55
void addPrimaryKey(const std::string &datasetName, te::da::PrimaryKey *pk)
It adds a primary key constraint to the dataset schema.
Definition: Transactor.cpp:668
std::vector< std::string > getCheckConstraintNames(const std::string &datasetName)
It gets the check constraint names of the given dataset.
Definition: Transactor.cpp:727
bool primaryKeyExists(const std::string &datasetName, const std::string &name)
It checks if a primary key exists in the dataset.
Definition: Transactor.cpp:663
void begin()
It starts a new transaction.
Definition: Transactor.cpp:92
std::size_t getNumberOfDataSets()
It retrieves the number of data sets available in the data source.
Definition: Transactor.cpp:304
virtual bool moveNext()=0
It moves the internal pointer to the next item of the collection.
AccessPolicy
Supported data access policies (can be used as bitfield).
Definition: Enums.h:40
TraverseType
A dataset can be traversed in two ways:
Definition: Enums.h:53
void addCheckConstraint(const std::string &datasetName, te::da::CheckConstraint *cc)
It adds a check constraint to the dataset.
Definition: Transactor.cpp:737
void setName(const std::string &name)
It sets the property name.
Definition: Property.h:137
double m_llx
Lower left corner x-coordinate.
Definition: Envelope.h:344
int getSRID() const
It returns the spatial reference system identifier associated to this property.
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
#define TE_UNKNOWN_SRS
A numeric value to represent a unknown SRS identification in TerraLib.
Definition: Config.h:41
A base class for date data types.
Definition: Date.h:53
This class represents a set of unique ids created in the same context. i.e. from the same data set...
Definition: ObjectIdSet.h:55
virtual bool isNull(std::size_t i) const =0
It checks if the attribute value is NULL.
void changePropertyDefinition(const std::string &datasetName, const std::string &propName, te::dt::Property *newProp)
Definition: Transactor.cpp:595
TEOGREXPORT te::gm::Geometry * Convert2TerraLib(OGRGeometry *ogrGeom)
It converts the OGR Geometry to TerraLib Geometry.
Definition: Utils.cpp:55
URI C++ Library.
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:397
A visitor for building an SQL statement using OGR dialect.
It models a foreign key constraint for a DataSetType.
Definition: ForeignKey.h:50
virtual std::string getAsString(std::size_t i, int precision=0) const
Method for retrieving a data value as a string plain representation.
Definition: DataSet.cpp:218
It describes a unique key (uk) constraint.
Definition: UniqueKey.h:53
std::size_t size() const
It returns the number of properties of the CompositeProperty.
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:676
virtual std::size_t getNumProperties() const =0
It returns the number of properties that composes an item of the dataset.
std::vector< std::string > getDataSetNames()
It It gets the dataset names available in the data source.
Definition: Transactor.cpp:291
The OGR data source provider.
Definition: DataSource.h:48
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
Definition: Geometry.h:73
void dropPrimaryKey(const std::string &datasetName)
It removes the primary key constraint from the dataset schema.
Definition: Transactor.cpp:672
A Select models a query to be used when retrieving data from a DataSource.
Definition: Select.h:65
virtual std::auto_ptr< te::gm::Geometry > getGeometry(std::size_t i) const =0
Method for retrieving a geometric attribute value.
void dropSequence(const std::string &name)
It removes the sequence from the data source.
Definition: Transactor.cpp:788
double m_lly
Lower left corner y-coordinate.
Definition: Envelope.h:345
void execute(const te::da::Query &command)
It executes the specified command using a generic query representation.
Definition: Transactor.cpp:246
void dropIndex(const std::string &datasetName, const std::string &idxName)
It removes the index from the dataset schema.
Definition: Transactor.cpp:765
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::string escape(const std::string &value)
It escapes a string for using in commands and queries.
Definition: Transactor.cpp:286
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:858
double m_ury
Upper right corner y-coordinate.
Definition: Envelope.h:347
virtual ReturnType accept(VisitorType &guest) const =0
It call the visit method from the guest object.
std::set< ObjectId *, te::common::LessCmp< ObjectId * > >::const_iterator end() const
Returns an iterator for the object ids in container.
A class to represent time duration with nano-second/micro-second resolution.
Definition: TimeDuration.h:51
void addSequence(te::da::Sequence *sequence)
It creates a new sequence in the data source.
Definition: Transactor.cpp:784
It describes a primary key (pk) constraint.
Definition: PrimaryKey.h:52
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:566
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:188
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:114
Implementation of a DataSet for OGR data provider.
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:952
void rollBack()
It aborts the transaction. Any changes will be rolled-back.
Definition: Transactor.cpp:105
std::auto_ptr< te::da::DataSetType > getDataSetType(const std::string &name)
It gets information about the given dataset.
Definition: Transactor.cpp:312
virtual std::string getString(std::size_t i) const =0
Method for retrieving a string value attribute.
std::vector< std::string > getPropertyNames(const std::string &datasetName)
It gets the property names of the given dataset.
Definition: Transactor.cpp:442
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:699
long getMinutes() const
It returns the minutes of a hour - from 0 to 59.
Definition: TimeDuration.h:98
std::vector< std::string > getForeignKeyNames(const std::string &datasetName)
It gets the foreign key names of the given dataset.
Definition: Transactor.cpp:681
TEDATAACCESSEXPORT te::gm::GeometryProperty * GetFirstGeomProperty(const DataSetType *dt)
Definition: Utils.cpp:557
virtual int getPropertyDataType(std::size_t i) const =0
It returns the underlying data type of the property at position pos.
void addUniqueKey(const std::string &datasetName, te::da::UniqueKey *uk)
It adds a unique key constraint to the dataset.
Definition: Transactor.cpp:714
OGRFieldType GetOGRType(int te_type)
Definition: Transactor.cpp:44
std::set< ObjectId *, te::common::LessCmp< ObjectId * > >::const_iterator begin() const
Returns an iterator for the object ids in container.
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:267
std::vector< std::string > getSequenceNames()
It gets the sequence names available in the data source.
Definition: Transactor.cpp:774
virtual bool moveFirst()=0
It moves the internal pointer to the first item in the collection.
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:760
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:489
TEOGREXPORT OGRSpatialReference * Convert2OGRProjection(int srid)
It converts the TerraLib Projection to OGR Projection.
Definition: Utils.cpp:221
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:709
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
long getHours() const
It returns the hours of a day - from 0 to 23.
Definition: TimeDuration.h:91
TEOGREXPORT int Convert2TerraLibProjection(OGRSpatialReference *osrs)
It converts the OGR Projection to TerraLib Projection.
Definition: Utils.cpp:126
std::size_t getNumberOfItems(const std::string &datasetName)
It retrieves the number of items of the given dataset.
Definition: Transactor.cpp:829
const std::string & getName() const
It returns the property name.
Definition: Property.h:127
std::vector< std::string > getIndexNames(const std::string &datasetName)
It gets the index names of the given dataset.
Definition: Transactor.cpp:750
bool hasDataSets()
It checks if the data source has any dataset.
Definition: Transactor.cpp:842