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