Подключаем еще некоторые привычные вещи работы с odb

This commit is contained in:
2025-08-03 08:14:55 +05:00
parent f4958e60bb
commit a7d667a2df
10 changed files with 122 additions and 47 deletions

View File

@@ -20,6 +20,7 @@ PSApplication {
Depends { name: "sqlite3" }
Depends { name: "restapi" }
Depends { name: "redkit_gen" }
Depends { name: "rdbase" }
cpp.cxxLanguageVersion: "c++20"

View File

@@ -6,10 +6,10 @@
#include "database_global.h"
#include <QString>
#include <QSharedPointer>
#include <QString>
#include <odb/core.hxx>
#include <rcore/smarttypes.h>
#include "author_s.h"
@@ -19,8 +19,8 @@ class DATABASE_EXPORT Book_S
public:
Book_S() = default;
Book_S(const QString & name,
const QSharedPointer<Author_S>& author,
Book_S(const QString& name,
const SH<Author_S>& author,
const int year)
{
m_author = author;
@@ -29,8 +29,8 @@ public:
};
unsigned long long id() const { return m_id; }
QSharedPointer<Author_S> author() const { return m_author; }
QString name() const { return m_name; }
SH<Author_S> 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;
QSharedPointer<Author_S> m_author;
QString m_name;
SH<Author_S> m_author;
QString m_name;
int m_year;
};

View File

@@ -34,19 +34,20 @@
#include <filesystem>
#include <random>
#include <rcore/smarttypes.h>
using odatabase = odb::sqlite::database;
using unidb = std::unique_ptr<odatabase>;
using oDBase = odb::sqlite::database;
using uDBase = U<oDBase>;
struct testData
{
std::string first;
std::string last;
QString first;
QString last;
int age;
};
// Функция для генерации случайной строки (имени или фамилии)
std::string generate_random_string(const std::vector<std::string>& pool)
QString generate_random_string(const QVector<QString>& pool)
{
static std::random_device rd;
static std::mt19937 gen(rd());
@@ -63,16 +64,16 @@ int generate_random_year(int min_year = 1900, int max_year = 2020)
return dis(gen);
}
std::vector<testData> fillDB()
QVector<testData> fillDB()
{
std::vector<std::string> first_names = {
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"
};
std::vector<std::string> last_names = {
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",
@@ -80,12 +81,12 @@ std::vector<testData> fillDB()
"Rogers", "Gutierrez", "Ramirez", "Diaz", "Perez", "Ross", "Sanders", "Price", "Howard", "Cooper"
};
std::vector<testData> vecTest;
QVector<testData> vecTest;
for (int i = 0; i < 50; ++i)
{
std::string first_name = generate_random_string(first_names);
std::string last_name = generate_random_string(last_names);
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 });
@@ -94,16 +95,16 @@ std::vector<testData> fillDB()
return vecTest;
}
inline unidb openDB(const std::string path_db)
inline uDBase openDB(const std::string path_db)
{
// Открыть БД, если она существует
auto db_ptr = new odatabase(path_db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE);
auto db_ptr = new oDBase(path_db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE);
if (std::filesystem::is_regular_file(path_db))
return unidb(db_ptr);
return uDBase(db_ptr);
// И создать новую, если не существует
unidb db(db_ptr);
uDBase db(db_ptr);
odb::connection_ptr connect_ptr(db->connection());

View File

@@ -17,6 +17,7 @@ PSLibrary {
Depends { name: "cpp" }
Depends { name: "odb.gen" }
Depends { name: "redkit_gen" }
Depends { name: "rdbase" }
odb.gen.databases: "sqlite"
cpp.cxxLanguageVersion: "c++17"

View File

@@ -2,6 +2,8 @@
#include <QPointer>
#include <QSharedPointer>
#include <QString>
#include <QVector>
#include <restapi/restapiserver.h>
@@ -15,30 +17,30 @@
#include <iostream>
void fillBooksBD(unidb& db)
void fillBooksBD(uDBase& db)
{
try
{
std::vector<QSharedPointer<Author_S>> authors = {
QSharedPointer<Author_S>::create("George", "Orwell", 142),
QSharedPointer<Author_S>::create("J.K.", "Rowling", 56),
QSharedPointer<Author_S>::create("J.R.R.", "Tolkien", 81),
QSharedPointer<Author_S>::create("Leo", "Tolstoy", 189),
QSharedPointer<Author_S>::create("Fyodor", "Dostoevsky", 174),
QSharedPointer<Author_S>::create("Mark", "Twain", 183),
QSharedPointer<Author_S>::create("Charles", "Dickens", 208),
QSharedPointer<Author_S>::create("Virginia", "Woolf", 134),
QSharedPointer<Author_S>::create("Ernest", "Hemingway", 122),
QSharedPointer<Author_S>::create("Gabriel", "García Márquez", 98),
QSharedPointer<Author_S>::create("Franz", "Kafka", 100),
QSharedPointer<Author_S>::create("Harper", "Lee", 64),
QSharedPointer<Author_S>::create("William", "Shakespeare", 459),
QSharedPointer<Author_S>::create("Oscar", "Wilde", 155),
QSharedPointer<Author_S>::create("Aldous", "Huxley", 123),
QSharedPointer<Author_S>::create("Jane", "Austen", 210),
QSharedPointer<Author_S>::create("John", "Steinbeck", 116),
QSharedPointer<Author_S>::create("Agatha", "Christie", 124),
QSharedPointer<Author_S>::create("Isaac", "Asimov", 105)
QVector<SH<Author_S>> authors = {
SH<Author_S>::create("George", "Orwell", 142),
SH<Author_S>::create("J.K.", "Rowling", 56),
SH<Author_S>::create("J.R.R.", "Tolkien", 81),
SH<Author_S>::create("Leo", "Tolstoy", 189),
SH<Author_S>::create("Fyodor", "Dostoevsky", 174),
SH<Author_S>::create("Mark", "Twain", 183),
SH<Author_S>::create("Charles", "Dickens", 208),
SH<Author_S>::create("Virginia", "Woolf", 134),
SH<Author_S>::create("Ernest", "Hemingway", 122),
SH<Author_S>::create("Gabriel", "García Márquez", 98),
SH<Author_S>::create("Franz", "Kafka", 100),
SH<Author_S>::create("Harper", "Lee", 64),
SH<Author_S>::create("William", "Shakespeare", 459),
SH<Author_S>::create("Oscar", "Wilde", 155),
SH<Author_S>::create("Aldous", "Huxley", 123),
SH<Author_S>::create("Jane", "Austen", 210),
SH<Author_S>::create("John", "Steinbeck", 116),
SH<Author_S>::create("Agatha", "Christie", 124),
SH<Author_S>::create("Isaac", "Asimov", 105)
};
// Список книг
@@ -124,7 +126,7 @@ int main(int argc, char* argv[])
QCoreApplication a(argc, argv);
const std::string dbPath = "test_db.sqlite";
unidb db(openDB(dbPath));
uDBase db(openDB(dbPath));
fillBooksBD(db);

View File

@@ -17,6 +17,7 @@ WPSLibrary {
Depends { name: "database" }
Depends { name: "odb.gen" }
Depends { name: "redkit_gen" }
Depends { name: "rdbase" }
cpp.cxxLanguageVersion: "c++20"

View File

@@ -25,7 +25,8 @@ void RestApiServer::start(quint16 port)
}
else
{
qDebug() << "REST API сервер запущен на порту:" << port;
QString addrs = "http://127.0.0.1:" + QString::number(this->serverPort());
qDebug() << "REST API сервер запущен по адресу:" << addrs;
}
}
@@ -36,6 +37,8 @@ void RestApiServer::incomingConnection(qintptr socketDescriptor)
connect(socket, &QTcpSocket::readyRead, this, &RestApiServer::handleRequest);
connect(socket, &QTcpSocket::disconnected, socket, &QTcpSocket::deleteLater);
qWarning() << "Есть входящее подключение" << socket->socketDescriptor();
}
void RestApiServer::handleRequest()
@@ -59,7 +62,8 @@ QByteArray RestApiServer::processRequest(const QString& request)
QString author = request.section(' ', 1, 1).section('/', 3, 3).replace("%20", " ");
qWarning() << "THIS";
// Books books;
odb::transaction t(m_db.begin());
odb::result<Book_S> res(m_db.query<Book_S>());
// QList<BookRecord> results = books.getBooksByAuthor(author);
QByteArray jsonResponse = "{ \"books\": [";