Все еще запрашиваем все записи из БД...

This commit is contained in:
2025-08-03 10:35:31 +05:00
parent a7d667a2df
commit 8c79d35a7d
4 changed files with 88 additions and 20 deletions

View File

@@ -18,33 +18,34 @@ class DATABASE_EXPORT Author_S
public:
Author_S() = default;
Author_S(const QString& first,
const QString& last,
Author_S(const QString& firstN,
const QString& lastN,
const unsigned short age) :
m_first(first),
m_last(last),
m_firstName(firstN),
m_lastName(lastN),
m_age(age)
{
}
unsigned long long id() const { return m_id; }
QString first() const { return m_first; }
QString last() const { return m_last; }
QString firstName() const { return m_firstName; }
QString lastName() const { return m_lastName; }
unsigned short age() const { return m_age; }
QString full_name() const { return m_first + " " + m_last; }
QString full_name() const { return m_firstName + " " + m_lastName; }
void age(unsigned short age) { m_age = age; }
private:
friend class odb::access;
private:
#pragma db id auto
unsigned long long m_id;
quint64 m_id;
QString m_first;
QString m_last;
unsigned short m_age;
QString m_firstName;
QString m_lastName;
qint8 m_age;
};
#pragma db view object(Author_S)

View File

@@ -38,11 +38,27 @@ private:
private:
#pragma db id auto
unsigned long long m_id;
quint64 m_id;
SH<Author_S> m_author;
QString m_name;
int m_year;
qint8 m_year;
};
// #pragma db view object(Book_S) object(Author_S = author:Book_S::m_author)
// struct BookByAuthorView
// {
// #pragma db column(Book_S::m_id)
// quint64 book_id;
// #pragma db column(Book_S::m_name)
// QString book_name;
// #pragma db column(Book_S::m_year)
// qint8 year;
// #pragma db column(Author_S::m_first + " " + Author_S::m_last)
// QString author_full_name;
// };
#endif // BOOK_S_H

View File

@@ -15,17 +15,19 @@ PSLibrary {
Depends { name: "Qt"; submodules: [ "core", "sql", "network" ] }
Depends { name: "cpp" }
Depends { name: "odb.gen" }
Depends { name: "redkit_gen" }
Depends { name: "rdbase" }
Depends { name: "redkit_gen" }
redkit_gen.includeModules: ["JsonSerializer"]
odb.gen.databases: "sqlite"
cpp.cxxLanguageVersion: "c++17"
Group {
name: "cpp"
files: [
"books.*",
"databasemanager.*",
"*.cpp",
"*.h",
@@ -41,7 +43,7 @@ PSLibrary {
"author_s.h",
"book_s.h",
]
fileTags: ["hpp", "odbxx", "odb"]
fileTags: ["hpp", "odbxx"]
}
cpp.dynamicLibraries: [

View File

@@ -14,6 +14,8 @@
#include <odb/database.hxx>
#include <odb/query.hxx>
#include <redkit-gen-modules/json_serializer.h>
RestApiServer::RestApiServer(odb::database& db, QObject* parent) :
QTcpServer(parent), m_db(db) {}
@@ -61,9 +63,56 @@ QByteArray RestApiServer::processRequest(const QString& request)
{
QString author = request.section(' ', 1, 1).section('/', 3, 3).replace("%20", " ");
qWarning() << "THIS";
quint64 ageReq = author.toInt();
qWarning() << "THIS" << author << ageReq;
// if (dbConn.query_one<SDB_T>(odb::query<SDB_T>::object == m_objectId, sett))
// odb::result<Book_S> res(m_db.query<Book_S>(odb::query<Book_S>::author-> == author));
// {
QStringList nameParts = author.split(' ');
QString firstName = nameParts.size() > 0 ? nameParts[0] : "";
QString lastName = nameParts.size() > 1 ? nameParts[1] : "";
qWarning()
<< "author:" << author << "\n"
<< "firstName:" << firstName << "\n"
<< "lastName:" << lastName << "\n";
// Result result(m_db.query<Book_S>(
// (Query::author->m_first + " " + Query::author->m_last == authorName) ||
// (Query::author->m_first.like("%" + firstName + "%") &&
// Query::author->m_last.like("%" + lastName + "%"))
// ));
odb::transaction t(m_db.begin());
odb::result<Book_S> res(m_db.query<Book_S>());
auto books = m_db.query<Book_S>();
// using Query = odb::query<Author_S>;
// auto authors = m_db.query<Author_S>(Query::id == 2);
auto authors = m_db.query<Author_S>();
for (auto book : books)
qWarning() << book.name() << book.author()->full_name();
for (auto author : authors)
qWarning() << author.full_name() << author.age();
// std::vector<SH<Book_S>> books(result.begin(), result.end());
t.commit();
// return books;
// }
// for (auto book : books)
// qWarning() << book.author()->full_name();
// qWarning() << books;
// QList<BookRecord> results = books.getBooksByAuthor(author);
QByteArray jsonResponse = "{ \"books\": [";
@@ -121,8 +170,8 @@ QByteArray RestApiServer::processRequest(const QString& request)
QJsonObject j;
j["id"] = QString::number(author.id());
j["first"] = author.first();
j["last"] = author.last();
j["first"] = author.firstName();
j["last"] = author.lastName();
j["age"] = author.age();
jArray.push_back(j);
}