1
0
Fork 0
mirror of https://github.com/KingDuckZ/dindexer.git synced 2025-08-17 15:19:48 +00:00

Move db delete to the postgresql backend lib.

This commit is contained in:
King_DuckZ 2016-05-22 03:02:39 +01:00
parent 5203fbece2
commit 45ecdb9a2b
5 changed files with 5 additions and 4 deletions

View file

@ -3,7 +3,6 @@ project(${bare_name}-delete CXX)
add_executable(${PROJECT_NAME}
main.cpp
commandline.cpp
postgre_delete.cpp
)
target_include_directories(${PROJECT_NAME}
@ -13,6 +12,7 @@ target_include_directories(${PROJECT_NAME}
target_link_libraries(${PROJECT_NAME}
PRIVATE ${bare_name}-if
PRIVATE ${bare_name}-common
PRIVATE ${bare_name}-backend-postgresql
)
string(REPLACE "${bare_name}-" "" ACTION_NAME "${PROJECT_NAME}")

View file

@ -18,7 +18,7 @@
#include "commandline.hpp"
#include "dindexer-common/settings.hpp"
#include "dindexerConfig.h"
#include "postgre_delete.hpp"
#include "backends/postgresql/delete.hpp"
#include <iostream>
#include <ciso646>
#include <ios>

View file

@ -1,75 +0,0 @@
/* Copyright 2015, 2016, 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 <boost/lexical_cast.hpp>
namespace din {
namespace {
IDDescMap fetch_existing_ids (pq::Connection& parConn, const std::vector<uint32_t>& parIDs) {
using boost::lexical_cast;
IDDescMap retmap;
if (parIDs.empty()) {
return 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[lexical_cast<IDDescMap::key_type>(record["id"])] = record["desc"];
}
return 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(oss.str());
}
} //namespace din

View file

@ -1,38 +0,0 @@
/* Copyright 2015, 2016, 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