Skip to content

Commit

Permalink
Merge pull request #385 from Infomaniak/KDESKTOP-1370-DB-error-at-par…
Browse files Browse the repository at this point in the history
…msDB-creation

KDESKTOP-1370-DB-error-at-parmsDB-creation
  • Loading branch information
herve-er authored Nov 26, 2024
2 parents 299a735 + bac1cce commit 4e5311b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/libparms/db/parmsdb.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ class PARMS_EXPORT ParmsDb : public Db {

bool insertDefaultParameters();
bool insertDefaultAppState();
bool insertAppState(AppStateKey key, const std::string &value, bool noEmptyValue = false);
bool insertAppState(AppStateKey key, const std::string &value, bool updateOnlyIfEmpty = false);
bool updateExclusionTemplates();

bool createAppState();
Expand Down
34 changes: 14 additions & 20 deletions src/libparms/db/parmsdbappstate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,8 @@ bool ParmsDb::insertDefaultAppState() {
return true;
}

bool ParmsDb::insertAppState(AppStateKey key, const std::string &value, bool noEmptyValue /*= false*/) {
bool ParmsDb::insertAppState(AppStateKey key, const std::string &value, const bool updateOnlyIfEmpty /*= false*/) {
const std::scoped_lock lock(_mutex);
int errId = 0;
std::string error;
bool found = false;
std::string valueStr = value;
if (valueStr.empty()) {
LOG_WARN(_logger, "Value is empty for AppStateKey: " << CommonUtility::appStateKeyToString(key).c_str());
Expand All @@ -127,31 +124,28 @@ bool ParmsDb::insertAppState(AppStateKey key, const std::string &value, bool noE

ASSERT(queryResetAndClearBindings(SELECT_APP_STATE_REQUEST_ID))
ASSERT(queryBindValue(SELECT_APP_STATE_REQUEST_ID, 1, static_cast<int>(key)))
bool found = false;
if (!queryNext(SELECT_APP_STATE_REQUEST_ID, found)) {
LOG_WARN(_logger, "Error getting query result: " << SELECT_APP_STATE_REQUEST_ID);
return false;
}
std::string existingValue;
ASSERT(queryStringValue(SELECT_APP_STATE_REQUEST_ID, 0, existingValue))
if (found) {
ASSERT(queryStringValue(SELECT_APP_STATE_REQUEST_ID, 0, existingValue))
}
ASSERT(queryResetAndClearBindings(SELECT_APP_STATE_REQUEST_ID))

if (!found) {
ASSERT(queryResetAndClearBindings(INSERT_APP_STATE_REQUEST_ID))
ASSERT(queryBindValue(INSERT_APP_STATE_REQUEST_ID, 1, static_cast<int>(key)))
ASSERT(queryBindValue(INSERT_APP_STATE_REQUEST_ID, 2, valueStr))
if (!queryExec(INSERT_APP_STATE_REQUEST_ID, errId, error)) {
LOG_WARN(_logger, "Error running query: " << INSERT_APP_STATE_REQUEST_ID);
return false;
}
} else if (noEmptyValue && existingValue.empty()) {
ASSERT(queryResetAndClearBindings(UPDATE_APP_STATE_REQUEST_ID))
ASSERT(queryBindValue(UPDATE_APP_STATE_REQUEST_ID, 1, static_cast<int>(key)))
ASSERT(queryBindValue(UPDATE_APP_STATE_REQUEST_ID, 2, valueStr))
if (!queryExec(UPDATE_APP_STATE_REQUEST_ID, errId, error)) {
LOG_WARN(_logger, "Error running query: " << UPDATE_APP_STATE_REQUEST_ID);
if (!found || (updateOnlyIfEmpty && existingValue.empty())) {
const auto requestId = found ? UPDATE_APP_STATE_REQUEST_ID : INSERT_APP_STATE_REQUEST_ID;
ASSERT(queryBindValue(requestId, 1, static_cast<int>(key)))
ASSERT(queryBindValue(requestId, 2, valueStr))
int errId = 0;
std::string error;
if (!queryExec(requestId, errId, error)) {
LOG_WARN(_logger, "Error running query: " << requestId);
return false;
}
ASSERT(queryResetAndClearBindings(UPDATE_APP_STATE_REQUEST_ID))
ASSERT(queryResetAndClearBindings(requestId))
}
return true;
}
Expand Down
8 changes: 8 additions & 0 deletions test/libparms/db/testparmsdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,14 @@ void TestParmsDb::testAppState(void) {
CPPUNIT_ASSERT_EQUAL(std::string("value"), std::get<std::string>(valueRes));
i++;
};

// Test add defaut value if and only if value is empty
CPPUNIT_ASSERT(ParmsDb::instance()->insertAppState(AppStateKey::Unknown, "test1", true));
CPPUNIT_ASSERT(ParmsDb::instance()->selectAppState(AppStateKey::Unknown, value, found) && found);
CPPUNIT_ASSERT_EQUAL(std::string("test1"), std::get<std::string>(value));
CPPUNIT_ASSERT(ParmsDb::instance()->insertAppState(AppStateKey::Unknown, "test2", true));
CPPUNIT_ASSERT(ParmsDb::instance()->selectAppState(AppStateKey::Unknown, value, found) && found);
CPPUNIT_ASSERT_EQUAL(std::string("test1"), std::get<std::string>(value));
}

#ifdef __APPLE__
Expand Down

0 comments on commit 4e5311b

Please sign in to comment.