std::pair<std::shared_ptr<te::da::DataSetType>, std::shared_ptr<te::da::DataSet> > terrama2::core::getDCPPositionsTable(std::shared_ptr<te::da::DataSource> datasource, const std::string& dataSetName) { terrama2::core::OpenClose< std::shared_ptr<te::da::DataSource> > openClose(datasource); Q_UNUSED(openClose); std::shared_ptr<te::da::DataSourceTransactor> transactorDestination(datasource->getTransactor()); te::da::ScopedTransaction scopedTransaction(*transactorDestination); std::shared_ptr<te::da::DataSet> teDataset = transactorDestination->getDataSet(dataSetName); std::shared_ptr<te::da::DataSetType> teDataSetType = transactorDestination->getDataSetType(dataSetName); return {teDataSetType, teDataset}; }
void terrama2::core::DataStoragerPostGis::store(DataSetSeries series, DataSetPtr outputDataSet) const { QUrl url(dataProvider_->uri.c_str()); std::shared_ptr<te::da::DataSource> datasourceDestination(te::da::DataSourceFactory::make("POSTGIS")); std::map<std::string, std::string> connInfo{{"PG_HOST", url.host().toStdString()}, {"PG_PORT", std::to_string(url.port())}, {"PG_USER", url.userName().toStdString()}, {"PG_PASSWORD", url.password().toStdString()}, {"PG_DB_NAME", url.path().section("/", 1, 1).toStdString()}, {"PG_CONNECT_TIMEOUT", "4"}, {"PG_CLIENT_ENCODING", "UTF-8"}}; datasourceDestination->setConnectionInfo(connInfo); OpenClose< std::shared_ptr<te::da::DataSource> > openClose(datasourceDestination); Q_UNUSED(openClose); if(!datasourceDestination->isOpened()) { QString errMsg = QObject::tr("Could not connect to database"); TERRAMA2_LOG_ERROR() << errMsg; throw DataProviderException() << ErrorDescription(errMsg); } std::string destinationDataSetName = getDataSetTableName(outputDataSet); std::shared_ptr<te::da::DataSourceTransactor> transactorDestination(datasourceDestination->getTransactor()); te::da::ScopedTransaction scopedTransaction(*transactorDestination); std::shared_ptr<te::da::DataSetType> datasetType = series.teDataSetType; std::map<std::string, std::string> options; std::shared_ptr<te::da::DataSetType> newDataSetType; if (!transactorDestination->dataSetExists(destinationDataSetName)) { // create and save datasettype in the datasource destination newDataSetType = std::shared_ptr<te::da::DataSetType>(static_cast<te::da::DataSetType*>(datasetType->clone())); if(!newDataSetType->getPrimaryKey()) { std::string pkName = "\""+newDataSetType->getName()+"_pk\""; auto pk = new te::da::PrimaryKey(pkName, newDataSetType.get()); te::dt::SimpleProperty* serialPk = new te::dt::SimpleProperty("pid", te::dt::INT32_TYPE, true); serialPk->setAutoNumber(true); newDataSetType->add(serialPk); pk->add(serialPk); } newDataSetType->setName(destinationDataSetName); transactorDestination->createDataSet(newDataSetType.get(),options); //Get original geometry to get srid te::gm::GeometryProperty* geom = GetFirstGeomProperty(datasetType.get()); //configure if there is a geometry property if(geom) { GetFirstGeomProperty(newDataSetType.get())->setSRID(geom->getSRID()); GetFirstGeomProperty(newDataSetType.get())->setGeometryType(te::gm::GeometryType); } } else { newDataSetType = transactorDestination->getDataSetType(destinationDataSetName); } const auto& oldPropertiesList = newDataSetType->getProperties(); for(const auto & property : datasetType->getProperties()) { auto it = std::find_if(oldPropertiesList.cbegin(), oldPropertiesList.cend(), [property] (te::dt::Property* oldMember) { return property->getName() == oldMember->getName() && property->getType() == oldMember->getType(); }); if(it == oldPropertiesList.cend()) transactorDestination->addProperty(newDataSetType->getName(), property); } series.syncDataSet->dataset()->moveBeforeFirst(); transactorDestination->add(newDataSetType->getName(), series.syncDataSet->dataset().get(), options); scopedTransaction.commit(); }