27 #include "../common/progress/TaskProgress.h" 28 #include "../common/StringUtils.h" 29 #include "../core/translator/Translator.h" 30 #include "../dataaccess/dataset/CheckConstraint.h" 31 #include "../dataaccess/dataset/DataSet.h" 32 #include "../dataaccess/dataset/ForeignKey.h" 33 #include "../dataaccess/dataset/Index.h" 34 #include "../dataaccess/dataset/ObjectIdSet.h" 35 #include "../dataaccess/dataset/PrimaryKey.h" 36 #include "../dataaccess/dataset/Sequence.h" 37 #include "../dataaccess/dataset/UniqueKey.h" 38 #include "../dataaccess/datasource/ScopedTransaction.h" 39 #include "../dataaccess/query/In.h" 40 #include "../dataaccess/query/Select.h" 41 #include "../dataaccess/query/SQLDialect.h" 42 #include "../dataaccess/utils/Utils.h" 43 #include "../datatype/Array.h" 44 #include "../datatype/Enums.h" 45 #include "../datatype/Property.h" 46 #include "../datatype/SimpleData.h" 47 #include "../geometry/GeometryProperty.h" 48 #include "../geometry/Utils.h" 49 #include "../raster/Grid.h" 50 #include "../raster/BandProperty.h" 51 #include "../raster/RasterProperty.h" 52 #include "../geometry/Geometry.h" 71 #include <boost/algorithm/algorithm.hpp> 72 #include <boost/format.hpp> 73 #include <boost/uuid/random_generator.hpp> 74 #include <boost/uuid/uuid_io.hpp> 80 #include "../datatype/Utils.h" 81 #include "../dataaccess/dataset/ObjectId.h" 93 m_connectionId(connectionId),
95 m_isInTransaction(false),
96 m_getConstraints(true)
152 std::string sizeQuery(
"SELECT COUNT(*) FROM ");
155 PGresult* sizeResult = scopedConnection->query(sizeQuery);
157 long long int size = *((
long long int*)(PQgetvalue(sizeResult, 0, 0)));
159 #if TE_MACHINE_BYTE_ORDER == TE_NDR 167 std::string cursor =
"DECLARE ";
169 boost::uuids::basic_random_generator<boost::mt19937> gen;
170 boost::uuids::uuid u = gen();
172 std::string id_cursor = boost::uuids::to_string(u);
173 boost::erase_all(id_cursor,
"-");
175 id_cursor =
"c_" + id_cursor;
177 cursor +=
" CURSOR WITH HOLD FOR ";
179 std::string sql(
"SELECT * FROM ");
184 scopedConnection->execute(cursor);
189 std::string fetch(
"FETCH FORWARD ");
190 fetch += boost::lexical_cast<std::string>(
m_fetchSize);
194 PGresult* result = scopedConnection->query(fetch);
196 std::vector<int> ptypes;
206 std::unique_ptr<std::string> sql(
new std::string(
"SELECT * FROM "));
209 PGresult* result = scopedConnection->query(*sql);
211 std::vector<int> ptypes;
219 const std::string& name,
const std::string& propertyName,
227 std::unique_ptr<te::dt::Property>
p =
getProperty(name, propertyName);
242 std::string sizeQuery(
"SELECT COUNT(*) FROM ");
244 sizeQuery +=
" WHERE ";
245 sizeQuery += propertyName;
250 PGresult* sizeResult = scopedConnection->query(sizeQuery);
251 long long int size = *((
long long int*)(PQgetvalue(sizeResult, 0, 0)));
253 #if TE_MACHINE_BYTE_ORDER == TE_NDR 261 std::string cursor =
"DECLARE ";
263 boost::uuids::basic_random_generator<boost::mt19937> gen;
264 boost::uuids::uuid u = gen();
266 std::string id_cursor = boost::uuids::to_string(u);
267 boost::erase_all(id_cursor,
"-");
269 id_cursor =
"c_" + id_cursor;
271 cursor +=
" CURSOR WITH HOLD FOR ";
273 std::string sql(
"SELECT * FROM ");
283 scopedConnection->execute(cursor);
288 std::string fetch(
"FETCH FORWARD ");
289 fetch += boost::lexical_cast<std::string>(
m_fetchSize);
293 PGresult* result = scopedConnection->query(fetch);
295 std::vector<int> ptypes;
305 std::string sql(
"SELECT * FROM ");
313 PGresult* result = scopedConnection->query(sql);
315 std::vector<int> ptypes;
318 return std::unique_ptr<te::da::DataSet>(
324 const std::string& name,
const std::string& propertyName,
342 std::string sizeQuery(
"SELECT COUNT(*) FROM ");
344 sizeQuery +=
"WHERE";
351 sizeQuery += propertyName;
354 PGresult* sizeResult = scopedConnection->query(sizeQuery);
355 long long int size = *((
long long int*)(PQgetvalue(sizeResult, 0, 0)));
357 #if TE_MACHINE_BYTE_ORDER == TE_NDR 365 std::string cursor =
"DECLARE ";
367 boost::uuids::basic_random_generator<boost::mt19937> gen;
368 boost::uuids::uuid u = gen();
370 std::string id_cursor = boost::uuids::to_string(u);
371 boost::erase_all(id_cursor,
"-");
373 id_cursor =
"c_" + id_cursor;
375 cursor +=
" CURSOR WITH HOLD FOR ";
377 std::string sql(
"SELECT * FROM ");
391 scopedConnection->execute(cursor);
396 std::string fetch(
"FETCH FORWARD ");
397 fetch += boost::lexical_cast<std::string>(
m_fetchSize);
401 PGresult* result = scopedConnection->query(fetch);
403 std::vector<int> ptypes;
413 std::string sql(
"SELECT * FROM ");
421 PGresult* result = scopedConnection->query(sql);
423 std::vector<int> ptypes;
426 return std::unique_ptr<te::da::DataSet>(
443 scopedConnection.release();
445 return query(sql, travType, isConnected,accessPolicy);
460 std::string sizeQuery(
"SELECT COUNT(*) FROM ");
461 sizeQuery +=
"("+ query +
") AS InputQuery";
462 PGresult* sizeResult = scopedConnection->query(sizeQuery);
463 long long int size = *((
long long int*)(PQgetvalue(sizeResult, 0, 0)));
465 #if TE_MACHINE_BYTE_ORDER == TE_NDR 472 std::string cursor =
"DECLARE ";
474 boost::uuids::basic_random_generator<boost::mt19937> gen;
475 boost::uuids::uuid u = gen();
477 std::string id_cursor = boost::uuids::to_string(u);
478 boost::erase_all(id_cursor,
"-");
480 id_cursor =
"c_" + id_cursor;
482 cursor +=
" CURSOR WITH HOLD FOR ";
486 scopedConnection->execute(cursor);
491 std::string fetch(
"FETCH FORWARD ");
492 fetch += boost::lexical_cast<std::string>(
m_fetchSize);
496 PGresult* result = scopedConnection->query(fetch);
498 std::vector<int> ptypes;
510 std::vector<int> ptypes;
532 scopedConnection->
execute(command);
537 return std::unique_ptr<te::da::PreparedQuery>(
new PreparedQuery(
this, qName));
542 return std::unique_ptr<te::da::BatchExecutor>(
nullptr);
551 std::string sql(
"SELECT lastval()");
553 std::unique_ptr<te::da::DataSet> dataset =
query(sql);
555 if (dataset->size() != 1)
558 dataset->moveFirst();
560 boost::int64_t value = dataset->getInt64(0);
572 std::vector<std::string> datasetNames;
574 std::string sql(
"SELECT pg_class.oid, pg_namespace.nspname, pg_class.relname, pg_class.relkind " 575 "FROM pg_class, pg_namespace " 576 "WHERE pg_class.relname !~ '^pg_' " 577 "AND pg_class.relname NOT IN ('spatial_ref_sys', 'geometry_columns', 'geography_columns', 'raster_columns', 'raster_overviews') " 578 "AND pg_class.relkind in ('r','v') " 579 "AND pg_class.relnamespace = pg_namespace.oid " 580 "AND pg_namespace.nspname NOT IN ('information_schema', 'pg_toast', 'pg_temp_1', 'pg_catalog', 'topology') " 581 "ORDER BY pg_class.relname" 584 std::unique_ptr<te::da::DataSet> datasetInfo =
query(sql);
586 while(datasetInfo->moveNext())
588 std::string datasetName = std::string(datasetInfo->getString(1) +
"." + datasetInfo->getString(2));
589 datasetNames.push_back(datasetName);
612 boost::ptr_vector<te::dt::Property> properties =
getProperties(datasetName);
613 for(std::size_t i = 0; i < properties.size(); ++i)
626 return std::unique_ptr<te::da::DataSetType>(
dt);
631 std::string fullDatasetName =
getFullName(datasetName);
633 boost::ptr_vector<te::dt::Property> properties;
637 while(pInfo->moveNext())
639 unsigned int attNum = pInfo->getInt16(0);
640 std::string attName = pInfo->getString(1);
641 unsigned int attType = pInfo->getInt32(2);
642 bool attNotNull = pInfo->getBool(3);
643 std::string fmt = pInfo->getString(4);
644 bool attHasDefault = pInfo->getBool(5);
645 std::string attDefValue = pInfo->getString(6);
646 int ndims = pInfo->getInt32(7);
649 fmt.c_str(), attHasDefault, attDefValue.c_str(),
652 properties.push_back(p);
656 getGeometryInfo(datasetName, static_cast<te::gm::GeometryProperty*>(p));
660 getRasterInfo(datasetName, static_cast<te::rst::RasterProperty*>(p));
669 std::string fullDatasetName =
getFullName(datasetName);
673 std::string pName = name;
676 std::size_t pos = pName.find_last_of(
".");
677 if(pos != std::string::npos)
678 pName = pName.substr(++pos);
681 while(pInfo->moveNext())
683 std::string attName = pInfo->getString(1);
687 unsigned int attNum = pInfo->getInt16(0);
688 unsigned int attType = pInfo->getInt32(2);
689 bool attNotNull = pInfo->getBool(3);
690 std::string fmt = pInfo->getString(4);
691 bool attHasDefault = pInfo->getBool(5);
692 std::string attDefValue = pInfo->getString(6);
693 int ndims = pInfo->getInt32(7);
696 fmt.c_str(), attHasDefault, attDefValue.c_str(),
701 getGeometryInfo(datasetName, static_cast<te::gm::GeometryProperty*>(p));
705 getRasterInfo(datasetName, static_cast<te::rst::RasterProperty*>(p));
709 return std::unique_ptr<te::dt::Property>(
p);
714 std::string fullDatasetName =
getFullName(datasetName);
716 boost::ptr_vector<te::dt::Property> properties =
getProperties(fullDatasetName);
718 assert(propertyPos < properties.size());
720 return std::unique_ptr<te::dt::Property>(properties[propertyPos].clone());
725 std::string fullDatasetName =
getFullName(datasetName);
727 boost::ptr_vector<te::dt::Property> properties =
getProperties(fullDatasetName);
729 std::size_t numProperties = properties.size();
731 std::vector<std::string> pNames(numProperties);
733 for(std::size_t i = 0; i < numProperties; ++i)
734 pNames[i] = properties[i].getName();
741 std::string fullDatasetName =
getFullName(datasetName);
747 std::string fullDatasetName =
getFullName(datasetName);
751 if(std::find(pNames.begin(), pNames.end(), name) != pNames.end())
759 std::string fullDatasetName =
getFullName(datasetName);
761 std::string pName = p->
getName();
770 sql =
"SELECT AddGeometryColumn('";
773 std::size_t pos = fullDatasetName.find(
".");
775 if(pos == std::string::npos)
805 sql =
"ALTER TABLE ";
806 sql += fullDatasetName;
807 sql +=
" ADD COLUMN ";
819 std::string fullDatasetName =
getFullName(datasetName);
821 std::unique_ptr<te::dt::Property>
p =
getProperty(fullDatasetName, name);
827 sql =
"SELECT DropGeometryColumn('";
830 std::size_t pos = fullDatasetName.find(
".");
832 if(pos == std::string::npos)
851 sql =
" ALTER TABLE ";
852 sql += fullDatasetName;
853 sql +=
" DROP COLUMN ";
861 const std::string& name,
862 const std::string& newName)
864 std::string fullDatasetName =
getFullName(datasetName);
866 std::unique_ptr<te::dt::Property>
p =
getProperty(fullDatasetName, name);
868 std::string sql(
"ALTER TABLE ");
869 sql += fullDatasetName;
870 sql +=
" RENAME COLUMN ";
871 sql += name +
" TO ";
878 sql =
"UPDATE geometry_columns SET f_geometry_column = '" + newName +
"'";
879 sql +=
" WHERE f_table_name = '" ;
882 std::size_t pos = fullDatasetName.find(
".");
884 if(pos == std::string::npos)
886 sql += fullDatasetName;
887 sql +=
"' AND f_table_schema ='";
893 sql += fullDatasetName.substr(pos + 1);
894 sql +=
"' AND f_table_schema ='";
895 sql += fullDatasetName.substr(0, pos);
899 sql +=
" AND f_geometry_column = '" + name +
"'";
907 std::unique_ptr<te::dt::Property> prp(newProp);
911 type =
"NUMERIC USING " + propName+
"::numeric";
916 std::string sql(
"ALTER TABLE ");
917 sql += datasetName +
" ALTER COLUMN " + propName +
" TYPE " + type;
930 std::string fullDatasetName =
getFullName(datasetName);
934 std::unique_ptr<te::da::DataSet> pkInfo =
getConstraints(fullDatasetName,
'p');
936 while(pkInfo->moveNext())
938 unsigned int pkId = pkInfo->getInt32(0);
939 std::string pkName = pkInfo->getString(2);
943 std::unique_ptr<te::dt::Array> pkCols(pkInfo->getArray(8));
944 std::size_t size = pkCols->getDimensionSize(0);
946 std::vector<std::size_t> pos;
948 for(std::size_t i = 0; i < size; ++i)
954 std::unique_ptr<te::dt::Property>
p =
getProperty(static_cast<te::dt::Int16*>(pkCol)->getValue(), fullDatasetName);
955 pk->
add(p.release());
960 std::vector<std::string> idxNames =
getIndexNames(fullDatasetName);
962 for(std::size_t i = 0; i < idxNames.size(); ++i)
964 if(pk && pk->
getName() == idxNames[i])
971 return std::unique_ptr<te::da::PrimaryKey>(pk);
976 std::string fullDatasetName =
getFullName(datasetName);
978 std::unique_ptr<te::da::PrimaryKey> pk =
getPrimaryKey(fullDatasetName);
980 if(pk->getName() == name)
988 std::string fullDatasetName =
getFullName(datasetName);
999 pkName = fullDatasetName +
"_pk";
1001 boost::replace_all(pkName,
".",
"_");
1008 std::string sql(
"ALTER TABLE ");
1009 sql += fullDatasetName;
1010 sql +=
" ADD CONSTRAINT ";
1012 sql +=
" PRIMARY KEY (";
1014 const std::vector<te::dt::Property*>& properties = pk->
getProperties();
1016 std::size_t size = properties.size();
1018 for(std::size_t i = 0; i < size; ++i)
1023 sql += properties[i]->getName();
1033 std::string fullDatasetName =
getFullName(datasetName);
1035 std::unique_ptr<te::da::PrimaryKey> pk =
getPrimaryKey(fullDatasetName);
1037 std::string pkName = pk->getName();
1039 std::string sql(
"ALTER TABLE ");
1040 sql += fullDatasetName;
1041 sql +=
" DROP CONSTRAINT " + pkName;
1048 std::string fullDatasetName =
getFullName(datasetName);
1052 std::unique_ptr<te::da::DataSet> fkInfo =
getConstraints(fullDatasetName,
'f');
1054 while(fkInfo->moveNext())
1056 std::string fkName = fkInfo->getString(2);
1061 unsigned int fkId = fkInfo->getInt32(0);
1062 unsigned int refDatasetId = fkInfo->getInt32(4);
1063 char onUpdate = fkInfo->getChar(5);
1064 char onDeletion = fkInfo->getChar(6);
1066 std::unique_ptr<te::dt::Array> fkCols(fkInfo->getArray(8));
1067 std::unique_ptr<te::dt::Array> fkRefCols(fkInfo->getArray(9));
1069 assert(fkCols->getDimension() == 1);
1070 assert(fkCols->getDimension() == fkRefCols->getDimension());
1071 assert(fkCols->getDimensionSize(0) == fkRefCols->getDimensionSize(0));
1074 std::unique_ptr<te::da::DataSetType> refdt =
getDataSetType(refName);
1081 std::size_t size = fkCols->getDimensionSize(0);
1083 std::vector<std::size_t> pos;
1086 for(std::size_t i = 0; i < size; ++i)
1091 fk->
addRefProperty(refdt->getPropertyById(static_cast<te::dt::Int16*>(fkRefCol)->getValue()));
1094 fk->
add(
getProperty(static_cast<te::dt::Int16*>(fkCol)->getValue(), datasetName).release());
1098 return std::unique_ptr<te::da::ForeignKey>(fk);
1103 std::string fullDatasetName =
getFullName(datasetName);
1105 std::vector<std::string> fkNames;
1107 std::unique_ptr<te::da::DataSet> fkInfo =
getConstraints(fullDatasetName,
'f');
1109 while(fkInfo->moveNext())
1111 std::string fkName = fkInfo->getString(2);
1112 fkNames.push_back(fkName);
1120 std::string fullDatasetName =
getFullName(datasetName);
1123 if(std::find(fkNames.begin(), fkNames.end(), name) != fkNames.end())
1131 std::string fullDatasetName =
getFullName(datasetName);
1133 std::string fkName = fk->
getName();
1135 std::string sql(
"ALTER TABLE ");
1136 sql += fullDatasetName;
1137 sql +=
" ADD CONSTRAINT ";
1139 sql +=
" FOREIGN KEY (";
1143 for(std::size_t i = 0; i < size; ++i)
1155 for(
size_t i = 0; i < size; ++i)
1163 sql +=
") ON DELETE ";
1168 sql +=
" NO ACTION ";
1172 sql +=
" RESTRICT ";
1185 sql +=
"SET DEFAULT ";
1189 sql +=
" ON UPDATE ";
1194 sql +=
" NO ACTION ";
1198 sql +=
" RESTRICT ";
1211 sql +=
"SET DEFAULT ";
1220 std::string fullDatasetName =
getFullName(datasetName);
1222 std::string sql(
"ALTER TABLE ");
1223 sql += fullDatasetName;
1224 sql +=
" DROP CONSTRAINT ";
1232 std::string fullDatasetName =
getFullName(datasetName);
1236 std::unique_ptr<te::da::DataSet> ukInfo =
getConstraints(fullDatasetName,
'u');
1238 while(ukInfo->moveNext())
1240 std::string ukName = ukInfo->getString(2);
1245 unsigned int ukId = ukInfo->getInt32(0);
1247 std::unique_ptr<te::dt::Array> ukCols(ukInfo->getArray(8));
1251 std::size_t size = ukCols->getDimensionSize(0);
1253 std::vector<std::size_t> pos;
1256 for(std::size_t i = 0; i < size; ++i)
1262 std::unique_ptr<te::dt::Property>
p =
getProperty(static_cast<te::dt::Int16*>(ukCol)->getValue(), fullDatasetName);
1264 uk->add(p.release());
1269 std::vector<std::string> idxNames =
getIndexNames(fullDatasetName);
1271 for(std::size_t i = 0; i < idxNames.size(); ++i)
1273 if(uk->
getName() == idxNames[i])
1280 return std::unique_ptr<te::da::UniqueKey>(uk);
1285 std::string fullDatasetName =
getFullName(datasetName);
1287 std::vector<std::string> ukNames;
1289 std::unique_ptr<te::da::DataSet> ukInfo =
getConstraints(fullDatasetName,
'u');
1291 while(ukInfo->moveNext())
1293 std::string ukName = ukInfo->getString(2);
1294 ukNames.push_back(ukName);
1302 std::string fullDatasetName =
getFullName(datasetName);
1306 if(std::find(ukNames.begin(), ukNames.end(), name) != ukNames.end())
1314 std::string fullDatasetName =
getFullName(datasetName);
1316 std::string ukName = uk->
getName();
1318 std::string sql(
"ALTER TABLE ");
1319 sql += fullDatasetName;
1320 sql +=
" ADD CONSTRAINT ";
1324 const std::vector<te::dt::Property*>& properties = uk->
getProperties();
1326 std::size_t size = properties.size();
1328 for(std::size_t i = 0; i < size; ++i)
1333 sql += properties[i]->getName();
1343 std::string fullDatasetName =
getFullName(datasetName);
1345 std::string sql(
"ALTER TABLE ");
1346 sql += fullDatasetName;
1347 sql +=
" DROP CONSTRAINT ";
1359 std::string fullDatasetName =
getFullName(datasetName);
1363 std::unique_ptr<te::da::DataSet> ccInfo =
getConstraints(datasetName,
'c');
1365 while(ccInfo->moveNext())
1367 std::string ccName = ccInfo->getString(2);
1372 unsigned int ccId = ccInfo->getInt32(0);
1379 return std::unique_ptr<te::da::CheckConstraint>(cc);
1384 std::string fullDatasetName =
getFullName(datasetName);
1386 std::vector<std::string> ccNames;
1388 std::unique_ptr<te::da::DataSet> ccInfo =
getConstraints(fullDatasetName,
'c');
1390 while(ccInfo->moveNext())
1392 std::string ccName = ccInfo->getString(2);
1394 ccNames.push_back(ccName);
1402 std::string fullDatasetName =
getFullName(datasetName);
1406 if(std::find(ccNames.begin(), ccNames.end(), name) != ccNames.end())
1414 std::string fullDatasetName =
getFullName(datasetName);
1416 std::string ccName = cc->
getName();
1418 std::string sql(
"ALTER TABLE ");
1419 sql += fullDatasetName;
1420 sql +=
" ADD CONSTRAINT ";
1431 std::string fullDatasetName =
getFullName(datasetName);
1433 std::string sql(
"ALTER TABLE ");
1434 sql += fullDatasetName;
1435 sql +=
" DROP CONSTRAINT ";
1443 std::string fullDatasetName =
getFullName(datasetName);
1449 std::string sql(
"SELECT idx_table.oid, s.nspname, idx_table.relname, pg_index.indkey, pg_am.amname, pg_index.indisunique, pg_index.indisprimary " 1450 "FROM pg_index, pg_class idx_table, pg_am, pg_namespace s " 1451 "WHERE s.oid = idx_table.relnamespace " 1452 "AND pg_index.indexrelid = idx_table.oid " 1453 "AND idx_table.relam = pg_am.oid " 1454 "AND pg_index.indrelid = ");
1457 std::unique_ptr<te::da::DataSet> idxInfo =
query(sql);
1459 while(idxInfo->moveNext())
1461 std::string idxName = idxInfo->getString(2);
1466 unsigned int idxId = idxInfo->getInt32(0);
1468 std::unique_ptr<te::dt::Array> idxCols(idxInfo->getArray(3));
1470 std::string idxType = idxInfo->getString(4);
1474 std::size_t size = idxCols->getDimensionSize(0);
1476 std::vector<std::size_t> pos;
1479 for(std::size_t i = 0; i < size; ++i)
1484 std::unique_ptr<te::dt::Property>
p =
getProperty(static_cast<te::dt::Int16*>(idxCol)->getValue(), fullDatasetName);
1486 idx->
add(p.release());
1490 return std::unique_ptr<te::da::Index>(idx);
1495 std::string fullDatasetName =
getFullName(datasetName);
1497 std::vector<std::string> idxNames;
1501 std::string sql(
"SELECT idx_table.oid, s.nspname, idx_table.relname, pg_index.indkey, pg_am.amname, pg_index.indisunique, pg_index.indisprimary " 1502 "FROM pg_index, pg_class idx_table, pg_am, pg_namespace s " 1503 "WHERE s.oid = idx_table.relnamespace " 1504 "AND pg_index.indexrelid = idx_table.oid " 1505 "AND idx_table.relam = pg_am.oid " 1506 "AND pg_index.indrelid = ");
1509 std::unique_ptr<te::da::DataSet> idxInfo =
query(sql);
1511 while(idxInfo->moveNext())
1513 std::string idxName = idxInfo->getString(2);
1515 idxNames.push_back(idxName);
1523 std::string fullDatasetName =
getFullName(datasetName);
1525 std::vector<std::string> idxNames =
getIndexNames(fullDatasetName);
1527 if(std::find(idxNames.begin(), idxNames.end(), name) != idxNames.end())
1535 const std::map<std::string, std::string>& )
1537 std::string fullDatasetName =
getFullName(datasetName);
1539 std::string idxName = idx->
getName();
1542 std::unique_ptr<te::da::PrimaryKey> pk =
getPrimaryKey(fullDatasetName);
1544 if(pk.get() && (pk->getAssociatedIndex() == idx))
1549 for(std::size_t i = 0; i < ukNames.size(); ++i)
1550 if(
getUniqueKey(datasetName, ukNames[i])->getAssociatedIndex() == idx)
1554 std::string sql(
"CREATE INDEX ");
1557 sql += fullDatasetName;
1560 sql +=
" USING HASH (";
1562 sql +=
" USING GIST (";
1564 sql +=
" USING BTREE (";
1570 for(
size_t i = 0; i < numProperties; ++i)
1585 std::string fullDatasetName =
getFullName(datasetName);
1587 std::string sql(
"DROP INDEX ");
1598 std::vector<std::string> seqNames;
1600 std::string sql(
"SELECT c.oid, n.nspname, c.relname, c.relkind " 1601 "FROM pg_class c, pg_namespace n " 1602 "WHERE c.relname !~ '^pg_' " 1603 "AND c.relkind = 'S' " 1604 "AND c.relnamespace = n.oid " 1605 "AND n.nspname NOT IN ('information_schema', 'pg_toast', 'pg_temp_1', 'pg_catalog')");
1607 std::unique_ptr<te::da::DataSet> seqNamesInfo =
query(sql);
1609 while(seqNamesInfo->moveNext())
1611 std::string seqName(seqNamesInfo->getString(2));
1616 std::string sql(
"SELECT * FROM ");
1619 std::unique_ptr<te::da::DataSet> result(
query(sql));
1621 if(result->moveNext())
1628 if(result->getBool(8))
1640 return std::unique_ptr<te::da::Sequence>(seq);
1645 std::vector<std::string> seqNames;
1647 std::string sql(
"SELECT c.oid, n.nspname, c.relname, c.relkind " 1648 "FROM pg_class c, pg_namespace n " 1649 "WHERE c.relname !~ '^pg_' " 1650 "AND c.relkind = 'S' " 1651 "AND c.relnamespace = n.oid " 1652 "AND n.nspname NOT IN ('information_schema', 'pg_toast', 'pg_temp_1', 'pg_catalog')");
1654 std::unique_ptr<te::da::DataSet> seqNamesInfo =
query(sql);
1656 while(seqNamesInfo->moveNext())
1658 std::string seqName(seqNamesInfo->getString(2));
1659 seqNames.push_back(seqName);
1669 if(std::find(seqNames.begin(), seqNames.end(), name) != seqNames.end())
1677 std::string seqName = sequence->
getName();
1679 std::string sql(
"CREATE SEQUENCE ");
1681 sql +=
" INCREMENT BY ";
1683 sql +=
" MINVALUE ";
1685 sql +=
" MAXVALUE ";
1687 sql +=
" START WITH ";
1699 sql +=
" OWNED BY ";
1709 sequence->
setId(seqId);
1714 std::unique_ptr<te::da::Sequence> seq =
getSequence(name);
1716 std::string sql(
"DROP SEQUENCE ");
1726 std::string sql(
"SELECT ST_Extent(");
1727 sql += propertyName;
1731 PGresult* result = PQexec(scopedConnection->getConn(), sql.c_str());
1733 if(PQresultStatus(result) != PGRES_TUPLES_OK)
1735 std::string errmsg(
TE_TR(
"Could not find the envelope for the given geometry property due to the following error: "));
1736 errmsg += PQerrorMessage(scopedConnection->getConn());
1743 const char* boxStr = PQgetvalue(result, 0, 0);
1751 return std::unique_ptr<te::gm::Envelope>(mbr);
1758 std::unique_ptr<te::dt::Property>
p =
getProperty(datasetName, propertyPos);
1760 std::string sql(
"SELECT ST_Extent(");
1761 sql += p->getName();
1765 PGresult* result = PQexec(scopedConnection->getConn(), sql.c_str());
1767 if(PQresultStatus(result) != PGRES_TUPLES_OK)
1769 std::string errmsg(
TE_TR(
"Could not find the envelope for the given geometry property due to the following error: "));
1770 errmsg += PQerrorMessage(scopedConnection->getConn());
1777 const char* boxStr = PQgetvalue(result, 0, 0);
1785 return std::unique_ptr<te::gm::Envelope>(mbr);
1790 std::unique_ptr<te::da::DataSet> result =
getDataSet(datasetName);
1791 return result->size();
1798 if(datasetNames.empty())
1810 if(std::find(datasetNames.begin(), datasetNames.end(), datasetName) != datasetNames.end())
1818 std::string datasetName = dt->
getName();
1821 std::string sql =
"CREATE TABLE ";
1834 for(std::size_t i = 0; i <
nCols; ++i)
1843 for(std::size_t i = 0; i < nUKs; ++i)
1848 for(std::size_t i = 0; i < nIdxs; ++i)
1853 for(std::size_t i = 0; i < nFKs; ++i)
1859 for(std::size_t i = 0; i < nCCs; ++i)
1863 std::vector<std::string> indexNames =
getIndexNames(datasetName);
1868 for(std::size_t i = 0; i < indexNames.size(); ++i)
1870 if(pk->
getName() == indexNames[i])
1881 for(std::size_t i = 0; i < numUKs; ++i)
1885 for(std::size_t j = 0; j < indexNames.size(); ++j)
1887 if(uk->
getName() == indexNames[j])
1897 const std::string& ,
1898 const std::map<std::string, std::string>& )
1911 std::string tSchema, tName;
1914 sql =
"SELECT DropGeometryTable('";
1922 sql +=
"DROP TABLE ";
1923 sql += dt->getName();
1931 std::string newTableName, newTableSchema, oldTableName, oldTableSchema;
1933 std::string sql(
"ALTER TABLE ");
1935 sql +=
" RENAME TO ";
1939 sql += newTableName;
1950 sql =
"UPDATE geometry_columns SET f_table_name = '";
1951 sql += newTableName;
1952 sql +=
"' WHERE f_table_name = '";
1953 sql += oldTableName;
1954 sql +=
"' AND f_table_schema ='";
1955 sql += oldTableSchema;
1964 const std::map<std::string, std::string>& , std::size_t limit,
1968 limit = std::string::npos;
1971 std::string sql =
"INSERT INTO ";
1977 std::unique_ptr<PreparedQuery> pq(
new PreparedQuery(
this,
"a" + boost::lexical_cast<std::string>((intptr_t)(
this))));
1983 std::size_t nProcessedRows = 0;
1985 pq->prepare(sql, paramTypes);
1991 while(d->
moveNext() && (nProcessedRows != limit))
2014 if(!dt->getPrimaryKey())
2016 throw Exception(
TE_TR(
"Can not remove dataset items because dataset doesn't have a primary key or unique key!"));
2020 std::string sql =
"DELETE FROM ";
2028 std::string oidsInClauseStr;
2030 v.
visit(*oidsInClause);
2033 sql += oidsInClauseStr;
2041 const std::vector<std::size_t>& ,
2043 const std::map<std::string, std::string>& ,
2099 const std::vector< std::set<int> >& properties,
2100 const std::vector<size_t>& ids)
2104 std::unique_ptr<PreparedQuery> pq(
new PreparedQuery(
this,
"a" + boost::lexical_cast<std::string>((intptr_t)(
this))));
2108 std::vector<int> paramTypes;
2110 std::size_t nProcessedRows = 0;
2114 const std::set<int>& setProperties = properties[nProcessedRows];
2116 std::vector<size_t> vecPropertiesPos;
2117 std::vector<te::dt::Property*> vecProperties;
2118 std::set<int>::iterator itProperties = setProperties.begin();
2120 while (itProperties != setProperties.end())
2125 vecProperties.push_back(property);
2126 vecPropertiesPos.push_back(*itProperties);
2128 paramTypes.push_back(dataType);
2134 for (
size_t j = 0; j<ids.size(); ++j)
2148 std::string sql =
"UPDATE ";
2152 sql +=
" WHERE " + id;
2154 pq->prepare(sql, paramTypes);
2156 pq->bind(vecPropertiesPos, dataset);
2161 for (
size_t i = 0; i < vecProperties.size(); ++i)
2163 delete vecProperties[i];
2181 std::string sql(
"SELECT oid FROM pg_type WHERE typname = 'geometry'");
2183 std::unique_ptr<te::da::DataSet> result(
query(sql));
2185 unsigned int id = 0;
2187 if(result->moveNext())
2188 id = result->getInt32(0);
2195 std::string sql(
"SELECT oid FROM pg_type WHERE typname = 'raster'");
2197 std::unique_ptr<te::da::DataSet> result(
query(sql));
2199 unsigned int id = 0;
2201 if(result->moveNext())
2202 id = result->getInt32(0);
2209 std::string sql(
"SELECT current_schema()");
2211 std::unique_ptr<te::da::DataSet> result(
query(sql));
2213 if(!result->moveNext())
2214 Exception(
TE_TR(
"Could not get information about PostgreSQL database backend!"));
2216 currentSchema = result->getString(0);
2221 std::string sql =
"SELECT g.coord_dimension, g.srid, g.type " 2222 "FROM geometry_columns g " 2223 "WHERE lower(g.f_table_name) = '";
2225 std::string tname, sname;
2230 sql +=
"' AND g.f_table_schema = '";
2232 sql +=
"' AND f_geometry_column = '";
2236 std::unique_ptr<te::da::DataSet> result(
query(sql));
2238 if(result->moveNext())
2241 int srid = result->getInt32(1);
2261 std::string sql =
"SELECT * FROM raster_columns as r WHERE r.r_table_name = '";
2263 std::string tname, sname;
2268 sql +=
"' AND r.r_table_schema = '";
2270 sql +=
"' AND r_raster_column = '";
2274 std::unique_ptr<te::da::DataSet> result(
query(sql));
2276 if(result->moveNext())
2278 int srid = result->getInt32(
"srid");
2283 double scale_x = result->getDouble(
"scale_x");
2285 double scale_y = result->getDouble(
"scale_y");
2287 int blocksize_x = result->getInt32(
"blocksize_x");
2289 int blocksize_y = result->getInt32(
"blocksize_y");
2293 int nbands = result->getInt32(
"num_bands");
2295 std::unique_ptr<te::dt::Array> pixel_types(result->getArray(
"pixel_types"));
2297 std::unique_ptr<te::dt::Array> nodata_values(result->getArray(
"nodata_values"));
2299 std::unique_ptr<te::gm::Geometry> g(result->getGeometry(
"extent"));
2305 rp->
set(grid.release());
2307 for(
int i = 0; i != nbands; ++i)
2309 std::vector<std::size_t> pos(1, i);
2311 std::string st = pixel_types->getData(pos)->toString();
2317 else if(st ==
"8BUI")
2319 else if(st ==
"16BI")
2321 else if(st ==
"16BUI")
2323 else if(st ==
"32BI")
2325 else if(st ==
"32BUI")
2327 else if(st ==
"32BF")
2329 else if(st ==
"64BF")
2332 throw Exception(
TE_TR(
"Band data type not supported by PostGIS driver!"));
2336 bp->
m_blkh = blocksize_y;
2338 bp->
m_blkw = blocksize_x;
2356 std::string fullName = name;
2358 if(fullName.find(
".") == std::string::npos)
2368 std::string tname, sname;
2372 std::string sql(
"SELECT pg_class.oid " 2373 "FROM pg_class, pg_namespace " 2374 "WHERE pg_class.relnamespace = pg_namespace.oid " 2375 "AND lower(pg_class.relname) = '");
2378 sql +=
"' AND lower(pg_namespace.nspname) = '";
2382 std::unique_ptr<te::da::DataSet> result(
query(sql));
2384 if(result->moveNext() ==
false)
2387 unsigned int tableid = result->getInt32(0);
2394 std::string sql(
"SELECT pg_namespace.nspname, pg_class.relname " 2395 "FROM pg_class, pg_namespace " 2396 "WHERE pg_class.relnamespace = pg_namespace.oid " 2397 "AND pg_class.oid = ");
2401 std::unique_ptr<te::da::DataSet> result(
query(sql));
2403 if(result->moveNext() ==
false)
2406 std::string tname = result->getString(0);
2408 tname += result->getString(1);
2415 std::string fullDatasetName =
getFullName(datasetName);
2419 std::string sql(
"SELECT a.attnum, a.attname, t.oid, a.attnotnull, format_type(a.atttypid, a.atttypmod), a.atthasdef, pg_get_expr(d.adbin, d.adrelid), a.attndims " 2420 "FROM pg_attribute AS a INNER JOIN pg_type AS t ON (a.atttypid = t.oid) LEFT JOIN pg_attrdef AS d ON (a.attrelid = d.adrelid AND a.attnum = d.adnum) " 2421 "WHERE a.attrelid = ");
2423 sql +=
" AND a.attisdropped = false" 2425 " ORDER BY a.attnum";
2435 std::string sql(
"SELECT attnum " 2436 "FROM pg_attribute WHERE attrelid = ");
2438 sql +=
" AND attisdropped = false " 2443 std::unique_ptr<te::da::DataSet> result(
query(sql));
2445 if(result->moveNext() ==
false)
2448 unsigned int id = result->getInt32(0);
2456 std::string sql(
"SELECT a.attnum, a.attname, t.oid, a.attnotnull, format_type(a.atttypid, a.atttypmod), a.atthasdef, pg_get_expr(d.adbin, d.adrelid), a.attndims " 2457 "FROM pg_attribute AS a INNER JOIN pg_type AS t ON (a.atttypid = t.oid) LEFT JOIN pg_attrdef AS d ON (a.attrelid = d.adrelid AND a.attnum = d.adnum) " 2458 "WHERE a.attrelid = ");
2460 sql +=
" AND a.attnum = ";
2462 sql +=
" AND a.attisdropped = false" 2463 " AND a.attnum > 0";
2465 std::unique_ptr<te::da::DataSet> result(
query(sql));
2467 std::unique_ptr<te::dt::Property>
p;
2469 if(result->moveNext())
2471 unsigned int attNum = result->getInt16(0);
2472 std::string attName = result->getString(1);
2473 unsigned int attType = result->getInt32(2);
2474 bool attNotNull = result->getBool(3);
2475 std::string fmt = result->getString(4);
2476 bool attHasDefault = result->getBool(5);
2477 std::string attDefValue = result->getString(6);
2478 int ndims = result->getInt32(7);
2480 p.reset(
Convert2TerraLib(attNum, attName.c_str(), attType, attNotNull, fmt.c_str(), attHasDefault,
2484 getGeometryInfo(datasetName, static_cast<te::gm::GeometryProperty*>(p.get()));
2494 std::string sql(
"SELECT c.oid, n.nspname, c.conname, c.contype, c.confrelid, c.confupdtype, c.confdeltype, c.confmatchtype, c.conkey, c.confkey, pg_get_constraintdef(c.oid) " 2495 "FROM pg_constraint c, pg_namespace n " 2496 "WHERE c.connamespace = n.oid " 2497 "AND c.conrelid = ");
2502 sql +=
" AND c.contype = '";
2512 std::string datasetName = dt->
getName();
2513 unsigned int dtid = dt->
getId();
2515 std::string sql(
"SELECT c.oid, n.nspname, c.conname, c.contype, c.confrelid, c.confupdtype, c.confdeltype, c.confmatchtype, c.conkey, c.confkey, pg_get_constraintdef(c.oid) " 2516 "FROM pg_constraint c, pg_namespace n " 2517 "WHERE c.connamespace = n.oid " 2518 "AND c.conrelid = ");
2521 std::unique_ptr<te::da::DataSet> cInfo =
query(sql);
2523 while(cInfo->moveNext())
2525 char cType = cInfo->getChar(3);
2529 unsigned int pkId = cInfo->getInt32(0);
2530 std::string pkName = cInfo->getString(2);
2534 std::unique_ptr<te::dt::Array> pkCols(cInfo->getArray(8));
2535 std::size_t size = pkCols->getDimensionSize(0);
2537 std::vector<std::size_t> pos;
2539 for(std::size_t i = 0; i < size; ++i)
2550 std::vector<std::string> idxNames =
getIndexNames(datasetName);
2552 for(std::size_t i = 0; i < idxNames.size(); ++i)
2554 if(pk->
getName() == idxNames[i])
2565 else if (cType ==
'f')
2568 unsigned int fkId = cInfo->getInt32(0);
2569 unsigned int refDatasetId = cInfo->getInt32(4);
2570 char onUpdate = cInfo->getChar(5);
2571 char onDeletion = cInfo->getChar(6);
2573 std::unique_ptr<te::dt::Array> fkCols(cInfo->getArray(8));
2574 std::unique_ptr<te::dt::Array> fkRefCols(cInfo->getArray(9));
2576 assert(fkCols->getDimension() == 1);
2577 assert(fkCols->getDimension() == fkRefCols->getDimension());
2578 assert(fkCols->getDimensionSize(0) == fkRefCols->getDimensionSize(0));
2583 std::unique_ptr<te::da::DataSetType> refDatasetType =
getDataSetType(refName);
2586 std::string fkName = cInfo->getString(2);
2593 std::size_t size = fkCols->getDimensionSize(0);
2595 std::vector<std::size_t> pos;
2598 for(std::size_t i = 0; i < size; ++i)
2603 fk->
addRefProperty(refDatasetType->getPropertyById(static_cast<te::dt::Int16*>(fkRefCol)->getValue()));
2606 fk->
add(
getProperty(static_cast<te::dt::Int16*>(fkCol)->getValue(), datasetName).release());
2613 else if(cType ==
'u')
2616 unsigned int ukId = cInfo->getInt32(0);
2617 std::string ukName = cInfo->getString(2);
2619 std::unique_ptr<te::dt::Array> ukCols(cInfo->getArray(8));
2623 std::size_t size = ukCols->getDimensionSize(0);
2625 std::vector<std::size_t> pos;
2628 for(std::size_t i = 0; i < size; ++i)
2634 std::unique_ptr<te::dt::Property>
p =
getProperty(static_cast<te::dt::Int16*>(ukCol)->getValue(), datasetName);
2635 uk->add(p.release());
2639 std::vector<std::string> idxNames =
getIndexNames(datasetName);
2641 for(std::size_t i = 0; i < idxNames.size(); ++i)
2643 if(uk->getName() == idxNames[i])
2645 uk->setAssociatedIndex(
getIndex(datasetName, idxNames[i]).
get());
2654 else if(cType ==
'c')
2657 std::string ccName = cInfo->getString(2);
2659 unsigned int ccId = cInfo->getInt32(0);
2674 std::string datasetName = dt->
getName();
2675 unsigned int dtid = dt->
getId();
2677 std::string sql(
"SELECT idx_table.oid, s.nspname, idx_table.relname, pg_index.indkey, pg_am.amname, pg_index.indisunique, pg_index.indisprimary " 2678 "FROM pg_index, pg_class idx_table, pg_am, pg_namespace s " 2679 "WHERE s.oid = idx_table.relnamespace " 2680 "AND pg_index.indexrelid = idx_table.oid " 2681 "AND idx_table.relam = pg_am.oid " 2682 "AND pg_index.indrelid = ");
2685 std::unique_ptr<te::da::DataSet> idxInfo =
query(sql);
2687 while(idxInfo->moveNext())
2689 unsigned int idxId = idxInfo->getInt32(0);
2690 std::string idxName = idxInfo->getString(2);
2692 std::unique_ptr<te::dt::Array> idxCols(idxInfo->getArray(3));
2694 std::string idxType = idxInfo->getString(4);
2695 bool isUK = idxInfo->getBool(5);
2696 bool isPK = idxInfo->getBool(6);
2700 std::size_t size = idxCols->getDimensionSize(0);
2702 std::vector<std::size_t> pos;
2705 for(std::size_t i = 0; i < size; ++i)
2714 idxName = idxInfo->getString(2);
2731 std::vector<te::da::Sequence*> seqs;
2734 std::vector<std::string> seqNames;
2736 std::string sql(
"SELECT c.oid, n.nspname, c.relname, c.relkind " 2737 "FROM pg_class c, pg_namespace n " 2738 "WHERE c.relname !~ '^pg_' " 2739 "AND c.relkind = 'S' " 2740 "AND c.relnamespace = n.oid " 2741 "AND n.nspname NOT IN ('information_schema', 'pg_toast', 'pg_temp_1', 'pg_catalog')");
2743 std::unique_ptr<te::da::DataSet> seqNamesInfo =
query(sql);
2745 while(seqNamesInfo->moveNext())
2747 std::string seqName(seqNamesInfo->getString(2));
2748 seqNames.push_back(seqName);
2751 for(std::size_t i = 0; i < seqNames.size(); ++i)
2753 std::string seqName = seqNames[i];
2755 std::string sql(
"SELECT * FROM ");
2758 std::unique_ptr<te::da::DataSet> result(
query(sql));
2760 if(result->moveNext() ==
false)
2761 throw Exception((boost::format(
TE_TR(
"There is no information about the sequence \"%1%\"!")) % seqName).str());
2768 if(result->getBool(8))
2778 seqs.push_back(seq);
virtual void setName(const std::string &name)
It sets the constraint name.
std::unique_ptr< te::da::DataSetType > getDataSetType(const std::string &name)
It gets information about the given dataset.
void setTitle(const std::string &title)
It sets a human descriptive title for the DataSetType.
Property * getProperty(std::size_t i) const
It returns the i-th property.
void getDatabaseInfo(std::string ¤tSchema)
It retrieves some information about the database such as the default schema used when no one is provi...
boost::int64_t getMinValue() const
It returns the minimum value that the sequence can generate.
void getIndexes(te::da::DataSetType *dt)
It gets all the indexes of the given dataset and adds them to the dummy schema.
void set(te::rst::Grid *grid)
Sets the definition of the raster grid support.
bool isInTransaction() const
It returns true if a transaction is in progress, otherwise, it returns false.
void update(const std::string &datasetName, te::da::DataSet *dataset, const std::vector< std::size_t > &properties, const te::da::ObjectIdSet *oids, const std::map< std::string, std::string > &options, std::size_t limit=0)
It updates the contents of a dataset for the set of data items.
void add(te::dt::Property *p)
It adds a property to the list of properties of the primary key.
GeomType
Each enumerated type is compatible with a Well-known Binary (WKB) type code.
boost::ptr_vector< te::dt::Property > getProperties(const std::string &datasetName)
It retrieves the properties of the dataset.
void SplitTableName(const std::string &fullName, const std::string *defaultSchema, std::string &schemaName, std::string &tableName)
void setMaxValue(boost::int64_t value)
It sets the maximum value that the sequence can generate.
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 visitor for building an SQL statement using PostGIS dialect.
An atomic property like an integer or double.
std::unique_ptr< te::da::ForeignKey > getForeignKey(const std::string &datasetName, const std::string &name)
It retrieves the foreign key from the given dataset.
std::string GetBindableUpdateSQL(const std::vector< te::dt::Property * > &properties)
Given a list of properties it constructs a string with bindable parameters that can be used inside an...
void getRasterInfo(const std::string &datasetName, te::rst::RasterProperty *rp)
It loads information about a given raster column.
te::dt::Property * getOwner() const
It returns the property type associated to the sequence.
A class that control the use of connection to a PostgreSQL database.
std::vector< std::string > getDataSetNames()
It It gets the dataset names available in the data source.
bool uniqueKeyExists(const std::string &datasetName, const std::string &name)
It checks if a unique key with the given name exists in the dataset.
std::string Convert2LCase(const std::string &value)
It converts a string to lower case.
void setAsNoCycle()
It sets the sequence as not cycled (it can't wrap).
#define TE_UNKNOWN_SRS
A numeric value to represent a unknown SRS identification in TerraLib.
boost::int64_t getMaxValue() const
It returns the maximum value that the sequence can generate.
void addForeignKey(const std::string &datasetName, te::da::ForeignKey *fk)
It adds a foreign key constraint to a dataset.
A raster band description.
Base exception class for plugin module.
A class that models the description of a dataset.
std::vector< std::string > getUniqueKeyNames(const std::string &datasetName)
It gets the unique key names of the given dataset.
void addPrimaryKey(const std::string &datasetName, te::da::PrimaryKey *pk)
It adds a primary key constraint to the dataset schema.
std::size_t getNumberOfUniqueKeys() const
It returns the number of unique keys defined for the dataset type.
void useTimer(bool flag)
Used to define if task use progress timer information.
const std::string & GetGeometryName(te::gm::GeomType t)
It returns the geometry names as usual for PostGIS.
CheckConstraint * getCheckConstraint(std::size_t i) const
It returns the i-th check-constraint associated to the dataset type.
GeomType getGeomTypeId() const _NOEXCEPT_OP(true)
It returns the geometry subclass type identifier.
struct pg_result PGresult
void addUniqueKey(const std::string &datasetName, te::da::UniqueKey *uk)
It adds a unique key constraint to the dataset.
This class can be used to inform the progress of a task.
std::string getDataSetName(unsigned int id)
It looks for a dataset name with the given id in the PostgreSQL.
unsigned int getRasterTypeId() const
It returns the type id associated to the PostGIS Raster type.
A visitor for building an SQL statement using PostGIS dialect.
bool checkConstraintExists(const std::string &datasetName, const std::string &name)
It checks if a check-constraint with the given name exists in the data source.
const std::string & getCurrentSchema() const
It returns the current schema associated to the database connection, or NULL, if none is set...
PrimaryKey * getPrimaryKey() const
It returns the primary key associated to the dataset type.
virtual void setId(unsigned int id)
It sets the constraint identifier.
An utility class to coordinate transactions.
Implementation of a connected dataset for the PostGIS driver.
boost::int64_t getCachedValues() const
It returns how many sequence numbers are preallocated.
SpatialRelation
Spatial relations between geometric objects.
void dropForeignKey(const std::string &datasetName, const std::string &fkName)
It removes the foreign key constraint from the dataset schema.
It describes a sequence (a number generator).
void add(te::dt::Property *p)
It adds the property to the list of properties of the index.
virtual ReturnType accept(VisitorType &guest) const =0
It call the visit method from the guest object.
te::gm::Envelope * GetEnvelope(const char *str)
It converts the pgType to a valid TerraLib data type.
A class that implements a connection to a PostgreSQL database.
TEDATAACCESSEXPORT std::string GetSQLValueNames(const DataSetType *dt)
static te::dt::Date ds(2010, 01, 01)
Implementation of a connected dataset for the PostGIS driver.
A class that describes a check constraint.
void commit()
It commits the transaction.
An abstract class for data providers like a DBMS, Web Services or a regular file. ...
double m_noDataValue
Value to indicate elements where there is no data, default is std::numeric_limits<double>::max().
A Transactor can be viewed as a connection to the data source for reading/writing things into it...
#define TE_TR(message)
It marks a string in order to get translated.
int m_connectionId
The connection id used by this transactor.
bool m_getConstraints
Flag that defines if the method getConstraints must be called.
std::unique_ptr< te::da::DataSet > getConstraints(const std::string &datasetName, char conType= '\0')
It gets the dataset containing information about one of the constraints(primary, foreign or unique ke...
int GetCoordDimension(GeomType t)
It returns the number of measurements or axes needed to describe a position in a coordinate system...
void execute(const std::string &command)
It executes the given SQL command and throws away the result.
std::unique_ptr< te::dt::Property > getProperty(const std::string &datasetName, const std::string &name)
It retrieves the property with the given name from the dataset.
It models a property definition.
bool isActive() const
Verify if the task is active.
Transactor(DataSource *ds, const int &connectionId=-1)
Constructor.
void remove(const std::string &datasetName, const te::da::ObjectIdSet *oids=0)
It removes all the informed items from the dataset.
std::vector< te::da::Sequence * > getSequences()
It gets information about all the sequences in the datasource.
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.
void dropCheckConstraint(const std::string &datasetName, const std::string &name)
It removes the check constraint from the dataset.
void commit()
It commits the transaction.
void renameDataSet(const std::string &name, const std::string &newName)
It renames a dataset.
Index * getIndex(std::size_t i) const
It returns the i-th index associated to the dataset type.
void setTotalSteps(int value)
Set the task total stepes.
void setId(unsigned int id)
It sets the property identifier.
void changePropertyDefinition(const std::string &datasetName, const std::string &propName, te::dt::Property *newProp)
virtual te::dt::AbstractData * clone() const
It clones the linestring.
void optimize(const std::map< std::string, std::string > &opInfo)
For some data access drivers, this method will perform some operations to optimize the data storage...
bool dataSetExists(const std::string &name)
It checks if a dataset with the given name exists in the data source.
void setIncrement(boost::int64_t n)
It sets the increment value.
virtual std::size_t size() const =0
It returns the collection size, if it is known.
te::da::DataSource * getDataSource() const
It returns the parent data source of the transactor.
virtual bool moveNext()=0
It moves the internal pointer to the next item of the collection.
unsigned int unsigned int nCols
AccessPolicy
Supported data access policies (can be used as bitfield).
TraverseType
A dataset can be traversed in two ways:
void dropDataSet(const std::string &name)
It removes the dataset schema from the data source.
An exception class for the PostGIS driver.
~Transactor()
The destructor will automatically release the connection to the pool.
std::size_t getNumberOfForeignKeys() const
It returns the number of foreign keys defined for the dataset type.
void getGeometryInfo(const std::string &datasetName, te::gm::GeometryProperty *gp)
It loads information about a given geometry column.
void add(te::rst::BandProperty *b)
It adds a new band information to the property.
const std::string & getExpression() const
It returns the check constraint expression.
void setOnDeleteAction(FKActionType a)
It sets the action to be performed when a referenced element value in the referenced DataSetType is b...
void setCachedValues(boost::int64_t value)
It sets how many sequence numbers are to be preallocated.
int getSRID() const
It returns the spatial reference system identifier associated to this property.
virtual int getPropertyDataType(std::size_t i) const =0
It returns the underlying data type of the property at position pos.
std::size_t getNumberOfItems(const std::string &datasetName)
It retrieves the number of items of the given dataset.
An Envelope defines a 2D rectangular region.
std::unique_ptr< te::da::PrimaryKey > getPrimaryKey(const std::string &datasetName)
It retrieves the primary key of the dataset.
void add(te::dt::Property *p)
It adds a property to the foreign key constraint.
This class represents a set of unique ids created in the same context. i.e. from the same data set...
virtual void visit(const Expression &visited)
std::unique_ptr< te::da::Index > getIndex(const std::string &datasetName, const std::string &name)
It gets the index with the given name from the dataset.
void dropPrimaryKey(const std::string &datasetName)
It removes the primary key constraint from the dataset schema.
void Convert2PostGIS(const te::gm::Envelope *e, int srid, std::string &output)
It converts the envelope into a PostGIS BOX3D.
std::string escape(const std::string &value)
It escapes a string for using in commands and queries.
Property * getPropertyById(unsigned int id) const
It searches for a property with the given ID.
int m_blkw
Block width (pixels).
static te::dt::DateTime d(2010, 8, 9, 15, 58, 39)
GeomType getGeometryType() const
It returns the geometry subtype allowed for the property.
static te::dt::TimeDuration dt(20, 30, 50, 11)
std::size_t getNumberOfProperties(const std::string &datasetName)
It gets the number of properties of the given dataset.
void createDataSet(te::da::DataSetType *dt, const std::map< std::string, std::string > &options)
It creates the dataset schema definition in the target data source.
std::size_t getNumberOfIndexes() const
It returns the number of indexes defined for the dataset type.
DataSetType * getReferencedDataSetType() const
It returns the referenced DataSetType of this foreign key constraint.
std::size_t getNumberOfDataSets()
It retrieves the number of data sets available in the data source.
std::unique_ptr< te::da::UniqueKey > getUniqueKey(const std::string &datasetName, const std::string &name)
It gets the unique key in the dataset with the given name.
It models a foreign key constraint for a DataSetType.
void pulse()
Calls setCurrentStep() function using getCurrentStep() + 1.
virtual std::string getAsString(std::size_t i, int precision=0) const
Method for retrieving a data value as a string plain representation.
A base class for values that can be retrieved from the data access module.
void addRefProperty(te::dt::Property *p)
It adds a reference property (on the referenced DataSetType) of this foreign key constraint.
const te::da::SQLDialect * getDialect() const
It returns the data source SQL dialect, if there is one.
It describes a unique key (uk) constraint.
void setAssociatedIndex(Index *idx)
It sets the associated index.
Property * getParent() const
It returns the parent of this property, or NULL, if it doesn't have one.
std::size_t size() const
It returns the number of properties of the CompositeProperty.
TEDATAACCESSEXPORT std::vector< int > GetPropertyDataTypes(const te::da::DataSet *dataset)
Utility functions for the data access module.
void addIndex(const std::string &datasetName, te::da::Index *idx, const std::map< std::string, std::string > &options)
It adds an index to the dataset.
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
A class that implements a connection to a PostgreSQL database.
void getPropertyId(te::dt::Property *p)
It sets the property id from the PostgreSQL system.
A Select models a query to be used when retrieving data from a DataSource.
std::unique_ptr< te::da::DataSet > query(const te::da::Select &q, te::common::TraverseType travType=te::common::FORWARDONLY, bool connected=false, const te::common::AccessPolicy accessPolicy=te::common::RAccess)
It executes a query that may return some data using a generic query. A dataset can be connected or di...
std::string getGeometryTypeName(te::gm::GeomType type)
It gets the datasource geometry type name equivalent to terralib.
void add(const std::string &datasetName, te::da::DataSet *d, const std::map< std::string, std::string > &options, std::size_t limit=0, bool enableProgress=true)
It adds data items to the dataset in the data source.
bool isCycled() const
It returns true if the sequence can wrap, otherwise it returns false.
int getType() const
It returns the property data type.
void add(Constraint *c)
It adds a new constraint.
void rollBack()
It aborts the transaction. Any changes will be rolled-back.
A dataset is the unit of information manipulated by the data access module of TerraLib.
void addProperty(const std::string &datasetName, te::dt::Property *p)
It adds a new property to the dataset schema.
const std::vector< te::dt::Property * > & getProperties() const
It returns the properties that form the unique key.
Connection * getConnection(const int &id=-1) const
It returns the underlying connection.
bool m_isInTransaction
Tells if there is a transaction in progress.
bool sequenceExists(const std::string &name)
It checks if a sequence with the given name exists in the data source.
void cloneDataSet(const std::string &name, const std::string &cloneName, const std::map< std::string, std::string > &options)
It clones the dataset in the data source.
A class that control the use of the connection to a PostgreSQL database.
ConnectionPool * getConnPool() const
It returns a pointer to the internal connection pool.
void setAssociatedIndex(Index *idx)
It sets the associated index.
Connection * getConnection(int id=-1)
It returns a connection from the pool.
ForeignKey * getForeignKey(std::size_t i) const
It returns the i-th foreign key associated to the dataset type.
void setOnUpdateAction(FKActionType a)
It sets the action to be performed when a referenced element value in the referenced DataSetType is b...
std::string GetSpatialRelation(te::gm::SpatialRelation rel)
It converts the spatial relationship to PostGIS dialect.
std::unique_ptr< te::da::PreparedQuery > getPrepared(const std::string &qName=std::string(""))
It creates a prepared query object that may be used for query commands (select, insert, update and delete) that are used repeatedly.
const std::vector< te::dt::Property * > & getProperties() const
It returns the properties that take part of the index.
std::size_t getNumberOfCheckConstraints() const
It returns the number of check-constraints defined over the dataset type.
std::vector< std::string > getIndexNames(const std::string &datasetName)
It gets the index names of the given dataset.
bool indexExists(const std::string &datasetName, const std::string &name)
It checks if an index with the given name exists in the dataset.
It describes a primary key (pk) constraint.
int getConnectionID() const
It returns the underlying connection ID.
void renameProperty(const std::string &datasetName, const std::string &propertyName, const std::string &newPropertyName)
It renames a property of the given dataset.
void setExpression(const std::string &e)
It sets the check constraint expression.
std::unique_ptr< te::gm::Envelope > getExtent(const std::string &datasetName, const std::string &propertyName)
It retrieves the bounding rectangle of the spatial property for the given dataset.
std::string getFullName(const std::string &name)
It gets the full name of the given name including the schema name.
An static class with global definitions.
te::dt::Property * Convert2TerraLib(unsigned int attNum, const char *attName, unsigned int attType, bool attNotNull, const char *fmt, bool attHasDefault, const char *attDefValue, unsigned int pgisGeomTypeOid, unsigned int pgisRasterTypeOid)
It creates a PropertyType from a PostgreSQL attribute description.
void execute(const te::da::Query &command)
It executes the specified command using a generic query representation.
unsigned int getGeomTypeId()
It will check in the database catalog the number that identifies the PostGIS Geometry type...
std::vector< std::string > getSequenceNames()
It gets the sequence names available in the data source.
std::unique_ptr< te::da::Sequence > getSequence(const std::string &name)
It gets the sequence with the given name in the data source.
boost::int64_t getIncrement() const
It returns the increment value.
void dropSequence(const std::string &name)
It removes the sequence from the data source.
boost::int64_t getStartValue() const
It returns the initial value of the sequence.
bool propertyExists(const std::string &datasetName, const std::string &name)
It checks if a property with the given name exists in the dataset.
Expression * getExpressionByInClause(const std::string source="") const
A visitor for building an SQL statement from a given Query hierarchy.
FKActionType getOnUpdateAction() const
It returns the action performed when a referenced element value in the referenced DataSetType is bein...
void dropProperty(const std::string &datasetName, const std::string &name)
It removes a property from the given dataset.
unsigned int getRasterTypeId()
It will check in the database catalog the number that identifies the PostGIS Raster type...
std::vector< std::string > getPropertyNames(const std::string &datasetName)
It gets the property names of the given dataset.
A class that represents the IN operator.
void addCheckConstraint(const std::string &datasetName, te::da::CheckConstraint *cc)
It adds a check constraint to the dataset.
void setAsCycle()
It sets the sequence as cycled (it can wrap).
te::da::IndexType GetIndexType(const char *t)
It converts the PostgreSQL index string to a TerraLib index type.
std::string GetBoxSpatialRelation(te::gm::SpatialRelation rel)
It converts the spatial relationship to PostGIS dialect.
void setReferencedDataSetType(DataSetType *refDt)
It sets the referenced DataSetType of this foreign key constraint.
std::vector< std::string > getForeignKeyNames(const std::string &datasetName)
It gets the foreign key names of the given dataset.
te::da::FKActionType GetAction(char a)
It converts the PostgreSQL foreign key modifier to a TerraLib data type.
void dropIndex(const std::string &datasetName, const std::string &idxName)
It removes the index from the dataset schema.
A template for atomic data types (integers, floats, strings and others).
std::string Convert2String(boost::int16_t value)
It converts a short integer value to a string.
int m_blkh
Block height (pixels).
A dataset is the unit of information manipulated by the data access module of TerraLib.
virtual const std::string & getName() const
It returns the constraint name.
virtual std::size_t getNumProperties() const =0
It returns the number of properties that composes an item of the dataset.
virtual std::string getPropertyName(std::size_t i) const =0
It returns the property name at position pos.
PGresult * query(const std::string &query)
It queries the database.
A class that implements a connection pool for PostGIS.
const std::string & getName() const
It returns the sequence name.
std::vector< std::string > getCheckConstraintNames(const std::string &datasetName)
It gets the check constraint names of the given dataset.
A rectified grid is the spatial support for raster data.
IndexType getIndexType() const
It gets the index type.
A class that implements a prepared query for PostgreSQL data access driver.
bool foreignKeyExists(const std::string &datasetName, const std::string &name)
It checks if a foreign key with the given name exists in the data source.
const std::vector< te::dt::Property * > & getProperties() const
It returns the properties that take part of the foreign key constraint.
std::unique_ptr< te::da::CheckConstraint > getCheckConstraint(const std::string &datasetName, const std::string &name)
It gets the check constraint of the dataset with the given name.
void addSequence(te::da::Sequence *sequence)
It creates a new sequence in the data source.
void begin()
It starts a new transaction.
virtual bool moveFirst()=0
It moves the internal pointer to the first item in the collection.
const std::vector< te::dt::Property * > & getReferencedProperties() const
It returns the referenced properties (on the referenced DataSetType) of this foreign key constraint...
std::unique_ptr< te::da::DataSet > getPropertiesInfo(const std::string &datasetName)
It gets the information about the given dataset.
Implementation of the data source for the PostGIS driver.
boost::int64_t getLastGeneratedId()
It returns the last id generated by an insertion command.
A class that implements a prepared query for PostgreSQL data access driver.
void cancel()
It requests that the data source stop the processing of the current command.
void setId(unsigned int id)
It sets the sequence identifier.
#define PGIS_UNKNOWN_SRS
A numeric value to represent a unknown SRS identification in PostGIS.
const int m_fetchSize
The size of cursor fetch.
A Query is independent from the data source language/dialect.
It describes an index associated to a DataSetType.
bool hasDataSets()
It checks if the data source has any dataset.
bool primaryKeyExists(const std::string &datasetName, const std::string &name)
It checks if a primary key exists in the dataset.
void SwapBytes(T &v)
It swaps the bytes in local.
unsigned int getId() const
It returns the property identifier.
DataSource * m_ds
The PostGIS data source associated to this transactor.
std::string GetSQLBindValues(std::size_t nproperties)
UniqueKey * getUniqueKey(std::size_t i) const
It returns the i-th unique key.
void setMinValue(boost::int64_t value)
It sets the minimum value that the sequence can generate.
bool SetColumnDef(std::string &s, const std::string &tname, const te::dt::SimpleProperty *p, bool justDataType=false)
unsigned int getDataSetId(const std::string &datasetName)
It looks for the dataset id in the PostgreSQL system.
std::unique_ptr< te::da::DataSet > getDataSet(const std::string &name, te::common::TraverseType travType=te::common::FORWARDONLY, bool isConnected=false, const te::common::AccessPolicy accessPolicy=te::common::RAccess)
It gets the dataset identified by the given name. A dataset can be connected or disconnected. A connected dataset, after its creation through the data source transactor, continues to depend on the connection given by its associated data source. Differently, a disconnected dataset, after its creation, no more depends of the connection given by the data source, and it continues to live after the connection has been released to the data source.
std::unique_ptr< te::da::BatchExecutor > getBatchExecutor()
It creates a batch command executor.
unsigned int getGeomTypeId() const
It returns the type id associated to the PostGIS Geometry type.
void dropUniqueKey(const std::string &datasetName, const std::string &name)
It removes the unique key constraint from the dataset.
FKActionType getOnDeleteAction() const
It returns the action performed when a referenced element value in the referenced DataSetType is bein...
const std::string & getName() const
It returns the property name.
const std::string & getName() const
It returns the index name.