Store DB::Crypto::m_connection as unique_ptr 70/227970/1
authorKonrad Lipinski <k.lipinski2@samsung.com>
Tue, 17 Mar 2020 16:31:45 +0000 (17:31 +0100)
committerKonrad Lipinski <k.lipinski2@samsung.com>
Tue, 17 Mar 2020 16:31:45 +0000 (17:31 +0100)
Change-Id: I289c8c7c62af72ae34ac1692f89af1d2bfd813f6

src/manager/service/db-crypto.cpp
src/manager/service/db-crypto.h

index d7acb0f..1121d66 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014 - 2019 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2014-2020 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.
@@ -150,10 +150,10 @@ const char *DB_CMD_NAME_SELECT_BY_TYPE_AND_PERMISSION =
 namespace CKM {
 namespace DB {
 Crypto::Crypto(const std::string &path, const RawBuffer &rawPass) :
-       m_connection(nullptr), m_inUserTransaction(false)
+       m_inUserTransaction(false)
 {
        try {
-               m_connection = new SqlConnection(path, SqlConnection::Flag::Option::CRW);
+               m_connection.reset(new SqlConnection(path, SqlConnection::Flag::Option::CRW));
                m_connection->SetKey(rawPass);
                initDatabase();
                m_connection->ExecCommand("VACUUM;");
@@ -173,27 +173,18 @@ Crypto::Crypto(const std::string &path, const RawBuffer &rawPass) :
 }
 
 Crypto::Crypto(Crypto &&other) :
-       m_connection(other.m_connection),
+       m_connection(std::move(other.m_connection)),
        m_inUserTransaction(other.m_inUserTransaction)
 {
-       other.m_connection = NULL;
        other.m_inUserTransaction = false;
 }
 
-Crypto::~Crypto()
-{
-       delete m_connection;
-}
-
 Crypto &Crypto::operator=(Crypto &&other)
 {
        if (this == &other)
                return *this;
 
-       delete m_connection;
-
-       m_connection = other.m_connection;
-       other.m_connection = NULL;
+       m_connection = std::move(other.m_connection);
 
        m_inUserTransaction = other.m_inUserTransaction;
        other.m_inUserTransaction = false;
@@ -232,7 +223,7 @@ bool Crypto::getDBVersion(int &schemaVersion)
        Transaction transaction(this);
 
        if (m_connection->CheckTableExist("SCHEMA_INFO")) {
-               SchemaInfo SchemaInfo(m_connection);
+               SchemaInfo SchemaInfo(m_connection.get());
                if (SchemaInfo.getVersionInfo(schemaVersion)) {
                        LogDebug("Current DB version: " << schemaVersion);
                        return true;
@@ -285,7 +276,7 @@ void Crypto::initDatabase()
                }
 
                // update DB version info
-               SchemaInfo SchemaInfo(m_connection);
+               SchemaInfo SchemaInfo(m_connection.get());
                SchemaInfo.setVersionInfo();
                transaction.commit();
        }
@@ -323,7 +314,7 @@ void Crypto::createDBSchema()
                                 "Can not create the database schema: no initialization script");
 
        m_connection->ExecCommand((*script).c_str());
-       SchemaInfo SchemaInfo(m_connection);
+       SchemaInfo SchemaInfo(m_connection.get());
        SchemaInfo.setVersionInfo();
        transaction.commit();
 }
@@ -344,7 +335,7 @@ void Crypto::resetDB()
 bool Crypto::isNameOwnerPresent(const Name &name, const ClientId &owner) const
 {
        try {
-               NameTable nameTable(this->m_connection);
+               NameTable nameTable(m_connection.get());
                return nameTable.isPresent(name, owner);
        } catch (const SqlConnection::Exception::SyntaxError &) {
                LogError("Couldn't prepare insert statement");
@@ -361,9 +352,9 @@ void Crypto::saveRows(const Name &name, const ClientId &owner,
 {
        try {
                // transaction is present in the layer above
-               NameTable nameTable(this->m_connection);
-               ObjectTable objectTable(this->m_connection);
-               PermissionTable permissionTable(this->m_connection);
+               NameTable nameTable(m_connection.get());
+               ObjectTable objectTable(m_connection.get());
+               PermissionTable permissionTable(m_connection.get());
                nameTable.addRow(name, owner);
 
                for (const auto &i : rows)
@@ -389,9 +380,9 @@ void Crypto::saveRow(const Row &row)
 {
        try {
                // transaction is present in the layer above
-               NameTable nameTable(this->m_connection);
-               ObjectTable objectTable(this->m_connection);
-               PermissionTable permissionTable(this->m_connection);
+               NameTable nameTable(m_connection.get());
+               ObjectTable objectTable(m_connection.get());
+               PermissionTable permissionTable(m_connection.get());
                nameTable.addRow(row.name, row.owner);
                objectTable.addRow(row);
                permissionTable.setPermission(row.name,
@@ -412,7 +403,7 @@ void Crypto::updateRow(const Row &row)
 {
        try {
                // transaction is present in the layer above
-               ObjectTable objectTable(this->m_connection);
+               ObjectTable objectTable(m_connection.get());
                objectTable.updateRow(row);
                return;
        } catch (const SqlConnection::Exception::SyntaxError &) {
@@ -430,7 +421,7 @@ bool Crypto::deleteRow(
 {
        try {
                // transaction is present in the layer above
-               NameTable nameTable(this->m_connection);
+               NameTable nameTable(m_connection.get());
 
                if (nameTable.isPresent(name, owner)) {
                        nameTable.deleteRow(name, owner);
@@ -473,7 +464,7 @@ PermissionMaskOptional Crypto::getPermissionRow(
        const ClientId &accessor) const
 {
        try {
-               PermissionTable permissionTable(this->m_connection);
+               PermissionTable permissionTable(m_connection.get());
                return permissionTable.getPermissionRow(name, owner, accessor);
        } catch (const SqlConnection::Exception::InvalidColumn &) {
                LogError("Select statement invalid column error");
@@ -679,7 +670,7 @@ void Crypto::deleteKey(const ClientId &owner)
                deleteCommand->BindString(1, owner.c_str());
                deleteCommand->Step();
 
-               NameTable nameTable(this->m_connection);
+               NameTable nameTable(m_connection.get());
                nameTable.deleteAllRows(owner);
 
                transaction.commit();
@@ -700,7 +691,7 @@ void Crypto::setPermission(
        const PermissionMask permissionMask)
 {
        try {
-               PermissionTable permissionTable(this->m_connection);
+               PermissionTable permissionTable(m_connection.get());
                permissionTable.setPermission(name, owner, accessor, permissionMask);
                return;
        } catch (const SqlConnection::Exception::SyntaxError &) {
index 4642b74..a915307 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014-2019 Samsung Electronics Co., Ltd. All rights reserved
+ * Copyright (c) 2014-2020 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.
@@ -43,7 +43,7 @@ public:
        using RowOptional = boost::optional<Row>;
        using RawBufferOptional = boost::optional<RawBuffer>;
 
-       Crypto() : m_connection(nullptr), m_inUserTransaction(false) {}
+       Crypto() : m_inUserTransaction(false) {}
 
        // user name instead of path?
        Crypto(const std::string &path, const RawBuffer &rawPass);
@@ -53,7 +53,7 @@ public:
        Crypto &operator=(const Crypto &) = delete;
        Crypto &operator=(Crypto &&other);
 
-       virtual ~Crypto();
+       virtual ~Crypto() = default;
 
        void saveRow(
                const Row &row);
@@ -198,7 +198,7 @@ public:
        };
 
 protected:
-       SqlConnection *m_connection;
+       std::unique_ptr<SqlConnection> m_connection;
 
        Row getRow(
                const SqlConnection::DataCommandUniquePtr &selectCommand) const;