From f808a8281feccf41f370eb55125f56e06de0eb2b Mon Sep 17 00:00:00 2001 From: Mathias Buhr Date: Tue, 20 Jul 2021 00:11:23 +0200 Subject: [PATCH 1/6] Use C++14 --- meson.build | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/meson.build b/meson.build index c5dd71a..53763bf 100755 --- a/meson.build +++ b/meson.build @@ -23,7 +23,7 @@ project('SampleHive', license : 'GPL v3', meson_version: '>= 0.58.0', default_options : ['warning_level=1', - 'cpp_std=c++11']) + 'cpp_std=c++14']) meson_src_root = meson.current_source_dir() meson_build_root = meson.current_build_dir() From 63173df9ade6cbf1d8a83cd2a960f300aaa32fc8 Mon Sep 17 00:00:00 2001 From: Mathias Buhr Date: Tue, 20 Jul 2021 00:13:50 +0200 Subject: [PATCH 2/6] Refactor database --- src/Database.cpp | 918 ++++++++++++++++++---------------------- src/Database.hpp | 68 +-- src/IDatabase.hpp | 96 +++++ src/MainFrame.cpp | 208 ++++----- src/MainFrame.hpp | 3 + src/TagEditorDialog.cpp | 8 +- 6 files changed, 636 insertions(+), 665 deletions(-) create mode 100644 src/IDatabase.hpp diff --git a/src/Database.cpp b/src/Database.cpp index 726bfc5..c4c3ac3 100644 --- a/src/Database.cpp +++ b/src/Database.cpp @@ -31,46 +31,69 @@ #include #include -Database::Database(wxInfoBar& infoBar) +void throw_on_sqlite3_error(int rc) +{ + if (rc != SQLITE_OK) + { + throw std::runtime_error(sqlite3_errstr(rc)); + } +} + +class Sqlite3Statement +{ +public: + Sqlite3Statement(sqlite3 *database, const std::string &query) + { + throw_on_sqlite3_error(sqlite3_prepare_v2(database, query.c_str(), query.size(), &stmt, NULL)); + } + ~Sqlite3Statement() + { + throw_on_sqlite3_error(sqlite3_finalize(stmt)); + } + sqlite3_stmt *stmt = nullptr; +}; + +std::unique_ptr createSqlDatabase(DatabaseType type, wxInfoBar &infoBar, const std::string &dbPath) +{ + switch (type) + { + case DatabaseType::SqlLite: + return std::make_unique(infoBar, dbPath); + default: + return std::unique_ptr(); + } +} + +Database::Database(wxInfoBar &infoBar, const std::string &dbPath) : m_InfoBar(infoBar) { - + open(dbPath); } Database::~Database() { - + close(); } -void Database::CreateTableSamples(const std::string& dbPath) +void Database::CreateTableSamples() { /* Create SQL statement */ std::string samples = "CREATE TABLE IF NOT EXISTS SAMPLES(" - "FAVORITE INT NOT NULL," - "FILENAME TEXT NOT NULL," - "EXTENSION TEXT NOT NULL," - "SAMPLEPACK TEXT NOT NULL," - "TYPE TEXT NOT NULL," - "CHANNELS INT NOT NULL," - "LENGTH INT NOT NULL," - "SAMPLERATE INT NOT NULL," - "BITRATE INT NOT NULL," - "PATH TEXT NOT NULL," - "TRASHED INT NOT NULL," - "HIVE TEXT NOT NULL);"; + "FAVORITE INT NOT NULL," + "FILENAME TEXT NOT NULL," + "EXTENSION TEXT NOT NULL," + "SAMPLEPACK TEXT NOT NULL," + "TYPE TEXT NOT NULL," + "CHANNELS INT NOT NULL," + "LENGTH INT NOT NULL," + "SAMPLERATE INT NOT NULL," + "BITRATE INT NOT NULL," + "PATH TEXT NOT NULL," + "TRASHED INT NOT NULL," + "HIVE TEXT NOT NULL);"; try { - if (sqlite3_open(dbPath.c_str(), &m_Database) != SQLITE_OK) - { - wxLogDebug("Error opening DB"); - throw sqlite3_errmsg(m_Database); - } - else - { - wxLogDebug("Opening DB.."); - } - rc = sqlite3_exec(m_Database, samples.c_str(), NULL, 0, &m_ErrMsg); if (rc != SQLITE_OK) @@ -84,13 +107,6 @@ void Database::CreateTableSamples(const std::string& dbPath) { wxLogDebug("Samples table created successfully."); } - - rc = sqlite3_close(m_Database); - - if (rc == SQLITE_OK) - wxLogDebug("DB Closed.."); - else - wxLogDebug("Error! Cannot close DB, Error code: %d, Error message: %s", rc, m_ErrMsg); } catch (const std::exception &exception) { @@ -98,23 +114,13 @@ void Database::CreateTableSamples(const std::string& dbPath) } } -void Database::CreateTableHives(const std::string& dbPath) +void Database::CreateTableHives() { /* Create SQL statement */ std::string hives = "CREATE TABLE IF NOT EXISTS HIVES(HIVE TEXT NOT NULL);"; try { - if (sqlite3_open(dbPath.c_str(), &m_Database) != SQLITE_OK) - { - wxLogDebug("Error opening DB"); - throw sqlite3_errmsg(m_Database); - } - else - { - wxLogDebug("Opening DB.."); - } - rc = sqlite3_exec(m_Database, hives.c_str(), NULL, 0, &m_ErrMsg); if (rc != SQLITE_OK) @@ -128,13 +134,6 @@ void Database::CreateTableHives(const std::string& dbPath) { wxLogDebug("Hives table created successfully."); } - - rc = sqlite3_close(m_Database); - - if (rc == SQLITE_OK) - wxLogDebug("DB Closed.."); - else - wxLogDebug("Error! Cannot close DB, Error code: %d, Error message: %s", rc, m_ErrMsg); } catch (const std::exception &exception) { @@ -143,41 +142,32 @@ void Database::CreateTableHives(const std::string& dbPath) } //Loops through a Sample array and adds them to the database -void Database::InsertIntoSamples(const std::string& dbPath, std::vector samples) +void Database::InsertIntoSamples(std::vector samples) { try { - if (sqlite3_open(dbPath.c_str(), &m_Database) != SQLITE_OK) - { - wxLogDebug("Error opening DB"); - throw sqlite3_errmsg(m_Database); - } - else - { - wxLogDebug("Opening DB.."); - } - std::string insert = "INSERT INTO SAMPLES (FAVORITE, FILENAME, \ EXTENSION, SAMPLEPACK, TYPE, CHANNELS, LENGTH, \ SAMPLERATE, BITRATE, PATH, TRASHED, HIVE) \ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);"; - rc = sqlite3_prepare_v2(m_Database, insert.c_str(), insert.size(), &m_Stmt, NULL); - + sqlite3_stmt *stmt = nullptr; + rc = sqlite3_prepare_v2(m_Database, insert.c_str(), insert.size(), &stmt, NULL); + rc = sqlite3_exec(m_Database, "BEGIN TRANSACTION", NULL, NULL, &m_ErrMsg); - + if (rc != SQLITE_OK) wxLogDebug("Cannot prepare sql statement.."); - + Sample sample; - + std::string filename; std::string file_extension; std::string sample_pack; std::string type; std::string path; - - for(unsigned int i = 0; i < samples.size(); i++) + + for (unsigned int i = 0; i < samples.size(); i++) { sample = samples[i]; @@ -189,31 +179,31 @@ void Database::InsertIntoSamples(const std::string& dbPath, std::vector std::string hive = "Favorites"; - rc = sqlite3_bind_int(m_Stmt, 1, sample.GetFavorite()); - rc = sqlite3_bind_text(m_Stmt, 2, filename.c_str(), filename.size(), SQLITE_STATIC); - rc = sqlite3_bind_text(m_Stmt, 3, file_extension.c_str(), file_extension.size(), SQLITE_STATIC); - rc = sqlite3_bind_text(m_Stmt, 4, sample_pack.c_str(), sample_pack.size(), SQLITE_STATIC); - rc = sqlite3_bind_text(m_Stmt, 5, type.c_str(), type.size(), SQLITE_STATIC); - rc = sqlite3_bind_int(m_Stmt, 6, sample.GetChannels()); - rc = sqlite3_bind_int(m_Stmt, 7, sample.GetLength()); - rc = sqlite3_bind_int(m_Stmt, 8, sample.GetSampleRate()); - rc = sqlite3_bind_int(m_Stmt, 9, sample.GetBitrate()); - rc = sqlite3_bind_text(m_Stmt, 10, path.c_str(), path.size(), SQLITE_STATIC); - rc = sqlite3_bind_int(m_Stmt, 11, sample.GetTrashed()); - rc = sqlite3_bind_text(m_Stmt, 12, hive.c_str(), hive.size(), SQLITE_STATIC); - - rc = sqlite3_step(m_Stmt); - rc = sqlite3_clear_bindings(m_Stmt); - rc = sqlite3_reset(m_Stmt); + rc = sqlite3_bind_int(stmt, 1, sample.GetFavorite()); + rc = sqlite3_bind_text(stmt, 2, filename.c_str(), filename.size(), SQLITE_STATIC); + rc = sqlite3_bind_text(stmt, 3, file_extension.c_str(), file_extension.size(), SQLITE_STATIC); + rc = sqlite3_bind_text(stmt, 4, sample_pack.c_str(), sample_pack.size(), SQLITE_STATIC); + rc = sqlite3_bind_text(stmt, 5, type.c_str(), type.size(), SQLITE_STATIC); + rc = sqlite3_bind_int(stmt, 6, sample.GetChannels()); + rc = sqlite3_bind_int(stmt, 7, sample.GetLength()); + rc = sqlite3_bind_int(stmt, 8, sample.GetSampleRate()); + rc = sqlite3_bind_int(stmt, 9, sample.GetBitrate()); + rc = sqlite3_bind_text(stmt, 10, path.c_str(), path.size(), SQLITE_STATIC); + rc = sqlite3_bind_int(stmt, 11, sample.GetTrashed()); + rc = sqlite3_bind_text(stmt, 12, hive.c_str(), hive.size(), SQLITE_STATIC); + + rc = sqlite3_step(stmt); + rc = sqlite3_clear_bindings(stmt); + rc = sqlite3_reset(stmt); } rc = sqlite3_exec(m_Database, "END TRANSACTION", NULL, NULL, &m_ErrMsg); - rc = sqlite3_finalize(m_Stmt); + rc = sqlite3_finalize(stmt); if (rc != SQLITE_OK) { - wxLogDebug("Error! Cannot insert data into table. Error code: %d: Msg: %s", rc , sqlite3_errmsg(m_Database)); + wxLogDebug("Error! Cannot insert data into table. Error code: %d: Msg: %s", rc, sqlite3_errmsg(m_Database)); } else { @@ -240,13 +230,6 @@ void Database::InsertIntoSamples(const std::string& dbPath, std::vector wxLogDebug("SQLITE_PERM"); if (rc == SQLITE_INTERNAL) wxLogDebug("SQLITE_INTERNAL"); - - rc = sqlite3_close(m_Database); - - if (rc == SQLITE_OK) - wxLogDebug("DB Closed.."); - else - wxLogDebug("Error! Cannot close DB, Error code: %d, Error message: %s", rc, m_ErrMsg); } catch (const std::exception &exception) { @@ -254,23 +237,14 @@ void Database::InsertIntoSamples(const std::string& dbPath, std::vector } } -void Database::InsertIntoHives(const std::string& dbPath, const std::string& hiveName) +void Database::InsertIntoHives(const std::string &hiveName) { try { - if (sqlite3_open(dbPath.c_str(), &m_Database) != SQLITE_OK) - { - wxLogDebug("Error opening DB"); - throw sqlite3_errmsg(m_Database); - } - else - { - wxLogDebug("Opening DB.."); - } - std::string insert = "INSERT INTO HIVES(HIVE) VALUES(?);"; - rc = sqlite3_prepare_v2(m_Database, insert.c_str(), insert.size(), &m_Stmt, NULL); + sqlite3_stmt *stmt = nullptr; + rc = sqlite3_prepare_v2(m_Database, insert.c_str(), insert.size(), &stmt, NULL); // rc = sqlite3_exec(m_Database, "BEGIN TRANSACTION", NULL, NULL, &m_ErrMsg); @@ -297,21 +271,21 @@ void Database::InsertIntoHives(const std::string& dbPath, const std::string& hiv // std::string hive = "Favourites"; - // rc = sqlite3_bind_int(m_Stmt, 1, sample.GetFavorite()); - rc = sqlite3_bind_text(m_Stmt, 1, hiveName.c_str(), hiveName.size(), SQLITE_STATIC); + // rc = sqlite3_bind_int(stmt, 1, sample.GetFavorite()); + rc = sqlite3_bind_text(stmt, 1, hiveName.c_str(), hiveName.size(), SQLITE_STATIC); - rc = sqlite3_step(m_Stmt); - // rc = sqlite3_clear_bindings(m_Stmt); - // rc = sqlite3_reset(m_Stmt); + rc = sqlite3_step(stmt); + // rc = sqlite3_clear_bindings(stmt); + // rc = sqlite3_reset(stmt); // } // rc = sqlite3_exec(m_Database, "END TRANSACTION", NULL, NULL, &m_ErrMsg); - rc = sqlite3_finalize(m_Stmt); + rc = sqlite3_finalize(stmt); if (rc != SQLITE_OK) { - wxLogDebug("Error! Cannot insert data into table. Error code: %d: Msg: %s", rc , sqlite3_errmsg(m_Database)); + wxLogDebug("Error! Cannot insert data into table. Error code: %d: Msg: %s", rc, sqlite3_errmsg(m_Database)); } else { @@ -338,13 +312,6 @@ void Database::InsertIntoHives(const std::string& dbPath, const std::string& hiv wxLogDebug("SQLITE_PERM"); if (rc == SQLITE_INTERNAL) wxLogDebug("SQLITE_INTERNAL"); - - rc = sqlite3_close(m_Database); - - if (rc == SQLITE_OK) - wxLogDebug("DB Closed.."); - else - wxLogDebug("Error! Cannot close DB, Error code: %d, Error message: %s", rc, m_ErrMsg); } catch (const std::exception &exception) { @@ -352,25 +319,24 @@ void Database::InsertIntoHives(const std::string& dbPath, const std::string& hiv } } -void Database::UpdateHive(const std::string& dbPath, const std::string& hiveOldName, const std::string& hiveNewName) +void Database::UpdateHive(const std::string &hiveOldName, const std::string &hiveNewName) { try { - rc = sqlite3_open(dbPath.c_str(), &m_Database); - std::string update = "UPDATE HIVES SET HIVE = ? WHERE HIVE = ?;"; - rc = sqlite3_prepare_v2(m_Database, update.c_str(), update.size(), &m_Stmt, NULL); + sqlite3_stmt *stmt = nullptr; + rc = sqlite3_prepare_v2(m_Database, update.c_str(), update.size(), &stmt, NULL); - rc = sqlite3_bind_text(m_Stmt, 1, hiveNewName.c_str(), hiveNewName.size(), SQLITE_STATIC); - rc = sqlite3_bind_text(m_Stmt, 2, hiveOldName.c_str(), hiveOldName.size(), SQLITE_STATIC); + rc = sqlite3_bind_text(stmt, 1, hiveNewName.c_str(), hiveNewName.size(), SQLITE_STATIC); + rc = sqlite3_bind_text(stmt, 2, hiveOldName.c_str(), hiveOldName.size(), SQLITE_STATIC); - if (sqlite3_step(m_Stmt) != SQLITE_DONE) + if (sqlite3_step(stmt) != SQLITE_DONE) { wxLogWarning("No data inserted."); } - rc = sqlite3_finalize(m_Stmt); + rc = sqlite3_finalize(stmt); if (rc != SQLITE_OK) { @@ -384,8 +350,6 @@ void Database::UpdateHive(const std::string& dbPath, const std::string& hiveOldN { wxLogDebug("Hive updated successfully. %s", m_ErrMsg); } - - sqlite3_close(m_Database); } catch (const std::exception &exception) { @@ -393,25 +357,24 @@ void Database::UpdateHive(const std::string& dbPath, const std::string& hiveOldN } } -void Database::UpdateHiveName(const std::string& dbPath, const std::string& filename, const std::string& hiveName) +void Database::UpdateHiveName(const std::string &filename, const std::string &hiveName) { try { - rc = sqlite3_open(dbPath.c_str(), &m_Database); - std::string update = "UPDATE SAMPLES SET HIVE = ? WHERE FILENAME = ?;"; - rc = sqlite3_prepare_v2(m_Database, update.c_str(), update.size(), &m_Stmt, NULL); + sqlite3_stmt *stmt = nullptr; + rc = sqlite3_prepare_v2(m_Database, update.c_str(), update.size(), &stmt, NULL); - rc = sqlite3_bind_text(m_Stmt, 1, hiveName.c_str(), hiveName.size(), SQLITE_STATIC); - rc = sqlite3_bind_text(m_Stmt, 2, filename.c_str(), filename.size(), SQLITE_STATIC); + rc = sqlite3_bind_text(stmt, 1, hiveName.c_str(), hiveName.size(), SQLITE_STATIC); + rc = sqlite3_bind_text(stmt, 2, filename.c_str(), filename.size(), SQLITE_STATIC); - if (sqlite3_step(m_Stmt) == SQLITE_ROW) + if (sqlite3_step(stmt) == SQLITE_ROW) { wxLogDebug("Record found, updating.."); } - rc = sqlite3_finalize(m_Stmt); + rc = sqlite3_finalize(stmt); if (rc != SQLITE_OK) { @@ -423,8 +386,6 @@ void Database::UpdateHiveName(const std::string& dbPath, const std::string& file { wxLogDebug("Updated record successfully."); } - - sqlite3_close(m_Database); } catch (const std::exception &exception) { @@ -432,25 +393,24 @@ void Database::UpdateHiveName(const std::string& dbPath, const std::string& file } } -void Database::UpdateFavoriteColumn(const std::string& dbPath, const std::string& filename, int value) +void Database::UpdateFavoriteColumn(const std::string &filename, int value) { try { - rc = sqlite3_open(dbPath.c_str(), &m_Database); - std::string update = "UPDATE SAMPLES SET FAVORITE = ? WHERE FILENAME = ?;"; - rc = sqlite3_prepare_v2(m_Database, update.c_str(), update.size(), &m_Stmt, NULL); + sqlite3_stmt *stmt = nullptr; + rc = sqlite3_prepare_v2(m_Database, update.c_str(), update.size(), &stmt, NULL); - rc = sqlite3_bind_int(m_Stmt, 1, value); - rc = sqlite3_bind_text(m_Stmt, 2, filename.c_str(), filename.size(), SQLITE_STATIC); + rc = sqlite3_bind_int(stmt, 1, value); + rc = sqlite3_bind_text(stmt, 2, filename.c_str(), filename.size(), SQLITE_STATIC); - if (sqlite3_step(m_Stmt) == SQLITE_ROW) + if (sqlite3_step(stmt) == SQLITE_ROW) { wxLogDebug("Record found, updating.."); } - rc = sqlite3_finalize(m_Stmt); + rc = sqlite3_finalize(stmt); if (rc != SQLITE_OK) { @@ -462,8 +422,6 @@ void Database::UpdateFavoriteColumn(const std::string& dbPath, const std::string { wxLogDebug("Updated record successfully."); } - - sqlite3_close(m_Database); } catch (const std::exception &exception) { @@ -471,25 +429,24 @@ void Database::UpdateFavoriteColumn(const std::string& dbPath, const std::string } } -void Database::UpdateSamplePack(const std::string& dbPath, const std::string& filename, const std::string& samplePack) +void Database::UpdateSamplePack(const std::string &filename, const std::string &samplePack) { try { - rc = sqlite3_open(dbPath.c_str(), &m_Database); - std::string update = "UPDATE SAMPLES SET SAMPLEPACK = ? WHERE FILENAME = ?;"; - rc = sqlite3_prepare_v2(m_Database, update.c_str(), update.size(), &m_Stmt, NULL); + sqlite3_stmt *stmt = nullptr; + rc = sqlite3_prepare_v2(m_Database, update.c_str(), update.size(), &stmt, NULL); - rc = sqlite3_bind_text(m_Stmt, 1, samplePack.c_str(), samplePack.size(), SQLITE_STATIC); - rc = sqlite3_bind_text(m_Stmt, 2, filename.c_str(), filename.size(), SQLITE_STATIC); + rc = sqlite3_bind_text(stmt, 1, samplePack.c_str(), samplePack.size(), SQLITE_STATIC); + rc = sqlite3_bind_text(stmt, 2, filename.c_str(), filename.size(), SQLITE_STATIC); - if (sqlite3_step(m_Stmt) == SQLITE_ROW) + if (sqlite3_step(stmt) == SQLITE_ROW) { wxLogDebug("Record found, updating.."); } - rc = sqlite3_finalize(m_Stmt); + rc = sqlite3_finalize(stmt); if (rc != SQLITE_OK) { @@ -501,8 +458,6 @@ void Database::UpdateSamplePack(const std::string& dbPath, const std::string& fi { wxLogDebug("Updated record successfully."); } - - sqlite3_close(m_Database); } catch (const std::exception &exception) { @@ -510,25 +465,24 @@ void Database::UpdateSamplePack(const std::string& dbPath, const std::string& fi } } -void Database::UpdateSampleType(const std::string& dbPath, const std::string& filename, const std::string& type) +void Database::UpdateSampleType(const std::string &filename, const std::string &type) { try { - rc = sqlite3_open(dbPath.c_str(), &m_Database); - std::string update = "UPDATE SAMPLES SET TYPE = ? WHERE FILENAME = ?;"; - rc = sqlite3_prepare_v2(m_Database, update.c_str(), update.size(), &m_Stmt, NULL); + sqlite3_stmt *stmt = nullptr; + rc = sqlite3_prepare_v2(m_Database, update.c_str(), update.size(), &stmt, NULL); - rc = sqlite3_bind_text(m_Stmt, 1, type.c_str(), type.size(), SQLITE_STATIC); - rc = sqlite3_bind_text(m_Stmt, 2, filename.c_str(), filename.size(), SQLITE_STATIC); + rc = sqlite3_bind_text(stmt, 1, type.c_str(), type.size(), SQLITE_STATIC); + rc = sqlite3_bind_text(stmt, 2, filename.c_str(), filename.size(), SQLITE_STATIC); - if (sqlite3_step(m_Stmt) == SQLITE_ROW) + if (sqlite3_step(stmt) == SQLITE_ROW) { wxLogDebug("Record found, updating.."); } - rc = sqlite3_finalize(m_Stmt); + rc = sqlite3_finalize(stmt); if (rc != SQLITE_OK) { @@ -540,8 +494,6 @@ void Database::UpdateSampleType(const std::string& dbPath, const std::string& fi { wxLogDebug("Updated record successfully."); } - - sqlite3_close(m_Database); } catch (const std::exception &exception) { @@ -549,28 +501,27 @@ void Database::UpdateSampleType(const std::string& dbPath, const std::string& fi } } -std::string Database::GetSampleType(const std::string& dbPath, const std::string& filename) +std::string Database::GetSampleType(const std::string &filename) { std::string type; try { - rc = sqlite3_open(dbPath.c_str(), &m_Database); - std::string select = "SELECT TYPE FROM SAMPLES WHERE FILENAME = ?;"; - rc = sqlite3_prepare_v2(m_Database, select.c_str(), select.size(), &m_Stmt, NULL); + sqlite3_stmt *stmt = nullptr; + rc = sqlite3_prepare_v2(m_Database, select.c_str(), select.size(), &stmt, NULL); - rc = sqlite3_bind_text(m_Stmt, 1, filename.c_str(), filename.size(), SQLITE_STATIC); + rc = sqlite3_bind_text(stmt, 1, filename.c_str(), filename.size(), SQLITE_STATIC); - if (sqlite3_step(m_Stmt) == SQLITE_ROW) + if (sqlite3_step(stmt) == SQLITE_ROW) { wxLogDebug("Record found, fetching.."); - type = std::string(reinterpret_cast(sqlite3_column_text(m_Stmt, 0))); + type = std::string(reinterpret_cast(sqlite3_column_text(stmt, 0))); } - rc = sqlite3_finalize(m_Stmt); + rc = sqlite3_finalize(stmt); if (rc != SQLITE_OK) { @@ -583,8 +534,6 @@ std::string Database::GetSampleType(const std::string& dbPath, const std::string { wxLogDebug("Selected data from table successfully."); } - - sqlite3_close(m_Database); } catch (const std::exception &exception) { @@ -594,27 +543,26 @@ std::string Database::GetSampleType(const std::string& dbPath, const std::string return type; } -int Database::GetFavoriteColumnValueByFilename(const std::string& dbPath, const std::string& filename) +int Database::GetFavoriteColumnValueByFilename(const std::string &filename) { int value = 0; try { - rc = sqlite3_open(dbPath.c_str(), &m_Database); - std::string select = "SELECT FAVORITE FROM SAMPLES WHERE FILENAME = ?;"; - rc = sqlite3_prepare_v2(m_Database, select.c_str(), select.size(), &m_Stmt, NULL); + sqlite3_stmt *stmt = nullptr; + rc = sqlite3_prepare_v2(m_Database, select.c_str(), select.size(), &stmt, NULL); - rc = sqlite3_bind_text(m_Stmt, 1, filename.c_str(), filename.size(), SQLITE_STATIC); + rc = sqlite3_bind_text(stmt, 1, filename.c_str(), filename.size(), SQLITE_STATIC); - if (sqlite3_step(m_Stmt) == SQLITE_ROW) + if (sqlite3_step(stmt) == SQLITE_ROW) { wxLogDebug("Record found, fetching.."); - value = sqlite3_column_int(m_Stmt, 0); + value = sqlite3_column_int(stmt, 0); } - rc = sqlite3_finalize(m_Stmt); + rc = sqlite3_finalize(stmt); if (rc != SQLITE_OK) { @@ -627,8 +575,6 @@ int Database::GetFavoriteColumnValueByFilename(const std::string& dbPath, const { wxLogDebug("Selected data from table successfully."); } - - sqlite3_close(m_Database); } catch (const std::exception &exception) { @@ -638,28 +584,27 @@ int Database::GetFavoriteColumnValueByFilename(const std::string& dbPath, const return value; } -std::string Database::GetHiveByFilename(const std::string& dbPath, const std::string& filename) +std::string Database::GetHiveByFilename(const std::string &filename) { std::string hive; try { - rc = sqlite3_open(dbPath.c_str(), &m_Database); - std::string select = "SELECT HIVE FROM SAMPLES WHERE FILENAME = ?;"; - rc = sqlite3_prepare_v2(m_Database, select.c_str(), select.size(), &m_Stmt, NULL); + sqlite3_stmt *stmt = nullptr; + rc = sqlite3_prepare_v2(m_Database, select.c_str(), select.size(), &stmt, NULL); - rc = sqlite3_bind_text(m_Stmt, 1, filename.c_str(), filename.size(), SQLITE_STATIC); + rc = sqlite3_bind_text(stmt, 1, filename.c_str(), filename.size(), SQLITE_STATIC); - if (sqlite3_step(m_Stmt) == SQLITE_ROW) + if (sqlite3_step(stmt) == SQLITE_ROW) { wxLogDebug("Record found, fetching.."); - hive = std::string(reinterpret_cast(sqlite3_column_text(m_Stmt, 0))); + hive = std::string(reinterpret_cast(sqlite3_column_text(stmt, 0))); } - rc = sqlite3_finalize(m_Stmt); + rc = sqlite3_finalize(stmt); if (rc != SQLITE_OK) { @@ -672,8 +617,6 @@ std::string Database::GetHiveByFilename(const std::string& dbPath, const std::st { wxLogDebug("Selected data from table successfully."); } - - sqlite3_close(m_Database); } catch (const std::exception &exception) { @@ -683,24 +626,23 @@ std::string Database::GetHiveByFilename(const std::string& dbPath, const std::st return hive; } -void Database::RemoveSampleFromDatabase(const std::string& dbPath, const std::string& filename) +void Database::RemoveSampleFromDatabase(const std::string &filename) { try { - rc = sqlite3_open(dbPath.c_str(), &m_Database); - std::string remove = "DELETE FROM SAMPLES WHERE FILENAME = ?;"; - rc = sqlite3_prepare_v2(m_Database, remove.c_str(), remove.size(), &m_Stmt, NULL); + sqlite3_stmt *stmt = nullptr; + rc = sqlite3_prepare_v2(m_Database, remove.c_str(), remove.size(), &stmt, NULL); - rc = sqlite3_bind_text(m_Stmt, 1, filename.c_str(), filename.size(), SQLITE_STATIC); + rc = sqlite3_bind_text(stmt, 1, filename.c_str(), filename.size(), SQLITE_STATIC); - if (sqlite3_step(m_Stmt) == SQLITE_DONE) + if (sqlite3_step(stmt) == SQLITE_DONE) { wxLogDebug("Record found, Deleting.."); } - rc = sqlite3_finalize(m_Stmt); + rc = sqlite3_finalize(stmt); if (rc != SQLITE_OK) { @@ -713,8 +655,6 @@ void Database::RemoveSampleFromDatabase(const std::string& dbPath, const std::st { wxLogDebug("Deleted data from table successfully."); } - - sqlite3_close(m_Database); } catch (const std::exception &exception) { @@ -722,24 +662,23 @@ void Database::RemoveSampleFromDatabase(const std::string& dbPath, const std::st } } -void Database::RemoveHiveFromDatabase(const std::string& dbPath, const std::string& hiveName) +void Database::RemoveHiveFromDatabase(const std::string &hiveName) { try { - rc = sqlite3_open(dbPath.c_str(), &m_Database); - std::string remove = "DELETE FROM HIVES WHERE HIVE = ?;"; - rc = sqlite3_prepare_v2(m_Database, remove.c_str(), remove.size(), &m_Stmt, NULL); + sqlite3_stmt *stmt = nullptr; + rc = sqlite3_prepare_v2(m_Database, remove.c_str(), remove.size(), &stmt, NULL); - rc = sqlite3_bind_text(m_Stmt, 1, hiveName.c_str(), hiveName.size(), SQLITE_STATIC); + rc = sqlite3_bind_text(stmt, 1, hiveName.c_str(), hiveName.size(), SQLITE_STATIC); - if (sqlite3_step(m_Stmt) == SQLITE_DONE) + if (sqlite3_step(stmt) == SQLITE_DONE) { wxLogDebug("Record found, Deleting.."); } - rc = sqlite3_finalize(m_Stmt); + rc = sqlite3_finalize(stmt); if (rc != SQLITE_OK) { @@ -752,8 +691,6 @@ void Database::RemoveHiveFromDatabase(const std::string& dbPath, const std::stri { wxLogDebug("Deleted data from table successfully."); } - - sqlite3_close(m_Database); } catch (const std::exception &exception) { @@ -761,27 +698,26 @@ void Database::RemoveHiveFromDatabase(const std::string& dbPath, const std::stri } } -std::string Database::GetSamplePathByFilename(const std::string& dbPath, const std::string& filename) +std::string Database::GetSamplePathByFilename(const std::string &filename) { std::string path; try { - rc = sqlite3_open(dbPath.c_str(), &m_Database); - std::string select = "SELECT PATH FROM SAMPLES WHERE FILENAME = ?;"; - rc = sqlite3_prepare_v2(m_Database, select.c_str(), select.size(), &m_Stmt, NULL); + sqlite3_stmt *stmt = nullptr; + rc = sqlite3_prepare_v2(m_Database, select.c_str(), select.size(), &stmt, NULL); - rc = sqlite3_bind_text(m_Stmt, 1, filename.c_str(), filename.size(), SQLITE_STATIC); + rc = sqlite3_bind_text(stmt, 1, filename.c_str(), filename.size(), SQLITE_STATIC); - if (sqlite3_step(m_Stmt) == SQLITE_ROW) + if (sqlite3_step(stmt) == SQLITE_ROW) { wxLogDebug("Record found, fetching.."); - path = std::string(reinterpret_cast(sqlite3_column_text(m_Stmt, 0))); + path = std::string(reinterpret_cast(sqlite3_column_text(stmt, 0))); } - rc = sqlite3_finalize(m_Stmt); + rc = sqlite3_finalize(stmt); if (rc != SQLITE_OK) { @@ -794,8 +730,6 @@ std::string Database::GetSamplePathByFilename(const std::string& dbPath, const s { wxLogDebug("Selected data from table successfully."); } - - sqlite3_close(m_Database); } catch (const std::exception &exception) { @@ -805,27 +739,26 @@ std::string Database::GetSamplePathByFilename(const std::string& dbPath, const s return path; } -std::string Database::GetSampleFileExtension(const std::string& dbPath, const std::string& filename) +std::string Database::GetSampleFileExtension(const std::string &filename) { std::string extension; try { - rc = sqlite3_open(dbPath.c_str(), &m_Database); - std::string select = "SELECT EXTENSION FROM SAMPLES WHERE FILENAME = ?;"; - rc = sqlite3_prepare_v2(m_Database, select.c_str(), select.size(), &m_Stmt, NULL); + sqlite3_stmt *stmt = nullptr; + rc = sqlite3_prepare_v2(m_Database, select.c_str(), select.size(), &stmt, NULL); - rc = sqlite3_bind_text(m_Stmt, 1, filename.c_str(), filename.size(), SQLITE_STATIC); + rc = sqlite3_bind_text(stmt, 1, filename.c_str(), filename.size(), SQLITE_STATIC); - if (sqlite3_step(m_Stmt) == SQLITE_ROW) + if (sqlite3_step(stmt) == SQLITE_ROW) { wxLogDebug("Record found, fetching.."); - extension = std::string(reinterpret_cast(sqlite3_column_text(m_Stmt, 0))); + extension = std::string(reinterpret_cast(sqlite3_column_text(stmt, 0))); } - rc = sqlite3_finalize(m_Stmt); + rc = sqlite3_finalize(stmt); if (rc != SQLITE_OK) { @@ -838,8 +771,6 @@ std::string Database::GetSampleFileExtension(const std::string& dbPath, const st { wxLogDebug("Selected data from table successfully."); } - - sqlite3_close(m_Database); } catch (const std::exception &exception) { @@ -850,161 +781,150 @@ std::string Database::GetSampleFileExtension(const std::string& dbPath, const st } wxVector> -Database::LoadSamplesDatabase(const std::string& dbPath, wxVector>& vecSet, +Database::LoadSamplesDatabase(wxVector> &vecSet, // wxTreeCtrl& favorite_tree, wxTreeItemId& favorite_item, - wxDataViewTreeCtrl& favorite_tree, wxDataViewItem& favorite_item, - wxTreeCtrl& trash_tree, wxTreeItemId& trash_item, bool show_extension, - const std::string& icon_star_filled, const std::string& icon_star_empty) + wxDataViewTreeCtrl &favorite_tree, wxDataViewItem &favorite_item, + wxTreeCtrl &trash_tree, wxTreeItemId &trash_item, bool show_extension, + const std::string &icon_star_filled, const std::string &icon_star_empty) { + wxVariant icon_filled, icon_empty; + icon_filled = wxVariant(wxBitmap(icon_star_filled)); + icon_empty = wxVariant(wxBitmap(icon_star_empty)); try { - if (sqlite3_open(dbPath.c_str(), &m_Database) != SQLITE_OK) + + int numRows = 0; + Sqlite3Statement statement1(m_Database, "SELECT Count(*) FROM SAMPLES;"); + if (SQLITE_ROW == sqlite3_step(statement1.stmt)) { - wxLogDebug("Error opening DB"); - throw sqlite3_errmsg(m_Database); + numRows = sqlite3_column_int(statement1.stmt, 0); + wxLogDebug("rows %d", numRows); + vecSet.reserve(numRows); } - std::string load = "SELECT FAVORITE, FILENAME, EXTENSION, SAMPLEPACK, \ + Sqlite3Statement statement(m_Database, "SELECT FAVORITE, FILENAME, EXTENSION, SAMPLEPACK, \ TYPE, CHANNELS, LENGTH, SAMPLERATE, BITRATE, PATH, \ - TRASHED, HIVE FROM SAMPLES;"; + TRASHED, HIVE FROM SAMPLES;"); - rc = sqlite3_prepare_v2(m_Database, load.c_str(), load.size(), &m_Stmt, NULL); + int row = 0; - if (rc == SQLITE_OK) + while (SQLITE_ROW == sqlite3_step(statement.stmt)) { - int row = 0; + int favorite = sqlite3_column_int(statement.stmt, 0); + wxString filename = std::string(reinterpret_cast(sqlite3_column_text(statement.stmt, 1))); + wxString file_extension = std::string(reinterpret_cast(sqlite3_column_text(statement.stmt, 2))); + wxString sample_pack = std::string(reinterpret_cast(sqlite3_column_text(statement.stmt, 3))); + wxString sample_type = std::string(reinterpret_cast(sqlite3_column_text(statement.stmt, 4))); + int channels = sqlite3_column_int(statement.stmt, 5); + int length = sqlite3_column_int(statement.stmt, 6); + int sample_rate = sqlite3_column_int(statement.stmt, 7); + int bitrate = sqlite3_column_int(statement.stmt, 8); + wxString path = std::string(reinterpret_cast(sqlite3_column_text(statement.stmt, 9))); + int trashed = sqlite3_column_int(statement.stmt, 10); + wxString hive_name = std::string(reinterpret_cast(sqlite3_column_text(statement.stmt, 11))); - while (SQLITE_ROW == sqlite3_step(m_Stmt)) + wxLongLong llLength = length; + int total_min = static_cast((llLength / 60000).GetValue()); + int total_sec = static_cast(((llLength % 60000) / 1000).GetValue()); + + wxVector vec; + vec.reserve(12); + if (trashed == 1) { - int favorite = sqlite3_column_int(m_Stmt, 0); - wxString filename = std::string(reinterpret_cast(sqlite3_column_text(m_Stmt, 1))); - wxString file_extension = std::string(reinterpret_cast(sqlite3_column_text(m_Stmt, 2))); - wxString sample_pack = std::string(reinterpret_cast(sqlite3_column_text(m_Stmt, 3))); - wxString sample_type = std::string(reinterpret_cast(sqlite3_column_text(m_Stmt, 4))); - int channels = sqlite3_column_int(m_Stmt, 5); - int length = sqlite3_column_int(m_Stmt, 6); - int sample_rate = sqlite3_column_int(m_Stmt, 7); - int bitrate = sqlite3_column_int(m_Stmt, 8); - wxString path = std::string(reinterpret_cast(sqlite3_column_text(m_Stmt, 9))); - int trashed = sqlite3_column_int(m_Stmt, 10); - wxString hive_name = std::string(reinterpret_cast(sqlite3_column_text(m_Stmt, 11))); - - wxLongLong llLength = length; - int total_min = static_cast((llLength / 60000).GetValue()); - int total_sec = static_cast(((llLength % 60000) / 1000).GetValue()); - - wxVector vec; - - if (trashed == 1) + if (show_extension) + trash_tree.AppendItem(trash_item, wxString::Format("%s.%s", filename, file_extension)); + else + trash_tree.AppendItem(trash_item, filename); + } + else + { + if (favorite == 1) { - if (show_extension) - trash_tree.AppendItem(trash_item, wxString::Format("%s.%s", filename, file_extension)); - else - trash_tree.AppendItem(trash_item, filename); + // vec.push_back(true); + vec.push_back(icon_filled); + + wxLogDebug("Loading hives.."); + + std::deque nodes; + nodes.push_back(favorite_tree.GetNthChild(wxDataViewItem(wxNullPtr), 0)); + + wxDataViewItem current_item, found_item; + + int row = 0; + int hive_count = favorite_tree.GetChildCount(wxDataViewItem(wxNullPtr)); + + while (!nodes.empty()) + { + current_item = nodes.front(); + nodes.pop_front(); + + if (favorite_tree.GetItemText(current_item) == hive_name) + { + found_item = current_item; + wxLogDebug("Loading, hive name: %s", hive_name); + break; + } + + wxDataViewItem child = favorite_tree.GetNthChild(wxDataViewItem(wxNullPtr), 0); + + while (row < (hive_count - 1)) + { + row++; + + child = favorite_tree.GetNthChild(wxDataViewItem(wxNullPtr), row); + nodes.push_back(child); + } + } + + nodes.clear(); + + if (found_item.IsOk()) + { + // wxLogDebug("Another hive by the name %s already exist. Please try with a different name.", + // hive_name); + if (show_extension) + favorite_tree.AppendItem(found_item, + wxString::Format("%s.%s", filename, file_extension)); + else + favorite_tree.AppendItem(found_item, filename); + } + // else + // { + // favorite_tree.AppendItem(wxDataViewItem(wxNullPtr), hive_name); + // } + } + else + // vec.push_back(false); + vec.push_back(icon_empty); + + if (show_extension) + { + vec.push_back(path.AfterLast('/')); } else { - wxVariant icon_filled, icon_empty; - icon_filled = wxVariant(wxBitmap(icon_star_filled)); - icon_empty = wxVariant(wxBitmap(icon_star_empty)); - - if (favorite == 1) - { - // vec.push_back(true); - vec.push_back(icon_filled); - - wxLogDebug("Loading hives.."); - - std::deque nodes; - nodes.push_back(favorite_tree.GetNthChild(wxDataViewItem(wxNullPtr), 0)); - - wxDataViewItem current_item, found_item; - - int row = 0; - int hive_count = favorite_tree.GetChildCount(wxDataViewItem(wxNullPtr)); - - while(!nodes.empty()) - { - current_item = nodes.front(); - nodes.pop_front(); - - if (favorite_tree.GetItemText(current_item) == hive_name) - { - found_item = current_item; - wxLogDebug("Loading, hive name: %s", hive_name); - break; - } - - wxDataViewItem child = favorite_tree.GetNthChild(wxDataViewItem(wxNullPtr), 0); - - while (row < (hive_count - 1)) - { - row ++; - - child = favorite_tree.GetNthChild(wxDataViewItem(wxNullPtr), row); - nodes.push_back(child); - } - } - - nodes.clear(); - - if (found_item.IsOk()) - { - // wxLogDebug("Another hive by the name %s already exist. Please try with a different name.", - // hive_name); - if (show_extension) - favorite_tree.AppendItem(found_item, - wxString::Format("%s.%s", filename, file_extension)); - else - favorite_tree.AppendItem(found_item, filename); - } - // else - // { - // favorite_tree.AppendItem(wxDataViewItem(wxNullPtr), hive_name); - // } - - } - else - // vec.push_back(false); - vec.push_back(icon_empty); - - if (show_extension) - { - vec.push_back(path.AfterLast('/')); - } - else - { - vec.push_back(path.AfterLast('/').BeforeLast('.')); - } - - vec.push_back(sample_pack); - vec.push_back(sample_type); - vec.push_back(wxString::Format("%d", channels)); - vec.push_back(wxString::Format("%2i:%02i", total_min, total_sec)); - vec.push_back(wxString::Format("%d", sample_rate)); - vec.push_back(wxString::Format("%d", bitrate)); - vec.push_back(path); - - vecSet.push_back(vec); + vec.push_back(path.AfterLast('/').BeforeLast('.')); } + vec.push_back(sample_pack); + vec.push_back(sample_type); + vec.push_back(wxString::Format("%d", channels)); + vec.push_back(wxString::Format("%2i:%02i", total_min, total_sec)); + vec.push_back(wxString::Format("%d", sample_rate)); + vec.push_back(wxString::Format("%d", bitrate)); + vec.push_back(path); - row++; + vecSet.push_back(vec); } - } - else - { - wxMessageDialog msgDialog(NULL, "Error! Cannot load data from table.", - "Error", wxOK | wxICON_ERROR); - msgDialog.ShowModal(); - sqlite3_free(m_ErrMsg); - } - rc = sqlite3_finalize(m_Stmt); - - sqlite3_close(m_Database); + row++; + } } catch (const std::exception &exception) { + wxMessageDialog msgDialog(NULL, "Error! Cannot load data from table.", + "Error", wxOK | wxICON_ERROR); + msgDialog.ShowModal(); + sqlite3_free(m_ErrMsg); wxLogDebug(exception.what()); } @@ -1012,42 +932,40 @@ Database::LoadSamplesDatabase(const std::string& dbPath, wxVector> -Database::FilterDatabaseBySampleName(const std::string& dbPath, wxVector>& sampleVec, - const std::string& sampleName, bool show_extension, - const std::string& icon_star_filled, const std::string& icon_star_empty) +Database::FilterDatabaseBySampleName(wxVector> &sampleVec, + const std::string &sampleName, bool show_extension, + const std::string &icon_star_filled, const std::string &icon_star_empty) { + wxVariant icon_filled, icon_empty; + icon_filled = wxVariant(wxBitmap(icon_star_filled)); + icon_empty = wxVariant(wxBitmap(icon_star_empty)); try { - if (sqlite3_open(dbPath.c_str(), &m_Database) != SQLITE_OK) - { - wxLogDebug("Error opening DB"); - throw sqlite3_errmsg(m_Database); - } std::string filter = "SELECT FAVORITE, FILENAME, SAMPLEPACK, TYPE, \ CHANNELS, LENGTH, SAMPLERATE, BITRATE, PATH \ - FROM SAMPLES WHERE FILENAME LIKE '%' || ? || '%';"; + FROM SAMPLES WHERE FILENAME LIKE '%' || ? || '%' ;"; + sqlite3_stmt *stmt = nullptr; - rc = sqlite3_prepare_v2(m_Database, filter.c_str(), filter.size(), &m_Stmt, NULL); - - rc = sqlite3_bind_text(m_Stmt, 1, sampleName.c_str(), sampleName.size(), SQLITE_STATIC); + rc = sqlite3_prepare_v2(m_Database, filter.c_str(), filter.size(), &stmt, NULL); + rc = sqlite3_bind_text(stmt, 1, sampleName.c_str(), sampleName.size(), SQLITE_STATIC); if (rc == SQLITE_OK) { int row = 0; - while (SQLITE_ROW == sqlite3_step(m_Stmt)) + while (SQLITE_ROW == sqlite3_step(stmt)) { wxLogDebug("Record found, fetching.."); - int favorite = sqlite3_column_int(m_Stmt, 0); - wxString filename = wxString(std::string(reinterpret_cast(sqlite3_column_text(m_Stmt, 1)))); - wxString sample_pack = wxString(std::string(reinterpret_cast(sqlite3_column_text(m_Stmt, 2)))); - wxString sample_type = std::string(reinterpret_cast(sqlite3_column_text(m_Stmt, 3))); - int channels = sqlite3_column_int(m_Stmt, 4); - int length = sqlite3_column_int(m_Stmt, 5); - int sample_rate = sqlite3_column_int(m_Stmt, 6); - int bitrate = sqlite3_column_int(m_Stmt, 7); - wxString path = wxString(std::string(reinterpret_cast(sqlite3_column_text(m_Stmt, 8)))); + int favorite = sqlite3_column_int(stmt, 0); + wxString filename = wxString(std::string(reinterpret_cast(sqlite3_column_text(stmt, 1)))); + wxString sample_pack = wxString(std::string(reinterpret_cast(sqlite3_column_text(stmt, 2)))); + wxString sample_type = std::string(reinterpret_cast(sqlite3_column_text(stmt, 3))); + int channels = sqlite3_column_int(stmt, 4); + int length = sqlite3_column_int(stmt, 5); + int sample_rate = sqlite3_column_int(stmt, 6); + int bitrate = sqlite3_column_int(stmt, 7); + wxString path = wxString(std::string(reinterpret_cast(sqlite3_column_text(stmt, 8)))); wxLongLong llLength = length; int total_min = static_cast((llLength / 60000).GetValue()); @@ -1055,10 +973,6 @@ Database::FilterDatabaseBySampleName(const std::string& dbPath, wxVector vec; - wxVariant icon_filled, icon_empty; - icon_filled = wxVariant(wxBitmap(icon_star_filled)); - icon_empty = wxVariant(wxBitmap(icon_star_empty)); - if (favorite == 1) vec.push_back(icon_filled); else @@ -1101,9 +1015,7 @@ Database::FilterDatabaseBySampleName(const std::string& dbPath, wxVector> -Database::FilterDatabaseByHiveName(const std::string& dbPath, wxVector>& sampleVec, - const std::string& hiveName, bool show_extension, - const std::string& icon_star_filled, const std::string& icon_star_empty) +Database::FilterDatabaseByHiveName(wxVector> &sampleVec, + const std::string &hiveName, bool show_extension, + const std::string &icon_star_filled, const std::string &icon_star_empty) { + wxVariant icon_filled, icon_empty; + icon_filled = wxVariant(wxBitmap(icon_star_filled)); + icon_empty = wxVariant(wxBitmap(icon_star_empty)); try { - if (sqlite3_open(dbPath.c_str(), &m_Database) != SQLITE_OK) - { - wxLogDebug("Error opening DB"); - throw sqlite3_errmsg(m_Database); - } - std::string filter = "SELECT FAVORITE, FILENAME, SAMPLEPACK, TYPE, \ CHANNELS, LENGTH, SAMPLERATE, BITRATE, PATH \ FROM SAMPLES WHERE HIVE = ? AND FAVORITE = 1;"; - rc = sqlite3_prepare_v2(m_Database, filter.c_str(), filter.size(), &m_Stmt, NULL); + sqlite3_stmt *stmt = nullptr; + rc = sqlite3_prepare_v2(m_Database, filter.c_str(), filter.size(), &stmt, NULL); - rc = sqlite3_bind_text(m_Stmt, 1, hiveName.c_str(), hiveName.size(), SQLITE_STATIC); + rc = sqlite3_bind_text(stmt, 1, hiveName.c_str(), hiveName.size(), SQLITE_STATIC); if (rc == SQLITE_OK) { int row = 0; - while (SQLITE_ROW == sqlite3_step(m_Stmt)) + while (SQLITE_ROW == sqlite3_step(stmt)) { wxLogDebug("Record found, fetching.."); - int favorite = sqlite3_column_int(m_Stmt, 0); - wxString filename = wxString(std::string(reinterpret_cast(sqlite3_column_text(m_Stmt, 1)))); - wxString sample_pack = wxString(std::string(reinterpret_cast(sqlite3_column_text(m_Stmt, 2)))); - wxString sample_type = std::string(reinterpret_cast(sqlite3_column_text(m_Stmt, 3))); - int channels = sqlite3_column_int(m_Stmt, 4); - int length = sqlite3_column_int(m_Stmt, 5); - int sample_rate = sqlite3_column_int(m_Stmt, 6); - int bitrate = sqlite3_column_int(m_Stmt, 7); - wxString path = wxString(std::string(reinterpret_cast(sqlite3_column_text(m_Stmt, 8)))); + int favorite = sqlite3_column_int(stmt, 0); + wxString filename = wxString(std::string(reinterpret_cast(sqlite3_column_text(stmt, 1)))); + wxString sample_pack = wxString(std::string(reinterpret_cast(sqlite3_column_text(stmt, 2)))); + wxString sample_type = std::string(reinterpret_cast(sqlite3_column_text(stmt, 3))); + int channels = sqlite3_column_int(stmt, 4); + int length = sqlite3_column_int(stmt, 5); + int sample_rate = sqlite3_column_int(stmt, 6); + int bitrate = sqlite3_column_int(stmt, 7); + wxString path = wxString(std::string(reinterpret_cast(sqlite3_column_text(stmt, 8)))); wxLongLong llLength = length; int total_min = static_cast((llLength / 60000).GetValue()); @@ -1157,10 +1067,6 @@ Database::FilterDatabaseByHiveName(const std::string& dbPath, wxVector vec; - wxVariant icon_filled, icon_empty; - icon_filled = wxVariant(wxBitmap(icon_star_filled)); - icon_empty = wxVariant(wxBitmap(icon_star_empty)); - if (favorite == 1) vec.push_back(icon_filled); else @@ -1203,9 +1109,7 @@ Database::FilterDatabaseByHiveName(const std::string& dbPath, wxVector(sqlite3_column_text(m_Stmt, 0)))); + wxString hive = wxString(std::string(reinterpret_cast(sqlite3_column_text(stmt, 0)))); treeCtrl.AppendContainer(wxDataViewItem(wxNullPtr), hive); } @@ -1247,9 +1146,7 @@ void Database::LoadHivesDatabase(const std::string& dbPath, wxDataViewTreeCtrl& sqlite3_free(m_ErrMsg); } - rc = sqlite3_finalize(m_Stmt); - - sqlite3_close(m_Database); + rc = sqlite3_finalize(stmt); } catch (const std::exception &exception) { @@ -1258,7 +1155,7 @@ void Database::LoadHivesDatabase(const std::string& dbPath, wxDataViewTreeCtrl& } //Compares the input array with the database and removes duplicates. -wxArrayString Database::CheckDuplicates(const std::string& dbPath, const wxArrayString& files) +wxArrayString Database::CheckDuplicates(const wxArrayString &files) { wxArrayString sorted_files; @@ -1267,59 +1164,56 @@ wxArrayString Database::CheckDuplicates(const std::string& dbPath, const wxArray try { - rc = sqlite3_open(dbPath.c_str(), &m_Database); - std::string select = "SELECT * FROM SAMPLES WHERE FILENAME = ?;"; - rc = sqlite3_prepare_v2(m_Database, select.c_str(), select.size(), &m_Stmt, NULL); - - for(unsigned int i = 0; i < files.size(); i++) + sqlite3_stmt *stmt = nullptr; + throw_on_sqlite3_error(sqlite3_prepare_v2(m_Database, select.c_str(), select.size(), &stmt, NULL)); + + for (unsigned int i = 0; i < files.size(); i++) { filename = files[i].AfterLast('/').BeforeLast('.').ToStdString(); - rc = sqlite3_bind_text(m_Stmt, 1, filename.c_str(), filename.size(), SQLITE_STATIC); - - if (sqlite3_step(m_Stmt) != SQLITE_ROW) + rc = sqlite3_bind_text(stmt, 1, filename.c_str(), filename.size(), SQLITE_STATIC); + + if (sqlite3_step(stmt) != SQLITE_ROW) sorted_files.push_back(files[i]); else wxLogDebug("Already added: %s. Skipping..", files[i]); - rc = sqlite3_clear_bindings(m_Stmt); - rc = sqlite3_reset(m_Stmt); + rc = sqlite3_clear_bindings(stmt); + rc = sqlite3_reset(stmt); } - - sqlite3_finalize(m_Stmt); - sqlite3_close(m_Database); + + sqlite3_finalize(stmt); } catch (const std::exception &exception) { wxLogDebug(exception.what()); } - return sorted_files; + return sorted_files; } -bool Database::IsTrashed(const std::string& dbPath, const std::string& filename) +bool Database::IsTrashed(const std::string &filename) { try { - rc = sqlite3_open(dbPath.c_str(), &m_Database); - std::string select = "SELECT TRASHED FROM SAMPLES WHERE FILENAME = ?;"; - rc = sqlite3_prepare_v2(m_Database, select.c_str(), select.size(), &m_Stmt, NULL); + sqlite3_stmt *stmt = nullptr; + rc = sqlite3_prepare_v2(m_Database, select.c_str(), select.size(), &stmt, NULL); - rc = sqlite3_bind_text(m_Stmt, 1, filename.c_str(), filename.size(), SQLITE_STATIC); + rc = sqlite3_bind_text(stmt, 1, filename.c_str(), filename.size(), SQLITE_STATIC); - if (sqlite3_step(m_Stmt) == SQLITE_ROW) + if (sqlite3_step(stmt) == SQLITE_ROW) { wxLogDebug("Record found, fetching.."); - if (sqlite3_column_int(m_Stmt, 0) == 1) + if (sqlite3_column_int(stmt, 0) == 1) return true; } - rc = sqlite3_finalize(m_Stmt); + rc = sqlite3_finalize(stmt); if (rc != SQLITE_OK) { @@ -1332,8 +1226,6 @@ bool Database::IsTrashed(const std::string& dbPath, const std::string& filename) { wxLogDebug("Selected data from table successfully."); } - - sqlite3_close(m_Database); } catch (const std::exception &exception) { @@ -1343,25 +1235,24 @@ bool Database::IsTrashed(const std::string& dbPath, const std::string& filename) return false; } -void Database::UpdateTrashColumn(const std::string& dbPath, const std::string& filename, int value) +void Database::UpdateTrashColumn(const std::string &filename, int value) { try { - rc = sqlite3_open(dbPath.c_str(), &m_Database); - std::string update = "UPDATE SAMPLES SET TRASHED = ? WHERE FILENAME = ?;"; + sqlite3_stmt *stmt = nullptr; - rc = sqlite3_prepare_v2(m_Database, update.c_str(), update.size(), &m_Stmt, NULL); + rc = sqlite3_prepare_v2(m_Database, update.c_str(), update.size(), &stmt, NULL); - rc = sqlite3_bind_int(m_Stmt, 1, value); - rc = sqlite3_bind_text(m_Stmt, 2, filename.c_str(), filename.size(), SQLITE_STATIC); + rc = sqlite3_bind_int(stmt, 1, value); + rc = sqlite3_bind_text(stmt, 2, filename.c_str(), filename.size(), SQLITE_STATIC); - if (sqlite3_step(m_Stmt) == SQLITE_ROW) + if (sqlite3_step(stmt) == SQLITE_ROW) { wxLogDebug("Record found, updating.."); } - rc = sqlite3_finalize(m_Stmt); + rc = sqlite3_finalize(stmt); if (rc != SQLITE_OK) { @@ -1374,8 +1265,6 @@ void Database::UpdateTrashColumn(const std::string& dbPath, const std::string& f { wxLogDebug("Updated record successfully."); } - - sqlite3_close(m_Database); } catch (const std::exception &exception) { @@ -1384,42 +1273,40 @@ void Database::UpdateTrashColumn(const std::string& dbPath, const std::string& f } wxVector> -Database::RestoreFromTrashByFilename(const std::string& dbPath, const std::string& filename, - wxVector>& vecSet, bool show_extension, - const std::string& icon_star_filled, const std::string& icon_star_empty) +Database::RestoreFromTrashByFilename(const std::string &filename, + wxVector> &vecSet, bool show_extension, + const std::string &icon_star_filled, const std::string &icon_star_empty) { + wxVariant icon_filled, icon_empty; + icon_filled = wxVariant(wxBitmap(icon_star_filled)); + icon_empty = wxVariant(wxBitmap(icon_star_empty)); try { - if (sqlite3_open(dbPath.c_str(), &m_Database) != SQLITE_OK) - { - wxLogDebug("Error opening DB"); - throw sqlite3_errmsg(m_Database); - } - std::string restore = "SELECT FAVORITE, FILENAME, EXTENSION, SAMPLEPACK, \ TYPE, CHANNELS, LENGTH, SAMPLERATE, BITRATE, PATH, \ TRASHED, HIVE FROM SAMPLES WHERE FILENAME = ?;"; + sqlite3_stmt *stmt = nullptr; - rc = sqlite3_prepare_v2(m_Database, restore.c_str(), restore.size(), &m_Stmt, NULL); + rc = sqlite3_prepare_v2(m_Database, restore.c_str(), restore.size(), &stmt, NULL); - rc = sqlite3_bind_text(m_Stmt, 1, filename.c_str(), filename.size(), SQLITE_STATIC); + rc = sqlite3_bind_text(stmt, 1, filename.c_str(), filename.size(), SQLITE_STATIC); if (rc == SQLITE_OK) { - while (SQLITE_ROW == sqlite3_step(m_Stmt)) + while (SQLITE_ROW == sqlite3_step(stmt)) { - int favorite = sqlite3_column_int(m_Stmt, 0); - wxString filename = std::string(reinterpret_cast(sqlite3_column_text(m_Stmt, 1))); - wxString file_extension = std::string(reinterpret_cast(sqlite3_column_text(m_Stmt, 2))); - wxString sample_pack = std::string(reinterpret_cast(sqlite3_column_text(m_Stmt, 3))); - wxString sample_type = std::string(reinterpret_cast(sqlite3_column_text(m_Stmt, 4))); - int channels = sqlite3_column_int(m_Stmt, 5); - int length = sqlite3_column_int(m_Stmt, 6); - int sample_rate = sqlite3_column_int(m_Stmt, 7); - int bitrate = sqlite3_column_int(m_Stmt, 8); - wxString path = std::string(reinterpret_cast(sqlite3_column_text(m_Stmt, 9))); - int trashed = sqlite3_column_int(m_Stmt, 10); - wxString hive_name = std::string(reinterpret_cast(sqlite3_column_text(m_Stmt, 11))); + int favorite = sqlite3_column_int(stmt, 0); + wxString filename = std::string(reinterpret_cast(sqlite3_column_text(stmt, 1))); + wxString file_extension = std::string(reinterpret_cast(sqlite3_column_text(stmt, 2))); + wxString sample_pack = std::string(reinterpret_cast(sqlite3_column_text(stmt, 3))); + wxString sample_type = std::string(reinterpret_cast(sqlite3_column_text(stmt, 4))); + int channels = sqlite3_column_int(stmt, 5); + int length = sqlite3_column_int(stmt, 6); + int sample_rate = sqlite3_column_int(stmt, 7); + int bitrate = sqlite3_column_int(stmt, 8); + wxString path = std::string(reinterpret_cast(sqlite3_column_text(stmt, 9))); + int trashed = sqlite3_column_int(stmt, 10); + wxString hive_name = std::string(reinterpret_cast(sqlite3_column_text(stmt, 11))); wxLongLong llLength = length; int total_min = static_cast((llLength / 60000).GetValue()); @@ -1427,10 +1314,6 @@ Database::RestoreFromTrashByFilename(const std::string& dbPath, const std::strin wxVector vec; - wxVariant icon_filled, icon_empty; - icon_filled = wxVariant(wxBitmap(icon_star_filled)); - icon_empty = wxVariant(wxBitmap(icon_star_empty)); - if (trashed == 0) { if (favorite == 1) @@ -1463,9 +1346,7 @@ Database::RestoreFromTrashByFilename(const std::string& dbPath, const std::strin sqlite3_free(m_ErrMsg); } - rc = sqlite3_finalize(m_Stmt); - - sqlite3_close(m_Database); + rc = sqlite3_finalize(stmt); } catch (const std::exception &exception) { @@ -1474,3 +1355,30 @@ Database::RestoreFromTrashByFilename(const std::string& dbPath, const std::strin return vecSet; } + +void Database::open(const std::string &dbPath) +{ + try + { + throw_on_sqlite3_error(sqlite3_open(dbPath.c_str(), &m_Database)); + + const auto qry = std::string("pragma journal_mode = WAL; pragma synchronous = normal; pragma temp_store = memory; pragma mmap_size = 30000000000;"); + + sqlite3_stmt *stmt = nullptr; + throw_on_sqlite3_error(sqlite3_prepare_v2(m_Database, qry.c_str(), qry.size(), &stmt, NULL)); + if (SQLITE_ROW != sqlite3_step(stmt)) + { + wxLogDebug("Error executing query"); + } + throw_on_sqlite3_error(sqlite3_finalize(stmt)); + } + catch (const std::exception &exception) + { + wxLogDebug(exception.what()); + } +} + +void Database::close() +{ + throw_on_sqlite3_error(sqlite3_close(m_Database)); +} diff --git a/src/Database.hpp b/src/Database.hpp index 0736db8..add61ac 100644 --- a/src/Database.hpp +++ b/src/Database.hpp @@ -33,10 +33,12 @@ #include -class Database +#include "IDatabase.hpp" + +class Database : public IDatabase { public: - Database(wxInfoBar& infoBar); + Database(wxInfoBar& infoBar, const std::string& dbPath); ~Database(); private: @@ -44,69 +46,71 @@ class Database sqlite3* m_Database; int rc; char* m_ErrMsg; - sqlite3_stmt* m_Stmt; private: // ------------------------------------------------------------------- wxInfoBar& m_InfoBar; + void open(const std::string& dbPath); + void close(); + public: // ------------------------------------------------------------------- // Create the table - void CreateTableSamples(const std::string& dbPath); - void CreateTableHives(const std::string& dbPath); + void CreateTableSamples() override; + void CreateTableHives() override; // ------------------------------------------------------------------- // Insert into database - void InsertIntoSamples(const std::string& dbPath, std::vector); - void InsertIntoHives(const std::string& dbPath, const std::string& hiveName); + void InsertIntoSamples(std::vector) override; + void InsertIntoHives(const std::string& hiveName) override; // ------------------------------------------------------------------- // Update database - void UpdateFavoriteColumn(const std::string& dbPath, const std::string& filename, int value); - void UpdateHive(const std::string& dbPath, const std::string& hiveOldName, const std::string& hiveNewName); - void UpdateHiveName(const std::string& dbPath, const std::string& filename, const std::string& hiveName); - void UpdateTrashColumn(const std::string& dbPath, const std::string& filename, int value); - void UpdateSamplePack(const std::string& dbPath, const std::string& filename, const std::string& samplePack); - void UpdateSampleType(const std::string& dbPath, const std::string& filename, const std::string& type); + void UpdateFavoriteColumn(const std::string& filename, int value) override; + void UpdateHive(const std::string& hiveOldName, const std::string& hiveNewName) override; + void UpdateHiveName(const std::string& filename, const std::string& hiveName) override; + void UpdateTrashColumn(const std::string& filename, int value) override; + void UpdateSamplePack(const std::string& filename, const std::string& samplePack) override; + void UpdateSampleType(const std::string& filename, const std::string& type) override; // ------------------------------------------------------------------- // Get from database - int GetFavoriteColumnValueByFilename(const std::string& dbPath, const std::string& filename); - std::string GetHiveByFilename(const std::string& dbPath, const std::string& filename); - std::string GetSamplePathByFilename(const std::string& dbPath, const std::string& filename); - std::string GetSampleFileExtension(const std::string& dbPath, const std::string& filename); - std::string GetSampleType(const std::string& dbPath, const std::string& filename); + int GetFavoriteColumnValueByFilename(const std::string& filename) override; + std::string GetHiveByFilename(const std::string& filename) override; + std::string GetSamplePathByFilename(const std::string& filename) override; + std::string GetSampleFileExtension(const std::string& filename) override; + std::string GetSampleType(const std::string& filename) override; // ------------------------------------------------------------------- // Check database - bool IsTrashed(const std::string& dbPath, const std::string& filename); - wxArrayString CheckDuplicates(const std::string& dbPath, const wxArrayString& files); + bool IsTrashed(const std::string& filename) override; + wxArrayString CheckDuplicates(const wxArrayString& files) override; // ------------------------------------------------------------------- // Remove from database - void RemoveSampleFromDatabase(const std::string& dbPath, const std::string& filename); - void RemoveHiveFromDatabase(const std::string& dbPath, const std::string& hiveName); + void RemoveSampleFromDatabase(const std::string& filename) override; + void RemoveHiveFromDatabase(const std::string& hiveName) override; // ------------------------------------------------------------------- wxVector> // LoadDatabase(wxVector> &vecSet, // wxTreeCtrl& favorite_tree, wxTreeItemId& favorite_item, - // wxTreeCtrl& trash_tree, wxTreeItemId& trash_item, bool show_extension); - LoadSamplesDatabase(const std::string& dbPath, wxVector>& vecSet, + // wxTreeCtrl& trash_tree, wxTreeItemId& trash_item, bool show_extension) override; + LoadSamplesDatabase(wxVector>& vecSet, wxDataViewTreeCtrl& favorite_tree, wxDataViewItem& favorite_item, wxTreeCtrl& trash_tree, wxTreeItemId& trash_item, bool show_extension, - const std::string& icon_star_filled, const std::string& icon_star_emtpy); - void LoadHivesDatabase(const std::string& dbPath, wxDataViewTreeCtrl& favorite_tree); + const std::string& icon_star_filled, const std::string& icon_star_emtpy) override; + void LoadHivesDatabase(wxDataViewTreeCtrl& favorite_tree) override; wxVector> - RestoreFromTrashByFilename(const std::string& dbPath, const std::string& filename, + RestoreFromTrashByFilename(const std::string& filename, wxVector>& vecSet, bool show_extension, - const std::string& icon_star_filled, const std::string& icon_star_empty); + const std::string& icon_star_filled, const std::string& icon_star_empty) override; wxVector> - FilterDatabaseBySampleName(const std::string& dbPath, wxVector>& sampleVec, + FilterDatabaseBySampleName(wxVector>& sampleVec, const std::string& sampleName, bool show_extension, - const std::string& icon_star_filled, const std::string& icon_star_empty); + const std::string& icon_star_filled, const std::string& icon_star_empty) override; wxVector> - FilterDatabaseByHiveName(const std::string& dbPath, wxVector>& sampleVec, + FilterDatabaseByHiveName(wxVector>& sampleVec, const std::string& hiveName, bool show_extension, - const std::string& icon_star_filled, const std::string& icon_star_empty); + const std::string& icon_star_filled, const std::string& icon_star_empty) override; }; diff --git a/src/IDatabase.hpp b/src/IDatabase.hpp new file mode 100644 index 0000000..8299ca1 --- /dev/null +++ b/src/IDatabase.hpp @@ -0,0 +1,96 @@ +/* SampleHive + * Copyright (C) 2021 Apoorv Singh + * A simple, modern audio sample browser/manager for GNU/Linux. + * + * This file is a part of SampleHive + * + * SampleHive 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. + * + * SampleHive 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 this program. If not, see . + */ +#pragma once + +#include +#include +#include +#include "Sample.hpp" +#include + +class IDatabase +{ public: + virtual ~IDatabase() = default; + // ------------------------------------------------------------------- + // Create the table + virtual void CreateTableSamples() = 0; + virtual void CreateTableHives() = 0; + + // ------------------------------------------------------------------- + // Insert into database + virtual void InsertIntoSamples(std::vector) = 0; + virtual void InsertIntoHives(const std::string& hiveName) = 0; + + // ------------------------------------------------------------------- + // Update database + virtual void UpdateFavoriteColumn(const std::string& filename, int value) = 0; + virtual void UpdateHive(const std::string& hiveOldName, const std::string& hiveNewName) = 0; + virtual void UpdateHiveName(const std::string& filename, const std::string& hiveName) = 0; + virtual void UpdateTrashColumn(const std::string& filename, int value) = 0; + virtual void UpdateSamplePack(const std::string& filename, const std::string& samplePack) = 0; + virtual void UpdateSampleType(const std::string& filename, const std::string& type) = 0; + + // ------------------------------------------------------------------- + // Get from database + virtual int GetFavoriteColumnValueByFilename(const std::string& filename) = 0; + virtual std::string GetHiveByFilename(const std::string& filename) = 0; + virtual std::string GetSamplePathByFilename(const std::string& filename) = 0; + virtual std::string GetSampleFileExtension(const std::string& filename) = 0; + virtual std::string GetSampleType(const std::string& filename) = 0; + + // ------------------------------------------------------------------- + // Check database + virtual bool IsTrashed(const std::string& filename) = 0; + virtual wxArrayString CheckDuplicates(const wxArrayString& files) = 0; + + // ------------------------------------------------------------------- + // Remove from database + virtual void RemoveSampleFromDatabase(const std::string& filename) = 0; + virtual void RemoveHiveFromDatabase(const std::string& hiveName) = 0; + + // ------------------------------------------------------------------- + virtual wxVector> + // LoadDatabase(wxVector> &vecSet, + // wxTreeCtrl& favorite_tree, wxTreeItemId& favorite_item, + // wxTreeCtrl& trash_tree, wxTreeItemId& trash_item, bool show_extension) = 0; + LoadSamplesDatabase(wxVector>& vecSet, + wxDataViewTreeCtrl& favorite_tree, wxDataViewItem& favorite_item, + wxTreeCtrl& trash_tree, wxTreeItemId& trash_item, bool show_extension, + const std::string& icon_star_filled, const std::string& icon_star_emtpy) = 0; + virtual void LoadHivesDatabase(wxDataViewTreeCtrl& favorite_tree) = 0; + virtual wxVector> + RestoreFromTrashByFilename(const std::string& filename, + wxVector>& vecSet, bool show_extension, + const std::string& icon_star_filled, const std::string& icon_star_empty) = 0; + virtual wxVector> + FilterDatabaseBySampleName(wxVector>& sampleVec, + const std::string& sampleName, bool show_extension, + const std::string& icon_star_filled, const std::string& icon_star_empty) = 0; + virtual wxVector> + FilterDatabaseByHiveName(wxVector>& sampleVec, + const std::string& hiveName, bool show_extension, + const std::string& icon_star_filled, const std::string& icon_star_empty) = 0; +}; + +enum class DatabaseType { + SqlLite +}; + +std::unique_ptr createSqlDatabase(DatabaseType type, wxInfoBar& infoBar, const std::string& dbPath); \ No newline at end of file diff --git a/src/MainFrame.cpp b/src/MainFrame.cpp index 292ded2..5247694 100644 --- a/src/MainFrame.cpp +++ b/src/MainFrame.cpp @@ -544,9 +544,9 @@ MainFrame::MainFrame() m_BottomRightPanelMainSizer->Layout(); // Initialize the database - Database db(*m_InfoBar); - db.CreateTableSamples(static_cast(DATABASE_FILEPATH)); - db.CreateTableHives(static_cast(DATABASE_FILEPATH)); + m_pDatabase = createSqlDatabase(DatabaseType::SqlLite, *m_InfoBar, m_DatabaseFilepath); + m_pDatabase->CreateTableSamples(); + m_pDatabase->CreateTableHives(); // Restore the data previously added to Library LoadDatabase(); @@ -582,7 +582,6 @@ void MainFrame::OnClickSettings(wxCommandEvent& event) void MainFrame::AddSamples(wxArrayString& files) { Settings settings(this, m_ConfigFilepath, m_DatabaseFilepath); - Database db(*m_InfoBar); wxBusyCursor busy_cursor; wxWindowDisabler window_disabler; @@ -605,7 +604,7 @@ void MainFrame::AddSamples(wxArrayString& files) //Check All Files At Once wxArrayString sorted_files; - sorted_files = db.CheckDuplicates(static_cast(DATABASE_FILEPATH), files); + sorted_files = m_pDatabase->CheckDuplicates(files); files = sorted_files; if(files.size() < 1) @@ -686,7 +685,7 @@ void MainFrame::AddSamples(wxArrayString& files) progressDialog->Pulse(_("Updating Database.."), NULL); - db.InsertIntoSamples(static_cast(DATABASE_FILEPATH), sample_array); + m_pDatabase->InsertIntoSamples(sample_array); progressDialog->Destroy(); } @@ -750,7 +749,6 @@ void MainFrame::OnDragAndDropToLibrary(wxDropFilesEvent& event) void MainFrame::OnDragAndDropToHives(wxDropFilesEvent& event) { - Database db(*m_InfoBar); Settings settings(this, m_ConfigFilepath, m_DatabaseFilepath); if (event.GetNumberOfFiles() > 0) @@ -787,27 +785,23 @@ void MainFrame::OnDragAndDropToHives(wxDropFilesEvent& event) rows - i, files[i], m_Hives->GetItemText(drop_target)); if (drop_target.IsOk() && m_Hives->IsContainer(drop_target) && - db.GetFavoriteColumnValueByFilename(static_cast(DATABASE_FILEPATH), - file_name.ToStdString()) == 0) + m_pDatabase->GetFavoriteColumnValueByFilename( file_name.ToStdString()) == 0) { m_Hives->AppendItem(drop_target, files[i]); m_Library->SetValue(wxVariant(wxBitmap(ICON_STAR_FILLED_16px)), row, 0); - db.UpdateFavoriteColumn(static_cast(DATABASE_FILEPATH), file_name.ToStdString(), 1); - db.UpdateHiveName(static_cast(DATABASE_FILEPATH), - file_name.ToStdString(), hive_name.ToStdString()); + m_pDatabase->UpdateFavoriteColumn(file_name.ToStdString(), 1); + m_pDatabase->UpdateHiveName( file_name.ToStdString(), hive_name.ToStdString()); msg = wxString::Format(_("%s added to %s."), files[i], hive_name); } else { - if (db.GetFavoriteColumnValueByFilename(static_cast(DATABASE_FILEPATH), - file_name.ToStdString()) == 1) + if (m_pDatabase->GetFavoriteColumnValueByFilename( file_name.ToStdString()) == 1) { wxMessageBox(wxString::Format(_("%s is already added to %s hive"), files[i], - db.GetHiveByFilename(static_cast(DATABASE_FILEPATH), - file_name.ToStdString())), + m_pDatabase->GetHiveByFilename( file_name.ToStdString())), _("Error!"), wxOK | wxICON_ERROR | wxCENTRE, this); } else @@ -901,20 +895,19 @@ void MainFrame::OnDragFromDirCtrl(wxTreeEvent& event) void MainFrame::OnDragFromLibrary(wxDataViewEvent& event) { // Settings settings(m_ConfigFilepath, m_DatabaseFilepath); - // Database db(*m_InfoBar); - + // int selected_row = m_Library->ItemToRow(event.GetItem()); if (selected_row < 0) return; wxString selection = m_Library->GetTextValue(selected_row, 1); - // wxString sample_with_extension = db.GetSamplePathByFilename(selection.BeforeLast('.').ToStdString()); - // wxString sample_without_extension = db.GetSamplePathByFilename(selection.ToStdString()); + // wxString sample_with_extension = m_pDatabase->GetSamplePathByFilename(selection.BeforeLast('.').ToStdString()); + // wxString sample_without_extension = m_pDatabase->GetSamplePathByFilename(selection.ToStdString()); // std::string extension = settings.ShouldShowFileExtension() ? - // db.GetSampleFileExtension(selection.ToStdString()) : - // db.GetSampleFileExtension(selection.BeforeLast('.').ToStdString()); + // m_pDatabase->GetSampleFileExtension(selection.ToStdString()) : + // m_pDatabase->GetSampleFileExtension(selection.BeforeLast('.').ToStdString()); // wxString sample = selection.Contains(wxString::Format(".%s", extension)) ? // sample_with_extension : sample_without_extension; @@ -934,8 +927,7 @@ void MainFrame::OnClickPlay(wxCommandEvent& event) bStopped = false; // Settings settings(m_ConfigFilepath, m_DatabaseFilepath); - // Database db(*m_InfoBar); - + // int selected_row = m_Library->GetSelectedRow(); if (selected_row < 0) @@ -943,12 +935,12 @@ void MainFrame::OnClickPlay(wxCommandEvent& event) wxString selection = m_Library->GetTextValue(selected_row, 1); - // wxString sample_with_extension = db.GetSamplePathByFilename(selection.BeforeLast('.').ToStdString()); - // wxString sample_without_extension = db.GetSamplePathByFilename(selection.ToStdString()); + // wxString sample_with_extension = m_pDatabase->GetSamplePathByFilename(selection.BeforeLast('.').ToStdString()); + // wxString sample_without_extension = m_pDatabase->GetSamplePathByFilename(selection.ToStdString()); // std::string extension = settings.ShouldShowFileExtension() ? - // db.GetSampleFileExtension(selection.ToStdString()) : - // db.GetSampleFileExtension(selection.BeforeLast('.').ToStdString()); + // m_pDatabase->GetSampleFileExtension(selection.ToStdString()) : + // m_pDatabase->GetSampleFileExtension(selection.BeforeLast('.').ToStdString()); // wxString sample = selection.Contains(wxString::Format(".%s", extension)) ? // sample_with_extension : sample_without_extension; @@ -1091,9 +1083,7 @@ void MainFrame::OnReleaseVolumeSlider(wxScrollEvent& event) void MainFrame::OnClickLibrary(wxDataViewEvent& event) { Settings settings(m_ConfigFilepath, m_DatabaseFilepath); - Database db(*m_InfoBar); - - int selected_row = m_Library->ItemToRow(event.GetItem()); + int selected_row = m_Library->ItemToRow(event.GetItem()); int current_row = m_Library->ItemToRow(m_Library->GetCurrentItem()); @@ -1139,12 +1129,12 @@ void MainFrame::OnClickLibrary(wxDataViewEvent& event) // else // selection = m_Library->GetTextValue(selected_row, 1); - // wxString sample_with_extension = db.GetSamplePathByFilename(selection.BeforeLast('.').ToStdString()); - // wxString sample_without_extension = db.GetSamplePathByFilename(selection.ToStdString()); + // wxString sample_with_extension = m_pDatabase->GetSamplePathByFilename(selection.BeforeLast('.').ToStdString()); + // wxString sample_without_extension = m_pDatabase->GetSamplePathByFilename(selection.ToStdString()); // std::string extension = settings.ShouldShowFileExtension() ? - // db.GetSampleFileExtension(selection.ToStdString()) : - // db.GetSampleFileExtension(selection.BeforeLast('.').ToStdString()); + // m_pDatabase->GetSampleFileExtension(selection.ToStdString()) : + // m_pDatabase->GetSampleFileExtension(selection.BeforeLast('.').ToStdString()); // wxString sample = selection.Contains(wxString::Format(".%s", extension)) ? // sample_with_extension : sample_without_extension; @@ -1187,12 +1177,12 @@ void MainFrame::OnClickLibrary(wxDataViewEvent& event) wxDataViewItem container; wxDataViewItem child; - if (db.GetFavoriteColumnValueByFilename(static_cast(DATABASE_FILEPATH), filename) == 0) + if (m_pDatabase->GetFavoriteColumnValueByFilename(filename) == 0) { m_Library->SetValue(wxVariant(wxBitmap(ICON_STAR_FILLED_16px)), selected_row, 0); - db.UpdateFavoriteColumn(static_cast(DATABASE_FILEPATH), filename, 1); - db.UpdateHiveName(static_cast(DATABASE_FILEPATH), filename, hive_name); + m_pDatabase->UpdateFavoriteColumn(filename, 1); + m_pDatabase->UpdateHiveName(filename, hive_name); for (int i = 0; i < m_Hives->GetChildCount(root); i++) { @@ -1211,9 +1201,8 @@ void MainFrame::OnClickLibrary(wxDataViewEvent& event) { m_Library->SetValue(wxVariant(wxBitmap(ICON_STAR_EMPTY_16px)), selected_row, 0); - db.UpdateFavoriteColumn(static_cast(DATABASE_FILEPATH), filename, 0); - db.UpdateHiveName(static_cast(DATABASE_FILEPATH), - filename, m_Hives->GetItemText(favorites_hive).ToStdString()); + m_pDatabase->UpdateFavoriteColumn(filename, 0); + m_pDatabase->UpdateHiveName( filename, m_Hives->GetItemText(favorites_hive).ToStdString()); for (int i = 0; i < m_Hives->GetChildCount(root); i++) { @@ -1242,9 +1231,7 @@ void MainFrame::OnClickLibrary(wxDataViewEvent& event) void MainFrame::OnShowHivesContextMenu(wxDataViewEvent& event) { Settings settings(this, m_ConfigFilepath, m_DatabaseFilepath); - Database db(*m_InfoBar); - - wxDataViewItem selected_hive = event.GetItem(); + wxDataViewItem selected_hive = event.GetItem(); wxString hive_name = m_Hives->GetItemText(selected_hive); @@ -1342,8 +1329,7 @@ void MainFrame::OnShowHivesContextMenu(wxDataViewEvent& event) wxLogDebug("Sample count: %d", sample_count); m_Hives->SetItemText(selected_hive, hive_name); - db.UpdateHive(static_cast(DATABASE_FILEPATH), - selected_hive_name.ToStdString(), hive_name.ToStdString()); + m_pDatabase->UpdateHive( selected_hive_name.ToStdString(), hive_name.ToStdString()); } else { @@ -1357,10 +1343,8 @@ void MainFrame::OnShowHivesContextMenu(wxDataViewEvent& event) wxLogDebug("Sample count: %d :: Sample name: %s", sample_count, sample_name); - db.UpdateHiveName(static_cast(DATABASE_FILEPATH), - sample_name.ToStdString(), hive_name.ToStdString()); - db.UpdateHive(static_cast(DATABASE_FILEPATH), - selected_hive_name.ToStdString(), hive_name.ToStdString()); + m_pDatabase->UpdateHiveName( sample_name.ToStdString(), hive_name.ToStdString()); + m_pDatabase->UpdateHive( selected_hive_name.ToStdString(), hive_name.ToStdString()); m_Hives->SetItemText(selected_hive, hive_name); } @@ -1429,8 +1413,7 @@ void MainFrame::OnShowHivesContextMenu(wxDataViewEvent& event) { m_Hives->DeleteItem(selected_hive); - db.RemoveHiveFromDatabase(static_cast(DATABASE_FILEPATH), - hive_name.ToStdString()); + m_pDatabase->RemoveHiveFromDatabase( hive_name.ToStdString()); msg = wxString::Format(_("%s deleted from hives successfully."), hive_name); } @@ -1471,10 +1454,8 @@ void MainFrame::OnShowHivesContextMenu(wxDataViewEvent& event) m_Library->SetValue(wxVariant(wxBitmap(ICON_STAR_EMPTY_16px)), i, 0); - db.UpdateFavoriteColumn(static_cast(DATABASE_FILEPATH), - matched_sample.ToStdString(), 0); - db.UpdateHiveName(static_cast(DATABASE_FILEPATH), - matched_sample.ToStdString(), + m_pDatabase->UpdateFavoriteColumn( matched_sample.ToStdString(), 0); + m_pDatabase->UpdateHiveName( matched_sample.ToStdString(), m_Hives->GetItemText(favorites_hive).ToStdString()); break; @@ -1487,8 +1468,7 @@ void MainFrame::OnShowHivesContextMenu(wxDataViewEvent& event) m_Hives->DeleteChildren(selected_hive); m_Hives->DeleteItem(selected_hive); - db.RemoveHiveFromDatabase(static_cast(DATABASE_FILEPATH), - hive_name.ToStdString()); + m_pDatabase->RemoveHiveFromDatabase( hive_name.ToStdString()); msg = wxString::Format( _("%s and all samples inside %s have been deleted from hives successfully."), @@ -1514,8 +1494,7 @@ void MainFrame::OnShowHivesContextMenu(wxDataViewEvent& event) { wxVector> dataset; - if (db.FilterDatabaseByHiveName(static_cast(DATABASE_FILEPATH), - dataset, hive_name.ToStdString(), + if (m_pDatabase->FilterDatabaseByHiveName( dataset, hive_name.ToStdString(), settings.ShouldShowFileExtension(), ICON_STAR_FILLED_16px, ICON_STAR_EMPTY_16px).empty()) { @@ -1549,8 +1528,7 @@ void MainFrame::OnShowHivesContextMenu(wxDataViewEvent& event) { wxVector> dataset; - if (db.FilterDatabaseBySampleName(static_cast(DATABASE_FILEPATH), - dataset, "", settings.ShouldShowFileExtension(), + if (m_pDatabase->FilterDatabaseBySampleName( dataset, "", settings.ShouldShowFileExtension(), ICON_STAR_FILLED_16px, ICON_STAR_EMPTY_16px).empty()) { wxMessageBox(_("Error! Database is empty."), _("Error!"), @@ -1601,8 +1579,8 @@ void MainFrame::OnShowHivesContextMenu(wxDataViewEvent& event) m_Library->SetValue(wxVariant(wxBitmap(ICON_STAR_EMPTY_16px)), i, 0); - db.UpdateFavoriteColumn(static_cast(DATABASE_FILEPATH), matched_sample.ToStdString(), 0); - db.UpdateHiveName(static_cast(DATABASE_FILEPATH), matched_sample.ToStdString(), + m_pDatabase->UpdateFavoriteColumn(matched_sample.ToStdString(), 0); + m_pDatabase->UpdateHiveName(matched_sample.ToStdString(), m_Hives->GetItemText(favorites_hive).ToStdString()); m_Hives->DeleteItem(selected_hive); @@ -1612,8 +1590,7 @@ void MainFrame::OnShowHivesContextMenu(wxDataViewEvent& event) m_InfoBar->ShowMessage(wxString::Format(_("Removed %s from %s"), m_Hives->GetItemText(event.GetItem()), - db.GetHiveByFilename(static_cast(DATABASE_FILEPATH), - matched_sample.ToStdString())), + m_pDatabase->GetHiveByFilename(matched_sample.ToStdString())), wxICON_INFORMATION); } break; @@ -1652,9 +1629,7 @@ void MainFrame::OnShowLibraryContextMenu(wxDataViewEvent& event) { TagEditor* tagEditor; Settings settings(this, m_ConfigFilepath, m_DatabaseFilepath); - Database db(*m_InfoBar); - - wxString msg; + wxString msg; wxDataViewItem item = event.GetItem(); int selected_row; @@ -1675,7 +1650,7 @@ void MainFrame::OnShowLibraryContextMenu(wxDataViewEvent& event) //true = add false = remove bool favorite_add = false; - if (db.GetFavoriteColumnValueByFilename(static_cast(DATABASE_FILEPATH), filename) == 1) + if (m_pDatabase->GetFavoriteColumnValueByFilename(filename) == 1) menu.Append(MN_FavoriteSample, _("Remove from hive"), _("Remove the selected sample(s) from hive")); else { @@ -1729,7 +1704,7 @@ void MainFrame::OnShowLibraryContextMenu(wxDataViewEvent& event) filename = settings.ShouldShowFileExtension() ? name.BeforeLast('.').ToStdString() : name.ToStdString(); - db_status = db.GetFavoriteColumnValueByFilename(static_cast(DATABASE_FILEPATH), filename); + db_status = m_pDatabase->GetFavoriteColumnValueByFilename(filename); // Aleady Added, Do Nothing if (favorite_add && db_status == 1) @@ -1744,8 +1719,8 @@ void MainFrame::OnShowLibraryContextMenu(wxDataViewEvent& event) { m_Library->SetValue(wxVariant(wxBitmap(ICON_STAR_FILLED_16px)), selected_row, 0); - db.UpdateFavoriteColumn(static_cast(DATABASE_FILEPATH), filename, 1); - db.UpdateHiveName(static_cast(DATABASE_FILEPATH), filename, hive_name); + m_pDatabase->UpdateFavoriteColumn(filename, 1); + m_pDatabase->UpdateHiveName(filename, hive_name); for (int i = 0; i < m_Hives->GetChildCount(root); i++) { @@ -1765,8 +1740,8 @@ void MainFrame::OnShowLibraryContextMenu(wxDataViewEvent& event) //Remove From Favorites m_Library->SetValue(wxVariant(wxBitmap(ICON_STAR_EMPTY_16px)), selected_row, 0); - db.UpdateFavoriteColumn(static_cast(DATABASE_FILEPATH), filename, 0); - db.UpdateHiveName(static_cast(DATABASE_FILEPATH), filename, + m_pDatabase->UpdateFavoriteColumn(filename, 0); + m_pDatabase->UpdateHiveName(filename, m_Hives->GetItemText(favorites_hive).ToStdString()); for (int i = 0; i < m_Hives->GetChildCount(root); i++) @@ -1829,7 +1804,7 @@ void MainFrame::OnShowLibraryContextMenu(wxDataViewEvent& event) { wxLogDebug("Selected row: %d :: Sample: %s", selected_row, filename); - db.RemoveSampleFromDatabase(static_cast(DATABASE_FILEPATH), filename); + m_pDatabase->RemoveSampleFromDatabase(filename); m_Library->DeleteItem(selected_row); for (int j = 0; j < m_Hives->GetChildCount(root); j++) @@ -1879,7 +1854,7 @@ void MainFrame::OnShowLibraryContextMenu(wxDataViewEvent& event) std::string multi_selection = settings.ShouldShowFileExtension() ? text_value.BeforeLast('.').ToStdString() : text_value.ToStdString() ; - db.RemoveSampleFromDatabase(static_cast(DATABASE_FILEPATH), multi_selection); + m_pDatabase->RemoveSampleFromDatabase(multi_selection); m_Library->DeleteItem(row); for (int j = 0; j < m_Hives->GetChildCount(root); j++) @@ -1921,7 +1896,7 @@ void MainFrame::OnShowLibraryContextMenu(wxDataViewEvent& event) wxDataViewItem root = wxDataViewItem(wxNullPtr); wxDataViewItem container, child; - if (db.IsTrashed(static_cast(DATABASE_FILEPATH), filename)) + if (m_pDatabase->IsTrashed(filename)) wxLogDebug(_("Already trashed..")); else { @@ -1946,11 +1921,11 @@ void MainFrame::OnShowLibraryContextMenu(wxDataViewEvent& event) files = file_data.GetFilenames(); - if (db.GetFavoriteColumnValueByFilename(static_cast(DATABASE_FILEPATH), files[i].ToStdString())) + if (m_pDatabase->GetFavoriteColumnValueByFilename(files[i].ToStdString())) { m_Library->SetValue(wxVariant(wxBitmap(ICON_STAR_EMPTY_16px)), item_row, 0); - db.UpdateFavoriteColumn(static_cast(DATABASE_FILEPATH), files[i].ToStdString(), 0); + m_pDatabase->UpdateFavoriteColumn(files[i].ToStdString(), 0); for (int j = 0; j < m_Hives->GetChildCount(root); j++) { @@ -1973,8 +1948,8 @@ void MainFrame::OnShowLibraryContextMenu(wxDataViewEvent& event) } } - db.UpdateTrashColumn(static_cast(DATABASE_FILEPATH), files[i].ToStdString(), 1); - db.UpdateHiveName(static_cast(DATABASE_FILEPATH), files[i].ToStdString(), + m_pDatabase->UpdateTrashColumn(files[i].ToStdString(), 1); + m_pDatabase->UpdateHiveName(files[i].ToStdString(), m_Hives->GetItemText(favorites_hive).ToStdString()); m_Trash->AppendItem(trash_root, text_value); @@ -2081,15 +2056,13 @@ void MainFrame::OnShowLibraryColumnHeaderContextMenu(wxDataViewEvent& event) void MainFrame::LoadDatabase() { Settings settings(this, m_ConfigFilepath, m_DatabaseFilepath); - Database db(*m_InfoBar); - - try + try { - db.LoadHivesDatabase(static_cast(DATABASE_FILEPATH), *m_Hives); + m_pDatabase->LoadHivesDatabase(*m_Hives); wxVector> dataset; - if (db.LoadSamplesDatabase(static_cast(DATABASE_FILEPATH), dataset, *m_Hives, favorites_hive, + if (m_pDatabase->LoadSamplesDatabase(dataset, *m_Hives, favorites_hive, *m_Trash, trash_root, settings.ShouldShowFileExtension(), ICON_STAR_FILLED_16px, ICON_STAR_EMPTY_16px).empty()) { @@ -2112,9 +2085,7 @@ void MainFrame::LoadDatabase() void MainFrame::OnShowTrashContextMenu(wxTreeEvent& event) { Settings settings(this, m_ConfigFilepath, m_DatabaseFilepath); - Database db(*m_InfoBar); - - wxTreeItemId selected_trashed_item = event.GetItem(); + wxTreeItemId selected_trashed_item = event.GetItem(); wxMenu menu; @@ -2133,7 +2104,7 @@ void MainFrame::OnShowTrashContextMenu(wxTreeEvent& event) m_Trash->GetItemText(selected_trashed_item).BeforeLast('.') : m_Trash->GetItemText(selected_trashed_item); - db.RemoveSampleFromDatabase(static_cast(DATABASE_FILEPATH), trashed_item_name.ToStdString()); + m_pDatabase->RemoveSampleFromDatabase(trashed_item_name.ToStdString()); m_Trash->Delete(selected_trashed_item); } @@ -2142,9 +2113,7 @@ void MainFrame::OnShowTrashContextMenu(wxTreeEvent& event) { wxLogDebug(_("Restore sample")); - Database db(*m_InfoBar); - - wxArrayTreeItemIds selected_item_ids; + wxArrayTreeItemIds selected_item_ids; m_Trash->GetSelections(selected_item_ids); wxFileDataObject file_data; @@ -2166,13 +2135,13 @@ void MainFrame::OnShowTrashContextMenu(wxTreeEvent& event) files = file_data.GetFilenames(); - db.UpdateTrashColumn(static_cast(DATABASE_FILEPATH), files[i].ToStdString(), 0); + m_pDatabase->UpdateTrashColumn(files[i].ToStdString(), 0); try { wxVector> dataset; - if (db.RestoreFromTrashByFilename(static_cast(DATABASE_FILEPATH), files[i].ToStdString(), + if (m_pDatabase->RestoreFromTrashByFilename(files[i].ToStdString(), dataset, settings.ShouldShowFileExtension(), ICON_STAR_FILLED_16px, ICON_STAR_EMPTY_16px).empty()) { @@ -2203,7 +2172,6 @@ void MainFrame::OnShowTrashContextMenu(wxTreeEvent& event) void MainFrame::OnDragAndDropToTrash(wxDropFilesEvent& event) { - Database db(*m_InfoBar); Settings settings(this, m_ConfigFilepath, m_DatabaseFilepath); if (event.GetNumberOfFiles() > 0) @@ -2233,11 +2201,11 @@ void MainFrame::OnDragAndDropToTrash(wxDropFilesEvent& event) files = file_data.GetFilenames(); - if (db.GetFavoriteColumnValueByFilename(static_cast(DATABASE_FILEPATH), files[i].ToStdString())) + if (m_pDatabase->GetFavoriteColumnValueByFilename(files[i].ToStdString())) { m_Library->SetValue(wxVariant(wxBitmap(ICON_STAR_EMPTY_16px)), item_row, 0); - db.UpdateFavoriteColumn(static_cast(DATABASE_FILEPATH), files[i].ToStdString(), 0); + m_pDatabase->UpdateFavoriteColumn(files[i].ToStdString(), 0); for (int j = 0; j < m_Hives->GetChildCount(root); j++) { @@ -2260,8 +2228,8 @@ void MainFrame::OnDragAndDropToTrash(wxDropFilesEvent& event) } } - db.UpdateTrashColumn(static_cast(DATABASE_FILEPATH), files[i].ToStdString(), 1); - db.UpdateHiveName(static_cast(DATABASE_FILEPATH), files[i].ToStdString(), + m_pDatabase->UpdateTrashColumn(files[i].ToStdString(), 1); + m_pDatabase->UpdateHiveName(files[i].ToStdString(), m_Hives->GetItemText(favorites_hive).ToStdString()); m_Trash->AppendItem(trash_root, text_value); @@ -2278,9 +2246,7 @@ void MainFrame::OnDragAndDropToTrash(wxDropFilesEvent& event) void MainFrame::OnClickAddHive(wxCommandEvent& event) { - Database db(*m_InfoBar); - - std::deque nodes; + std::deque nodes; nodes.push_back(m_Hives->GetNthChild(wxDataViewItem(wxNullPtr), 0)); wxDataViewItem current_item, found_item; @@ -2341,7 +2307,7 @@ void MainFrame::OnClickAddHive(wxCommandEvent& event) else { m_Hives->AppendContainer(wxDataViewItem(wxNullPtr), hive_name); - db.InsertIntoHives(static_cast(DATABASE_FILEPATH), hive_name.ToStdString()); + m_pDatabase->InsertIntoHives(hive_name.ToStdString()); msg = wxString::Format(_("%s added to Hives."), hive_name); } @@ -2360,9 +2326,7 @@ void MainFrame::OnClickAddHive(wxCommandEvent& event) void MainFrame::OnClickRemoveHive(wxCommandEvent& event) { Settings settings(this, m_ConfigFilepath, m_DatabaseFilepath); - Database db(*m_InfoBar); - - wxDataViewItem selected_item = m_Hives->GetSelection(); + wxDataViewItem selected_item = m_Hives->GetSelection(); wxString hive_name = m_Hives->GetItemText(selected_item); wxString msg; @@ -2411,7 +2375,7 @@ void MainFrame::OnClickRemoveHive(wxCommandEvent& event) { m_Hives->DeleteItem(selected_item); - db.RemoveHiveFromDatabase(static_cast(DATABASE_FILEPATH), hive_name.ToStdString()); + m_pDatabase->RemoveHiveFromDatabase(hive_name.ToStdString()); msg = wxString::Format(_("%s deleted from hives successfully."), hive_name); } break; @@ -2451,8 +2415,8 @@ void MainFrame::OnClickRemoveHive(wxCommandEvent& event) m_Library->SetValue(wxVariant(wxBitmap(ICON_STAR_EMPTY_16px)), i, 0); - db.UpdateFavoriteColumn(static_cast(DATABASE_FILEPATH), matched_sample.ToStdString(), 0); - db.UpdateHiveName(static_cast(DATABASE_FILEPATH), matched_sample.ToStdString(), + m_pDatabase->UpdateFavoriteColumn(matched_sample.ToStdString(), 0); + m_pDatabase->UpdateHiveName(matched_sample.ToStdString(), m_Hives->GetItemText(favorites_hive).ToStdString()); break; @@ -2465,7 +2429,7 @@ void MainFrame::OnClickRemoveHive(wxCommandEvent& event) m_Hives->DeleteChildren(selected_item); m_Hives->DeleteItem(selected_item); - db.RemoveHiveFromDatabase(static_cast(DATABASE_FILEPATH), hive_name.ToStdString()); + m_pDatabase->RemoveHiveFromDatabase(hive_name.ToStdString()); msg = wxString::Format(_("%s and all samples inside %s have been deleted from hives successfully."), hive_name, hive_name); @@ -2484,7 +2448,6 @@ void MainFrame::OnClickRemoveHive(wxCommandEvent& event) void MainFrame::OnClickRestoreTrashItem(wxCommandEvent& event) { - Database db(*m_InfoBar); Settings settings(this, m_ConfigFilepath, m_DatabaseFilepath); wxArrayTreeItemIds selected_item_ids; @@ -2521,13 +2484,13 @@ void MainFrame::OnClickRestoreTrashItem(wxCommandEvent& event) files = file_data.GetFilenames(); - db.UpdateTrashColumn(static_cast(DATABASE_FILEPATH), files[i].ToStdString(), 0); + m_pDatabase->UpdateTrashColumn(files[i].ToStdString(), 0); try { wxVector> dataset; - if (db.RestoreFromTrashByFilename(static_cast(DATABASE_FILEPATH), files[i].ToStdString(), dataset, + if (m_pDatabase->RestoreFromTrashByFilename(files[i].ToStdString(), dataset, settings.ShouldShowFileExtension(), ICON_STAR_FILLED_16px, ICON_STAR_EMPTY_16px).empty()) { @@ -2552,16 +2515,15 @@ void MainFrame::OnClickRestoreTrashItem(wxCommandEvent& event) void MainFrame::OnDoSearch(wxCommandEvent& event) { - Database db(*m_InfoBar); Settings settings(this, m_ConfigFilepath, m_DatabaseFilepath); - std::string search = m_SearchBox->GetValue().ToStdString(); + const auto search = m_SearchBox->GetValue().ToStdString(); try { wxVector> dataset; - if (db.FilterDatabaseBySampleName(static_cast(DATABASE_FILEPATH), dataset, search, settings.ShouldShowFileExtension(), + if (m_pDatabase->FilterDatabaseBySampleName(dataset, search, settings.ShouldShowFileExtension(), ICON_STAR_FILLED_16px, ICON_STAR_EMPTY_16px).empty()) { wxLogDebug(_("Error! Database is empty.")); @@ -2685,21 +2647,19 @@ void MainFrame::RefreshDatabase() FileInfo MainFrame::GetFilenamePathAndExtension(const wxString& selected, bool checkExtension, bool doGetFilename) const { - Database db(*m_InfoBar); Settings settings(m_ConfigFilepath, m_DatabaseFilepath); wxString path; std::string extension, filename; - wxString filename_with_extension = db.GetSamplePathByFilename(static_cast(DATABASE_FILEPATH), - selected.BeforeLast('.').ToStdString()); - wxString filename_without_extension = db.GetSamplePathByFilename(static_cast(DATABASE_FILEPATH), selected.ToStdString()); + wxString filename_with_extension = m_pDatabase->GetSamplePathByFilename(selected.BeforeLast('.').ToStdString()); + wxString filename_without_extension = m_pDatabase->GetSamplePathByFilename(selected.ToStdString()); if (checkExtension) { extension = settings.ShouldShowFileExtension() ? - db.GetSampleFileExtension(static_cast(DATABASE_FILEPATH), selected.ToStdString()) : - db.GetSampleFileExtension(static_cast(DATABASE_FILEPATH), selected.BeforeLast('.').ToStdString()); + m_pDatabase->GetSampleFileExtension(selected.ToStdString()) : + m_pDatabase->GetSampleFileExtension(selected.BeforeLast('.').ToStdString()); } path = selected.Contains(wxString::Format(".%s", extension)) ? diff --git a/src/MainFrame.hpp b/src/MainFrame.hpp index 509318a..2365dff 100644 --- a/src/MainFrame.hpp +++ b/src/MainFrame.hpp @@ -65,6 +65,8 @@ #include #endif +#include "IDatabase.hpp" + struct FileInfo { wxString Path; @@ -180,6 +182,7 @@ class MainFrame : public wxFrame // ------------------------------------------------------------------- wxSystemAppearance m_Theme = wxSystemSettings::GetAppearance(); + std::unique_ptr m_pDatabase; private: // ------------------------------------------------------------------- bool bAutoplay = false; diff --git a/src/TagEditorDialog.cpp b/src/TagEditorDialog.cpp index 8145e1f..b9b6fd5 100644 --- a/src/TagEditorDialog.cpp +++ b/src/TagEditorDialog.cpp @@ -226,7 +226,7 @@ void TagEditor::OnClickCustomTagButton(wxCommandEvent& event) void TagEditor::OnClickApply(wxCommandEvent& event) { - Database db(m_InfoBar); + Database db(m_InfoBar, m_DatabaseFilepath); wxString title = m_TitleText->GetValue(); wxString artist = m_ArtistText->GetValue(); @@ -235,7 +235,7 @@ void TagEditor::OnClickApply(wxCommandEvent& event) wxString comment = m_CommentText->GetValue(); wxString type = m_SampleTypeChoice->GetStringSelection(); - std::string sampleType = db.GetSampleType(m_DatabaseFilepath, m_Filename); + std::string sampleType = db.GetSampleType(m_Filename); std::string filename = wxString(m_Filename).AfterLast('/').BeforeLast('.').ToStdString(); @@ -265,7 +265,7 @@ void TagEditor::OnClickApply(wxCommandEvent& event) wxLogDebug("Changing artist tag.."); tags.SetArtist(artist.ToStdString()); - db.UpdateSamplePack(m_DatabaseFilepath, m_Filename, artist.ToStdString()); + db.UpdateSamplePack(m_Filename, artist.ToStdString()); wxLogDebug("SAMPLE FILENAME HERE: %s", m_Filename); @@ -299,7 +299,7 @@ void TagEditor::OnClickApply(wxCommandEvent& event) if (m_SampleTypeCheck->GetValue() && m_SampleTypeChoice->GetStringSelection() != sampleType) { wxLogDebug("Changing type tag.."); - db.UpdateSampleType(m_DatabaseFilepath, filename, type.ToStdString()); + db.UpdateSampleType(filename, type.ToStdString()); info_msg = wxString::Format("Successfully changed type tag to %s", type); } From e00d753302f17b8d3a77f6a40b81f262d5baaee0 Mon Sep 17 00:00:00 2001 From: Mathias Buhr Date: Tue, 20 Jul 2021 21:39:15 +0200 Subject: [PATCH 3/6] Cleanup Database interface --- src/Database.cpp | 26 +++----- src/Database.hpp | 66 +++++++++--------- src/IDatabase.hpp | 96 -------------------------- src/MainFrame.cpp | 167 +++++++++++++++++++++++----------------------- src/MainFrame.hpp | 5 +- 5 files changed, 125 insertions(+), 235 deletions(-) delete mode 100644 src/IDatabase.hpp diff --git a/src/Database.cpp b/src/Database.cpp index c4c3ac3..e954693 100644 --- a/src/Database.cpp +++ b/src/Database.cpp @@ -53,17 +53,6 @@ public: sqlite3_stmt *stmt = nullptr; }; -std::unique_ptr createSqlDatabase(DatabaseType type, wxInfoBar &infoBar, const std::string &dbPath) -{ - switch (type) - { - case DatabaseType::SqlLite: - return std::make_unique(infoBar, dbPath); - default: - return std::unique_ptr(); - } -} - Database::Database(wxInfoBar &infoBar, const std::string &dbPath) : m_InfoBar(infoBar) { @@ -142,7 +131,7 @@ void Database::CreateTableHives() } //Loops through a Sample array and adds them to the database -void Database::InsertIntoSamples(std::vector samples) +void Database::InsertIntoSamples(const std::vector &samples) { try { @@ -780,13 +769,13 @@ std::string Database::GetSampleFileExtension(const std::string &filename) return extension; } -wxVector> -Database::LoadSamplesDatabase(wxVector> &vecSet, +wxVector> Database::LoadSamplesDatabase( // wxTreeCtrl& favorite_tree, wxTreeItemId& favorite_item, wxDataViewTreeCtrl &favorite_tree, wxDataViewItem &favorite_item, wxTreeCtrl &trash_tree, wxTreeItemId &trash_item, bool show_extension, const std::string &icon_star_filled, const std::string &icon_star_empty) { + wxVector> vecSet; wxVariant icon_filled, icon_empty; icon_filled = wxVariant(wxBitmap(icon_star_filled)); icon_empty = wxVariant(wxBitmap(icon_star_empty)); @@ -932,10 +921,10 @@ Database::LoadSamplesDatabase(wxVector> &vecSet, } wxVector> -Database::FilterDatabaseBySampleName(wxVector> &sampleVec, - const std::string &sampleName, bool show_extension, +Database::FilterDatabaseBySampleName(const std::string &sampleName, bool show_extension, const std::string &icon_star_filled, const std::string &icon_star_empty) { + wxVector> sampleVec; wxVariant icon_filled, icon_empty; icon_filled = wxVariant(wxBitmap(icon_star_filled)); icon_empty = wxVariant(wxBitmap(icon_star_empty)); @@ -1026,10 +1015,10 @@ Database::FilterDatabaseBySampleName(wxVector> &sampleVec, } wxVector> -Database::FilterDatabaseByHiveName(wxVector> &sampleVec, - const std::string &hiveName, bool show_extension, +Database::FilterDatabaseByHiveName(const std::string &hiveName, bool show_extension, const std::string &icon_star_filled, const std::string &icon_star_empty) { + wxVector> sampleVec; wxVariant icon_filled, icon_empty; icon_filled = wxVariant(wxBitmap(icon_star_filled)); icon_empty = wxVariant(wxBitmap(icon_star_empty)); @@ -1382,3 +1371,4 @@ void Database::close() { throw_on_sqlite3_error(sqlite3_close(m_Database)); } + diff --git a/src/Database.hpp b/src/Database.hpp index add61ac..0eeb247 100644 --- a/src/Database.hpp +++ b/src/Database.hpp @@ -17,6 +17,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ +#pragma once #include "Sample.hpp" @@ -33,9 +34,7 @@ #include -#include "IDatabase.hpp" - -class Database : public IDatabase +class Database { public: Database(wxInfoBar& infoBar, const std::string& dbPath); @@ -56,61 +55,58 @@ class Database : public IDatabase public: // ------------------------------------------------------------------- // Create the table - void CreateTableSamples() override; - void CreateTableHives() override; + void CreateTableSamples(); + void CreateTableHives(); // ------------------------------------------------------------------- // Insert into database - void InsertIntoSamples(std::vector) override; - void InsertIntoHives(const std::string& hiveName) override; + void InsertIntoSamples(const std::vector&); + void InsertIntoHives(const std::string& hiveName); // ------------------------------------------------------------------- // Update database - void UpdateFavoriteColumn(const std::string& filename, int value) override; - void UpdateHive(const std::string& hiveOldName, const std::string& hiveNewName) override; - void UpdateHiveName(const std::string& filename, const std::string& hiveName) override; - void UpdateTrashColumn(const std::string& filename, int value) override; - void UpdateSamplePack(const std::string& filename, const std::string& samplePack) override; - void UpdateSampleType(const std::string& filename, const std::string& type) override; + void UpdateFavoriteColumn(const std::string& filename, int value); + void UpdateHive(const std::string& hiveOldName, const std::string& hiveNewName); + void UpdateHiveName(const std::string& filename, const std::string& hiveName); + void UpdateTrashColumn(const std::string& filename, int value); + void UpdateSamplePack(const std::string& filename, const std::string& samplePack); + void UpdateSampleType(const std::string& filename, const std::string& type); // ------------------------------------------------------------------- // Get from database - int GetFavoriteColumnValueByFilename(const std::string& filename) override; - std::string GetHiveByFilename(const std::string& filename) override; - std::string GetSamplePathByFilename(const std::string& filename) override; - std::string GetSampleFileExtension(const std::string& filename) override; - std::string GetSampleType(const std::string& filename) override; + int GetFavoriteColumnValueByFilename(const std::string& filename); + std::string GetHiveByFilename(const std::string& filename); + std::string GetSamplePathByFilename(const std::string& filename); + std::string GetSampleFileExtension(const std::string& filename); + std::string GetSampleType(const std::string& filename); // ------------------------------------------------------------------- // Check database - bool IsTrashed(const std::string& filename) override; - wxArrayString CheckDuplicates(const wxArrayString& files) override; + bool IsTrashed(const std::string& filename); + wxArrayString CheckDuplicates(const wxArrayString& files); // ------------------------------------------------------------------- // Remove from database - void RemoveSampleFromDatabase(const std::string& filename) override; - void RemoveHiveFromDatabase(const std::string& hiveName) override; + void RemoveSampleFromDatabase(const std::string& filename); + void RemoveHiveFromDatabase(const std::string& hiveName); // ------------------------------------------------------------------- - wxVector> // LoadDatabase(wxVector> &vecSet, // wxTreeCtrl& favorite_tree, wxTreeItemId& favorite_item, - // wxTreeCtrl& trash_tree, wxTreeItemId& trash_item, bool show_extension) override; - LoadSamplesDatabase(wxVector>& vecSet, - wxDataViewTreeCtrl& favorite_tree, wxDataViewItem& favorite_item, + // wxTreeCtrl& trash_tree, wxTreeItemId& trash_item, bool show_extension); + wxVector> + LoadSamplesDatabase(wxDataViewTreeCtrl& favorite_tree, wxDataViewItem& favorite_item, wxTreeCtrl& trash_tree, wxTreeItemId& trash_item, bool show_extension, - const std::string& icon_star_filled, const std::string& icon_star_emtpy) override; - void LoadHivesDatabase(wxDataViewTreeCtrl& favorite_tree) override; + const std::string& icon_star_filled, const std::string& icon_star_emtpy); + void LoadHivesDatabase(wxDataViewTreeCtrl& favorite_tree); wxVector> RestoreFromTrashByFilename(const std::string& filename, wxVector>& vecSet, bool show_extension, - const std::string& icon_star_filled, const std::string& icon_star_empty) override; + const std::string& icon_star_filled, const std::string& icon_star_empty); wxVector> - FilterDatabaseBySampleName(wxVector>& sampleVec, - const std::string& sampleName, bool show_extension, - const std::string& icon_star_filled, const std::string& icon_star_empty) override; + FilterDatabaseBySampleName(const std::string& sampleName, bool show_extension, + const std::string& icon_star_filled, const std::string& icon_star_empty); wxVector> - FilterDatabaseByHiveName(wxVector>& sampleVec, - const std::string& hiveName, bool show_extension, - const std::string& icon_star_filled, const std::string& icon_star_empty) override; + FilterDatabaseByHiveName(const std::string& hiveName, bool show_extension, + const std::string& icon_star_filled, const std::string& icon_star_empty); }; diff --git a/src/IDatabase.hpp b/src/IDatabase.hpp deleted file mode 100644 index 8299ca1..0000000 --- a/src/IDatabase.hpp +++ /dev/null @@ -1,96 +0,0 @@ -/* SampleHive - * Copyright (C) 2021 Apoorv Singh - * A simple, modern audio sample browser/manager for GNU/Linux. - * - * This file is a part of SampleHive - * - * SampleHive 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. - * - * SampleHive 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 this program. If not, see . - */ -#pragma once - -#include -#include -#include -#include "Sample.hpp" -#include - -class IDatabase -{ public: - virtual ~IDatabase() = default; - // ------------------------------------------------------------------- - // Create the table - virtual void CreateTableSamples() = 0; - virtual void CreateTableHives() = 0; - - // ------------------------------------------------------------------- - // Insert into database - virtual void InsertIntoSamples(std::vector) = 0; - virtual void InsertIntoHives(const std::string& hiveName) = 0; - - // ------------------------------------------------------------------- - // Update database - virtual void UpdateFavoriteColumn(const std::string& filename, int value) = 0; - virtual void UpdateHive(const std::string& hiveOldName, const std::string& hiveNewName) = 0; - virtual void UpdateHiveName(const std::string& filename, const std::string& hiveName) = 0; - virtual void UpdateTrashColumn(const std::string& filename, int value) = 0; - virtual void UpdateSamplePack(const std::string& filename, const std::string& samplePack) = 0; - virtual void UpdateSampleType(const std::string& filename, const std::string& type) = 0; - - // ------------------------------------------------------------------- - // Get from database - virtual int GetFavoriteColumnValueByFilename(const std::string& filename) = 0; - virtual std::string GetHiveByFilename(const std::string& filename) = 0; - virtual std::string GetSamplePathByFilename(const std::string& filename) = 0; - virtual std::string GetSampleFileExtension(const std::string& filename) = 0; - virtual std::string GetSampleType(const std::string& filename) = 0; - - // ------------------------------------------------------------------- - // Check database - virtual bool IsTrashed(const std::string& filename) = 0; - virtual wxArrayString CheckDuplicates(const wxArrayString& files) = 0; - - // ------------------------------------------------------------------- - // Remove from database - virtual void RemoveSampleFromDatabase(const std::string& filename) = 0; - virtual void RemoveHiveFromDatabase(const std::string& hiveName) = 0; - - // ------------------------------------------------------------------- - virtual wxVector> - // LoadDatabase(wxVector> &vecSet, - // wxTreeCtrl& favorite_tree, wxTreeItemId& favorite_item, - // wxTreeCtrl& trash_tree, wxTreeItemId& trash_item, bool show_extension) = 0; - LoadSamplesDatabase(wxVector>& vecSet, - wxDataViewTreeCtrl& favorite_tree, wxDataViewItem& favorite_item, - wxTreeCtrl& trash_tree, wxTreeItemId& trash_item, bool show_extension, - const std::string& icon_star_filled, const std::string& icon_star_emtpy) = 0; - virtual void LoadHivesDatabase(wxDataViewTreeCtrl& favorite_tree) = 0; - virtual wxVector> - RestoreFromTrashByFilename(const std::string& filename, - wxVector>& vecSet, bool show_extension, - const std::string& icon_star_filled, const std::string& icon_star_empty) = 0; - virtual wxVector> - FilterDatabaseBySampleName(wxVector>& sampleVec, - const std::string& sampleName, bool show_extension, - const std::string& icon_star_filled, const std::string& icon_star_empty) = 0; - virtual wxVector> - FilterDatabaseByHiveName(wxVector>& sampleVec, - const std::string& hiveName, bool show_extension, - const std::string& icon_star_filled, const std::string& icon_star_empty) = 0; -}; - -enum class DatabaseType { - SqlLite -}; - -std::unique_ptr createSqlDatabase(DatabaseType type, wxInfoBar& infoBar, const std::string& dbPath); \ No newline at end of file diff --git a/src/MainFrame.cpp b/src/MainFrame.cpp index 5247694..5398655 100644 --- a/src/MainFrame.cpp +++ b/src/MainFrame.cpp @@ -544,9 +544,9 @@ MainFrame::MainFrame() m_BottomRightPanelMainSizer->Layout(); // Initialize the database - m_pDatabase = createSqlDatabase(DatabaseType::SqlLite, *m_InfoBar, m_DatabaseFilepath); - m_pDatabase->CreateTableSamples(); - m_pDatabase->CreateTableHives(); + m_database = std::make_unique(*m_InfoBar, m_DatabaseFilepath); + m_database->CreateTableSamples(); + m_database->CreateTableHives(); // Restore the data previously added to Library LoadDatabase(); @@ -604,7 +604,7 @@ void MainFrame::AddSamples(wxArrayString& files) //Check All Files At Once wxArrayString sorted_files; - sorted_files = m_pDatabase->CheckDuplicates(files); + sorted_files = m_database->CheckDuplicates(files); files = sorted_files; if(files.size() < 1) @@ -685,7 +685,7 @@ void MainFrame::AddSamples(wxArrayString& files) progressDialog->Pulse(_("Updating Database.."), NULL); - m_pDatabase->InsertIntoSamples(sample_array); + m_database->InsertIntoSamples(sample_array); progressDialog->Destroy(); } @@ -785,23 +785,23 @@ void MainFrame::OnDragAndDropToHives(wxDropFilesEvent& event) rows - i, files[i], m_Hives->GetItemText(drop_target)); if (drop_target.IsOk() && m_Hives->IsContainer(drop_target) && - m_pDatabase->GetFavoriteColumnValueByFilename( file_name.ToStdString()) == 0) + m_database->GetFavoriteColumnValueByFilename(file_name.ToStdString()) == 0) { m_Hives->AppendItem(drop_target, files[i]); m_Library->SetValue(wxVariant(wxBitmap(ICON_STAR_FILLED_16px)), row, 0); - m_pDatabase->UpdateFavoriteColumn(file_name.ToStdString(), 1); - m_pDatabase->UpdateHiveName( file_name.ToStdString(), hive_name.ToStdString()); + m_database->UpdateFavoriteColumn(file_name.ToStdString(), 1); + m_database->UpdateHiveName( file_name.ToStdString(), hive_name.ToStdString()); msg = wxString::Format(_("%s added to %s."), files[i], hive_name); } else { - if (m_pDatabase->GetFavoriteColumnValueByFilename( file_name.ToStdString()) == 1) + if (m_database->GetFavoriteColumnValueByFilename(file_name.ToStdString()) == 1) { wxMessageBox(wxString::Format(_("%s is already added to %s hive"), files[i], - m_pDatabase->GetHiveByFilename( file_name.ToStdString())), + m_database->GetHiveByFilename(file_name.ToStdString())), _("Error!"), wxOK | wxICON_ERROR | wxCENTRE, this); } else @@ -902,12 +902,12 @@ void MainFrame::OnDragFromLibrary(wxDataViewEvent& event) wxString selection = m_Library->GetTextValue(selected_row, 1); - // wxString sample_with_extension = m_pDatabase->GetSamplePathByFilename(selection.BeforeLast('.').ToStdString()); - // wxString sample_without_extension = m_pDatabase->GetSamplePathByFilename(selection.ToStdString()); + // wxString sample_with_extension = m_database->GetSamplePathByFilename(selection.BeforeLast('.').ToStdString()); + // wxString sample_without_extension = m_database->GetSamplePathByFilename(selection.ToStdString()); // std::string extension = settings.ShouldShowFileExtension() ? - // m_pDatabase->GetSampleFileExtension(selection.ToStdString()) : - // m_pDatabase->GetSampleFileExtension(selection.BeforeLast('.').ToStdString()); + // m_database->GetSampleFileExtension(selection.ToStdString()) : + // m_database->GetSampleFileExtension(selection.BeforeLast('.').ToStdString()); // wxString sample = selection.Contains(wxString::Format(".%s", extension)) ? // sample_with_extension : sample_without_extension; @@ -935,12 +935,12 @@ void MainFrame::OnClickPlay(wxCommandEvent& event) wxString selection = m_Library->GetTextValue(selected_row, 1); - // wxString sample_with_extension = m_pDatabase->GetSamplePathByFilename(selection.BeforeLast('.').ToStdString()); - // wxString sample_without_extension = m_pDatabase->GetSamplePathByFilename(selection.ToStdString()); + // wxString sample_with_extension = m_database->GetSamplePathByFilename(selection.BeforeLast('.').ToStdString()); + // wxString sample_without_extension = m_database->GetSamplePathByFilename(selection.ToStdString()); // std::string extension = settings.ShouldShowFileExtension() ? - // m_pDatabase->GetSampleFileExtension(selection.ToStdString()) : - // m_pDatabase->GetSampleFileExtension(selection.BeforeLast('.').ToStdString()); + // m_database->GetSampleFileExtension(selection.ToStdString()) : + // m_database->GetSampleFileExtension(selection.BeforeLast('.').ToStdString()); // wxString sample = selection.Contains(wxString::Format(".%s", extension)) ? // sample_with_extension : sample_without_extension; @@ -1129,12 +1129,12 @@ void MainFrame::OnClickLibrary(wxDataViewEvent& event) // else // selection = m_Library->GetTextValue(selected_row, 1); - // wxString sample_with_extension = m_pDatabase->GetSamplePathByFilename(selection.BeforeLast('.').ToStdString()); - // wxString sample_without_extension = m_pDatabase->GetSamplePathByFilename(selection.ToStdString()); + // wxString sample_with_extension = m_database->GetSamplePathByFilename(selection.BeforeLast('.').ToStdString()); + // wxString sample_without_extension = m_database->GetSamplePathByFilename(selection.ToStdString()); // std::string extension = settings.ShouldShowFileExtension() ? - // m_pDatabase->GetSampleFileExtension(selection.ToStdString()) : - // m_pDatabase->GetSampleFileExtension(selection.BeforeLast('.').ToStdString()); + // m_database->GetSampleFileExtension(selection.ToStdString()) : + // m_database->GetSampleFileExtension(selection.BeforeLast('.').ToStdString()); // wxString sample = selection.Contains(wxString::Format(".%s", extension)) ? // sample_with_extension : sample_without_extension; @@ -1177,12 +1177,12 @@ void MainFrame::OnClickLibrary(wxDataViewEvent& event) wxDataViewItem container; wxDataViewItem child; - if (m_pDatabase->GetFavoriteColumnValueByFilename(filename) == 0) + if (m_database->GetFavoriteColumnValueByFilename(filename) == 0) { m_Library->SetValue(wxVariant(wxBitmap(ICON_STAR_FILLED_16px)), selected_row, 0); - m_pDatabase->UpdateFavoriteColumn(filename, 1); - m_pDatabase->UpdateHiveName(filename, hive_name); + m_database->UpdateFavoriteColumn(filename, 1); + m_database->UpdateHiveName(filename, hive_name); for (int i = 0; i < m_Hives->GetChildCount(root); i++) { @@ -1201,8 +1201,8 @@ void MainFrame::OnClickLibrary(wxDataViewEvent& event) { m_Library->SetValue(wxVariant(wxBitmap(ICON_STAR_EMPTY_16px)), selected_row, 0); - m_pDatabase->UpdateFavoriteColumn(filename, 0); - m_pDatabase->UpdateHiveName( filename, m_Hives->GetItemText(favorites_hive).ToStdString()); + m_database->UpdateFavoriteColumn(filename, 0); + m_database->UpdateHiveName(filename, m_Hives->GetItemText(favorites_hive).ToStdString()); for (int i = 0; i < m_Hives->GetChildCount(root); i++) { @@ -1329,7 +1329,7 @@ void MainFrame::OnShowHivesContextMenu(wxDataViewEvent& event) wxLogDebug("Sample count: %d", sample_count); m_Hives->SetItemText(selected_hive, hive_name); - m_pDatabase->UpdateHive( selected_hive_name.ToStdString(), hive_name.ToStdString()); + m_database->UpdateHive(selected_hive_name.ToStdString(), hive_name.ToStdString()); } else { @@ -1343,8 +1343,8 @@ void MainFrame::OnShowHivesContextMenu(wxDataViewEvent& event) wxLogDebug("Sample count: %d :: Sample name: %s", sample_count, sample_name); - m_pDatabase->UpdateHiveName( sample_name.ToStdString(), hive_name.ToStdString()); - m_pDatabase->UpdateHive( selected_hive_name.ToStdString(), hive_name.ToStdString()); + m_database->UpdateHiveName(sample_name.ToStdString(), hive_name.ToStdString()); + m_database->UpdateHive(selected_hive_name.ToStdString(), hive_name.ToStdString()); m_Hives->SetItemText(selected_hive, hive_name); } @@ -1413,7 +1413,7 @@ void MainFrame::OnShowHivesContextMenu(wxDataViewEvent& event) { m_Hives->DeleteItem(selected_hive); - m_pDatabase->RemoveHiveFromDatabase( hive_name.ToStdString()); + m_database->RemoveHiveFromDatabase(hive_name.ToStdString()); msg = wxString::Format(_("%s deleted from hives successfully."), hive_name); } @@ -1454,9 +1454,8 @@ void MainFrame::OnShowHivesContextMenu(wxDataViewEvent& event) m_Library->SetValue(wxVariant(wxBitmap(ICON_STAR_EMPTY_16px)), i, 0); - m_pDatabase->UpdateFavoriteColumn( matched_sample.ToStdString(), 0); - m_pDatabase->UpdateHiveName( matched_sample.ToStdString(), - m_Hives->GetItemText(favorites_hive).ToStdString()); + m_database->UpdateFavoriteColumn(matched_sample.ToStdString(), 0); + m_database->UpdateHiveName(matched_sample.ToStdString(), m_Hives->GetItemText(favorites_hive).ToStdString()); break; } @@ -1468,7 +1467,7 @@ void MainFrame::OnShowHivesContextMenu(wxDataViewEvent& event) m_Hives->DeleteChildren(selected_hive); m_Hives->DeleteItem(selected_hive); - m_pDatabase->RemoveHiveFromDatabase( hive_name.ToStdString()); + m_database->RemoveHiveFromDatabase(hive_name.ToStdString()); msg = wxString::Format( _("%s and all samples inside %s have been deleted from hives successfully."), @@ -1492,11 +1491,11 @@ void MainFrame::OnShowHivesContextMenu(wxDataViewEvent& event) { try { - wxVector> dataset; - - if (m_pDatabase->FilterDatabaseByHiveName( dataset, hive_name.ToStdString(), + const auto dataset = m_database->FilterDatabaseByHiveName(hive_name.ToStdString(), settings.ShouldShowFileExtension(), - ICON_STAR_FILLED_16px, ICON_STAR_EMPTY_16px).empty()) + ICON_STAR_FILLED_16px, ICON_STAR_EMPTY_16px); + + if (dataset.empty()) { wxMessageBox(_("Error! Database is empty."), _("Error!"), wxOK | wxICON_ERROR | wxCENTRE, this); @@ -1526,10 +1525,10 @@ void MainFrame::OnShowHivesContextMenu(wxDataViewEvent& event) { try { - wxVector> dataset; + const auto dataset = m_database->FilterDatabaseBySampleName("", settings.ShouldShowFileExtension(), + ICON_STAR_FILLED_16px, ICON_STAR_EMPTY_16px); - if (m_pDatabase->FilterDatabaseBySampleName( dataset, "", settings.ShouldShowFileExtension(), - ICON_STAR_FILLED_16px, ICON_STAR_EMPTY_16px).empty()) + if (dataset.empty()) { wxMessageBox(_("Error! Database is empty."), _("Error!"), wxOK | wxICON_ERROR | wxCENTRE, this); @@ -1579,8 +1578,8 @@ void MainFrame::OnShowHivesContextMenu(wxDataViewEvent& event) m_Library->SetValue(wxVariant(wxBitmap(ICON_STAR_EMPTY_16px)), i, 0); - m_pDatabase->UpdateFavoriteColumn(matched_sample.ToStdString(), 0); - m_pDatabase->UpdateHiveName(matched_sample.ToStdString(), + m_database->UpdateFavoriteColumn(matched_sample.ToStdString(), 0); + m_database->UpdateHiveName(matched_sample.ToStdString(), m_Hives->GetItemText(favorites_hive).ToStdString()); m_Hives->DeleteItem(selected_hive); @@ -1590,7 +1589,7 @@ void MainFrame::OnShowHivesContextMenu(wxDataViewEvent& event) m_InfoBar->ShowMessage(wxString::Format(_("Removed %s from %s"), m_Hives->GetItemText(event.GetItem()), - m_pDatabase->GetHiveByFilename(matched_sample.ToStdString())), + m_database->GetHiveByFilename(matched_sample.ToStdString())), wxICON_INFORMATION); } break; @@ -1650,7 +1649,7 @@ void MainFrame::OnShowLibraryContextMenu(wxDataViewEvent& event) //true = add false = remove bool favorite_add = false; - if (m_pDatabase->GetFavoriteColumnValueByFilename(filename) == 1) + if (m_database->GetFavoriteColumnValueByFilename(filename) == 1) menu.Append(MN_FavoriteSample, _("Remove from hive"), _("Remove the selected sample(s) from hive")); else { @@ -1704,7 +1703,7 @@ void MainFrame::OnShowLibraryContextMenu(wxDataViewEvent& event) filename = settings.ShouldShowFileExtension() ? name.BeforeLast('.').ToStdString() : name.ToStdString(); - db_status = m_pDatabase->GetFavoriteColumnValueByFilename(filename); + db_status = m_database->GetFavoriteColumnValueByFilename(filename); // Aleady Added, Do Nothing if (favorite_add && db_status == 1) @@ -1719,8 +1718,8 @@ void MainFrame::OnShowLibraryContextMenu(wxDataViewEvent& event) { m_Library->SetValue(wxVariant(wxBitmap(ICON_STAR_FILLED_16px)), selected_row, 0); - m_pDatabase->UpdateFavoriteColumn(filename, 1); - m_pDatabase->UpdateHiveName(filename, hive_name); + m_database->UpdateFavoriteColumn(filename, 1); + m_database->UpdateHiveName(filename, hive_name); for (int i = 0; i < m_Hives->GetChildCount(root); i++) { @@ -1740,8 +1739,8 @@ void MainFrame::OnShowLibraryContextMenu(wxDataViewEvent& event) //Remove From Favorites m_Library->SetValue(wxVariant(wxBitmap(ICON_STAR_EMPTY_16px)), selected_row, 0); - m_pDatabase->UpdateFavoriteColumn(filename, 0); - m_pDatabase->UpdateHiveName(filename, + m_database->UpdateFavoriteColumn(filename, 0); + m_database->UpdateHiveName(filename, m_Hives->GetItemText(favorites_hive).ToStdString()); for (int i = 0; i < m_Hives->GetChildCount(root); i++) @@ -1804,7 +1803,7 @@ void MainFrame::OnShowLibraryContextMenu(wxDataViewEvent& event) { wxLogDebug("Selected row: %d :: Sample: %s", selected_row, filename); - m_pDatabase->RemoveSampleFromDatabase(filename); + m_database->RemoveSampleFromDatabase(filename); m_Library->DeleteItem(selected_row); for (int j = 0; j < m_Hives->GetChildCount(root); j++) @@ -1854,7 +1853,7 @@ void MainFrame::OnShowLibraryContextMenu(wxDataViewEvent& event) std::string multi_selection = settings.ShouldShowFileExtension() ? text_value.BeforeLast('.').ToStdString() : text_value.ToStdString() ; - m_pDatabase->RemoveSampleFromDatabase(multi_selection); + m_database->RemoveSampleFromDatabase(multi_selection); m_Library->DeleteItem(row); for (int j = 0; j < m_Hives->GetChildCount(root); j++) @@ -1896,7 +1895,7 @@ void MainFrame::OnShowLibraryContextMenu(wxDataViewEvent& event) wxDataViewItem root = wxDataViewItem(wxNullPtr); wxDataViewItem container, child; - if (m_pDatabase->IsTrashed(filename)) + if (m_database->IsTrashed(filename)) wxLogDebug(_("Already trashed..")); else { @@ -1921,11 +1920,11 @@ void MainFrame::OnShowLibraryContextMenu(wxDataViewEvent& event) files = file_data.GetFilenames(); - if (m_pDatabase->GetFavoriteColumnValueByFilename(files[i].ToStdString())) + if (m_database->GetFavoriteColumnValueByFilename(files[i].ToStdString())) { m_Library->SetValue(wxVariant(wxBitmap(ICON_STAR_EMPTY_16px)), item_row, 0); - m_pDatabase->UpdateFavoriteColumn(files[i].ToStdString(), 0); + m_database->UpdateFavoriteColumn(files[i].ToStdString(), 0); for (int j = 0; j < m_Hives->GetChildCount(root); j++) { @@ -1948,8 +1947,8 @@ void MainFrame::OnShowLibraryContextMenu(wxDataViewEvent& event) } } - m_pDatabase->UpdateTrashColumn(files[i].ToStdString(), 1); - m_pDatabase->UpdateHiveName(files[i].ToStdString(), + m_database->UpdateTrashColumn(files[i].ToStdString(), 1); + m_database->UpdateHiveName(files[i].ToStdString(), m_Hives->GetItemText(favorites_hive).ToStdString()); m_Trash->AppendItem(trash_root, text_value); @@ -2058,13 +2057,13 @@ void MainFrame::LoadDatabase() Settings settings(this, m_ConfigFilepath, m_DatabaseFilepath); try { - m_pDatabase->LoadHivesDatabase(*m_Hives); + m_database->LoadHivesDatabase(*m_Hives); - wxVector> dataset; - - if (m_pDatabase->LoadSamplesDatabase(dataset, *m_Hives, favorites_hive, + const auto dataset = m_database->LoadSamplesDatabase(*m_Hives, favorites_hive, *m_Trash, trash_root, settings.ShouldShowFileExtension(), - ICON_STAR_FILLED_16px, ICON_STAR_EMPTY_16px).empty()) + ICON_STAR_FILLED_16px, ICON_STAR_EMPTY_16px); + + if (dataset.empty()) { wxLogInfo(_("Error! Database is empty.")); } @@ -2104,7 +2103,7 @@ void MainFrame::OnShowTrashContextMenu(wxTreeEvent& event) m_Trash->GetItemText(selected_trashed_item).BeforeLast('.') : m_Trash->GetItemText(selected_trashed_item); - m_pDatabase->RemoveSampleFromDatabase(trashed_item_name.ToStdString()); + m_database->RemoveSampleFromDatabase(trashed_item_name.ToStdString()); m_Trash->Delete(selected_trashed_item); } @@ -2135,13 +2134,13 @@ void MainFrame::OnShowTrashContextMenu(wxTreeEvent& event) files = file_data.GetFilenames(); - m_pDatabase->UpdateTrashColumn(files[i].ToStdString(), 0); + m_database->UpdateTrashColumn(files[i].ToStdString(), 0); try { wxVector> dataset; - if (m_pDatabase->RestoreFromTrashByFilename(files[i].ToStdString(), + if (m_database->RestoreFromTrashByFilename(files[i].ToStdString(), dataset, settings.ShouldShowFileExtension(), ICON_STAR_FILLED_16px, ICON_STAR_EMPTY_16px).empty()) { @@ -2201,11 +2200,11 @@ void MainFrame::OnDragAndDropToTrash(wxDropFilesEvent& event) files = file_data.GetFilenames(); - if (m_pDatabase->GetFavoriteColumnValueByFilename(files[i].ToStdString())) + if (m_database->GetFavoriteColumnValueByFilename(files[i].ToStdString())) { m_Library->SetValue(wxVariant(wxBitmap(ICON_STAR_EMPTY_16px)), item_row, 0); - m_pDatabase->UpdateFavoriteColumn(files[i].ToStdString(), 0); + m_database->UpdateFavoriteColumn(files[i].ToStdString(), 0); for (int j = 0; j < m_Hives->GetChildCount(root); j++) { @@ -2228,8 +2227,8 @@ void MainFrame::OnDragAndDropToTrash(wxDropFilesEvent& event) } } - m_pDatabase->UpdateTrashColumn(files[i].ToStdString(), 1); - m_pDatabase->UpdateHiveName(files[i].ToStdString(), + m_database->UpdateTrashColumn(files[i].ToStdString(), 1); + m_database->UpdateHiveName(files[i].ToStdString(), m_Hives->GetItemText(favorites_hive).ToStdString()); m_Trash->AppendItem(trash_root, text_value); @@ -2307,7 +2306,7 @@ void MainFrame::OnClickAddHive(wxCommandEvent& event) else { m_Hives->AppendContainer(wxDataViewItem(wxNullPtr), hive_name); - m_pDatabase->InsertIntoHives(hive_name.ToStdString()); + m_database->InsertIntoHives(hive_name.ToStdString()); msg = wxString::Format(_("%s added to Hives."), hive_name); } @@ -2375,7 +2374,7 @@ void MainFrame::OnClickRemoveHive(wxCommandEvent& event) { m_Hives->DeleteItem(selected_item); - m_pDatabase->RemoveHiveFromDatabase(hive_name.ToStdString()); + m_database->RemoveHiveFromDatabase(hive_name.ToStdString()); msg = wxString::Format(_("%s deleted from hives successfully."), hive_name); } break; @@ -2415,8 +2414,8 @@ void MainFrame::OnClickRemoveHive(wxCommandEvent& event) m_Library->SetValue(wxVariant(wxBitmap(ICON_STAR_EMPTY_16px)), i, 0); - m_pDatabase->UpdateFavoriteColumn(matched_sample.ToStdString(), 0); - m_pDatabase->UpdateHiveName(matched_sample.ToStdString(), + m_database->UpdateFavoriteColumn(matched_sample.ToStdString(), 0); + m_database->UpdateHiveName(matched_sample.ToStdString(), m_Hives->GetItemText(favorites_hive).ToStdString()); break; @@ -2429,7 +2428,7 @@ void MainFrame::OnClickRemoveHive(wxCommandEvent& event) m_Hives->DeleteChildren(selected_item); m_Hives->DeleteItem(selected_item); - m_pDatabase->RemoveHiveFromDatabase(hive_name.ToStdString()); + m_database->RemoveHiveFromDatabase(hive_name.ToStdString()); msg = wxString::Format(_("%s and all samples inside %s have been deleted from hives successfully."), hive_name, hive_name); @@ -2484,13 +2483,13 @@ void MainFrame::OnClickRestoreTrashItem(wxCommandEvent& event) files = file_data.GetFilenames(); - m_pDatabase->UpdateTrashColumn(files[i].ToStdString(), 0); + m_database->UpdateTrashColumn(files[i].ToStdString(), 0); try { wxVector> dataset; - if (m_pDatabase->RestoreFromTrashByFilename(files[i].ToStdString(), dataset, + if (m_database->RestoreFromTrashByFilename(files[i].ToStdString(), dataset, settings.ShouldShowFileExtension(), ICON_STAR_FILLED_16px, ICON_STAR_EMPTY_16px).empty()) { @@ -2521,10 +2520,10 @@ void MainFrame::OnDoSearch(wxCommandEvent& event) try { - wxVector> dataset; + const auto dataset = m_database->FilterDatabaseBySampleName(search, settings.ShouldShowFileExtension(), + ICON_STAR_FILLED_16px, ICON_STAR_EMPTY_16px); - if (m_pDatabase->FilterDatabaseBySampleName(dataset, search, settings.ShouldShowFileExtension(), - ICON_STAR_FILLED_16px, ICON_STAR_EMPTY_16px).empty()) + if (dataset.empty()) { wxLogDebug(_("Error! Database is empty.")); } @@ -2652,14 +2651,14 @@ MainFrame::GetFilenamePathAndExtension(const wxString& selected, bool checkExten wxString path; std::string extension, filename; - wxString filename_with_extension = m_pDatabase->GetSamplePathByFilename(selected.BeforeLast('.').ToStdString()); - wxString filename_without_extension = m_pDatabase->GetSamplePathByFilename(selected.ToStdString()); + wxString filename_with_extension = m_database->GetSamplePathByFilename(selected.BeforeLast('.').ToStdString()); + wxString filename_without_extension = m_database->GetSamplePathByFilename(selected.ToStdString()); if (checkExtension) { extension = settings.ShouldShowFileExtension() ? - m_pDatabase->GetSampleFileExtension(selected.ToStdString()) : - m_pDatabase->GetSampleFileExtension(selected.BeforeLast('.').ToStdString()); + m_database->GetSampleFileExtension(selected.ToStdString()) : + m_database->GetSampleFileExtension(selected.BeforeLast('.').ToStdString()); } path = selected.Contains(wxString::Format(".%s", extension)) ? diff --git a/src/MainFrame.hpp b/src/MainFrame.hpp index 2365dff..7438ca5 100644 --- a/src/MainFrame.hpp +++ b/src/MainFrame.hpp @@ -24,6 +24,7 @@ #include "SampleHiveConfig.hpp" #include "SH_Event.hpp" +#include #include #include @@ -65,7 +66,7 @@ #include #endif -#include "IDatabase.hpp" +#include "Database.hpp" struct FileInfo { @@ -182,7 +183,7 @@ class MainFrame : public wxFrame // ------------------------------------------------------------------- wxSystemAppearance m_Theme = wxSystemSettings::GetAppearance(); - std::unique_ptr m_pDatabase; + std::unique_ptr m_database; private: // ------------------------------------------------------------------- bool bAutoplay = false; From 7eccf406dc66d15dd3cee307a535adc2264f59b0 Mon Sep 17 00:00:00 2001 From: Mathias Buhr Date: Sat, 24 Jul 2021 11:57:18 +0200 Subject: [PATCH 4/6] Use const& for loop --- src/MainFrame.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/MainFrame.cpp b/src/MainFrame.cpp index 5398655..99e2f7e 100644 --- a/src/MainFrame.cpp +++ b/src/MainFrame.cpp @@ -2533,7 +2533,7 @@ void MainFrame::OnDoSearch(wxCommandEvent& event) std::cout << search << std::endl; - for (auto data : dataset) + for (const auto& data : dataset) { m_Library->AppendItem(data); } From 3558cac62d062e123d37e12c4ae7319f670a6056 Mon Sep 17 00:00:00 2001 From: Mathias Buhr Date: Sat, 24 Jul 2021 11:57:56 +0200 Subject: [PATCH 5/6] Reduce redundant database code --- src/Database.cpp | 1049 +++++++++++++--------------------------- src/MainFrame.cpp | 12 +- src/WaveformViewer.cpp | 32 +- src/WaveformViewer.hpp | 6 +- 4 files changed, 370 insertions(+), 729 deletions(-) diff --git a/src/Database.cpp b/src/Database.cpp index e954693..d074e51 100644 --- a/src/Database.cpp +++ b/src/Database.cpp @@ -22,6 +22,7 @@ #include #include +#include #include #include @@ -39,6 +40,24 @@ void throw_on_sqlite3_error(int rc) } } +void debug_log_on_error(int rc) +{ + if (rc != SQLITE_OK) + { + sqlite3_errstr(rc); + } +} + +void show_modal_dialog_and_log(const std::string &message, const std::string &title, const std::string &error_msg) +{ + std::stringstream ss; + ss << message << error_msg; + const auto msg = ss.str(); + wxLogDebug(msg.c_str()); + wxMessageDialog msgDialog(NULL, msg, title, wxOK | wxICON_ERROR); + msgDialog.ShowModal(); +} + class Sqlite3Statement { public: @@ -67,66 +86,44 @@ Database::~Database() void Database::CreateTableSamples() { /* Create SQL statement */ - std::string samples = "CREATE TABLE IF NOT EXISTS SAMPLES(" - "FAVORITE INT NOT NULL," - "FILENAME TEXT NOT NULL," - "EXTENSION TEXT NOT NULL," - "SAMPLEPACK TEXT NOT NULL," - "TYPE TEXT NOT NULL," - "CHANNELS INT NOT NULL," - "LENGTH INT NOT NULL," - "SAMPLERATE INT NOT NULL," - "BITRATE INT NOT NULL," - "PATH TEXT NOT NULL," - "TRASHED INT NOT NULL," - "HIVE TEXT NOT NULL);"; + const auto samples = "CREATE TABLE IF NOT EXISTS SAMPLES(" + "FAVORITE INT NOT NULL," + "FILENAME TEXT NOT NULL," + "EXTENSION TEXT NOT NULL," + "SAMPLEPACK TEXT NOT NULL," + "TYPE TEXT NOT NULL," + "CHANNELS INT NOT NULL," + "LENGTH INT NOT NULL," + "SAMPLERATE INT NOT NULL," + "BITRATE INT NOT NULL," + "PATH TEXT NOT NULL," + "TRASHED INT NOT NULL," + "HIVE TEXT NOT NULL);"; try { - rc = sqlite3_exec(m_Database, samples.c_str(), NULL, 0, &m_ErrMsg); - - if (rc != SQLITE_OK) - { - wxMessageDialog msgDialog(NULL, "Error! Cannot create table samples.", - "Error", wxOK | wxICON_ERROR); - msgDialog.ShowModal(); - sqlite3_free(m_ErrMsg); - } - else - { - wxLogDebug("Samples table created successfully."); - } + throw_on_sqlite3_error(sqlite3_exec(m_Database, samples, NULL, 0, &m_ErrMsg)); + wxLogDebug("Samples table created successfully."); } - catch (const std::exception &exception) + catch (const std::exception &e) { - wxLogDebug(exception.what()); + show_modal_dialog_and_log("Error! Cannot create table samples: ", "Error", e.what()); } } void Database::CreateTableHives() { /* Create SQL statement */ - std::string hives = "CREATE TABLE IF NOT EXISTS HIVES(HIVE TEXT NOT NULL);"; + const auto hives = "CREATE TABLE IF NOT EXISTS HIVES(HIVE TEXT NOT NULL);"; try { - rc = sqlite3_exec(m_Database, hives.c_str(), NULL, 0, &m_ErrMsg); - - if (rc != SQLITE_OK) - { - wxMessageDialog msgDialog(NULL, "Error! Cannot create table hives.", - "Error", wxOK | wxICON_ERROR); - msgDialog.ShowModal(); - sqlite3_free(m_ErrMsg); - } - else - { - wxLogDebug("Hives table created successfully."); - } + throw_on_sqlite3_error(sqlite3_exec(m_Database, hives, NULL, 0, &m_ErrMsg)); + wxLogDebug("Hives table created successfully."); } - catch (const std::exception &exception) + catch (const std::exception &e) { - wxLogDebug(exception.what()); + show_modal_dialog_and_log("Error! Cannot create table hives: ", "Error", e.what()); } } @@ -135,18 +132,14 @@ void Database::InsertIntoSamples(const std::vector &samples) { try { - std::string insert = "INSERT INTO SAMPLES (FAVORITE, FILENAME, \ - EXTENSION, SAMPLEPACK, TYPE, CHANNELS, LENGTH, \ - SAMPLERATE, BITRATE, PATH, TRASHED, HIVE) \ - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);"; + const auto sql = "INSERT INTO SAMPLES (FAVORITE, FILENAME, \ + EXTENSION, SAMPLEPACK, TYPE, CHANNELS, LENGTH, \ + SAMPLERATE, BITRATE, PATH, TRASHED, HIVE) \ + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);"; - sqlite3_stmt *stmt = nullptr; - rc = sqlite3_prepare_v2(m_Database, insert.c_str(), insert.size(), &stmt, NULL); + Sqlite3Statement statement(m_Database, sql); - rc = sqlite3_exec(m_Database, "BEGIN TRANSACTION", NULL, NULL, &m_ErrMsg); - - if (rc != SQLITE_OK) - wxLogDebug("Cannot prepare sql statement.."); + throw_on_sqlite3_error(sqlite3_exec(m_Database, "BEGIN TRANSACTION", NULL, NULL, &m_ErrMsg)); Sample sample; @@ -168,57 +161,27 @@ void Database::InsertIntoSamples(const std::vector &samples) std::string hive = "Favorites"; - rc = sqlite3_bind_int(stmt, 1, sample.GetFavorite()); - rc = sqlite3_bind_text(stmt, 2, filename.c_str(), filename.size(), SQLITE_STATIC); - rc = sqlite3_bind_text(stmt, 3, file_extension.c_str(), file_extension.size(), SQLITE_STATIC); - rc = sqlite3_bind_text(stmt, 4, sample_pack.c_str(), sample_pack.size(), SQLITE_STATIC); - rc = sqlite3_bind_text(stmt, 5, type.c_str(), type.size(), SQLITE_STATIC); - rc = sqlite3_bind_int(stmt, 6, sample.GetChannels()); - rc = sqlite3_bind_int(stmt, 7, sample.GetLength()); - rc = sqlite3_bind_int(stmt, 8, sample.GetSampleRate()); - rc = sqlite3_bind_int(stmt, 9, sample.GetBitrate()); - rc = sqlite3_bind_text(stmt, 10, path.c_str(), path.size(), SQLITE_STATIC); - rc = sqlite3_bind_int(stmt, 11, sample.GetTrashed()); - rc = sqlite3_bind_text(stmt, 12, hive.c_str(), hive.size(), SQLITE_STATIC); + throw_on_sqlite3_error(sqlite3_bind_int(statement.stmt, 1, sample.GetFavorite())); + throw_on_sqlite3_error(sqlite3_bind_text(statement.stmt, 2, filename.c_str(), filename.size(), SQLITE_STATIC)); + throw_on_sqlite3_error(sqlite3_bind_text(statement.stmt, 3, file_extension.c_str(), file_extension.size(), SQLITE_STATIC)); + throw_on_sqlite3_error(sqlite3_bind_text(statement.stmt, 4, sample_pack.c_str(), sample_pack.size(), SQLITE_STATIC)); + throw_on_sqlite3_error(sqlite3_bind_text(statement.stmt, 5, type.c_str(), type.size(), SQLITE_STATIC)); + throw_on_sqlite3_error(sqlite3_bind_int(statement.stmt, 6, sample.GetChannels())); + throw_on_sqlite3_error(sqlite3_bind_int(statement.stmt, 7, sample.GetLength())); + throw_on_sqlite3_error(sqlite3_bind_int(statement.stmt, 8, sample.GetSampleRate())); + throw_on_sqlite3_error(sqlite3_bind_int(statement.stmt, 9, sample.GetBitrate())); + throw_on_sqlite3_error(sqlite3_bind_text(statement.stmt, 10, path.c_str(), path.size(), SQLITE_STATIC)); + throw_on_sqlite3_error(sqlite3_bind_int(statement.stmt, 11, sample.GetTrashed())); + throw_on_sqlite3_error(sqlite3_bind_text(statement.stmt, 12, hive.c_str(), hive.size(), SQLITE_STATIC)); - rc = sqlite3_step(stmt); - rc = sqlite3_clear_bindings(stmt); - rc = sqlite3_reset(stmt); + sqlite3_step(statement.stmt); + throw_on_sqlite3_error(sqlite3_clear_bindings(statement.stmt)); + throw_on_sqlite3_error(sqlite3_reset(statement.stmt)); } - rc = sqlite3_exec(m_Database, "END TRANSACTION", NULL, NULL, &m_ErrMsg); + throw_on_sqlite3_error(sqlite3_exec(m_Database, "END TRANSACTION", NULL, NULL, &m_ErrMsg)); - rc = sqlite3_finalize(stmt); - - if (rc != SQLITE_OK) - { - wxLogDebug("Error! Cannot insert data into table. Error code: %d: Msg: %s", rc, sqlite3_errmsg(m_Database)); - } - else - { - wxLogDebug("Data inserted successfully."); - } - - if (rc == SQLITE_BUSY) - wxLogDebug("SQLITE_BUSY"); - if (rc == SQLITE_ABORT) - wxLogDebug("SQLITE_ABORT"); - if (rc == SQLITE_NOMEM) - wxLogDebug("SQLITE_NOMEM"); - if (rc == SQLITE_LOCKED) - wxLogDebug("SQLITE_LOCKED"); - if (rc == SQLITE_IOERR) - wxLogDebug("SQLITE_IOERR"); - if (rc == SQLITE_CORRUPT) - wxLogDebug("SQLITE_CORRUPT"); - if (rc == SQLITE_READONLY) - wxLogDebug("SQLITE_READONLY"); - if (rc == SQLITE_ERROR) - wxLogDebug("SQLITE_ERROR"); - if (rc == SQLITE_PERM) - wxLogDebug("SQLITE_PERM"); - if (rc == SQLITE_INTERNAL) - wxLogDebug("SQLITE_INTERNAL"); + wxLogDebug("Data inserted successfully."); } catch (const std::exception &exception) { @@ -230,16 +193,11 @@ void Database::InsertIntoHives(const std::string &hiveName) { try { - std::string insert = "INSERT INTO HIVES(HIVE) VALUES(?);"; - - sqlite3_stmt *stmt = nullptr; - rc = sqlite3_prepare_v2(m_Database, insert.c_str(), insert.size(), &stmt, NULL); + const auto sql = "INSERT INTO HIVES(HIVE) VALUES(?);"; + Sqlite3Statement statement(m_Database, sql); // rc = sqlite3_exec(m_Database, "BEGIN TRANSACTION", NULL, NULL, &m_ErrMsg); - if (rc != SQLITE_OK) - wxLogDebug("Cannot prepare sql statement.."); - // Sample sample; // std::string filename; @@ -260,47 +218,17 @@ void Database::InsertIntoHives(const std::string &hiveName) // std::string hive = "Favourites"; - // rc = sqlite3_bind_int(stmt, 1, sample.GetFavorite()); - rc = sqlite3_bind_text(stmt, 1, hiveName.c_str(), hiveName.size(), SQLITE_STATIC); + // rc = sqlite3_bind_int(statement.stmt, 1, sample.GetFavorite()); + throw_on_sqlite3_error(sqlite3_bind_text(statement.stmt, 1, hiveName.c_str(), hiveName.size(), SQLITE_STATIC)); - rc = sqlite3_step(stmt); - // rc = sqlite3_clear_bindings(stmt); - // rc = sqlite3_reset(stmt); + throw_on_sqlite3_error(sqlite3_step(statement.stmt)); + // rc = sqlite3_clear_bindings(statement.stmt); + // rc = sqlite3_reset(statement.stmt); // } // rc = sqlite3_exec(m_Database, "END TRANSACTION", NULL, NULL, &m_ErrMsg); - rc = sqlite3_finalize(stmt); - - if (rc != SQLITE_OK) - { - wxLogDebug("Error! Cannot insert data into table. Error code: %d: Msg: %s", rc, sqlite3_errmsg(m_Database)); - } - else - { - wxLogDebug("Data inserted successfully."); - } - - if (rc == SQLITE_BUSY) - wxLogDebug("SQLITE_BUSY"); - if (rc == SQLITE_ABORT) - wxLogDebug("SQLITE_ABORT"); - if (rc == SQLITE_NOMEM) - wxLogDebug("SQLITE_NOMEM"); - if (rc == SQLITE_LOCKED) - wxLogDebug("SQLITE_LOCKED"); - if (rc == SQLITE_IOERR) - wxLogDebug("SQLITE_IOERR"); - if (rc == SQLITE_CORRUPT) - wxLogDebug("SQLITE_CORRUPT"); - if (rc == SQLITE_READONLY) - wxLogDebug("SQLITE_READONLY"); - if (rc == SQLITE_ERROR) - wxLogDebug("SQLITE_ERROR"); - if (rc == SQLITE_PERM) - wxLogDebug("SQLITE_PERM"); - if (rc == SQLITE_INTERNAL) - wxLogDebug("SQLITE_INTERNAL"); + wxLogDebug("Data inserted successfully."); } catch (const std::exception &exception) { @@ -312,29 +240,16 @@ void Database::UpdateHive(const std::string &hiveOldName, const std::string &hiv { try { - std::string update = "UPDATE HIVES SET HIVE = ? WHERE HIVE = ?;"; + const auto sql = "UPDATE HIVES SET HIVE = ? WHERE HIVE = ?;"; + Sqlite3Statement statement(m_Database, sql); - sqlite3_stmt *stmt = nullptr; - rc = sqlite3_prepare_v2(m_Database, update.c_str(), update.size(), &stmt, NULL); + throw_on_sqlite3_error(sqlite3_bind_text(statement.stmt, 1, hiveNewName.c_str(), hiveNewName.size(), SQLITE_STATIC)); + throw_on_sqlite3_error(sqlite3_bind_text(statement.stmt, 2, hiveOldName.c_str(), hiveOldName.size(), SQLITE_STATIC)); - rc = sqlite3_bind_text(stmt, 1, hiveNewName.c_str(), hiveNewName.size(), SQLITE_STATIC); - rc = sqlite3_bind_text(stmt, 2, hiveOldName.c_str(), hiveOldName.size(), SQLITE_STATIC); - - if (sqlite3_step(stmt) != SQLITE_DONE) + if (sqlite3_step(statement.stmt) != SQLITE_DONE) { wxLogWarning("No data inserted."); } - - rc = sqlite3_finalize(stmt); - - if (rc != SQLITE_OK) - { - wxMessageDialog msgDialog(NULL, - "Error! Cannot update hive name.", - "Error", wxOK | wxICON_ERROR); - msgDialog.ShowModal(); - sqlite3_free(m_ErrMsg); - } else { wxLogDebug("Hive updated successfully. %s", m_ErrMsg); @@ -350,35 +265,22 @@ void Database::UpdateHiveName(const std::string &filename, const std::string &hi { try { - std::string update = "UPDATE SAMPLES SET HIVE = ? WHERE FILENAME = ?;"; + const auto sql = "UPDATE SAMPLES SET HIVE = ? WHERE FILENAME = ?;"; + Sqlite3Statement statement(m_Database, sql); - sqlite3_stmt *stmt = nullptr; - rc = sqlite3_prepare_v2(m_Database, update.c_str(), update.size(), &stmt, NULL); + throw_on_sqlite3_error(sqlite3_bind_text(statement.stmt, 1, hiveName.c_str(), hiveName.size(), SQLITE_STATIC)); + throw_on_sqlite3_error(sqlite3_bind_text(statement.stmt, 2, filename.c_str(), filename.size(), SQLITE_STATIC)); - rc = sqlite3_bind_text(stmt, 1, hiveName.c_str(), hiveName.size(), SQLITE_STATIC); - rc = sqlite3_bind_text(stmt, 2, filename.c_str(), filename.size(), SQLITE_STATIC); - - if (sqlite3_step(stmt) == SQLITE_ROW) + if (sqlite3_step(statement.stmt) == SQLITE_ROW) { wxLogDebug("Record found, updating.."); } - rc = sqlite3_finalize(stmt); - - if (rc != SQLITE_OK) - { - wxMessageDialog msgDialog(NULL, "Error! Cannot update record.", "Error", wxOK | wxICON_ERROR); - msgDialog.ShowModal(); - sqlite3_free(m_ErrMsg); - } - else - { - wxLogDebug("Updated record successfully."); - } + wxLogDebug("Updated record successfully."); } - catch (const std::exception &exception) + catch (const std::exception &e) { - wxLogDebug(exception.what()); + show_modal_dialog_and_log("Error! Cannot update record: ", "Error", e.what()); } } @@ -386,35 +288,22 @@ void Database::UpdateFavoriteColumn(const std::string &filename, int value) { try { - std::string update = "UPDATE SAMPLES SET FAVORITE = ? WHERE FILENAME = ?;"; + const auto sql = "UPDATE SAMPLES SET FAVORITE = ? WHERE FILENAME = ?;"; + Sqlite3Statement statement(m_Database, sql); - sqlite3_stmt *stmt = nullptr; - rc = sqlite3_prepare_v2(m_Database, update.c_str(), update.size(), &stmt, NULL); + throw_on_sqlite3_error(sqlite3_bind_int(statement.stmt, 1, value)); + throw_on_sqlite3_error(sqlite3_bind_text(statement.stmt, 2, filename.c_str(), filename.size(), SQLITE_STATIC)); - rc = sqlite3_bind_int(stmt, 1, value); - rc = sqlite3_bind_text(stmt, 2, filename.c_str(), filename.size(), SQLITE_STATIC); - - if (sqlite3_step(stmt) == SQLITE_ROW) + if (sqlite3_step(statement.stmt) == SQLITE_ROW) { wxLogDebug("Record found, updating.."); } - rc = sqlite3_finalize(stmt); - - if (rc != SQLITE_OK) - { - wxMessageDialog msgDialog(NULL, "Error! Cannot update record.", "Error", wxOK | wxICON_ERROR); - msgDialog.ShowModal(); - sqlite3_free(m_ErrMsg); - } - else - { - wxLogDebug("Updated record successfully."); - } + wxLogDebug("Updated record successfully."); } - catch (const std::exception &exception) + catch (const std::exception &e) { - wxLogDebug(exception.what()); + show_modal_dialog_and_log("Error! Cannot update record: ", "Error", e.what()); } } @@ -422,35 +311,22 @@ void Database::UpdateSamplePack(const std::string &filename, const std::string & { try { - std::string update = "UPDATE SAMPLES SET SAMPLEPACK = ? WHERE FILENAME = ?;"; + const auto sql = "UPDATE SAMPLES SET SAMPLEPACK = ? WHERE FILENAME = ?;"; + Sqlite3Statement statement(m_Database, sql); - sqlite3_stmt *stmt = nullptr; - rc = sqlite3_prepare_v2(m_Database, update.c_str(), update.size(), &stmt, NULL); + throw_on_sqlite3_error(sqlite3_bind_text(statement.stmt, 1, samplePack.c_str(), samplePack.size(), SQLITE_STATIC)); + throw_on_sqlite3_error(sqlite3_bind_text(statement.stmt, 2, filename.c_str(), filename.size(), SQLITE_STATIC)); - rc = sqlite3_bind_text(stmt, 1, samplePack.c_str(), samplePack.size(), SQLITE_STATIC); - rc = sqlite3_bind_text(stmt, 2, filename.c_str(), filename.size(), SQLITE_STATIC); - - if (sqlite3_step(stmt) == SQLITE_ROW) + if (sqlite3_step(statement.stmt) == SQLITE_ROW) { wxLogDebug("Record found, updating.."); } - rc = sqlite3_finalize(stmt); - - if (rc != SQLITE_OK) - { - wxMessageDialog msgDialog(NULL, "Error! Cannot update record.", "Error", wxOK | wxICON_ERROR); - msgDialog.ShowModal(); - sqlite3_free(m_ErrMsg); - } - else - { - wxLogDebug("Updated record successfully."); - } + wxLogDebug("Updated record successfully."); } - catch (const std::exception &exception) + catch (const std::exception &e) { - wxLogDebug(exception.what()); + show_modal_dialog_and_log("Error! Cannot update record: ", "Error", e.what()); } } @@ -458,35 +334,21 @@ void Database::UpdateSampleType(const std::string &filename, const std::string & { try { - std::string update = "UPDATE SAMPLES SET TYPE = ? WHERE FILENAME = ?;"; + const auto sql = "UPDATE SAMPLES SET TYPE = ? WHERE FILENAME = ?;"; + Sqlite3Statement statement(m_Database, sql); - sqlite3_stmt *stmt = nullptr; - rc = sqlite3_prepare_v2(m_Database, update.c_str(), update.size(), &stmt, NULL); + throw_on_sqlite3_error(sqlite3_bind_text(statement.stmt, 1, type.c_str(), type.size(), SQLITE_STATIC)); + throw_on_sqlite3_error(sqlite3_bind_text(statement.stmt, 2, filename.c_str(), filename.size(), SQLITE_STATIC)); - rc = sqlite3_bind_text(stmt, 1, type.c_str(), type.size(), SQLITE_STATIC); - rc = sqlite3_bind_text(stmt, 2, filename.c_str(), filename.size(), SQLITE_STATIC); - - if (sqlite3_step(stmt) == SQLITE_ROW) + if (sqlite3_step(statement.stmt) == SQLITE_ROW) { wxLogDebug("Record found, updating.."); } - - rc = sqlite3_finalize(stmt); - - if (rc != SQLITE_OK) - { - wxMessageDialog msgDialog(NULL, "Error! Cannot update record.", "Error", wxOK | wxICON_ERROR); - msgDialog.ShowModal(); - sqlite3_free(m_ErrMsg); - } - else - { - wxLogDebug("Updated record successfully."); - } + wxLogDebug("Updated record successfully."); } - catch (const std::exception &exception) + catch (const std::exception &e) { - wxLogDebug(exception.what()); + show_modal_dialog_and_log("Error! Cannot update record: ", "Error", e.what()); } } @@ -496,37 +358,22 @@ std::string Database::GetSampleType(const std::string &filename) try { - std::string select = "SELECT TYPE FROM SAMPLES WHERE FILENAME = ?;"; + const auto sql = "SELECT TYPE FROM SAMPLES WHERE FILENAME = ?;"; + Sqlite3Statement statement(m_Database, sql); - sqlite3_stmt *stmt = nullptr; - rc = sqlite3_prepare_v2(m_Database, select.c_str(), select.size(), &stmt, NULL); + throw_on_sqlite3_error(sqlite3_bind_text(statement.stmt, 1, filename.c_str(), filename.size(), SQLITE_STATIC)); - rc = sqlite3_bind_text(stmt, 1, filename.c_str(), filename.size(), SQLITE_STATIC); - - if (sqlite3_step(stmt) == SQLITE_ROW) + if (sqlite3_step(statement.stmt) == SQLITE_ROW) { wxLogDebug("Record found, fetching.."); - type = std::string(reinterpret_cast(sqlite3_column_text(stmt, 0))); - } - - rc = sqlite3_finalize(stmt); - - if (rc != SQLITE_OK) - { - wxMessageDialog msgDialog(NULL, "Error! Cannot get sample type column value from table.", - "Error", wxOK | wxICON_ERROR); - msgDialog.ShowModal(); - sqlite3_free(m_ErrMsg); - } - else - { - wxLogDebug("Selected data from table successfully."); + type = std::string(reinterpret_cast(sqlite3_column_text(statement.stmt, 0))); } + wxLogDebug("Selected data from table successfully."); } - catch (const std::exception &exception) + catch (const std::exception &e) { - wxLogDebug(exception.what()); + show_modal_dialog_and_log("Error! Cannot get sample type column value from table: ", "Error", e.what()); } return type; @@ -538,36 +385,22 @@ int Database::GetFavoriteColumnValueByFilename(const std::string &filename) try { - std::string select = "SELECT FAVORITE FROM SAMPLES WHERE FILENAME = ?;"; + const auto sql = "SELECT FAVORITE FROM SAMPLES WHERE FILENAME = ?;"; + Sqlite3Statement statement(m_Database, sql); - sqlite3_stmt *stmt = nullptr; - rc = sqlite3_prepare_v2(m_Database, select.c_str(), select.size(), &stmt, NULL); + throw_on_sqlite3_error(sqlite3_bind_text(statement.stmt, 1, filename.c_str(), filename.size(), SQLITE_STATIC)); - rc = sqlite3_bind_text(stmt, 1, filename.c_str(), filename.size(), SQLITE_STATIC); - - if (sqlite3_step(stmt) == SQLITE_ROW) + if (sqlite3_step(statement.stmt) == SQLITE_ROW) { wxLogDebug("Record found, fetching.."); - value = sqlite3_column_int(stmt, 0); + value = sqlite3_column_int(statement.stmt, 0); } - rc = sqlite3_finalize(stmt); - - if (rc != SQLITE_OK) - { - wxMessageDialog msgDialog(NULL, "Error! Cannot get favorite column value from table.", - "Error", wxOK | wxICON_ERROR); - msgDialog.ShowModal(); - sqlite3_free(m_ErrMsg); - } - else - { - wxLogDebug("Selected data from table successfully."); - } + wxLogDebug("Selected data from table successfully."); } - catch (const std::exception &exception) + catch (const std::exception &e) { - wxLogDebug(exception.what()); + show_modal_dialog_and_log("Error! Cannot get favorite column value from table: ", "Error", e.what()); } return value; @@ -579,37 +412,22 @@ std::string Database::GetHiveByFilename(const std::string &filename) try { - std::string select = "SELECT HIVE FROM SAMPLES WHERE FILENAME = ?;"; + const auto sql = "SELECT HIVE FROM SAMPLES WHERE FILENAME = ?;"; + Sqlite3Statement statement(m_Database, sql); - sqlite3_stmt *stmt = nullptr; - rc = sqlite3_prepare_v2(m_Database, select.c_str(), select.size(), &stmt, NULL); + throw_on_sqlite3_error(sqlite3_bind_text(statement.stmt, 1, filename.c_str(), filename.size(), SQLITE_STATIC)); - rc = sqlite3_bind_text(stmt, 1, filename.c_str(), filename.size(), SQLITE_STATIC); - - if (sqlite3_step(stmt) == SQLITE_ROW) + if (sqlite3_step(statement.stmt) == SQLITE_ROW) { wxLogDebug("Record found, fetching.."); - - hive = std::string(reinterpret_cast(sqlite3_column_text(stmt, 0))); + hive = std::string(reinterpret_cast(sqlite3_column_text(statement.stmt, 0))); } - rc = sqlite3_finalize(stmt); - - if (rc != SQLITE_OK) - { - wxMessageDialog msgDialog(NULL, "Error! Cannot get hive value from table.", - "Error", wxOK | wxICON_ERROR); - msgDialog.ShowModal(); - sqlite3_free(m_ErrMsg); - } - else - { - wxLogDebug("Selected data from table successfully."); - } + wxLogDebug("Selected data from table successfully."); } - catch (const std::exception &exception) + catch (const std::exception &e) { - wxLogDebug(exception.what()); + show_modal_dialog_and_log("Error! Cannot get hive value from table: ", "Error", e.what()); } return hive; @@ -619,35 +437,21 @@ void Database::RemoveSampleFromDatabase(const std::string &filename) { try { - std::string remove = "DELETE FROM SAMPLES WHERE FILENAME = ?;"; + const auto sql = "DELETE FROM SAMPLES WHERE FILENAME = ?;"; + Sqlite3Statement statement(m_Database, sql); - sqlite3_stmt *stmt = nullptr; - rc = sqlite3_prepare_v2(m_Database, remove.c_str(), remove.size(), &stmt, NULL); + throw_on_sqlite3_error(sqlite3_bind_text(statement.stmt, 1, filename.c_str(), filename.size(), SQLITE_STATIC)); - rc = sqlite3_bind_text(stmt, 1, filename.c_str(), filename.size(), SQLITE_STATIC); - - if (sqlite3_step(stmt) == SQLITE_DONE) + if (sqlite3_step(statement.stmt) == SQLITE_DONE) { wxLogDebug("Record found, Deleting.."); } - rc = sqlite3_finalize(stmt); - - if (rc != SQLITE_OK) - { - wxMessageDialog msgDialog(NULL, "Error! Cannot delete data from table.", - "Error", wxOK | wxICON_ERROR); - msgDialog.ShowModal(); - sqlite3_free(m_ErrMsg); - } - else - { - wxLogDebug("Deleted data from table successfully."); - } + wxLogDebug("Deleted data from table successfully."); } - catch (const std::exception &exception) + catch (const std::exception &e) { - wxLogDebug(exception.what()); + show_modal_dialog_and_log("Error! Cannot get hive value from table: ", "Error", e.what()); } } @@ -655,35 +459,21 @@ void Database::RemoveHiveFromDatabase(const std::string &hiveName) { try { - std::string remove = "DELETE FROM HIVES WHERE HIVE = ?;"; + const auto sql = "DELETE FROM HIVES WHERE HIVE = ?;"; + Sqlite3Statement statement(m_Database, sql); - sqlite3_stmt *stmt = nullptr; - rc = sqlite3_prepare_v2(m_Database, remove.c_str(), remove.size(), &stmt, NULL); + throw_on_sqlite3_error(sqlite3_bind_text(statement.stmt, 1, hiveName.c_str(), hiveName.size(), SQLITE_STATIC)); - rc = sqlite3_bind_text(stmt, 1, hiveName.c_str(), hiveName.size(), SQLITE_STATIC); - - if (sqlite3_step(stmt) == SQLITE_DONE) + if (sqlite3_step(statement.stmt) == SQLITE_DONE) { wxLogDebug("Record found, Deleting.."); } - rc = sqlite3_finalize(stmt); - - if (rc != SQLITE_OK) - { - wxMessageDialog msgDialog(NULL, "Error! Cannot delete data from table.", - "Error", wxOK | wxICON_ERROR); - msgDialog.ShowModal(); - sqlite3_free(m_ErrMsg); - } - else - { - wxLogDebug("Deleted data from table successfully."); - } + wxLogDebug("Deleted data from table successfully."); } - catch (const std::exception &exception) + catch (const std::exception &e) { - wxLogDebug(exception.what()); + show_modal_dialog_and_log("Error! Cannot get hive value from table: ", "Error", e.what()); } } @@ -693,36 +483,22 @@ std::string Database::GetSamplePathByFilename(const std::string &filename) try { - std::string select = "SELECT PATH FROM SAMPLES WHERE FILENAME = ?;"; + const auto sql = "SELECT PATH FROM SAMPLES WHERE FILENAME = ?;"; + Sqlite3Statement statement(m_Database, sql); - sqlite3_stmt *stmt = nullptr; - rc = sqlite3_prepare_v2(m_Database, select.c_str(), select.size(), &stmt, NULL); + throw_on_sqlite3_error(sqlite3_bind_text(statement.stmt, 1, filename.c_str(), filename.size(), SQLITE_STATIC)); - rc = sqlite3_bind_text(stmt, 1, filename.c_str(), filename.size(), SQLITE_STATIC); - - if (sqlite3_step(stmt) == SQLITE_ROW) + if (sqlite3_step(statement.stmt) == SQLITE_ROW) { wxLogDebug("Record found, fetching.."); - path = std::string(reinterpret_cast(sqlite3_column_text(stmt, 0))); + path = std::string(reinterpret_cast(sqlite3_column_text(statement.stmt, 0))); } - rc = sqlite3_finalize(stmt); - - if (rc != SQLITE_OK) - { - wxMessageDialog msgDialog(NULL, "Error! Cannot select sample path from table.", - "Error", wxOK | wxICON_ERROR); - msgDialog.ShowModal(); - sqlite3_free(m_ErrMsg); - } - else - { - wxLogDebug("Selected data from table successfully."); - } + wxLogDebug("Selected data from table successfully."); } - catch (const std::exception &exception) + catch (const std::exception &e) { - wxLogDebug(exception.what()); + show_modal_dialog_and_log("Error! Cannot select sample path from table: ", "Error", e.what()); } return path; @@ -734,46 +510,32 @@ std::string Database::GetSampleFileExtension(const std::string &filename) try { - std::string select = "SELECT EXTENSION FROM SAMPLES WHERE FILENAME = ?;"; + const auto sql = "SELECT EXTENSION FROM SAMPLES WHERE FILENAME = ?;"; + Sqlite3Statement statement(m_Database, sql); - sqlite3_stmt *stmt = nullptr; - rc = sqlite3_prepare_v2(m_Database, select.c_str(), select.size(), &stmt, NULL); + throw_on_sqlite3_error(sqlite3_bind_text(statement.stmt, 1, filename.c_str(), filename.size(), SQLITE_STATIC)); - rc = sqlite3_bind_text(stmt, 1, filename.c_str(), filename.size(), SQLITE_STATIC); - - if (sqlite3_step(stmt) == SQLITE_ROW) + if (sqlite3_step(statement.stmt) == SQLITE_ROW) { - wxLogDebug("Record found, fetching.."); - extension = std::string(reinterpret_cast(sqlite3_column_text(stmt, 0))); + wxLogInfo("Record found, fetching.."); + extension = std::string(reinterpret_cast(sqlite3_column_text(statement.stmt, 0))); } - rc = sqlite3_finalize(stmt); - - if (rc != SQLITE_OK) - { - wxMessageDialog msgDialog(NULL, "Error! Cannot select sample path from table.", - "Error", wxOK | wxICON_ERROR); - msgDialog.ShowModal(); - sqlite3_free(m_ErrMsg); - } - else - { - wxLogDebug("Selected data from table successfully."); - } + wxLogDebug("Selected data from table successfully."); } - catch (const std::exception &exception) + catch (const std::exception &e) { - wxLogDebug(exception.what()); + show_modal_dialog_and_log("Error! Cannot select sample extension from table: ", "Error", e.what()); } return extension; } wxVector> Database::LoadSamplesDatabase( - // wxTreeCtrl& favorite_tree, wxTreeItemId& favorite_item, - wxDataViewTreeCtrl &favorite_tree, wxDataViewItem &favorite_item, - wxTreeCtrl &trash_tree, wxTreeItemId &trash_item, bool show_extension, - const std::string &icon_star_filled, const std::string &icon_star_empty) + // wxTreeCtrl& favorite_tree, wxTreeItemId& favorite_item, + wxDataViewTreeCtrl &favorite_tree, wxDataViewItem &favorite_item, + wxTreeCtrl &trash_tree, wxTreeItemId &trash_item, bool show_extension, + const std::string &icon_star_filled, const std::string &icon_star_empty) { wxVector> vecSet; wxVariant icon_filled, icon_empty; @@ -908,13 +670,9 @@ wxVector> Database::LoadSamplesDatabase( row++; } } - catch (const std::exception &exception) + catch (const std::exception &e) { - wxMessageDialog msgDialog(NULL, "Error! Cannot load data from table.", - "Error", wxOK | wxICON_ERROR); - msgDialog.ShowModal(); - sqlite3_free(m_ErrMsg); - wxLogDebug(exception.what()); + show_modal_dialog_and_log("Error! Cannot load data from table: ", "Error", e.what()); } return vecSet; @@ -930,85 +688,69 @@ Database::FilterDatabaseBySampleName(const std::string &sampleName, bool show_ex icon_empty = wxVariant(wxBitmap(icon_star_empty)); try { - - std::string filter = "SELECT FAVORITE, FILENAME, SAMPLEPACK, TYPE, \ + Sqlite3Statement statement(m_Database, "SELECT FAVORITE, FILENAME, SAMPLEPACK, TYPE, \ CHANNELS, LENGTH, SAMPLERATE, BITRATE, PATH \ - FROM SAMPLES WHERE FILENAME LIKE '%' || ? || '%' ;"; - sqlite3_stmt *stmt = nullptr; + FROM SAMPLES WHERE FILENAME LIKE '%' || ? || '%' ;"); + throw_on_sqlite3_error(sqlite3_bind_text(statement.stmt, 1, sampleName.c_str(), sampleName.size(), SQLITE_STATIC)); - rc = sqlite3_prepare_v2(m_Database, filter.c_str(), filter.size(), &stmt, NULL); - rc = sqlite3_bind_text(stmt, 1, sampleName.c_str(), sampleName.size(), SQLITE_STATIC); + int row = 0; - if (rc == SQLITE_OK) + while (SQLITE_ROW == sqlite3_step(statement.stmt)) { - int row = 0; + wxLogDebug("Record found, fetching.."); + int favorite = sqlite3_column_int(statement.stmt, 0); + wxString filename = wxString(std::string(reinterpret_cast(sqlite3_column_text(statement.stmt, 1)))); + wxString sample_pack = wxString(std::string(reinterpret_cast(sqlite3_column_text(statement.stmt, 2)))); + wxString sample_type = std::string(reinterpret_cast(sqlite3_column_text(statement.stmt, 3))); + int channels = sqlite3_column_int(statement.stmt, 4); + int length = sqlite3_column_int(statement.stmt, 5); + int sample_rate = sqlite3_column_int(statement.stmt, 6); + int bitrate = sqlite3_column_int(statement.stmt, 7); + wxString path = wxString(std::string(reinterpret_cast(sqlite3_column_text(statement.stmt, 8)))); - while (SQLITE_ROW == sqlite3_step(stmt)) + wxLongLong llLength = length; + int total_min = static_cast((llLength / 60000).GetValue()); + int total_sec = static_cast(((llLength % 60000) / 1000).GetValue()); + + wxVector vec; + + if (favorite == 1) + vec.push_back(icon_filled); + else + vec.push_back(icon_empty); + + // if (favorite == 1) + // vec.push_back(true); + // else + // vec.push_back(false); + + // vec.push_back(filename); + + if (show_extension) { - wxLogDebug("Record found, fetching.."); - int favorite = sqlite3_column_int(stmt, 0); - wxString filename = wxString(std::string(reinterpret_cast(sqlite3_column_text(stmt, 1)))); - wxString sample_pack = wxString(std::string(reinterpret_cast(sqlite3_column_text(stmt, 2)))); - wxString sample_type = std::string(reinterpret_cast(sqlite3_column_text(stmt, 3))); - int channels = sqlite3_column_int(stmt, 4); - int length = sqlite3_column_int(stmt, 5); - int sample_rate = sqlite3_column_int(stmt, 6); - int bitrate = sqlite3_column_int(stmt, 7); - wxString path = wxString(std::string(reinterpret_cast(sqlite3_column_text(stmt, 8)))); - - wxLongLong llLength = length; - int total_min = static_cast((llLength / 60000).GetValue()); - int total_sec = static_cast(((llLength % 60000) / 1000).GetValue()); - - wxVector vec; - - if (favorite == 1) - vec.push_back(icon_filled); - else - vec.push_back(icon_empty); - - // if (favorite == 1) - // vec.push_back(true); - // else - // vec.push_back(false); - - // vec.push_back(filename); - - if (show_extension) - { - vec.push_back(path.AfterLast('/')); - } - else - { - vec.push_back(path.AfterLast('/').BeforeLast('.')); - } - - vec.push_back(sample_pack); - vec.push_back(sample_type); - vec.push_back(wxString::Format("%d", channels)); - vec.push_back(wxString::Format("%2i:%02i", total_min, total_sec)); - vec.push_back(wxString::Format("%d", sample_rate)); - vec.push_back(wxString::Format("%d", bitrate)); - vec.push_back(path); - - sampleVec.push_back(vec); - - row++; + vec.push_back(path.AfterLast('/')); + } + else + { + vec.push_back(path.AfterLast('/').BeforeLast('.')); } - } - else - { - wxMessageDialog msgDialog(NULL, "Error! Cannot filter data from table.", - "Error", wxOK | wxICON_ERROR); - msgDialog.ShowModal(); - sqlite3_free(m_ErrMsg); - } - rc = sqlite3_finalize(stmt); + vec.push_back(sample_pack); + vec.push_back(sample_type); + vec.push_back(wxString::Format("%d", channels)); + vec.push_back(wxString::Format("%2i:%02i", total_min, total_sec)); + vec.push_back(wxString::Format("%d", sample_rate)); + vec.push_back(wxString::Format("%d", bitrate)); + vec.push_back(path); + + sampleVec.push_back(vec); + + row++; + } } - catch (const std::exception &exception) + catch (const std::exception &e) { - wxLogDebug(exception.what()); + show_modal_dialog_and_log("Error! Cannot filter data from table: ", "Error", e.what()); } return sampleVec; @@ -1024,85 +766,70 @@ Database::FilterDatabaseByHiveName(const std::string &hiveName, bool show_extens icon_empty = wxVariant(wxBitmap(icon_star_empty)); try { - std::string filter = "SELECT FAVORITE, FILENAME, SAMPLEPACK, TYPE, \ + Sqlite3Statement statement(m_Database, "SELECT FAVORITE, FILENAME, SAMPLEPACK, TYPE, \ CHANNELS, LENGTH, SAMPLERATE, BITRATE, PATH \ - FROM SAMPLES WHERE HIVE = ? AND FAVORITE = 1;"; + FROM SAMPLES WHERE HIVE = ? AND FAVORITE = 1;"); - sqlite3_stmt *stmt = nullptr; - rc = sqlite3_prepare_v2(m_Database, filter.c_str(), filter.size(), &stmt, NULL); + throw_on_sqlite3_error(sqlite3_bind_text(statement.stmt, 1, hiveName.c_str(), hiveName.size(), SQLITE_STATIC)); - rc = sqlite3_bind_text(stmt, 1, hiveName.c_str(), hiveName.size(), SQLITE_STATIC); + int row = 0; - if (rc == SQLITE_OK) + while (SQLITE_ROW == sqlite3_step(statement.stmt)) { - int row = 0; + wxLogDebug("Record found, fetching.."); + int favorite = sqlite3_column_int(statement.stmt, 0); + wxString filename = wxString(std::string(reinterpret_cast(sqlite3_column_text(statement.stmt, 1)))); + wxString sample_pack = wxString(std::string(reinterpret_cast(sqlite3_column_text(statement.stmt, 2)))); + wxString sample_type = std::string(reinterpret_cast(sqlite3_column_text(statement.stmt, 3))); + int channels = sqlite3_column_int(statement.stmt, 4); + int length = sqlite3_column_int(statement.stmt, 5); + int sample_rate = sqlite3_column_int(statement.stmt, 6); + int bitrate = sqlite3_column_int(statement.stmt, 7); + wxString path = wxString(std::string(reinterpret_cast(sqlite3_column_text(statement.stmt, 8)))); - while (SQLITE_ROW == sqlite3_step(stmt)) + wxLongLong llLength = length; + int total_min = static_cast((llLength / 60000).GetValue()); + int total_sec = static_cast(((llLength % 60000) / 1000).GetValue()); + + wxVector vec; + + if (favorite == 1) + vec.push_back(icon_filled); + else + vec.push_back(icon_empty); + + // if (favorite == 1) + // vec.push_back(true); + // else + // vec.push_back(false); + + // vec.push_back(filename); + + if (show_extension) { - wxLogDebug("Record found, fetching.."); - int favorite = sqlite3_column_int(stmt, 0); - wxString filename = wxString(std::string(reinterpret_cast(sqlite3_column_text(stmt, 1)))); - wxString sample_pack = wxString(std::string(reinterpret_cast(sqlite3_column_text(stmt, 2)))); - wxString sample_type = std::string(reinterpret_cast(sqlite3_column_text(stmt, 3))); - int channels = sqlite3_column_int(stmt, 4); - int length = sqlite3_column_int(stmt, 5); - int sample_rate = sqlite3_column_int(stmt, 6); - int bitrate = sqlite3_column_int(stmt, 7); - wxString path = wxString(std::string(reinterpret_cast(sqlite3_column_text(stmt, 8)))); - - wxLongLong llLength = length; - int total_min = static_cast((llLength / 60000).GetValue()); - int total_sec = static_cast(((llLength % 60000) / 1000).GetValue()); - - wxVector vec; - - if (favorite == 1) - vec.push_back(icon_filled); - else - vec.push_back(icon_empty); - - // if (favorite == 1) - // vec.push_back(true); - // else - // vec.push_back(false); - - // vec.push_back(filename); - - if (show_extension) - { - vec.push_back(path.AfterLast('/')); - } - else - { - vec.push_back(path.AfterLast('/').BeforeLast('.')); - } - - vec.push_back(sample_pack); - vec.push_back(sample_type); - vec.push_back(wxString::Format("%d", channels)); - vec.push_back(wxString::Format("%2i:%02i", total_min, total_sec)); - vec.push_back(wxString::Format("%d", sample_rate)); - vec.push_back(wxString::Format("%d", bitrate)); - vec.push_back(path); - - sampleVec.push_back(vec); - - row++; + vec.push_back(path.AfterLast('/')); + } + else + { + vec.push_back(path.AfterLast('/').BeforeLast('.')); } - } - else - { - wxMessageDialog msgDialog(NULL, "Error! Cannot filter data from table.", - "Error", wxOK | wxICON_ERROR); - msgDialog.ShowModal(); - sqlite3_free(m_ErrMsg); - } - rc = sqlite3_finalize(stmt); + vec.push_back(sample_pack); + vec.push_back(sample_type); + vec.push_back(wxString::Format("%d", channels)); + vec.push_back(wxString::Format("%2i:%02i", total_min, total_sec)); + vec.push_back(wxString::Format("%d", sample_rate)); + vec.push_back(wxString::Format("%d", bitrate)); + vec.push_back(path); + + sampleVec.push_back(vec); + + row++; + } } - catch (const std::exception &exception) + catch (const std::exception &e) { - wxLogDebug(exception.what()); + show_modal_dialog_and_log("Error! Cannot filter data from table: ", "Error", e.what()); } return sampleVec; @@ -1112,34 +839,20 @@ void Database::LoadHivesDatabase(wxDataViewTreeCtrl &treeCtrl) { try { - std::string select = "SELECT HIVE FROM HIVES;"; + const auto sql = "SELECT HIVE FROM HIVES;"; + Sqlite3Statement statement(m_Database, sql); - sqlite3_stmt *stmt = nullptr; - rc = sqlite3_prepare_v2(m_Database, select.c_str(), select.size(), &stmt, NULL); - - if (rc == SQLITE_OK) + while (SQLITE_ROW == sqlite3_step(statement.stmt)) { - while (SQLITE_ROW == sqlite3_step(stmt)) - { - wxLogDebug("Record found, fetching.."); - wxString hive = wxString(std::string(reinterpret_cast(sqlite3_column_text(stmt, 0)))); + wxLogDebug("Record found, fetching.."); + const auto hive = wxString(std::string(reinterpret_cast(sqlite3_column_text(statement.stmt, 0)))); - treeCtrl.AppendContainer(wxDataViewItem(wxNullPtr), hive); - } + treeCtrl.AppendContainer(wxDataViewItem(wxNullPtr), hive); } - else - { - wxMessageDialog msgDialog(NULL, "Error! Cannot load hive from hives table.", - "Error", wxOK | wxICON_ERROR); - msgDialog.ShowModal(); - sqlite3_free(m_ErrMsg); - } - - rc = sqlite3_finalize(stmt); } - catch (const std::exception &exception) + catch (const std::exception &e) { - wxLogDebug(exception.what()); + show_modal_dialog_and_log("Error! Cannot load hive from hives table: ", "Error", e.what()); } } @@ -1153,27 +866,23 @@ wxArrayString Database::CheckDuplicates(const wxArrayString &files) try { - std::string select = "SELECT * FROM SAMPLES WHERE FILENAME = ?;"; - - sqlite3_stmt *stmt = nullptr; - throw_on_sqlite3_error(sqlite3_prepare_v2(m_Database, select.c_str(), select.size(), &stmt, NULL)); + const auto sql = "SELECT * FROM SAMPLES WHERE FILENAME = ?;"; + Sqlite3Statement statement(m_Database, sql); for (unsigned int i = 0; i < files.size(); i++) { filename = files[i].AfterLast('/').BeforeLast('.').ToStdString(); - rc = sqlite3_bind_text(stmt, 1, filename.c_str(), filename.size(), SQLITE_STATIC); + throw_on_sqlite3_error(sqlite3_bind_text(statement.stmt, 1, filename.c_str(), filename.size(), SQLITE_STATIC)); - if (sqlite3_step(stmt) != SQLITE_ROW) + if (sqlite3_step(statement.stmt) != SQLITE_ROW) sorted_files.push_back(files[i]); else wxLogDebug("Already added: %s. Skipping..", files[i]); - rc = sqlite3_clear_bindings(stmt); - rc = sqlite3_reset(stmt); + rc = sqlite3_clear_bindings(statement.stmt); + rc = sqlite3_reset(statement.stmt); } - - sqlite3_finalize(stmt); } catch (const std::exception &exception) { @@ -1187,38 +896,24 @@ bool Database::IsTrashed(const std::string &filename) { try { - std::string select = "SELECT TRASHED FROM SAMPLES WHERE FILENAME = ?;"; + const auto sql = "SELECT TRASHED FROM SAMPLES WHERE FILENAME = ?;"; + Sqlite3Statement statement(m_Database, sql); - sqlite3_stmt *stmt = nullptr; - rc = sqlite3_prepare_v2(m_Database, select.c_str(), select.size(), &stmt, NULL); + throw_on_sqlite3_error(sqlite3_bind_text(statement.stmt, 1, filename.c_str(), filename.size(), SQLITE_STATIC)); - rc = sqlite3_bind_text(stmt, 1, filename.c_str(), filename.size(), SQLITE_STATIC); - - if (sqlite3_step(stmt) == SQLITE_ROW) + if (sqlite3_step(statement.stmt) == SQLITE_ROW) { wxLogDebug("Record found, fetching.."); - if (sqlite3_column_int(stmt, 0) == 1) + if (sqlite3_column_int(statement.stmt, 0) == 1) return true; } - rc = sqlite3_finalize(stmt); - - if (rc != SQLITE_OK) - { - wxMessageDialog msgDialog(NULL, "Error! Cannot select sample path from table.", - "Error", wxOK | wxICON_ERROR); - msgDialog.ShowModal(); - sqlite3_free(m_ErrMsg); - } - else - { - wxLogDebug("Selected data from table successfully."); - } + wxLogDebug("Selected data from table successfully."); } - catch (const std::exception &exception) + catch (const std::exception &e) { - wxLogDebug(exception.what()); + show_modal_dialog_and_log("Error! Cannot select sample path from table: ", "Error", e.what()); } return false; @@ -1228,36 +923,22 @@ void Database::UpdateTrashColumn(const std::string &filename, int value) { try { - std::string update = "UPDATE SAMPLES SET TRASHED = ? WHERE FILENAME = ?;"; - sqlite3_stmt *stmt = nullptr; + const auto sql = "UPDATE SAMPLES SET TRASHED = ? WHERE FILENAME = ?;"; + Sqlite3Statement statement(m_Database, sql); - rc = sqlite3_prepare_v2(m_Database, update.c_str(), update.size(), &stmt, NULL); + throw_on_sqlite3_error(sqlite3_bind_int(statement.stmt, 1, value)); + throw_on_sqlite3_error(sqlite3_bind_text(statement.stmt, 2, filename.c_str(), filename.size(), SQLITE_STATIC)); - rc = sqlite3_bind_int(stmt, 1, value); - rc = sqlite3_bind_text(stmt, 2, filename.c_str(), filename.size(), SQLITE_STATIC); - - if (sqlite3_step(stmt) == SQLITE_ROW) + if (sqlite3_step(statement.stmt) == SQLITE_ROW) { wxLogDebug("Record found, updating.."); } - rc = sqlite3_finalize(stmt); - - if (rc != SQLITE_OK) - { - wxMessageDialog msgDialog(NULL, "Error! Cannot update record.", - "Error", wxOK | wxICON_ERROR); - msgDialog.ShowModal(); - sqlite3_free(m_ErrMsg); - } - else - { - wxLogDebug("Updated record successfully."); - } + wxLogDebug("Updated record successfully."); } - catch (const std::exception &exception) + catch (const std::exception &e) { - wxLogDebug(exception.what()); + show_modal_dialog_and_log("Error! Cannot update record: ", "Error", e.what()); } } @@ -1271,75 +952,61 @@ Database::RestoreFromTrashByFilename(const std::string &filename, icon_empty = wxVariant(wxBitmap(icon_star_empty)); try { - std::string restore = "SELECT FAVORITE, FILENAME, EXTENSION, SAMPLEPACK, \ + const auto sql = "SELECT FAVORITE, FILENAME, EXTENSION, SAMPLEPACK, \ TYPE, CHANNELS, LENGTH, SAMPLERATE, BITRATE, PATH, \ TRASHED, HIVE FROM SAMPLES WHERE FILENAME = ?;"; - sqlite3_stmt *stmt = nullptr; + Sqlite3Statement statement(m_Database, sql); - rc = sqlite3_prepare_v2(m_Database, restore.c_str(), restore.size(), &stmt, NULL); + throw_on_sqlite3_error(sqlite3_bind_text(statement.stmt, 1, filename.c_str(), filename.size(), SQLITE_STATIC)); - rc = sqlite3_bind_text(stmt, 1, filename.c_str(), filename.size(), SQLITE_STATIC); - - if (rc == SQLITE_OK) + while (SQLITE_ROW == sqlite3_step(statement.stmt)) { - while (SQLITE_ROW == sqlite3_step(stmt)) + int favorite = sqlite3_column_int(statement.stmt, 0); + wxString filename = std::string(reinterpret_cast(sqlite3_column_text(statement.stmt, 1))); + wxString file_extension = std::string(reinterpret_cast(sqlite3_column_text(statement.stmt, 2))); + wxString sample_pack = std::string(reinterpret_cast(sqlite3_column_text(statement.stmt, 3))); + wxString sample_type = std::string(reinterpret_cast(sqlite3_column_text(statement.stmt, 4))); + int channels = sqlite3_column_int(statement.stmt, 5); + int length = sqlite3_column_int(statement.stmt, 6); + int sample_rate = sqlite3_column_int(statement.stmt, 7); + int bitrate = sqlite3_column_int(statement.stmt, 8); + wxString path = std::string(reinterpret_cast(sqlite3_column_text(statement.stmt, 9))); + int trashed = sqlite3_column_int(statement.stmt, 10); + wxString hive_name = std::string(reinterpret_cast(sqlite3_column_text(statement.stmt, 11))); + + wxLongLong llLength = length; + int total_min = static_cast((llLength / 60000).GetValue()); + int total_sec = static_cast(((llLength % 60000) / 1000).GetValue()); + + wxVector vec; + + if (trashed == 0) { - int favorite = sqlite3_column_int(stmt, 0); - wxString filename = std::string(reinterpret_cast(sqlite3_column_text(stmt, 1))); - wxString file_extension = std::string(reinterpret_cast(sqlite3_column_text(stmt, 2))); - wxString sample_pack = std::string(reinterpret_cast(sqlite3_column_text(stmt, 3))); - wxString sample_type = std::string(reinterpret_cast(sqlite3_column_text(stmt, 4))); - int channels = sqlite3_column_int(stmt, 5); - int length = sqlite3_column_int(stmt, 6); - int sample_rate = sqlite3_column_int(stmt, 7); - int bitrate = sqlite3_column_int(stmt, 8); - wxString path = std::string(reinterpret_cast(sqlite3_column_text(stmt, 9))); - int trashed = sqlite3_column_int(stmt, 10); - wxString hive_name = std::string(reinterpret_cast(sqlite3_column_text(stmt, 11))); + if (favorite == 1) + vec.push_back(icon_filled); + else + vec.push_back(icon_empty); - wxLongLong llLength = length; - int total_min = static_cast((llLength / 60000).GetValue()); - int total_sec = static_cast(((llLength % 60000) / 1000).GetValue()); + if (show_extension) + vec.push_back(path.AfterLast('/')); + else + vec.push_back(path.AfterLast('/').BeforeLast('.')); - wxVector vec; + vec.push_back(sample_pack); + vec.push_back(sample_type); + vec.push_back(wxString::Format("%d", channels)); + vec.push_back(wxString::Format("%2i:%02i", total_min, total_sec)); + vec.push_back(wxString::Format("%d", sample_rate)); + vec.push_back(wxString::Format("%d", bitrate)); + vec.push_back(path); - if (trashed == 0) - { - if (favorite == 1) - vec.push_back(icon_filled); - else - vec.push_back(icon_empty); - - if (show_extension) - vec.push_back(path.AfterLast('/')); - else - vec.push_back(path.AfterLast('/').BeforeLast('.')); - - vec.push_back(sample_pack); - vec.push_back(sample_type); - vec.push_back(wxString::Format("%d", channels)); - vec.push_back(wxString::Format("%2i:%02i", total_min, total_sec)); - vec.push_back(wxString::Format("%d", sample_rate)); - vec.push_back(wxString::Format("%d", bitrate)); - vec.push_back(path); - - vecSet.push_back(vec); - } + vecSet.push_back(vec); } } - else - { - wxMessageDialog msgDialog(NULL, "Error! Cannot load data from table.", - "Error", wxOK | wxICON_ERROR); - msgDialog.ShowModal(); - sqlite3_free(m_ErrMsg); - } - - rc = sqlite3_finalize(stmt); } - catch (const std::exception &exception) + catch (const std::exception &e) { - wxLogDebug(exception.what()); + show_modal_dialog_and_log("Error! Cannot load data from table: ", "Error", e.what()); } return vecSet; @@ -1347,28 +1014,10 @@ Database::RestoreFromTrashByFilename(const std::string &filename, void Database::open(const std::string &dbPath) { - try - { - throw_on_sqlite3_error(sqlite3_open(dbPath.c_str(), &m_Database)); - - const auto qry = std::string("pragma journal_mode = WAL; pragma synchronous = normal; pragma temp_store = memory; pragma mmap_size = 30000000000;"); - - sqlite3_stmt *stmt = nullptr; - throw_on_sqlite3_error(sqlite3_prepare_v2(m_Database, qry.c_str(), qry.size(), &stmt, NULL)); - if (SQLITE_ROW != sqlite3_step(stmt)) - { - wxLogDebug("Error executing query"); - } - throw_on_sqlite3_error(sqlite3_finalize(stmt)); - } - catch (const std::exception &exception) - { - wxLogDebug(exception.what()); - } + throw_on_sqlite3_error(sqlite3_open(dbPath.c_str(), &m_Database)); } void Database::close() { throw_on_sqlite3_error(sqlite3_close(m_Database)); } - diff --git a/src/MainFrame.cpp b/src/MainFrame.cpp index 99e2f7e..26630d5 100644 --- a/src/MainFrame.cpp +++ b/src/MainFrame.cpp @@ -398,7 +398,12 @@ MainFrame::MainFrame() // Intializing wxTimer m_Timer = new wxTimer(this); - m_TopWaveformPanel = new WaveformViewer(this, m_TopPanel, *m_Library, *m_MediaCtrl, *m_InfoBar, + // Initialize the database + m_database = std::make_unique(*m_InfoBar, m_DatabaseFilepath); + m_database->CreateTableSamples(); + m_database->CreateTableHives(); + + m_TopWaveformPanel = new WaveformViewer(this, m_TopPanel, *m_Library, *m_MediaCtrl, *m_database, m_ConfigFilepath, m_DatabaseFilepath); // Binding events. @@ -543,11 +548,6 @@ MainFrame::MainFrame() m_BottomRightPanelMainSizer->SetSizeHints(m_BottomRightPanel); m_BottomRightPanelMainSizer->Layout(); - // Initialize the database - m_database = std::make_unique(*m_InfoBar, m_DatabaseFilepath); - m_database->CreateTableSamples(); - m_database->CreateTableHives(); - // Restore the data previously added to Library LoadDatabase(); diff --git a/src/WaveformViewer.cpp b/src/WaveformViewer.cpp index d69ed10..64a6739 100644 --- a/src/WaveformViewer.cpp +++ b/src/WaveformViewer.cpp @@ -39,10 +39,10 @@ #include WaveformViewer::WaveformViewer(wxWindow* parentFrame, wxWindow* window, wxDataViewListCtrl& library, - wxMediaCtrl& mediaCtrl, wxInfoBar& infoBar, + wxMediaCtrl& mediaCtrl, Database& database, const std::string& configFilepath, const std::string& databaseFilepath) : wxPanel(window, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL | wxNO_BORDER | wxFULL_REPAINT_ON_RESIZE), - m_ParentFrame(parentFrame), m_Window(window), m_Library(library), m_InfoBar(infoBar), m_MediaCtrl(mediaCtrl), + m_ParentFrame(parentFrame), m_Window(window), m_Library(library), m_database(database), m_MediaCtrl(mediaCtrl), m_ConfigFilepath(configFilepath), m_DatabaseFilepath(databaseFilepath) { this->SetDoubleBuffered(true); @@ -110,15 +110,13 @@ void WaveformViewer::OnPaint(wxPaintEvent& event) void WaveformViewer::RenderPlayhead(wxDC& dc) { - Database db(m_InfoBar); - int selected_row = m_Library.GetSelectedRow(); if (selected_row < 0) return; wxString selected = m_Library.GetTextValue(selected_row, 1); - std::string path = db.GetSamplePathByFilename(m_DatabaseFilepath, selected.BeforeLast('.').ToStdString()); + std::string path = m_database.GetSamplePathByFilename(selected.BeforeLast('.').ToStdString()); Tags tags(path); @@ -150,7 +148,6 @@ void WaveformViewer::RenderPlayhead(wxDC& dc) void WaveformViewer::UpdateWaveformBitmap() { - Database db(m_InfoBar); Settings settings(m_ParentFrame, m_ConfigFilepath, m_DatabaseFilepath); Serializer serializer(m_ConfigFilepath); @@ -161,12 +158,12 @@ void WaveformViewer::UpdateWaveformBitmap() wxString selection = m_Library.GetTextValue(selected_row, 1); - wxString filepath_with_extension = db.GetSamplePathByFilename(m_DatabaseFilepath, selection.BeforeLast('.').ToStdString()); - wxString filepath_without_extension = db.GetSamplePathByFilename(m_DatabaseFilepath, selection.ToStdString()); + wxString filepath_with_extension = m_database.GetSamplePathByFilename(selection.BeforeLast('.').ToStdString()); + wxString filepath_without_extension = m_database.GetSamplePathByFilename(selection.ToStdString()); std::string extension = settings.ShouldShowFileExtension() ? - db.GetSampleFileExtension(m_DatabaseFilepath, selection.ToStdString()) : - db.GetSampleFileExtension(m_DatabaseFilepath, selection.BeforeLast('.').ToStdString()); + m_database.GetSampleFileExtension(selection.ToStdString()) : + m_database.GetSampleFileExtension(selection.BeforeLast('.').ToStdString()); wxString path = selection.Contains(wxString::Format(".%s", extension)) ? filepath_with_extension : filepath_without_extension; @@ -293,15 +290,13 @@ void WaveformViewer::OnControlKeyUp(wxKeyEvent &event) void WaveformViewer::OnMouseMotion(wxMouseEvent& event) { - Database db(m_InfoBar); - int selected_row = m_Library.GetSelectedRow(); if (selected_row < 0) return; wxString selected = m_Library.GetTextValue(selected_row, 1); - std::string path = db.GetSamplePathByFilename(m_DatabaseFilepath, selected.BeforeLast('.').ToStdString()); + std::string path = m_database.GetSamplePathByFilename(selected.BeforeLast('.').ToStdString()); Tags tags(path); @@ -335,14 +330,13 @@ void WaveformViewer::OnMouseMotion(wxMouseEvent& event) void WaveformViewer::OnMouseLeftButtonDown(wxMouseEvent& event) { - Database db(m_InfoBar); int selected_row = m_Library.GetSelectedRow(); if (selected_row < 0) return; wxString selected = m_Library.GetTextValue(selected_row, 1); - std::string path = db.GetSamplePathByFilename(m_DatabaseFilepath, selected.BeforeLast('.').ToStdString()); + std::string path = m_database.GetSamplePathByFilename(selected.BeforeLast('.').ToStdString()); Tags tags(path); @@ -385,15 +379,13 @@ void WaveformViewer::OnMouseLeftButtonDown(wxMouseEvent& event) void WaveformViewer::OnMouseLeftButtonUp(wxMouseEvent& event) { - Database db(m_InfoBar); - int selected_row = m_Library.GetSelectedRow(); if (selected_row < 0) return; wxString selected = m_Library.GetTextValue(selected_row, 1); - std::string path = db.GetSamplePathByFilename(m_DatabaseFilepath, selected.BeforeLast('.').ToStdString()); + std::string path = m_database.GetSamplePathByFilename(selected.BeforeLast('.').ToStdString()); Tags tags(path); @@ -457,15 +449,13 @@ void WaveformViewer::SendLoopPoints() SampleHive::SH_LoopPointsEvent event(SampleHive::SH_EVT_LOOP_POINTS_UPDATED, this->GetId()); event.SetEventObject(this); - Database db(m_InfoBar); - int selected_row = m_Library.GetSelectedRow(); if (selected_row < 0) return; wxString selected = m_Library.GetTextValue(selected_row, 1); - std::string path = db.GetSamplePathByFilename(m_DatabaseFilepath, selected.BeforeLast('.').ToStdString()); + std::string path = m_database.GetSamplePathByFilename(selected.BeforeLast('.').ToStdString()); Tags tags(path); diff --git a/src/WaveformViewer.hpp b/src/WaveformViewer.hpp index a1f6a67..f16b5a0 100644 --- a/src/WaveformViewer.hpp +++ b/src/WaveformViewer.hpp @@ -20,6 +20,8 @@ #pragma once +#include "Database.hpp" + #include #include #include @@ -37,7 +39,7 @@ class WaveformViewer : public wxPanel { public: WaveformViewer(wxWindow* parentFrame, wxWindow* window, wxDataViewListCtrl& library, - wxMediaCtrl& mediaCtrl, wxInfoBar& infoBar, + wxMediaCtrl& mediaCtrl, Database& database, const std::string& configFilepath, const std::string& databaseFilepath); ~WaveformViewer(); @@ -47,7 +49,7 @@ class WaveformViewer : public wxPanel wxWindow* m_Window; wxDataViewListCtrl& m_Library; - wxInfoBar& m_InfoBar; + Database& m_database; wxMediaCtrl& m_MediaCtrl; const std::string& m_ConfigFilepath; From c7005dd559a931e65922a782bdd6e3600411236a Mon Sep 17 00:00:00 2001 From: Mathias Buhr Date: Thu, 7 Oct 2021 22:48:57 +0200 Subject: [PATCH 6/6] Checkout submodules as well --- subprojects/wxwidgets.wrap | 1 + 1 file changed, 1 insertion(+) diff --git a/subprojects/wxwidgets.wrap b/subprojects/wxwidgets.wrap index 301cf22..dbe112d 100644 --- a/subprojects/wxwidgets.wrap +++ b/subprojects/wxwidgets.wrap @@ -1,3 +1,4 @@ [wrap-git] url = https://github.com/wxWidgets/wxWidgets revision = master +clone-recursive = true \ No newline at end of file