void ServicesDbReader::open(QString urlStr) { if (!isSupported(urlStr)) { throw HootException("An unsupported URL was passed in."); } QUrl url(urlStr); QString osmElemId = url.queryItemValue("osm-element-id"); QString osmElemType = url.queryItemValue("osm-element-type"); QStringList pList = url.path().split("/"); bool ok; bool ok2; QString mapName; _database.open(url); _mapId = pList[pList.size() - 1].toLong(&ok); if(osmElemId.length() > 0 && osmElemType.length() > 0) { _osmElemId = osmElemId.toLong(&ok2); _osmElemType = ElementType::fromString(osmElemType); } if (!ok) { if (_email == "") { throw HootException("If a map name is specified then the user email must also be specified " "via: " + emailKey()); } mapName = pList[pList.size() - 1]; long userId = _database.getUserId(_email); set<long> mapIds = _database.selectMapIds(mapName, userId); if (mapIds.size() != 1) { QString str = QString("Expected 1 map with the name '%1' but found %2 maps.").arg(mapName) .arg(mapIds.size()); throw HootException(str); } _mapId = *mapIds.begin(); } if (!_database.mapExists(_mapId)) { _database.close(); throw HootException("No map exists with ID: " + QString::number(_mapId)); } //using a transaction seems to make sense here, b/c we don't want to read a map being modified //in the middle of its modification caused by a changeset upload, which could cause the map to //be invalid as a whole _database.transaction(); _open = true; }
void ServicesDbWriter::_openDb(QString& urlStr, bool deleteMapFlag) { if (!isSupported(urlStr)) { throw HootException("An unsupported URL was passed in."); } if (_userEmail.isEmpty()) { throw HootException("Please set the user's email address via the '" + emailKey() + "' " "configuration setting."); } QUrl url(urlStr); _sdb.open(url); _open = true; LOG_DEBUG("DB opened"); // create the user before we have a transaction so we can make sure the user gets added. if (_createUserIfNotFound) { _sdb.setUserId(_sdb.getOrCreateUser(_userEmail, _userEmail)); } else { _sdb.setUserId(_sdb.getUserId(_userEmail, true)); } //LOG_DEBUG("DB user set"); // start the transaction. We'll close it when finalizePartial is called. _sdb.transaction(); if ( _sdb.getDatabaseType() == ServicesDb::DBTYPE_SERVICES) {\ QStringList pList = url.path().split("/"); QString mapName = pList[2]; set<long> mapIds = _sdb.selectMapIds(mapName); if (mapIds.size() > 0) { if (deleteMapFlag) // deleteMapFlag is either True or _overwriteMap { for (set<long>::const_iterator it = mapIds.begin(); it != mapIds.end(); ++it) { LOG_INFO("Removing map with ID: " << *it); _sdb.deleteMap(*it); LOG_INFO("Finished removing map with ID: " << *it); } _sdb.setMapId(_sdb.insertMap(mapName, true)); } else { LOG_INFO("There are one or more maps with this name. Consider using " "'services.db.writer.overwrite.map'. Map IDs: " << mapIds); } } else if ( mapIds.size() == 0 ) { LOG_DEBUG("Map " << mapName << " was not found, must insert"); _sdb.setMapId(_sdb.insertMap(mapName, true)); } } }
void ServicesDbReader::setConfiguration(const Settings& conf) { setMaxElementsPerMap(ConfigOptions(conf).getMaxElementsPerPartialMap()); setUserEmail(conf.getString(emailKey(), "")); }
void ServicesDbWriter::setConfiguration(const Settings &conf) { setUserEmail(conf.getString(emailKey(), "")); setCreateUser(ConfigOptions(conf).getServicesDbWriterCreateUser()); setOverwriteMap(conf.getBool(overwriteMapKey(), false)); }