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"
58 #include "../Exception.h"
65 #include <boost/algorithm/string.hpp>
70 assert(!datasourceId.empty());
75 if(datasource.get() == 0)
78 if(dataset->
size() == 0)
80 boost::ptr_vector<te::dt::Property> properties = datasource->getProperties(dataset->
getName());
82 dataset->
add(properties);
120 assert(!datasourceId.empty());
125 if(datasource.get() == 0)
128 boost::ptr_vector<te::dt::Property> properties = datasource->getProperties(dataset->
getName());
130 dataset->
add(properties);
134 const std::string& propertyName,
135 const std::string& datasourceId)
137 assert(!datasourceId.empty());
142 if(datasource.get() == 0)
143 throw Exception(
TE_TR(
"Could not retrieve data source in order to search for a property extent!"));
145 std::auto_ptr<te::gm::Envelope> mbr(datasource->getExtent(datasetName, propertyName));
147 return mbr.release();
152 assert(!datasourceId.empty());
158 datasetNames = ds->getDataSetNames();
190 return "Regular File";
199 assert(!datasourceId.empty());
204 if(datasource.get() == 0)
207 return datasource->hasDataSets();
212 assert(!datasourceId.empty());
217 if(datasource.get() == 0)
220 std::auto_ptr<DataSet> dataset(datasource->getDataSet(name));
222 return dataset.release();
227 assert(!datasourceId.empty());
230 if(datasource.get() == 0)
233 std::auto_ptr<DataSetType> datasettype(datasource->getDataSetType(name));
235 return datasettype.release();
261 assert(!datasourceId.empty());
265 if(datasource.get() == 0)
269 if(dsinfo.get() == 0)
270 throw Exception(
TE_TR(
"Could not find data source!"));
274 datasource->setConnectionInfo(dsinfo->getConnInfo());
277 if(opened && !datasource->isOpened())
287 std::vector<size_t> ppos;
288 std::vector<int> ptypes;
289 std::vector<std::string> pnames;
290 std::vector<size_t>::iterator it;
295 for(it=ppos.begin(); it!=ppos.end(); ++it)
298 ptypes.push_back(prop->
getType());
299 pnames.push_back(prop->
getName());
302 for(
size_t i=0; i<ppos.size(); i++)
314 const std::vector<te::dt::Property*>& pkProperties = pk->
getProperties();
316 for(std::size_t i = 0; i < pkProperties.size(); ++i)
317 pnames.push_back(pkProperties[i]->getName());
329 const std::vector<te::dt::Property*>& ukProperties = uk->
getProperties();
331 for(std::size_t j = 0; j < ukProperties.size(); ++j)
332 pnames.push_back(ukProperties[j]->getName());
340 const std::vector<te::dt::Property*>& props = type->
getProperties();
341 for(std::size_t i = 0; i < props.size(); ++i)
348 pnames.push_back(props[i]->getName());
356 std::vector<std::string> oidprops;
359 for(std::size_t i = 0; i < oidprops.size(); ++i)
368 std::vector<std::string> oidprops;
377 assert(!names.empty());
381 for(std::size_t i = 0; i < names.size(); ++i)
385 if(pos == std::string::npos)
386 throw Exception(
TE_TR(
"Primary Key ") + names[i] +
TE_TR(
" not found!"));
400 assert(!names.empty());
404 for(std::size_t i = 0; i < names.size(); ++i)
419 for(std::size_t i = 0; i != np; ++i)
429 return std::string::npos;
438 for(std::size_t i = 0; i != np; ++i)
448 return std::string::npos;
457 for(std::size_t i = 0; i != np; ++i)
461 if(boost::iequals(pname, name))
465 return std::string::npos;
474 for(std::size_t i = 0; i != np; ++i)
478 if(boost::iequals(pname, name))
482 return std::string::npos;
489 const std::size_t np = dt->
size();
491 for(std::size_t i = 0; i != np; ++i)
550 std::vector<std::string>& pnames,
551 std::vector<int>& ptypes)
555 for(std::size_t i = 0; i != dt->
size(); ++i)
559 pnames.push_back(p->
getName());
560 ptypes.push_back(p->
getType());
565 std::vector<std::string>& pnames,
566 std::vector<int>& ptypes)
579 std::map<std::string, std::string> options;
581 Create(ds, dt, d, options, limit);
587 const std::map<std::string, std::string>& options,
604 const std::vector<AttributeConverter>& funcs = converter->
getConverters();
606 assert((type->
size() == indexes.size()) && (type->
size() == funcs.size()));
610 for(std::size_t i = 0; i < type->
size(); ++i)
623 std::string valueNames(
"(");
625 const std::size_t np = dt->
size();
627 for(std::size_t i = 0; i != np; ++i)
642 std::string valueNames(
"(");
646 for(std::size_t i = 0; i != np; ++i)
661 std::vector<int> properties;
665 for(std::size_t i = 0; i != np; ++i)
678 std::auto_ptr<te::da::Expression> op;
722 std::auto_ptr<Fields> fields(
new Fields);
724 for(std::size_t i = 0; i < properties.size(); ++i)
735 std::auto_ptr<te::da::Select>
te::da::BuildSelect(
const std::string& dsname,
const std::string& propertyName)
737 std::vector<std::string> p;
738 p.push_back(propertyName);
743 std::auto_ptr<te::da::Select>
te::da::BuildSelect(
const std::string& dsname,
const std::vector<std::string>& properties)
746 std::auto_ptr<Fields> fields =
BuildFields(properties);
754 std::auto_ptr<Select> select(
new Select(fields.release(), from));
760 const std::vector<std::string>& properties,
761 const std::string& geometryProperty,
767 std::auto_ptr<Fields> fields =
BuildFields(properties);
770 fields->push_back(
new Field(geometryProperty));
781 std::auto_ptr<Expression> spatialOp =
BuildSpatialOp(geomPropertyName, lenv, r);
787 std::auto_ptr<Select> select(
new Select(fields.release(), from, filter));
793 const std::vector<std::string>& properties,
794 const std::string& geometryProperty,
799 std::auto_ptr<Fields> fields =
BuildFields(properties);
802 fields->push_back(
new Field(geometryProperty));
813 std::auto_ptr<Expression> spatialOp =
BuildSpatialOp(geomPropertyName, lgeom, r);
819 std::auto_ptr<Select> select(
new Select(fields.release(), from, filter));
825 const std::vector<std::string>& properties,
836 std::auto_ptr<Fields> fields =
BuildFields(properties);
840 for(std::size_t i = 0; i < oidsProperties.size(); ++i)
842 const std::string& oidPropertyName = oidsProperties[i];
844 bool alreadyIncluded =
false;
846 for(std::size_t j = 0; j < properties.size(); ++j)
848 if(oidPropertyName == properties[j])
850 alreadyIncluded =
true;
856 fields->push_back(
new Field(oidPropertyName));
862 from->push_back(fromItem);
865 std::auto_ptr<Select> select(
new Select(fields.release(), from, filter));
892 if(name[0] >= 0x30 && name[0] <= 0x39)
894 invalidChar =
"begin with a numeric character\n";
899 invalidChar +=
"begin with a invalid character: underscore _\n";
903 int ff = name.find(
" ");
906 invalidChar +=
"invalid character: blank space\n";
913 invalidChar +=
"invalid character: dot '.'\n";
920 invalidChar +=
"invalid character: mathematical symbol '*'\n";
927 invalidChar +=
"invalid character: mathematical symbol '/'\n";
934 invalidChar +=
"invalid character: parentheses '('\n";
941 invalidChar +=
"invalid character: parentheses ')'\n";
948 invalidChar +=
"invalid character: mathematical symbol '-'\n";
955 invalidChar +=
"invalid character: mathematical symbol '+'\n";
962 invalidChar +=
"invalid character: mathematical symbol '%'\n";
969 invalidChar +=
"invalid character: mathematical symbol '>'\n";
976 invalidChar +=
"invalid character: mathematical symbol '<'\n";
983 invalidChar +=
"invalid character: mathematical symbol '&'\n";
990 invalidChar +=
"invalid symbol: '$'\n";
997 invalidChar +=
"invalid symbol: ';'\n";
1001 ff = name.find(
"=");
1004 invalidChar +=
"invalid symbol: '='\n";
1008 ff = name.find(
"!");
1011 invalidChar +=
"invalid symbol: '!'\n";
1015 ff = name.find(
"?");
1018 invalidChar +=
"invalid symbol: '?'\n";
1022 ff = name.find(
"#");
1025 invalidChar +=
"invalid symbol: '#'\n";
1029 ff = name.find(
"¨");
1032 invalidChar +=
"invalid symbol: '¨'\n";
1036 ff = name.find(
",");
1039 invalidChar +=
"invalid symbol: ','\n";
1043 ff = name.find(
"/");
1046 invalidChar +=
"invalid symbol: '/'\n";
1050 ff = name.find(
"@");
1053 invalidChar +=
"invalid symbol: '@'\n";
1057 ff = name.find(
"{");
1060 invalidChar +=
"invalid symbol: '{'\n";
1064 ff = name.find(
"}");
1067 invalidChar +=
"invalid symbol: '}'\n";
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(
"³");
1078 for(
unsigned int i = 0; i < vecInvalidChars.size(); ++i)
1080 std::string invalidItem = vecInvalidChars[i];
1082 ff = name.find(invalidItem);
1085 invalidChar +=
"invalid symbol: '" + invalidItem +
"'\n";
1090 for(
unsigned int i = 0; i < name.size(); ++i)
1092 char value = name[i];
1095 invalidChar +=
"invalid symbol\n";
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" )
1109 invalidChar +=
"invalid name: using reserved word " + u +
"\n";
1115 if( (n==
"zone") || (n==
"comp") || (n==
"no") || (n==
"local") ||
1116 (n==
"level") || (n==
"long"))
1118 invalidChar +=
"invalid name: using reserved word " + n +
"\n";
TEDATAACCESSEXPORT DataSourcePtr GetDataSource(const std::string &datasourceId, const bool opened=true)
Search for a data source with the informed id in the DataSourceManager.
Property * getProperty(std::size_t i) const
It returns the i-th property.
TEDATAACCESSEXPORT te::rst::RasterProperty * GetFirstRasterProperty(const DataSetType *dt)
TEDATAACCESSEXPORT te::gm::Envelope * GetExtent(const std::string &datasetName, const std::string &propertyName, const std::string &datasourceId)
TEDATAACCESSEXPORT void LoadProperties(te::da::DataSetType *dataset, const std::string &datasourceId)
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.
The Field class can be used to model an expression that takes part of the output items of a SELECT...
TEDATAACCESSEXPORT void GetEmptyOIDSet(const DataSetType *type, ObjectIdSet *&set)
Returns an empty ObjectIdSet, with the definitions of fields that compose it.
TEDATAACCESSEXPORT ObjectId * GenerateOID(DataSet *dataset, const std::vector< std::string > &names)
Spatial intersects operator.
A class that models the name of a dataset used in a From clause.
boost::shared_ptr< DataSource > DataSourcePtr
std::string Convert2LCase(const std::string &value)
It converts a string to lower case.
TEDATAACCESSEXPORT std::auto_ptr< Select > BuildSelect(const std::string &dsname)
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...
TEDATAACCESSEXPORT std::auto_ptr< Expression > BuildSpatialOp(Expression *e1, Expression *e2, te::gm::SpatialRelation r)
A class that models the name of any property of an object.
A class that models the description of a dataset.
TEDATAACCESSEXPORT std::string GetDataSetCategoryName(int category)
std::size_t getNumberOfUniqueKeys() const
It returns the number of unique keys defined for the dataset type.
virtual void createDataSet(DataSetType *dt, const std::map< std::string, std::string > &options)
It creates the dataset schema definition in the target data source.
DataSetType * getResult() const
TEDATAACCESSEXPORT std::size_t GetPropertyPos(const DataSet *dataset, const std::string &name)
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.
PrimaryKey * getPrimaryKey() const
It returns the primary key associated to the dataset type.
std::string Convert2UCase(const std::string &value)
It converts a string to upper case.
SpatialRelation
Spatial relations between geometric objects.
TEDATAACCESSEXPORT std::string GetSQLValueNames(const DataSetType *dt)
void addValue(te::dt::AbstractData *data)
It adds a property value to uniquely identify a data set element.
An abstract class for data providers like a DBMS, Web Services or a regular file. ...
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.
#define TE_TR(message)
It marks a string in order to get translated.
Spatial crosses operator.
It models a property definition.
TEDATAACCESSEXPORT int GetPropertyIndex(te::da::DataSet *dataSet, const std::string propName)
This is an abstract class that models a query expression.
const std::vector< te::dt::Property * > & getProperties() const
It returns the properties that take part of the primary key.
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.
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.
An Envelope defines a 2D rectangular region.
This class represents a set of unique ids created in the same context. i.e. from the same data set...
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...
Spatial touches operator.
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.
boost::ptr_vector< Field > Fields
Fields is just a boost::ptr_vector of Field pointers.
TEDATAACCESSEXPORT DataSet * GetDataSet(const std::string &name, const std::string &datasourceId)
TEDATAACCESSEXPORT bool HasDataSet(const std::string &datasourceId)
A class that can be used to model a filter expression that can be applied to a query.
It describes a unique key (uk) constraint.
std::size_t size() const
It returns the number of properties of the CompositeProperty.
TEDATAACCESSEXPORT ObjectIdSet * GenerateOIDSet(DataSet *dataset, const DataSetType *type)
TEDATAACCESSEXPORT void GetDataSetNames(std::vector< std::string > &datasetNames, const std::string &datasourceId)
TEDATAACCESSEXPORT std::vector< int > GetPropertyDataTypes(const te::da::DataSet *dataset)
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.
A Select models a query to be used when retrieving data from a DataSource.
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.
Spatial contains operator.
int getType() const
It returns the property data type.
void add(Constraint *c)
It adds a new constraint.
A dataset is the unit of information manipulated by the data access module of TerraLib.
const std::vector< te::dt::Property * > & getProperties() const
It returns the properties that form the unique key.
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)
It describes a primary key (pk) constraint.
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...
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)
TEDATAACCESSEXPORT DataSetType * GetDataSetType(const std::string &name, const std::string &datasourceId)
Spatial Disjoint operator.
TEDATAACCESSEXPORT std::size_t GetFirstPropertyPos(const te::da::DataSet *dataset, int datatype)
TEDATAACCESSEXPORT te::gm::GeometryProperty * GetFirstGeomProperty(const DataSetType *dt)
TEDATAACCESSEXPORT DataSetAdapter * CreateAdapter(DataSet *ds, DataSetTypeConverter *converter, bool isOwner=false)
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)
TEDATAACCESSEXPORT std::auto_ptr< Fields > BuildFields(const std::vector< std::string > &properties)
TEDATAACCESSEXPORT te::dt::Property * GetFirstSpatialProperty(const DataSetType *dt)
UniqueKey * getUniqueKey(std::size_t i) const
It returns the i-th unique key.
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.
boost::shared_ptr< DataSourceInfo > DataSourceInfoPtr
TEDATAACCESSEXPORT void GetPropertyInfo(const DataSetType *const dt, std::vector< std::string > &pnames, std::vector< int > &ptypes)
A class that models a literal for Geometry values.
const std::string & getName() const
It returns the property name.