2022-09-09 17:15:40 +00:00
|
|
|
// Squawk messenger.
|
|
|
|
// Copyright (C) 2019 Yury Gubich <blue@macaw.me>
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
#include "exceptions.h"
|
|
|
|
|
2022-09-15 21:34:39 +00:00
|
|
|
DataBase::Exception::Exception():
|
|
|
|
std::exception()
|
|
|
|
{}
|
|
|
|
|
|
|
|
DataBase::Exception::~Exception() {}
|
|
|
|
|
|
|
|
const char* DataBase::Exception::what() const noexcept( true )
|
|
|
|
{
|
|
|
|
std::string* msg = new std::string(getMessage());
|
|
|
|
return msg->c_str();
|
|
|
|
}
|
|
|
|
|
2022-09-14 22:18:31 +00:00
|
|
|
DataBase::Directory::Directory(const std::string& p_path):
|
2022-09-09 17:15:40 +00:00
|
|
|
Exception(),
|
|
|
|
path(p_path) {}
|
|
|
|
|
2022-09-14 22:18:31 +00:00
|
|
|
std::string DataBase::Directory::getMessage() const {
|
2022-09-09 17:15:40 +00:00
|
|
|
return "Can't create directory for database at " + path;}
|
|
|
|
|
2022-09-14 22:18:31 +00:00
|
|
|
DataBase::Closed::Closed(
|
2022-09-09 17:15:40 +00:00
|
|
|
const std::string& p_operation,
|
|
|
|
const std::string& p_dbName,
|
|
|
|
const std::string& p_tableName
|
|
|
|
):
|
|
|
|
Exception(),
|
|
|
|
operation(p_operation),
|
|
|
|
dbName(p_dbName),
|
|
|
|
tableName(p_tableName) {}
|
|
|
|
|
2022-09-14 22:18:31 +00:00
|
|
|
std::string DataBase::Closed::getMessage() const {
|
2022-09-09 17:15:40 +00:00
|
|
|
return "An attempt to perform operation " + operation
|
|
|
|
+ " on closed database " + dbName
|
|
|
|
+ " on table " + tableName;
|
|
|
|
}
|
|
|
|
|
2022-09-14 22:18:31 +00:00
|
|
|
DataBase::Opened::Opened(const std::string& p_dbName, const std::string& p_tableName):
|
2022-09-09 17:15:40 +00:00
|
|
|
Exception(),
|
|
|
|
dbName(p_dbName),
|
|
|
|
tableName(p_tableName) {}
|
|
|
|
|
|
|
|
|
2022-09-14 22:18:31 +00:00
|
|
|
std::string DataBase::Opened::getMessage() const {
|
2022-09-09 17:15:40 +00:00
|
|
|
return "An attempt to add table " + tableName
|
|
|
|
+ " to the database " + dbName
|
|
|
|
+ " but it's can't be done because the DataBase is already opened."
|
|
|
|
+ " Please add all tables before opening DataBase.";
|
|
|
|
}
|
|
|
|
|
2022-09-14 22:18:31 +00:00
|
|
|
DataBase::NotFound::NotFound(
|
2022-09-09 17:15:40 +00:00
|
|
|
const std::string& p_key,
|
|
|
|
const std::string& p_dbName,
|
|
|
|
const std::string& p_tableName
|
|
|
|
):
|
|
|
|
Exception(),
|
|
|
|
key(p_key),
|
|
|
|
dbName(p_dbName),
|
|
|
|
tableName(p_tableName) {}
|
|
|
|
|
2022-09-14 22:18:31 +00:00
|
|
|
std::string DataBase::NotFound::getMessage() const {
|
2022-09-09 17:15:40 +00:00
|
|
|
return "Element for id " + key + " wasn't found "
|
|
|
|
+ " in database " + dbName
|
|
|
|
+ " in table " + tableName;}
|
|
|
|
|
2022-09-14 22:18:31 +00:00
|
|
|
DataBase::Exist::Exist(
|
2022-09-09 17:15:40 +00:00
|
|
|
const std::string& p_key,
|
|
|
|
const std::string& p_dbName,
|
|
|
|
const std::string& p_tableName
|
|
|
|
):
|
|
|
|
Exception(),
|
|
|
|
key(p_key),
|
|
|
|
dbName(p_dbName),
|
|
|
|
tableName(p_tableName) {}
|
|
|
|
|
2022-09-14 22:18:31 +00:00
|
|
|
std::string DataBase::Exist::getMessage() const {
|
2022-09-09 17:15:40 +00:00
|
|
|
return "An attempt to insert element with key " + key
|
|
|
|
+ " to database " + dbName
|
|
|
|
+ " to table " + tableName
|
|
|
|
+ " but it already has an element with given id";
|
|
|
|
}
|
|
|
|
|
2022-09-14 22:18:31 +00:00
|
|
|
DataBase::Unknown::Unknown(
|
2022-09-09 17:15:40 +00:00
|
|
|
const std::string& p_dbName,
|
|
|
|
const std::string& message,
|
|
|
|
const std::optional<std::string>& p_tableName
|
|
|
|
):
|
|
|
|
Exception(),
|
|
|
|
dbName(p_dbName),
|
|
|
|
tableName(p_tableName),
|
|
|
|
msg(message) {}
|
|
|
|
|
2022-09-14 22:18:31 +00:00
|
|
|
std::string DataBase::Unknown::getMessage() const
|
2022-09-09 17:15:40 +00:00
|
|
|
{
|
|
|
|
std::string result = "Unknown error in database " + dbName;
|
|
|
|
if (tableName.has_value()) {
|
|
|
|
result += " in table " + tableName.value();
|
|
|
|
}
|
|
|
|
result += ": " + msg;
|
|
|
|
return result;
|
|
|
|
}
|