All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties 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/dataaccess/utils/Utils.cpp
22 
23  \brief Utility functions for the data access module.
24 */
25 
26 // TerraLib
27 #include "../../common/Translator.h"
28 #include "../../common/StringUtils.h"
29 #include "../../geometry/Envelope.h"
30 #include "../../geometry/GeometryProperty.h"
31 #include "../../raster/RasterProperty.h"
32 #include "../dataset/DataSet.h"
33 #include "../dataset/DataSetAdapter.h"
34 #include "../dataset/DataSetType.h"
35 #include "../dataset/DataSetTypeConverter.h"
36 #include "../dataset/ObjectId.h"
37 #include "../dataset/ObjectIdSet.h"
38 #include "../dataset/PrimaryKey.h"
39 #include "../dataset/UniqueKey.h"
40 #include "../datasource/DataSourceInfoManager.h"
41 #include "../datasource/DataSourceManager.h"
42 #include "../query/DataSetName.h"
43 #include "../query/Field.h"
44 #include "../query/Fields.h"
45 #include "../query/LiteralEnvelope.h"
46 #include "../query/LiteralGeom.h"
47 #include "../query/PropertyName.h"
48 #include "../query/ST_Contains.h"
49 #include "../query/ST_Crosses.h"
50 #include "../query/ST_Disjoint.h"
51 #include "../query/ST_Equals.h"
52 #include "../query/ST_Intersects.h"
53 #include "../query/ST_Overlaps.h"
54 #include "../query/ST_Touches.h"
55 #include "../query/ST_Within.h"
56 #include "../query/Where.h"
57 #include "../Enums.h"
58 #include "../Exception.h"
59 #include "Utils.h"
60 
61 // STL
62 #include <cassert>
63 
64 //BOOST
65 #include <boost/algorithm/string.hpp>
66 
67 void te::da::LoadFull(te::da::DataSetType* dataset, const std::string& datasourceId)
68 {
69  assert(dataset);
70  assert(!datasourceId.empty());
71 
72  //DataSourcePtr datasource(te::da::DataSourceManager::getInstance().get(datasourceId));
73  DataSourcePtr datasource(te::da::DataSourceManager::getInstance().find(datasourceId));
74 
75  if(datasource.get() == 0)
76  return;
77 
78  if(dataset->size() == 0)
79  {
80  boost::ptr_vector<te::dt::Property> properties = datasource->getProperties(dataset->getName());
81 
82  dataset->add(properties);
83  }
84 
85  //datasource->getCheckConstraints(dataset);
86 
87  //cloader->getIndexes(dataset);
88 
89  //cloader->getUniqueKeys(dataset);
90 
91  //cloader->getPrimaryKey(dataset);
92 }
93 
94 //void te::da::LoadFull(te::da::DataSetType* dataset, const std::string& datasourceId)
95 //{
96 // assert(dataset);
97 // assert(!datasourceId.empty());
98 //
99 // DataSourcePtr datasource(te::da::DataSourceManager::getInstance().find(datasourceId));
100 //
101 // if(datasource.get() == 0)
102 // return;
103 //
104 // LoadFull(dataset, datasource.get());
105 //}
106 
107 //void te::da::LoadFull(DataSetType* dataset, DataSource* datasource)
108 //{
109 // assert(dataset);
110 // assert(datasource);
111 //
112 // std::auto_ptr<DataSourceTransactor> transactor(datasource->getTransactor());
113 //
114 // LoadFull(dataset, transactor.get());
115 //}
116 
117 void te::da::LoadProperties(te::da::DataSetType* dataset, const std::string& datasourceId)
118 {
119  assert(dataset);
120  assert(!datasourceId.empty());
121 
122  //DataSourcePtr datasource(te::da::DataSourceManager::getInstance().get(datasourceId));
123  DataSourcePtr datasource(te::da::DataSourceManager::getInstance().find(datasourceId));
124 
125  if(datasource.get() == 0)
126  return;
127 
128  boost::ptr_vector<te::dt::Property> properties = datasource->getProperties(dataset->getName());
129 
130  dataset->add(properties);
131 }
132 
133 te::gm::Envelope* te::da::GetExtent(const std::string& datasetName,
134  const std::string& propertyName,
135  const std::string& datasourceId)
136 {
137  assert(!datasourceId.empty());
138 
139  //DataSourcePtr datasource(te::da::DataSourceManager::getInstance().get(datasourceId));
140  DataSourcePtr datasource(te::da::DataSourceManager::getInstance().find(datasourceId));
141 
142  if(datasource.get() == 0)
143  throw Exception(TE_TR("Could not retrieve data source in order to search for a property extent!"));
144 
145  std::auto_ptr<te::gm::Envelope> mbr(datasource->getExtent(datasetName, propertyName));
146 
147  return mbr.release();
148 }
149 
150 void te::da::GetDataSetNames(std::vector<std::string>& datasetNames, const std::string& datasourceId)
151 {
152  assert(!datasourceId.empty());
153 
154  DataSourcePtr ds(te::da::DataSourceManager::getInstance().find(datasourceId));
155  if(ds.get() == 0)
156  return;
157 
158  datasetNames = ds->getDataSetNames();
159 }
160 
161 std::string te::da::GetDataSetCategoryName(int category)
162 {
163  switch(category)
164  {
166  return "Unknown";
167 
168  case te::da::TABLE_TYPE:
169  return "Table";
170 
172  return "System";
173 
174  case te::da::VIEW_TYPE:
175  return "View";
176 
177  case te::da::QUERY_TYPE:
178  return "Query";
179 
180  case te::da::INDEX_TYPE:
181  return "Index";
182 
184  return "Sequence";
185 
187  return "Trigger";
188 
190  return "Regular File";
191 
192  default:
193  return "";
194  }
195 }
196 
197 bool te::da::HasDataSet(const std::string& datasourceId)
198 {
199  assert(!datasourceId.empty());
200 
201  //DataSourcePtr datasource(te::da::DataSourceManager::getInstance().get(datasourceId));
202  DataSourcePtr datasource(te::da::DataSourceManager::getInstance().find(datasourceId));
203 
204  if(datasource.get() == 0)
205  return false;
206 
207  return datasource->hasDataSets();
208 }
209 
210 te::da::DataSet* te::da::GetDataSet(const std::string& name, const std::string& datasourceId)
211 {
212  assert(!datasourceId.empty());
213 
214  //DataSourcePtr datasource(te::da::DataSourceManager::getInstance().get(datasourceId));
215 
216  DataSourcePtr datasource(te::da::DataSourceManager::getInstance().find(datasourceId));
217  if(datasource.get() == 0)
218  return false;
219 
220  std::auto_ptr<DataSet> dataset(datasource->getDataSet(name));
221 
222  return dataset.release();
223 }
224 
225 te::da::DataSetType* te::da::GetDataSetType(const std::string& name, const std::string& datasourceId)
226 {
227  assert(!datasourceId.empty());
228 
229  DataSourcePtr datasource(te::da::DataSourceManager::getInstance().find(datasourceId));
230  if(datasource.get() == 0)
231  return false;
232 
233  std::auto_ptr<DataSetType> datasettype(datasource->getDataSetType(name));
234 
235  return datasettype.release();
236 }
237 
238 //te::da::DataSourcePtr te::da::GetDataSource(const std::string& datasourceId)
239 //{
240 // assert(!datasourceId.empty());
241 //
242 // DataSourcePtr datasource(te::da::DataSourceManager::getInstance().get(datasourceId));
243 //
244 // if(datasource.get() == 0)
245 // {
246 // DataSourceInfoPtr dsinfo = te::da::DataSourceInfoManager::getInstance().get(datasourceId);
247 //
248 // if(dsinfo.get() == 0)
249 // throw Exception(TE_TR("Could not find data source!"));
250 //
251 // te::da::DataSourceManager::getInstance().open(datasourceId, dsinfo->getAccessDriver(), dsinfo->getConnInfo());
252 //
253 // datasource = te::da::DataSourceManager::getInstance().get(datasourceId);
254 // }
255 //
256 // return datasource;
257 //}
258 
259 te::da::DataSourcePtr te::da::GetDataSource(const std::string& datasourceId, const bool opened)
260 {
261  assert(!datasourceId.empty());
262 
263  DataSourcePtr datasource(te::da::DataSourceManager::getInstance().find(datasourceId));
264 
265  if(datasource.get() == 0)
266  {
268 
269  if(dsinfo.get() == 0)
270  throw Exception(TE_TR("Could not find data source!"));
271 
272  datasource = te::da::DataSourceManager::getInstance().make(datasourceId, dsinfo->getAccessDriver());
273 
274  datasource->setConnectionInfo(dsinfo->getConnInfo());
275  }
276 
277  if(opened && !datasource->isOpened())
278  datasource->open();
279 
280  return datasource;
281 }
282 
284 {
285  assert (type);
286 
287  std::vector<size_t> ppos;
288  std::vector<int> ptypes;
289  std::vector<std::string> pnames;
290  std::vector<size_t>::iterator it;
291  set = new ObjectIdSet();
292 
293  GetOIDPropertyPos(type, ppos);
294 
295  for(it=ppos.begin(); it!=ppos.end(); ++it)
296  {
297  te::dt::Property* prop = type->getProperty(*it);
298  ptypes.push_back(prop->getType());
299  pnames.push_back(prop->getName());
300  }
301 
302  for(size_t i=0; i<ppos.size(); i++)
303  set->addProperty(pnames[i], ppos[i], ptypes[i]);
304 }
305 
306 void te::da::GetOIDPropertyNames(const te::da::DataSetType* type, std::vector<std::string>& pnames)
307 {
308  assert(type);
309 
310  // Looking for the primary key properties
311  PrimaryKey* pk = type->getPrimaryKey();
312  if(pk != 0)
313  {
314  const std::vector<te::dt::Property*>& pkProperties = pk->getProperties();
315 
316  for(std::size_t i = 0; i < pkProperties.size(); ++i)
317  pnames.push_back(pkProperties[i]->getName());
318 
319  return;
320  }
321 
322  // Looking for the unique key properties
323  if(type->getNumberOfUniqueKeys() > 0)
324  {
325  for(std::size_t i = 0; i < type->getNumberOfUniqueKeys(); ++i)
326  {
327  UniqueKey* uk = type->getUniqueKey(i);
328 
329  const std::vector<te::dt::Property*>& ukProperties = uk->getProperties();
330 
331  for(std::size_t j = 0; j < ukProperties.size(); ++j)
332  pnames.push_back(ukProperties[j]->getName());
333  }
334 
335  return;
336  }
337 
338  // Here, the data set type do not have primary key properties or unique key properties.
339  // So, use all the non geometric properties.
340  const std::vector<te::dt::Property*>& props = type->getProperties();
341  for(std::size_t i = 0; i < props.size(); ++i)
342  {
343  if(props[i]->getType() == te::dt::GEOMETRY_TYPE || props[i]->getType() == te::dt::RASTER_TYPE ||
344  props[i]->getType() == te::dt::FLOAT_TYPE || props[i]->getType() == te::dt::DOUBLE_TYPE ||
345  props[i]->getType() == te::dt::BYTE_ARRAY_TYPE || props[i]->getType() == te::dt::NUMERIC_TYPE)
346  continue;
347 
348  pnames.push_back(props[i]->getName());
349  }
350 }
351 
352 void te::da::GetOIDPropertyPos(const te::da::DataSetType* type, std::vector<std::size_t>& ppos)
353 {
354  assert(type);
355 
356  std::vector<std::string> oidprops;
357  GetOIDPropertyNames(type, oidprops);
358 
359  for(std::size_t i = 0; i < oidprops.size(); ++i)
360  ppos.push_back(type->getPropertyPosition(oidprops[i]));
361 }
362 
364 {
365  assert(dataset);
366  assert(type);
367 
368  std::vector<std::string> oidprops;
369  GetOIDPropertyNames(type, oidprops);
370 
371  return te::da::GenerateOIDSet(dataset, oidprops);
372 }
373 
374 te::da::ObjectIdSet* te::da::GenerateOIDSet(te::da::DataSet* dataset, const std::vector<std::string>& names)
375 {
376  assert(dataset);
377  assert(!names.empty());
378 
379  ObjectIdSet* oids = new ObjectIdSet;
380 
381  for(std::size_t i = 0; i < names.size(); ++i)
382  {
383  std::size_t pos = GetPropertyPos(dataset, names[i]);
384 
385  if(pos == std::string::npos)
386  throw Exception(TE_TR("Primary Key ") + names[i] + TE_TR(" not found!"));
387 
388  oids->addProperty(names[i], pos, dataset->getPropertyDataType(pos));
389  }
390 
391  while(dataset->moveNext())
392  oids->add(GenerateOID(dataset, names));
393 
394  return oids;
395 }
396 
397 te::da::ObjectId* te::da::GenerateOID(te::da::DataSet* dataset, const std::vector<std::string>& names)
398 {
399  assert(dataset);
400  assert(!names.empty());
401 
402  ObjectId* oid = new ObjectId;
403 
404  for(std::size_t i = 0; i < names.size(); ++i)
405  {
406  if(!dataset->isNull(i))
407  oid->addValue(dataset->getValue(names[i]).release());
408  }
409 
410  return oid;
411 }
412 
414 {
415  assert(dataset);
416 
417  const std::size_t np = dataset->getNumProperties();
418 
419  for(std::size_t i = 0; i != np; ++i)
420  {
421  int pdt = dataset->getPropertyDataType(i);
422 
423  if(pdt == te::dt::GEOMETRY_TYPE || pdt == te::dt::RASTER_TYPE)
424  {
425  return i;
426  }
427  }
428 
429  return std::string::npos;
430 }
431 
432 std::size_t te::da::GetFirstPropertyPos(const te::da::DataSet* dataset, int datatype)
433 {
434  assert(dataset);
435 
436  const std::size_t np = dataset->getNumProperties();
437 
438  for(std::size_t i = 0; i != np; ++i)
439  {
440  int pdt = dataset->getPropertyDataType(i);
441 
442  if(pdt == datatype)
443  {
444  return i;
445  }
446  }
447 
448  return std::string::npos;
449 }
450 
451 std::size_t te::da::GetPropertyPos(const DataSet* dataset, const std::string& name)
452 {
453  assert(dataset);
454 
455  const std::size_t np = dataset->getNumProperties();
456 
457  for(std::size_t i = 0; i != np; ++i)
458  {
459  std::string pname = dataset->getPropertyName(i);
460 
461  if(boost::iequals(pname, name))
462  return i;
463  }
464 
465  return std::string::npos;
466 }
467 
468 std::size_t te::da::GetPropertyPos(const DataSetType* dt, const std::string& name)
469 {
470  assert(dt);
471 
472  const std::size_t np = dt->getProperties().size();
473 
474  for(std::size_t i = 0; i != np; ++i)
475  {
476  std::string pname = dt->getProperty(i)->getName();
477 
478  if(boost::iequals(pname, name))
479  return i;
480  }
481 
482  return std::string::npos;
483 }
484 
486 {
487  assert(dt);
488 
489  const std::size_t np = dt->size();
490 
491  for(std::size_t i = 0; i != np; ++i)
492  {
493  te::dt::Property* p = dt->getProperty(i);
494 
495  assert(p);
496 
497  int pdt = p->getType();
498 
499  if(pdt == te::dt::GEOMETRY_TYPE || pdt == te::dt::RASTER_TYPE)
500  {
501  return p;
502  }
503  }
504 
505  return 0;
506 }
507 
509 {
511 
512  if(p)
513  {
514  return static_cast<te::gm::GeometryProperty*>(p);
515  }
516  else
517  {
518  return 0;
519  }
520 }
521 
523 {
525 
526  assert(p);
527 
528  return static_cast<te::rst::RasterProperty*>(p);
529 }
530 
531 //te::da::DataSetType* te::da::CreateDataSetType(const te::da::DataSet* dataset)
532 //{
533 // assert(dataset);
534 //
535 // te::da::DataSetType* dt = new DataSetType("");
536 //
537 // const std::size_t np = dataset->getNumProperties();
538 //
539 // for(std::size_t i = 0; i != np; ++i)
540 // {
541 // const te::dt::Property* p = dataset->getProperty(i);
542 //
543 // dt->add(p->clone());
544 // }
545 //
546 // return dt;
547 //}
548 
550  std::vector<std::string>& pnames,
551  std::vector<int>& ptypes)
552 {
553  assert(dt);
554 
555  for(std::size_t i = 0; i != dt->size(); ++i)
556  {
557  const te::dt::Property* p = dt->getProperty(i);
558 
559  pnames.push_back(p->getName());
560  ptypes.push_back(p->getType());
561  }
562 }
563 
564 void te::da::GetPropertyInfo(const DataSet* const dataset,
565  std::vector<std::string>& pnames,
566  std::vector<int>& ptypes)
567 {
568  assert(dataset);
569 
570  for(std::size_t i = 0; i != dataset->getNumProperties(); ++i)
571  {
572  pnames.push_back(dataset->getPropertyName(i));
573  ptypes.push_back(dataset->getPropertyDataType(i));
574  }
575 }
576 
577 void te::da::Create(DataSource* ds, DataSetType* dt, DataSet* d, std::size_t limit)
578 {
579  std::map<std::string, std::string> options;
580 
581  Create(ds, dt, d, options, limit);
582 }
583 
585  DataSetType* dt,
586  DataSet* d,
587  const std::map<std::string, std::string>& options,
588  std::size_t limit)
589 {
590  ds->createDataSet(dt, options);
591 
592  ds->add(dt->getName(), d, options, limit);
593 }
594 
596 {
597  assert(ds);
598  assert(converter);
599 
600  DataSetType* type = converter->getResult();
601  assert(type);
602 
603  const std::vector<std::vector<std::size_t> >& indexes = converter->getConvertedPropertyIndexes();
604  const std::vector<AttributeConverter>& funcs = converter->getConverters();
605 
606  assert((type->size() == indexes.size()) && (type->size() == funcs.size()));
607 
608  DataSetAdapter* adapter = new DataSetAdapter(ds, isOwner);
609 
610  for(std::size_t i = 0; i < type->size(); ++i)
611  {
612  te::dt::Property* p = type->getProperty(i);
613  assert(p);
614 
615  adapter->add(p->getName(), p->getType(), indexes[i], funcs[i]);
616  }
617 
618  return adapter;
619 }
620 
622 {
623  std::string valueNames("(");
624 
625  const std::size_t np = dt->size();
626 
627  for(std::size_t i = 0; i != np; ++i)
628  {
629  if(i != 0)
630  valueNames += ",";
631 
632  valueNames += dt->getProperty(i)->getName();
633  }
634 
635  valueNames += ")";
636 
637  return valueNames;
638 }
639 
640 std::string te::da::GetSQLValueNames(const te::da::DataSet* dataset)
641 {
642  std::string valueNames("(");
643 
644  const std::size_t np = dataset->getNumProperties();
645 
646  for(std::size_t i = 0; i != np; ++i)
647  {
648  if(i != 0)
649  valueNames += ",";
650 
651  valueNames += dataset->getPropertyName(i);
652  }
653 
654  valueNames += ")";
655 
656  return valueNames;
657 }
658 
659 std::vector<int> te::da::GetPropertyDataTypes(const te::da::DataSet* dataset)
660 {
661  std::vector<int> properties;
662 
663  const std::size_t np = dataset->getNumProperties();
664 
665  for(std::size_t i = 0; i != np; ++i)
666  {
667  properties.push_back(dataset->getPropertyDataType(i));
668  }
669 
670  return properties;
671 }
672 
673 std::auto_ptr<te::da::Expression> te::da::BuildSpatialOp(Expression* e1,
674  Expression* e2,
676 
677 {
678  std::auto_ptr<te::da::Expression> op;
679  switch(r)
680  {
681  case te::gm::INTERSECTS:
682  op.reset(new ST_Intersects(e1, e2));
683  break;
684 
685  case te::gm::DISJOINT:
686  op.reset(new ST_Disjoint(e1, e2));
687  break;
688 
689  case te::gm::TOUCHES:
690  op.reset(new ST_Touches(e1, e2));
691  break;
692 
693  case te::gm::OVERLAPS:
694  op.reset(new ST_Overlaps(e1, e2));
695  break;
696 
697  case te::gm::CROSSES:
698  op.reset(new ST_Crosses(e1, e2));
699  break;
700 
701  case te::gm::WITHIN:
702  op.reset(new ST_Within(e1, e2));
703  break;
704 
705  case te::gm::CONTAINS:
706  op.reset(new ST_Contains(e1, e2));
707  break;
708 
709  case te::gm::EQUALS:
710  op.reset(new ST_Equals(e1, e2));
711  break;
712 
713  default:
714  throw;
715  }
716 
717  return op;
718 }
719 
720 std::auto_ptr<te::da::Fields> te::da::BuildFields(const std::vector<std::string>& properties)
721 {
722  std::auto_ptr<Fields> fields(new Fields);
723 
724  for(std::size_t i = 0; i < properties.size(); ++i)
725  fields->push_back(new te::da::Field(properties[i]));
726 
727  return fields;
728 }
729 
730 std::auto_ptr<te::da::Select> te::da::BuildSelect(const std::string& dsname)
731 {
732  return BuildSelect(dsname, "*");
733 }
734 
735 std::auto_ptr<te::da::Select> te::da::BuildSelect(const std::string& dsname, const std::string& propertyName)
736 {
737  std::vector<std::string> p;
738  p.push_back(propertyName);
739 
740  return BuildSelect(dsname, p);
741 }
742 
743 std::auto_ptr<te::da::Select> te::da::BuildSelect(const std::string& dsname, const std::vector<std::string>& properties)
744 {
745  // Fields
746  std::auto_ptr<Fields> fields = BuildFields(properties);
747 
748  // From
749  FromItem* fi = new DataSetName(dsname);
750  From* from = new From;
751  from->push_back(fi);
752 
753  // Select
754  std::auto_ptr<Select> select(new Select(fields.release(), from));
755 
756  return select;
757 }
758 
759 std::auto_ptr<te::da::Select> te::da::BuildSelect(const std::string& dsname,
760  const std::vector<std::string>& properties,
761  const std::string& geometryProperty,
762  const te::gm::Envelope* e,
763  int srid,
765 {
766  // Fields
767  std::auto_ptr<Fields> fields = BuildFields(properties);
768 
769  // Adding the geometry property
770  fields->push_back(new Field(geometryProperty));
771 
772  // From
773  FromItem* fi = new DataSetName(dsname);
774  From* from = new From;
775  from->push_back(fi);
776 
777  PropertyName* geomPropertyName = new PropertyName(geometryProperty);
778  LiteralEnvelope* lenv = new LiteralEnvelope(*e, srid);
779 
780  // The spatial restriction
781  std::auto_ptr<Expression> spatialOp = BuildSpatialOp(geomPropertyName, lenv, r);
782 
783  // Where
784  te::da::Where* filter = new Where(spatialOp.release());
785 
786  // Select
787  std::auto_ptr<Select> select(new Select(fields.release(), from, filter));
788 
789  return select;
790 }
791 
792 std::auto_ptr<te::da::Select> te::da::BuildSelect(const std::string& dsname,
793  const std::vector<std::string>& properties,
794  const std::string& geometryProperty,
795  te::gm::Geometry* g,
797 {
798  // Fields
799  std::auto_ptr<Fields> fields = BuildFields(properties);
800 
801  // Adding the geometry property
802  fields->push_back(new Field(geometryProperty));
803 
804  // From
805  FromItem* fi = new DataSetName(dsname);
806  From* from = new From;
807  from->push_back(fi);
808 
809  PropertyName* geomPropertyName = new PropertyName(geometryProperty);
810  LiteralGeom* lgeom = new LiteralGeom(g);
811 
812  // The spatial restriction
813  std::auto_ptr<Expression> spatialOp = BuildSpatialOp(geomPropertyName, lgeom, r);
814 
815  // Where
816  te::da::Where* filter = new Where(spatialOp.release());
817 
818  // Select
819  std::auto_ptr<Select> select(new Select(fields.release(), from, filter));
820 
821  return select;
822 }
823 
824 std::auto_ptr<te::da::Select> te::da::BuildSelect(const std::string& dsname,
825  const std::vector<std::string>& properties,
826  const ObjectIdSet* oids)
827 {
828  // OIDS restriction
829  Expression* exp = oids->getExpression();
830  assert(exp);
831 
832  // Where
833  Where* filter = new Where(exp);
834 
835  // Fields
836  std::auto_ptr<Fields> fields = BuildFields(properties);
837 
838  // Adding the oids properties case not included
839  const std::vector<std::string>& oidsProperties = oids->getPropertyNames();
840  for(std::size_t i = 0; i < oidsProperties.size(); ++i)
841  {
842  const std::string& oidPropertyName = oidsProperties[i];
843 
844  bool alreadyIncluded = false;
845 
846  for(std::size_t j = 0; j < properties.size(); ++j)
847  {
848  if(oidPropertyName == properties[j])
849  {
850  alreadyIncluded = true;
851  break;
852  }
853  }
854 
855  if(!alreadyIncluded)
856  fields->push_back(new Field(oidPropertyName));
857  }
858 
859  // From
860  FromItem* fromItem = new DataSetName(dsname);
861  From* from = new From;
862  from->push_back(fromItem);
863 
864  // Select
865  std::auto_ptr<Select> select(new Select(fields.release(), from, filter));
866 
867  return select;
868 }
869 
870 int te::da::GetPropertyIndex(te::da::DataSet* dataSet, const std::string propName)
871 {
872  int index = 0;
873 
874  for(std::size_t i = 0; i < dataSet->getNumProperties(); ++i)
875  {
876  if(propName == dataSet->getPropertyName(i))
877  {
878  index = i;
879  return index;
880  }
881  }
882  return -1;
883 }
884 
885 bool te::da::IsValidName(const std::string& name, std::string& invalidChar)
886 {
887  if(name.empty())
888  {
889  return false;
890  }
891 
892  if(name[0] >= 0x30 && name[0] <= 0x39)
893  {
894  invalidChar = "begin with a numeric character\n";
895  return false;
896  }
897  if(name[0] == '_')
898  {
899  invalidChar += "begin with a invalid character: underscore _\n";
900  return false;
901  }
902 
903  int ff = name.find(" ");
904  if(ff >= 0)
905  {
906  invalidChar += "invalid character: blank space\n";
907  return false;
908  }
909 
910  ff = name.find(".");
911  if(ff >= 0)
912  {
913  invalidChar += "invalid character: dot '.'\n";
914  return false;
915  }
916 
917  ff = name.find("*");
918  if(ff >= 0)
919  {
920  invalidChar += "invalid character: mathematical symbol '*'\n";
921  return false;
922  }
923 
924  ff = name.find("/");
925  if(ff >= 0)
926  {
927  invalidChar += "invalid character: mathematical symbol '/'\n";
928  return false;
929  }
930 
931  ff = name.find("(");
932  if(ff >= 0)
933  {
934  invalidChar += "invalid character: parentheses '('\n";
935  return false;
936  }
937 
938  ff = name.find(")");
939  if(ff >= 0)
940  {
941  invalidChar += "invalid character: parentheses ')'\n";
942  return false;
943  }
944 
945  ff = name.find("-");
946  if(ff >= 0)
947  {
948  invalidChar += "invalid character: mathematical symbol '-'\n";
949  return false;
950  }
951 
952  ff = name.find("+");
953  if(ff >= 0)
954  {
955  invalidChar += "invalid character: mathematical symbol '+'\n";
956  return false;
957  }
958 
959  ff = name.find("%");
960  if(ff >= 0)
961  {
962  invalidChar += "invalid character: mathematical symbol '%'\n";
963  return false;
964  }
965 
966  ff = name.find(">");
967  if(ff >= 0)
968  {
969  invalidChar += "invalid character: mathematical symbol '>'\n";
970  return false;
971  }
972 
973  ff = name.find("<");
974  if(ff >= 0)
975  {
976  invalidChar += "invalid character: mathematical symbol '<'\n";
977  return false;
978  }
979 
980  ff = name.find("&");
981  if(ff >= 0)
982  {
983  invalidChar += "invalid character: mathematical symbol '&'\n";
984  return false;
985  }
986 
987  ff = name.find("$");
988  if(ff >= 0)
989  {
990  invalidChar += "invalid symbol: '$'\n";
991  return false;
992  }
993 
994  ff = name.find(";");
995  if(ff >= 0)
996  {
997  invalidChar += "invalid symbol: ';'\n";
998  return false;
999  }
1000 
1001  ff = name.find("=");
1002  if(ff >= 0)
1003  {
1004  invalidChar += "invalid symbol: '='\n";
1005  return false;
1006  }
1007 
1008  ff = name.find("!");
1009  if(ff >= 0)
1010  {
1011  invalidChar += "invalid symbol: '!'\n";
1012  return false;
1013  }
1014 
1015  ff = name.find("?");
1016  if(ff >= 0)
1017  {
1018  invalidChar += "invalid symbol: '?'\n";
1019  return false;
1020  }
1021 
1022  ff = name.find("#");
1023  if(ff >= 0)
1024  {
1025  invalidChar += "invalid symbol: '#'\n";
1026  return false;
1027  }
1028 
1029  ff = name.find("¨");
1030  if(ff >= 0)
1031  {
1032  invalidChar += "invalid symbol: '¨'\n";
1033  return false;
1034  }
1035 
1036  ff = name.find(",");
1037  if(ff >= 0)
1038  {
1039  invalidChar += "invalid symbol: ','\n";
1040  return false;
1041  }
1042 
1043  ff = name.find("/");
1044  if(ff >= 0)
1045  {
1046  invalidChar += "invalid symbol: '/'\n";
1047  return false;
1048  }
1049 
1050  ff = name.find("@");
1051  if(ff >= 0)
1052  {
1053  invalidChar += "invalid symbol: '@'\n";
1054  return false;
1055  }
1056 
1057  ff = name.find("{");
1058  if(ff >= 0)
1059  {
1060  invalidChar += "invalid symbol: '{'\n";
1061  return false;
1062  }
1063 
1064  ff = name.find("}");
1065  if(ff >= 0)
1066  {
1067  invalidChar += "invalid symbol: '}'\n";
1068  return false;
1069  }
1070 
1071  std::vector<std::string> vecInvalidChars;
1072  vecInvalidChars.push_back("ª");
1073  vecInvalidChars.push_back("º");
1074  vecInvalidChars.push_back("¹");
1075  vecInvalidChars.push_back("²");
1076  vecInvalidChars.push_back("³");
1077 
1078  for(unsigned int i = 0; i < vecInvalidChars.size(); ++i)
1079  {
1080  std::string invalidItem = vecInvalidChars[i];
1081 
1082  ff = name.find(invalidItem);
1083  if(ff >= 0)
1084  {
1085  invalidChar += "invalid symbol: '" + invalidItem + "'\n";
1086  return false;
1087  }
1088  }
1089 
1090  for(unsigned int i = 0; i < name.size(); ++i)
1091  {
1092  char value = name[i];
1093  if(value < 0)
1094  {
1095  invalidChar += "invalid symbol\n";
1096  return false;
1097  }
1098  }
1099 
1100  std::string u = te::common::Convert2UCase(name);
1101  if(u=="OR" || u=="AND" || u=="NOT" || u=="LIKE" ||
1102  u=="SELECT" || u=="FROM" || u=="UPDATE" || u=="DELETE" ||u=="BY" || u=="GROUP" || u=="ORDER" ||
1103  u=="DROP" || u=="INTO" || u=="VALUE" || u=="IN" || u=="ASC" || u=="DESC"|| u=="COUNT" || u=="JOIN" ||
1104  u=="LEFT" || u=="RIGHT" || u=="INNER" || u=="UNION" || u=="IS" || u=="NULL" || u=="WHERE" ||
1105  u=="BETWEEN" || u=="DISTINCT" || u=="TRY" || u=="IT" || u=="INSERT" || u=="ALIASES" || u=="CREATE" ||
1106  u=="ALTER" || u=="TABLE" || u=="INDEX" || u=="ALL" || u=="HAVING" || u=="EXEC" || u== "SET" ||
1107  u == "AVG" || u == "MAX" || u == "MIN" || u == "SUM" || u == "FILTER" || u == "OFFSET" || u == "LENGHT" )
1108  {
1109  invalidChar += "invalid name: using reserved word " + u + "\n";
1110  return false;
1111  }
1112 
1113  std::string n = te::common::Convert2LCase(name);
1114  // reserved words
1115  if( (n=="zone") || (n=="comp") || (n=="no") || (n=="local") ||
1116  (n=="level") || (n=="long"))
1117  {
1118  invalidChar += "invalid name: using reserved word " + n + "\n";
1119  return false;
1120  }
1121 
1122  return true;
1123 }
TEDATAACCESSEXPORT DataSourcePtr GetDataSource(const std::string &datasourceId, const bool opened=true)
Search for a data source with the informed id in the DataSourceManager.
Definition: Utils.cpp:259
Property * getProperty(std::size_t i) const
It returns the i-th property.
TEDATAACCESSEXPORT te::rst::RasterProperty * GetFirstRasterProperty(const DataSetType *dt)
Definition: Utils.cpp:522
Geometric property.
TEDATAACCESSEXPORT te::gm::Envelope * GetExtent(const std::string &datasetName, const std::string &propertyName, const std::string &datasourceId)
Definition: Utils.cpp:133
TEDATAACCESSEXPORT void LoadProperties(te::da::DataSetType *dataset, const std::string &datasourceId)
Definition: Utils.cpp:117
Utility functions for the data access module.
const std::vector< std::vector< std::size_t > > & getConvertedPropertyIndexes() const
An abstract class that models a source of data in a query.
Definition: FromItem.h:50
The Field class can be used to model an expression that takes part of the output items of a SELECT...
Definition: Field.h:50
TEDATAACCESSEXPORT void GetEmptyOIDSet(const DataSetType *type, ObjectIdSet *&set)
Returns an empty ObjectIdSet, with the definitions of fields that compose it.
Definition: Utils.cpp:283
Spatial within operator.
Definition: ST_Within.h:46
TEDATAACCESSEXPORT ObjectId * GenerateOID(DataSet *dataset, const std::vector< std::string > &names)
Definition: Utils.cpp:397
Spatial intersects operator.
Definition: ST_Intersects.h:46
A class that models the name of a dataset used in a From clause.
Definition: DataSetName.h:43
boost::shared_ptr< DataSource > DataSourcePtr
Definition: DataSource.h:1435
std::string Convert2LCase(const std::string &value)
It converts a string to lower case.
Definition: StringUtils.h:197
TEDATAACCESSEXPORT std::auto_ptr< Select > BuildSelect(const std::string &dsname)
Definition: Utils.cpp:730
Expression * getExpression(const std::string source="") const
It returns the expression that can be used to retrieve the data set that contains the all indentified...
Definition: ObjectIdSet.cpp:77
TEDATAACCESSEXPORT std::auto_ptr< Expression > BuildSpatialOp(Expression *e1, Expression *e2, te::gm::SpatialRelation r)
Definition: Utils.cpp:673
A class that models the name of any property of an object.
Definition: PropertyName.h:50
A class that models the description of a dataset.
Definition: DataSetType.h:72
TEDATAACCESSEXPORT std::string GetDataSetCategoryName(int category)
Definition: Utils.cpp:161
std::size_t getNumberOfUniqueKeys() const
It returns the number of unique keys defined for the dataset type.
Definition: DataSetType.h:269
virtual void createDataSet(DataSetType *dt, const std::map< std::string, std::string > &options)
It creates the dataset schema definition in the target data source.
Definition: DataSource.cpp:445
TEDATAACCESSEXPORT std::size_t GetPropertyPos(const DataSet *dataset, const std::string &name)
Definition: Utils.cpp:451
virtual void add(const std::string &datasetName, 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.
Definition: DataSource.cpp:471
PrimaryKey * getPrimaryKey() const
It returns the primary key associated to the dataset type.
Definition: DataSetType.h:214
std::string Convert2UCase(const std::string &value)
It converts a string to upper case.
Definition: StringUtils.h:163
SpatialRelation
Spatial relations between geometric objects.
Definition: Enums.h:122
TEDATAACCESSEXPORT std::string GetSQLValueNames(const DataSetType *dt)
Definition: Utils.cpp:621
void addValue(te::dt::AbstractData *data)
It adds a property value to uniquely identify a data set element.
Definition: ObjectId.cpp:61
An abstract class for data providers like a DBMS, Web Services or a regular file. ...
Definition: DataSource.h:118
void addProperty(const std::string &name, std::size_t pos, int type)
It adds a property that will be used to generate the unique ids.
Definition: ObjectIdSet.cpp:63
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:345
Spatial crosses operator.
Definition: ST_Crosses.h:46
It models a property definition.
Definition: Property.h:59
TEDATAACCESSEXPORT int GetPropertyIndex(te::da::DataSet *dataSet, const std::string propName)
Definition: Utils.cpp:870
Raster property.
This is an abstract class that models a query expression.
Definition: Expression.h:47
const std::vector< te::dt::Property * > & getProperties() const
It returns the properties that take part of the primary key.
Definition: PrimaryKey.h:109
An converter for DataSetType.
const std::vector< AttributeConverter > & getConverters() const
virtual bool moveNext()=0
It moves the internal pointer to the next item of the collection.
TEDATAACCESSEXPORT std::size_t GetFirstSpatialPropertyPos(const te::da::DataSet *dataset)
It returns the first dataset spatial property or NULL if none is found.
Definition: Utils.cpp:413
const std::vector< Property * > & getProperties() const
It returns the list of properties describing the CompositeProperty.
static DataSourceManager & getInstance()
It returns a reference to the singleton instance.
Spatial overlaps operator.
Definition: ST_Overlaps.h:46
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
This class represents a set of unique ids created in the same context. i.e. from the same data set...
Definition: ObjectIdSet.h:53
virtual std::string getPropertyName(std::size_t i) const =0
It returns the property name at position pos.
TEDATAACCESSEXPORT void Create(DataSource *ds, DataSetType *dt, DataSet *d, std::size_t limit=0)
It creates the dataset definition in a data source and then fill it with data from the input dataset...
Definition: Utils.cpp:577
Spatial touches operator.
Definition: ST_Touches.h:46
virtual bool isNull(std::size_t i) const =0
It checks if the attribute value is NULL.
This class represents an unique id for a data set element.
Definition: ObjectId.h:47
boost::ptr_vector< Field > Fields
Fields is just a boost::ptr_vector of Field pointers.
Definition: Fields.h:37
TEDATAACCESSEXPORT DataSet * GetDataSet(const std::string &name, const std::string &datasourceId)
Definition: Utils.cpp:210
TEDATAACCESSEXPORT bool HasDataSet(const std::string &datasourceId)
Definition: Utils.cpp:197
A class that can be used to model a filter expression that can be applied to a query.
Definition: Where.h:47
It describes a unique key (uk) constraint.
Definition: UniqueKey.h:53
std::size_t size() const
It returns the number of properties of the CompositeProperty.
TEDATAACCESSEXPORT ObjectIdSet * GenerateOIDSet(DataSet *dataset, const DataSetType *type)
Definition: Utils.cpp:363
TEDATAACCESSEXPORT void GetDataSetNames(std::vector< std::string > &datasetNames, const std::string &datasourceId)
Definition: Utils.cpp:150
TEDATAACCESSEXPORT std::vector< int > GetPropertyDataTypes(const te::da::DataSet *dataset)
Definition: Utils.cpp:659
virtual std::size_t getNumProperties() const =0
It returns the number of properties that composes an item of the dataset.
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
Definition: Geometry.h:73
A Select models a query to be used when retrieving data from a DataSource.
Definition: Select.h:65
void add(const std::string &newPropertyName, int newPropertyType, const std::vector< std::size_t > &adaptedPropertyPos, AttributeConverter conv)
boost::ptr_vector< FromItem > From
It models the FROM clause for a query.
Definition: From.h:37
Spatial contains operator.
Definition: ST_Contains.h:46
int getType() const
It returns the property data type.
Definition: Property.h:143
void add(Constraint *c)
It adds a new constraint.
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
Property * findFirstPropertyOfType(const int t) const
returns the first property of the given data type. Caller doesn't take ownership of the returned poin...
TEDATAACCESSEXPORT void GetOIDPropertyPos(const DataSetType *type, std::vector< std::size_t > &ppos)
Definition: Utils.cpp:352
Spatial equals operator.
Definition: ST_Equals.h:46
It describes a primary key (pk) constraint.
Definition: PrimaryKey.h:52
TEDATAACCESSEXPORT bool IsValidName(const std::string &name, std::string &invalidChar)
It checks if the name is not valid as the existence of invalid characters, reserved words...
Definition: Utils.cpp:885
A class that models a literal for Envelope values.
const std::vector< std::string > & getPropertyNames() const
It returns the property names used to generated the oids.
std::size_t getPropertyPosition(const std::string &name) const
It returns the property position based on its name.
TEDATAACCESSEXPORT void GetOIDPropertyNames(const DataSetType *type, std::vector< std::string > &pnames)
Definition: Utils.cpp:306
TEDATAACCESSEXPORT DataSetType * GetDataSetType(const std::string &name, const std::string &datasourceId)
Definition: Utils.cpp:225
Spatial Disjoint operator.
Definition: ST_Disjoint.h:46
TEDATAACCESSEXPORT std::size_t GetFirstPropertyPos(const te::da::DataSet *dataset, int datatype)
Definition: Utils.cpp:432
TEDATAACCESSEXPORT te::gm::GeometryProperty * GetFirstGeomProperty(const DataSetType *dt)
Definition: Utils.cpp:508
TEDATAACCESSEXPORT DataSetAdapter * CreateAdapter(DataSet *ds, DataSetTypeConverter *converter, bool isOwner=false)
Definition: Utils.cpp:595
virtual int getPropertyDataType(std::size_t i) const =0
It returns the underlying data type of the property at position pos.
TEDATAACCESSEXPORT void LoadFull(te::da::DataSetType *dataset, const std::string &datasourceId)
Definition: Utils.cpp:67
An adapter for DataSet.
TEDATAACCESSEXPORT std::auto_ptr< Fields > BuildFields(const std::vector< std::string > &properties)
Definition: Utils.cpp:720
TEDATAACCESSEXPORT te::dt::Property * GetFirstSpatialProperty(const DataSetType *dt)
Definition: Utils.cpp:485
UniqueKey * getUniqueKey(std::size_t i) const
It returns the i-th unique key.
Definition: DataSetType.h:294
virtual std::auto_ptr< te::dt::AbstractData > getValue(std::size_t i) const
Method for retrieving any other type of data value stored in the data source.
Definition: DataSet.cpp:151
boost::shared_ptr< DataSourceInfo > DataSourceInfoPtr
TEDATAACCESSEXPORT void GetPropertyInfo(const DataSetType *const dt, std::vector< std::string > &pnames, std::vector< int > &ptypes)
Definition: Utils.cpp:549
A class that models a literal for Geometry values.
Definition: LiteralGeom.h:46
const std::string & getName() const
It returns the property name.
Definition: Property.h:126