first thoughts about database
This commit is contained in:
parent
1b9d5e4a7b
commit
03f38387e2
14 changed files with 255 additions and 20 deletions
11
database/CMakeLists.txt
Normal file
11
database/CMakeLists.txt
Normal file
|
@ -0,0 +1,11 @@
|
|||
set(HEADERS
|
||||
dbinterface.h
|
||||
)
|
||||
|
||||
set(SOURCES
|
||||
dbinterface.cpp
|
||||
)
|
||||
|
||||
target_sources(pica PRIVATE ${SOURCES})
|
||||
|
||||
add_subdirectory(mysql)
|
23
database/dbinterface.cpp
Normal file
23
database/dbinterface.cpp
Normal file
|
@ -0,0 +1,23 @@
|
|||
#include "dbinterface.h"
|
||||
|
||||
#include "mysql/mysql.h"
|
||||
|
||||
DBInterface::DBInterface(Type type):
|
||||
type(type),
|
||||
state(State::disconnected)
|
||||
{}
|
||||
|
||||
DBInterface::~DBInterface() {}
|
||||
|
||||
std::unique_ptr<DBInterface> DBInterface::create(Type type) {
|
||||
switch (type) {
|
||||
case Type::mysql:
|
||||
return std::make_unique<MySQL>();
|
||||
}
|
||||
|
||||
throw std::runtime_error("Unexpected database type: " + std::to_string((uint8_t)type));
|
||||
}
|
||||
|
||||
DBInterface::State DBInterface::currentState() const {
|
||||
return state;
|
||||
}
|
36
database/dbinterface.h
Normal file
36
database/dbinterface.h
Normal file
|
@ -0,0 +1,36 @@
|
|||
#pragma once
|
||||
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <memory>
|
||||
|
||||
class DBInterface {
|
||||
public:
|
||||
enum class Type {
|
||||
mysql
|
||||
};
|
||||
enum class State {
|
||||
disconnected,
|
||||
connecting,
|
||||
connected
|
||||
};
|
||||
static std::unique_ptr<DBInterface> create(Type type);
|
||||
|
||||
virtual ~DBInterface();
|
||||
|
||||
State currentState() const;
|
||||
|
||||
const Type type;
|
||||
|
||||
public:
|
||||
virtual void connect(const std::string& path) = 0;
|
||||
virtual void disconnect() = 0;
|
||||
virtual void setDatabase(const std::string& newDatabase) = 0;
|
||||
virtual void setCredentials(const std::string& login, const std::string& password) = 0;
|
||||
|
||||
protected:
|
||||
DBInterface(Type type);
|
||||
|
||||
protected:
|
||||
State state;
|
||||
};
|
13
database/mysql/CMakeLists.txt
Normal file
13
database/mysql/CMakeLists.txt
Normal file
|
@ -0,0 +1,13 @@
|
|||
set(HEADERS
|
||||
mysql.h
|
||||
)
|
||||
|
||||
set(SOURCES
|
||||
mysql.cpp
|
||||
)
|
||||
|
||||
find_package(MariaDB REQUIRED)
|
||||
|
||||
target_sources(pica PRIVATE ${SOURCES})
|
||||
|
||||
target_link_libraries(pica PRIVATE MariaDB::client)
|
85
database/mysql/mysql.cpp
Normal file
85
database/mysql/mysql.cpp
Normal file
|
@ -0,0 +1,85 @@
|
|||
#include "mysql.h"
|
||||
|
||||
MySQL::MySQL():
|
||||
DBInterface(Type::mysql),
|
||||
connection(),
|
||||
login(),
|
||||
password(),
|
||||
database()
|
||||
{
|
||||
mysql_init(&connection);
|
||||
}
|
||||
|
||||
MySQL::~MySQL() {
|
||||
mysql_close(&connection);
|
||||
}
|
||||
|
||||
|
||||
void MySQL::connect(const std::string& path) {
|
||||
if (state != State::disconnected)
|
||||
return;
|
||||
|
||||
MYSQL* con = &connection;
|
||||
MYSQL* res = mysql_real_connect(
|
||||
con,
|
||||
NULL,
|
||||
login.c_str(),
|
||||
password.c_str(),
|
||||
database.empty() ? NULL : database.c_str(),
|
||||
0,
|
||||
path.c_str(),
|
||||
0
|
||||
);
|
||||
|
||||
if (res != con)
|
||||
throw std::runtime_error(std::string("Error changing connecting: ") + mysql_error(con));
|
||||
|
||||
state = State::connected;
|
||||
}
|
||||
|
||||
void MySQL::setCredentials(const std::string& login, const std::string& password) {
|
||||
if (MySQL::login == login && MySQL::password == password)
|
||||
return;
|
||||
|
||||
MySQL::login = login;
|
||||
MySQL::password = password;
|
||||
|
||||
if (state == State::disconnected)
|
||||
return;
|
||||
|
||||
MYSQL* con = &connection;
|
||||
int result = mysql_change_user(
|
||||
con,
|
||||
login.c_str(),
|
||||
password.c_str(),
|
||||
database.empty() ? NULL : database.c_str()
|
||||
);
|
||||
|
||||
if (result != 0)
|
||||
throw std::runtime_error(std::string("Error changing credetials: ") + mysql_error(con));
|
||||
}
|
||||
|
||||
void MySQL::setDatabase(const std::string& database) {
|
||||
if (MySQL::database == database)
|
||||
return;
|
||||
|
||||
MySQL::database = database;
|
||||
|
||||
if (state == State::disconnected)
|
||||
return;
|
||||
|
||||
MYSQL* con = &connection;
|
||||
int result = mysql_select_db(con, database.c_str());
|
||||
|
||||
if (result != 0)
|
||||
throw std::runtime_error(std::string("Error changing db: ") + mysql_error(con));
|
||||
}
|
||||
|
||||
void MySQL::disconnect() {
|
||||
if (state == State::disconnected)
|
||||
return;
|
||||
|
||||
MYSQL* con = &connection;
|
||||
mysql_close(con);
|
||||
mysql_init(con); //this is ridiculous!
|
||||
}
|
24
database/mysql/mysql.h
Normal file
24
database/mysql/mysql.h
Normal file
|
@ -0,0 +1,24 @@
|
|||
#pragma once
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
#include <mysql.h>
|
||||
|
||||
#include "database/dbinterface.h"
|
||||
|
||||
class MySQL : public DBInterface {
|
||||
public:
|
||||
MySQL();
|
||||
~MySQL() override;
|
||||
|
||||
void connect(const std::string& path) override;
|
||||
void disconnect() override;
|
||||
void setCredentials(const std::string& login, const std::string& password) override;
|
||||
void setDatabase(const std::string& database) override;
|
||||
|
||||
protected:
|
||||
MYSQL connection;
|
||||
std::string login;
|
||||
std::string password;
|
||||
std::string database;
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue