Compare commits
1 Commits
master
...
00332b5b0e
| Author | SHA1 | Date | |
|---|---|---|---|
| 00332b5b0e |
@@ -15,7 +15,6 @@ Project {
|
||||
|
||||
"src/cpp-opds.qbs",
|
||||
"src/database/database.qbs",
|
||||
"src/model/model.qbs",
|
||||
"src/restapi/restapi.qbs",
|
||||
]
|
||||
}
|
||||
|
||||
@@ -21,7 +21,6 @@ PSApplication {
|
||||
Depends { name: "restapi" }
|
||||
Depends { name: "redkit_gen" }
|
||||
Depends { name: "rdbase" }
|
||||
Depends { name: "model" }
|
||||
|
||||
cpp.cxxLanguageVersion: "c++20"
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
#ifndef AUTHOR_S_H
|
||||
#define AUTHOR_S_H
|
||||
|
||||
#include "model_global.h"
|
||||
#include "database_global.h"
|
||||
|
||||
#include <QString>
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
#include <odb/query.hxx>
|
||||
|
||||
#pragma db object
|
||||
class MODEL_EXPORT Author_S
|
||||
class DATABASE_EXPORT Author_S
|
||||
{
|
||||
public:
|
||||
Author_S() = default;
|
||||
@@ -45,7 +45,7 @@ private:
|
||||
|
||||
QString m_firstName;
|
||||
QString m_lastName;
|
||||
quint64 m_age;
|
||||
quint8 m_age;
|
||||
};
|
||||
|
||||
#pragma db view object(Author_S)
|
||||
@@ -4,7 +4,7 @@
|
||||
#ifndef BOOK_S_H
|
||||
#define BOOK_S_H
|
||||
|
||||
#include "model_global.h"
|
||||
#include "database_global.h"
|
||||
|
||||
#include <QSharedPointer>
|
||||
#include <QString>
|
||||
@@ -14,7 +14,7 @@
|
||||
#include "author_s.h"
|
||||
|
||||
#pragma db object
|
||||
class MODEL_EXPORT Book_S
|
||||
class DATABASE_EXPORT Book_S
|
||||
{
|
||||
public:
|
||||
Book_S() = default;
|
||||
@@ -13,9 +13,87 @@
|
||||
#include <memory> // std::unique_ptr
|
||||
#include <string>
|
||||
|
||||
#include <filesystem>
|
||||
#include <odb/database.hxx>
|
||||
|
||||
#include "database_utils.h"
|
||||
#if defined(DATABASE_MYSQL)
|
||||
#include <odb/mysql/database.hxx>
|
||||
#elif defined(DATABASE_SQLITE)
|
||||
#include <odb/connection.hxx>
|
||||
#include <odb/schema-catalog.hxx>
|
||||
#include <odb/sqlite/database.hxx>
|
||||
#include <odb/transaction.hxx>
|
||||
#elif defined(DATABASE_PGSQL)
|
||||
#include <odb/pgsql/database.hxx>
|
||||
#elif defined(DATABASE_ORACLE)
|
||||
#include <odb/oracle/database.hxx>
|
||||
#elif defined(DATABASE_MSSQL)
|
||||
#include <odb/mssql/database.hxx>
|
||||
#else
|
||||
#error unknown database; did you forget to define the DATABASE_* macros?
|
||||
#endif
|
||||
|
||||
#include <filesystem>
|
||||
#include <random>
|
||||
#include <rcore/smarttypes.h>
|
||||
|
||||
using oDBase = odb::sqlite::database;
|
||||
using uDBase = U<oDBase>;
|
||||
|
||||
struct testData
|
||||
{
|
||||
QString first;
|
||||
QString last;
|
||||
int age;
|
||||
};
|
||||
|
||||
// Функция для генерации случайной строки (имени или фамилии)
|
||||
QString generate_random_string(const QVector<QString>& pool)
|
||||
{
|
||||
static std::random_device rd;
|
||||
static std::mt19937 gen(rd());
|
||||
std::uniform_int_distribution<> dis(0, pool.size() - 1);
|
||||
return pool[dis(gen)];
|
||||
}
|
||||
|
||||
// Функция для генерации случайного года
|
||||
int generate_random_year(int min_year = 1900, int max_year = 2020)
|
||||
{
|
||||
static std::random_device rd;
|
||||
static std::mt19937 gen(rd());
|
||||
std::uniform_int_distribution<> dis(min_year, max_year);
|
||||
return dis(gen);
|
||||
}
|
||||
|
||||
QVector<testData> fillDB()
|
||||
{
|
||||
QVector<QString> first_names = {
|
||||
"John", "Jane", "Alex", "Chris", "Robert", "Emily", "James", "Linda", "David", "Sarah",
|
||||
"Michael", "Elizabeth", "Daniel", "Samantha", "William", "Olivia", "Ethan", "Sophia", "Joshua", "Charlotte",
|
||||
"Daniel", "Grace", "Benjamin", "Isabella", "Matthew", "Victoria", "Henry", "Abigail", "Samuel", "Megan",
|
||||
"Lucas", "Lily", "Andrew", "Madison", "Jackson", "Chloe", "Aiden", "Amelia", "Thomas", "Natalie",
|
||||
"Ryan", "Zoe", "Jack", "Harper", "Elijah", "Ava", "Isaac", "Mia", "Caleb", "Ella"
|
||||
};
|
||||
QVector<QString> last_names = {
|
||||
"Doe", "Smith", "Johnson", "Williams", "Jones", "Brown", "Davis", "Miller", "Wilson", "Moore",
|
||||
"Taylor", "Anderson", "Thomas", "Jackson", "White", "Harris", "Martin", "Thompson", "Garcia", "Martinez",
|
||||
"Roberts", "Clark", "Lewis", "Walker", "Young", "Allen", "King", "Wright", "Scott", "Adams",
|
||||
"Baker", "Gonzalez", "Nelson", "Carter", "Mitchell", "Perez", "Robinson", "Hughes", "Flores", "Cook",
|
||||
"Rogers", "Gutierrez", "Ramirez", "Diaz", "Perez", "Ross", "Sanders", "Price", "Howard", "Cooper"
|
||||
};
|
||||
|
||||
QVector<testData> vecTest;
|
||||
|
||||
for (int i = 0; i < 50; ++i)
|
||||
{
|
||||
QString first_name = generate_random_string(first_names);
|
||||
QString last_name = generate_random_string(last_names);
|
||||
int birth_year = generate_random_year(1900, 2000);
|
||||
|
||||
vecTest.push_back({ first_name, last_name, birth_year });
|
||||
}
|
||||
|
||||
return vecTest;
|
||||
}
|
||||
|
||||
inline uDBase openDB(const std::string path_db)
|
||||
{
|
||||
|
||||
@@ -13,22 +13,37 @@ PSLibrary {
|
||||
]
|
||||
consoleApplication: true
|
||||
|
||||
Depends { name: "Qt"; submodules: [ "core", "network" ] }
|
||||
Depends { name: "Qt"; submodules: [ "core", "sql", "network" ] }
|
||||
Depends { name: "cpp" }
|
||||
|
||||
Depends { name: "odb.gen" }
|
||||
Depends { name: "rdbase" }
|
||||
|
||||
Depends { name: "redkit_gen" }
|
||||
redkit_gen.includeModules: ["JsonSerializer"]
|
||||
|
||||
odb.gen.databases: "sqlite"
|
||||
cpp.cxxLanguageVersion: "c++17"
|
||||
|
||||
Group {
|
||||
name: "cpp"
|
||||
files: [
|
||||
"**/*.h",
|
||||
"**/*.hxx",
|
||||
"**/*.cpp",
|
||||
"databasemanager.*",
|
||||
"*.cpp",
|
||||
"*.h",
|
||||
"*.hxx",
|
||||
]
|
||||
excludeFiles: odbs.files
|
||||
}
|
||||
|
||||
Group {
|
||||
id: odbs
|
||||
name: "odb"
|
||||
files: [
|
||||
"author_s.h",
|
||||
"book_s.h",
|
||||
]
|
||||
fileTags: ["hpp", "odbxx"]
|
||||
}
|
||||
|
||||
cpp.dynamicLibraries: [
|
||||
|
||||
@@ -1,57 +0,0 @@
|
||||
#include "database_utils.h"
|
||||
|
||||
#include <random>
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
// Функция для генерации случайной строки (имени или фамилии)
|
||||
QString generate_random_string(const QVector<QString>& pool)
|
||||
{
|
||||
static std::random_device rd;
|
||||
static std::mt19937 gen(rd());
|
||||
std::uniform_int_distribution<> dis(0, pool.size() - 1);
|
||||
return pool[dis(gen)];
|
||||
}
|
||||
|
||||
// Функция для генерации случайного года
|
||||
int generate_random_year(int min_year = 1900, int max_year = 2020)
|
||||
{
|
||||
static std::random_device rd;
|
||||
static std::mt19937 gen(rd());
|
||||
std::uniform_int_distribution<> dis(min_year, max_year);
|
||||
return dis(gen);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
QVector<testData> fillAuthorDB()
|
||||
{
|
||||
QVector<QString> first_names = {
|
||||
"John", "Jane", "Alex", "Chris", "Robert", "Emily", "James", "Linda", "David", "Sarah",
|
||||
"Michael", "Elizabeth", "Daniel", "Samantha", "William", "Olivia", "Ethan", "Sophia", "Joshua", "Charlotte",
|
||||
"Daniel", "Grace", "Benjamin", "Isabella", "Matthew", "Victoria", "Henry", "Abigail", "Samuel", "Megan",
|
||||
"Lucas", "Lily", "Andrew", "Madison", "Jackson", "Chloe", "Aiden", "Amelia", "Thomas", "Natalie",
|
||||
"Ryan", "Zoe", "Jack", "Harper", "Elijah", "Ava", "Isaac", "Mia", "Caleb", "Ella"
|
||||
};
|
||||
QVector<QString> last_names = {
|
||||
"Doe", "Smith", "Johnson", "Williams", "Jones", "Brown", "Davis", "Miller", "Wilson", "Moore",
|
||||
"Taylor", "Anderson", "Thomas", "Jackson", "White", "Harris", "Martin", "Thompson", "Garcia", "Martinez",
|
||||
"Roberts", "Clark", "Lewis", "Walker", "Young", "Allen", "King", "Wright", "Scott", "Adams",
|
||||
"Baker", "Gonzalez", "Nelson", "Carter", "Mitchell", "Perez", "Robinson", "Hughes", "Flores", "Cook",
|
||||
"Rogers", "Gutierrez", "Ramirez", "Diaz", "Perez", "Ross", "Sanders", "Price", "Howard", "Cooper"
|
||||
};
|
||||
|
||||
QVector<testData> vecTest;
|
||||
|
||||
for (int i = 0; i < 50; ++i)
|
||||
{
|
||||
QString first_name = generate_random_string(first_names);
|
||||
QString last_name = generate_random_string(last_names);
|
||||
int birth_year = generate_random_year(1900, 2000);
|
||||
|
||||
vecTest.push_back({ first_name, last_name, birth_year });
|
||||
}
|
||||
|
||||
return vecTest;
|
||||
}
|
||||
@@ -1,37 +0,0 @@
|
||||
#include <QString>
|
||||
#include <QVector>
|
||||
|
||||
#include <odb/database.hxx>
|
||||
|
||||
#if defined(DATABASE_MYSQL)
|
||||
#include <odb/mysql/database.hxx>
|
||||
#elif defined(DATABASE_SQLITE)
|
||||
#include <odb/connection.hxx>
|
||||
#include <odb/schema-catalog.hxx>
|
||||
#include <odb/sqlite/database.hxx>
|
||||
#include <odb/transaction.hxx>
|
||||
#elif defined(DATABASE_PGSQL)
|
||||
#include <odb/pgsql/database.hxx>
|
||||
#elif defined(DATABASE_ORACLE)
|
||||
#include <odb/oracle/database.hxx>
|
||||
#elif defined(DATABASE_MSSQL)
|
||||
#include <odb/mssql/database.hxx>
|
||||
#else
|
||||
#error unknown database; did you forget to define the DATABASE_* macros?
|
||||
#endif
|
||||
|
||||
#include <rcore/smarttypes.h>
|
||||
|
||||
#include "database_global.h"
|
||||
|
||||
using oDBase = odb::sqlite::database;
|
||||
using uDBase = U<oDBase>;
|
||||
|
||||
struct testData
|
||||
{
|
||||
QString first;
|
||||
QString last;
|
||||
int age;
|
||||
};
|
||||
|
||||
QVector<testData> DATABASE_EXPORT fillAuthorDB();
|
||||
24
src/main.cpp
24
src/main.cpp
@@ -8,10 +8,10 @@
|
||||
#include <restapi/restapiserver.h>
|
||||
|
||||
/* Опыты с odb */
|
||||
#include <model/author_s-odb.hxx> // Должен быть здесь
|
||||
#include <model/author_s.h>
|
||||
#include <model/book_s-odb.hxx> // Должен быть здесь
|
||||
#include <model/book_s.h>
|
||||
#include <database/author_s-odb.hxx> // Должен быть здесь
|
||||
#include <database/author_s.h>
|
||||
#include <database/book_s-odb.hxx> // Должен быть здесь
|
||||
#include <database/book_s.h>
|
||||
#include <database/database.hxx> // create_database
|
||||
#include <odb/database.hxx>
|
||||
|
||||
@@ -101,7 +101,7 @@ void fillBooksBD(uDBase& db)
|
||||
{
|
||||
odb::core::transaction t(db->begin());
|
||||
for (auto& author : authors)
|
||||
db->persist(author);
|
||||
db->persist(author); // Используем get() для получения сырого указателя
|
||||
t.commit();
|
||||
}
|
||||
|
||||
@@ -129,19 +129,7 @@ int main(int argc, char* argv[])
|
||||
uDBase db(openDB(dbPath));
|
||||
|
||||
// TODO Как-то нужно выполнять лишь раз
|
||||
fillBooksBD(db);
|
||||
|
||||
// Сохранение дополнителньых авторов в базу данных
|
||||
{
|
||||
auto authors = fillAuthorDB();
|
||||
odb::core::transaction t(db->begin());
|
||||
for (auto& author : authors)
|
||||
{
|
||||
SH<Author_S> tempAuthor = SH<Author_S>::create(author.first, author.last, author.age);
|
||||
db->persist(tempAuthor);
|
||||
}
|
||||
t.commit();
|
||||
}
|
||||
// fillBooksBD(db);
|
||||
|
||||
RestApiServer server(*db);
|
||||
server.start(8080);
|
||||
|
||||
56
src/model/author.h
Normal file
56
src/model/author.h
Normal file
@@ -0,0 +1,56 @@
|
||||
#ifndef AUTHOR_H
|
||||
#define AUTHOR_H
|
||||
|
||||
#include <sql_builder/create_table.h>
|
||||
|
||||
#include <QString>
|
||||
|
||||
namespace model
|
||||
{
|
||||
|
||||
namespace authorconst
|
||||
{
|
||||
|
||||
const QString TABLE = "authors";
|
||||
|
||||
const QString ID = "id";
|
||||
const QString NAME = "name";
|
||||
|
||||
// const QString M_AUTHOR_ID = "id";
|
||||
// const QString M_AUTHOR_FIRST_NAME = "first_name";
|
||||
// const QString M_AUTHOR_LAST_NAME = "last_name";
|
||||
// const QString M_AUTHOR_YEAR = "year";
|
||||
|
||||
}
|
||||
|
||||
struct Author
|
||||
{
|
||||
int id = 0;
|
||||
QString name;
|
||||
// QString firstName;
|
||||
// QString lastName;
|
||||
// QString birthdayYear;
|
||||
};
|
||||
|
||||
static inline QString createTableAuthor()
|
||||
{
|
||||
// query.exec("CREATE TABLE IF NOT EXISTS authors ("
|
||||
// "id INTEGER PRIMARY KEY AUTOINCREMENT,"
|
||||
// "name TEXT UNIQUE NOT NULL"
|
||||
// ")");
|
||||
|
||||
Builder::TableSchema tBuilder(model::authorconst::TABLE);
|
||||
tBuilder.addColumn({ .name = model::authorconst::ID,
|
||||
.type = "INTEGER",
|
||||
.primaryKey = true });
|
||||
|
||||
tBuilder.addColumn({ .name = model::authorconst::NAME,
|
||||
.type = "TEXT",
|
||||
.notNull = true,
|
||||
.unique = true });
|
||||
return tBuilder.get();
|
||||
}
|
||||
|
||||
} // namespace model
|
||||
|
||||
#endif // AUTHOR_H
|
||||
65
src/model/book.h
Normal file
65
src/model/book.h
Normal file
@@ -0,0 +1,65 @@
|
||||
#ifndef BOOK_H
|
||||
#define BOOK_H
|
||||
|
||||
#include <model/author.h>
|
||||
|
||||
#include <sql_builder/create_table.h>
|
||||
|
||||
#include <QString>
|
||||
|
||||
namespace model
|
||||
{
|
||||
|
||||
namespace bookconst
|
||||
{
|
||||
|
||||
const QString TABLE = "books";
|
||||
|
||||
const QString ID = "id";
|
||||
const QString TITLE = "title";
|
||||
const QString AUTHOR_ID = "author_id";
|
||||
const QString FILEPATH = "file_path";
|
||||
|
||||
}
|
||||
|
||||
struct Book
|
||||
{
|
||||
int id = 0;
|
||||
QString title;
|
||||
Author author;
|
||||
QString filePath;
|
||||
};
|
||||
|
||||
static inline QString createTableBook()
|
||||
{
|
||||
// query.exec("CREATE TABLE IF NOT EXISTS books ("
|
||||
// "id INTEGER PRIMARY KEY AUTOINCREMENT,"
|
||||
// "title TEXT NOT NULL,"
|
||||
// "file_path TEXT NOT NULL,"
|
||||
// "author_id INTEGER NOT NULL,"
|
||||
// "FOREIGN KEY (author_id) REFERENCES authors(id) ON DELETE CASCADE"
|
||||
// ");");
|
||||
|
||||
Builder::TableSchema tBuilder(model::bookconst::TABLE);
|
||||
tBuilder.addColumn({ .name = model::bookconst::ID,
|
||||
.type = "INTEGER",
|
||||
.primaryKey = true });
|
||||
|
||||
tBuilder.addColumn({ .name = model::bookconst::TITLE,
|
||||
.type = "TEXT",
|
||||
.notNull = true });
|
||||
|
||||
tBuilder.addColumn({ .name = model::bookconst::FILEPATH,
|
||||
.type = "TEXT",
|
||||
.notNull = true });
|
||||
|
||||
tBuilder.addColumn({ .name = model::bookconst::AUTHOR_ID,
|
||||
.type = "INTEGER",
|
||||
.notNull = true,
|
||||
.foreignKeyTable = model::authorconst::TABLE,
|
||||
.foreignKeyColumn = model::authorconst::ID });
|
||||
return tBuilder.get();
|
||||
}
|
||||
|
||||
} // namespace model
|
||||
#endif // BOOK_H
|
||||
@@ -1,43 +1,15 @@
|
||||
/*!
|
||||
\qmltype cpp-opds
|
||||
\inherits Project
|
||||
\brief Описание
|
||||
*/
|
||||
import qbs
|
||||
|
||||
PSLibrary {
|
||||
cpp.defines: [
|
||||
// You can make your code fail to compile if it uses deprecated APIs.
|
||||
// In order to do so, uncomment the following line.
|
||||
//"QT_DISABLE_DEPRECATED_BEFORE=0x060000" // disables all the APIs deprecated before Qt 6.0.0
|
||||
"DATABASE_SQLITE",
|
||||
"MODEL_LIBRARY"
|
||||
]
|
||||
consoleApplication: true
|
||||
Depends { name: "Qt"; submodules: "core", "sql"}
|
||||
|
||||
Depends { name: "Qt"; submodules: [ "core", "network" ] }
|
||||
Depends { name: "cpp" }
|
||||
|
||||
Depends { name: "odb.gen" }
|
||||
Depends { name: "rdbase" }
|
||||
|
||||
Depends { name: "redkit_gen" }
|
||||
redkit_gen.includeModules: ["JsonSerializer"]
|
||||
|
||||
odb.gen.databases: "sqlite"
|
||||
cpp.cxxLanguageVersion: "c++17"
|
||||
Depends { name: "sql_builder" }
|
||||
|
||||
Group {
|
||||
id: odbs
|
||||
name: "odb"
|
||||
name: "cpp"
|
||||
files: [
|
||||
"**/*.h",
|
||||
"*.h",
|
||||
"*.cpp"
|
||||
]
|
||||
fileTags: ["hpp", "odbxx", "rgen"]
|
||||
}
|
||||
|
||||
cpp.dynamicLibraries: [
|
||||
"odb-sqlite",
|
||||
"odb-qt",
|
||||
"odb",
|
||||
"sqlite3"
|
||||
]
|
||||
}
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
#ifndef MODEL_GLOBAL_H
|
||||
#define MODEL_GLOBAL_H
|
||||
|
||||
#include <QtCore/qglobal.h>
|
||||
|
||||
#if defined(MODEL_LIBRARY)
|
||||
#define MODEL_EXPORT Q_DECL_EXPORT
|
||||
#else
|
||||
#define MODEL_EXPORT Q_DECL_IMPORT
|
||||
#endif
|
||||
|
||||
#endif // MODEL_GLOBAL_H
|
||||
78
src/repositories/author_repository.cpp
Normal file
78
src/repositories/author_repository.cpp
Normal file
@@ -0,0 +1,78 @@
|
||||
// #define BUILD_REPOSITORIES
|
||||
#include "author_repository.h"
|
||||
|
||||
#include <sql_builder/insert.h>
|
||||
#include <sql_builder/select.h>
|
||||
|
||||
#include <QDebug>
|
||||
|
||||
QVector<model::Author> AuthorRepository::getAll()
|
||||
{
|
||||
QVector<model::Author> authors;
|
||||
|
||||
Builder::Select sBuilder({ .tableName = model::authorconst::TABLE, .rows = { model::authorconst::ID, model::authorconst::NAME } });
|
||||
QSqlQuery query(sBuilder.get());
|
||||
|
||||
qDebug() << query.lastError().text();
|
||||
|
||||
while (query.next())
|
||||
{
|
||||
authors.append({ query.value(0).toInt(),
|
||||
query.value(1).toString() });
|
||||
}
|
||||
|
||||
return authors;
|
||||
}
|
||||
|
||||
std::optional<model::Author> AuthorRepository::getByName(const QString& name)
|
||||
{
|
||||
Q_UNUSED(name);
|
||||
Builder::Select sBuilder({ .tableName = model::authorconst::TABLE, .rows = { model::authorconst::ID, model::authorconst::NAME } });
|
||||
QString qResult = sBuilder.get() + QString(" WHERE %1 = \"%2\"").arg(model::authorconst::NAME).arg(name);
|
||||
|
||||
QSqlQuery query(qResult);
|
||||
|
||||
if (query.next())
|
||||
{
|
||||
model::Author author({ .id = query.value(0).toInt(),
|
||||
.name = query.value(1).toString() });
|
||||
return { author };
|
||||
}
|
||||
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
bool AuthorRepository::insert(const model::Author& author)
|
||||
{
|
||||
QSqlQuery query(DatabaseManager::instance().database());
|
||||
Builder::Insert iBuild({ model::authorconst::TABLE,
|
||||
{ model::authorconst::NAME },
|
||||
true });
|
||||
|
||||
query.prepare(iBuild.get());
|
||||
query.bindValue(":name", author.name);
|
||||
int authorId = 0;
|
||||
Q_UNUSED(authorId);
|
||||
|
||||
if (!query.exec())
|
||||
{
|
||||
qDebug() << query.lastError().text();
|
||||
if (query.lastError().nativeErrorCode() == 19)
|
||||
{
|
||||
// Автор уже есть, ищем его ID
|
||||
QSqlQuery findQuery(DatabaseManager::instance().database());
|
||||
findQuery.prepare("SELECT id FROM authors WHERE name = :name");
|
||||
findQuery.bindValue(":name", author.name);
|
||||
if (findQuery.exec() && findQuery.next())
|
||||
{
|
||||
authorId = findQuery.value(0).toInt();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
qCritical() << "Ошибка добавления автора:" << query.lastError().text();
|
||||
return false;
|
||||
}
|
||||
|
||||
authorId = query.lastInsertId().toInt();
|
||||
return true;
|
||||
}
|
||||
29
src/repositories/author_repository.h
Normal file
29
src/repositories/author_repository.h
Normal file
@@ -0,0 +1,29 @@
|
||||
#pragma once
|
||||
|
||||
#include <model/author.h>
|
||||
|
||||
#include <database/databasemanager.h>
|
||||
|
||||
#include <QSqlDatabase>
|
||||
#include <QSqlError>
|
||||
#include <QSqlQuery>
|
||||
#include <QString>
|
||||
#include <QVariant>
|
||||
#include <QVector>
|
||||
#include <optional>
|
||||
|
||||
#ifdef BUILD_REPOSITORIES
|
||||
#define REPOSITORIES_EXPORT Q_DECL_EXPORT
|
||||
#else
|
||||
#define REPOSITORIES_EXPORT Q_DECL_IMPORT
|
||||
#endif
|
||||
|
||||
class REPOSITORIES_EXPORT AuthorRepository
|
||||
{
|
||||
public:
|
||||
static QVector<model::Author> getAll();
|
||||
static std::optional<model::Author> getByName(const QString& name);
|
||||
static bool insert(const model::Author& author);
|
||||
|
||||
// TODO Возвращать не bool - а номер id записи
|
||||
};
|
||||
59
src/repositories/book_repository.cpp
Normal file
59
src/repositories/book_repository.cpp
Normal file
@@ -0,0 +1,59 @@
|
||||
// #define BUILD_REPOSITORIES
|
||||
#include "book_repository.h"
|
||||
|
||||
#include <QDebug>
|
||||
#include <qsqlerror.h>
|
||||
|
||||
#include <sql_builder/insert.h>
|
||||
#include <sql_builder/select.h>
|
||||
|
||||
QVector<model::Book> BookRepository::getAll()
|
||||
{
|
||||
QVector<model::Book> books;
|
||||
Builder::Select qBuilder {
|
||||
model::bookconst::TABLE,
|
||||
{ model::bookconst::ID,
|
||||
model::bookconst::TITLE,
|
||||
model::bookconst::AUTHOR_ID,
|
||||
model::bookconst::FILEPATH }
|
||||
};
|
||||
|
||||
QSqlQuery query(qBuilder.get());
|
||||
|
||||
qDebug() << query.lastError().text();
|
||||
|
||||
while (query.next())
|
||||
{
|
||||
books.append({ query.value(0).toInt(),
|
||||
query.value(1).toString(),
|
||||
{ query.value(2).toInt() },
|
||||
query.value(3).toString() });
|
||||
}
|
||||
|
||||
// TODO получить так же авторов как строку
|
||||
|
||||
return books;
|
||||
}
|
||||
|
||||
bool BookRepository::insert(const model::Book& book)
|
||||
{
|
||||
QSqlQuery query(DatabaseManager::instance().database());
|
||||
|
||||
Builder::Insert iBuilder {
|
||||
model::bookconst::TABLE,
|
||||
{ model::bookconst::TITLE,
|
||||
model::bookconst::FILEPATH,
|
||||
model::bookconst::AUTHOR_ID }
|
||||
};
|
||||
|
||||
query.prepare(iBuilder.get());
|
||||
query.bindValue(":" + model::bookconst::TITLE, book.title);
|
||||
query.bindValue(":" + model::bookconst::FILEPATH, book.filePath);
|
||||
query.bindValue(":" + model::bookconst::AUTHOR_ID, book.author.id);
|
||||
|
||||
bool res = query.exec();
|
||||
if (!res)
|
||||
qDebug() << query.lastError().text();
|
||||
|
||||
return res;
|
||||
}
|
||||
22
src/repositories/book_repository.h
Normal file
22
src/repositories/book_repository.h
Normal file
@@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
|
||||
#include <model/book.h>
|
||||
|
||||
#include <database/databasemanager.h>
|
||||
|
||||
#include <QSqlQuery>
|
||||
#include <QVariant>
|
||||
#include <QVector>
|
||||
|
||||
#ifdef BUILD_REPOSITORIES
|
||||
#define REPOSITORIES_EXPORT Q_DECL_EXPORT
|
||||
#else
|
||||
#define REPOSITORIES_EXPORT Q_DECL_IMPORT
|
||||
#endif
|
||||
|
||||
class REPOSITORIES_EXPORT BookRepository
|
||||
{
|
||||
public:
|
||||
static QVector<model::Book> getAll();
|
||||
static bool insert(const model::Book& book);
|
||||
};
|
||||
21
src/repositories/repositories.qbs
Normal file
21
src/repositories/repositories.qbs
Normal file
@@ -0,0 +1,21 @@
|
||||
import qbs
|
||||
|
||||
PSLibrary {
|
||||
Depends { name: "Qt"; submodules: "core", "sql"}
|
||||
|
||||
Depends { name: "database" }
|
||||
Depends { name: "sql_builder" }
|
||||
|
||||
Group {
|
||||
name: "cpp"
|
||||
files: [
|
||||
"*.h",
|
||||
"*.cpp"
|
||||
]
|
||||
}
|
||||
|
||||
Properties {
|
||||
condition: qbs.buildVariant === "debug"
|
||||
cpp.defines: ["BUILD_REPOSITORIES"]
|
||||
}
|
||||
}
|
||||
@@ -18,7 +18,6 @@ WPSLibrary {
|
||||
Depends { name: "odb.gen" }
|
||||
Depends { name: "redkit_gen" }
|
||||
Depends { name: "rdbase" }
|
||||
Depends { name: "model" }
|
||||
|
||||
cpp.cxxLanguageVersion: "c++20"
|
||||
|
||||
|
||||
@@ -5,10 +5,10 @@
|
||||
#include <QJsonObject>
|
||||
#include <QJsonValue>
|
||||
|
||||
#include <model/author_s-odb.hxx> // Должен быть здесь
|
||||
#include <model/author_s.h>
|
||||
#include <model/book_s-odb.hxx> // Должен быть здесь
|
||||
#include <model/book_s.h>
|
||||
#include <database/author_s-odb.hxx> // Должен быть здесь
|
||||
#include <database/author_s.h>
|
||||
#include <database/book_s-odb.hxx> // Должен быть здесь
|
||||
#include <database/book_s.h>
|
||||
|
||||
#include <odb/core.hxx>
|
||||
#include <odb/database.hxx>
|
||||
|
||||
12
src/services/author_service.cpp
Normal file
12
src/services/author_service.cpp
Normal file
@@ -0,0 +1,12 @@
|
||||
// #define BUILD_SERVICES
|
||||
#include "author_service.h"
|
||||
|
||||
QVector<model::Author> AuthorService::fetchAll()
|
||||
{
|
||||
return AuthorRepository::getAll();
|
||||
}
|
||||
|
||||
bool AuthorService::add(const model::Author& author)
|
||||
{
|
||||
return AuthorRepository::insert(author);
|
||||
}
|
||||
20
src/services/author_service.h
Normal file
20
src/services/author_service.h
Normal file
@@ -0,0 +1,20 @@
|
||||
#pragma once
|
||||
|
||||
#include <model/author.h>
|
||||
|
||||
#include <repositories/author_repository.h>
|
||||
|
||||
#include <QVector>
|
||||
|
||||
#ifdef BUILD_SERVICES
|
||||
#define SERVICES_EXPORT Q_DECL_EXPORT
|
||||
#else
|
||||
#define SERVICES_EXPORT Q_DECL_IMPORT
|
||||
#endif
|
||||
|
||||
class SERVICES_EXPORT AuthorService
|
||||
{
|
||||
public:
|
||||
static QVector<model::Author> fetchAll();
|
||||
static bool add(const model::Author& name);
|
||||
};
|
||||
28
src/services/book_service.cpp
Normal file
28
src/services/book_service.cpp
Normal file
@@ -0,0 +1,28 @@
|
||||
// #define BUILD_SERVICES
|
||||
#include "book_service.h"
|
||||
#include "author_service.h"
|
||||
|
||||
QVector<model::Book> BookService::fetchAll()
|
||||
{
|
||||
return BookRepository::getAll();
|
||||
}
|
||||
|
||||
bool BookService::add(const model::Book& book)
|
||||
{
|
||||
auto res = AuthorRepository::getByName(book.author.name);
|
||||
|
||||
if (!res.has_value())
|
||||
{
|
||||
if (!AuthorService::add(book.author))
|
||||
{
|
||||
qCritical() << "Ошибка добавления автора!";
|
||||
return false;
|
||||
}
|
||||
|
||||
res = AuthorRepository::getByName(book.author.name);
|
||||
}
|
||||
|
||||
model::Book BookAuthor = book;
|
||||
BookAuthor.author = res.value();
|
||||
return BookRepository::insert(BookAuthor);
|
||||
}
|
||||
20
src/services/book_service.h
Normal file
20
src/services/book_service.h
Normal file
@@ -0,0 +1,20 @@
|
||||
#pragma once
|
||||
|
||||
#include <model/book.h>
|
||||
|
||||
#include <repositories/book_repository.h>
|
||||
|
||||
#include <QVector>
|
||||
|
||||
#ifdef BUILD_SERVICES
|
||||
#define SERVICES_EXPORT Q_DECL_EXPORT
|
||||
#else
|
||||
#define SERVICES_EXPORT Q_DECL_IMPORT
|
||||
#endif
|
||||
|
||||
class SERVICES_EXPORT BookService
|
||||
{
|
||||
public:
|
||||
static QVector<model::Book> fetchAll();
|
||||
static bool add(const model::Book& book);
|
||||
};
|
||||
22
src/services/services.qbs
Normal file
22
src/services/services.qbs
Normal file
@@ -0,0 +1,22 @@
|
||||
import qbs
|
||||
|
||||
PSLibrary {
|
||||
Depends { name: "Qt"; submodules: "core", "sql"}
|
||||
|
||||
Depends { name: "repositories" }
|
||||
|
||||
Group {
|
||||
name: "cpp"
|
||||
files: [
|
||||
"*.h",
|
||||
"*.cpp"
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
|
||||
Properties {
|
||||
condition: qbs.buildVariant === "debug"
|
||||
cpp.defines: ["BUILD_SERVICES"]
|
||||
}
|
||||
}
|
||||
45
src/sql_builder/create_table.cpp
Normal file
45
src/sql_builder/create_table.cpp
Normal file
@@ -0,0 +1,45 @@
|
||||
#include "create_table.h"
|
||||
|
||||
#include <QDebug>
|
||||
|
||||
namespace Builder
|
||||
{
|
||||
|
||||
QString Column::toSQL() const
|
||||
{
|
||||
QString columnDef = name + " " + type;
|
||||
if (primaryKey)
|
||||
columnDef += " PRIMARY KEY";
|
||||
if (notNull)
|
||||
columnDef += " NOT NULL";
|
||||
if (unique)
|
||||
columnDef += " UNIQUE";
|
||||
if (defaultValue)
|
||||
columnDef += " DEFAULT " + *defaultValue;
|
||||
if (foreignKeyTable && foreignKeyColumn)
|
||||
{
|
||||
columnDef += " REFERENCES " + *foreignKeyTable + "(" + *foreignKeyColumn + ")";
|
||||
}
|
||||
return columnDef;
|
||||
}
|
||||
|
||||
TableSchema& TableSchema::addColumn(const Column& column)
|
||||
{
|
||||
columns.push_back(column);
|
||||
return *this;
|
||||
}
|
||||
|
||||
QString TableSchema::get() const
|
||||
{
|
||||
QStringList columnDefs;
|
||||
for (const auto& col : columns)
|
||||
{
|
||||
columnDefs.append(col.toSQL());
|
||||
}
|
||||
|
||||
QString result = "CREATE TABLE IF NOT EXISTS " + name + " ( " + columnDefs.join(", ") + " );";
|
||||
// qDebug() << result;
|
||||
return result;
|
||||
}
|
||||
|
||||
} // namespace Builder
|
||||
50
src/sql_builder/create_table.h
Normal file
50
src/sql_builder/create_table.h
Normal file
@@ -0,0 +1,50 @@
|
||||
#pragma once
|
||||
|
||||
#include <QString>
|
||||
#include <QStringList>
|
||||
#include <QVector>
|
||||
|
||||
#include <optional>
|
||||
|
||||
namespace Builder
|
||||
{
|
||||
|
||||
struct Column
|
||||
{
|
||||
QString name; // Имя колонки
|
||||
QString type; // Тип колонки
|
||||
bool primaryKey = false; // Является ли колонка первичным ключом?
|
||||
bool notNull = false; // Колонка не должна быть пуста
|
||||
bool unique = false; // Значение в колонке должны быть уникальны
|
||||
std::optional<QString> defaultValue;
|
||||
std::optional<QString> foreignKeyTable;
|
||||
std::optional<QString> foreignKeyColumn;
|
||||
|
||||
/*!
|
||||
* \brief Сформировать SQL код для колонки
|
||||
*/
|
||||
QString toSQL() const;
|
||||
};
|
||||
|
||||
class Q_DECL_EXPORT TableSchema
|
||||
{
|
||||
public:
|
||||
TableSchema(QString tableName) :
|
||||
name(std::move(tableName)) {}
|
||||
|
||||
/*!
|
||||
* \brief Добавить колонку
|
||||
*/
|
||||
TableSchema& addColumn(const Column& column);
|
||||
|
||||
/*!
|
||||
* \brief Сформировать SQL код для создания таблицы
|
||||
*/
|
||||
QString get() const;
|
||||
|
||||
private:
|
||||
QString name; // Имя таблицы
|
||||
QVector<Column> columns; // Список колонок таблицы
|
||||
};
|
||||
|
||||
} // namespace Builder
|
||||
39
src/sql_builder/insert.cpp
Normal file
39
src/sql_builder/insert.cpp
Normal file
@@ -0,0 +1,39 @@
|
||||
#include "insert.h"
|
||||
|
||||
#include <QDebug>
|
||||
|
||||
namespace Builder
|
||||
{
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
const QString INSERT = QStringLiteral("INSERT");
|
||||
const QString INTO = QStringLiteral("INTO");
|
||||
const QString VALUES = QStringLiteral("VALUES");
|
||||
const QString IGNORE = QStringLiteral("IGNORE");
|
||||
|
||||
}
|
||||
|
||||
QString Insert::get() const
|
||||
{
|
||||
QString resultString = QString("%1 ").arg(INSERT);
|
||||
if (orIgnore)
|
||||
resultString += QString("OR %1 ").arg(IGNORE);
|
||||
resultString += QString("%1 %2 ").arg(INTO).arg(tableName);
|
||||
|
||||
resultString += QString("( %1 ) ").arg(rows.join(", "));
|
||||
|
||||
// TODO как будто бы проще сразу это заполнить значениями
|
||||
QStringList allValue;
|
||||
for (const auto& row : rows)
|
||||
allValue.push_back(QString(":%1").arg(row));
|
||||
|
||||
resultString += QString("%1 ( %2 )").arg(VALUES).arg(allValue.join(", "));
|
||||
|
||||
resultString += ';';
|
||||
// qDebug() << resultString;
|
||||
return resultString;
|
||||
}
|
||||
|
||||
} // namespace Builder
|
||||
22
src/sql_builder/insert.h
Normal file
22
src/sql_builder/insert.h
Normal file
@@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
|
||||
#include <QString>
|
||||
#include <QStringList>
|
||||
|
||||
namespace Builder
|
||||
{
|
||||
|
||||
struct Q_DECL_EXPORT Insert
|
||||
{
|
||||
QString tableName; // Имя таблицы
|
||||
QStringList rows; // Именования колонок
|
||||
// QStringList params;
|
||||
bool orIgnore = false;
|
||||
|
||||
/*!
|
||||
* \brief Сформировать SQL код для выполнения
|
||||
*/
|
||||
QString get() const;
|
||||
};
|
||||
|
||||
} // namespace Builder
|
||||
32
src/sql_builder/select.cpp
Normal file
32
src/sql_builder/select.cpp
Normal file
@@ -0,0 +1,32 @@
|
||||
#include "select.h"
|
||||
|
||||
#include <QDebug>
|
||||
|
||||
namespace Builder
|
||||
{
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
const QString SELECT = QStringLiteral("SELECT");
|
||||
const QString FROM = QStringLiteral("FROM");
|
||||
const QString WHERE = QStringLiteral("WHERE");
|
||||
|
||||
}
|
||||
|
||||
QString Select::get() const
|
||||
{
|
||||
QString allRows = rows.join(", ");
|
||||
|
||||
QString resultString = QString("%1 %2").arg(SELECT).arg(allRows);
|
||||
|
||||
resultString += QString(" %1 %2").arg(FROM).arg(tableName);
|
||||
|
||||
if (!where.isEmpty())
|
||||
resultString += QString(" %1 %2").arg(WHERE).arg(where);
|
||||
|
||||
// resultString += ';';
|
||||
return resultString;
|
||||
}
|
||||
|
||||
} // namespace Builder
|
||||
21
src/sql_builder/select.h
Normal file
21
src/sql_builder/select.h
Normal file
@@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
#include <QString>
|
||||
#include <QStringList>
|
||||
|
||||
namespace Builder
|
||||
{
|
||||
|
||||
struct Q_DECL_EXPORT Select
|
||||
{
|
||||
QString tableName; // Имя таблицы
|
||||
QStringList rows = { "*" }; // Колонки
|
||||
QString where = {}; // Условие поиска
|
||||
|
||||
/*!
|
||||
* \brief Сформировать SQL код для выполнения
|
||||
*/
|
||||
QString get() const;
|
||||
};
|
||||
|
||||
} // namespace Builder
|
||||
14
src/sql_builder/sql_builder.qbs
Normal file
14
src/sql_builder/sql_builder.qbs
Normal file
@@ -0,0 +1,14 @@
|
||||
import qbs
|
||||
|
||||
PSLibrary {
|
||||
Depends { name: "Qt"; submodules: "sql"}
|
||||
|
||||
|
||||
Group {
|
||||
name: "cpp"
|
||||
files: [
|
||||
"**/*.h",
|
||||
"**/*.cpp"
|
||||
]
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user