src/terralib/ado/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 "../core/translator/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::unique_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::unique_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::unique_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::unique_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::unique_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::unique_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::unique_ptr<te::da::DataSet> dset(new DataSet(this, result));
234 
235  if(connected)
236  {
237  return dset;
238  }
239  else
240  {
241  std::unique_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::unique_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::unique_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::unique_ptr<te::da::DataSetType> te::ado::Transactor::getDataSetType(const std::string& name)
334 {
335  std::unique_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::unique_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::unique_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::unique_ptr<te::da::DataSetType> dt(getDataSetType(datasetName));
372 
373  return std::unique_ptr<te::dt::Property>(dt->getProperty(name)->clone());
374 }
375 
376 std::unique_ptr<te::dt::Property> te::ado::Transactor::getProperty(const std::string& datasetName, std::size_t propertyPos)
377 {
378  std::unique_ptr<te::da::DataSetType> dt(getDataSetType(datasetName));
379 
380  assert(propertyPos < dt->size());
381 
382  return std::unique_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::unique_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::unique_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::unique_ptr<te::dt::SimpleProperty> lowerX(new te::dt::SimpleProperty("lower_x", te::dt::DOUBLE_TYPE));
547  std::unique_ptr<te::dt::SimpleProperty> lowerY(new te::dt::SimpleProperty("lower_y", te::dt::DOUBLE_TYPE));
548  std::unique_ptr<te::dt::SimpleProperty> upperX(new te::dt::SimpleProperty("upper_x", te::dt::DOUBLE_TYPE));
549  std::unique_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::unique_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::unique_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::unique_ptr<te::da::PrimaryKey> te::ado::Transactor::getPrimaryKey(const std::string& datasetName)
652 {
653  std::unique_ptr<te::da::DataSetType> dt(getDataSetType(datasetName));
654 
655  return std::unique_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::unique_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::unique_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::unique_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::unique_ptr<te::da::DataSetType> dt(getDataSetType(datasetName));
736 
737  return std::unique_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::unique_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::unique_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::unique_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::unique_ptr<te::da::DataSetType> dt(getDataSetType(datasetName));
837 
838  return std::unique_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::unique_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::unique_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::unique_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::unique_ptr<te::da::DataSetType> dt(getDataSetType(datasetName));
934 
935  return std::unique_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::unique_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::unique_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::unique_ptr<te::da::DataSetType> dt(getDataSetType(datasetName));
978 
979  return std::unique_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::unique_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::unique_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::unique_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::unique_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::unique_ptr<te::da::DataSet> resultBox(query(sql));
1093 
1094  std::unique_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::unique_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::unique_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  bool)
1222 {
1223  _RecordsetPtr recset;
1224  TESTHR(recset.CreateInstance(__uuidof(Recordset)));
1225 
1226  try
1227  {
1228  TESTHR(recset->Open(_bstr_t(datasetName.c_str()),
1229  _variant_t((IDispatch*)m_conn->getConn(),true), adOpenKeyset, adLockOptimistic, adCmdTable));
1230 
1231  while(d->moveNext())
1232  {
1233  TESTHR(recset->AddNew());
1234 
1235  for(std::size_t i = 0; i < d->getNumProperties(); ++i)
1236  {
1237 
1238  std::string pname = d->getPropertyName(i);
1239  int pType = d->getPropertyDataType(i);
1240 
1241  if(d->isNull(i))
1242  continue;
1243  //{
1244  // _variant_t var;
1245  // var.ChangeType(VT_NULL, NULL);
1246  // recset->GetFields()->GetItem(pname.c_str())->PutValue(var);
1247  //}
1248 
1249 
1250  switch(pType)
1251  {
1252  case te::dt::CHAR_TYPE:
1253  recset->GetFields()->GetItem(pname.c_str())->Value = (_bstr_t)d->getChar(pname.c_str());
1254  break;
1255 
1256  case te::dt::UCHAR_TYPE:
1257  recset->GetFields()->GetItem(pname.c_str())->Value = (_bstr_t)d->getUChar(pname.c_str());
1258  break;
1259 
1260  case te::dt::INT16_TYPE:
1261  recset->GetFields()->GetItem(pname.c_str())->Value = (_variant_t)d->getInt16(pname.c_str());
1262  break;
1263 
1264  case te::dt::INT32_TYPE:
1265  recset->GetFields()->GetItem(pname.c_str())->Value = (_variant_t)d->getInt32(pname.c_str());
1266  break;
1267 
1268  case te::dt::INT64_TYPE:
1269  recset->GetFields()->GetItem(pname.c_str())->Value = (_variant_t)d->getInt64(pname.c_str());
1270  break;
1271 
1272  case te::dt::NUMERIC_TYPE:
1273  {
1274  std::string sval = d->getNumeric(pname);
1275  if(sval.empty())
1276  sval = "0.0";
1277 
1278  double dval = boost::lexical_cast<double>(sval);
1279  recset->GetFields()->GetItem(pname.c_str())->Value = (_variant_t)(dval);
1280  }
1281  break;
1282 
1283  case te::dt::DATETIME_TYPE:
1284  {
1285  std::unique_ptr<te::dt::DateTime> dtm(d->getDateTime(i));
1286 
1287  std::string dtt = te::ado::GetFormattedDateTime(dtm.get());
1288 
1289  recset->GetFields()->GetItem(pname.c_str())->Value = (_bstr_t)dtt.c_str();
1290 
1291  break;
1292  }
1293 
1294  case te::dt::FLOAT_TYPE:
1295  recset->GetFields()->GetItem(pname.c_str())->Value = (_variant_t)d->getFloat(pname.c_str());
1296  break;
1297 
1298  case te::dt::DOUBLE_TYPE:
1299  recset->GetFields()->GetItem(pname.c_str())->Value = (_variant_t)d->getDouble(pname.c_str());
1300  break;
1301 
1302  case te::dt::STRING_TYPE:
1303  recset->GetFields()->GetItem(pname.c_str())->Value = (_bstr_t)d->getString(pname.c_str()).c_str();
1304  break;
1305 
1306  case te::dt::BOOLEAN_TYPE:
1307  recset->GetFields()->GetItem(pname.c_str())->Value = (_variant_t)d->getBool(pname.c_str());
1308  break;
1309 
1311  {
1312  /*char * data = ((te::dt::ByteArray*)props[i])->getData();
1313 
1314  _variant_t var;
1315  te::ado::Blob2Variant(data, ((te::dt::ByteArray*)props[i])->bytesUsed(), var);
1316 
1317  recset->Fields->GetItem(props[i]->getName().c_str())->AppendChunk (var);
1318 
1319  break;*/
1320  }
1321 
1322  //case te::dt::ARRAY_TYPE:
1323  case te::dt::GEOMETRY_TYPE:
1324  {
1325  std::unique_ptr<te::gm::Geometry> geometry(d->getGeometry(pname));
1326  const te::gm::Envelope* env = geometry->getMBR();
1327 
1328  recset->GetFields()->GetItem("lower_x")->Value = (_variant_t)env->m_llx;
1329  recset->GetFields()->GetItem("lower_y")->Value = (_variant_t)env->m_lly;
1330  recset->GetFields()->GetItem("upper_x")->Value = (_variant_t)env->m_urx;
1331  recset->GetFields()->GetItem("upper_y")->Value = (_variant_t)env->m_ury;
1332 
1333  _variant_t var;
1334  Convert2Ado(geometry.get(), var);
1335 
1336  recset->Fields->GetItem(pname.c_str())->AppendChunk (var);
1337 
1338  break;
1339  }
1340 
1341  default:
1342  throw te::common::Exception(TE_TR("The informed type could not be mapped to ADO type system!"));
1343  break;
1344  }
1345  }
1346 
1347  TESTHR(recset->Update());
1348 
1349  }
1350  }
1351  catch(_com_error& e)
1352  {
1353  throw te::common::Exception((LPCSTR)e.ErrorMessage());
1354  }
1355 }
1356 
1357 void te::ado::Transactor::remove(const std::string& datasetName, const te::da::ObjectIdSet*)
1358 {
1359  ADOX::_CatalogPtr pCatalog = 0;
1360 
1361  TESTHR(pCatalog.CreateInstance(__uuidof(ADOX::Catalog)));
1362 
1363  try
1364  {
1365  pCatalog->PutActiveConnection(variant_t((IDispatch *)m_conn->getConn()));
1366 
1367  TESTHR(pCatalog->Tables->Delete(datasetName.c_str()));
1368  }
1369  catch(_com_error& e)
1370  {
1371  throw te::common::Exception((LPCSTR)e.ErrorMessage());
1372  }
1373 }
1374 
1375 void te::ado::Transactor::update(const std::string&,
1376  te::da::DataSet*,
1377  const std::vector<std::size_t>&,
1378  const te::da::ObjectIdSet*,
1379  const std::map<std::string, std::string>&,
1380  std::size_t)
1381 {
1382  //TODO
1383 }
1384 
1385 void te::ado::Transactor::update(const std::string& datasetName,
1386  te::da::DataSet* dataset,
1387  const std::vector< std::set<int> >& properties,
1388  const std::vector<size_t>& ids)
1389 {
1390  dataset->moveFirst();
1391 
1392  int i=0;
1393  std::set<int> plst;
1394  std::set<int>::iterator it;
1395 
1396  try
1397  {
1398  begin();
1399 
1400  do
1401  {
1402  std::string sql = "UPDATE " + datasetName + " SET ";
1403  plst = properties[i];
1404  std::string pName;
1405  std::string id;
1406  int k = 0;
1407 
1408  for(it = plst.begin(); it != plst.end(); ++it)
1409  {
1410  if(k>0)
1411  pName += ",";
1412 
1413  pName += dataset->getPropertyName(*it);
1414 
1415  if(dataset->getPropertyDataType(*it) == te::dt::STRING_TYPE)
1416  pName += "=\"" + dataset->getAsString(*it) + "\"";
1417  else
1418  pName += "=" + dataset->getAsString(*it);
1419 
1420  k++;
1421  }
1422 
1423  for(size_t j=0; j<ids.size(); ++j)
1424  {
1425  if(j>0)
1426  id += " AND ";
1427 
1428  id += dataset->getPropertyName(j) += "=";
1429 
1430  if(dataset->getPropertyDataType(j) == te::dt::STRING_TYPE)
1431  id += "\"" + dataset->getAsString(j) + "\"";
1432  else
1433  id += dataset->getAsString(j);
1434  }
1435 
1436  sql += pName + " WHERE " + id;
1437 
1438  execute(sql);
1439 
1440  i++;
1441  } while (dataset->moveNext());
1442 
1443  commit();
1444  }
1445  catch(te::common::Exception& e)
1446  {
1447  rollBack();
1448 
1449  throw e;
1450  }
1451 }
1452 
1453 void te::ado::Transactor::optimize(const std::map<std::string, std::string>&)
1454 {
1455 
1456 }
1457 
1459 {
1460  _ConnectionPtr adoConn = m_conn->getConn();
1461 
1462  ADOX::_CatalogPtr pCatalog = 0;
1463 
1464  TESTHR(pCatalog.CreateInstance(__uuidof(ADOX::Catalog)));
1465 
1466  try
1467  {
1468  pCatalog->PutActiveConnection(variant_t((IDispatch *)adoConn));
1469  }
1470  catch(_com_error& e)
1471  {
1472  throw te::common::Exception((LPCSTR)e.ErrorMessage());
1473  }
1474  ADOX::TablesPtr tables = pCatalog->GetTables();
1475 
1476  ADOX::_TablePtr t = tables->GetItem(dt->getName().c_str());
1477 
1478  ADOX::KeysPtr keys = t->GetKeys();
1479 
1480  ADOX::_KeyPtr pk = 0;
1481 
1482  for(long i = 0; i < keys->Count; i++)
1483  {
1484  if(keys->GetItem(i)->GetType() == ADOX::adKeyPrimary)
1485  pk = keys->GetItem(i);
1486  }
1487 
1488  if(pk == 0)
1489  {
1490  dt->setPrimaryKey(0);
1491  return;
1492  }
1493 
1494  ADOX::ColumnsPtr cols = pk->GetColumns();
1495 
1496  te::da::PrimaryKey* tlPk = new te::da::PrimaryKey(std::string(pk->GetName()), dt);
1497 
1498  for(long i = 0; i < cols->GetCount(); i++)
1499  tlPk->add(dt->getProperty(std::string(cols->GetItem(i)->GetName())));
1500 }
1501 
1503 {
1504  std::string dsName = dt->getName();
1505  int numCols = 0;
1506 
1507  ADOX::DataTypeEnum colType;
1508  std::map<int, std::string> colNamesMap;
1509  std::map<int, ADOX::DataTypeEnum> colTypesMap;
1510  std::map<int, int> charLengthMap;
1511  std::map<int, bool> isRequiredMap;
1512  std::map<int, bool> hasDefaultMap;
1513  std::map<int, std::string> defaultValueMap;
1514 
1515  _ConnectionPtr conn = 0;
1516 
1517  try
1518  {
1519  HRESULT hr = S_OK;
1520 
1521  conn = m_conn->getConn();
1522 
1523  _RecordsetPtr rs = NULL;
1524 
1525  TESTHR(rs.CreateInstance(__uuidof(Recordset)));
1526 
1527  // Create a safearray which takes three elements,and pass it as
1528  // the second parameter in the OpenSchema method.
1529  SAFEARRAY FAR* psa = NULL;
1530  SAFEARRAYBOUND rgsabound;
1531  _variant_t var[3];
1532 
1533  _variant_t Array;
1534  rgsabound.lLbound = 0;
1535  rgsabound.cElements = 3;
1536  psa = SafeArrayCreate(VT_VARIANT, 1, &rgsabound);
1537 
1538  var[0].vt = VT_EMPTY;
1539  var[1].vt = VT_EMPTY;
1540  var[2] = dsName.c_str();
1541 
1542  // Fill the safe array.
1543  for(LONG i = 0; i < 3; ++i)
1544  hr = SafeArrayPutElement(psa, &i, &var[i]);
1545 
1546  Array.vt = VT_ARRAY | VT_VARIANT;
1547  Array.parray = psa;
1548 
1549  rs = conn->OpenSchema(adSchemaColumns, &Array, vtMissing);
1550 
1551  int pos;
1552  while (!(rs->EndOfFile))
1553  {
1554  // Get the column name
1555  _bstr_t columnName = rs->Fields->GetItem("COLUMN_NAME")->Value;
1556  pos = rs->Fields->GetItem("ORDINAL_POSITION")->Value;
1557  pos = pos - 1;
1558  colNamesMap[pos] = (LPCSTR)columnName;
1559 
1560  // Get the data type of the column
1561  colType = ADOX::DataTypeEnum(int(rs->Fields->GetItem("DATA_TYPE")->Value));
1562  colTypesMap[pos] = colType;
1563 
1564  // Get the length of the column
1565  _variant_t length = rs->Fields->GetItem("CHARACTER_MAXIMUM_LENGTH")->Value;
1566  int charLength = 0;
1567  if(length.vt != VT_NULL)
1568  charLength = (int)length.dblVal;
1569  charLengthMap[pos] = charLength;
1570 
1571  // Get the columns that accept null values
1572  bool nullVal = rs->Fields->GetItem("IS_NULLABLE")->Value;
1573  isRequiredMap[pos] = !nullVal;
1574 
1575  // Get the columns that has default values
1576  bool hasDefault = rs->Fields->GetItem("COLUMN_HASDEFAULT")->Value;
1577  hasDefaultMap[pos] = !hasDefault;
1578 
1579  // Get the default value
1580  std::string defaultStr;
1581  if(hasDefault)
1582  {
1583  _bstr_t defaultValue = rs->Fields->GetItem("COLUMN_DEFAULT")->Value;
1584  defaultStr = (LPSTR)defaultValue;
1585  }
1586 
1587  defaultValueMap[pos] = defaultStr;
1588 
1589  rs->MoveNext();
1590  ++numCols;
1591  }
1592  }
1593  catch (_com_error& e)
1594  {
1595  std::cout << "Error = " << (char*) e.ErrorMessage() << std::endl;
1596  }
1597 
1598  // Create the dataset properties
1599  for(int i = 0; i < numCols; ++i)
1600  {
1601  te::dt::Property* p = 0;
1602  ADOX::DataTypeEnum colType = colTypesMap[i];
1603  std::string colName = colNamesMap[i];
1604 
1605  switch(colType)
1606  {
1607  case ::adBoolean:
1608  {
1609  p = new te::dt::SimpleProperty(colName, Convert2Terralib(colType));
1611 
1612  sp->setRequired(isRequiredMap[i]);
1613  break;
1614  }
1615 
1616  case ::adVarWChar:
1617  case ::adWChar:
1618  case ::adVarChar:
1619  case ::adLongVarChar:
1620  case ::adLongVarWChar:
1621  case ::adBSTR:
1622  case ::adChar:
1623  {
1624  p = new te::dt::StringProperty(colName, (te::dt::StringType)Convert2Terralib(colType), charLengthMap[i]);
1626 
1627  sp->setRequired(isRequiredMap[i]);
1628  sp->setSize(charLengthMap[i]);
1629 
1630  break;
1631  }
1632 
1633  case ADOX::adTinyInt:
1634  case ADOX::adSmallInt:
1635  case ADOX::adInteger:
1636  case ADOX::adBigInt:
1637  case ADOX::adSingle:
1638  case ADOX::adDouble:
1639  case ADOX::adDecimal:
1640  case ::adUnsignedBigInt:
1641  case ::adUnsignedInt:
1642  case ::adUnsignedSmallInt:
1643  case ::adUnsignedTinyInt:
1644  {
1645  p = new te::dt::SimpleProperty(colName, Convert2Terralib(colType));
1647 
1648  sp->setRequired(isRequiredMap[i]);
1649  break;
1650  }
1651 
1652  case ADOX::adBinary:
1653  case ADOX::adLongVarBinary:
1654  {
1655 
1656  std::map<std::string, std::string> geomColumns = m_ds->getGeomColumns();
1657  std::map<std::string, std::string>::iterator it = geomColumns.find(dsName);
1658 
1659  if(it != geomColumns.end())
1660  {
1661  if(it->second == colName)
1662  {
1663  p = new te::gm::GeometryProperty(colName, te::ado::GetSRID(conn, dsName, colName), te::ado::GetType(conn, dsName, colName));
1664  }
1665  }
1666  else
1667  {
1668  p = new te::dt::SimpleProperty(colName, Convert2Terralib(colType));
1669  }
1670 
1671  break;
1672  }
1673 
1674  case ADOX::adDate:
1675  case ADOX::adDBDate:
1676  case ADOX::adDBTime:
1677  case ADOX::adDBTimeStamp:
1679  break;
1680 
1681  default:
1682  p = new te::dt::SimpleProperty(colName, te::dt::UNKNOWN_TYPE);
1683  break;
1684  }
1685 
1686  dt->add(p);
1687  }
1688 }
1689 
1691 {
1692  _ConnectionPtr adoConn = m_conn->getConn();
1693 
1694  ADOX::_CatalogPtr pCatalog = 0;
1695 
1696  TESTHR(pCatalog.CreateInstance(__uuidof(ADOX::Catalog)));
1697 
1698  try
1699  {
1700  pCatalog->PutActiveConnection(variant_t((IDispatch *)adoConn));
1701  }
1702  catch(_com_error& e)
1703  {
1704  throw te::common::Exception((LPCSTR)e.ErrorMessage());
1705  }
1706 
1707  ADOX::TablesPtr tables = pCatalog->GetTables();
1708 
1709  ADOX::_TablePtr t = tables->GetItem(dt->getName().c_str());
1710 
1711  ADOX::KeysPtr keys = t->GetKeys();
1712 
1713  for(long i = 0; i < keys->Count; i++)
1714  {
1715  if(keys->GetItem(i)->GetType() == ADOX::adKeyUnique)
1716  {
1717  ADOX::_KeyPtr uk = keys->GetItem(i);
1718 
1719  te::da::UniqueKey* tlUk = new te::da::UniqueKey(std::string(uk->GetName()), dt);
1720 
1721  ADOX::ColumnsPtr cols = uk->GetColumns();
1722 
1723  for(long j = 0; j < cols->Count; j++)
1724  tlUk->add(dt->getProperty(std::string(cols->GetItem(i)->GetName())));
1725  }
1726  }
1727 }
1728 
1730 {
1731  _ConnectionPtr adoConn = m_conn->getConn();
1732 
1733  ADOX::_CatalogPtr pCatalog = 0;
1734 
1735  TESTHR((pCatalog.CreateInstance(__uuidof(ADOX::Catalog))));
1736 
1737  try
1738  {
1739  pCatalog->PutActiveConnection(variant_t((IDispatch *)adoConn));
1740  }
1741  catch(_com_error& e)
1742  {
1743  throw te::common::Exception((LPCSTR)e.ErrorMessage());
1744  }
1745 
1746  ADOX::TablesPtr tables = pCatalog->GetTables();
1747 
1748  ADOX::_TablePtr t = tables->GetItem(dt->getName().c_str());
1749 
1750  ADOX::IndexesPtr idxs = t->GetIndexes();
1751 
1752  for(long i = 0; i < idxs->GetCount(); i++)
1753  {
1754  ADOX::_IndexPtr idx = idxs->GetItem(i);
1755 
1756  te::da::Index* tlIdx = new te::da::Index();
1757  tlIdx->setName(std::string(idx->GetName()));
1758 
1759  std::vector<te::dt::Property*> props;
1760 
1761  ADOX::ColumnsPtr cols = idx->GetColumns();
1762  for(long i = 0; i < cols->GetCount(); i++)
1763  props.push_back(dt->getProperty(std::string(cols->GetItem(i)->GetName())));
1764 
1765  tlIdx->setProperties(props);
1766 
1767  dt->add(tlIdx);
1768  }
1769 }
1770 
1772 {
1773  _RecordsetPtr rs = NULL;
1774 
1775  std::string dtName = dt->getName();
1776 
1777  std::string str = "[" + dtName + "]";
1778 
1779  try
1780  {
1781  _ConnectionPtr adoConn = m_conn->getConn();
1782 
1783  TESTHR(rs.CreateInstance(__uuidof(Recordset)));
1784 
1785  rs = adoConn->OpenSchema(adSchemaCheckConstraints);
1786 
1787  while (!(rs->EndOfFile))
1788  {
1789  std::string constraintName = (LPCSTR)(bstr_t)(rs->Fields->GetItem("CONSTRAINT_NAME")->GetValue());
1790 
1791  if(constraintName.find(str) != std::string::npos)
1792  {
1793  te::da::CheckConstraint* cc = new te::da::CheckConstraint(constraintName, dt);
1794  std::string checkClause = (LPCSTR)(bstr_t)(rs->Fields->GetItem("CHECK_CLAUSE")->GetValue());
1795  cc->setExpression(checkClause);
1796  }
1797 
1798  rs->MoveNext();
1799  }
1800  }
1801  catch(_com_error& e)
1802  {
1803  throw te::common::Exception((LPCSTR)e.ErrorMessage());
1804  }
1805 
1806  // Clean up objects before exit.
1807  if(rs && rs->State == adStateOpen)
1808  rs->Close();
1809 }
1810 
1811 void te::ado::Transactor::insertIntoGeometryColumns(const std::string& datasetName,
1812  te::gm::GeometryProperty* geomProp)
1813 {
1814  _ConnectionPtr adoConn = m_conn->getConn();
1815 
1816  int coord_dimension = 2;
1817 
1818  if(te::ado::IsZProperty(geomProp->getGeometryType()))
1819  coord_dimension = 3;
1820 
1821  _RecordsetPtr recset;
1822  TESTHR(recset.CreateInstance(__uuidof(Recordset)));
1823 
1824  try
1825  {
1826  TESTHR(recset->Open(_bstr_t("geometry_columns"),
1827  _variant_t((IDispatch*)adoConn,true), adOpenKeyset, adLockOptimistic, adCmdTable));
1828 
1829  TESTHR(recset->AddNew());
1830 
1831  recset->GetFields()->GetItem("f_table_catalog")->Value = (_bstr_t)std::string("''").c_str();
1832  recset->GetFields()->GetItem("f_table_schema")->Value = (_bstr_t)std::string("public").c_str();
1833  recset->GetFields()->GetItem("f_table_name")->Value = (_bstr_t)datasetName.c_str();
1834  recset->GetFields()->GetItem("f_geometry_column")->Value = (_bstr_t)geomProp->getName().c_str();
1835  recset->GetFields()->GetItem("coord_dimension")->Value = (_variant_t)coord_dimension;
1836  recset->GetFields()->GetItem("srid")->Value = (_variant_t)geomProp->getSRID();
1837  recset->GetFields()->GetItem("type")->Value = (_bstr_t)te::ado::GetGeometryName(geomProp->getGeometryType()).c_str();
1838 
1839  recset->Update();
1840  }
1841  catch(_com_error& e)
1842  {
1843  throw te::common::Exception((LPCSTR)e.ErrorMessage());
1844  }
1845 
1846  m_ds->registerGeometryColumn(datasetName, geomProp->getName());
1847 }
virtual std::unique_ptr< te::gm::Geometry > getGeometry(std::size_t i) const =0
Method for retrieving a geometric attribute value.
Property * getProperty(std::size_t i) const
It returns the i-th property.
virtual unsigned char getUChar(std::size_t i) const =0
Method for retrieving an unsigned character attribute value (1 byte long).
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.
Geometric property.
std::vector< std::string > getForeignKeyNames(const std::string &datasetName)
It gets the foreign key names of the given dataset.
_ConnectionPtr getConn() const
It gets the ADO Connection object.
void add(te::dt::Property *p)
It adds a property to the list of properties of the primary key.
Definition: PrimaryKey.h:123
virtual double getDouble(std::size_t i) const =0
Method for retrieving a double attribute value.
std::unique_ptr< te::da::PrimaryKey > getPrimaryKey(const std::string &datasetName)
It retrieves the primary key of the dataset.
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.
std::vector< std::string > getCheckConstraintNames(const std::string &datasetName)
It gets the check constraint names of the given dataset.
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.
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.
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.
const std::string & GetGeometryName(te::gm::GeomType t)
It returns the geometry OGC names.
te::gm::GeomType GetType(_ConnectionPtr adoConn, std::string tableName, std::string geomPropName)
Read the geometry_columns table end return a geometry type.
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.
A class that models the description of a dataset.
Definition: DataSetType.h:72
int Convert2Terralib(ADOX::DataTypeEnum adoType)
Bind ADOX Type to TerraLib Type.
virtual std::string getNumeric(std::size_t i) const =0
Method for retrieving a numeric attribute value.
bool isInTransaction() const
It returns true if a transaction is in progress, otherwise, it returns false.
Utility functions for ADO.
boost::int64_t getLastGeneratedId()
It returns the last id generated by an insertion command.
virtual float getFloat(std::size_t i) const =0
Method for retrieving a float attribute value.
void addForeignKey(const std::string &datasetName, te::da::ForeignKey *fk)
It adds a foreign key constraint to a dataset.
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.
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.
std::unique_ptr< te::da::Sequence > getSequence(const std::string &name)
It gets the sequence with the given name in the data source.
double m_urx
Upper right corner x-coordinate.
An static class with global definitions.
PrimaryKey * getPrimaryKey() const
It returns the primary key associated to the dataset type.
Definition: DataSetType.h:214
std::unique_ptr< te::dt::Property > getProperty(const std::string &datasetName, const std::string &name)
It retrieves the property with the given name from the dataset.
SpatialRelation
Spatial relations between geometric objects.
bool hasDataSets()
It checks if the data source has any dataset.
It describes a sequence (a number generator).
virtual ReturnType accept(VisitorType &guest) const =0
It call the visit method from the guest object.
static te::dt::Date ds(2010, 01, 01)
std::unique_ptr< te::da::DataSetType > getDataSetType(const std::string &name)
It gets information about the given dataset.
_RecordsetPtr query(const std::string &query, bool connected=false)
It queries the database.
A class that describes a check constraint.
An abstract class for data providers like a DBMS, Web Services or a regular file. ...
void addProperty(const std::string &datasetName, te::dt::Property *p)
It adds a new property to the dataset schema.
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:242
std::string GetFormattedDateTime(te::dt::DateTime *dateTime)
It gets a formatted DateTime string for ADO.
A class that implements a connection to a ADO database.
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
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.
std::unique_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.
std::vector< std::string > getUniqueKeyNames(const std::string &datasetName)
It gets the unique key names of the given dataset.
virtual bool moveNext()=0
It moves the internal pointer to the next item of the collection.
std::unique_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.
AccessPolicy
Supported data access policies (can be used as bitfield).
TraverseType
A dataset can be traversed in two ways:
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...
std::string GetAdoStringType(const int &terralib)
Bind TerraLib type to an ADO valid fiel type name.
double m_llx
Lower left corner x-coordinate.
bool primaryKeyExists(const std::string &datasetName, const std::string &name)
It checks if a primary key exists in the dataset.
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.
virtual boost::int16_t getInt16(std::size_t i) const =0
Method for retrieving a 16-bit integer attribute value (2 bytes long).
void begin()
It starts a new transaction.
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)
virtual int getPropertyDataType(std::size_t i) const =0
It returns the underlying data type of the property at position pos.
An Envelope defines a 2D rectangular region.
void addCheckConstraint(const std::string &datasetName, te::da::CheckConstraint *cc)
It adds a check constraint to the dataset.
This class represents a set of unique ids created in the same context. i.e. from the same data set...
Definition: ObjectIdSet.h:55
void dropProperty(const std::string &datasetName, const std::string &name)
It removes a property from the given dataset.
std::unique_ptr< te::da::Index > getIndex(const std::string &datasetName, const std::string &name)
It gets the index with the given name from the dataset.
void insertIntoGeometryColumns(const std::string &datasetName, te::gm::GeometryProperty *geomProp)
It insert a geometry property in the geometry_clumns (SFS Schema).
std::unique_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.
std::string escape(const std::string &value)
It escapes a string for using in commands and queries.
virtual boost::int32_t getInt32(std::size_t i) const =0
Method for retrieving a 32-bit integer attribute value (4 bytes long).
const te::da::SQLDialect * getDialect() const
It returns the data source SQL dialect, if there is one.
void DataSet()
DataSource * m_ds
The ADO data source associated to this transactor.
static te::dt::DateTime d(2010, 8, 9, 15, 58, 39)
GeomType getGeometryType() const
It returns the geometry subtype allowed for the property.
static te::dt::TimeDuration dt(20, 30, 50, 11)
std::vector< std::string > getDataSetNames()
It It gets the dataset names available in the data source.
void addUniqueKey(const std::string &datasetName, te::da::UniqueKey *uk)
It adds a unique key constraint to the dataset.
void addPrimaryKey(const std::string &datasetName, te::da::PrimaryKey *pk)
It adds a primary key constraint to the dataset schema.
void registerGeometryColumn(const std::string &datasetName, const std::string &geomColName)
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.
std::unique_ptr< te::da::ForeignKey > getForeignKey(const std::string &datasetName, const std::string &name)
It retrieves the foreign key from the given dataset.
te::gm::Polygon * p
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
A visitor for building an SQL statement using ADO dialect.
virtual std::string getAsString(std::size_t i, int precision=0) const
Method for retrieving a data value as a string plain representation.
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
const std::map< std::string, std::string > & getGeomColumns() const
void setProperties(const std::vector< te::dt::Property * > &properties)
It sets the properties that take part of the index.
Property * getParent() const
It returns the parent of this property, or NULL, if it doesn&#39;t have one.
Definition: Property.h:168
std::size_t size() const
It returns the number of properties of the CompositeProperty.
Implementation of the data source class for the ADO driver.
void rollBack()
It aborts the transaction. Any changes will be rolled-back.
A visitor for building an SQL statement using ADO dialect.
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
std::unique_ptr< te::da::BatchExecutor > getBatchExecutor()
It creates a batch command executor.
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
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.
A Select models a query to be used when retrieving data from a DataSource.
Definition: Select.h:65
void getCheckConstraints(te::da::DataSetType *dt)
It update the DataSetType about the Check Constraints.
Connection * m_conn
The connection used by this transactor.
double m_lly
Lower left corner y-coordinate.
std::unique_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.
void commit()
It commits the transaction.
void dropPrimaryKey(const std::string &datasetName)
It removes the primary key constraint from the dataset schema.
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.
virtual std::unique_ptr< te::dt::DateTime > getDateTime(std::size_t i) const =0
Method for retrieving a date and time attribute value.
A dataset is the unit of information manipulated by the data access module of TerraLib.
virtual char getChar(std::size_t i) const =0
Method for retrieving a signed character attribute value (1 byte long).
bool m_isInTransaction
Tells if there is a transaction in progress.
const std::vector< te::dt::Property * > & getProperties() const
It returns the properties that form the unique key.
Definition: UniqueKey.h:110
std::string MakeConnectionStr(const te::core::URI &connInfo)
Create a connection string based on a map.
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.
double m_ury
Upper right corner y-coordinate.
const std::vector< te::dt::Property * > & getProperties() const
It returns the properties that take part of the index.
It describes a primary key (pk) constraint.
Definition: PrimaryKey.h:52
const te::core::URI & getConnectionInfo() const
An Uniform Resource Identifier used to describe the datasource connection.
void dropForeignKey(const std::string &datasetName, const std::string &fkName)
It removes the foreign key constraint from the dataset schema.
void setExpression(const std::string &e)
It sets the check constraint expression.
ADOX::DataTypeEnum Convert2Ado(int terralib)
Bind TerraLib Type to ADO Type.
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.
void add(const std::string &datasetName, te::da::DataSet *d, const std::map< std::string, std::string > &options, std::size_t limit=0, bool enableProgress=true)
It adds data items to the dataset in the data source.
void execute(const std::string &command)
It executes the given SQL command and throws away the result.
virtual bool getBool(std::size_t i) const =0
Method for retrieving a boolean attribute value.
A class that implements a connection to a ADO database.
virtual bool isNull(std::size_t i) const =0
It checks if the attribute value is NULL.
bool IsZProperty(te::gm::GeomType type)
Verifies whether Z property.
void TESTHR(HRESULT hr)
virtual boost::int64_t getInt64(std::size_t i) const =0
Method for retrieving a 64-bit integer attribute value (8 bytes long).
void setName(const std::string &name)
It sets the index name.
te::da::DataSource * getDataSource() const
It returns the parent data source of the transactor.
DataSourceTransactor class implementation for Microsoft Access driver.
virtual std::size_t getNumProperties() const =0
It returns the number of properties that composes an item of the dataset.
virtual std::string getPropertyName(std::size_t i) const =0
It returns the property name at position pos.
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.
std::size_t getNumberOfProperties(const std::string &datasetName)
It gets the number of properties of the given dataset.
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.
virtual bool moveFirst()=0
It moves the internal pointer to the first item in the collection.
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.
void execute(const te::da::Query &command)
It executes the specified command using a generic query representation.
void cancel()
It requests that the data source stop the processing of the current command.
A Query is independent from the data source language/dialect.
Definition: Query.h:46
unsigned int col
It describes an index associated to a DataSetType.
virtual std::string getString(std::size_t i) const =0
Method for retrieving a string value attribute.
void setPrimaryKey(PrimaryKey *pk)
It sets the primary key constraint.
std::unique_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.
bool uniqueKeyExists(const std::string &datasetName, const std::string &name)
It checks if a unique key with the given name exists in the dataset.
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 isEnvelopeProperty(const std::string &name)
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.
std::unique_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...