Билдим с odb из redkitty

This commit is contained in:
2025-08-03 00:14:45 +05:00
parent 3c2b8e7cf4
commit f4958e60bb
14 changed files with 117 additions and 158 deletions

3
.gitmodules vendored
View File

@@ -1,3 +1,6 @@
[submodule "redkbuild"] [submodule "redkbuild"]
path = redkbuild path = redkbuild
url = ssh://git@gitea.redkit-lab.work:57322/redkit-lab/redkbuild.git 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

View File

@@ -3,14 +3,9 @@ import qbs
Project { Project {
name: "opds++" name: "opds++"
qbsSearchPaths: [ qbsSearchPaths: [
"qbs", "redkitty/qbs",
// "redkitty/qbs",
// // "guiness/qbs",
// // "prerequisites/qbs",
// // "report/qbs",
"redkbuild", "redkbuild",
// // "ext_libs/openssl/qbs", "redkitty/lib/redkit-gen-integration/qbs",
// "redkitty/lib/redkit-gen-integration/qbs",
] ]
references: [ references: [

View File

@@ -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"
]
}

1
redkitty Submodule

Submodule redkitty added at 2316ddea72

View File

@@ -3,7 +3,7 @@
\inherits Project \inherits Project
\brief Описание \brief Описание
*/ */
Application { PSApplication {
// CppApplication { // CppApplication {
cpp.defines: [ cpp.defines: [
// You can make your code fail to compile if it uses deprecated APIs. // 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: "Qt"; submodules: [ "core", "sql", "network" ] }
Depends { name: "cpp" } Depends { name: "cpp" }
Depends { name: "database" } Depends { name: "database" }
Depends { name: "odbModule"} Depends { name: "odb.gen" }
Depends { name: "sqlite3" }
// Depends { name: "redkit_gen" } Depends { name: "restapi" }
Depends { name: "redkit_gen" }
cpp.cxxLanguageVersion: "c++20" cpp.cxxLanguageVersion: "c++20"
@@ -29,6 +30,12 @@ Application {
] ]
} }
cpp.dynamicLibraries: [
"odb-sqlite",
"odb-qt",
"odb",
"sqlite3"
]
// } // }
} // Project } // Project

View File

@@ -4,24 +4,22 @@
#ifndef AUTHOR_S_H #ifndef AUTHOR_S_H
#define AUTHOR_S_H #define AUTHOR_S_H
// #include <QtCore/QObject> #include "database_global.h"
#include <cstddef> // std::size_t #include <QString>
#include <memory>
#include <string>
#include <odb/core.hxx> #include <odb/core.hxx>
#include <odb/database.hxx> #include <odb/database.hxx>
#include <odb/query.hxx> #include <odb/query.hxx>
#pragma db object #pragma db object
class __attribute__((visibility("default"))) Author_S class DATABASE_EXPORT Author_S
{ {
public: public:
Author_S() = default; Author_S() = default;
Author_S(const std::string& first, Author_S(const QString& first,
const std::string& last, const QString& last,
const unsigned short age) : const unsigned short age) :
m_first(first), m_first(first),
m_last(last), m_last(last),
@@ -30,11 +28,11 @@ public:
} }
unsigned long long id() const { return m_id; } unsigned long long id() const { return m_id; }
std::string first() const { return m_first; } QString first() const { return m_first; }
std::string last() const { return m_last; } QString last() const { return m_last; }
unsigned short age() const { return m_age; } 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; } void age(unsigned short age) { m_age = age; }
@@ -44,8 +42,8 @@ private:
#pragma db id auto #pragma db id auto
unsigned long long m_id; unsigned long long m_id;
std::string m_first; QString m_first;
std::string m_last; QString m_last;
unsigned short m_age; unsigned short m_age;
}; };

View File

