diff options
author | Andrey Oleynik <aoleynik@luxoft.com> | 2015-06-18 18:38:19 +0300 |
---|---|---|
committer | Andrey Oleynik <my@email.com> | 2015-07-26 15:53:18 +0300 |
commit | 3b9cf20c66ac99fed64317fdd56129e15ceb13d1 (patch) | |
tree | d7c91cfdb39c08deb65194b42a55a19cf88a7975 /src/components/application_manager/src/resumption/resumption_data_db.cc | |
parent | 6dbcb82f5f17766b10cf762e943141b48285e1d8 (diff) | |
download | sdl_core-3b9cf20c66ac99fed64317fdd56129e15ceb13d1.tar.gz |
Implemented DB version checking and re-building on version change.
Resumption and policy DBs will be re-built on internal DBs version
change. Old data will be kept untouched (in case of same structs will be
present in new version), new structs and/or field can be added with new
SDL binaries support.
Conflicts:
src/components/application_manager/src/resumption/resumption_data_db.cc
src/components/policy/src/policy/include/policy/pt_representation.h
src/components/policy/src/policy/include/policy/sql_pt_representation.h
src/components/policy/src/policy/src/cache_manager.cc
src/components/policy/src/policy/src/sql_pt_representation.cc
Diffstat (limited to 'src/components/application_manager/src/resumption/resumption_data_db.cc')
-rw-r--r-- | src/components/application_manager/src/resumption/resumption_data_db.cc | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/src/components/application_manager/src/resumption/resumption_data_db.cc b/src/components/application_manager/src/resumption/resumption_data_db.cc index afb6c97d06..395242773a 100644 --- a/src/components/application_manager/src/resumption/resumption_data_db.cc +++ b/src/components/application_manager/src/resumption/resumption_data_db.cc @@ -37,6 +37,8 @@ #include "application_manager/smart_object_keys.h" #include "config_profile/profile.h" #include "application_manager/message_helper.h" +#include "utils/helpers.h" +#include "utils/gen_hash.h" namespace { const std::string kDatabaseName = "resumption"; @@ -595,6 +597,110 @@ void ResumptionDataDB::UpdateHmiLevel(const std::string& policy_app_id, } } +bool ResumptionDataDB::RefreshDB() const { + utils::dbms::SQLQuery query(db()); + if (!query.Exec(resumption::kDropSchema)) { + LOG4CXX_WARN(logger_, + "Failed dropping database: " << query.LastError().text()); + return false; + } + if (!query.Exec(resumption::kCreateSchema)) { + LOG4CXX_ERROR( + logger_, + "Failed creating schema of database: " << query.LastError().text()); + return false; + } + if (!query.Exec(resumption::kInsertInitData)) { + LOG4CXX_ERROR( + logger_, + "Failed insert init data to database: " << query.LastError().text()); + return false; + } + return true; +} + +bool ResumptionDataDB::GetAllData(smart_objects::SmartObject& data) const { + LOG4CXX_AUTO_TRACE(logger_); + utils::dbms::SQLQuery query(db()); + if (!query.Prepare(resumption::kSelectAllApps)) { + LOG4CXX_ERROR(logger_, "Can't get applications data from DB."); + return false; + } + + data = smart_objects::SmartObject(smart_objects::SmartType_Array); + + uint32_t index = 0; + while (query.Next()) { + const std::string app_id = query.GetString(0); + const std::string device_id = query.GetString(1); + if (GetSavedApplication(app_id, device_id, data[index])) { + ++index; + } + } + return true; +} + +bool ResumptionDataDB::SaveAllData( + const smart_objects::SmartObject& data) { + LOG4CXX_AUTO_TRACE(logger_); + if (smart_objects::SmartType_Array != data.getType()) { + LOG4CXX_ERROR(logger_, "Unexpected type for resumption data."); + return false; + } + const smart_objects::SmartArray* apps = data.asArray(); + smart_objects::SmartArray::const_iterator it_apps = + apps->begin(); + for (;apps->end() != it_apps; ++it_apps) { + if (!SaveApplicationToDB((*it_apps), + (*it_apps)["appID"].asString(), + (*it_apps)["deviceID"].asString())) { + return false; + } + } + return true; +} + +bool ResumptionDataDB::IsDBVersionActual() const { + LOG4CXX_AUTO_TRACE(logger_); + utils::dbms::SQLQuery query(db()); + if (!query.Prepare(resumption::kSelectDBVersion) || !query.Exec()) { + LOG4CXX_ERROR(logger_, "Failed to get DB version: " + << query.LastError().text()); + return false; + } + + const int32_t saved_db_version = query.GetInteger(0); + const int32_t current_db_version = GetDBVersion(); + LOG4CXX_DEBUG(logger_, "Saved DB version is: " << saved_db_version + << ". Current DB vesion is: " << current_db_version); + + return current_db_version == saved_db_version; +} + +bool ResumptionDataDB::UpdateDBVersion() const { + LOG4CXX_AUTO_TRACE(logger_); + utils::dbms::SQLQuery query(db()); + if (!query.Prepare(resumption::kUpdateDBVersion)) { + LOG4CXX_ERROR(logger_, "Incorrect DB version update query: " + << query.LastError().text()); + return false; + } + + query.Bind(0, GetDBVersion()); + + if (!query.Exec()) { + LOG4CXX_ERROR(logger_, "DB version update failed: " + << query.LastError().text()); + return false; + } + + return true; +} + +const int32_t ResumptionDataDB::GetDBVersion() const { + return utils::Djb2HashFromString(resumption::kCreateSchema); +} + bool ResumptionDataDB::SelectFilesData(const std::string& policy_app_id, const std::string& device_id, smart_objects::SmartObject& saved_app) const { |