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/ado/Transactor.cpp
22 
23  \brief DataSourceTransactor class implementation for Microsoft Access driver.
24 */
25 
26 // TerraLib
27 #include "../common/Exception.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 "Connection.h"
54 #include "DataSet.h"
55 #include "DataSource.h"
56 #include "Globals.h"
57 #include "SQLVisitor.h"
58 #include "Transactor.h"
59 #include "Utils.h"
60 
61 // STL
62 #include <cassert>
63 #include <memory>
64 #include <iostream>
65 
66 // Boost
67 #include <boost/format.hpp>
68 #include <boost/lexical_cast.hpp>
69 
70 // ADO
71 #import "msado15.dll" \
72  no_namespace rename("EOF", "EndOfFile")
73 #import "msadox.dll"
74 
75 inline void TESTHR(HRESULT hr)
76 {
77  if(FAILED(hr))
78  _com_issue_error(hr);
79 }
80 
81 bool isEnvelopeProperty(const std::string& name)
82 {
83  if(name == "lower_x" ||
84  name == "lower_y" ||
85  name == "upper_x" ||
86  name == "upper_y")
87  return true;
88  return false;
89 }
90 
92  : m_ds(ds),
93  m_conn(0),
94  m_isInTransaction(false)
95 {
96  std::string connInfoAux = MakeConnectionStr(ds->getConnectionInfo());
97 
98  m_conn = new te::ado::Connection(connInfoAux);
99 }
100 
102 {
103  delete m_conn;
104 }
105 
107 {
108  return m_ds;
109 }
110 
112 {
113  try
114  {
115  m_conn->getConn()->BeginTrans();
116  m_isInTransaction = true;
117  }
118  catch(_com_error& e)
119  {
120  throw te::common::Exception((LPCSTR)e.ErrorMessage());
121  }
122 }
123 
125 {
126  try
127  {
128  m_conn->getConn()->CommitTrans();
129  m_isInTransaction = false;
130  }
131  catch(_com_error& e)
132  {
133  throw te::common::Exception((LPCSTR)e.ErrorMessage());
134  }
135 }
136 
138 {
139  try
140  {
141  m_conn->getConn()->RollbackTrans();
142  m_isInTransaction = false;
143  }
144  catch(_com_error& e)
145  {
146  throw te::common::Exception((LPCSTR)e.ErrorMessage());
147  }
148 }
149 
151 {
152  return m_isInTransaction;
153 }
154 
155 std::auto_ptr<te::da::DataSet> te::ado::Transactor::getDataSet(const std::string& name,
156  te::common::TraverseType travType,
157  bool connected,
158  const te::common::AccessPolicy accessPolicy)
159 {
160  std::string sql("SELECT * FROM ");
161  sql += name;
162 
163  return query(sql, travType, connected, accessPolicy);
164 }
165 
166 std::auto_ptr<te::da::DataSet> te::ado::Transactor::getDataSet(const std::string& name,
167  const std::string&,
168  const te::gm::Envelope* e,
170  te::common::TraverseType travType,
171  bool connected,
172  const te::common::AccessPolicy accessPolicy)
173 {
174  if(e == 0)
175  throw te::common::Exception(TE_TR("The envelope is missing!"));
176 
177  std::string lowerX = "lower_x";
178  std::string upperX = "upper_x";
179  std::string lowerY = "lower_y";
180  std::string upperY = "upper_y";
181 
182  std::string q("SELECT * FROM " + name + " WHERE ");
183 
184  q += "NOT("+ lowerX +" >= " + boost::lexical_cast<std::string>(e->m_urx) + " OR ";
185  q += upperX +" <= " + boost::lexical_cast<std::string>(e->m_llx) + " OR ";
186  q += lowerY +" >= " + boost::lexical_cast<std::string>(e->m_ury) + " OR ";
187  q += upperY +" <= " + boost::lexical_cast<std::string>(e->m_lly) + ")";
188 
189  return query(q, travType, connected, accessPolicy);
190 }
191 
192 std::auto_ptr<te::da::DataSet> te::ado::Transactor::getDataSet(const std::string& /*name*/,
193  const std::string& /*propertyName*/,
194  const te::gm::Geometry* /*g*/,
196  te::common::TraverseType /*travType*/,
197  bool /*connected*/,
198  const te::common::AccessPolicy /*accessPolicy*/)
199 {
200  throw te::common::Exception(TE_TR("Method getDataSet by geometry filter: not implemented yet!"));
201 }
202 
203 std::auto_ptr<te::da::DataSet> te::ado::Transactor::getDataSet(const std::string& /*name*/,
204  const ObjectIdSet* /*oids*/,
205  te::common::TraverseType /*travType*/,
206  bool /*connected*/,
207  const te::common::AccessPolicy /*accessPolicy*/)
208 {
209  throw te::common::Exception(TE_TR("Method getDataSet by oids: not implemented yet!"));
210 }
211 
212 std::auto_ptr<te::da::DataSet> te::ado::Transactor::query(const te::da::Select& q,
213  te::common::TraverseType travType,
214  bool,
216 {
217  std::string sql;
218 
219  SQLVisitor visitor(*(m_ds->getDialect()), sql, m_conn->getConn());
220 
221  q.accept(visitor);
222 
223  return query(sql, travType);
224 }
225 
226 std::auto_ptr<te::da::DataSet> te::ado::Transactor::query(const std::string& query,
228  bool connected,
230 {
231  _RecordsetPtr result = m_conn->query(query, connected);
232 
233  std::auto_ptr<te::da::DataSet> dset(new DataSet(this, result));
234 
235  if(connected)
236  {
237  return dset;
238  }
239  else
240  {
241  std::auto_ptr<te::da::DataSet> mdset(new te::mem::DataSet(*dset));
242 
243  return mdset;
244  }
245 }
246 
248 {
249  std::string sql;
250 
251  SQLVisitor visitor(*(m_ds->getDialect()), sql, m_conn->getConn());
252 
253  command.accept(visitor);
254 
255  execute(sql);
256 }
257 
258 void te::ado::Transactor::execute(const std::string& command)
259 {
260  m_conn->execute(command);
261 }
262 
263 std::auto_ptr<te::da::PreparedQuery> te::ado::Transactor::getPrepared(const std::string&)
264 {
265  throw te::common::Exception(TE_TR("Method getPrepared: not implemented yet!"));
266 }
267 
268 std::auto_ptr<te::da::BatchExecutor> te::ado::Transactor::getBatchExecutor()
269 {
270  throw te::common::Exception(TE_TR("Method getBatchExecutor: not implemented yet!"));
271 }
272 
274 {
275 }
276 
278 {
279  throw te::common::Exception(TE_TR("Method getLastGeneratedId: not implemented yet!"));
280 }
281 
282 std::string te::ado::Transactor::escape(const std::string& value)
283 {
284  return value;
285 }
286 
287 std::vector<std::string> te::ado::Transactor::getDataSetNames()
288 {
289  std::vector<std::string> datasets;
290 
291  ADOX::_CatalogPtr pCatalog = 0;
292 
293  TESTHR(pCatalog.CreateInstance(__uuidof(ADOX::Catalog)));
294 
295  try
296  {
297  pCatalog->PutActiveConnection(variant_t((IDispatch *)m_conn->getConn()));
298 
299  ADOX::TablesPtr tables = pCatalog->GetTables();
300 
301  for(long i = 0; i < tables->GetCount(); ++i)
302  {
303  ADOX::_TablePtr table = tables->GetItem(i);
304  std::string tableName = table->GetName();
305 
306  std::string tabletype = table->GetType();
307 
308  if(table->GetType() == _bstr_t("ACCESS TABLE") ||
309  table->GetType() == _bstr_t("LINK") ||
310  table->GetType() == _bstr_t("PASS-THROUGH") ||
311  table->GetType() == _bstr_t("SYSTEM TABLE") ||
312  table->GetType() == _bstr_t("VIEW") ||
313  table->GetType() == _bstr_t("GLOBAL TEMPORARY") ||
314  tableName == "geometry_columns")
315  continue;
316 
317  datasets.push_back(std::string(table->GetName()));
318  }
319  }
320  catch(_com_error &e)
321  {
322  throw te::common::Exception(TE_TR(e.ErrorMessage()));
323  }
324 
325  return datasets;
326 }
327 
329 {
330  return getDataSetNames().size();
331 }
332 
333 std::auto_ptr<te::da::DataSetType> te::ado::Transactor::getDataSetType(const std::string& name)
334 {
335  std::auto_ptr<te::da::DataSetType> dt(new te::da::DataSetType(name));
336 
337  dt->setTitle(name);
338 
339  getProperties(dt.get());
340 
341  getPrimaryKey(dt.get());
342 
343  getUniqueKeys(dt.get());
344 
345  getIndexes(dt.get());
346 
347  getCheckConstraints(dt.get());
348 
349  return dt;
350 }
351 
352 boost::ptr_vector<te::dt::Property> te::ado::Transactor::getProperties(const std::string& datasetName)
353 {
354  std::auto_ptr<te::da::DataSetType> dt(getDataSetType(datasetName));
355 
356  std::vector<te::dt::Property*> dtProperties = dt->getProperties();
357 
358  boost::ptr_vector<te::dt::Property> properties;
359 
360  for(std::size_t i = 0; i < dtProperties.size(); ++i)
361  properties.push_back(dtProperties[i]->clone());
362 
363  return properties;
364 }
365 
366 std::auto_ptr<te::dt::Property> te::ado::Transactor::getProperty(const std::string& datasetName, const std::string& name)
367 {
368  if(!propertyExists(datasetName, name))
369  throw te::common::Exception((boost::format(TE_TR("The dataset \"%1%\" has no property with this name \"%2%\"!")) % datasetName % name).str());
370 
371  std::auto_ptr<te::da::DataSetType> dt(getDataSetType(datasetName));
372 
373  return std::auto_ptr<te::dt::Property>(dt->getProperty(name)->clone());
374 }
375 
376 std::auto_ptr<te::dt::Property> te::ado::Transactor::getProperty(const std::string& datasetName, std::size_t propertyPos)
377 {
378  std::auto_ptr<te::da::DataSetType> dt(getDataSetType(datasetName));
379 
380  assert(propertyPos < dt->size());
381 
382  return std::auto_ptr<te::dt::Property>(dt->getProperty(propertyPos)->clone());
383 }
384 
385 std::vector<std::string> te::ado::Transactor::getPropertyNames(const std::string& datasetName)
386 {
387  std::auto_ptr<te::da::DataSetType> dt(getDataSetType(datasetName));
388 
389  std::vector<std::string> pNames;
390 
391  std::size_t numProperties = dt->size();
392 
393  for(std::size_t i = 0; i < numProperties; ++i)
394  pNames.push_back(dt->getProperty(i)->getName());
395 
396  return pNames;
397 }
398 
399 std::size_t te::ado::Transactor::getNumberOfProperties(const std::string& datasetName)
400 {
401  std::auto_ptr<te::da::DataSetType> dt(getDataSetType(datasetName));
402 
403  return dt->size();
404 }
405 
406 bool te::ado::Transactor::propertyExists(const std::string& datasetName, const std::string& name)
407 {
408  std::vector<std::string> datasets;
409 
410  ADOX::_CatalogPtr pCatalog = 0;
411 
412  TESTHR(pCatalog.CreateInstance(__uuidof(ADOX::Catalog)));
413 
414  try
415  {
416  pCatalog->PutActiveConnection(variant_t((IDispatch *)m_conn->getConn()));
417 
418  ADOX::TablesPtr tables = pCatalog->GetTables();
419 
420  ADOX::_TablePtr table;
421 
422  for(long i = 0; i < tables->GetCount(); ++i)
423  {
424  table = 0;
425  table = tables->GetItem(i);
426  std::string tableName = table->GetName();
427 
428  if(tableName == datasetName)
429  {
430  break;
431  }
432  }
433 
434  if(table)
435  {
436  ADOX::ColumnsPtr cols = table->GetColumns();
437 
438  for(int i = 0; i < cols->Count; ++i)
439  {
440  ADOX::_ColumnPtr col = cols->GetItem((long)i);
441 
442  if(((LPCSTR)(_bstr_t)col->GetName()) == name)
443  return true;
444  }
445  }
446 
447  return false;
448 
449  }
450  catch(_com_error &e)
451  {
452  throw te::common::Exception(TE_TR(e.ErrorMessage()));
453  }
454 }
455 
456 void te::ado::Transactor::addProperty(const std::string& datasetName, te::dt::Property* p)
457 {
458  const std::string& propertyName = p->getName();
459 
460  if(isEnvelopeProperty(propertyName) && propertyExists(datasetName, propertyName))
461  return;
462 
463  if(propertyExists(datasetName, propertyName))
464  throw te::common::Exception((boost::format(TE_TR("The dataset already \"%1%\" has a property with this name \"%2%\"!")) % datasetName % propertyName).str());
465 
466  ADOX::_CatalogPtr pCatalog = 0;
467 
468  ADOX::_TablePtr pTable = 0;
469 
470  TESTHR(pCatalog.CreateInstance(__uuidof(ADOX::Catalog)));
471 
472  try
473  {
474  pCatalog->PutActiveConnection(variant_t((IDispatch *)m_conn->getConn()));
475 
476  pTable = pCatalog->Tables->GetItem(datasetName.c_str());
477 
478  ADOX::_ColumnPtr newColumn = 0;
479 
480  TESTHR(newColumn.CreateInstance(__uuidof(ADOX::Column)));
481 
482  newColumn->PutName(propertyName.c_str());
483 
484  newColumn->PutType(te::ado::Convert2Ado(p->getType()));
485 
486  ADOX::DataTypeEnum ado_type = te::ado::Convert2Ado(p->getType());
487 
488  switch(p->getType())
489  {
490  case te::dt::CHAR_TYPE:
491  case te::dt::UCHAR_TYPE:
492  case te::dt::INT16_TYPE:
493  case te::dt::INT32_TYPE:
494  case te::dt::INT64_TYPE:
495  case te::dt::FLOAT_TYPE:
496  case te::dt::DOUBLE_TYPE:
499  case te::dt::ARRAY_TYPE:
502  {
503  const te::dt::SimpleProperty* simple = static_cast<const te::dt::SimpleProperty*>(p);
504 
505  if(!simple->isRequired())
506  newColumn->PutAttributes(ADOX::adColNullable);
507 
508  pTable->Columns->Append(_variant_t ((IDispatch*)newColumn), ado_type, 0);
509 
510  break;
511  }
512 
513  case te::dt::STRING_TYPE:
514  {
515  const te::dt::StringProperty* sp = static_cast<const te::dt::StringProperty*>(p);
516 
517  std::size_t ssize = 0;
518 
519  if(sp->size() != 0)
520  ssize = sp->size();
521 
522  newColumn->DefinedSize = (long)ssize;
523 
524  if(!sp->isRequired())
525  newColumn->PutAttributes(ADOX::adColNullable);
526 
527  pTable->Columns->Append(_variant_t ((IDispatch*)newColumn), ado_type, (long)ssize);
528 
529  break;
530  }
531 
533  {
534  // verify if already exist envelope properties
535  if(propertyExists(datasetName, "lower_x"))
536  break;
537 
538  const te::dt::SimpleProperty* simple = static_cast<const te::dt::SimpleProperty*>(p);
539 
540  if(!simple->isRequired())
541  newColumn->PutAttributes(ADOX::adColNullable);
542 
543  pTable->Columns->Append(_variant_t ((IDispatch*)newColumn), ado_type, 0);
544 
545 // update geometry columns and metadata cache
546  std::auto_ptr<te::dt::SimpleProperty> lowerX(new te::dt::SimpleProperty("lower_x", te::dt::DOUBLE_TYPE));
547  std::auto_ptr<te::dt::SimpleProperty> lowerY(new te::dt::SimpleProperty("lower_y", te::dt::DOUBLE_TYPE));
548  std::auto_ptr<te::dt::SimpleProperty> upperX(new te::dt::SimpleProperty("upper_x", te::dt::DOUBLE_TYPE));
549  std::auto_ptr<te::dt::SimpleProperty> upperY(new te::dt::SimpleProperty("upper_y", te::dt::DOUBLE_TYPE));
550 
551  addProperty(datasetName, lowerX.get());
552  addProperty(datasetName, lowerY.get());
553  addProperty(datasetName, upperX.get());
554  addProperty(datasetName, upperY.get());
555 
556  insertIntoGeometryColumns(datasetName, static_cast<te::gm::GeometryProperty*>(p));
557 
558  break;
559  }
560 
561  default:
562  throw te::common::Exception(TE_TR("The informed type could not be mapped to ADO type system!"));
563  break;
564  }
565  }
566  catch(_com_error& e)
567  {
568  throw te::common::Exception((LPCSTR)e.ErrorMessage());
569  }
570 }
571 
572 void te::ado::Transactor::dropProperty(const std::string& datasetName, const std::string& name)
573 {
574  if(!propertyExists(datasetName, name))
575  throw te::common::Exception((boost::format(TE_TR("The dataset \"%1%\" has no property with this name \"%2%\"!")) % datasetName % name).str());
576 
577  std::auto_ptr<te::da::DataSetType> dt(getDataSetType(datasetName));
578 
579  te::dt::Property* p = dt->getProperty(name);
580 
581  ADOX::_CatalogPtr pCatalog = 0;
582 
583  ADOX::_TablePtr pTable = 0;
584 
585  TESTHR(pCatalog.CreateInstance(__uuidof(ADOX::Catalog)));
586 
587  try
588  {
589  pCatalog->PutActiveConnection(variant_t((IDispatch *)m_conn->getConn()));
590 
591  pTable = pCatalog->Tables->GetItem(p->getParent()->getName().c_str());
592 
593  TESTHR(pTable->GetColumns()->Delete(p->getName().c_str()));
594 
595  if(p->getType() == te::dt::GEOMETRY_TYPE)
596  {
597  std::string sql = "delete from geometry_columns where f_geometry_column = '";
598  sql += p->getName();
599  sql += "' AND f_table_name = '";
600  sql += datasetName;
601  sql +="'";
602 
603  m_conn->execute(sql);
604  }
605  }
606  catch(_com_error& e)
607  {
608  throw te::common::Exception((LPCSTR)e.ErrorMessage());
609  }
610 }
611 
612 void te::ado::Transactor::renameProperty(const std::string& datasetName,
613  const std::string& propertyName,
614  const std::string& newPropertyName)
615 {
616  ADOX::_CatalogPtr pCatalog = 0;
617 
618  ADOX::_TablePtr pTable = 0;
619 
620  TESTHR(pCatalog.CreateInstance(__uuidof(ADOX::Catalog)));
621 
622  try
623  {
624  pCatalog->PutActiveConnection(variant_t((IDispatch *)m_conn->getConn()));
625 
626  pTable = pCatalog->Tables->GetItem(datasetName.c_str());
627 
628  ADOX::_ColumnPtr col = pTable->GetColumns()->GetItem(propertyName.c_str());
629 
630  col->PutName(newPropertyName.c_str());
631 
632  pCatalog->GetTables()->Refresh();
633  }
634  catch(_com_error& e)
635  {
636  throw te::common::Exception((LPCSTR)e.ErrorMessage());
637  }
638 }
639 
640 void te::ado::Transactor::changePropertyDefinition(const std::string& datasetName, const std::string& propName, te::dt::Property* newProp)
641 {
642  std::auto_ptr<te::dt::Property> prp(newProp);
643  std::string type = GetAdoStringType(prp->getType());
644 
645  std::string sql("ALTER TABLE ");
646  sql += datasetName + " ALTER COLUMN " + propName + " " + type;
647 
648  execute(sql);
649 }
650 
651 std::auto_ptr<te::da::PrimaryKey> te::ado::Transactor::getPrimaryKey(const std::string& datasetName)
652 {
653  std::auto_ptr<te::da::DataSetType> dt(getDataSetType(datasetName));
654 
655  return std::auto_ptr<te::da::PrimaryKey>(static_cast<te::da::PrimaryKey*>(dt->getPrimaryKey()->clone()));
656 }
657 
658 bool te::ado::Transactor::primaryKeyExists(const std::string& datasetName, const std::string& name)
659 {
660  std::auto_ptr<te::da::DataSetType> dt(getDataSetType(datasetName));
661 
662  if(dt->getPrimaryKey()->getName() == name)
663  return true;
664 
665  return false;
666 }
667 
668 void te::ado::Transactor::addPrimaryKey(const std::string& datasetName, te::da::PrimaryKey* pk)
669 {
670  _variant_t vOptional;
671  vOptional.vt = VT_ERROR;
672  vOptional.scode = DISP_E_PARAMNOTFOUND;
673 
674  ADOX::_KeyPtr pKey = 0;
675  ADOX::_TablePtr pTable = 0;
676  ADOX::_CatalogPtr pCatalog = 0;
677 
678  TESTHR(pCatalog.CreateInstance(__uuidof(ADOX::Catalog)));
679  TESTHR(pKey.CreateInstance(__uuidof(ADOX::Key)));
680 
681  try
682  {
683  pCatalog->PutActiveConnection(variant_t((IDispatch *)m_conn->getConn()));
684 
685  pTable = pCatalog->Tables->GetItem(datasetName.c_str());
686 
687  pKey->Name = pTable->Name + "_pk";
688  pKey->Type = ADOX::adKeyPrimary;
689 
690  for(size_t i = 0; i < pk->getProperties().size(); i++)
691  {
692  te::dt::Property* p = pk->getProperties()[i];
693  TESTHR(pKey->Columns->Append(p->getName().c_str(), te::ado::Convert2Ado(p->getType()), 256));
694 
695  }
696 
697  TESTHR(pTable->Keys->Append(_variant_t((IDispatch *)pKey),ADOX::adKeyPrimary,vOptional,L"",L""));
698  pCatalog->Tables->Refresh();
699  }
700  catch(_com_error& e)
701  {
702  throw te::common::Exception((LPCSTR)e.ErrorMessage());
703  }
704 }
705 
706 void te::ado::Transactor::dropPrimaryKey(const std::string& datasetName)
707 {
708  ADOX::_KeyPtr pKey = 0;
709  ADOX::_TablePtr pTable = 0;
710  ADOX::_CatalogPtr pCatalog = 0;
711 
712  std::auto_ptr<te::da::PrimaryKey> pk(getPrimaryKey(datasetName));
713 
714  try
715  {
716  pCatalog->PutActiveConnection(variant_t((IDispatch *)m_conn->getConn()));
717 
718  pTable = pCatalog->Tables->GetItem(datasetName.c_str());
719 
720  pKey = pTable->Keys->GetItem(pk->getName().c_str());
721 
722  TESTHR(pTable->GetKeys()->Delete(_variant_t((IDispatch *)pKey)));
723  }
724  catch(_com_error& e)
725  {
726  throw te::common::Exception(TE_TR((LPCSTR)e.ErrorMessage()));
727  }
728 }
729 
730 std::auto_ptr<te::da::ForeignKey> te::ado::Transactor::getForeignKey(const std::string& datasetName, const std::string& name)
731 {
732  if(!foreignKeyExists(datasetName, name))
733  throw te::common::Exception((boost::format(TE_TR("The dataset \"%1%\" has no foreign key with this name \"%2%\"!")) % datasetName % name).str());
734 
735  std::auto_ptr<te::da::DataSetType> dt(getDataSetType(datasetName));
736 
737  return std::auto_ptr<te::da::ForeignKey>(static_cast<te::da::ForeignKey*>(dt->getForeignKey(name)->clone()));
738 }
739 
740 std::vector<std::string> te::ado::Transactor::getForeignKeyNames(const std::string& datasetName)
741 {
742  std::auto_ptr<te::da::DataSetType> dt(getDataSetType(datasetName));
743 
744  std::vector<std::string> fkNames;
745 
746  std::size_t numFK = dt->getNumberOfForeignKeys();
747 
748  for(std::size_t i = 0; i < numFK; ++i)
749  fkNames.push_back(dt->getForeignKey(i)->getName());
750 
751  return fkNames;
752 }
753 
754 bool te::ado::Transactor::foreignKeyExists(const std::string& datasetName, const std::string& name)
755 {
756  std::auto_ptr<te::da::DataSetType> dt(getDataSetType(datasetName));
757 
758  std::vector<std::string> fkNames = getForeignKeyNames(datasetName);
759 
760  if(std::find(fkNames.begin(), fkNames.end(), name) != fkNames.end())
761  return true;
762 
763  return false;
764 }
765 
766 void te::ado::Transactor::addForeignKey(const std::string& datasetName, te::da::ForeignKey* fk)
767 {
768  _variant_t vOptional;
769  vOptional.vt = VT_ERROR;
770  vOptional.scode = DISP_E_PARAMNOTFOUND;
771 
772  ADOX::_KeyPtr pKey = 0;
773  ADOX::_TablePtr pTable = 0;
774  ADOX::_CatalogPtr pCatalog = 0;
775 
776  try
777  {
778  pCatalog->PutActiveConnection(variant_t((IDispatch *)m_conn->getConn()));
779 
780  pTable = pCatalog->Tables->GetItem(datasetName.c_str());
781 
782  TESTHR(pKey.CreateInstance(__uuidof(ADOX::Key)));
783 
784  pKey->Name = pTable->Name + "_fk";
785  pKey->Type = ADOX::adKeyForeign;
786  pKey->RelatedTable = fk->getReferencedDataSetType()->getName().c_str();
787 
788  for(size_t i = 0; i < fk->getProperties().size(); i++)
789  {
790 
791  te::dt::Property* p = fk->getProperties()[i];
792 
793  TESTHR(pKey->Columns->Append(p->getName().c_str(), te::ado::Convert2Ado(p->getType()), 256));
794  pKey->Columns->GetItem(p->getName().c_str())->RelatedColumn = fk->getReferencedProperties()[i]->getName().c_str();
795 
796  }
797 
798  TESTHR(pTable->Keys->Append(_variant_t((IDispatch *)pKey),ADOX::adKeyPrimary,vOptional,L"",L""));
799 
800  pCatalog->Tables->Refresh();
801  }
802  catch(_com_error& e)
803  {
804  throw te::common::Exception((LPCSTR)e.ErrorMessage());
805  }
806 }
807 
808 void te::ado::Transactor::dropForeignKey(const std::string& datasetName, const std::string& fkName)
809 {
810  ADOX::_KeyPtr pKey = 0;
811  ADOX::_TablePtr pTable = 0;
812  ADOX::_CatalogPtr pCatalog = 0;
813 
814  try
815  {
816 
817  pCatalog->PutActiveConnection(variant_t((IDispatch *)m_conn->getConn()));
818 
819  pTable = pCatalog->Tables->GetItem(datasetName.c_str());
820 
821  pKey = pTable->Keys->GetItem(fkName.c_str());
822 
823  TESTHR(pTable->GetKeys()->Delete(_variant_t((IDispatch *)pKey)));
824  }
825  catch(_com_error& e)
826  {
827  throw te::common::Exception((LPCSTR)e.ErrorMessage());
828  }
829 }
830 
831 std::auto_ptr<te::da::UniqueKey> te::ado::Transactor::getUniqueKey(const std::string& datasetName, const std::string& name)
832 {
833  if(!uniqueKeyExists(datasetName, name))
834  throw te::common::Exception((boost::format(TE_TR("The dataset \"%1%\" has no unique key with this name \"%2%\"!")) % datasetName % name).str());
835 
836  std::auto_ptr<te::da::DataSetType> dt(getDataSetType(datasetName));
837 
838  return std::auto_ptr<te::da::UniqueKey>(static_cast<te::da::UniqueKey*>(dt->getUniqueKey(name)->clone()));
839 }
840 
841 std::vector<std::string> te::ado::Transactor::getUniqueKeyNames(const std::string& datasetName)
842 {
843  std::auto_ptr<te::da::DataSetType> dt(getDataSetType(datasetName));
844 
845  std::vector<std::string> ukNames;
846 
847  std::size_t numUKs = dt->getNumberOfUniqueKeys();
848 
849  for(std::size_t i = 0; i < numUKs; ++i)
850  ukNames.push_back(dt->getUniqueKey(i)->getName());
851 
852  return ukNames;
853 }
854 
855 bool te::ado::Transactor::uniqueKeyExists(const std::string& datasetName, const std::string& name)
856 {
857  std::auto_ptr<te::da::DataSetType> dt(getDataSetType(datasetName));
858 
859  std::vector<std::string> ukNames = getUniqueKeyNames(datasetName);
860 
861  if(std::find(ukNames.begin(), ukNames.end(), name) != ukNames.end())
862  return true;
863 
864  return false;
865 }
866 
867 void te::ado::Transactor::addUniqueKey(const std::string& datasetName, te::da::UniqueKey* uk)
868 {
869  _variant_t vOptional;
870  vOptional.vt = VT_ERROR;
871  vOptional.scode = DISP_E_PARAMNOTFOUND;
872 
873  ADOX::_KeyPtr pKey = 0;
874  ADOX::_TablePtr pTable = 0;
875  ADOX::_CatalogPtr pCatalog = 0;
876 
877  TESTHR(pCatalog.CreateInstance(__uuidof(ADOX::Catalog)));
878  TESTHR(pKey.CreateInstance(__uuidof(ADOX::Key)));
879 
880  try
881  {
882  pCatalog->PutActiveConnection(variant_t((IDispatch *)m_conn->getConn()));
883 
884  pTable = pCatalog->Tables->GetItem(datasetName.c_str());
885 
886  pKey->Name = pTable->Name + "_uk";
887  pKey->Type = ADOX::adKeyUnique;
888 
889  for(size_t i = 0; i < uk->getProperties().size(); i++)
890  {
891  te::dt::Property* p = uk->getProperties()[i];
892 
893  TESTHR(pKey->Columns->Append(p->getName().c_str(), te::ado::Convert2Ado(p->getType()), 256));
894  }
895 
896  TESTHR(pTable->Keys->Append(_variant_t((IDispatch *)pKey),ADOX::adKeyUnique,vOptional,L"",L""));
897 
898  pCatalog->Tables->Refresh();
899  }
900  catch(_com_error& e)
901  {
902  throw te::common::Exception((LPCSTR)e.ErrorMessage());
903  }
904 }
905 
906 void te::ado::Transactor::dropUniqueKey(const std::string& datasetName, const std::string& name)
907 {
908  ADOX::_KeyPtr pKey = 0;
909  ADOX::_TablePtr pTable = 0;
910  ADOX::_CatalogPtr pCatalog = 0;
911 
912  try
913  {
914  pCatalog->PutActiveConnection(variant_t((IDispatch *)m_conn->getConn()));
915 
916  pTable = pCatalog->Tables->GetItem(datasetName.c_str());
917 
918  pKey = pTable->Keys->GetItem(name.c_str());
919 
920  TESTHR(pTable->GetKeys()->Delete(_variant_t((IDispatch *)pKey)));
921  }
922  catch(_com_error& e)
923  {
924  throw te::common::Exception((LPCSTR)e.ErrorMessage());
925  }
926 }
927 
928 std::auto_ptr<te::da::CheckConstraint> te::ado::Transactor::getCheckConstraint(const std::string& datasetName, const std::string& name)
929 {
930  if(!checkConstraintExists(datasetName, name))
931  throw te::common::Exception((boost::format(TE_TR("The dataset \"%1%\" has no check constraint with this name \"%2%\"!")) % datasetName % name).str());
932 
933  std::auto_ptr<te::da::DataSetType> dt(getDataSetType(datasetName));
934 
935  return std::auto_ptr<te::da::CheckConstraint>(static_cast<te::da::CheckConstraint*>(dt->getCheckConstraint(name)->clone()));
936 }
937 
938 std::vector<std::string> te::ado::Transactor::getCheckConstraintNames(const std::string& datasetName)
939 {
940  std::auto_ptr<te::da::DataSetType> dt(getDataSetType(datasetName));
941 
942  std::vector<std::string> ccNames;
943 
944  std::size_t numCCs = dt->getNumberOfCheckConstraints();
945 
946  for(std::size_t i = 0; i < numCCs; ++i)
947  ccNames.push_back(dt->getCheckConstraint(i)->getName());
948 
949  return ccNames;
950 }
951 
952 bool te::ado::Transactor::checkConstraintExists(const std::string& datasetName, const std::string& name)
953 {
954  std::vector<std::string> ccNames = getCheckConstraintNames(datasetName);
955 
956  if(std::find(ccNames.begin(), ccNames.end(), name) != ccNames.end())
957  return true;
958 
959  return false;
960 }
961 
962 void te::ado::Transactor::addCheckConstraint(const std::string& /*datasetName*/, te::da::CheckConstraint* /*cc*/)
963 {
964  throw te::common::Exception(TE_TR("Method addCheckConstraint: not implemented yet!"));
965 }
966 
967 void te::ado::Transactor::dropCheckConstraint(const std::string& /*datasetName*/, const std::string& /*name*/)
968 {
969  throw te::common::Exception(TE_TR("Method dropCheckConstraint: not implemented yet!"));
970 }
971 
972 std::auto_ptr<te::da::Index> te::ado::Transactor::getIndex(const std::string& datasetName, const std::string& name)
973 {
974  if(!indexExists(datasetName, name))
975  throw te::common::Exception((boost::format(TE_TR("The dataset \"%1%\" has no index with this name \"%2%\"!")) % datasetName % name).str());
976 
977  std::auto_ptr<te::da::DataSetType> dt(getDataSetType(datasetName));
978 
979  return std::auto_ptr<te::da::Index>(dt->getIndex(name)->clone());
980 }
981 
982 std::vector<std::string> te::ado::Transactor::getIndexNames(const std::string& datasetName)
983 {
984  std::vector<std::string> idxNames;
985 
986  std::auto_ptr<te::da::DataSetType> dt(getDataSetType(datasetName));
987 
988  std::size_t numIdxs = dt->getNumberOfIndexes();
989 
990  for(std::size_t i = 0; i < numIdxs; ++i)
991  idxNames.push_back(dt->getIndex(i)->getName());
992 
993  return idxNames;
994 }
995 
996 bool te::ado::Transactor::indexExists(const std::string& datasetName, const std::string& name)
997 {
998  std::vector<std::string> idxNames = getIndexNames(datasetName);
999 
1000  if(std::find(idxNames.begin(), idxNames.end(), name) != idxNames.end())
1001  return true;
1002 
1003  return false;
1004 }
1005 
1006 void te::ado::Transactor::addIndex(const std::string& datasetName, te::da::Index* idx,
1007  const std::map<std::string, std::string>&)
1008 {
1009  ADOX::_IndexPtr pIndex = 0;
1010  ADOX::_TablePtr pTable = 0;
1011  ADOX::_CatalogPtr pCatalog = 0;
1012 
1013  TESTHR(pCatalog.CreateInstance(__uuidof(ADOX::Catalog)));
1014  TESTHR(pIndex.CreateInstance(__uuidof(ADOX::Index)));
1015 
1016  try
1017  {
1018  pCatalog->PutActiveConnection(variant_t((IDispatch *)m_conn->getConn()));
1019 
1020  pTable = pCatalog->Tables->GetItem(datasetName.c_str());
1021 
1022  pIndex->Name = idx->getName().c_str();
1023 
1024  std::vector<te::dt::Property*> idxProps = idx->getProperties();
1025  for(size_t i = 0; i < idxProps.size(); i++)
1026  {
1027  long size = 0;
1028  if(idxProps[i]->getType() == te::dt::STRING_TYPE)
1029  size = (long)((te::dt::StringProperty*)idxProps[i])->size();
1030 
1031  TESTHR(pIndex->Columns->Append(idxProps[i]->getName().c_str(), te::ado::Convert2Ado(idxProps[i]->getType()), size));
1032  }
1033 
1034  TESTHR(pTable->Indexes->Append(_variant_t((IDispatch *)pIndex)));
1035  }
1036  catch(_com_error& e)
1037  {
1038  throw te::common::Exception((LPCSTR)e.ErrorMessage());
1039  }
1040 }
1041 
1042 void te::ado::Transactor::dropIndex(const std::string& datasetName, const std::string& idxName)
1043 {
1044  if(!indexExists(datasetName, idxName))
1045  throw te::common::Exception((boost::format(TE_TR("The dataset \"%1%\" has no index with this name: \"%2%\"!")) % datasetName % idxName).str());
1046 
1047  std::string sql("DROP INDEX ");
1048  sql += idxName;
1049 
1050  execute(sql);
1051 
1052  std::auto_ptr<te::da::DataSetType> dt(getDataSetType(datasetName));
1053 
1054  te::da::Index* idx = dt->getIndex(idxName);
1055 
1056  dt->remove(idx);
1057 }
1058 
1059 std::auto_ptr<te::da::Sequence> te::ado::Transactor::getSequence(const std::string&)
1060 {
1061  throw te::common::Exception(TE_TR("Method getSequence: not implemented yet!"));
1062 }
1063 
1064 std::vector<std::string> te::ado::Transactor::getSequenceNames()
1065 {
1066  throw te::common::Exception(TE_TR("Method getSequenceNames: not implemented yet!"));
1067 }
1068 
1069 bool te::ado::Transactor::sequenceExists(const std::string& /*name*/)
1070 {
1071  throw te::common::Exception(TE_TR("Method sequenceExists: not implemented yet!"));
1072 }
1073 
1075 {
1076  throw te::common::Exception(TE_TR("Method addSequence: not implemented yet!"));
1077 }
1078 
1079 void te::ado::Transactor::dropSequence(const std::string& /*name*/)
1080 {
1081  throw te::common::Exception(TE_TR("Method dropSequence: not implemented yet!"));
1082 }
1083 
1084 std::auto_ptr<te::gm::Envelope> te::ado::Transactor::getExtent(const std::string& datasetName,
1085  const std::string& /*propertyName*/)
1086 {
1087  if(!dataSetExists(datasetName))
1088  throw te::common::Exception(TE_TR("The Data Set Type does not exist!"));
1089 
1090  std::string sql = "SELECT MIN(lower_x), MIN(lower_y), MAX(upper_x), MAX(upper_y) from " + datasetName;
1091 
1092  std::auto_ptr<te::da::DataSet> resultBox(query(sql));
1093 
1094  std::auto_ptr<te::gm::Envelope> env(new te::gm::Envelope());
1095 
1096  if(resultBox.get())
1097  {
1098  resultBox->moveFirst();
1099  env->m_llx = resultBox->getDouble(0);
1100  env->m_lly = resultBox->getDouble(1);
1101  env->m_urx = resultBox->getDouble(2);
1102  env->m_ury = resultBox->getDouble(3);
1103  }
1104  else
1105  {
1106  throw te::common::Exception(TE_TR("Error when calculating the envelope!"));
1107  }
1108 
1109  return env;
1110 }
1111 
1112 std::auto_ptr<te::gm::Envelope> te::ado::Transactor::getExtent(const std::string& datasetName,
1113  std::size_t /*propertyPos*/)
1114 {
1115  return getExtent(datasetName, 0);
1116 }
1117 
1118 std::size_t te::ado::Transactor::getNumberOfItems(const std::string& datasetName)
1119 {
1120  std::auto_ptr<te::da::DataSet> result(getDataSet(datasetName));
1121 
1122  return result->size();
1123 }
1124 
1126 {
1127  std::vector<std::string> datasetNames = getDataSetNames();
1128 
1129  if(datasetNames.empty())
1130  return false;
1131 
1132  return true;
1133 }
1134 
1135 bool te::ado::Transactor::dataSetExists(const std::string& name)
1136 {
1137  std::vector<std::string> datasetNames = getDataSetNames();
1138 
1139  if(std::find(datasetNames.begin(), datasetNames.end(), name) != datasetNames.end())
1140  return true;
1141 
1142  return false;
1143 }
1144 
1145 void te::ado::Transactor::createDataSet(te::da::DataSetType* dt, const std::map<std::string, std::string>&)
1146 {
1147  ADOX::_TablePtr pTable = 0;
1148  ADOX::_CatalogPtr pCatalog = 0;
1149 
1150  TESTHR(pTable.CreateInstance(__uuidof (ADOX::Table)));
1151  TESTHR(pCatalog.CreateInstance(__uuidof(ADOX::Catalog)));
1152 
1153  try
1154  {
1155  pCatalog->PutActiveConnection(variant_t((IDispatch *)m_conn->getConn()));
1156 
1157  pTable->Name = dt->getName().c_str();
1158 
1159  TESTHR(pCatalog->Tables->Append(_variant_t((IDispatch*)pTable)));
1160 
1161  }
1162  catch(_com_error &e)
1163  {
1164  throw te::common::Exception(TE_TR(e.ErrorMessage()));
1165  }
1166 
1167  std::size_t ncols = dt->size();
1168 
1169  for(std::size_t i = 0; i < ncols; ++i)
1170  addProperty(dt->getName(), dt->getProperty(i));
1171 
1172  if(dt->getPrimaryKey())
1173  addPrimaryKey(dt->getName(), dt->getPrimaryKey());
1174 }
1175 
1176 void te::ado::Transactor::cloneDataSet(const std::string& /*name*/,
1177  const std::string& /*cloneName*/,
1178  const std::map<std::string, std::string>& /*options*/)
1179 {
1180  throw te::common::Exception(TE_TR("Method cloneDataSet: not implemented yet!"));
1181 }
1182 
1183 void te::ado::Transactor::dropDataSet(const std::string& name)
1184 {
1185  ADOX::_CatalogPtr pCatalog = 0;
1186 
1187  TESTHR(pCatalog.CreateInstance(__uuidof(ADOX::Catalog)));
1188 
1189  try
1190  {
1191  pCatalog->PutActiveConnection(variant_t((IDispatch *)m_conn->getConn()));
1192 
1193  TESTHR(pCatalog->Tables->Delete(name.c_str()));
1194  }
1195  catch(_com_error& e)
1196  {
1197  throw te::common::Exception((LPCSTR)e.ErrorMessage());
1198  }
1199 }
1200 
1201 void te::ado::Transactor::renameDataSet(const std::string& name, const std::string& newName)
1202 {
1203  ADOX::_CatalogPtr pCatalog = 0;
1204 
1205  TESTHR(pCatalog.CreateInstance(__uuidof(ADOX::Catalog)));
1206 
1207  try
1208  {
1209  pCatalog->Tables->GetItem(name.c_str())->PutName(newName.c_str());
1210  }
1211  catch(_com_error& e)
1212  {
1213  throw te::common::Exception((LPCSTR)e.ErrorMessage());
1214  }
1215 }
1216 
1217 void te::ado::Transactor::add(const std::string& datasetName,
1218  te::da::DataSet* d,
1219  const std::map<std::string, std::string>&,
1220  std::size_t)
1221 {
1222  _RecordsetPtr recset;
1223  TESTHR(recset.CreateInstance(__uuidof(Recordset)));
1224 
1225  try
1226  {
1227  TESTHR(recset->Open(_bstr_t(datasetName.c_str()),
1228  _variant_t((IDispatch*)m_conn->getConn(),true), adOpenKeyset, adLockOptimistic, adCmdTable));
1229 
1230  while(d->moveNext())
1231  {
1232  TESTHR(recset->AddNew());
1233 
1234  for(std::size_t i = 0; i < d->getNumProperties(); ++i)
1235  {
1236 
1237  std::string pname = d->getPropertyName(i);
1238  int pType = d->getPropertyDataType(i);
1239 
1240  if(d->isNull(i))
1241  continue;
1242  //{
1243  // _variant_t var;
1244  // var.ChangeType(VT_NULL, NULL);
1245  // recset->GetFields()->GetItem(pname.c_str())->PutValue(var);
1246  //}
1247 
1248 
1249  switch(pType)
1250  {
1251  case te::dt::CHAR_TYPE:
1252  recset->GetFields()->GetItem(pname.c_str())->Value = (_bstr_t)d->getChar(pname.c_str());
1253  break;
1254 
1255  case te::dt::UCHAR_TYPE:
1256  recset->GetFields()->GetItem(pname.c_str())->Value = (_bstr_t)d->getUChar(pname.c_str());
1257  break;
1258 
1259  case te::dt::INT16_TYPE:
1260  recset->GetFields()->GetItem(pname.c_str())->Value = (_variant_t)d->getInt16(pname.c_str());
1261  break;
1262 
1263  case te::dt::INT32_TYPE:
1264  recset->GetFields()->GetItem(pname.c_str())->Value = (_variant_t)d->getInt32(pname.c_str());
1265  break;
1266 
1267  case te::dt::INT64_TYPE:
1268  recset->GetFields()->GetItem(pname.c_str())->Value = (_variant_t)d->getInt64(pname.c_str());
1269  break;
1270 
1271  case te::dt::NUMERIC_TYPE:
1272  {
1273  std::string sval = d->getNumeric(pname);
1274  if(sval.empty())
1275  sval = "0.0";
1276 
1277  double dval = boost::lexical_cast<double>(sval);
1278  recset->GetFields()->GetItem(pname.c_str())->Value = (_variant_t)(dval);
1279  }
1280  break;
1281 
1282  case te::dt::DATETIME_TYPE:
1283  {
1284  std::auto_ptr<te::dt::DateTime> dtm(d->getDateTime(i));
1285 
1286  std::string dtt = te::ado::GetFormattedDateTime(dtm.get());
1287 
1288  recset->GetFields()->GetItem(pname.c_str())->Value = (_bstr_t)dtt.c_str();
1289 
1290  break;
1291  }
1292 
1293  case te::dt::FLOAT_TYPE:
1294  recset->GetFields()->GetItem(pname.c_str())->Value = (_variant_t)d->getFloat(pname.c_str());
1295  break;
1296 
1297  case te::dt::DOUBLE_TYPE:
1298  recset->GetFields()->GetItem(pname.c_str())->Value = (_variant_t)d->getDouble(pname.c_str());
1299  break;
1300 
1301  case te::dt::STRING_TYPE:
1302  recset->GetFields()->GetItem(pname.c_str())->Value = (_bstr_t)d->getString(pname.c_str()).c_str();
1303  break;
1304 
1305  case te::dt::BOOLEAN_TYPE:
1306  recset->GetFields()->GetItem(pname.c_str())->Value = (_variant_t)d->getBool(pname.c_str());
1307  break;
1308 
1310  {
1311  /*char * data = ((te::dt::ByteArray*)props[i])->getData();
1312 
1313  _variant_t var;
1314  te::ado::Blob2Variant(data, ((te::dt::ByteArray*)props[i])->bytesUsed(), var);
1315 
1316  recset->Fields->GetItem(props[i]->getName().c_str())->AppendChunk (var);
1317 
1318  break;*/
1319  }
1320 
1321  //case te::dt::ARRAY_TYPE:
1322  case te::dt::GEOMETRY_TYPE:
1323  {
1324  std::auto_ptr<te::gm::Geometry> geometry(d->getGeometry(pname));
1325  const te::gm::Envelope* env = geometry->getMBR();
1326 
1327  recset->GetFields()->GetItem("lower_x")->Value = (_variant_t)env->m_llx;
1328  recset->GetFields()->GetItem("lower_y")->Value = (_variant_t)env->m_lly;
1329  recset->GetFields()->GetItem("upper_x")->Value = (_variant_t)env->m_urx;
1330  recset->GetFields()->GetItem("upper_y")->Value = (_variant_t)env->m_ury;
1331 
1332  _variant_t var;
1333  Convert2Ado(geometry.get(), var);
1334 
1335  recset->Fields->GetItem(pname.c_str())->AppendChunk (var);
1336 
1337  break;
1338  }
1339 
1340  default:
1341  throw te::common::Exception(TE_TR("The informed type could not be mapped to ADO type system!"));
1342  break;
1343  }
1344  }
1345 
1346  TESTHR(recset->Update());
1347 
1348  }
1349  }
1350  catch(_com_error& e)
1351  {
1352  throw te::common::Exception((LPCSTR)e.ErrorMessage());
1353  }
1354 }
1355 
1356 void te::ado::Transactor::remove(const std::string& datasetName, const te::da::ObjectIdSet*)
1357 {
1358  ADOX::_CatalogPtr pCatalog = 0;
1359 
1360  TESTHR(pCatalog.CreateInstance(__uuidof(ADOX::Catalog)));
1361 
1362  try
1363  {
1364  pCatalog->PutActiveConnection(variant_t((IDispatch *)m_conn->getConn()));
1365 
1366  TESTHR(pCatalog->Tables->Delete(datasetName.c_str()));
1367  }
1368  catch(_com_error& e)
1369  {
1370  throw te::common::Exception((LPCSTR)e.ErrorMessage());
1371  }
1372 }
1373 
1374 void te::ado::Transactor::update(const std::string&,
1375  te::da::DataSet*,
1376  const std::vector<std::size_t>&,
1377  const te::da::ObjectIdSet*,
1378  const std::map<std::string, std::string>&,
1379  std::size_t)
1380 {
1381  //TODO
1382 }
1383 
1384 void te::ado::Transactor::update(const std::string& datasetName,
1385  te::da::DataSet* dataset,
1386  const std::vector< std::set<int> >& properties,
1387  const std::vector<size_t>& ids)
1388 {
1389  dataset->moveFirst();
1390 
1391  int i=0;
1392  std::set<int> plst;
1393  std::set<int>::iterator it;
1394 
1395  try
1396  {
1397  begin();
1398 
1399  do
1400  {
1401  std::string sql = "UPDATE " + datasetName + " SET ";
1402  plst = properties[i];
1403  std::string pName;
1404  std::string id;
1405  int k = 0;
1406 
1407  for(it = plst.begin(); it != plst.end(); ++it)
1408  {
1409  if(k>0)
1410  pName += ",";
1411 
1412  pName += dataset->getPropertyName(*it);
1413 
1414  if(dataset->getPropertyDataType(*it) == te::dt::STRING_TYPE)
1415  pName += "=\"" + dataset->getAsString(*it) + "\"";
1416  else
1417  pName += "=" + dataset->getAsString(*it);
1418 
1419  k++;
1420  }
1421 
1422  for(size_t j=0; j<ids.size(); ++j)
1423  {
1424  if(j>0)
1425  id += " AND ";
1426 
1427  id += dataset->getPropertyName(j) += "=";
1428 
1429  if(dataset->getPropertyDataType(j) == te::dt::STRING_TYPE)
1430  id += "\"" + dataset->getAsString(j) + "\"";
1431  else
1432  id += dataset->getAsString(j);
1433  }
1434 
1435  sql += pName + " WHERE " + id;
1436 
1437  execute(sql);
1438 
1439  i++;
1440  } while (dataset->moveNext());
1441 
1442  commit();
1443  }
1444  catch(te::common::Exception& e)
1445  {
1446  rollBack();
1447 
1448  throw e;
1449  }
1450 }
1451 
1452 void te::ado::Transactor::optimize(const std::map<std::string, std::string>&)
1453 {
1454 
1455 }
1456 
1458 {
1459  _ConnectionPtr adoConn = m_conn->getConn();
1460 
1461  ADOX::_CatalogPtr pCatalog = 0;
1462 
1463  TESTHR(pCatalog.CreateInstance(__uuidof(ADOX::Catalog)));
1464 
1465  try
1466  {
1467  pCatalog->PutActiveConnection(variant_t((IDispatch *)adoConn));
1468  }
1469  catch(_com_error& e)
1470  {
1471  throw te::common::Exception((LPCSTR)e.ErrorMessage());
1472  }
1473  ADOX::TablesPtr tables = pCatalog->GetTables();
1474 
1475  ADOX::_TablePtr t = tables->GetItem(dt->getName().c_str());
1476 
1477  ADOX::KeysPtr keys = t->GetKeys();
1478 
1479  ADOX::_KeyPtr pk = 0;
1480 
1481  for(long i = 0; i < keys->Count; i++)
1482  {
1483  if(keys->GetItem(i)->GetType() == ADOX::adKeyPrimary)
1484  pk = keys->GetItem(i);
1485  }
1486 
1487  if(pk == 0)
1488  {
1489  dt->setPrimaryKey(0);
1490  return;
1491  }
1492 
1493  ADOX::ColumnsPtr cols = pk->GetColumns();
1494 
1495  te::da::PrimaryKey* tlPk = new te::da::PrimaryKey(std::string(pk->GetName()), dt);
1496 
1497  for(long i = 0; i < cols->GetCount(); i++)
1498  tlPk->add(dt->getProperty(std::string(cols->GetItem(i)->GetName())));
1499 }
1500 
1502 {
1503  std::string dsName = dt->getName();
1504  int numCols = 0;
1505 
1506  ADOX::DataTypeEnum colType;
1507  std::map<int, std::string> colNamesMap;
1508  std::map<int, ADOX::DataTypeEnum> colTypesMap;
1509  std::map<int, int> charLengthMap;
1510  std::map<int, bool> isRequiredMap;
1511  std::map<int, bool> hasDefaultMap;
1512  std::map<int, std::string> defaultValueMap;
1513 
1514  _ConnectionPtr conn = 0;
1515 
1516  try
1517  {
1518  HRESULT hr = S_OK;
1519 
1520  conn = m_conn->getConn();
1521 
1522  _RecordsetPtr rs = NULL;
1523 
1524  TESTHR(rs.CreateInstance(__uuidof(Recordset)));
1525 
1526  // Create a safearray which takes three elements,and pass it as
1527  // the second parameter in the OpenSchema method.
1528  SAFEARRAY FAR* psa = NULL;
1529  SAFEARRAYBOUND rgsabound;
1530  _variant_t var[3];
1531 
1532  _variant_t Array;
1533  rgsabound.lLbound = 0;
1534  rgsabound.cElements = 3;
1535  psa = SafeArrayCreate(VT_VARIANT, 1, &rgsabound);
1536 
1537  var[0].vt = VT_EMPTY;
1538  var[1].vt = VT_EMPTY;
1539  var[2] = dsName.c_str();
1540 
1541  // Fill the safe array.
1542  for(LONG i = 0; i < 3; ++i)
1543  hr = SafeArrayPutElement(psa, &i, &var[i]);
1544 
1545  Array.vt = VT_ARRAY | VT_VARIANT;
1546  Array.parray = psa;
1547 
1548  rs = conn->OpenSchema(adSchemaColumns, &Array, vtMissing);
1549 
1550  int pos;
1551  while (!(rs->EndOfFile))
1552  {
1553  // Get the column name
1554  _bstr_t columnName = rs->Fields->GetItem("COLUMN_NAME")->Value;
1555  pos = rs->Fields->GetItem("ORDINAL_POSITION")->Value;
1556  pos = pos - 1;
1557  colNamesMap[pos] = (LPCSTR)columnName;
1558 
1559  // Get the data type of the column
1560  colType = ADOX::DataTypeEnum(int(rs->Fields->GetItem("DATA_TYPE")->Value));
1561  colTypesMap[pos] = colType;
1562 
1563  // Get the length of the column
1564  _variant_t length = rs->Fields->GetItem("CHARACTER_MAXIMUM_LENGTH")->Value;
1565  int charLength = 0;
1566  if(length.vt != VT_NULL)
1567  charLength = (int)length.dblVal;
1568  charLengthMap[pos] = charLength;
1569 
1570  // Get the columns that accept null values
1571  bool nullVal = rs->Fields->GetItem("IS_NULLABLE")->Value;
1572  isRequiredMap[pos] = !nullVal;
1573 
1574  // Get the columns that has default values
1575  bool hasDefault = rs->Fields->GetItem("COLUMN_HASDEFAULT")->Value;
1576  hasDefaultMap[pos] = !hasDefault;
1577 
1578  // Get the default value
1579  std::string defaultStr;
1580  if(hasDefault)
1581  {
1582  _bstr_t defaultValue = rs->Fields->GetItem("COLUMN_DEFAULT")->Value;
1583  defaultStr = (LPSTR)defaultValue;
1584  }
1585 
1586  defaultValueMap[pos] = defaultStr;
1587 
1588  rs->MoveNext();
1589  ++numCols;
1590  }
1591  }
1592  catch (_com_error& e)
1593  {
1594  std::cout << "Error = " << (char*) e.ErrorMessage() << std::endl;
1595  }
1596 
1597  // Create the dataset properties
1598  for(int i = 0; i < numCols; ++i)
1599  {
1600  te::dt::Property* p = 0;
1601  ADOX::DataTypeEnum colType = colTypesMap[i];
1602  std::string colName = colNamesMap[i];
1603 
1604  switch(colType)
1605  {
1606  case ::adBoolean:
1607  {
1608  p = new te::dt::SimpleProperty(colName, Convert2Terralib(colType));
1610 
1611  sp->setRequired(isRequiredMap[i]);
1612  break;
1613  }
1614 
1615  case ::adVarWChar:
1616  case ::adWChar:
1617  case ::adVarChar:
1618  case ::adLongVarChar:
1619  case ::adLongVarWChar:
1620  case ::adBSTR:
1621  case ::adChar:
1622  {
1623  p = new te::dt::StringProperty(colName, (te::dt::StringType)Convert2Terralib(colType), charLengthMap[i]);
1625 
1626  sp->setRequired(isRequiredMap[i]);
1627  sp->setSize(charLengthMap[i]);
1628 
1629  break;
1630  }
1631 
1632  case ADOX::adTinyInt:
1633  case ADOX::adSmallInt:
1634  case ADOX::adInteger:
1635  case ADOX::adBigInt:
1636  case ADOX::adSingle:
1637  case ADOX::adDouble:
1638  case ADOX::adDecimal:
1639  case ::adUnsignedBigInt:
1640  case ::adUnsignedInt:
1641  case ::adUnsignedSmallInt:
1642  case ::adUnsignedTinyInt:
1643  {
1644  p = new te::dt::SimpleProperty(colName, Convert2Terralib(colType));
1646 
1647  sp->setRequired(isRequiredMap[i]);
1648  break;
1649  }
1650 
1651  case ADOX::adBinary:
1652  case ADOX::adLongVarBinary:
1653  {
1654 
1655  std::map<std::string, std::string> geomColumns = m_ds->getGeomColumns();
1656  std::map<std::string, std::string>::iterator it = geomColumns.find(dsName);
1657 
1658  if(it != geomColumns.end())
1659  {
1660  if(it->second == colName)
1661  {
1662  p = new te::gm::GeometryProperty(colName, te::ado::GetSRID(conn, dsName, colName), te::ado::GetType(conn, dsName, colName));
1663  }
1664  }
1665  else
1666  {
1667  p = new te::dt::SimpleProperty(colName, Convert2Terralib(colType));
1668  }
1669 
1670  break;
1671  }
1672 
1673  case ADOX::adDate:
1674  case ADOX::adDBDate:
1675  case ADOX::adDBTime:
1676  case ADOX::adDBTimeStamp:
1678  break;
1679 
1680  default:
1681  p = new te::dt::SimpleProperty(colName, te::dt::UNKNOWN_TYPE);
1682  break;
1683  }
1684 
1685  dt->add(p);
1686  }
1687 }
1688 
1690 {
1691  _ConnectionPtr adoConn = m_conn->getConn();
1692 
1693  ADOX::_CatalogPtr pCatalog = 0;
1694 
1695  TESTHR(pCatalog.CreateInstance(__uuidof(ADOX::Catalog)));
1696 
1697  try
1698  {
1699  pCatalog->PutActiveConnection(variant_t((IDispatch *)adoConn));
1700  }
1701  catch(_com_error& e)
1702  {
1703  throw te::common::Exception((LPCSTR)e.ErrorMessage());
1704  }
1705 
1706  ADOX::TablesPtr tables = pCatalog->GetTables();
1707 
1708  ADOX::_TablePtr t = tables->GetItem(dt->getName().c_str());
1709 
1710  ADOX::KeysPtr keys = t->GetKeys();
1711 
1712  for(long i = 0; i < keys->Count; i++)
1713  {
1714  if(keys->GetItem(i)->GetType() == ADOX::adKeyUnique)
1715  {
1716  ADOX::_KeyPtr uk = keys->GetItem(i);
1717 
1718  te::da::UniqueKey* tlUk = new te::da::UniqueKey(std::string(uk->GetName()), dt);
1719 
1720  ADOX::ColumnsPtr cols = uk->GetColumns();
1721 
1722  for(long j = 0; j < cols->Count; j++)
1723  tlUk->add(dt->getProperty(std::string(cols->GetItem(i)->GetName())));
1724  }
1725  }
1726 }
1727 
1729 {
1730  _ConnectionPtr adoConn = m_conn->getConn();
1731 
1732  ADOX::_CatalogPtr pCatalog = 0;
1733 
1734  TESTHR((pCatalog.CreateInstance(__uuidof(ADOX::Catalog))));
1735 
1736  try
1737  {
1738  pCatalog->PutActiveConnection(variant_t((IDispatch *)adoConn));
1739  }
1740  catch(_com_error& e)
1741  {
1742  throw te::common::Exception((LPCSTR)e.ErrorMessage());
1743  }
1744 
1745  ADOX::TablesPtr tables = pCatalog->GetTables();
1746 
1747  ADOX::_TablePtr t = tables->GetItem(dt->getName().c_str());
1748 
1749  ADOX::IndexesPtr idxs = t->GetIndexes();
1750 
1751  for(long i = 0; i < idxs->GetCount(); i++)
1752  {
1753  ADOX::_IndexPtr idx = idxs->GetItem(i);
1754 
1755  te::da::Index* tlIdx = new te::da::Index();
1756  tlIdx->setName(std::string(idx->GetName()));
1757 
1758  std::vector<te::dt::Property*> props;
1759 
1760  ADOX::ColumnsPtr cols = idx->GetColumns();
1761  for(long i = 0; i < cols->GetCount(); i++)
1762  props.push_back(dt->getProperty(std::string(cols->GetItem(i)->GetName())));
1763 
1764  tlIdx->setProperties(props);
1765 
1766  dt->add(tlIdx);
1767  }
1768 }
1769 
1771 {
1772  _RecordsetPtr rs = NULL;
1773 
1774  std::string dtName = dt->getName();
1775 
1776  std::string str = "[" + dtName + "]";
1777 
1778  try
1779  {
1780  _ConnectionPtr adoConn = m_conn->getConn();
1781 
1782  TESTHR(rs.CreateInstance(__uuidof(Recordset)));
1783 
1784  rs = adoConn->OpenSchema(adSchemaCheckConstraints);
1785 
1786  while (!(rs->EndOfFile))
1787  {
1788  std::string constraintName = (LPCSTR)(bstr_t)(rs->Fields->GetItem("CONSTRAINT_NAME")->GetValue());
1789 
1790  if(constraintName.find(str) != std::string::npos)
1791  {
1792  te::da::CheckConstraint* cc = new te::da::CheckConstraint(constraintName, dt);
1793  std::string checkClause = (LPCSTR)(bstr_t)(rs->Fields->GetItem("CHECK_CLAUSE")->GetValue());
1794  cc->setExpression(checkClause);
1795  }
1796 
1797  rs->MoveNext();
1798  }
1799  }
1800  catch(_com_error& e)
1801  {
1802  throw te::common::Exception((LPCSTR)e.ErrorMessage());
1803  }
1804 
1805  // Clean up objects before exit.
1806  if(rs && rs->State == adStateOpen)
1807  rs->Close();
1808 }
1809 
1810 void te::ado::Transactor::insertIntoGeometryColumns(const std::string& datasetName,
1811  te::gm::GeometryProperty* geomProp)
1812 {
1813  _ConnectionPtr adoConn = m_conn->getConn();
1814 
1815  int coord_dimension = 2;
1816 
1817  if(te::ado::IsZProperty(geomProp->getGeometryType()))
1818  coord_dimension = 3;
1819 
1820  _RecordsetPtr recset;
1821  TESTHR(recset.CreateInstance(__uuidof(Recordset)));
1822 
1823  try
1824  {
1825  TESTHR(recset->Open(_bstr_t("geometry_columns"),
1826  _variant_t((IDispatch*)adoConn,true), adOpenKeyset, adLockOptimistic, adCmdTable));
1827 
1828  TESTHR(recset->AddNew());
1829 
1830  recset->GetFields()->GetItem("f_table_catalog")->Value = (_bstr_t)std::string("''").c_str();
1831  recset->GetFields()->GetItem("f_table_schema")->Value = (_bstr_t)std::string("public").c_str();
1832  recset->GetFields()->GetItem("f_table_name")->Value = (_bstr_t)datasetName.c_str();
1833  recset->GetFields()->GetItem("f_geometry_column")->Value = (_bstr_t)geomProp->getName().c_str();
1834  recset->GetFields()->GetItem("coord_dimension")->Value = (_variant_t)coord_dimension;
1835  recset->GetFields()->GetItem("srid")->Value = (_variant_t)geomProp->getSRID();
1836  recset->GetFields()->GetItem("type")->Value = (_bstr_t)te::ado::GetGeometryName(geomProp->getGeometryType()).c_str();
1837 
1838  recset->Update();
1839  }
1840  catch(_com_error& e)
1841  {
1842  throw te::common::Exception((LPCSTR)e.ErrorMessage());
1843  }
1844 
1845  m_ds->registerGeometryColumn(datasetName, geomProp->getName());
1846 }
1847 
1849 {
1850  return te::common::LATIN1; // TODO
1851 }
Property * getProperty(std::size_t i) const
It returns the i-th property.
void add(te::dt::Property *p)
It adds the property to the list of properties that participates in the unique key.
Definition: UniqueKey.h:124
StringType
The subtype of string property.
Definition: Enums.h:171
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:155
std::auto_ptr< te::da::BatchExecutor > getBatchExecutor()
It creates a batch command executor.
Definition: Transactor.cpp:268
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::vector< std::string > getForeignKeyNames(const std::string &datasetName)
It gets the foreign key names of the given dataset.
Definition: Transactor.cpp:740
void add(te::dt::Property *p)
It adds a property to the list of properties of the primary key.
Definition: PrimaryKey.h:123
DataSet class implementation for Microsoft Access driver.
void dropCheckConstraint(const std::string &datasetName, const std::string &name)
It removes the check constraint from the dataset.
Definition: Transactor.cpp:967
std::vector< std::string > getCheckConstraintNames(const std::string &datasetName)
It gets the check constraint names of the given dataset.
Definition: Transactor.cpp:938
void renameDataSet(const std::string &name, const std::string &newName)
It renames a dataset.
int GetSRID(_ConnectionPtr adoConn, std::string tableName, std::string geomPropName)
Read the geometry_columns table end return a SRID.
Definition: Utils.cpp:828
virtual boost::int32_t getInt32(std::size_t i) const =0
Method for retrieving a 32-bit integer attribute value (4 bytes long).
std::vector< std::string > getSequenceNames()
It gets the sequence names available in the data source.
void remove(const std::string &datasetName, const te::da::ObjectIdSet *oids=0)
It removes all the informed items from the dataset.
void getIndexes(te::da::DataSetType *dt)
It update the DataSetType about the Indexes.
An atomic property like an integer or double.
CharEncoding
Supported charsets (character encoding).
std::size_t getNumberOfItems(const std::string &datasetName)
It retrieves the number of items of the given dataset.
void getUniqueKeys(te::da::DataSetType *dt)
It update the DataSetType about the Unique Keys.
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:972
const std::string & GetGeometryName(te::gm::GeomType t)
It returns the geometry OGC names.
Definition: Utils.cpp:525
te::gm::GeomType GetType(_ConnectionPtr adoConn, std::string tableName, std::string geomPropName)
Read the geometry_columns table end return a geometry type.
Definition: Utils.cpp:852
virtual char getChar(std::size_t i) const =0
Method for retrieving a signed character attribute value (1 byte long).
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.
virtual bool getBool(std::size_t i) const =0
Method for retrieving a boolean attribute value.
A class that models the description of a dataset.
Definition: DataSetType.h:72
int Convert2Terralib(ADOX::DataTypeEnum adoType)
Bind ADOX Type to TerraLib Type.
Definition: Utils.cpp:229
bool isInTransaction() const
It returns true if a transaction is in progress, otherwise, it returns false.
Definition: Transactor.cpp:150
boost::int64_t getLastGeneratedId()
It returns the last id generated by an insertion command.
Definition: Transactor.cpp:277
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:730
virtual boost::int64_t getInt64(std::size_t i) const =0
Method for retrieving a 64-bit integer attribute value (8 bytes long).
void addForeignKey(const std::string &datasetName, te::da::ForeignKey *fk)
It adds a foreign key constraint to a dataset.
Definition: Transactor.cpp:766
void setSize(std::size_t s)
It sets the maximum number of characters for a varying string, or the number of characters for a fixe...
std::vector< std::string > getIndexNames(const std::string &datasetName)
It gets the index names of the given dataset.
Definition: Transactor.cpp:982
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.
double m_urx
Upper right corner x-coordinate.
Definition: Envelope.h:346
PrimaryKey * getPrimaryKey() const
It returns the primary key associated to the dataset type.
Definition: DataSetType.h:214
SpatialRelation
Spatial relations between geometric objects.
Definition: Enums.h:122
bool hasDataSets()
It checks if the data source has any dataset.
It describes a sequence (a number generator).
Definition: Sequence.h:56
A visitor for building an SQL statement using ADO dialect.
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
std::string MakeConnectionStr(const std::map< std::string, std::string > &dsInfo)
Create a connection string based on a map.
Definition: Utils.cpp:75
void addProperty(const std::string &datasetName, te::dt::Property *p)
It adds a new property to the dataset schema.
Definition: Transactor.cpp:456
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
std::string GetFormattedDateTime(te::dt::DateTime *dateTime)
It gets a formatted DateTime string for ADO.
Definition: Utils.cpp:1255
A class that implements a connection to a ADO database.
Definition: Connection.h:60
std::size_t size() const
It returns the maximum number of characters for a varying string, or the number of characters for a f...
It models a property definition.
Definition: Property.h:59
virtual double getDouble(std::size_t i) const =0
Method for retrieving a double attribute value.
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:366
bool isEnvelopeProperty(const std::string &name)
Definition: Transactor.cpp:81
const std::vector< te::dt::Property * > & getProperties() const
It returns the properties that take part of the primary key.
Definition: PrimaryKey.h:109
void addSequence(te::da::Sequence *sequence)
It creates a new sequence in the data source.
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:996
std::vector< std::string > getUniqueKeyNames(const std::string &datasetName)
It gets the unique key names of the given dataset.
Definition: Transactor.cpp:841
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 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.
Implementation of a random-access dataset class for the TerraLib In-Memory Data Access driver...
Definition: DataSet.h:65
Implementation of a dataset for the ADO driver.
Definition: DataSet.h:56
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.
std::string GetAdoStringType(const int &terralib)
Bind TerraLib type to an ADO valid fiel type name.
Definition: Utils.cpp:174
double m_llx
Lower left corner x-coordinate.
Definition: Envelope.h:344
bool primaryKeyExists(const std::string &datasetName, const std::string &name)
It checks if a primary key exists in the dataset.
Definition: Transactor.cpp:658
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...
bool sequenceExists(const std::string &name)
It checks if a sequence with the given name exists in the data source.
void begin()
It starts a new transaction.
Definition: Transactor.cpp:111
int getSRID() const
It returns the spatial reference system identifier associated to this property.
void changePropertyDefinition(const std::string &datasetName, const std::string &propName, te::dt::Property *newProp)
Definition: Transactor.cpp:640
virtual float getFloat(std::size_t i) const =0
Method for retrieving a float attribute value.
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
void addCheckConstraint(const std::string &datasetName, te::da::CheckConstraint *cc)
It adds a check constraint to the dataset.
Definition: Transactor.cpp:962
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 std::string getPropertyName(std::size_t i) const =0
It returns the property name at position pos.
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:831
virtual bool isNull(std::size_t i) const =0
It checks if the attribute value is NULL.
void dropProperty(const std::string &datasetName, const std::string &name)
It removes a property from the given dataset.
Definition: Transactor.cpp:572
void insertIntoGeometryColumns(const std::string &datasetName, te::gm::GeometryProperty *geomProp)
It insert a geometry property in the geometry_clumns (SFS Schema).
std::string escape(const std::string &value)
It escapes a string for using in commands and queries.
Definition: Transactor.cpp:282
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:263
GeomType getGeometryType() const
It returns the geometry subtype allowed for the property.
std::vector< std::string > getDataSetNames()
It It gets the dataset names available in the data source.
Definition: Transactor.cpp:287
void addUniqueKey(const std::string &datasetName, te::da::UniqueKey *uk)
It adds a unique key constraint to the dataset.
Definition: Transactor.cpp:867
void addPrimaryKey(const std::string &datasetName, te::da::PrimaryKey *pk)
It adds a primary key constraint to the dataset schema.
Definition: Transactor.cpp:668
DataSetType * getReferencedDataSetType() const
It returns the referenced DataSetType of this foreign key constraint.
Definition: ForeignKey.h:153
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:406
void dropIndex(const std::string &datasetName, const std::string &idxName)
It removes the index from the dataset schema.
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
bool isRequired() const
It returns true if the attribute is required, otherwise it returns false.
The type for string types: FIXED_STRING, VAR_STRING or STRING.
It describes a unique key (uk) constraint.
Definition: UniqueKey.h:53
void setProperties(const std::vector< te::dt::Property * > &properties)
It sets the properties that take part of the index.
Definition: Index.h:190
Property * getParent() const
It returns the parent of this property, or NULL, if it doesn't have one.
Definition: Property.h:168
std::size_t size() const
It returns the number of properties of the CompositeProperty.
void rollBack()
It aborts the transaction. Any changes will be rolled-back.
Definition: Transactor.cpp:137
A visitor for building an SQL statement using ADO dialect.
Definition: SQLVisitor.h:49
std::auto_ptr< te::da::PrimaryKey > getPrimaryKey(const std::string &datasetName)
It retrieves the primary key of the dataset.
Definition: Transactor.cpp:651
virtual std::size_t getNumProperties() const =0
It returns the number of properties that composes an item of the dataset.
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
Definition: Exception.h:58
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
Definition: Geometry.h:73
void dropDataSet(const std::string &name)
It removes the dataset schema from the data source.
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:612
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 getCheckConstraints(te::da::DataSetType *dt)
It update the DataSetType about the Check Constraints.
Connection * m_conn
The connection used by this transactor.
Definition: Transactor.h:329
double m_lly
Lower left corner y-coordinate.
Definition: Envelope.h:345
Implementation of the data source class for the ADO driver.
void commit()
It commits the transaction.
Definition: Transactor.cpp:124
void dropPrimaryKey(const std::string &datasetName)
It removes the primary key constraint from the dataset schema.
Definition: Transactor.cpp:706
DataSourceTransactor class implementation for Microsoft Access driver.
int getType() const
It returns the property data type.
Definition: Property.h:161
void add(Constraint *c)
It adds a new constraint.
std::size_t getNumberOfDataSets()
It retrieves the number of data sets available in the data source.
Definition: Transactor.cpp:328
A dataset is the unit of information manipulated by the data access module of TerraLib.
Definition: DataSet.h:112
const std::vector< te::dt::Property * > & getProperties() const
It returns the properties that form the unique key.
Definition: UniqueKey.h:110
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:754
te::common::CharEncoding getEncoding()
It return the DataSource current encoding.
Utility functions for ADO.
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.
const std::vector< te::dt::Property * > & getProperties() const
It returns the properties that take part of the index.
Definition: Index.h:183
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:928
It describes a primary key (pk) constraint.
Definition: PrimaryKey.h:52
void dropForeignKey(const std::string &datasetName, const std::string &fkName)
It removes the foreign key constraint from the dataset schema.
Definition: Transactor.cpp:808
void setExpression(const std::string &e)
It sets the check constraint expression.
ADOX::DataTypeEnum Convert2Ado(int terralib)
Bind TerraLib Type to ADO Type.
Definition: Utils.cpp:121
The type for date and time types: date, date period, date duration, time duration, time instant, time period, time instant with time zone or time period with time zone.
void setRequired(bool r)
It tells if the property is required or not.
void dropUniqueKey(const std::string &datasetName, const std::string &name)
It removes the unique key constraint from the dataset.
Definition: Transactor.cpp:906
void TESTHR(HRESULT hr)
Definition: Transactor.cpp:75
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:212
The ADO driver.
Definition: DataSource.h:61
A class that implements a connection to a ADO database.
bool IsZProperty(te::gm::GeomType type)
Verifies whether Z property.
Definition: Utils.cpp:917
void setName(const std::string &name)
It sets the index name.
Definition: Index.h:162
const std::map< std::string, std::string > & getConnectionInfo() const
It returns the set of parameters used to set up the access channel to the underlying repository...
Definition: DataSource.cpp:71
virtual std::string getString(std::size_t i) const =0
Method for retrieving a string value attribute.
te::da::DataSource * getDataSource() const
It returns the parent data source of the transactor.
Definition: Transactor.cpp:106
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.
Transactor(DataSource *ds)
Definition: Transactor.cpp:91
virtual unsigned char getUChar(std::size_t i) const =0
Method for retrieving an unsigned character attribute value (1 byte long).
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.
std::vector< std::string > getPropertyNames(const std::string &datasetName)
It gets the property names of the given dataset.
Definition: Transactor.cpp:385
std::size_t getNumberOfProperties(const std::string &datasetName)
It gets the number of properties of the given dataset.
Definition: Transactor.cpp:399
virtual int getPropertyDataType(std::size_t i) const =0
It returns the underlying data type of the property at position pos.
const std::vector< te::dt::Property * > & getProperties() const
It returns the properties that take part of the foreign key constraint.
Definition: ForeignKey.h:103
boost::ptr_vector< te::dt::Property > getProperties(const std::string &datasetName)
It retrieves the properties of the dataset.
Definition: Transactor.cpp:352
virtual bool moveFirst()=0
It moves the internal pointer to the first item in the collection.
std::auto_ptr< te::da::Sequence > getSequence(const std::string &name)
It gets the sequence with the given name in the data source.
const std::vector< te::dt::Property * > & getReferencedProperties() const
It returns the referenced properties (on the referenced DataSetType) of this foreign key constraint...
Definition: ForeignKey.h:128
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:952
void execute(const te::da::Query &command)
It executes the specified command using a generic query representation.
Definition: Transactor.cpp:247
void cancel()
It requests that the data source stop the processing of the current command.
Definition: Transactor.cpp:273
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
An static class with global definitions.
std::auto_ptr< te::da::DataSetType > getDataSetType(const std::string &name)
It gets information about the given dataset.
Definition: Transactor.cpp:333
void setPrimaryKey(PrimaryKey *pk)
It sets the primary key constraint.
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:855
void dropSequence(const std::string &name)
It removes the sequence from the data source.
const std::string & getName() const
It returns the property name.
Definition: Property.h:127
bool dataSetExists(const std::string &name)
It checks if a dataset with the given name exists in the data source.
const std::string & getName() const
It returns the index name.
Definition: Index.h:155