Merge branch 'testing' into 'testing'

Fix Segfault And SQLITE_BUSY Bug

This fixes the segfaults and cleans up unneeded variables, also fixes the SQLITE_BUSY bug.

See merge request apoorv569/sample-hive!1
This commit is contained in:
Apoorv 2021-04-25 18:42:03 +00:00
commit 265febe09a
2 changed files with 12 additions and 21 deletions

View File

@ -36,7 +36,7 @@ void Database::CreateDatabase()
try try
{ {
if (sqlite3_open("sample.hive", &m_Database) != SQLITE_OK && IsOK) if (sqlite3_open("sample.hive", &m_Database) != SQLITE_OK)
{ {
wxLogDebug("Error opening DB"); wxLogDebug("Error opening DB");
throw sqlite3_errmsg(m_Database); throw sqlite3_errmsg(m_Database);
@ -81,13 +81,6 @@ void Database::InsertSample(int favorite, std::string filename,
{ {
try try
{ {
// while (!IsOK)
// {
// wxLogDebug("Waiting On Previous Transaction");
// }
m.lock();
// rc = sqlite3_open("sample.hive", &m_Database);
if (sqlite3_open("sample.hive", &m_Database) != SQLITE_OK) if (sqlite3_open("sample.hive", &m_Database) != SQLITE_OK)
{ {
wxLogDebug("Error opening DB"); wxLogDebug("Error opening DB");
@ -118,10 +111,12 @@ void Database::InsertSample(int favorite, std::string filename,
rc = sqlite3_bind_int(m_Stmt, 9, bitrate); rc = sqlite3_bind_int(m_Stmt, 9, bitrate);
rc = sqlite3_bind_text(m_Stmt, 10, path.c_str(), path.size(), SQLITE_STATIC); rc = sqlite3_bind_text(m_Stmt, 10, path.c_str(), path.size(), SQLITE_STATIC);
rc = sqlite3_bind_int(m_Stmt, 11, trashed); rc = sqlite3_bind_int(m_Stmt, 11, trashed);
if (sqlite3_step(m_Stmt) != SQLITE_DONE) rc = sqlite3_step(m_Stmt);
if (rc != SQLITE_DONE)
{ {
wxLogDebug("No data inserted. Error code: %d", sqlite3_step(m_Stmt)); wxLogDebug("No data inserted. Error code: %d: Msg: %s", rc , sqlite3_errmsg(m_Database));
} }
rc = sqlite3_finalize(m_Stmt); rc = sqlite3_finalize(m_Stmt);
@ -132,12 +127,11 @@ void Database::InsertSample(int favorite, std::string filename,
// "Error! Cannot insert data into table.", // "Error! Cannot insert data into table.",
// "Error", wxOK | wxICON_ERROR); // "Error", wxOK | wxICON_ERROR);
// msgDialog.ShowModal(); // msgDialog.ShowModal();
wxLogDebug("Error! Cannot insert data into table. Error code: %d, Error Message: %s", rc, m_ErrMsg); wxLogDebug("Error! Cannot insert data into table. Error code: %d: Msg: %s", rc , sqlite3_errmsg(m_Database));
sqlite3_free(m_ErrMsg);
} }
else else
{ {
wxLogDebug("Data inserted successfully. %s", m_ErrMsg); wxLogDebug("Data inserted successfully.");
} }
if (rc == SQLITE_BUSY) if (rc == SQLITE_BUSY)
@ -162,13 +156,10 @@ void Database::InsertSample(int favorite, std::string filename,
wxLogDebug("SQLITE_INTERNAL"); wxLogDebug("SQLITE_INTERNAL");
sqlite3_close(m_Database); sqlite3_close(m_Database);
// IsOK = true;
m.unlock();
} }
catch (const std::exception &exception) catch (const std::exception &exception)
{ {
wxLogDebug(exception.what()); wxLogDebug(exception.what());
m.unlock();
} }
} }
@ -755,6 +746,7 @@ wxVector<wxVector<wxVariant>> Database::FilterDatabaseBySampleName(wxVector<wxVe
bool Database::HasSample(std::string filename) bool Database::HasSample(std::string filename)
{ {
std::string sample; std::string sample;
bool haveSample = false;
try try
{ {
rc = sqlite3_open("sample.hive", &m_Database); rc = sqlite3_open("sample.hive", &m_Database);
@ -769,7 +761,7 @@ bool Database::HasSample(std::string filename)
{ {
wxLogInfo("Record found, fetching.."); wxLogInfo("Record found, fetching..");
sample = std::string(reinterpret_cast< const char* >(sqlite3_column_text(m_Stmt, 0))); sample = std::string(reinterpret_cast< const char* >(sqlite3_column_text(m_Stmt, 0)));
return true; haveSample = true;
} }
rc = sqlite3_finalize(m_Stmt); rc = sqlite3_finalize(m_Stmt);
@ -787,6 +779,8 @@ bool Database::HasSample(std::string filename)
} }
sqlite3_close(m_Database); sqlite3_close(m_Database);
return haveSample;
} }
catch (const std::exception &exception) catch (const std::exception &exception)
{ {

View File

@ -21,9 +21,6 @@ class Database
int rc; int rc;
char* m_ErrMsg; char* m_ErrMsg;
sqlite3_stmt* m_Stmt; sqlite3_stmt* m_Stmt;
std::mutex m;
private: private:
// ------------------------------------------------------------------- // -------------------------------------------------------------------
wxInfoBar& m_InfoBar; wxInfoBar& m_InfoBar;