@@ -4,23 +4,23 @@
#ifndef BOOK_S_H #ifndef BOOK_S_H
#define BOOK_S_H #define BOOK_S_H
// #include <QtCore/QObject> #include "database_global.h"
#include <memory> #include <QString>
#include <string> #include <QSharedPointer>
#include <odb/core.hxx> #include <odb/core.hxx>
#include "author_s.h" #include "author_s.h"
#pragma db object #pragma db object
class __attribute__((visibility("default"))) Book_S class DATABASE_EXPORT Book_S
{ {
public: public:
Book_S() = default; Book_S() = default;
Book_S(const std::string& name, Book_S(const QString & name,
const std::shared_ptr<Author_S>& author, const QSharedPointer<Author_S>& author,
const int year) const int year)
{ {
m_author = author; m_author = author;
@@ -29,8 +29,8 @@ public:
}; };
unsigned long long id() const { return m_id; } unsigned long long id() const { return m_id; }
std::shared_ptr<Author_S> author() const { return m_author; } QSharedPointer<Author_S> author() const { return m_author; }
std::string name() const { return m_name; } QString name() const { return m_name; }
int year() const { return m_year; } int year() const { return m_year; }
private: private:
@@ -40,8 +40,8 @@ private:
#pragma db id auto #pragma db id auto
unsigned long long m_id; unsigned long long m_id;
std::shared_ptr<Author_S> m_author; QSharedPointer<Author_S> m_author;
std::string m_name; QString m_name;
int m_year; int m_year;
}; };

View File

@@ -35,6 +35,9 @@
#include <filesystem> #include <filesystem>
#include <random> #include <random>
using odatabase = odb::sqlite::database;
using unidb = std::unique_ptr<odatabase>;
struct testData struct testData
{ {
std::string first; std::string first;
@@ -91,21 +94,23 @@ std::vector<testData> fillDB()
return vecTest; return vecTest;
} }
inline std::unique_ptr<odb::database> 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)) if (std::filesystem::is_regular_file(path_db))
return std::unique_ptr<odb::core::database>(new odb::sqlite::database(path_db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE)); return unidb(db_ptr);
// И создать новую, если не существует // И создать новую, если не существует
std::unique_ptr<odb::core::database> 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"); connect_ptr->execute("PRAGMA foreign_keys=OFF");
odb::core::transaction tans(db->begin()); odb::transaction tans(db->begin());
odb::core::schema_catalog::create_schema(*db); odb::schema_catalog::create_schema(*db);
tans.commit(); tans.commit();
connect_ptr->execute("PRAGMA foreign_keys=ON"); connect_ptr->execute("PRAGMA foreign_keys=ON");

View File

@@ -8,45 +8,45 @@ PSLibrary {
// You can make your code fail to compile if it uses deprecated APIs. // You can make your code fail to compile if it uses deprecated APIs.
// In order to do so, uncomment the following line. // In order to do so, uncomment the following line.
//"QT_DISABLE_DEPRECATED_BEFORE=0x060000" // disables all the APIs deprecated before Qt 6.0.0 //"QT_DISABLE_DEPRECATED_BEFORE=0x060000" // disables all the APIs deprecated before Qt 6.0.0
"DATABASE_SQLITE" "DATABASE_SQLITE",
"DATABASE_LIBRARY"
] ]
consoleApplication: true consoleApplication: true
Depends { name: "Qt"; submodules: [ "core", "sql", "network" ] } Depends { name: "Qt"; submodules: [ "core", "sql", "network" ] }
Depends { name: "cpp" } 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 { Group {
name: "cpp" name: "cpp"
files: [ files: [
"books.*", "books.*",
"databasemanager.*", "databasemanager.*",
"*.cpp",
"*.h",
"*.hxx",
] ]
excludeFiles: odbs.files
} }
Group { Group {
id: odbs
name: "odb" name: "odb"
files: [ files: [
"author_s.h", "author_s.h",
"book_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: [ cpp.dynamicLibraries: [
"odb-sqlite",
"odb-qt",
"odb", "odb",
"odb-sqlite" "sqlite3"
] ]
} }

View File

@@ -0,0 +1,12 @@
#ifndef DATABASE_GLOBAL_H
#define DATABASE_GLOBAL_H
#include <QtCore/qglobal.h>
#if defined(DATABASE_LIBRARY)
#define DATABASE_EXPORT Q_DECL_EXPORT
#else
#define DATABASE_EXPORT Q_DECL_IMPORT
#endif
#endif // DATABASE_GLOBAL_H

View File

