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

Add column 'type' to sets and take its value on command line.

This commit is contained in:
King_DuckZ 2015-11-11 20:06:14 +00:00
parent 585c7f45b7
commit 911da3bb00
9 changed files with 78 additions and 25 deletions

View file

@ -19,14 +19,30 @@
#include "dindexerConfig.h"
#include <boost/program_options.hpp>
#include <iostream>
#include <algorithm>
#define STRINGIZE_IMPL(s) #s
#define STRINGIZE(s) STRINGIZE_IMPL(s)
#if defined(lengthof)
# undef lengthof
#endif
//http://stackoverflow.com/questions/4415524/common-array-length-macro-for-c#4415646
#define lengthof(x) ((sizeof(x)/sizeof(0[x])) / ((std::size_t)(!(sizeof(x) % sizeof(0[x])))))
namespace po = boost::program_options;
namespace din {
namespace {
const char g_allowed_types[] = {
'D', //Directory
'V', //DVD
'B', //BluRay
'F', //Floppy Disk
'H', //Hard Disk
'Z', //Iomega Zip
'O' //Other
};
const char* const g_version_string =
PROGRAM_NAME " v"
STRINGIZE(VERSION_MAJOR) "."
@ -48,6 +64,7 @@ namespace din {
po::options_description set_options("Set options");
set_options.add_options()
("setname,n", po::value<std::string>()->default_value("New set"), "Name to be given to the new set being scanned.")
("type,t", po::value<char>()->default_value('V'), "Default set type. Valid values are B, D, F, H, O, V, Z.")
;
po::options_description positional_options("Positional options");
positional_options.add_options()
@ -92,6 +109,9 @@ namespace din {
if (parVarMap.count("search-path") == 0) {
throw std::invalid_argument("No search path specified");
}
if (g_allowed_types + lengthof(g_allowed_types) == std::find(g_allowed_types, g_allowed_types + lengthof(g_allowed_types), parVarMap["type"].as<char>())) {
throw std::invalid_argument("Invalid value for parameter \"type\"");
}
return false;
}
} //namespace din

View file

@ -26,16 +26,17 @@ namespace din {
namespace {
const std::size_t g_batch_size = 100;
std::string make_set_insert_query (pq::Connection& parConn, const std::string& parSetName) {
std::string make_set_insert_query (pq::Connection& parConn, const SetRecordData& parSetData) {
std::ostringstream oss;
oss << "INSERT INTO \"sets\" (\"desc\") VALUES ("
<< parConn.escaped_literal(parSetName)
oss << "INSERT INTO \"sets\" (\"desc\",\"type\") VALUES ("
<< parConn.escaped_literal(parSetData.name) << ','
<< '\'' << parSetData.type << '\''
<< ");";
return oss.str();
}
} //unnamed namespace
void write_to_db (const DinDBSettings& parDB, const std::vector<FileRecordData>& parData, const std::string& parSetName) {
void write_to_db (const DinDBSettings& parDB, const std::vector<FileRecordData>& parData, const SetRecordData& parSetData) {
if (parData.empty()) {
return;
}
@ -44,7 +45,7 @@ namespace din {
conn.connect();
conn.query_void("BEGIN;");
conn.query_void(make_set_insert_query(conn, parSetName));
conn.query_void(make_set_insert_query(conn, parSetData));
//TODO: use COPY instead of INSERT INTO
for (std::size_t z = 0; z < parData.size(); z += g_batch_size) {
std::ostringstream query;

View file

@ -21,6 +21,7 @@
#include <string>
#include <vector>
#include <cstdint>
#include <boost/utility/string_ref.hpp>
namespace din {
struct DinDBSettings;
@ -34,7 +35,12 @@ namespace din {
const bool is_symlink;
};
void write_to_db ( const DinDBSettings& parDB, const std::vector<FileRecordData>& parData, const std::string& parSetName );
struct SetRecordData {
const boost::string_ref name;
const char type;
};
void write_to_db ( const DinDBSettings& parDB, const std::vector<FileRecordData>& parData, const SetRecordData& parSetData );
} //namespace din
#endif

View file

@ -235,7 +235,8 @@ namespace din {
#endif
}
void Indexer::add_to_db (const std::string& parSetName) const {
void Indexer::add_to_db (const std::string& parSetName, char parType) const {
assert(m_local_data->done_count == m_local_data->paths.size());
PathName base_path(m_local_data->paths.front().path);
std::vector<FileRecordData> data;
data.reserve(m_local_data->paths.size());
@ -249,7 +250,9 @@ namespace din {
itm.is_symlink
});
}
write_to_db(m_local_data->db_settings, data, parSetName);
SetRecordData set_data {parSetName, parType};
write_to_db(m_local_data->db_settings, data, set_data);
}
bool Indexer::add_path (const char* parPath, int parLevel, bool parIsDir, bool parIsSymLink) {

View file

@ -38,7 +38,7 @@ namespace din {
std::size_t total_items ( void ) const;
std::size_t processed_items ( void ) const;
void calculate_hash ( void );
void add_to_db ( const std::string& parSetName ) const;
void add_to_db ( const std::string& parSetName, char parType ) const;
bool empty ( void ) const;
private:

View file

@ -63,7 +63,7 @@ int main (int parArgc, char* parArgv[]) {
}
else {
indexer.calculate_hash();
indexer.add_to_db(vm["setname"].as<std::string>());
indexer.add_to_db(vm["setname"].as<std::string>(), vm["type"].as<char>());
}
return 0;
}

View file

@ -125,9 +125,13 @@ namespace pq {
}
std::string Connection::escaped_literal (const std::string& parString) {
return this->escaped_literal(boost::string_ref(parString));
}
std::string Connection::escaped_literal (boost::string_ref parString) {
typedef std::unique_ptr<char[], void(*)(void*)> PQArrayType;
PQArrayType clean_str(PQescapeLiteral(m_localData->connection, parString.c_str(), parString.size()), &PQfreemem);
PQArrayType clean_str(PQescapeLiteral(m_localData->connection, parString.data(), parString.size()), &PQfreemem);
return std::string(clean_str.get());
}
} //namespace pq

View file

@ -22,6 +22,7 @@
#include <string>
#include <cstdint>
#include <memory>
#include <boost/utility/string_ref.hpp>
namespace pq {
class Connection {
@ -37,6 +38,7 @@ namespace pq {
ResultSet query ( const std::string& parQuery );
std::string escaped_literal ( const std::string& parString );
std::string escaped_literal ( boost::string_ref parString );
private:
struct LocalData;