27 #include "../common/Translator.h"
28 #include "../dataaccess/dataset/DataSetType.h"
29 #include "../dataaccess/dataset/ForeignKey.h"
30 #include "../dataaccess/dataset/Index.h"
31 #include "../dataaccess/dataset/PrimaryKey.h"
32 #include "../dataaccess/dataset/UniqueKey.h"
33 #include "../datatype/Property.h"
34 #include "../geometry/Envelope.h"
35 #include "../geometry/Geometry.h"
36 #include "../geometry/GeometryProperty.h"
43 #include <boost/algorithm/string/case_conv.hpp>
44 #include <boost/format.hpp>
73 std::vector<std::string> datasets;
75 std::auto_ptr<te::da::DataSet> tables(getDataSets());
77 while(tables->moveNext())
79 std::string name = tables->getString(1);
80 datasets.push_back(name);
88 std::string sql(
"SELECT rowid, name, tbl_name, type FROM sqlite_master "
89 "WHERE type IN ('table', 'view') "
90 "AND name NOT LIKE 'sqlite_%'"
91 "AND name NOT LIKE 'idx_%'");
93 std::vector<std::string> ommitTables;
97 sql +=
" AND name NOT IN ('SpatialIndex'";
99 for(std::size_t i = 0; i < ommitTables.size(); ++i)
102 sql += ommitTables[i];
108 return std::auto_ptr<te::da::DataSet>(m_pImpl->m_parent->query(sql));
113 std::auto_ptr<te::da::DataSet> datasets(getDataSets());
115 return datasets->moveNext();
120 std::string sql(
"SELECT rowid FROM sqlite_master WHERE name = '");
124 std::auto_ptr<te::da::DataSet> table(m_pImpl->m_parent->query(sql));
126 return table->moveNext();
132 std::string sql(
"SELECT rowid, type FROM sqlite_master WHERE name = '");
136 std::auto_ptr<te::da::DataSet> tinfo(m_pImpl->m_parent->query(sql));
138 if(!tinfo->moveNext())
141 unsigned int tid = tinfo->getInt32(0);
146 sql =
"PRAGMA table_info(";
150 std::auto_ptr<te::da::DataSet> attributes(m_pImpl->m_parent->query(sql));
156 dt->setTitle(datasetName);
158 dt->setCategory(tcategory);
160 while(attributes->moveNext())
162 int col = attributes->getInt32(0);
164 std::string colName = attributes->getString(1);
166 std::string colType = attributes->getString(2);
168 bool required = attributes->getInt32(3) != 0;
170 std::string* defaultValue = attributes->isNull(4) ? 0 :
new std::string(attributes->getString(4));
172 bool isPK = attributes->getInt32(5) != 0;
184 getGeometryInfo(datasetName, gp);
189 if(!pk->getProperties().empty())
190 dt->setPrimaryKey(pk.release());
192 getIndexes(dt.get());
193 getUniqueKeys(dt.get());
203 std::auto_ptr<te::da::DataSet> ftable(0);
205 ftable = getGeometryInfo(datasetName, gp->
getName());
207 if(!ftable->moveNext())
210 int gtype = ftable->getInt32(0);
218 int srid = ftable->getInt32(2);
227 std::string sql =
"SELECT geometry_type, coord_dimension, srid, spatial_index_enabled "
228 "FROM geometry_columns "
229 "WHERE f_table_name = '";
230 sql += boost::to_lower_copy(tableName);
232 if(!geomColName.empty())
234 sql +=
"' AND f_geometry_column = '";
235 sql += boost::to_lower_copy(geomColName);
240 return m_pImpl->m_parent->query(sql);
245 boost::ptr_vector<te::dt::Property> pvec;
247 std::string sql =
"PRAGMA table_info(";
251 std::auto_ptr<te::da::DataSet> attributes(m_pImpl->m_parent->query(sql));
253 while(attributes->moveNext())
255 int col = attributes->getInt32(0);
257 std::string colName = attributes->getString(1);
259 std::string colType = attributes->getString(2);
261 bool required = attributes->getInt32(3) != 0;
263 std::string* defaultValue = attributes->isNull(4) ? 0 :
new std::string(attributes->getString(4));
272 getGeometryInfo(datasetName, gp);
282 std::string sql =
"PRAGMA table_info(";
286 std::auto_ptr<te::da::DataSet> attributes(m_pImpl->m_parent->query(sql));
290 while(attributes->moveNext())
292 bool isPK = attributes->getInt32(5) != 0;
296 int col = attributes->getInt32(0);
303 if(pk->getProperties().empty())
317 std::string sql(
"PRAGMA index_list(");
321 std::auto_ptr<te::da::DataSet> indexes(m_pImpl->m_parent->query(sql));
323 while(indexes->moveNext())
325 int idxId = indexes->getInt32(0);
327 std::string idxName = indexes->getString(1);
329 bool isUnique = indexes->getInt32(2) != 0;
331 if(isUnique && (idxName.find(
"sqlite_autoindex_") != std::string::npos))
335 getUniqueKey(dt, static_cast<unsigned int>(idxId), idxName);
341 const std::string& idxName)
343 std::string sql(
"PRAGMA index_info(");
347 std::auto_ptr<te::da::DataSet> indexInfo(m_pImpl->m_parent->query(sql));
351 while(indexInfo->moveNext())
353 int col = indexInfo->getInt32(1);
359 sql =
"PRAGMA table_info(";
363 std::auto_ptr<te::da::DataSet> attributes(m_pImpl->m_parent->query(sql));
365 std::size_t nMatchAttributes = 0;
368 while(mayBePK && attributes->moveNext())
370 bool isPK = attributes->getInt32(5) != 0;
375 int col = attributes->getInt32(0);
384 if(mayBePK && (nMatchAttributes == uk->getProperties().size()))
399 dt->
add(uk.release());
406 std::string sql(
"PRAGMA index_list(");
410 std::auto_ptr<te::da::DataSet> indexes(m_pImpl->m_parent->query(sql));
412 while(indexes->moveNext())
414 int idxId = indexes->getInt32(0);
416 std::string idxName = indexes->getString(1);
418 bool isUnique = indexes->getInt32(2) != 0;
420 if(isUnique && (idxName.find(
"sqlite_autoindex_") != std::string::npos))
423 getIndex(dt, static_cast<unsigned int>(idxId), idxName, isUnique);
426 getSpatialIndexes(dt);
431 const std::size_t nattrs = dt->
size();
433 for(std::size_t i = 0; i < nattrs; ++i)
438 std::string tname = dt->
getName();
443 std::string sql =
"SELECT f_table_name, f_geometry_column "
444 "FROM views_geometry_columns "
445 "WHERE view_name = '";
447 sql +=
"' AND view_geometry = '";
451 std::auto_ptr<te::da::DataSet> feature(m_pImpl->m_parent->query(sql));
453 if(!feature->moveNext())
456 tname = feature->getString(0);
457 gcol = feature->getString(1);
460 std::auto_ptr<te::da::DataSet> ftable(getGeometryInfo(tname, gcol));
462 if(!ftable->moveNext())
465 bool hasSPIDX = ftable->getInt32(3) != 0;
470 idx->
add(dt->getProperty(i));
477 const std::string& idxName,
480 std::string sql(
"PRAGMA index_info(");
484 std::auto_ptr<te::da::DataSet> indexInfo(m_pImpl->m_parent->query(sql));
488 while(indexInfo->moveNext())
490 int col = indexInfo->getInt32(1);
500 for(std::size_t i = 0; i < nuks; ++i)
514 dt->
add(idx.release());
520 std::string sql(
"SELECT MIN(xmin), MIN(ymin), MAX(xmax), MAX(ymax) ");
526 std::auto_ptr<te::da::DataSet> result(m_pImpl->m_parent->query(sql));
528 if(result->moveNext() ==
false)
529 throw te::common::Exception((boost::format(TR_COMMON(
"Could not get a MBR for table/view: %1%!")) % tableName).str());
531 double xmin = result->getDouble(0);
532 double ymin = result->getDouble(1);
533 double xmax = result->getDouble(2);
534 double ymax = result->getDouble(3);
Property * getProperty(std::size_t i) const
It returns the i-th property.
te::da::DataSetType * getDataSetType(const std::string &datasetName)
GeomType
Each enumerated type is compatible with a Well-known Binary (WKB) type code.
void setSRID(int srid)
It sets the spatial reference system identifier associated to this property.
void setGeometryType(GeomType t)
It sets the geometry subtype.
A class that models the description of a dataset.
std::size_t getNumberOfUniqueKeys() const
It returns the number of unique keys defined for the dataset type.
DataSourceCatalogLoader manages metadata information for the TerraLib SQLite Data Access Driver...
void GetHiddenTables(const te::da::DataSource *ds, std::vector< std::string > &tables)
PrimaryKey * getPrimaryKey() const
It returns the primary key associated to the dataset type.
void add(te::dt::Property *p)
It adds the property to the list of properties of the index.
boost::ptr_vector< te::dt::Property > getProperties(const std::string &datasetName)
void getGeometryInfo(const std::string &datasetName, te::gm::GeometryProperty *gp)
It models a property definition.
void getSpatialIndexes(te::da::DataSetType *dt)
An Envelope defines a 2D rectangular region.
void getIndex(te::da::DataSetType *dt, int idxId, const std::string &idxName, bool isUnique)
void getIndexes(te::da::DataSetType *dt)
DataSourceCatalogLoader(DataSourceTransactor *parent)
int getCategory() const
It returns the dataset category.
It describes a unique key (uk) constraint.
void setAssociatedIndex(Index *idx)
It sets the associated index.
te::gm::Envelope * getExtent(const std::string &tableName, const std::string &geomColName)
std::size_t size() const
It returns the number of properties of the CompositeProperty.
Utility functions for the TerraLib SQLite Data Access driver.
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
std::auto_ptr< te::da::DataSet > getDataSets()
int getType() const
It returns the property data type.
bool datasetExists(const std::string &name)
void add(Constraint *c)
It adds a new constraint.
void setAssociatedIndex(Index *idx)
It sets the associated index.
void clearUniqueKeys()
It removes all unique keys from the dataset type.
It describes a primary key (pk) constraint.
std::vector< std::string > getDataSetNames()
void getPrimaryKey(te::da::DataSetType *dt)
DataSourceTransactor * m_parent
An implementation of DataSourceTransactor class for the TerraLib SQLite Data Access Driver...
void clearIndexes()
It removes all indexes from the dataset type.
virtual const std::string & getName() const
It returns the constraint name.
te::da::DataSetType * Convert2TerraLib(sqlite3_stmt *pStmt)
Impl(DataSourceTransactor *parent)
void getUniqueKey(te::da::DataSetType *dt, int idxId, const std::string &idxName)
It describes an index associated to a DataSetType.
void setPrimaryKey(PrimaryKey *pk)
It sets the primary key constraint.
UniqueKey * getUniqueKey(std::size_t i) const
It returns the i-th unique key.
int Convert2TerraLibCategory(const std::string &category)
Implementation of a forward-only dataset for the TerraLib SQLite Data Access driver.
void getUniqueKeys(te::da::DataSetType *dt)
~DataSourceCatalogLoader()
const std::string & getName() const
It returns the property name.