1
0
Fork 0
mirror of https://github.com/KingDuckZ/dindexer.git synced 2025-02-19 12:04:54 +00:00

Implement deleting sets from the db.

This commit is contained in:
King_DuckZ 2015-12-12 20:20:57 +00:00
parent 4a0ddb4beb
commit 861ea2c809
5 changed files with 198 additions and 5 deletions

View file

@ -0,0 +1,48 @@
// see http://stackoverflow.com/questions/3496982/printing-lists-with-commas-c/3497021#3497021
// infix_iterator.h
//
// Lifted from Jerry Coffin's 's prefix_ostream_iterator
#if !defined(INFIX_ITERATOR_H_)
#define INFIX_ITERATOR_H_
#include <ostream>
#include <iterator>
template <class T,
class charT=char,
class traits=std::char_traits<charT> >
class infix_ostream_iterator :
public std::iterator<std::output_iterator_tag,void,void,void,void>
{
std::basic_ostream<charT,traits> *os;
charT const* delimiter;
bool first_elem;
public:
typedef charT char_type;
typedef traits traits_type;
typedef std::basic_ostream<charT,traits> ostream_type;
infix_ostream_iterator(ostream_type& s)
: os(&s),delimiter(0), first_elem(true)
{}
infix_ostream_iterator(ostream_type& s, charT const *d)
: os(&s),delimiter(d), first_elem(true)
{}
infix_ostream_iterator<T,charT,traits>& operator=(T const &item)
{
// Here's the only real change from ostream_iterator:
// Normally, the '*os << item;' would come before the 'if'.
if (!first_elem && delimiter != 0)
*os << delimiter;
*os << item;
first_elem = false;
return *this;
}
infix_ostream_iterator<T,charT,traits> &operator*() {
return *this;
}
infix_ostream_iterator<T,charT,traits> &operator++() {
return *this;
}
infix_ostream_iterator<T,charT,traits> &operator++(int) {
return *this;
}
};
#endif

View file

@ -3,6 +3,7 @@ project(${bare_name}-delete CXX C)
add_executable(${PROJECT_NAME}
main.cpp
commandline.cpp
postgre_delete.cpp
)
target_include_directories(${PROJECT_NAME}

View file

@ -16,10 +16,32 @@
*/
#include "commandline.hpp"
#include "dindexer-common/settings.hpp"
#include "dindexerConfig.h"
#include "postgre_delete.hpp"
#include <iostream>
#include <ciso646>
#include <ios>
#include <vector>
#include <cstdint>
namespace {
bool confirm_delete (const din::IDDescMap& parMap) {
for (const auto& itm : parMap) {
std::cout << "ID " << itm.first << '\t' << itm.second << '\n';
}
std::cout << "Confirm deleting these sets? (Y/n)" << std::endl;
std::string answer;
std::cin >> std::noskipws >> answer >> std::skipws;
return (answer.empty() or "y" == answer or "Y" == answer);
}
bool always_delete (const din::IDDescMap&) {
return true;
}
} //unnamed namespace
int main (int parArgc, char* parArgv[]) {
using boost::program_options::variables_map;
@ -38,12 +60,23 @@ int main (int parArgc, char* parArgv[]) {
return 2;
}
auto ids = vm["groupid"].as<std::vector<uint32_t>>();
std::cout << "Would delete " << ids.size() << " ids: ";
for (auto n : ids) {
std::cout << n << ", ";
dinlib::Settings settings;
{
const bool loaded = dinlib::load_settings(CONFIG_FILE_PATH, settings);
if (not loaded) {
std::cerr << "Can't load settings from " << CONFIG_FILE_PATH << ", quitting\n";
return 1;
}
}
std::cout << std::endl;
if (not vm.count("groupid")) {
std::cerr << "No IDs specified\n";
return 2;
}
const auto ids = vm["groupid"].as<std::vector<uint32_t>>();
auto confirm_func = (vm.count("confirm") ? &always_delete : &confirm_delete);
din::delete_group_from_db(settings.db, ids, confirm_func);
return 0;
}

View file

@ -0,0 +1,73 @@
/* Copyright 2015, Michele Santullo
* This file is part of "dindexer".
*
* "dindexer" is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* "dindexer" is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with "dindexer". If not, see <http://www.gnu.org/licenses/>.
*/
#include "postgre_delete.hpp"
#include "pq/connection.hpp"
#include "dindexer-common/settings.hpp"
#include "helpers/infix_iterator.hpp"
#include <sstream>
#include <utility>
#include <iterator>
#include <ciso646>
#include <boost/range/adaptor/map.hpp>
#include <boost/range/algorithm/copy.hpp>
#include <cstring>
namespace din {
namespace {
IDDescMap fetch_existing_ids (pq::Connection& parConn, const std::vector<uint32_t>& parIDs) {
IDDescMap retmap;
if (parIDs.empty()) {
return std::move(retmap);
}
std::ostringstream oss;
oss << "SELECT \"id\",\"desc\" FROM \"sets\" WHERE \"id\"=";
boost::copy(parIDs, infix_ostream_iterator<uint32_t>(oss, " OR \"id\"="));
oss << ';';
auto resultset = parConn.query(oss.str());
for (const auto& record : resultset) {
retmap[std::stoul(record["id"])] = record["desc"];
}
return std::move(retmap);
}
} //unnamed namespace
void delete_group_from_db (const dinlib::SettingsDB& parDB, const std::vector<uint32_t>& parIDs, ConfirmDeleCallback parConf) {
pq::Connection conn(std::string(parDB.username), std::string(parDB.password), std::string(parDB.dbname), std::string(parDB.address), parDB.port);
conn.connect();
const auto dele_ids = fetch_existing_ids(conn, parIDs);
if (dele_ids.empty()) {
return;
}
if (not parConf(dele_ids)) {
return;
}
std::vector<std::string> ids;
ids.reserve(dele_ids.size());
std::ostringstream oss;
oss << "BEGIN;\nDELETE FROM \"files\" WHERE \"group_id\"=";
boost::copy(dele_ids | boost::adaptors::map_keys, infix_ostream_iterator<uint32_t>(oss, " OR \"group_id\"="));
oss << ";\nDELETE FROM \"sets\" WHERE \"id\"=";
boost::copy(dele_ids | boost::adaptors::map_keys, infix_ostream_iterator<uint32_t>(oss, " OR \"id\"="));
oss << ";\nCOMMIT;";
conn.query_void(oss.str());
}
} //namespace din

View file

@ -0,0 +1,38 @@
/* Copyright 2015, Michele Santullo
* This file is part of "dindexer".
*
* "dindexer" is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* "dindexer" is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with "dindexer". If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef idB070B86E0E4047B1AF4144DEF2759F3C
#define idB070B86E0E4047B1AF4144DEF2759F3C
#include <functional>
#include <vector>
#include <string>
#include <cstdint>
#include <map>
namespace dinlib {
struct SettingsDB;
} //namespace dinlib
namespace din {
using IDDescMap = std::map<uint32_t, std::string>;
using ConfirmDeleCallback = std::function<bool(const IDDescMap&)>;
void delete_group_from_db ( const dinlib::SettingsDB& parDB, const std::vector<uint32_t>& parIDs, ConfirmDeleCallback parConf );
} //namespace din
#endif