From f4958e60bb710da45195262a57c51934e7c005fa Mon Sep 17 00:00:00 2001 From: alex Date: Sun, 3 Aug 2025 00:14:45 +0500 Subject: [PATCH] =?UTF-8?q?=D0=91=D0=B8=D0=BB=D0=B4=D0=B8=D0=BC=20=D1=81?= =?UTF-8?q?=20odb=20=D0=B8=D0=B7=20redkitty?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitmodules | 3 ++ project.qbs | 9 +--- qbs/modules/odbModule/odbModule.qbs | 65 ----------------------------- redkitty | 1 + src/cpp-opds.qbs | 15 +++++-- src/database/author_s.h | 22 +++++----- src/database/book_s.h | 20 ++++----- src/database/database.hxx | 17 +++++--- src/database/database.qbs | 30 ++++++------- src/database/database_global.h | 12 ++++++ src/database/databasemanager.h | 4 +- src/main.cpp | 53 +++++++++++------------ src/restapi/restapi.qbs | 8 ++-- src/restapi/restapiserver.cpp | 16 +++---- 14 files changed, 117 insertions(+), 158 deletions(-) delete mode 100644 qbs/modules/odbModule/odbModule.qbs create mode 160000 redkitty create mode 100644 src/database/database_global.h diff --git a/.gitmodules b/.gitmodules index 223e5c9..cc5a562 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,6 @@ [submodule "redkbuild"] path = redkbuild url = ssh://git@gitea.redkit-lab.work:57322/redkit-lab/redkbuild.git +[submodule "redkitty"] + path = redkitty + url = git@gitea.redkit-lab.work:redkit-lab/redkitty.git diff --git a/project.qbs b/project.qbs index a7628b6..381658f 100644 --- a/project.qbs +++ b/project.qbs @@ -3,14 +3,9 @@ import qbs Project { name: "opds++" qbsSearchPaths: [ - "qbs", - // "redkitty/qbs", - // // "guiness/qbs", - // // "prerequisites/qbs", - // // "report/qbs", + "redkitty/qbs", "redkbuild", - // // "ext_libs/openssl/qbs", - // "redkitty/lib/redkit-gen-integration/qbs", + "redkitty/lib/redkit-gen-integration/qbs", ] references: [ diff --git a/qbs/modules/odbModule/odbModule.qbs b/qbs/modules/odbModule/odbModule.qbs deleted file mode 100644 index 1f0837c..0000000 --- a/qbs/modules/odbModule/odbModule.qbs +++ /dev/null @@ -1,65 +0,0 @@ -import qbs -import qbs.FileInfo - -Module { - Depends { name: "cpp" } - Depends { name: "Qt.core" } - - FileTagger { - patterns: "*.hxx" - fileTags: ["odb"] - } - - Rule { - inputs: ["odb"] - Artifact { - // filePath: FileInfo.baseName(input.fileName)+ "-odb.hxx" - filePath: FileInfo.cleanPath(input.filePath + "/../" ) + "/" + FileInfo.baseName(input.fileName) + "-odb.hxx" - fileTags: ["hpp"] - } - Artifact { - // filePath: FileInfo.baseName(input.fileName) + "-odb.cxx" - filePath: { - // console.error("THIS ART "+FileInfo.cleanPath(input.filePath + "/../") + "/" + FileInfo.baseName(input.fileName) + "-odb.cxx") - return FileInfo.cleanPath(input.filePath + "/../") + "/" + FileInfo.baseName(input.fileName) + "-odb.cxx"} - fileTags: ["cpp"] - } - - prepare: { - var cmd = new Command("odb", [ - "--std", "c++11", - "-d", "sqlite", - "--generate-query", - "--generate-schema", - "--generate-prepared", - // "--include-prefix", FileInfo.cleanPath(input.filePath + "/../"), - // "--output-dir", product.Qt.core.qmDir, - "--output-dir", FileInfo.cleanPath(input.filePath + "/../"), - "-I/home/uzver/qt5.15/include", - input.filePath, - ]); - cmd.description = "Generating ODB files for " + input.fileName; - - console.error(cmd.arguments) - - return [cmd]; - } - } - - cpp.includePaths: [ - project.sourceDirectory, - product.Qt.core.qmDir, - "/usr/include", // Общие заголовки - "/usr/include/odb" // Заголовки ODB - ] - - // Подключаем библиотеки ODB - cpp.libraryPaths: [ - "/usr/lib" // Путь к библиотекам - ] - - cpp.dynamicLibraries: [ - "odb", - "odb-sqlite" - ] -} diff --git a/redkitty b/redkitty new file mode 160000 index 0000000..2316dde --- /dev/null +++ b/redkitty @@ -0,0 +1 @@ +Subproject commit 2316ddea7250c3d828a7c4a45f49ea8bc100448b diff --git a/src/cpp-opds.qbs b/src/cpp-opds.qbs index c1f36a7..1db962c 100644 --- a/src/cpp-opds.qbs +++ b/src/cpp-opds.qbs @@ -3,7 +3,7 @@ \inherits Project \brief Описание */ -Application { +PSApplication { // CppApplication { cpp.defines: [ // You can make your code fail to compile if it uses deprecated APIs. @@ -16,9 +16,10 @@ Application { Depends { name: "Qt"; submodules: [ "core", "sql", "network" ] } Depends { name: "cpp" } Depends { name: "database" } - Depends { name: "odbModule"} - - // Depends { name: "redkit_gen" } + Depends { name: "odb.gen" } + Depends { name: "sqlite3" } + Depends { name: "restapi" } + Depends { name: "redkit_gen" } cpp.cxxLanguageVersion: "c++20" @@ -29,6 +30,12 @@ Application { ] } + cpp.dynamicLibraries: [ + "odb-sqlite", + "odb-qt", + "odb", + "sqlite3" + ] // } } // Project diff --git a/src/database/author_s.h b/src/database/author_s.h index 2a5487b..4c3fa5b 100644 --- a/src/database/author_s.h +++ b/src/database/author_s.h @@ -4,24 +4,22 @@ #ifndef AUTHOR_S_H #define AUTHOR_S_H -// #include +#include "database_global.h" -#include // std::size_t -#include -#include +#include #include #include #include #pragma db object -class __attribute__((visibility("default"))) Author_S +class DATABASE_EXPORT Author_S { public: Author_S() = default; - Author_S(const std::string& first, - const std::string& last, + Author_S(const QString& first, + const QString& last, const unsigned short age) : m_first(first), m_last(last), @@ -30,11 +28,11 @@ public: } unsigned long long id() const { return m_id; } - std::string first() const { return m_first; } - std::string last() const { return m_last; } + QString first() const { return m_first; } + QString last() const { return m_last; } unsigned short age() const { return m_age; } - std::string full_name() const { return m_first + " " + m_last; } + QString full_name() const { return m_first + " " + m_last; } void age(unsigned short age) { m_age = age; } @@ -44,8 +42,8 @@ private: #pragma db id auto unsigned long long m_id; - std::string m_first; - std::string m_last; + QString m_first; + QString m_last; unsigned short m_age; }; diff --git a/src/database/book_s.h b/src/database/book_s.h index a09ac73..bb5d559 100644 --- a/src/database/book_s.h +++ b/src/database/book_s.h @@ -4,23 +4,23 @@ #ifndef BOOK_S_H #define BOOK_S_H -// #include +#include "database_global.h" -#include -#include +#include +#include #include #include "author_s.h" #pragma db object -class __attribute__((visibility("default"))) Book_S +class DATABASE_EXPORT Book_S { public: Book_S() = default; - Book_S(const std::string& name, - const std::shared_ptr& author, + Book_S(const QString & name, + const QSharedPointer& author, const int year) { m_author = author; @@ -29,8 +29,8 @@ public: }; unsigned long long id() const { return m_id; } - std::shared_ptr author() const { return m_author; } - std::string name() const { return m_name; } + QSharedPointer author() const { return m_author; } + QString name() const { return m_name; } int year() const { return m_year; } private: @@ -40,8 +40,8 @@ private: #pragma db id auto unsigned long long m_id; - std::shared_ptr m_author; - std::string m_name; + QSharedPointer m_author; + QString m_name; int m_year; }; diff --git a/src/database/database.hxx b/src/database/database.hxx index 8961f54..9e43499 100644 --- a/src/database/database.hxx +++ b/src/database/database.hxx @@ -35,6 +35,9 @@ #include #include +using odatabase = odb::sqlite::database; +using unidb = std::unique_ptr; + struct testData { std::string first; @@ -91,21 +94,23 @@ std::vector fillDB() return vecTest; } -inline std::unique_ptr openDB(std::string path_db) +inline unidb openDB(const std::string path_db) { // Открыть БД, если она существует + auto db_ptr = new odatabase(path_db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE); + if (std::filesystem::is_regular_file(path_db)) - return std::unique_ptr(new odb::sqlite::database(path_db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE)); + return unidb(db_ptr); // И создать новую, если не существует - std::unique_ptr db(new odb::sqlite::database(path_db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE)); + unidb db(db_ptr); - odb::core::connection_ptr connect_ptr(db->connection()); + odb::connection_ptr connect_ptr(db->connection()); connect_ptr->execute("PRAGMA foreign_keys=OFF"); - odb::core::transaction tans(db->begin()); - odb::core::schema_catalog::create_schema(*db); + odb::transaction tans(db->begin()); + odb::schema_catalog::create_schema(*db); tans.commit(); connect_ptr->execute("PRAGMA foreign_keys=ON"); diff --git a/src/database/database.qbs b/src/database/database.qbs index 87b0a38..ba173c0 100644 --- a/src/database/database.qbs +++ b/src/database/database.qbs @@ -8,45 +8,45 @@ PSLibrary { // 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" + "DATABASE_SQLITE", + "DATABASE_LIBRARY" ] consoleApplication: true Depends { name: "Qt"; submodules: [ "core", "sql", "network" ] } Depends { name: "cpp" } - Depends { name: "odbModule"} + Depends { name: "odb.gen" } + Depends { name: "redkit_gen" } - cpp.cxxLanguageVersion: "c++20" + odb.gen.databases: "sqlite" + cpp.cxxLanguageVersion: "c++17" Group { name: "cpp" files: [ "books.*", "databasemanager.*", + "*.cpp", + "*.h", + "*.hxx", ] + excludeFiles: odbs.files } Group { + id: odbs name: "odb" files: [ "author_s.h", "book_s.h", ] - fileTags: ["odb"] + fileTags: ["hpp", "odbxx", "odb"] } - cpp.includePaths: [ - "/usr/include", // Общие заголовки - "/usr/include/odb" // Заголовки ODB - ] - - // Подключаем библиотеки ODB - cpp.libraryPaths: [ - "/usr/lib" // Путь к библиотекам - ] - cpp.dynamicLibraries: [ + "odb-sqlite", + "odb-qt", "odb", - "odb-sqlite" + "sqlite3" ] } diff --git a/src/database/database_global.h b/src/database/database_global.h new file mode 100644 index 0000000..35e570f --- /dev/null +++ b/src/database/database_global.h @@ -0,0 +1,12 @@ +#ifndef DATABASE_GLOBAL_H +#define DATABASE_GLOBAL_H + +#include + +#if defined(DATABASE_LIBRARY) +#define DATABASE_EXPORT Q_DECL_EXPORT +#else +#define DATABASE_EXPORT Q_DECL_IMPORT +#endif + +#endif // DATABASE_GLOBAL_H diff --git a/src/database/databasemanager.h b/src/database/databasemanager.h index 5d1b844..9e7530e 100644 --- a/src/database/databasemanager.h +++ b/src/database/databasemanager.h @@ -1,6 +1,8 @@ #ifndef DATABASEMANAGER_H #define DATABASEMANAGER_H +#include "database_global.h" + #include #include @@ -35,7 +37,7 @@ struct SelectBuilder } }; -class Q_DECL_EXPORT DatabaseManager +class DATABASE_EXPORT DatabaseManager { public: static DatabaseManager& instance(); // Singleton diff --git a/src/main.cpp b/src/main.cpp index f9a7275..224b3c9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,5 +1,8 @@ #include +#include +#include + #include /* Опыты с odb */ @@ -12,36 +15,34 @@ #include -using unidb = std::unique_ptr; - void fillBooksBD(unidb& db) { try { - std::vector> authors = { - std::make_shared("George", "Orwell", 142), - std::make_shared("J.K.", "Rowling", 56), - std::make_shared("J.R.R.", "Tolkien", 81), - std::make_shared("Leo", "Tolstoy", 189), - std::make_shared("Fyodor", "Dostoevsky", 174), - std::make_shared("Mark", "Twain", 183), - std::make_shared("Charles", "Dickens", 208), - std::make_shared("Virginia", "Woolf", 134), - std::make_shared("Ernest", "Hemingway", 122), - std::make_shared("Gabriel", "García Márquez", 98), - std::make_shared("Franz", "Kafka", 100), - std::make_shared("Harper", "Lee", 64), - std::make_shared("William", "Shakespeare", 459), - std::make_shared("Oscar", "Wilde", 155), - std::make_shared("Aldous", "Huxley", 123), - std::make_shared("Jane", "Austen", 210), - std::make_shared("John", "Steinbeck", 116), - std::make_shared("Agatha", "Christie", 124), - std::make_shared("Isaac", "Asimov", 105) + std::vector> authors = { + QSharedPointer::create("George", "Orwell", 142), + QSharedPointer::create("J.K.", "Rowling", 56), + QSharedPointer::create("J.R.R.", "Tolkien", 81), + QSharedPointer::create("Leo", "Tolstoy", 189), + QSharedPointer::create("Fyodor", "Dostoevsky", 174), + QSharedPointer::create("Mark", "Twain", 183), + QSharedPointer::create("Charles", "Dickens", 208), + QSharedPointer::create("Virginia", "Woolf", 134), + QSharedPointer::create("Ernest", "Hemingway", 122), + QSharedPointer::create("Gabriel", "García Márquez", 98), + QSharedPointer::create("Franz", "Kafka", 100), + QSharedPointer::create("Harper", "Lee", 64), + QSharedPointer::create("William", "Shakespeare", 459), + QSharedPointer::create("Oscar", "Wilde", 155), + QSharedPointer::create("Aldous", "Huxley", 123), + QSharedPointer::create("Jane", "Austen", 210), + QSharedPointer::create("John", "Steinbeck", 116), + QSharedPointer::create("Agatha", "Christie", 124), + QSharedPointer::create("Isaac", "Asimov", 105) }; // Список книг - std::vector books = { + QVector books = { Book_S("1984", authors[0], 1949), Book_S("Harry Potter and the Philosopher's Stone", authors[1], 1997), Book_S("The Hobbit", authors[2], 1937), @@ -98,7 +99,7 @@ void fillBooksBD(unidb& db) { odb::core::transaction t(db->begin()); for (auto& author : authors) - db->persist(author.get()); // Используем get() для получения сырого указателя + db->persist(author); // Используем get() для получения сырого указателя t.commit(); } @@ -123,9 +124,9 @@ int main(int argc, char* argv[]) QCoreApplication a(argc, argv); const std::string dbPath = "test_db.sqlite"; - std::unique_ptr db(openDB(dbPath)); + unidb db(openDB(dbPath)); - // fillBooksBD(db); + fillBooksBD(db); RestApiServer server(*db); server.start(8080); diff --git a/src/restapi/restapi.qbs b/src/restapi/restapi.qbs index 6d99fdb..ea4c7a5 100644 --- a/src/restapi/restapi.qbs +++ b/src/restapi/restapi.qbs @@ -3,7 +3,7 @@ \inherits Project \brief Описание */ -PSLibrary { +WPSLibrary { cpp.defines: [ // You can make your code fail to compile if it uses deprecated APIs. // In order to do so, uncomment the following line. @@ -15,10 +15,8 @@ PSLibrary { Depends { name: "Qt"; submodules: [ "core", "sql", "network" ] } Depends { name: "cpp" } Depends { name: "database" } - Depends { name: "odbModule"} - - - // Depends { name: "redkit_gen" } + Depends { name: "odb.gen" } + Depends { name: "redkit_gen" } cpp.cxxLanguageVersion: "c++20" diff --git a/src/restapi/restapiserver.cpp b/src/restapi/restapiserver.cpp index da06f3d..30ad780 100644 --- a/src/restapi/restapiserver.cpp +++ b/src/restapi/restapiserver.cpp @@ -5,10 +5,11 @@ #include #include +#include // Должен быть здесь +#include +#include // Должен быть здесь #include -#include "database/book_s-odb.hxx" - #include #include #include @@ -57,6 +58,7 @@ QByteArray RestApiServer::processRequest(const QString& request) { QString author = request.section(' ', 1, 1).section('/', 3, 3).replace("%20", " "); + qWarning() << "THIS"; // Books books; // QList results = books.getBooksByAuthor(author); @@ -72,7 +74,7 @@ QByteArray RestApiServer::processRequest(const QString& request) // } // if (results.size() > 0) // jsonResponse.chop(1); // Убираем последнюю запятую - // jsonResponse += "] }"; + jsonResponse += "] }"; return "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n" + jsonResponse; } @@ -90,8 +92,8 @@ QByteArray RestApiServer::processRequest(const QString& request) QJsonObject j; j["id"] = QString::number(book.id()); - j["title"] = QString::fromStdString(book.name()); - j["author"] = QString::fromStdString(book.author()->full_name()); + j["title"] = book.name(); + j["author"] = book.author()->full_name(); j["year"] = book.year(); jArray.push_back(j); } @@ -115,8 +117,8 @@ QByteArray RestApiServer::processRequest(const QString& request) QJsonObject j; j["id"] = QString::number(author.id()); - j["first"] = QString::fromStdString(author.first()); - j["last"] = QString::fromStdString(author.last()); + j["first"] = author.first(); + j["last"] = author.last(); j["age"] = author.age(); jArray.push_back(j); }