diff options
-rw-r--r-- | src/manager/service/db-crypto.cpp | 64 | ||||
-rw-r--r-- | src/manager/service/db-crypto.h | 6 |
2 files changed, 31 insertions, 39 deletions
diff --git a/src/manager/service/db-crypto.cpp b/src/manager/service/db-crypto.cpp index 2bc2a92e..d7acb0fc 100644 --- a/src/manager/service/db-crypto.cpp +++ b/src/manager/service/db-crypto.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved + * Copyright (c) 2014 - 2019 Samsung Electronics Co., Ltd All Rights Reserved * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -229,25 +229,28 @@ void Crypto::createView(const char *create_cmd) bool Crypto::getDBVersion(int &schemaVersion) { - SchemaInfo SchemaInfo(this); - - if (SchemaInfo.getVersionInfo(schemaVersion)) { - LogDebug("Current DB version: " << schemaVersion); - return true; - } else { - LogDebug("No DB version known or DB not present"); + Transaction transaction(this); - if (m_connection->CheckTableExist("CKM_TABLE")) { - // special case: old CKM_TABLE exists - schemaVersion = DB_VERSION_1; - return true; - } else if (m_connection->CheckTableExist("NAME_TABLE")) { - // special case: new scheme exists, but no SCHEMA_INFO table present - schemaVersion = DB_VERSION_2; + if (m_connection->CheckTableExist("SCHEMA_INFO")) { + SchemaInfo SchemaInfo(m_connection); + if (SchemaInfo.getVersionInfo(schemaVersion)) { + LogDebug("Current DB version: " << schemaVersion); return true; } } + LogDebug("No DB version known or DB not present"); + + if (m_connection->CheckTableExist("CKM_TABLE")) { + // special case: old CKM_TABLE exists + schemaVersion = DB_VERSION_1; + return true; + } else if (m_connection->CheckTableExist("NAME_TABLE")) { + // special case: new scheme exists, but no SCHEMA_INFO table present + schemaVersion = DB_VERSION_2; + return true; + } + // not recognized - proceed with an empty DBs return false; } @@ -282,7 +285,7 @@ void Crypto::initDatabase() } // update DB version info - SchemaInfo SchemaInfo(this); + SchemaInfo SchemaInfo(m_connection); SchemaInfo.setVersionInfo(); transaction.commit(); } @@ -320,7 +323,7 @@ void Crypto::createDBSchema() "Can not create the database schema: no initialization script"); m_connection->ExecCommand((*script).c_str()); - SchemaInfo SchemaInfo(this); + SchemaInfo SchemaInfo(m_connection); SchemaInfo.setVersionInfo(); transaction.commit(); } @@ -712,34 +715,23 @@ void Crypto::setPermission( void Crypto::SchemaInfo::setVersionInfo() { SqlConnection::DataCommandUniquePtr insertContextCommand = - m_db->m_connection->PrepareDataCommand(DB_CMD_SCHEMA_SET); + m_connection->PrepareDataCommand(DB_CMD_SCHEMA_SET); insertContextCommand->BindString(101, DB_SCHEMA_VERSION_FIELD); insertContextCommand->BindString(103, std::to_string(DB_VERSION_CURRENT).c_str()); insertContextCommand->Step(); } -bool Crypto::SchemaInfo::getVersionInfo(int &version) const +bool Crypto::SchemaInfo::getVersionInfo(int &version) { - // Try..Catch mandatory here - we don't need to escalate the error - // if it happens - we just won't return the version, allowing CKM to work - try { - SqlConnection::DataCommandUniquePtr selectCommand = - m_db->m_connection->PrepareDataCommand(DB_CMD_SCHEMA_GET); - selectCommand->BindString(101, DB_SCHEMA_VERSION_FIELD); + SqlConnection::DataCommandUniquePtr selectCommand = + m_connection->PrepareDataCommand(DB_CMD_SCHEMA_GET); + selectCommand->BindString(101, DB_SCHEMA_VERSION_FIELD); - if (selectCommand->Step()) { - version = static_cast<int>(atoi(selectCommand->GetColumnString(1).c_str())); - return true; - } - } catch (const SqlConnection::Exception::InvalidColumn &) { - LogError("Select statement invalid column error"); - } catch (const SqlConnection::Exception::SyntaxError &) { - LogError("Couldn't prepare select statement"); - } catch (const SqlConnection::Exception::InternalError &) { - LogError("Couldn't execute select statement"); + if (selectCommand->Step()) { + version = static_cast<int>(atoi(selectCommand->GetColumnString(1).c_str())); + return true; } - return false; } diff --git a/src/manager/service/db-crypto.h b/src/manager/service/db-crypto.h index 65ef3a38..ad0ef12b 100644 --- a/src/manager/service/db-crypto.h +++ b/src/manager/service/db-crypto.h @@ -231,13 +231,13 @@ private: class SchemaInfo { public: - explicit SchemaInfo(const Crypto *db) : m_db(db) {} + explicit SchemaInfo(SqlConnection *connection) : m_connection(connection) {} void setVersionInfo(); - bool getVersionInfo(int &version) const; + bool getVersionInfo(int &version); private: - const Crypto *m_db; + SqlConnection *m_connection; }; public: |