From 905dd309d037a6ec14076e1bcabcfdbddd5791e7 Mon Sep 17 00:00:00 2001 From: alex Date: Mon, 4 Aug 2025 20:51:24 +0500 Subject: [PATCH] =?UTF-8?q?=D0=92=D1=8B=D0=BD=D0=B5=D1=81=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=20=D0=BE=D0=BF=D0=B8=D1=81=D0=B0=D0=BD=D0=B8=D0=B5?= =?UTF-8?q?=20=D1=82=D0=B0=D0=B1=D0=BB=D0=B8=D1=86=20=D0=B2=20model?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- project.qbs | 1 + src/cpp-opds.qbs | 1 + src/database/database.hxx | 80 +----------------------------- src/database/database.qbs | 23 ++------- src/database/database_utils.cpp | 57 +++++++++++++++++++++ src/database/database_utils.h | 37 ++++++++++++++ src/main.cpp | 8 +-- src/{database => model}/author_s.h | 4 +- src/{database => model}/book_s.h | 4 +- src/model/model.qbs | 43 ++++++++++++++++ src/model/model_global.h | 12 +++++ src/restapi/restapi.qbs | 1 + src/restapi/restapiserver.cpp | 8 +-- 13 files changed, 169 insertions(+), 110 deletions(-) create mode 100644 src/database/database_utils.cpp create mode 100644 src/database/database_utils.h rename src/{database => model}/author_s.h (95%) rename src/{database => model}/book_s.h (95%) create mode 100644 src/model/model.qbs create mode 100644 src/model/model_global.h diff --git a/project.qbs b/project.qbs index d717770..fee6594 100644 --- a/project.qbs +++ b/project.qbs @@ -15,6 +15,7 @@ Project { "src/cpp-opds.qbs", "src/database/database.qbs", + "src/model/model.qbs", "src/restapi/restapi.qbs", ] } diff --git a/src/cpp-opds.qbs b/src/cpp-opds.qbs index ae24c6d..f1b969f 100644 --- a/src/cpp-opds.qbs +++ b/src/cpp-opds.qbs @@ -21,6 +21,7 @@ PSApplication { Depends { name: "restapi" } Depends { name: "redkit_gen" } Depends { name: "rdbase" } + Depends { name: "model" } cpp.cxxLanguageVersion: "c++20" diff --git a/src/database/database.hxx b/src/database/database.hxx index 91ccace..0b43538 100644 --- a/src/database/database.hxx +++ b/src/database/database.hxx @@ -13,87 +13,9 @@ #include // std::unique_ptr #include -#include - -#if defined(DATABASE_MYSQL) -#include -#elif defined(DATABASE_SQLITE) -#include -#include -#include -#include -#elif defined(DATABASE_PGSQL) -#include -#elif defined(DATABASE_ORACLE) -#include -#elif defined(DATABASE_MSSQL) -#include -#else -#error unknown database; did you forget to define the DATABASE_* macros? -#endif - #include -#include -#include -using oDBase = odb::sqlite::database; -using uDBase = U; - -struct testData -{ - QString first; - QString last; - int age; -}; - -// Функция для генерации случайной строки (имени или фамилии) -QString generate_random_string(const QVector& 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 fillAuthorDB() -{ - QVector 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 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 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; -} +#include "database_utils.h" inline uDBase openDB(const std::string path_db) { diff --git a/src/database/database.qbs b/src/database/database.qbs index 349b198..c1e4a3e 100644 --- a/src/database/database.qbs +++ b/src/database/database.qbs @@ -13,37 +13,22 @@ PSLibrary { ] consoleApplication: true - Depends { name: "Qt"; submodules: [ "core", "sql", "network" ] } + 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" Group { name: "cpp" files: [ - "databasemanager.*", - "*.cpp", - "*.h", - "*.hxx", + "**/*.h", + "**/*.hxx", + "**/*.cpp", ] - excludeFiles: odbs.files - } - - Group { - id: odbs - name: "odb" - files: [ - "author_s.h", - "book_s.h", - ] - fileTags: ["hpp", "odbxx"] } cpp.dynamicLibraries: [ diff --git a/src/database/database_utils.cpp b/src/database/database_utils.cpp new file mode 100644 index 0000000..0081295 --- /dev/null +++ b/src/database/database_utils.cpp @@ -0,0 +1,57 @@ +#include "database_utils.h" + +#include + +namespace +{ + +// Функция для генерации случайной строки (имени или фамилии) +QString generate_random_string(const QVector& 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 fillAuthorDB() +{ + QVector 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 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 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; +} diff --git a/src/database/database_utils.h b/src/database/database_utils.h new file mode 100644 index 0000000..a4f227a --- /dev/null +++ b/src/database/database_utils.h @@ -0,0 +1,37 @@ +#include +#include + +#include + +#if defined(DATABASE_MYSQL) +#include +#elif defined(DATABASE_SQLITE) +#include +#include +#include +#include +#elif defined(DATABASE_PGSQL) +#include +#elif defined(DATABASE_ORACLE) +#include +#elif defined(DATABASE_MSSQL) +#include +#else +#error unknown database; did you forget to define the DATABASE_* macros? +#endif + +#include + +#include "database_global.h" + +using oDBase = odb::sqlite::database; +using uDBase = U; + +struct testData +{ + QString first; + QString last; + int age; +}; + +QVector DATABASE_EXPORT fillAuthorDB(); diff --git a/src/main.cpp b/src/main.cpp index 026e6f8..247ddbd 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,10 +8,10 @@ #include /* Опыты с odb */ -#include // Должен быть здесь -#include -#include // Должен быть здесь -#include +#include // Должен быть здесь +#include +#include // Должен быть здесь +#include #include // create_database #include diff --git a/src/database/author_s.h b/src/model/author_s.h similarity index 95% rename from src/database/author_s.h rename to src/model/author_s.h index f821cb6..bb9ea34 100644 --- a/src/database/author_s.h +++ b/src/model/author_s.h @@ -4,7 +4,7 @@ #ifndef AUTHOR_S_H #define AUTHOR_S_H -#include "database_global.h" +#include "model_global.h" #include @@ -13,7 +13,7 @@ #include #pragma db object -class DATABASE_EXPORT Author_S +class MODEL_EXPORT Author_S { public: Author_S() = default; diff --git a/src/database/book_s.h b/src/model/book_s.h similarity index 95% rename from src/database/book_s.h rename to src/model/book_s.h index d516b51..8a82ded 100644 --- a/src/database/book_s.h +++ b/src/model/book_s.h @@ -4,7 +4,7 @@ #ifndef BOOK_S_H #define BOOK_S_H -#include "database_global.h" +#include "model_global.h" #include #include @@ -14,7 +14,7 @@ #include "author_s.h" #pragma db object -class DATABASE_EXPORT Book_S +class MODEL_EXPORT Book_S { public: Book_S() = default; diff --git a/src/model/model.qbs b/src/model/model.qbs new file mode 100644 index 0000000..fbd8c8d --- /dev/null +++ b/src/model/model.qbs @@ -0,0 +1,43 @@ +/*! + \qmltype cpp-opds + \inherits Project + \brief Описание +*/ +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", "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 { + id: odbs + name: "odb" + files: [ + "**/*.h", + ] + fileTags: ["hpp", "odbxx", "rgen"] + } + + cpp.dynamicLibraries: [ + "odb-sqlite", + "odb-qt", + "odb", + "sqlite3" + ] +} diff --git a/src/model/model_global.h b/src/model/model_global.h new file mode 100644 index 0000000..c01f3b5 --- /dev/null +++ b/src/model/model_global.h @@ -0,0 +1,12 @@ +#ifndef MODEL_GLOBAL_H +#define MODEL_GLOBAL_H + +#include + +#if defined(MODEL_LIBRARY) +#define MODEL_EXPORT Q_DECL_EXPORT +#else +#define MODEL_EXPORT Q_DECL_IMPORT +#endif + +#endif // MODEL_GLOBAL_H diff --git a/src/restapi/restapi.qbs b/src/restapi/restapi.qbs index cdce2c0..046ae13 100644 --- a/src/restapi/restapi.qbs +++ b/src/restapi/restapi.qbs @@ -18,6 +18,7 @@ WPSLibrary { Depends { name: "odb.gen" } Depends { name: "redkit_gen" } Depends { name: "rdbase" } + Depends { name: "model" } cpp.cxxLanguageVersion: "c++20" diff --git a/src/restapi/restapiserver.cpp b/src/restapi/restapiserver.cpp index 3f7ff75..b9820e2 100644 --- a/src/restapi/restapiserver.cpp +++ b/src/restapi/restapiserver.cpp @@ -5,10 +5,10 @@ #include #include -#include // Должен быть здесь -#include -#include // Должен быть здесь -#include +#include // Должен быть здесь +#include +#include // Должен быть здесь +#include #include #include