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/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 "../../geometry/Envelope.h"
29 #include "../../geometry/GeometryProperty.h"
30 #include "../../raster/RasterProperty.h"
31 #include "../dataset/DataSet.h"
32 #include "../dataset/DataSetAdapter.h"
33 #include "../dataset/DataSetType.h"
34 #include "../dataset/DataSetTypeConverter.h"
35 #include "../dataset/ObjectId.h"
36 #include "../dataset/ObjectIdSet.h"
37 #include "../dataset/PrimaryKey.h"
38 #include "../dataset/UniqueKey.h"
39 #include "../datasource/DataSourceInfoManager.h"
40 #include "../datasource/DataSourceManager.h"
41 #include "../query/DataSetName.h"
42 #include "../query/Field.h"
43 #include "../query/Fields.h"
44 #include "../query/LiteralEnvelope.h"
45 #include "../query/LiteralGeom.h"
46 #include "../query/PropertyName.h"
47 #include "../query/ST_Contains.h"
48 #include "../query/ST_Crosses.h"
49 #include "../query/ST_Disjoint.h"
50 #include "../query/ST_Equals.h"
51 #include "../query/ST_Intersects.h"
52 #include "../query/ST_Overlaps.h"
53 #include "../query/ST_Touches.h"
54 #include "../query/ST_Within.h"
55 #include "../query/Where.h"
56 #include "../Enums.h"
57 #include "../Exception.h"
58 #include "Utils.h"
59 
60 // STL
61 #include <cassert>
62 
63 //BOOST
64 #include <boost/algorithm/string.hpp>
65 
66 void te::da::LoadFull(te::da::DataSetType* dataset, const std::string& datasourceId)
67 {
68  assert(dataset);
69  assert(!datasourceId.empty());
70 
71  //DataSourcePtr datasource(te::da::DataSourceManager::getInstance().get(datasourceId));
72  DataSourcePtr datasource(te::da::DataSourceManager::getInstance().find(datasourceId));
73 
74  if(datasource.get() == 0)
75  return;
76 
77  if(dataset->size() == 0)
78  {
79  boost::ptr_vector<te::dt::Property> properties = datasource->getProperties(dataset->getName());
80 
81  dataset->add(properties);
82  }
83 
84  //datasource->getCheckConstraints(dataset);
85 
86  //cloader->getIndexes(dataset);
87 
88  //cloader->getUniqueKeys(dataset);
89 
90  //cloader->getPrimaryKey(dataset);
91 }
92 
93 //void te::da::LoadFull(te::da::DataSetType* dataset, const std::string& datasourceId)
94 //{
95 // assert(dataset);
96 // assert(!datasourceId.empty());
97 //
98 // DataSourcePtr datasource(te::da::DataSourceManager::getInstance().find(datasourceId));
99 //
100 // if(datasource.get() == 0)
101 // return;
102 //
103 // LoadFull(dataset, datasource.get());
104 //}
105 
106 //void te::da::LoadFull(DataSetType* dataset, DataSource* datasource)
107 //{
108 // assert(dataset);
109 // assert(datasource);
110 //
111 // std::auto_ptr<DataSourceTransactor> transactor(datasource->getTransactor());
112 //
113 // LoadFull(dataset, transactor.get());
114 //}
115 
116 void te::da::LoadProperties(te::da::DataSetType* dataset, const std::string& datasourceId)
117 {
118  assert(dataset);
119  assert(!datasourceId.empty());
120 
121  //DataSourcePtr datasource(te::da::DataSourceManager::getInstance().get(datasourceId));
122  DataSourcePtr datasource(te::da::DataSourceManager::getInstance().find(datasourceId));
123 
124  if(datasource.get() == 0)
125  return;
126 
127  boost::ptr_vector<te::dt::Property> properties = datasource->getProperties(dataset->getName());
128 
129  dataset->add(properties);
130 }
131 
132 te::gm::Envelope* te::da::GetExtent(const std::string& datasetName,
133  const std::string& propertyName,
134  const std::string& datasourceId)
135 {
136  assert(!datasourceId.empty());
137 
138  //DataSourcePtr datasource(te::da::DataSourceManager::getInstance().get(datasourceId));
139  DataSourcePtr datasource(te::da::DataSourceManager::getInstance().find(datasourceId));
140 
141  if(datasource.get() == 0)
142  throw Exception(TR_DATAACCESS("Could not retrieve data source in order to search for a property extent!"));
143 
144  std::auto_ptr<te::gm::Envelope> mbr(datasource->getExtent(datasetName, propertyName));
145 
146  return mbr.release();
147 }
148 
149 void te::da::GetDataSetNames(std::vector<std::string>& datasetNames, const std::string& datasourceId)
150 {
151  assert(!datasourceId.empty());
152 
153  DataSourcePtr ds(te::da::DataSourceManager::getInstance().find(datasourceId));
154  if(ds.get() == 0)
155  return;
156 
157  datasetNames = ds->getDataSetNames();
158 }
159 
160 std::string te::da::GetDataSetCategoryName(int category)
161 {
162  switch(category)
163  {
165  return "Unknown";
166 
167  case te::da::TABLE_TYPE:
168  return "Table";
169 
171  return "System";
172 
173  case te::da::VIEW_TYPE:
174  return "View";
175 
176  case te::da::QUERY_TYPE:
177  return "Query";
178 
179  case te::da::INDEX_TYPE:
180  return "Index";
181 
183  return "Sequence";
184 
186  return "Trigger";
187 
189  return "Regular File";
190 
191  default:
192  return "";
193  }
194 }
195 
196 bool te::da::HasDataSet(const std::string& datasourceId)
197 {
198  assert(!datasourceId.empty());
199 
200  //DataSourcePtr datasource(te::da::DataSourceManager::getInstance().get(datasourceId));
201  DataSourcePtr datasource(te::da::DataSourceManager::getInstance().find(datasourceId));
202 
203  if(datasource.get() == 0)
204  return false;
205 
206  return datasource->hasDataSets();
207 }
208 
209 te::da::DataSet* te::da::GetDataSet(const std::string& name, const std::string& datasourceId)
210 {
211  assert(!datasourceId.empty());
212 
213  //DataSourcePtr datasource(te::da::DataSourceManager::getInstance().get(datasourceId));
214 
215  DataSourcePtr datasource(te::da::DataSourceManager::getInstance().find(datasourceId));
216  if(datasource.get() == 0)
217  return false;
218 
219  std::auto_ptr<DataSet> dataset(datasource->getDataSet(name));
220 
221  return dataset.release();
222 }
223 
224 te::da::DataSetType* te::da::GetDataSetType(const std::string& name, const std::string& datasourceId)
225 {
226  assert(!datasourceId.empty());
227 
228  DataSourcePtr datasource(te::da::DataSourceManager::getInstance().find(datasourceId));
229  if(datasource.get() == 0)
230  return false;
231 
232  std::auto_ptr<DataSetType> datasettype(datasource->getDataSetType(name));
233 
234  return datasettype.release();
235 }
236 
237 //te::da::DataSourcePtr te::da::GetDataSource(const std::string& datasourceId)
238 //{
239 // assert(!datasourceId.empty());
240 //
241 // DataSourcePtr datasource(te::da::DataSourceManager::getInstance().get(datasourceId));
242 //
243 // if(datasource.get() == 0)
244 // {
245 // DataSourceInfoPtr dsinfo = te::da::DataSourceInfoManager::getInstance().get(datasourceId);
246 //
247 // if(dsinfo.get() == 0)
248 // throw Exception(TR_DATAACCESS("Could not find data source!"));
249 //
250 // te::da::DataSourceManager::getInstance().open(datasourceId, dsinfo->getAccessDriver(), dsinfo->getConnInfo());
251 //
252 // datasource = te::da::DataSourceManager::getInstance().get(datasourceId);
253 // }
254 //
255 // return datasource;
256 //}
257 
258 te::da::DataSourcePtr te::da::GetDataSource(const std::string& datasourceId, const bool opened)
259 {
260  assert(!datasourceId.empty());
261 
262  DataSourcePtr datasource(te::da::DataSourceManager::getInstance().find(datasourceId));
263 
264  if(datasource.get() == 0)
265  {
267 
268  if(dsinfo.get() == 0)
269  throw Exception(TR_DATAACCESS("Could not find data source!"));
270 
271  datasource = te::da::DataSourceManager::getInstance().make(datasourceId, dsinfo->getAccessDriver());
272 
273  datasource->setConnectionInfo(dsinfo->getConnInfo());
274  }
275 
276  if(opened && !datasource->isOpened())
277  datasource->open();
278 
279  return datasource;
280 }
281 
283 {
284  assert (type);
285 
286  std::vector<size_t> ppos;
287  std::vector<int> ptypes;
288  std::vector<std::string> pnames;
289  std::vector<size_t>::iterator it;
290  set = new ObjectIdSet();
291 
292  GetOIDPropertyPos(type, ppos);
293 
294  for(it=ppos.begin(); it!=ppos.end(); ++it)
295  {
296  te::dt::Property* prop = type->getProperty(*it);
297  ptypes.push_back(prop->getType());
298  pnames.push_back(prop->getName());
299  }
300 
301  for(size_t i=0; i<ppos.size(); i++)
302  set->addProperty(pnames[i], ppos[i], ptypes[i]);
303 }
304 
305 void te::da::GetOIDPropertyNames(const te::da::DataSetType* type, std::vector<std::string>& pnames)
306 {
307  assert(type);
308 
309  // Looking for the primary key properties
310  PrimaryKey* pk = type->getPrimaryKey();
311  if(pk != 0)
312  {
313  const std::vector<te::dt::Property*>& pkProperties = pk->getProperties();
314 
315  for(std::size_t i = 0; i < pkProperties.size(); ++i)
316  pnames.push_back(pkProperties[i]->getName());
317 
318  return;
319  }
320 
321  // Looking for the unique key properties
322  if(type->getNumberOfUniqueKeys() > 0)
323  {
324  for(std::size_t i = 0; i < type->getNumberOfUniqueKeys(); ++i)
325  {
326  UniqueKey* uk = type->getUniqueKey(i);
327 
328  const std::vector<te::dt::Property*>& ukProperties = uk->getProperties();
329 
330  for(std::size_t j = 0; j < ukProperties.size(); ++j)
331  pnames.push_back(ukProperties[j]->getName());
332  }
333 
334  return;
335  }
336 
337  // Here, the data set type do not have primary key properties or unique key properties.
338  // So, use all the non geometric properties.
339  const std::vector<te::dt::Property*>& props = type->getProperties();
340  for(std::size_t i = 0; i < props.size(); ++i)
341  {
342  if(props[i]->getType() == te::dt::GEOMETRY_TYPE || props[i]->getType() == te::dt::RASTER_TYPE ||
343  props[i]->getType() == te::dt::FLOAT_TYPE || props[i]->getType() == te::dt::DOUBLE_TYPE ||
344  props[i]->getType() == te::dt::BYTE_ARRAY_TYPE || props[i]->getType() == te::dt::NUMERIC_TYPE)
345  continue;
346 
347  pnames.push_back(props[i]->getName());
348  }
349 }
350 
351 void te::da::GetOIDPropertyPos(const te::da::DataSetType* type, std::vector<std::size_t>& ppos)
352 {
353  assert(type);
354 
355  std::vector<std::string> oidprops;
356  GetOIDPropertyNames(type, oidprops);
357 
358  for(std::size_t i = 0; i < oidprops.size(); ++i)
359  ppos.push_back(type->getPropertyPosition(oidprops[i]));
360 }
361 
363 {
364  assert(dataset);
365  assert(type);
366 
367  std::vector<std::string> oidprops;
368  GetOIDPropertyNames(type, oidprops);
369 
370  return te::da::GenerateOIDSet(dataset, oidprops);
371 }
372 
373 te::da::ObjectIdSet* te::da::GenerateOIDSet(te::da::DataSet* dataset, const std::vector<std::string>& names)
374 {
375  assert(dataset);
376  assert(!names.empty());
377 
378  ObjectIdSet* oids = new ObjectIdSet;
379 
380  for(std::size_t i = 0; i < names.size(); ++i)
381  {
382  std::size_t pos = GetPropertyPos(dataset, names[i]);
383  assert(pos != std::string::npos);
384  oids->addProperty(names[i], pos, dataset->getPropertyDataType(pos));
385  }
386 
387  while(dataset->moveNext())
388  oids->add(GenerateOID(dataset, names));
389 
390  return oids;
391 }
392 
393 te::da::ObjectId* te::da::GenerateOID(te::da::DataSet* dataset, const std::vector<std::string>& names)
394 {
395  assert(dataset);
396  assert(!names.empty());
397 
398  ObjectId* oid = new ObjectId;
399 
400  for(std::size_t i = 0; i < names.size(); ++i)
401  {
402  if(!dataset->isNull(i))
403  oid->addValue(dataset->getValue(names[i]).release());
404  }
405 
406  return oid;
407 }
408 
410 {
411  assert(dataset);
412 
413  const std::size_t np = dataset->getNumProperties();
414 
415  for(std::size_t i = 0; i != np; ++i)
416  {
417  int pdt = dataset->getPropertyDataType(i);
418 
419  if(pdt == te::dt::GEOMETRY_TYPE || pdt == te::dt::RASTER_TYPE)
420  {
421  return i;
422  }
423  }
424 
425  return std::string::npos;
426 }
427 
428 std::size_t te::da::GetFirstPropertyPos(const te::da::DataSet* dataset, int datatype)
429 {
430  assert(dataset);
431 
432  const std::size_t np = dataset->getNumProperties();
433 
434  for(std::size_t i = 0; i != np; ++i)
435  {
436  int pdt = dataset->getPropertyDataType(i);
437 
438  if(pdt == datatype)
439  {
440  return i;
441  }
442  }
443 
444  return std::string::npos;
445 }
446 
447 std::size_t te::da::GetPropertyPos(const DataSet* dataset, const std::string& name)
448 {
449  assert(dataset);
450 
451  const std::size_t np = dataset->getNumProperties();
452 
453  for(std::size_t i = 0; i != np; ++i)
454  {
455  std::string pname = dataset->getPropertyName(i);
456 
457  if(boost::iequals(pname, name))
458  return i;
459  }
460 
461  return std::string::npos;
462 }
463 
464 std::size_t te::da::GetPropertyPos(const DataSetType* dt, const std::string& name)
465 {
466  assert(dt);
467 
468  const std::size_t np = dt->getProperties().size();
469 
470  for(std::size_t i = 0; i != np; ++i)
471  {
472  std::string pname = dt->getProperty(i)->getName();
473 
474  if(boost::iequals(pname, name))
475  return i;
476  }
477 
478  return std::string::npos;
479 }
480 
482 {
483  assert(dt);
484 
485  const std::size_t np = dt->size();
486 
487  for(std::size_t i = 0; i != np; ++i)
488  {
489  te::dt::Property* p = dt->getProperty(i);
490 
491  assert(p);
492 
493  int pdt = p->getType();
494 
495  if(pdt == te::dt::GEOMETRY_TYPE || pdt == te::dt::RASTER_TYPE)
496  {
497  return p;
498  }
499  }
500 
501  return 0;
502 }
503 
505 {
507 
508  if(p)
509  {
510  return static_cast<te::gm::GeometryProperty*>(p);
511  }
512  else
513  {
514  return 0;
515  }
516 }
517 
519 {
521 
522  assert(p);
523 
524  return static_cast<te::rst::RasterProperty*>(p);
525 }
526 
527 //te::da::DataSetType* te::da::CreateDataSetType(const te::da::DataSet* dataset)
528 //{
529 // assert(dataset);
530 //
531 // te::da::DataSetType* dt = new DataSetType("");
532 //
533 // const std::size_t np = dataset->getNumProperties();
534 //
535 // for(std::size_t i = 0; i != np; ++i)
536 // {
537 // const te::dt::Property* p = dataset->getProperty(i);
538 //
539 // dt->add(p->clone());
540 // }
541 //
542 // return dt;
543 //}
544 
546  std::vector<std::string>& pnames,
547  std::vector<int>& ptypes)
548 {
549  assert(dt);
550 
551  for(std::size_t i = 0; i != dt->size(); ++i)
552  {
553  const te::dt::Property* p = dt->getProperty(i);
554 
555  pnames.push_back(p->getName());
556  ptypes.push_back(p->getType());
557  }
558 }
559 
560 void te::da::GetPropertyInfo(const DataSet* const dataset,
561  std::vector<std::string>& pnames,
562  std::vector<int>& ptypes)
563 {
564  assert(dataset);
565 
566  for(std::size_t i = 0; i != dataset->getNumProperties(); ++i)
567  {
568  pnames.push_back(dataset->getPropertyName(i));
569  ptypes.push_back(dataset->getPropertyDataType(i));
570  }
571 }
572 
573 void te::da::Create(DataSource* ds, DataSetType* dt, DataSet* d, std::size_t limit)
574 {
575  std::map<std::string, std::string> options;
576 
577  Create(ds, dt, d, options, limit);
578 }
579 
581  DataSetType* dt,
582  DataSet* d,
583  const std::map<std::string, std::string>& options,
584  std::size_t limit)
585 {
586  ds->createDataSet(dt, options);
587 
588  ds->add(dt->getName(), d, options, limit);
589 }
590 
592 {
593  assert(ds);
594  assert(converter);
595 
596  DataSetType* type = converter->getResult();
597  assert(type);
598 
599  const std::vector<std::vector<std::size_t> >& indexes = converter->getConvertedPropertyIndexes();
600  const std::vector<AttributeConverter>& funcs = converter->getConverters();
601 
602  assert((type->size() == indexes.size()) && (type->size() == funcs.size()));
603 
604  DataSetAdapter* adapter = new DataSetAdapter(ds, isOwner);
605 
606  for(std::size_t i = 0; i < type->size(); ++i)
607  {
608  te::dt::Property* p = type->getProperty(i);
609  assert(p);
610 
611  adapter->add(p->getName(), p->getType(), indexes[i], funcs[i]);
612  }
613 
614  return adapter;
615 }
616 
618 {
619  std::string valueNames("(");
620 
621  const std::size_t np = dt->size();
622 
623  for(std::size_t i = 0; i != np; ++i)
624  {
625  if(i != 0)
626  valueNames += ",";
627 
628  valueNames += dt->getProperty(i)->getName();
629  }
630 
631  valueNames += ")";
632 
633  return valueNames;
634 }
635 
636 std::string te::da::GetSQLValueNames(const te::da::DataSet* dataset)
637 {
638  std::string valueNames("(");
639 
640  const std::size_t np = dataset->getNumProperties();
641 
642  for(std::size_t i = 0; i != np; ++i)
643  {
644  if(i != 0)
645  valueNames += ",";
646 
647  valueNames += dataset->getPropertyName(i);
648  }
649 
650  valueNames += ")";
651 
652  return valueNames;
653 }
654 
655 std::vector<int> te::da::GetPropertyDataTypes(const te::da::DataSet* dataset)
656 {
657  std::vector<int> properties;
658 
659  const std::size_t np = dataset->getNumProperties();
660 
661  for(std::size_t i = 0; i != np; ++i)
662  {
663  properties.push_back(dataset->getPropertyDataType(i));
664  }
665 
666  return properties;
667 }
668 
669 std::auto_ptr<te::da::Expression> te::da::BuildSpatialOp(Expression* e1,
670  Expression* e2,
672 
673 {
674  std::auto_ptr<te::da::Expression> op;
675  switch(r)
676  {
677  case te::gm::INTERSECTS:
678  op.reset(new ST_Intersects(e1, e2));
679  break;
680 
681  case te::gm::DISJOINT:
682  op.reset(new ST_Disjoint(e1, e2));
683  break;
684 
685  case te::gm::TOUCHES:
686  op.reset(new ST_Touches(e1, e2));
687  break;
688 
689  case te::gm::OVERLAPS:
690  op.reset(new ST_Overlaps(e1, e2));
691  break;
692 
693  case te::gm::CROSSES:
694  op.reset(new ST_Crosses(e1, e2));
695  break;
696 
697  case te::gm::WITHIN:
698  op.reset(new ST_Within(e1, e2));
699  break;
700 
701  case te::gm::CONTAINS:
702  op.reset(new ST_Contains(e1, e2));
703  break;
704 
705  case te::gm::EQUALS:
706  op.reset(new ST_Equals(e1, e2));
707  break;
708 
709  default:
710  throw;
711  }
712 
713  return op;
714 }
715 
716 std::auto_ptr<te::da::Fields> te::da::BuildFields(const std::vector<std::string>& properties)
717 {
718  std::auto_ptr<Fields> fields(new Fields);
719 
720  for(std::size_t i = 0; i < properties.size(); ++i)
721  fields->push_back(new te::da::Field(properties[i]));
722 
723  return fields;
724 }
725 
726 std::auto_ptr<te::da::Select> te::da::BuildSelect(const std::string& dsname)
727 {
728  return BuildSelect(dsname, "*");
729 }
730 
731 std::auto_ptr<te::da::Select> te::da::BuildSelect(const std::string& dsname, const std::string& propertyName)
732 {
733  std::vector<std::string> p;
734  p.push_back(propertyName);
735 
736  return BuildSelect(dsname, p);
737 }
738 
739 std::auto_ptr<te::da::Select> te::da::BuildSelect(const std::string& dsname, const std::vector<std::string>& properties)
740 {
741  // Fields
742  std::auto_ptr<Fields> fields = BuildFields(properties);
743 
744  // From
745  FromItem* fi = new DataSetName(dsname);
746  From* from = new From;
747  from->push_back(fi);
748 
749  // Select
750  std::auto_ptr<Select> select(new Select(fields.release(), from));
751 
752  return select;
753 }
754 
755 std::auto_ptr<te::da::Select> te::da::BuildSelect(const std::string& dsname,
756  const std::vector<std::string>& properties,
757  const std::string& geometryProperty,
758  const te::gm::Envelope* e,
759  int srid,
761 {
762  // Fields
763  std::auto_ptr<Fields> fields = BuildFields(properties);
764 
765  // Adding the geometry property
766  fields->push_back(new Field(geometryProperty));
767 
768  // From
769  FromItem* fi = new DataSetName(dsname);
770  From* from = new From;
771  from->push_back(fi);
772 
773  PropertyName* geomPropertyName = new PropertyName(geometryProperty);
774  LiteralEnvelope* lenv = new LiteralEnvelope(*e, srid);
775 
776  // The spatial restriction
777  std::auto_ptr<Expression> spatialOp = BuildSpatialOp(geomPropertyName, lenv, r);
778 
779  // Where
780  te::da::Where* filter = new Where(spatialOp.release());
781 
782  // Select
783  std::auto_ptr<Select> select(new Select(fields.release(), from, filter));
784 
785  return select;
786 }
787 
788 std::auto_ptr<te::da::Select> te::da::BuildSelect(const std::string& dsname,
789  const std::vector<std::string>& properties,
790  const std::string& geometryProperty,
791  te::gm::Geometry* g,
793 {
794  // Fields
795  std::auto_ptr<Fields> fields = BuildFields(properties);
796 
797  // Adding the geometry property
798  fields->push_back(new Field(geometryProperty));
799 
800  // From
801  FromItem* fi = new DataSetName(dsname);
802  From* from = new From;
803  from->push_back(fi);
804 
805  PropertyName* geomPropertyName = new PropertyName(geometryProperty);
806  LiteralGeom* lgeom = new LiteralGeom(g);
807 
808  // The spatial restriction
809  std::auto_ptr<Expression> spatialOp = BuildSpatialOp(geomPropertyName, lgeom, r);
810 
811  // Where
812  te::da::Where* filter = new Where(spatialOp.release());
813 
814  // Select
815  std::auto_ptr<Select> select(new Select(fields.release(), from, filter));
816 
817  return select;
818 }
819 
820 std::auto_ptr<te::da::Select> te::da::BuildSelect(const std::string& dsname,
821  const std::vector<std::string>& properties,
822  const ObjectIdSet* oids)
823 {
824  // OIDS restriction
825  Expression* exp = oids->getExpression();
826  assert(exp);
827 
828  // Where
829  Where* filter = new Where(exp);
830 
831  // Fields
832  std::auto_ptr<Fields> fields = BuildFields(properties);
833 
834  // Adding the oids properties case not included
835  const std::vector<std::string>& oidsProperties = oids->getPropertyNames();
836  for(std::size_t i = 0; i < oidsProperties.size(); ++i)
837  {
838  const std::string& oidPropertyName = oidsProperties[i];
839 
840  bool alreadyIncluded = false;
841 
842  for(std::size_t j = 0; j < properties.size(); ++j)
843  {
844  if(oidPropertyName == properties[j])
845  {
846  alreadyIncluded = true;
847  break;
848  }
849  }
850 
851  if(!alreadyIncluded)
852  fields->push_back(new Field(oidPropertyName));
853  }
854 
855  // From
856  FromItem* fromItem = new DataSetName(dsname);
857  From* from = new From;
858  from->push_back(fromItem);
859 
860  // Select
861  std::auto_ptr<Select> select(new Select(fields.release(), from, filter));
862 
863  return select;
864 }
865 
866 int te::da::GetPropertyIndex(te::da::DataSet* dataSet, const std::string propName)
867 {
868  int index = 0;
869 
870  for(std::size_t i = 0; i < dataSet->getNumProperties(); ++i)
871  {
872  if(propName == dataSet->getPropertyName(i))
873  {
874  index = i;
875  return index;
876  }
877  }
878  return -1;
879 }
Spatial overlaps operator.
Definition: ST_Overlaps.h:46
virtual std::string getPropertyName(std::size_t i) const =0
It returns the property name at position pos.
TEDATAACCESSEXPORT std::auto_ptr< Expression > BuildSpatialOp(Expression *e1, Expression *e2, te::gm::SpatialRelation r)
Definition: Utils.cpp:669
TEDATAACCESSEXPORT void GetDataSetNames(std::vector< std::string > &datasetNames, const std::string &datasourceId)
Definition: Utils.cpp:149
TEDATAACCESSEXPORT ObjectIdSet * GenerateOIDSet(DataSet *dataset, const DataSetType *type)
Definition: Utils.cpp:362
virtual std::size_t getNumProperties() const =0
It returns the number of properties that composes an item of the dataset.
Spatial within operator.
Definition: ST_Within.h:46
This class represents a set of unique ids created in the same context. i.e. from the same data set...
Definition: ObjectIdSet.h:53
Spatial Disjoint operator.
Definition: ST_Disjoint.h:46
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:424
A class that models the name of a dataset used in a From clause.
Definition: DataSetName.h:43
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:409
TEDATAACCESSEXPORT te::rst::RasterProperty * GetFirstRasterProperty(const DataSetType *dt)
Definition: Utils.cpp:518
Property * getProperty(std::size_t i) const
It returns the i-th property.
std::size_t size() const
It returns the number of properties of the CompositeProperty.
TEDATAACCESSEXPORT std::auto_ptr< Fields > BuildFields(const std::vector< std::string > &properties)
Definition: Utils.cpp:716
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:258
An converter for DataSetType.
TEDATAACCESSEXPORT te::gm::GeometryProperty * GetFirstGeomProperty(const DataSetType *dt)
Definition: Utils.cpp:504
A Select models a query to be used when retrieving data from a DataSource.
Definition: Select.h:65
TEDATAACCESSEXPORT DataSet * GetDataSet(const std::string &name, const std::string &datasourceId)
Definition: Utils.cpp:209
Spatial intersects operator.
Definition: ST_Intersects.h:46
The Field class can be used to model an expression that takes part of the output items of a SELECT...
Definition: Field.h:50
void addValue(te::dt::AbstractData *data)
It adds a property value to uniquely identify a data set element.
Definition: ObjectId.cpp:61
virtual bool moveNext()=0
It moves the internal pointer to the next item of the collection.
It describes a primary key (pk) constraint.
Definition: PrimaryKey.h:52
boost::ptr_vector< FromItem > From
It models the FROM clause for a query.
Definition: From.h:37
TEDATAACCESSEXPORT void GetEmptyOIDSet(const DataSetType *type, ObjectIdSet *&set)
Returns an empty ObjectIdSet, with the definitions of fields that compose it.
Definition: Utils.cpp:282
void add(const std::string &newPropertyName, int newPropertyType, const std::vector< std::size_t > &adaptedPropertyPos, AttributeConverter conv)
const std::string & getName() const
It returns the property name.
Definition: Property.h:126
void add(Constraint *c)
It adds a new constraint.
Expression * getExpression() const
It returns the expression that can be used to retrieve the data set that contains the all indentified...
Definition: ObjectIdSet.cpp:77
A class that models the name of any property of an object.
Definition: PropertyName.h:50
virtual bool isNull(std::size_t i) const =0
It checks if the attribute value is NULL.
TEDATAACCESSEXPORT te::gm::Envelope * GetExtent(const std::string &datasetName, const std::string &propertyName, const std::string &datasourceId)
Definition: Utils.cpp:132
UniqueKey * getUniqueKey(std::size_t i) const
It returns the i-th unique key.
Definition: DataSetType.h:294
const std::vector< AttributeConverter > & getConverters() const
SpatialRelation
Spatial relations between geometric objects.
Definition: Enums.h:122
const std::vector< te::dt::Property * > & getProperties() const
It returns the properties that take part of the primary key.
Definition: PrimaryKey.h:109
TEDATAACCESSEXPORT void GetPropertyInfo(const DataSetType *const dt, std::vector< std::string > &pnames, std::vector< int > &ptypes)
Definition: Utils.cpp:545
A class that models a literal for Geometry values.
Definition: LiteralGeom.h:46
TEDATAACCESSEXPORT te::dt::Property * GetFirstSpatialProperty(const DataSetType *dt)
Definition: Utils.cpp:481
boost::ptr_vector< Field > Fields
Fields is just a boost::ptr_vector of Field pointers.
Definition: Fields.h:37
const std::vector< std::string > & getPropertyNames() const
It returns the property names used to generated the oids.
An abstract class that models a source of data in a query.
Definition: FromItem.h:50
TEDATAACCESSEXPORT ObjectId * GenerateOID(DataSet *dataset, const std::vector< std::string > &names)
Definition: Utils.cpp:393
Spatial contains operator.
Definition: ST_Contains.h:46
Utility functions for the data access module.
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
Definition: Geometry.h:73
TEDATAACCESSEXPORT void GetOIDPropertyPos(const DataSetType *type, std::vector< std::size_t > &ppos)
Definition: Utils.cpp:351
virtual int getPropertyDataType(std::size_t i) const =0
It returns the underlying data type of the property at position pos.
TEDATAACCESSEXPORT std::size_t GetFirstPropertyPos(const te::da::DataSet *dataset, int datatype)
Definition: Utils.cpp:428
TEDATAACCESSEXPORT std::vector< int > GetPropertyDataTypes(const te::da::DataSet *dataset)
Definition: Utils.cpp:655
const std::vector< Property * > & getProperties() const
It returns the list of properties describing the CompositeProperty.
A class that models a literal for Envelope values.
std::size_t getPropertyPosition(const std::string &name) const
It returns the property position based on its name.
It describes a unique key (uk) constraint.
Definition: UniqueKey.h:53
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< DataSource > DataSourcePtr
Definition: DataSource.h:1395
Property * findFirstPropertyOfType(const int t) const
returns the first property of the given data type. Caller doesn&#39;t take ownership of the returned poin...
TEDATAACCESSEXPORT DataSetType * GetDataSetType(const std::string &name, const std::string &datasourceId)
Definition: Utils.cpp:224
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:450
This class represents an unique id for a data set element.
Definition: ObjectId.h:47
#define TR_DATAACCESS(message)
It marks a string in order to get translated. This is a special mark used in the DataAccess module of...
Definition: Config.h:95
Spatial crosses operator.
Definition: ST_Crosses.h:46
TEDATAACCESSEXPORT bool HasDataSet(const std::string &datasourceId)
Definition: Utils.cpp:196
TEDATAACCESSEXPORT std::auto_ptr< Select > BuildSelect(const std::string &dsname)
Definition: Utils.cpp:726
TEDATAACCESSEXPORT std::size_t GetPropertyPos(const DataSet *dataset, const std::string &name)
Definition: Utils.cpp:447
An abstract class for data providers like a DBMS, Web Services or a regular file. ...
Definition: DataSource.h:116
A class that models the description of a dataset.
Definition: DataSetType.h:72
TEDATAACCESSEXPORT int GetPropertyIndex(te::da::DataSet *dataSet, const std::string propName)
Definition: Utils.cpp:866
TEDATAACCESSEXPORT std::string GetDataSetCategoryName(int category)
Definition: Utils.cpp:160
Raster property.
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:573
It models a property definition.
Definition: Property.h:59
TEDATAACCESSEXPORT void GetOIDPropertyNames(const DataSetType *type, std::vector< std::string > &pnames)
Definition: Utils.cpp:305
This is an abstract class that models a query expression.
Definition: Expression.h:47
TEDATAACCESSEXPORT void LoadProperties(te::da::DataSetType *dataset, const std::string &datasourceId)
Definition: Utils.cpp:116
int getType() const
It returns the property data type.
Definition: Property.h:143
TEDATAACCESSEXPORT std::string GetSQLValueNames(const DataSetType *dt)
Definition: Utils.cpp:617
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
static DataSourceManager & getInstance()
It returns a reference to the singleton instance.
TEDATAACCESSEXPORT void LoadFull(te::da::DataSetType *dataset, const std::string &datasourceId)
Definition: Utils.cpp:66
PrimaryKey * getPrimaryKey() const
It returns the primary key associated to the dataset type.
Definition: DataSetType.h:214
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
boost::shared_ptr< DataSourceInfo > DataSourceInfoPtr
A class that can be used to model a filter expression that can be applied to a query.
Definition: Where.h:47
Geometric property.
const std::vector< te::dt::Property * > & getProperties() const
It returns the properties that form the unique key.
Definition: UniqueKey.h:110
const std::vector< std::vector< std::size_t > > & getConvertedPropertyIndexes() const
std::size_t getNumberOfUniqueKeys() const
It returns the number of unique keys defined for the dataset type.
Definition: DataSetType.h:269
A dataset is the unit of information manipulated by the data access module of TerraLib.
Definition: DataSet.h:111
Spatial equals operator.
Definition: ST_Equals.h:46
TEDATAACCESSEXPORT DataSetAdapter * CreateAdapter(DataSet *ds, DataSetTypeConverter *converter, bool isOwner=false)
Definition: Utils.cpp:591
Spatial touches operator.
Definition: ST_Touches.h:46
An adapter for DataSet.