-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
842 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
cmake_minimum_required(VERSION 3.5) | ||
|
||
project(08_PreDataBase VERSION 0.1 LANGUAGES CXX) | ||
|
||
set(CMAKE_AUTOUIC ON) | ||
set(CMAKE_AUTOMOC ON) | ||
set(CMAKE_AUTORCC ON) | ||
|
||
set(CMAKE_CXX_STANDARD 17) | ||
set(CMAKE_CXX_STANDARD_REQUIRED ON) | ||
|
||
find_package(Qt6 REQUIRED COMPONENTS Widgets) | ||
|
||
|
||
set(PROJECT_SOURCES | ||
main.cpp | ||
mainwindow.cpp | ||
mainwindow.h | ||
mainwindow.ui | ||
database.h | ||
database.cpp | ||
dbdata.h | ||
dbdata.cpp | ||
dbdata.ui | ||
) | ||
|
||
add_executable(08_PreDataBase | ||
${PROJECT_SOURCES} | ||
) | ||
|
||
|
||
target_link_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/PG_Libs) | ||
target_link_libraries(08_PreDataBase PRIVATE Qt6::Widgets | ||
libcrypto-3-x64 | ||
libiconv-2 | ||
libintl-9 | ||
libpq | ||
libssl-3-x64) | ||
|
||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
#include "database.h" | ||
|
||
DataBase::DataBase(QObject *parent) | ||
: QObject{parent} | ||
{ | ||
|
||
dataBase = new QSqlDatabase(); | ||
|
||
|
||
} | ||
|
||
DataBase::~DataBase() | ||
{ | ||
delete dataBase; | ||
} | ||
|
||
/*! | ||
* \brief Метод добавляет БД к экземпляру класса QSqlDataBase | ||
* \param driver драйвер БД | ||
* \param nameDB имя БД (Если отсутствует Qt задает имя по умолчанию) | ||
*/ | ||
void DataBase::AddDataBase(QString driver, QString nameDB) | ||
{ | ||
|
||
*dataBase = QSqlDatabase::addDatabase(driver, nameDB); | ||
|
||
} | ||
|
||
/*! | ||
* \brief Метод подключается к БД | ||
* \param для удобства передаем контейнер с данными необходимыми для подключения | ||
* \return возвращает тип ошибки | ||
*/ | ||
void DataBase::ConnectToDataBase(QVector<QString> data) | ||
{ | ||
|
||
dataBase->setHostName(data[hostName]); | ||
dataBase->setDatabaseName(data[dbName]); | ||
dataBase->setUserName(data[login]); | ||
dataBase->setPassword(data[pass]); | ||
dataBase->setPort(data[port].toInt()); | ||
|
||
|
||
///Тут должен быть код ДЗ | ||
|
||
|
||
bool status; | ||
status = dataBase->open( ); | ||
emit sig_SendStatusConnection(status); | ||
|
||
} | ||
/*! | ||
* \brief Метод производит отключение от БД | ||
* \param Имя БД | ||
*/ | ||
void DataBase::DisconnectFromDataBase(QString nameDb) | ||
{ | ||
|
||
*dataBase = QSqlDatabase::database(nameDb); | ||
dataBase->close(); | ||
|
||
} | ||
/*! | ||
* \brief Метод формирует запрос к БД. | ||
* \param request - SQL запрос | ||
* \return | ||
*/ | ||
void DataBase::RequestToDB(QString request) | ||
{ | ||
|
||
///Тут должен быть код ДЗ | ||
|
||
} | ||
|
||
/*! | ||
* @brief Метод возвращает последнюю ошибку БД | ||
*/ | ||
QSqlError DataBase::GetLastError() | ||
{ | ||
return dataBase->lastError(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#ifndef DATABASE_H | ||
#define DATABASE_H | ||
|
||
#include <QTableWidget> | ||
#include <QObject> | ||
|
||
|
||
|
||
#define POSTGRE_DRIVER "QPSQL" | ||
#define DB_NAME "MyDB" | ||
|
||
//Количество полей данных необходимых для подключения к БД | ||
#define NUM_DATA_FOR_CONNECT_TO_DB 5 | ||
|
||
//Перечисление полей данных | ||
enum fieldsForConnect{ | ||
hostName = 0, | ||
dbName = 1, | ||
login = 2, | ||
pass = 3, | ||
port = 4 | ||
}; | ||
|
||
//Типы запросов | ||
enum requestType{ | ||
|
||
requestAllFilms = 1, | ||
requestComedy = 2, | ||
requestHorrors = 3 | ||
|
||
}; | ||
|
||
|
||
|
||
class DataBase : public QObject | ||
{ | ||
Q_OBJECT | ||
|
||
public: | ||
explicit DataBase(QObject *parent = nullptr); | ||
~DataBase(); | ||
|
||
void AddDataBase(QString driver, QString nameDB = ""); | ||
void DisconnectFromDataBase(QString nameDb = ""); | ||
void RequestToDB(QString request); | ||
QSqlError GetLastError(void); | ||
void ConnectToDataBase(QVector<QString> dataForConnect); | ||
|
||
|
||
signals: | ||
|
||
void sig_SendDataFromDB(const QTableWidget *tableWg, int typeR); | ||
void sig_SendStatusConnection(bool); | ||
|
||
|
||
|
||
private: | ||
|
||
QSqlDatabase* dataBase; | ||
|
||
|
||
}; | ||
|
||
#endif // DATABASE_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#include "dbdata.h" | ||
#include "database.h" | ||
#include "ui_dbdata.h" | ||
|
||
DbData::DbData(QWidget *parent) : | ||
QDialog(parent), | ||
ui(new Ui::DbData) | ||
{ | ||
ui->setupUi(this); | ||
|
||
|
||
//Ресайзим вектор значений, по количеству полей необходимых для | ||
//подключения к БД | ||
data.resize(NUM_DATA_FOR_CONNECT_TO_DB); | ||
|
||
} | ||
|
||
DbData::~DbData() | ||
{ | ||
delete ui; | ||
} | ||
/*! | ||
* \brief Обработчик кнопки "Ок" | ||
*/ | ||
void DbData::on_buttonBox_accepted() | ||
{ | ||
|
||
//Добавляем данные в контейнер и передаем в главное окно | ||
data[hostName] = ui->le_host->text(); | ||
data[dbName] = ui->le_dbName->text(); | ||
data[login] = ui->le_login->text(); | ||
data[pass] = ui->le_pass->text(); | ||
data[port] = ui->spB_port->text(); | ||
|
||
emit sig_sendData(data); | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#ifndef DBDATA_H | ||
#define DBDATA_H | ||
|
||
#include <QDialog> | ||
|
||
namespace Ui { | ||
class DbData; | ||
} | ||
|
||
class DbData : public QDialog | ||
{ | ||
Q_OBJECT | ||
|
||
public: | ||
explicit DbData(QWidget *parent = nullptr); | ||
~DbData(); | ||
|
||
|
||
signals: | ||
void sig_sendData(QVector<QString> dbData); | ||
|
||
|
||
private slots: | ||
void on_buttonBox_accepted(); | ||
|
||
private: | ||
Ui::DbData *ui; | ||
QVector<QString> data; | ||
|
||
}; | ||
|
||
#endif // DBDATA_H |
Oops, something went wrong.