commit 37c3312cae23d7c15489ddd3d32540c9c12be086
parent 6cfba4a39f6b8168b123ff1a77f34a9a0b59cf70
Author: cfillion <cfillion@users.noreply.github.com>
Date: Wed, 5 Oct 2016 02:16:10 -0400
registry: convert data to the new explicit AL section format
Diffstat:
6 files changed, 51 insertions(+), 14 deletions(-)
diff --git a/src/database.hpp b/src/database.hpp
@@ -69,6 +69,9 @@ class Statement {
public:
typedef std::function<bool (void)> ExecCallback;
+ Statement(const char *sql, const Database *db);
+ ~Statement();
+
void bind(int index, const std::string &text);
void bind(int index, int64_t integer);
void exec();
@@ -81,9 +84,6 @@ public:
private:
friend Database;
- Statement(const char *sql, const Database *db);
- ~Statement();
-
const Database *m_db;
sqlite3_stmt *m_stmt;
};
diff --git a/src/registry.cpp b/src/registry.cpp
@@ -74,8 +74,8 @@ Registry::Registry(const Path &path)
void Registry::migrate()
{
+ const Database::Version version{0, 5};
const Database::Version ¤t = m_db.version();
- const Database::Version version{0, 4};
if(!current) {
// new database!
@@ -126,6 +126,8 @@ void Registry::migrate()
m_db.exec("ALTER TABLE files ADD COLUMN type INTEGER NOT NULL DEFAULT 0;");
case 3:
m_db.exec("ALTER TABLE entries ADD COLUMN desc TEXT NOT NULL DEFAULT '';");
+ case 4:
+ convertImplicitSections();
}
m_db.setVersion(version);
@@ -324,6 +326,25 @@ void Registry::commit()
m_db.commit();
}
+void Registry::convertImplicitSections()
+{
+ // convert from v1.0 main=true format to v1.1 flag format
+
+ Statement entries("SELECT id, category FROM entries", &m_db);
+ entries.exec([&] {
+ const int id = entries.intColumn(0);
+ const string &category = entries.stringColumn(1);
+ const int section = Source::detectSection(category);
+
+ Statement update("UPDATE files SET main = ? WHERE entry = ? AND main != 0", &m_db);
+ update.bind(1, section);
+ update.bind(2, id);
+ update.exec();
+
+ return true;
+ });
+}
+
void Registry::fillEntry(const Statement *stmt, Entry *entry) const
{
int col = 0;
diff --git a/src/registry.hpp b/src/registry.hpp
@@ -66,6 +66,7 @@ public:
private:
void migrate();
+ void convertImplicitSections();
void fillEntry(const Statement *, Entry *) const;
Database m_db;
diff --git a/src/source.cpp b/src/source.cpp
@@ -36,6 +36,19 @@ auto Source::getSection(const char *name) -> Section
return UnknownSection;
}
+auto Source::detectSection(const string &category) -> Section
+{
+ // this is for compatibility with v1.0
+
+ string topcategory = Path(category).first();
+ boost::algorithm::to_lower(topcategory);
+
+ if(topcategory == "midi editor")
+ return MIDIEditorSection;
+ else
+ return MainSection;
+}
+
Source::Source(const string &file, const string &url, const Version *ver)
: m_type(Package::UnknownType), m_file(file), m_url(url), m_sections(0),
m_version(ver)
@@ -73,20 +86,12 @@ const string &Source::file() const
void Source::setSections(int sections)
{
if(sections == ImplicitSection) {
- // for compatibility with v1.0
-
const Package *pkg = package();
const Category *cat = pkg ? pkg->category() : nullptr;
if(!cat)
throw reapack_error("cannot resolve implicit section: category is unset");
- string category = Path(cat->name()).first();
- boost::algorithm::to_lower(category);
-
- if(category == "midi editor")
- sections = MIDIEditorSection;
- else
- sections = MainSection;
+ sections = detectSection(cat->name());
}
m_sections = sections;
diff --git a/src/source.hpp b/src/source.hpp
@@ -36,6 +36,7 @@ public:
};
static Section getSection(const char *);
+ static Section detectSection(const std::string &category);
Source(const std::string &file, const std::string &url,
const Version * = nullptr);
diff --git a/test/source.cpp b/test/source.cpp
@@ -82,7 +82,7 @@ TEST_CASE("implicit source section") {
}
SECTION("midi editor") {
- Category cat("midi Editor/somthing else");
+ Category cat("MIDI Editor");
Package pack(Package::UnknownType, "package name", &cat);
Version ver("1.0", &pack);
@@ -101,6 +101,15 @@ TEST_CASE("implicit source section") {
}
}
+TEST_CASE("implicit section detection", M) {
+ REQUIRE(Source::MainSection == Source::detectSection("Hello World"));
+ REQUIRE(Source::MainSection == Source::detectSection("Hello/World"));
+ REQUIRE(Source::MainSection == Source::detectSection("Hello/midi editor"));
+
+ REQUIRE(Source::MIDIEditorSection == Source::detectSection("midi editor"));
+ REQUIRE(Source::MIDIEditorSection == Source::detectSection("midi editor/Hello"));
+}
+
TEST_CASE("empty source url", M) {
try {
const Source source("filename", {});