add: db ! not connect
This commit is contained in:
parent
e1c8ce23c5
commit
28b55a0e21
@ -13,6 +13,7 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Werror -O2")
|
|||||||
|
|
||||||
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets Core)
|
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets Core)
|
||||||
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets Core)
|
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets Core)
|
||||||
|
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Sql)
|
||||||
|
|
||||||
set(MAIN_SOURCES
|
set(MAIN_SOURCES
|
||||||
main.cpp
|
main.cpp
|
||||||
@ -37,6 +38,9 @@ set(MAIN_SOURCES
|
|||||||
view/ui/printer_settings.ui
|
view/ui/printer_settings.ui
|
||||||
view/ui/updates.ui
|
view/ui/updates.ui
|
||||||
view/ui/add_preset.ui
|
view/ui/add_preset.ui
|
||||||
|
|
||||||
|
logic/printer_db.cpp
|
||||||
|
logic/printer_db.h
|
||||||
)
|
)
|
||||||
|
|
||||||
if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
|
if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
|
||||||
@ -50,13 +54,20 @@ else()
|
|||||||
)
|
)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
qt_add_resources(Calc3D "app_resources"
|
# qt_add_resources(Calc3D "app_resources"
|
||||||
PREFIX "/"
|
# PREFIX "/"
|
||||||
FILES
|
# FILES
|
||||||
view/resources.qrc
|
# view/resources.qrc
|
||||||
)
|
# )
|
||||||
|
|
||||||
target_link_libraries(Calc3D PRIVATE Qt${QT_VERSION_MAJOR}::Widgets Qt${QT_VERSION_MAJOR}::Core)
|
target_link_libraries(Calc3D PRIVATE Qt${QT_VERSION_MAJOR}::Widgets Qt${QT_VERSION_MAJOR}::Core Qt${QT_VERSION_MAJOR}::Sql)
|
||||||
|
|
||||||
|
# путь к бд в проекте, опционально
|
||||||
|
# configure_file(
|
||||||
|
# ${CMAKE_CURRENT_SOURCE_DIR}/printers.db
|
||||||
|
# ${CMAKE_CURRENT_BINARY_DIR}/printers.db
|
||||||
|
# COPYONLY
|
||||||
|
# )
|
||||||
|
|
||||||
set_target_properties(Calc3D PROPERTIES
|
set_target_properties(Calc3D PROPERTIES
|
||||||
MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com
|
MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com
|
||||||
|
@ -1,23 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#include <memory>
|
|
||||||
|
|
||||||
#include "../model/facade.h"
|
|
||||||
|
|
||||||
class Controller {
|
|
||||||
public:
|
|
||||||
Controller(const Controller &other) = delete;
|
|
||||||
Controller(Controller &&other) = delete;
|
|
||||||
void operator=(const Controller &other) = delete;
|
|
||||||
~Controller() = default;
|
|
||||||
|
|
||||||
static std::shared_ptr<Controller> GetInstance() {
|
|
||||||
static auto instance = std::shared_ptr<Controller>(new Controller);
|
|
||||||
return instance;
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
std::shared_ptr<Facade> facade;
|
|
||||||
|
|
||||||
Controller() { facade_ = s21::Facade::GetInstance(); }
|
|
||||||
};
|
|
48
src/logic/facade.h
Normal file
48
src/logic/facade.h
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
#include "printer_db.h"
|
||||||
|
|
||||||
|
class Facade {
|
||||||
|
public:
|
||||||
|
Facade(const Facade &other) = delete;
|
||||||
|
Facade(Facade &&other) = delete;
|
||||||
|
void operator=(const Facade &other) = delete;
|
||||||
|
~Facade() = default;
|
||||||
|
|
||||||
|
static std::shared_ptr<Facade> GetInstance() {
|
||||||
|
static auto instance = std::shared_ptr<Facade>(new Facade);
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool addPrinter(const QString &name, double power, int age, double cost) {
|
||||||
|
return db.addPrinter(name, power, age, cost);
|
||||||
|
}
|
||||||
|
|
||||||
|
QList<QString> getPrinterList() { return db.getPrinterList(); }
|
||||||
|
|
||||||
|
bool deletePrinterByName(const QString &name) {
|
||||||
|
return db.deletePrinterByName(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Database::updatePrinter(const QString &name,
|
||||||
|
const QHash<QString, QVariant> &updates) {
|
||||||
|
return db.updatePrinter(name, updates);
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
Database db;
|
||||||
|
|
||||||
|
Facade() {
|
||||||
|
//! TODO create if doesn't exist
|
||||||
|
QString dbPath =
|
||||||
|
QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) +
|
||||||
|
"/printers.db"; //! TODO name!
|
||||||
|
db(dbPath);
|
||||||
|
if (!db.init()) {
|
||||||
|
qFatal("Database initialization failed!");
|
||||||
|
// TODO make exc
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
83
src/logic/printer_db.cpp
Normal file
83
src/logic/printer_db.cpp
Normal file
@ -0,0 +1,83 @@
|
|||||||
|
#include "printer_db.h"
|
||||||
|
|
||||||
|
Database::Database(const QString &path) {
|
||||||
|
db = QSqlDatabase::addDatabase("QSQLITE");
|
||||||
|
db.setDatabaseName(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Database::init() {
|
||||||
|
if (!db.open()) {
|
||||||
|
qWarning() << "Error opening database:" << db.lastError();
|
||||||
|
// TODO return ERROR
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
QSqlQuery query;
|
||||||
|
return query.exec(
|
||||||
|
"CREATE TABLE IF NOT EXISTS printers ("
|
||||||
|
"id INTEGER PRIMARY KEY AUTOINCREMENT,"
|
||||||
|
"name TEXT NOT NULL UNIQUE,"
|
||||||
|
"power REAL NOT NULL," //! int??
|
||||||
|
"age INTEGER NOT NULL,"
|
||||||
|
"cost REAL NOT NULL");
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Database::addPrinter(const QString &name, double power, int age,
|
||||||
|
double cost) {
|
||||||
|
QSqlQuery query;
|
||||||
|
query.prepare(
|
||||||
|
"INSERT INTO printers (name, power, age, cost) VALUES (:name, :power, "
|
||||||
|
":age, :cost)");
|
||||||
|
query.bindValue(":name", name);
|
||||||
|
query.bindValue(":power", power);
|
||||||
|
query.bindValue(":age", age);
|
||||||
|
query.bindValue(":cost", cost);
|
||||||
|
return query.exec();
|
||||||
|
}
|
||||||
|
|
||||||
|
QList<QString> Database::getPrinterList() {
|
||||||
|
QList<QString> printers;
|
||||||
|
QSqlQuery query("SELECT name FROM printers");
|
||||||
|
while (query.next()) {
|
||||||
|
printers.append({query.value(0).toString()});
|
||||||
|
}
|
||||||
|
return printers;
|
||||||
|
}
|
||||||
|
bool Database::deletePrinterByName(const QString &name) {
|
||||||
|
QSqlQuery query;
|
||||||
|
query.prepare("DELETE FROM printers WHERE name = :name");
|
||||||
|
query.bindValue(":name", name);
|
||||||
|
|
||||||
|
if (!query.exec()) {
|
||||||
|
// TODO exc
|
||||||
|
qWarning() << "Delete failed:" << query.lastError().text();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// была ли удалена хотя бы одна запись?
|
||||||
|
return query.numRowsAffected() > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Database::updatePrinter(const QString &name,
|
||||||
|
const QHash<QString, QVariant> &updates) {
|
||||||
|
QStringList setClauses;
|
||||||
|
QSqlQuery query;
|
||||||
|
|
||||||
|
for (auto it = updates.begin(); it != updates.end(); ++it) {
|
||||||
|
setClauses << QString("%1 = :%2").arg(it.key(), it.key());
|
||||||
|
query.bindValue(":" + it.key(), it.value());
|
||||||
|
}
|
||||||
|
|
||||||
|
query.prepare("UPDATE printers SET " + setClauses.join(", ") +
|
||||||
|
" WHERE name = :name");
|
||||||
|
query.bindValue(":name", name);
|
||||||
|
|
||||||
|
return query.exec();
|
||||||
|
|
||||||
|
//! Как создать новое из фасада:
|
||||||
|
// !проще забрать сразу все
|
||||||
|
// QHash<QString, QVariant> updates;
|
||||||
|
// updates["power"] = 350.0;
|
||||||
|
// updates["cost"] = 27000.0;
|
||||||
|
// db->updatePrinterPartial("MyPrinter", updates);
|
||||||
|
}
|
18
src/logic/printer_db.h
Normal file
18
src/logic/printer_db.h
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
#include <QDebug>
|
||||||
|
#include <QSqlDatabase>
|
||||||
|
#include <QSqlError>
|
||||||
|
#include <QSqlQuery>
|
||||||
|
|
||||||
|
class Database {
|
||||||
|
public:
|
||||||
|
Database(const QString &path);
|
||||||
|
bool init();
|
||||||
|
bool addPrinter(const QString &name, double power, int age, double cost);
|
||||||
|
bool deletePrinterByName(const QString &name);
|
||||||
|
QList<QString> getPrinterList();
|
||||||
|
bool updatePrinter(const QString &name,
|
||||||
|
const QHash<QString, QVariant> &updates);
|
||||||
|
|
||||||
|
private:
|
||||||
|
QSqlDatabase db;
|
||||||
|
};
|
@ -1,19 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#include <memory>
|
|
||||||
|
|
||||||
class Facade {
|
|
||||||
public:
|
|
||||||
Facade(const Facade &other) = delete;
|
|
||||||
Facade(Facade &&other) = delete;
|
|
||||||
void operator=(const Facade &other) = delete;
|
|
||||||
~Facade() = default;
|
|
||||||
|
|
||||||
static std::shared_ptr<Facade> GetInstance() {
|
|
||||||
static auto instance = std::shared_ptr<Facade>(new Facade);
|
|
||||||
return instance;
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
Facade() {}
|
|
||||||
};
|
|
Loading…
x
Reference in New Issue
Block a user