All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Utils.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/Utils.cpp
22 
23  \brief Utility functions for ADO.
24 */
25 
26 // TerraLib
27 #include "../common/Translator.h"
28 #include "../datatype.h"
29 #include "../dataaccess/dataset/DataSetType.h"
30 #include "../dataaccess/dataset/ForeignKey.h"
31 #include "../dataaccess/dataset/CheckConstraint.h"
32 #include "../dataaccess/dataset/Constraint.h"
33 #include "../dataaccess/dataset/PrimaryKey.h"
34 #include "../dataaccess/dataset/UniqueKey.h"
35 #include "../dataaccess/utils/Utils.h"
36 #include "../geometry/GeometryProperty.h"
37 #include "../geometry/Envelope.h"
38 #include "../geometry/Enums.h"
39 #include "../memory/DataSet.h"
40 #include "../memory/DataSetItem.h"
41 #include "DataSet.h"
42 #include "Config.h"
43 #include "Utils.h"
44 #include "Exception.h"
45 #include "Globals.h"
46 
47 // Boost
48 #include <boost/format.hpp>
49 #include <boost/lexical_cast.hpp>
50 
51 inline void TESTHR( HRESULT hr )
52 {
53  if( FAILED(hr) ) _com_issue_error( hr );
54 }
55 
56 void te::ado::Blob2Variant(const char* blob, int size, _variant_t & var)
57 {
58  try
59  {
60  char *pByte;
61 
62  SAFEARRAY FAR* psa;
63  SAFEARRAYBOUND rgsabound[1];
64  rgsabound[0].lLbound = 0;
65  rgsabound[0].cElements = size;
66 
67  psa = SafeArrayCreate(VT_I1, 1, rgsabound);
68 
69  if(SafeArrayAccessData(psa,(void **)&pByte) == NOERROR)
70  memcpy(pByte, blob, size);
71  SafeArrayUnaccessData(psa);
72 
73  var.vt = VT_ARRAY | VT_UI1;
74  var.parray = psa;
75  }
76  catch(_com_error& e)
77  {
78  throw Exception(TR_ADO(e.Description()));
79  }
80 }
81 
82 std::string te::ado::MakeConnectionStr(const std::map<std::string, std::string>& dsInfo)
83 {
84  std::map<std::string, std::string>::const_iterator it = dsInfo.find("PROVIDER");
85  std::map<std::string, std::string>::const_iterator it_end = dsInfo.end();
86  std::string connInfo;
87 
88  if(it != it_end)
89  connInfo += "Provider=" + it->second;
90 
91  it = dsInfo.find("DB_NAME");
92 
93  if(it != it_end)
94  connInfo += ";Data Source=" + it->second;
95 
96  it = dsInfo.find("USER_NAME");
97 
98  if(it != it_end)
99  connInfo += ";User Id=" + it->second;
100 
101  it = dsInfo.find("PASSWORD");
102 
103  if(it != it_end)
104  connInfo += ";Jet OLEDB:Database Password=" + it->second + ";";
105 
106  return connInfo;
107 }
108 
109 void te::ado::Variant2Blob(const _variant_t var, int size, char* & blob)
110 {
111  try
112  {
113  char *cdata = 0;
114  if(var.vt == (VT_ARRAY | VT_UI1))
115  {
116  SafeArrayAccessData(var.parray,(void **)&cdata);
117  blob = new char[size];
118  memcpy(blob, cdata, size);
119  SafeArrayUnaccessData(var.parray);
120  }
121  }
122  catch(_com_error& e)
123  {
124  throw Exception(TR_ADO(e.Description()));
125  }
126 }
127 
128 ADOX::DataTypeEnum te::ado::Convert2Ado(int terralib)
129 {
130  switch(terralib)
131  {
132  case te::dt::CHAR_TYPE:
133  case te::dt::UCHAR_TYPE:
134  return ADOX::adWChar;
135  break;
136 
137  case te::dt::INT16_TYPE:
138  return ADOX::adInteger;
139  break;
140 
141  case te::dt::INT32_TYPE:
142  return ADOX::adInteger;
143  break;
144 
145  case te::dt::INT64_TYPE:
146  return ADOX::adInteger;
147  break;
148 
150  return ADOX::adDate;
151 
152  case te::dt::FLOAT_TYPE:
153  case te::dt::DOUBLE_TYPE:
155  return ADOX::adDouble;
156  break;
157 
158  case te::dt::STRING_TYPE:
159  return ADOX::adLongVarWChar;
160  break;
161 
163  return ADOX::adBoolean;
164  break;
165 
167  return ADOX::adLongVarBinary;
168  break;
169 
171  case te::dt::ARRAY_TYPE:
172  return ADOX::adLongVarBinary;
173  break;
174 
175  default:
176  throw te::ado::Exception(TR_ADO("The informed type could not be mapped to ADO type system!"));
177  break;
178  }
179 }
180 
181 void te::ado::Convert2Ado(const te::gm::Geometry* geo, _variant_t & var)
182 {
183  long size = geo->getWkbSize();
184 
185  char* wkb = new char[size];
186 
187  geo->getWkb(wkb, te::common::NDR);
188 
189  unsigned int newWkbSize = size+4;
190 
191  char* newWkb = new char[newWkbSize];
192 
193  memcpy(newWkb, wkb, size);
194 
195  unsigned int srid = geo->getSRID();
196 
197  memcpy(newWkb+size, &srid, 4);
198 
199  te::ado::Blob2Variant(newWkb, newWkbSize, var);
200 }
201 
202 int te::ado::Convert2Terralib(ADOX::DataTypeEnum adoType)
203 {
204  switch(adoType)
205  {
206  case ADOX::adBoolean:
207  return te::dt::BOOLEAN_TYPE;
208  break;
209 
210  case ADOX::adEmpty:
211  return te::dt::VOID_TYPE;
212  break;
213 
214  case ADOX::adBinary:
215  case ADOX::adVarBinary:
216  case ADOX::adLongVarBinary:
218  break;
219 
220  case ADOX::adVarWChar:
221  case ADOX::adWChar:
222  case ADOX::adVarChar:
223  case ADOX::adLongVarChar:
224  case ADOX::adLongVarWChar:
225  case ADOX::adBSTR:
226  case ADOX::adChar:
227  return te::dt::STRING_TYPE;
228  break;
229 
230  case ADOX::adBigInt:
231  return te::dt::INT16_TYPE;
232  break;
233 
234  case ADOX::adSingle:
235  return te::dt::FLOAT_TYPE;
236  break;
237 
238  case ADOX::adDouble:
239  case ADOX::adDecimal:
240  return te::dt::DOUBLE_TYPE;
241  break;
242 
243  case ADOX::adInteger:
244  return te::dt::INT32_TYPE;
245  break;
246 
247  case ADOX::adTinyInt:
248  case ADOX::adSmallInt:
249  return te::dt::INT16_TYPE;
250  break;
251 
252  case ADOX::adUnsignedBigInt:
253  return te::dt::UINT64_TYPE;
254  break;
255 
256  case ADOX::adUnsignedInt:
257  return te::dt::UINT32_TYPE;
258  break;
259 
260  case ADOX::adUnsignedSmallInt:
261  case ADOX::adUnsignedTinyInt:
262  return te::dt::UINT16_TYPE;
263  break;
264 
265  case ADOX::adDate:
266  case ADOX::adDBDate:
267  case ADOX::adDBTime:
268  case ADOX::adDBTimeStamp:
269  return te::dt::DATETIME_TYPE;
270  break;
271 
272  //case ADOX::adGUID:
273  //case ADOX::adError:
274  //case ADOX::adSingle:
275  //case ADOX::adDecimal:
276  //case ADOX::adNumeric:
277  //case ADOX::adChapter:
278  //case ADOX::adVarNumeric:
279  //case ADOX::adCurrency:
280  //case ADOX::adFileTime:
281  //case ADOX::adPropVariant:
282  //case ADOX::adUserDefined:
283 
284  default:
285  return te::dt::UNKNOWN_TYPE;
286  break;
287  }
288 }
289 
290 int te::ado::Convert2Terralib(::DataTypeEnum adoType)
291 {
292  switch(adoType)
293  {
294  case ::adBoolean:
295  return te::dt::BOOLEAN_TYPE;
296  break;
297 
298  case ::adEmpty:
299  return te::dt::VOID_TYPE;
300  break;
301 
302  case ::adBinary:
303  case ::adVarBinary:
304  case ::adLongVarBinary:
306  break;
307 
308  case ::adVarWChar:
309  case ::adWChar:
310  case ::adVarChar:
311  case ::adLongVarChar:
312  case ::adLongVarWChar:
313  case ::adBSTR:
314  case ::adChar:
315  return te::dt::STRING_TYPE;
316  break;
317 
318  case ::adBigInt:
319  return te::dt::INT16_TYPE;
320  break;
321 
322  case ::adSingle:
323  return te::dt::FLOAT_TYPE;
324  break;
325 
326  case ::adDouble:
327  case ::adDecimal:
328  return te::dt::DOUBLE_TYPE;
329  break;
330 
331  case ::adInteger:
332  return te::dt::INT32_TYPE;
333  break;
334 
335  case ::adTinyInt:
336  case ::adSmallInt:
337  return te::dt::INT16_TYPE;
338  break;
339 
340  case ::adUnsignedBigInt:
341  return te::dt::UINT64_TYPE;
342  break;
343 
344  case ::adUnsignedInt:
345  return te::dt::UINT32_TYPE;
346  break;
347 
348  case ::adUnsignedSmallInt:
349  case ::adUnsignedTinyInt:
350  return te::dt::UINT16_TYPE;
351  break;
352 
353  case ::adDate:
354  case ::adDBDate:
355  case ::adDBTime:
356  case ::adDBTimeStamp:
357  return te::dt::DATETIME_TYPE;
358  break;
359 
360  //case ::adGUID:
361  //case ::adError:
362  //case ::adSingle:
363  //case ::adDecimal:
364  //case ::adNumeric:
365  //case ::adChapter:
366  //case ::adVarNumeric:
367  //case ::adCurrency:
368  //case ::adFileTime:
369  //case ::adPropVariant:
370  //case ::adUserDefined:
371 
372  default:
373  return te::dt::UNKNOWN_TYPE;
374  break;
375  }
376 }
377 
379 {
380  te::dt::Property* prop = 0;
381 
382  _bstr_t cName = column->GetName();
383 
384  ADOX::DataTypeEnum cType = column->GetType();
385 
386  long cSize = column->GetDefinedSize();
387 
388  switch(cType)
389  {
390  case ::adBoolean:
391  prop = new te::dt::SimpleProperty(std::string(cName), Convert2Terralib(cType));
392  break;
393 
394  case ::adVarWChar:
395  case ::adWChar:
396  case ::adVarChar:
397  case ::adLongVarChar:
398  case ::adLongVarWChar:
399  case ::adBSTR:
400  case ::adChar:
401  prop = new te::dt::StringProperty(std::string(cName), (te::dt::StringType)Convert2Terralib(cType), cSize);
402  break;
403 
404  case ADOX::adTinyInt:
405  case ADOX::adSmallInt:
406  prop = new te::dt::SimpleProperty(std::string(cName), Convert2Terralib(cType));
407  break;
408 
409  case ADOX::adInteger:
410  prop = new te::dt::SimpleProperty(std::string(cName), Convert2Terralib(cType));
411  break;
412 
413  case ADOX::adBigInt:
414  prop = new te::dt::SimpleProperty(std::string(cName), Convert2Terralib(cType));
415  break;
416 
417  case ADOX::adDouble:
418  case ADOX::adDecimal:
419  prop = new te::dt::SimpleProperty(std::string(cName), Convert2Terralib(cType));
420  break;
421 
422  case ::adUnsignedBigInt:
423  prop = new te::dt::SimpleProperty(std::string(cName), Convert2Terralib(cType));
424  break;
425 
426  case ::adUnsignedInt:
427  prop = new te::dt::SimpleProperty(std::string(cName), Convert2Terralib(cType));
428  break;
429 
430  case ::adUnsignedSmallInt:
431  case ::adUnsignedTinyInt:
432  prop = new te::dt::SimpleProperty(std::string(cName),Convert2Terralib(cType));
433  break;
434 
435  case ADOX::adLongVarBinary:
436  prop = new te::dt::ArrayProperty(std::string(cName), new te::dt::SimpleProperty(std::string(cName), Convert2Terralib(cType)));
437  break;
438 
439  case ADOX::adDate:
440  case ADOX::adDBDate:
441  prop = new te::dt::DateTimeProperty(std::string(cName), te::dt::DATE);
442  break;
443 
444  case ADOX::adDBTime:
445  prop = new te::dt::DateTimeProperty(std::string(cName), te::dt::TIME_DURATION);
446  break;
447 
448  case ADOX::adDBTimeStamp:
449  prop = new te::dt::DateTimeProperty(std::string(cName), te::dt::TIME_INSTANT);
450  break;
451 
452  default:
453  throw te::ado::Exception(TR_ADO("The informed column could not be mapped to TerraLib Data Set Type!"));
454  }
455 
456  return prop;
457 }
458 
459 std::vector<te::dt::Property*> te::ado::Convert2Terralib(ADOX::ColumnsPtr columns)
460 {
461  std::vector<te::dt::Property*> properties;
462 
463  for(long i = 0; i < columns->GetCount(); i++)
464  properties.push_back(Convert2Terralib(columns->GetItem(i)));
465 
466  return properties;
467 }
468 
470 {
471 
472  if(key->GetType() == ADOX::adKeyPrimary)
473  {
474  te::da::PrimaryKey* con = new te::da::PrimaryKey(std::string(key->GetName()));
475  con->setProperties(Convert2Terralib(key->GetColumns()));
476 
477  return con;
478  }
479  else if(key->GetType() == ADOX::adKeyForeign)
480  {
481  te::da::ForeignKey* con = new te::da::ForeignKey(std::string(key->GetName()));
482  con->setProperties(Convert2Terralib(key->GetColumns()));
483 
484  return con;
485  }
486  else if(key->GetType() == ADOX::adKeyUnique)
487  {
488  te::da::UniqueKey* con = new te::da::UniqueKey(std::string(key->GetName()));
489  con->setProperties(Convert2Terralib(key->GetColumns()));
490 
491  return con;
492  }
493  else
494  throw te::ado::Exception(TR_ADO("Unknown type!"));
495 
496 }
497 
499 {
500  switch(t)
501  {
503  return Globals::sm_geometryTypeName;
504  break;
505 
506  case te::gm::PointType:
507  return Globals::sm_pointTypeName;
508  break;
509 
511  return Globals::sm_lineStringTypeName;
512  break;
513 
514  case te::gm::PolygonType:
515  return Globals::sm_polygonTypeName;
516  break;
517 
519  return Globals::sm_multiPointTypeName;
520  break;
521 
523  return Globals::sm_multiLineStringTypeName;
524  break;
525 
527  return Globals::sm_multiPolygonTypeName;
528  break;
529 
531  return Globals::sm_curvePolygonTypeName;
532  break;
533 
535  return Globals::sm_polyhedralSurfaceTypeName;
536  break;
537 
539  return Globals::sm_geometryZTypeName;
540  break;
541 
542  case te::gm::PointZType:
543  return Globals::sm_pointZTypeName;
544  break;
545 
547  return Globals::sm_lineStringZTypeName;
548  break;
549 
551  return Globals::sm_polygonZTypeName;
552  break;
553 
555  return Globals::sm_multiPointZTypeName;
556  break;
557 
559  return Globals::sm_multiLineStringZTypeName;
560  break;
561 
563  return Globals::sm_multiPolygonZTypeName;
564  break;
565 
567  return Globals::sm_curvePolygonZTypeName;
568  break;
569 
571  return Globals::sm_polyhedralSurfaceZTypeName;
572  break;
573 
575  return Globals::sm_geometryMTypeName;
576  break;
577 
578  case te::gm::PointMType:
579  return Globals::sm_pointMTypeName;
580  break;
581 
583  return Globals::sm_lineStringMTypeName;
584  break;
585 
587  return Globals::sm_polygonMTypeName;
588  break;
589 
591  return Globals::sm_multiPointMTypeName;
592  break;
593 
595  return Globals::sm_multiLineStringMTypeName;
596  break;
597 
599  return Globals::sm_multiPolygonMTypeName;
600  break;
601 
603  return Globals::sm_curvePolygonMTypeName;
604  break;
605 
607  return Globals::sm_polyhedralSurfaceMTypeName;
608  break;
609 
611  return Globals::sm_geometryZMTypeName;
612  break;
613 
614  case te::gm::PointZMType:
615  return Globals::sm_pointZMTypeName;
616  break;
617 
619  return Globals::sm_lineStringZMTypeName;
620  break;
621 
623  return Globals::sm_polygonZMTypeName;
624  break;
625 
627  return Globals::sm_multiPointZMTypeName;
628  break;
629 
631  return Globals::sm_multiLineStringZMTypeName;
632  break;
633 
635  return Globals::sm_multiPolygonZMTypeName;
636  break;
637 
639  return Globals::sm_curvePolygonZMTypeName;
640  break;
641 
643  return Globals::sm_polyhedralSurfaceZMTypeName;
644  break;
645 
646  default:
647  return Globals::sm_geometryTypeName;
648  }
649 }
650 
652 {
653 
654  if(t == Globals::sm_geometryTypeName)
655  return te::gm::GeometryType;
656 
657  else if(t == Globals::sm_pointTypeName)
658  return te::gm::PointType;
659 
660 
661  else if(t == Globals::sm_lineStringTypeName)
662  return te::gm::LineStringType;
663 
664 
665  else if(t == Globals::sm_polygonTypeName)
666  return te::gm::PolygonType;
667 
668 
669  else if(t == Globals::sm_multiPointTypeName)
670  return te::gm::MultiPointType;
671 
672 
673  else if(t == Globals::sm_multiLineStringTypeName)
675 
676 
677  else if(t == Globals::sm_multiPolygonTypeName)
679 
680 
681  else if(t == Globals::sm_curvePolygonTypeName)
683 
684 
685  else if(t == Globals::sm_polyhedralSurfaceTypeName)
687 
688 
689  else if(t == Globals::sm_geometryZTypeName)
690  return te::gm::GeometryZType;
691 
692 
693  else if(t == Globals::sm_pointZTypeName)
694  return te::gm::PointZType;
695 
696 
697  else if(t == Globals::sm_lineStringZTypeName)
699 
700 
701  else if(t == Globals::sm_polygonZTypeName)
702  return te::gm::PolygonZType;
703 
704 
705  else if(t == Globals::sm_multiPointZTypeName)
707 
708 
709  else if(t == Globals::sm_multiLineStringZTypeName)
711 
712 
713  else if(t == Globals::sm_multiPolygonZTypeName)
715 
716 
717  else if(t == Globals::sm_curvePolygonZTypeName)
718 
720 
721  else if(t == Globals::sm_polyhedralSurfaceZTypeName)
723 
724 
725  else if(t == Globals::sm_geometryMTypeName)
726  return te::gm::GeometryMType;
727 
728 
729  else if(t == Globals::sm_pointMTypeName)
730  return te::gm::PointMType;
731 
732 
733  else if(t == Globals::sm_lineStringMTypeName)
735 
736 
737  else if(t == Globals::sm_polygonMTypeName)
738  return te::gm::PolygonMType;
739 
740 
741  else if(t == Globals::sm_multiPointMTypeName)
743 
744 
745  else if(t == Globals::sm_multiLineStringMTypeName)
747 
748 
749  else if(t == Globals::sm_multiPolygonMTypeName)
751 
752 
753  else if(t == Globals::sm_curvePolygonMTypeName)
755 
756 
757  else if(t == Globals::sm_polyhedralSurfaceMTypeName)
759 
760 
761  else if(t == Globals::sm_geometryZMTypeName)
762  return te::gm::GeometryZMType;
763 
764 
765  else if(t == Globals::sm_pointZMTypeName)
766  return te::gm::PointZMType;
767 
768 
769  else if(t == Globals::sm_lineStringZMTypeName)
771 
772 
773  else if(t == Globals::sm_polygonZMTypeName)
774  return te::gm::PolygonZMType;
775 
776 
777  else if(t == Globals::sm_multiPointZMTypeName)
779 
780 
781  else if(t == Globals::sm_multiLineStringZMTypeName)
783 
784 
785  else if(t == Globals::sm_multiPolygonZMTypeName)
787 
788 
789  else if(t == Globals::sm_curvePolygonZMTypeName)
791 
792 
793  else if(t == Globals::sm_polyhedralSurfaceZMTypeName)
795 
796  else
798 
799 }
800 
801 int te::ado::GetSRID(_ConnectionPtr adoConn, std::string tableName, std::string geomPropName)
802 {
803  _RecordsetPtr recset;
804  TESTHR(recset.CreateInstance(__uuidof(Recordset)));
805 
806  std::string sel = "SELECT * FROM geometry_columns where f_table_name = '";
807  sel += tableName + "' and f_geometry_column = '" + geomPropName + "'";
808 
809  _variant_t variantSel;
810  variantSel.SetString(sel.c_str());
811 
812  try
813  {
814  TESTHR(recset->Open(variantSel, _variant_t((IDispatch *)adoConn),
815  adOpenStatic, adLockReadOnly, adCmdText));
816  }
817  catch(_com_error& e)
818  {
819  throw Exception(TR_ADO(e.Description()));
820  }
821 
822  return (int32_t)recset->GetFields()->GetItem("srid")->GetValue();
823 }
824 
825 te::gm::GeomType te::ado::GetType(_ConnectionPtr adoConn, std::string tableName, std::string geomPropName)
826 {
827  _RecordsetPtr recset;
828  TESTHR(recset.CreateInstance(__uuidof(Recordset)));
829 
830  std::string sel = "SELECT * FROM geometry_columns where f_table_name = '";
831  sel += tableName + "' and f_geometry_column = '" + geomPropName + "'";
832 
833  _variant_t variantSel;
834  variantSel.SetString(sel.c_str());
835 
836  try
837  {
838  TESTHR(recset->Open(variantSel, _variant_t((IDispatch *)adoConn),
839  adOpenStatic, adLockReadOnly, adCmdText));
840  }
841  catch(_com_error& e)
842  {
843  throw Exception(TR_ADO(e.Description()));
844  }
845 
846  std::string type = (LPCSTR)(_bstr_t)recset->GetFields()->GetItem("type")->GetValue();
847 
848  return GetGeometryType(type);
849 }
850 
851 bool te::ado::IsGeomProperty(_ConnectionPtr adoConn, std::string tableName, std::string columnName)
852 {
853  ADOX::_CatalogPtr pCatalog;
854 
855  TESTHR(pCatalog.CreateInstance(__uuidof(ADOX::Catalog)));
856 
857  try
858  {
859  pCatalog->PutActiveConnection(variant_t((IDispatch *)adoConn));
860 
861  for(long i = 0; i < pCatalog->GetTables()->Count; i++)
862  {
863  if(std::string(pCatalog->GetTables()->GetItem(i)->GetName()) == "geometry_columns")
864  {
865  _RecordsetPtr recset;
866  TESTHR(recset.CreateInstance(__uuidof(Recordset)));
867 
868  TESTHR(recset->Open(_bstr_t("geometry_columns"),
869  _variant_t((IDispatch*)adoConn,true), adOpenKeyset, adLockOptimistic, adCmdTable));
870 
871  while(!recset->EndOfFile)
872  {
873  if(std::string((_bstr_t)recset->Fields->GetItem("f_table_name")->Value) == tableName
874  && std::string((_bstr_t)recset->Fields->GetItem("f_geometry_column")->Value) == columnName)
875  return true;
876 
877  recset->MoveNext();
878  }
879  }
880  }
881  }
882  catch(_com_error& e)
883  {
884  throw Exception(TR_ADO(e.Description()));
885  }
886 
887  return false;
888 }
889 
891 {
892  if( (type >= 1000 && type < 2000) || (type >= 3000 && type < 4000) )
893  return true;
894  return false;
895 }
896 
897 std::string te::ado::GetSystemDateTimeFormat(std::string& indAM, std::string& indPM, std::string& sepD, std::string& sepT)
898 {
899  std::string key = "Control Panel\\International";
900  std::string dateFormat = "sShortDate"; //formato da data d/M/yyyy
901  std::string timeFormat = "sTimeFormat"; //formato da hora HH:mm:ss
902  std::string hourFormat = "iTime"; //12 horas (0) ou 24 horas(1)
903  std::string indicatorAM = "s1159"; //AM
904  std::string indicatorPM = "s2359"; //PM
905  std::string dateSeparator = "sDate"; //separador de data
906  std::string timeSeparator = "sTime"; //separador de hora
907 
908  std::string rdateFormat;
909  std::string rtimeFormat;
910  std::string rhourFormat;
911  std::string rdateSeparator;
912  std::string rtimeSeparator;
913 
914  HKEY hk;
915  DWORD DataSize = 1024;
916  DWORD Type = REG_SZ;
917  char buf[1024];
918 
919  if (RegOpenKeyExA(HKEY_CURRENT_USER, key.c_str(), 0, KEY_READ, &hk) == ERROR_SUCCESS)
920  {
921  memset (buf, 0, 1024);
922  DataSize = 1024;
923  //date format
924  if (RegQueryValueExA(hk, dateFormat.c_str(), NULL, &Type, (LPBYTE)buf, &DataSize) == ERROR_SUCCESS)
925  rdateFormat = buf;
926 
927  memset (buf, 0, 1024);
928  DataSize = 1024;
929  //date separator
930  if (RegQueryValueExA(hk, dateSeparator.c_str(), NULL, &Type, (LPBYTE)buf, &DataSize) == ERROR_SUCCESS)
931  rdateSeparator = buf;
932 
933  memset (buf, 0, 1024);
934  DataSize = 1024;
935  //time format
936  if (RegQueryValueExA(hk, timeFormat.c_str(), NULL, &Type, (LPBYTE)buf, &DataSize) == ERROR_SUCCESS)
937  rtimeFormat = buf;
938 
939  memset (buf, 0, 1024);
940  DataSize = 1024;
941  //hour format
942  if (RegQueryValueExA(hk, hourFormat.c_str(), NULL, &Type, (LPBYTE)buf, &DataSize) == ERROR_SUCCESS)
943  rhourFormat = buf;
944 
945  memset (buf, 0, 1024);
946  DataSize = 1024;
947  //indicator AM
948  if (RegQueryValueExA(hk, indicatorAM.c_str(), NULL, &Type, (LPBYTE)buf, &DataSize) == ERROR_SUCCESS)
949  indAM = buf;
950 
951  memset (buf, 0, 1024);
952  DataSize = 1024;
953  //indicator PM
954  if (RegQueryValueExA(hk, indicatorPM.c_str(), NULL, &Type, (LPBYTE)buf, &DataSize) == ERROR_SUCCESS)
955  indPM = buf;
956 
957  memset (buf, 0, 1024);
958  DataSize = 1024;
959  //time separator
960  if (RegQueryValueExA(hk, timeSeparator.c_str(), NULL, &Type, (LPBYTE)buf, &DataSize) == ERROR_SUCCESS)
961  rtimeSeparator = buf;
962  }
963  else
964  return "";
965 
966  sepD = rdateSeparator;
967  sepT = rtimeSeparator;
968 
969  //DATE
970  //first
971  int pos = rdateFormat.find(rdateSeparator);
972  std::string firstD = rdateFormat.substr(0,pos);
973  std::string temp = rdateFormat.substr(pos+1);
974  //second and third
975  pos = temp.find(rdateSeparator);
976  std::string secondD = temp.substr(0,pos);
977  std::string thirdD = temp.substr(pos+1);
978 
979  //passar para o formato
980  if(firstD.find("a")==0)
981  replace(firstD.begin(), firstD.end(), 97, 89);
982  else if(secondD.find("a")==0)
983  replace(secondD.begin(), secondD.end(),97, 89);
984  else if(thirdD.find("a")==0)
985  replace(thirdD.begin(), thirdD.end(), 97, 89);
986 
987  //TIME
988  //first
989  pos = rtimeFormat.find(rtimeSeparator);
990  std::string firstT = rtimeFormat.substr(0,pos);
991  temp = rtimeFormat.substr(pos+1);
992  //second and third
993  pos = temp.find(rtimeSeparator);
994  std::string secondT = temp.substr(0,pos);
995  int posEmpth = temp.find(" ");
996  std::string thirdT;
997 
998  if(posEmpth==-1)
999  thirdT = temp.substr(pos+1);
1000  else
1001  thirdT = temp.substr(pos+1, (posEmpth-(pos+1)));
1002 
1003  int hFormat = atoi(rhourFormat.c_str());
1004 
1005  //passar para o formato
1006 
1007  firstT = te::common::Convert2UCase(firstT);
1008  secondT = te::common::Convert2UCase(secondT);
1009  thirdT = te::common::Convert2UCase(thirdT);
1010 
1011  if((firstT.find("M")==0))
1012  replace(firstT.begin(), firstT.end(), 77, 109);
1013  else if(secondT.find("M")==0)
1014  replace(secondT.begin(), secondT.end(), 77, 109);
1015  else if(thirdT.find("M")==0)
1016  replace(thirdT.begin(), thirdT.end(), 77, 109);
1017 
1018  std::string timef;
1019  if(hFormat==0)
1020  timef = "sTT";
1021  else
1022  timef = "";
1023 
1024  std::string result = te::common::Convert2UCase(firstD) +"s"+
1025  te::common::Convert2UCase(secondD) +"s"+
1026  te::common::Convert2UCase(thirdD) +"s"+
1027  firstT +"s"+
1028  secondT +"s"+
1029  thirdT + timef;
1030 
1031  RegCloseKey (hk);
1032  return result;
1033 }
1034 
1035 int te::ado::GetMonth(const std::string& month)
1036 {
1037  std::string tempM = te::common::Convert2UCase(month);
1038  if(tempM=="JAN")
1039  return 1;
1040  else if(tempM=="FEB")
1041  return 2;
1042  else if(tempM=="MAR")
1043  return 3;
1044  else if(tempM=="APR")
1045  return 4;
1046  else if(tempM=="MAY")
1047  return 5;
1048  else if(tempM=="JUN")
1049  return 6;
1050  else if(tempM=="JUL")
1051  return 7;
1052  else if(tempM=="AUG")
1053  return 8;
1054  else if(tempM=="SEP")
1055  return 9;
1056  else if(tempM=="OCT")
1057  return 12;
1058  else if(tempM=="NOV")
1059  return 11;
1060  else if(tempM=="DEC")
1061  return 12;
1062 
1063  return -1;
1064 }
1065 
1066 std::auto_ptr<te::dt::DateTime> te::ado::GetDateTime(std::string& value, std::string& mask,
1067  std::string& sepD, std::string& sepT)
1068 {
1069  int sec = 0;
1070  int min = 0;
1071  int hour = 0;
1072  int day = 0;
1073  int mon = 0;
1074  int year = 0;
1075 
1076  if(mask == "DDsMMsYYYYsHHsmmsSS")
1077  {
1078  std::vector<std::string> tokGeneral;
1079  te::common::Tokenize(value, tokGeneral, " ");
1080 
1081  // Only Date or only Time
1082  if(tokGeneral.size() == 1)
1083  {
1084  std::vector<std::string> tokDate;
1085  te::common::Tokenize(tokGeneral[0], tokDate, sepD);
1086  std::vector<std::string> tokTime;
1087  te::common::Tokenize(tokGeneral[0], tokTime, sepT);
1088 
1089  // Only Date
1090  if(tokDate.size() > 1)
1091  {
1092  day = boost::lexical_cast<int>(tokDate[0]);
1093  mon = boost::lexical_cast<int>(tokDate[1]);
1094  year = boost::lexical_cast<int>(tokDate[2]);
1095  }
1096  // Only Time
1097  else if(tokTime.size() > 1)
1098  {
1099  hour = boost::lexical_cast<int>(tokTime[0]);
1100  min = boost::lexical_cast<int>(tokTime[1]);
1101  sec = boost::lexical_cast<int>(tokTime[2]);
1102  }
1103  }
1104  // Date and Time
1105  else if(tokGeneral.size() == 2)
1106  {
1107  std::vector<std::string> tokDate;
1108  te::common::Tokenize(tokGeneral[0], tokDate, sepD);
1109  std::vector<std::string> tokTime;
1110  te::common::Tokenize(tokGeneral[1], tokTime, sepT);
1111 
1112  day = boost::lexical_cast<int>(tokDate[0]);
1113  mon = boost::lexical_cast<int>(tokDate[1]);
1114  year = boost::lexical_cast<int>(tokDate[2]);
1115  hour = boost::lexical_cast<int>(tokTime[0]);
1116  min = boost::lexical_cast<int>(tokTime[1]);
1117  sec = boost::lexical_cast<int>(tokTime[2]);
1118  }
1119  }
1120  else if(mask == "MsDsYYYYsHsmmsSSsTT")
1121  {
1122  std::vector<std::string> tokGeneral;
1123  te::common::Tokenize(value, tokGeneral, " ");
1124 
1125  // Only Date
1126  if(tokGeneral.size() == 1)
1127  {
1128  std::vector<std::string> tokDate;
1129  te::common::Tokenize(tokGeneral[0], tokDate, sepD);
1130  mon = boost::lexical_cast<int>(tokDate[0]);
1131  day = boost::lexical_cast<int>(tokDate[1]);
1132  year = boost::lexical_cast<int>(tokDate[2]);
1133  }
1134  // Only Time
1135  else if(tokGeneral.size() == 2)
1136  {
1137  std::vector<std::string> tokTime;
1138  te::common::Tokenize(tokGeneral[0], tokTime, sepT);
1139  hour = boost::lexical_cast<int>(tokTime[0]);
1140  min = boost::lexical_cast<int>(tokTime[1]);
1141  sec = boost::lexical_cast<int>(tokTime[2]);
1142  }
1143  // Date and Time
1144  else if(tokGeneral.size() == 3)
1145  {
1146  std::vector<std::string> tokDate;
1147  std::vector<std::string> tokTime;
1148  te::common::Tokenize(tokGeneral[0], tokDate, sepD);
1149  te::common::Tokenize(tokGeneral[1], tokTime, sepT);
1150 
1151  mon = boost::lexical_cast<int>(tokDate[0]);
1152  day = boost::lexical_cast<int>(tokDate[1]);
1153  year = boost::lexical_cast<int>(tokDate[2]);
1154  hour = boost::lexical_cast<int>(tokTime[0]);
1155  min = boost::lexical_cast<int>(tokTime[1]);
1156  sec = boost::lexical_cast<int>(tokTime[2]);
1157  }
1158  }
1159  else if(mask == "YYYYsMMsDDsHHsmmsSS")
1160  {
1161  std::vector<std::string> tokGeneral;
1162  te::common::Tokenize(value, tokGeneral, " ");
1163 
1164  // Only Date or only Time
1165  if(tokGeneral.size() == 1)
1166  {
1167  std::vector<std::string> tokDate;
1168  te::common::Tokenize(tokGeneral[0], tokDate, sepD);
1169  std::vector<std::string> tokTime;
1170  te::common::Tokenize(tokGeneral[0], tokTime, sepT);
1171 
1172  // Only Date
1173  if(tokDate.size() > 1)
1174  {
1175  year = boost::lexical_cast<int>(tokDate[0]);
1176  mon = boost::lexical_cast<int>(tokDate[1]);
1177  day = boost::lexical_cast<int>(tokDate[2]);
1178  }
1179  // Only Time
1180  else if(tokTime.size() > 1)
1181  {
1182  hour = boost::lexical_cast<int>(tokTime[0]);
1183  min = boost::lexical_cast<int>(tokTime[1]);
1184  sec = boost::lexical_cast<int>(tokTime[2]);
1185  }
1186  }
1187  // Date and Time
1188  else if(tokGeneral.size() == 2)
1189  {
1190  std::vector<std::string> tokDate;
1191  te::common::Tokenize(tokGeneral[0], tokDate, sepD);
1192  std::vector<std::string> tokTime;
1193  te::common::Tokenize(tokGeneral[1], tokTime, sepT);
1194 
1195  year = boost::lexical_cast<int>(tokDate[0]);
1196  mon = boost::lexical_cast<int>(tokDate[1]);
1197  day = boost::lexical_cast<int>(tokDate[2]);
1198  hour = boost::lexical_cast<int>(tokTime[0]);
1199  min = boost::lexical_cast<int>(tokTime[1]);
1200  sec = boost::lexical_cast<int>(tokTime[2]);
1201  }
1202  }
1203  else
1204  throw Exception((boost::format(TR_ADO("DateTime format not provided or invalid: %1%!")) % mask).str());
1205 
1206  te::dt::DateTime* result = 0;
1207 
1208  if((day > 0 || mon > 0 || year > 0) && (sec > 0 || min > 0 || hour > 0))
1209  {
1210  te::dt::Date d(year, mon, day);
1211  te::dt::TimeDuration td(hour, min, sec);
1212  result = new te::dt::TimeInstant(d, td);
1213  }
1214  else if(day > 0 || mon > 0 || year > 0)
1215  {
1216  result = new te::dt::Date(year, mon, day);
1217  }
1218  else if(sec > 0 || min > 0 || hour > 0)
1219  {
1220  result = new te::dt::TimeDuration(hour, min, sec);
1221  }
1222  else
1223  return std::auto_ptr<te::dt::DateTime>(0);
1224 
1225  return std::auto_ptr<te::dt::DateTime>(result);
1226 }
1227 
1229 {
1230  std::string result = "";
1231 
1232  te::dt::Date* dtime = dynamic_cast<te::dt::Date*>(dateTime);
1233 
1234  std::string mAM, mPM, sepD, sepT;
1235 
1236  std::string mask = GetSystemDateTimeFormat(mAM, mPM, sepD, sepT);
1237 
1238  if(dtime)
1239  {
1240  if(mask.find("DDsMMsYYYY") != std::string::npos)
1241  {
1242  result = boost::lexical_cast<std::string>(dtime->getDay()) + sepD;
1243  result += boost::lexical_cast<std::string>(GetMonth(boost::lexical_cast<std::string>(dtime->getMonth()))) + sepD;
1244  result += boost::lexical_cast<std::string>(dtime->getYear());
1245  }
1246  else if(mask.find("MsDsYYYY") != std::string::npos)
1247  {
1248  result = boost::lexical_cast<std::string>(GetMonth(boost::lexical_cast<std::string>(dtime->getMonth()))) + sepD;
1249  result += boost::lexical_cast<std::string>(dtime->getDay()) + sepD;
1250  result += boost::lexical_cast<std::string>(dtime->getYear());
1251  }
1252  else if(mask.find("YYYYsMMsDD") != std::string::npos)
1253  {
1254  result = boost::lexical_cast<std::string>(dtime->getYear()) + sepD;
1255  result += boost::lexical_cast<std::string>(GetMonth(boost::lexical_cast<std::string>(dtime->getMonth()))) + sepD;
1256  result += boost::lexical_cast<std::string>(dtime->getDay());
1257  }
1258  }
1259 
1260  te::dt::TimeDuration* tduration = dynamic_cast<te::dt::TimeDuration*>(dateTime);
1261 
1262  if(tduration)
1263  {
1264  if(mask.find("HHsmmsSS") != std::string::npos)
1265  {
1266  result = boost::lexical_cast<std::string>(tduration->getHours()) + sepT;
1267  result += boost::lexical_cast<std::string>(tduration->getMinutes()) + sepT;
1268  result += boost::lexical_cast<std::string>(tduration->getSeconds());
1269  }
1270  else if(mask.find("HsmmsSSsTT") != std::string::npos)
1271  {
1272  result = boost::lexical_cast<std::string>(tduration->getHours()) + sepT;
1273  result += boost::lexical_cast<std::string>(tduration->getMinutes()) + sepT;
1274  result += boost::lexical_cast<std::string>(tduration->getSeconds());
1275  }
1276  }
1277 
1278  te::dt::TimeInstant* tinst = dynamic_cast<te::dt::TimeInstant*>(dateTime);
1279 
1280  if(tinst)
1281  {
1282  te::dt::Date date = tinst->getDate();
1283  te::dt::TimeDuration time = tinst->getTime();
1284 
1285  if(mask == "DDsMMsYYYYsHHsmmsSS")
1286  {
1287  result = boost::lexical_cast<std::string>(date.getDay()) + sepD;
1288  result += boost::lexical_cast<std::string>(GetMonth(boost::lexical_cast<std::string>(date.getMonth()))) + sepD;
1289  result += boost::lexical_cast<std::string>(date.getYear()) + " ";
1290  result += boost::lexical_cast<std::string>(time.getHours()) + sepT;
1291  result += boost::lexical_cast<std::string>(time.getMinutes()) + sepT;
1292  result += boost::lexical_cast<std::string>(time.getSeconds());
1293  }
1294  else if(mask == "MsDsYYYYsHsmmsSSsTT")
1295  {
1296  result = boost::lexical_cast<std::string>(GetMonth(boost::lexical_cast<std::string>(date.getMonth()))) + sepD;
1297  result += boost::lexical_cast<std::string>(date.getDay()) + sepD;
1298  result += boost::lexical_cast<std::string>(date.getYear()) + " ";
1299  result += boost::lexical_cast<std::string>(time.getHours()) + sepT;
1300  result += boost::lexical_cast<std::string>(time.getMinutes()) + sepT;
1301  result += boost::lexical_cast<std::string>(time.getSeconds());
1302  }
1303  else if(mask == "YYYYsMMsDDsHHsmmsSS")
1304  {
1305  result = boost::lexical_cast<std::string>(date.getYear()) + sepD;
1306  result += boost::lexical_cast<std::string>(GetMonth(boost::lexical_cast<std::string>(date.getMonth()))) + sepD;
1307  result += boost::lexical_cast<std::string>(date.getDay()) + " ";
1308  result += boost::lexical_cast<std::string>(time.getHours()) + sepT;
1309  result += boost::lexical_cast<std::string>(time.getMinutes()) + sepT;
1310  result += boost::lexical_cast<std::string>(time.getSeconds());
1311  }
1312  }
1313 
1314  return result;
1315 }
1316 
int Convert2Terralib(ADOX::DataTypeEnum adoType)
Bind ADOX Type to TerraLib Type.
Definition: Utils.cpp:202
Utility functions for ADO.
The type for variable-length multidimensional arrays.
Definition: ArrayProperty.h:45
int getSRID() const
It returns the Spatial Reference System ID associated to this geometric object.
Definition: Geometry.h:189
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:825
An atomic property like an integer or double.
std::size_t getWkbSize() const
It returns the size required by a WKB representation for this geometric object.
Definition: Geometry.cpp:133
DataSet class implementation for Microsoft Access driver.
void getWkb(char *wkb, te::common::MachineByteOrder byteOrder) const
It serializes the geometry to a WKB representation into the specified buffer.
Definition: Geometry.cpp:138
bool IsGeomProperty(_ConnectionPtr adoConn, std::string tableName, std::string columnName)
Verifies whether is in the geometry_columns table.
Definition: Utils.cpp:851
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.
boost::gregorian::greg_year getYear() const
It returns the gregorian year.
Definition: Date.h:111
boost::gregorian::greg_month getMonth() const
It returns the gregorian month - from 1 to 12.
Definition: Date.h:104
The type for string types: FIXED_STRING, VAR_STRING or STRING.
std::string GetFormattedDateTime(te::dt::DateTime *dateTime)
It gets a formatted DateTime string for ADO.
Definition: Utils.cpp:1228
Date getDate() const
It returns the date associated to time instant.
Definition: TimeInstant.h:106
It describes a primary key (pk) constraint.
Definition: PrimaryKey.h:52
std::string Convert2UCase(const std::string &value)
It converts a string to upper case.
Definition: StringUtils.h:163
ADOX::DataTypeEnum Convert2Ado(int terralib)
Bind TerraLib Type to ADO Type.
Definition: Utils.cpp:128
It models a foreign key constraint for a DataSetType.
Definition: ForeignKey.h:50
A base class for date data types.
Definition: Date.h:53
int GetSRID(_ConnectionPtr adoConn, std::string tableName, std::string geomPropName)
Read the geometry_columns table end return a SRID.
Definition: Utils.cpp:801
Configuration flags for the TerraLib ADO Data Access driver.
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
Definition: Geometry.h:73
std::string MakeConnectionStr(const std::map< std::string, std::string > &dsInfo)
Create a connection string based on a map.
Definition: Utils.cpp:82
boost::gregorian::greg_day getDay() const
It returns the gregorian day - from 1 to 31.
Definition: Date.h:97
It describes a unique key (uk) constraint.
Definition: UniqueKey.h:53
GeomType
Each enumerated type is compatible with a Well-known Binary (WKB) type code.
Definition: Enums.h:41
long getSeconds() const
It returns the seconds of a minute - from 0 to 59.
Definition: TimeDuration.h:105
void Tokenize(const std::string &str, std::vector< std::string > &tokens, const std::string &delimiters=" ")
It tokenizes a given string with a delimiter of your own choice.
Definition: StringUtils.h:216
void Variant2Blob(const _variant_t var, int size, char *&blob)
Convert a variant to a blob.
Definition: Utils.cpp:109
std::string GetSystemDateTimeFormat(std::string &indAM, std::string &indPM, std::string &sepD, std::string &sepT)
It gets the system Date and Time format.
Definition: Utils.cpp:897
te::common::AccessPolicy Convert2Terralib(std::string accessPolicy)
Definition: Serializer.cpp:404
long getMinutes() const
It returns the minutes of a hour - from 0 to 59.
Definition: TimeDuration.h:98
void setProperties(const std::vector< te::dt::Property * > &properties)
It sets the properties that form the unique key.
Definition: UniqueKey.h:117
long getHours() const
It returns the hours of a day - from 0 to 23.
Definition: TimeDuration.h:91
StringType
The subtype of string property.
Definition: Enums.h:107
A class to represent time instant.
Definition: TimeInstant.h:55
It models a property definition.
Definition: Property.h:59
An static class with global definitions.
void Blob2Variant(const char *blob, int size, _variant_t &var)
Convert a blob to a variant.
Definition: Utils.cpp:56
int GetMonth(const std::string &month)
It gets the index of a month.
Definition: Utils.cpp:1035
bool IsZProperty(te::gm::GeomType type)
Verifies whether Z property.
Definition: Utils.cpp:890
void TESTHR(HRESULT hr)
Definition: Connection.cpp:38
Spatial reference system transformation function.
TimeDuration getTime() const
It returns the time duration associated to time instant.
Definition: TimeInstant.cpp:49
void setProperties(const std::vector< te::dt::Property * > &properties)
It sets the properties that form the primary key.
Definition: PrimaryKey.h:116
An exception class for ADO.
const std::string & GetGeometryName(te::gm::GeomType t)
It returns the geometry OGC names.
Definition: Utils.cpp:498
std::auto_ptr< te::dt::DateTime > GetDateTime(std::string &value, std::string &mask, std::string &sepD, std::string &sepT)
It gets the DateTime TerraLib 5 from string.
Definition: Utils.cpp:1066
A class to represent time duration with nano-second/micro-second resolution.
Definition: TimeDuration.h:51
const te::gm::GeomType GetGeometryType(std::string t)
It returns the geometry type concerning the OGC name.
Definition: Utils.cpp:651
#define TR_ADO(message)
It marks a string in order to get translated. This is a special mark used in the DataAccess module of...
Definition: Config.h:126
void setProperties(const std::vector< te::dt::Property * > &properties)
It sets the properties that take part of the foreign key constraint.
Definition: ForeignKey.h:121