@@ -1,6 +1,8 @@
#ifndef DATABASEMANAGER_H #ifndef DATABASEMANAGER_H
#define DATABASEMANAGER_H #define DATABASEMANAGER_H
#include "database_global.h"
#include <QDebug> #include <QDebug>
#include <QtSql> #include <QtSql>
@@ -35,7 +37,7 @@ struct SelectBuilder
} }
}; };
class Q_DECL_EXPORT DatabaseManager class DATABASE_EXPORT DatabaseManager
{ {
public: public:
static DatabaseManager& instance(); // Singleton static DatabaseManager& instance(); // Singleton

View File

@@ -1,5 +1,8 @@
#include <QCoreApplication> #include <QCoreApplication>
#include <QPointer>
#include <QSharedPointer>
#include <restapi/restapiserver.h> #include <restapi/restapiserver.h>
/* Опыты с odb */ /* Опыты с odb */
@@ -12,36 +15,34 @@
#include <iostream> #include <iostream>
using unidb = std::unique_ptr<odb::database>;
void fillBooksBD(unidb& db) void fillBooksBD(unidb& db)
{ {
try try
{ {
std::vector<std::shared_ptr<Author_S>> authors = { std::vector<QSharedPointer<Author_S>> authors = {
std::make_shared<Author_S>("George", "Orwell", 142), QSharedPointer<Author_S>::create("George", "Orwell", 142),
std::make_shared<Author_S>("J.K.", "Rowling", 56), QSharedPointer<Author_S>::create("J.K.", "Rowling", 56),
std::make_shared<Author_S>("J.R.R.", "Tolkien", 81), QSharedPointer<Author_S>::create("J.R.R.", "Tolkien", 81),
std::make_shared<Author_S>("Leo", "Tolstoy", 189), QSharedPointer<Author_S>::create("Leo", "Tolstoy", 189),
std::make_shared<Author_S>("Fyodor", "Dostoevsky", 174), QSharedPointer<Author_S>::create("Fyodor", "Dostoevsky", 174),
std::make_shared<Author_S>("Mark", "Twain", 183), QSharedPointer<Author_S>::create("Mark", "Twain", 183),
std::make_shared<Author_S>("Charles", "Dickens", 208), QSharedPointer<Author_S>::create("Charles", "Dickens", 208),
std::make_shared<Author_S>("Virginia", "Woolf", 134), QSharedPointer<Author_S>::create("Virginia", "Woolf", 134),
std::make_shared<Author_S>("Ernest", "Hemingway", 122), QSharedPointer<Author_S>::create("Ernest", "Hemingway", 122),
std::make_shared<Author_S>("Gabriel", "García Márquez", 98), QSharedPointer<Author_S>::create("Gabriel", "García Márquez", 98),
std::make_shared<Author_S>("Franz", "Kafka", 100), QSharedPointer<Author_S>::create("Franz", "Kafka", 100),
std::make_shared<Author_S>("Harper", "Lee", 64), QSharedPointer<Author_S>::create("Harper", "Lee", 64),
std::make_shared<Author_S>("William", "Shakespeare", 459), QSharedPointer<Author_S>::create("William", "Shakespeare", 459),
std::make_shared<Author_S>("Oscar", "Wilde", 155), QSharedPointer<Author_S>::create("Oscar", "Wilde", 155),
std::make_shared<Author_S>("Aldous", "Huxley", 123), QSharedPointer<Author_S>::create("Aldous", "Huxley", 123),
std::make_shared<Author_S>("Jane", "Austen", 210), QSharedPointer<Author_S>::create("Jane", "Austen", 210),
std::make_shared<Author_S>("John", "Steinbeck", 116), QSharedPointer<Author_S>::create("John", "Steinbeck", 116),
std::make_shared<Author_S>("Agatha", "Christie", 124), QSharedPointer<Author_S>::create("Agatha", "Christie", 124),
std::make_shared<Author_S>("Isaac", "Asimov", 105) QSharedPointer<Author_S>::create("Isaac", "Asimov", 105)
}; };
// Список книг // Список книг
std::vector<Book_S> books = { QVector<Book_S> books = {
Book_S("1984", authors[0], 1949), Book_S("1984", authors[0], 1949),
Book_S("Harry Potter and the Philosopher's Stone", authors[1], 1997), Book_S("Harry Potter and the Philosopher's Stone", authors[1], 1997),
Book_S("The Hobbit", authors[2], 1937), Book_S("The Hobbit", authors[2], 1937),
@@ -98,7 +99,7 @@ void fillBooksBD(unidb& db)
{ {
odb::core::transaction t(db->begin()); odb::core::transaction t(db->begin());
for (auto& author : authors) for (auto& author : authors)
db->persist(author.get()); // Используем get() для получения сырого указателя db->persist(author); // Используем get() для получения сырого указателя
t.commit(); t.commit();
} }
@@ -123,9 +124,9 @@ int main(int argc, char* argv[])
QCoreApplication a(argc, argv); QCoreApplication a(argc, argv);
const std::string dbPath = "test_db.sqlite"; const std::string dbPath = "test_db.sqlite";
std::unique_ptr<odb::database> db(openDB(dbPath)); unidb db(openDB(dbPath));
// fillBooksBD(db); fillBooksBD(db);
RestApiServer server(*db); RestApiServer server(*db);
server.start(8080); server.start(8080);

View File

@@ -3,7 +3,7 @@
\inherits Project \inherits Project
\brief Описание \brief Описание
*/ */
PSLibrary { WPSLibrary {
cpp.defines: [ cpp.defines: [
// You can make your code fail to compile if it uses deprecated APIs. // You can make your code fail to compile if it uses deprecated APIs.
// In order to do so, uncomment the following line. // In order to do so, uncomment the following line.
@@ -15,10 +15,8 @@ PSLibrary {
Depends { name: "Qt"; submodules: [ "core", "sql", "network" ] } Depends { name: "Qt"; submodules: [ "core", "sql", "network" ] }
Depends { name: "cpp" } Depends { name: "cpp" }
Depends { name: "database" } Depends { name: "database" }
Depends { name: "odbModule"} Depends { name: "odb.gen" }
Depends { name: "redkit_gen" }
// Depends { name: "redkit_gen" }
cpp.cxxLanguageVersion: "c++20" cpp.cxxLanguageVersion: "c++20"

View File

@@ -5,10 +5,11 @@
#include <QJsonObject> #include <QJsonObject>
#include <QJsonValue> #include <QJsonValue>
#include <database/author_s-odb.hxx> // Должен быть здесь
#include <database/author_s.h>
#include <database/book_s-odb.hxx> // Должен быть здесь
#include <database/book_s.h> #include <database/book_s.h>
#include "database/book_s-odb.hxx"
#include <odb/core.hxx> #include <odb/core.hxx>
#include <odb/database.hxx> #include <odb/database.hxx>
#include <odb/query.hxx> #include <odb/query.hxx>
@@ -57,6 +58,7 @@ QByteArray RestApiServer::processRequest(const QString& request)
{ {
QString author = request.section(' ', 1, 1).section('/', 3, 3).replace("%20", " "); QString author = request.section(' ', 1, 1).section('/', 3, 3).replace("%20", " ");
qWarning() << "THIS";
// Books books; // Books books;
// QList<BookRecord> results = books.getBooksByAuthor(author); // QList<BookRecord> results = books.getBooksByAuthor(author);
@@ -72,7 +74,7 @@ QByteArray RestApiServer::processRequest(const QString& request)
// } // }
// if (results.size() > 0) // if (results.size() > 0)
// jsonResponse.chop(1); // Убираем последнюю запятую // jsonResponse.chop(1); // Убираем последнюю запятую
// jsonResponse += "] }"; jsonResponse += "] }";
return "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n" + 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; QJsonObject j;
j["id"] = QString::number(book.id()); j["id"] = QString::number(book.id());
j["title"] = QString::fromStdString(book.name()); j["title"] = book.name();
j["author"] = QString::fromStdString(book.author()->full_name()); j["author"] = book.author()->full_name();
j["year"] = book.year(); j["year"] = book.year();
jArray.push_back(j); jArray.push_back(j);
} }
@@ -115,8 +117,8 @@ QByteArray RestApiServer::processRequest(const QString& request)
QJsonObject j; QJsonObject j;
j["id"] = QString::number(author.id()); j["id"] = QString::number(author.id());
j["first"] = QString::fromStdString(author.first()); j["first"] = author.first();
j["last"] = QString::fromStdString(author.last()); j["last"] = author.last();
j["age"] = author.age(); j["age"] = author.age();
jArray.push_back(j); jArray.push_back(j);
} }