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