Подключаем еще некоторые привычные вещи работы с odb
This commit is contained in:
8
prerequisites/qbs/imports/SQLiteArtifactsExport.qbs
Normal file
8
prerequisites/qbs/imports/SQLiteArtifactsExport.qbs
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
import qbs
|
||||||
|
|
||||||
|
Export {
|
||||||
|
Depends { name: "cpp" }
|
||||||
|
Depends { name: "rsc.sqlite.settings" }
|
||||||
|
|
||||||
|
rsc.sqlite.settings.includeRegexValues: exportingProduct.includeRegexValue
|
||||||
|
}
|
||||||
53
prerequisites/qbs/modules/karchive/karchive.qbs
Normal file
53
prerequisites/qbs/modules/karchive/karchive.qbs
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
import qbs
|
||||||
|
|
||||||
|
Module {
|
||||||
|
id: karchive
|
||||||
|
|
||||||
|
readonly property string karchive_path: project.sourceDirectory + "/external_libs/karchive"
|
||||||
|
readonly property string libKF5ArchivePath: karchive.karchive_path + "/lib"
|
||||||
|
+ (qbs.buildVariant == "release" ? "/Release" : "/Debug")
|
||||||
|
property bool installFiles: true
|
||||||
|
|
||||||
|
Depends { name: "glob" }
|
||||||
|
Depends { name: "cpp" }
|
||||||
|
Depends { name: "zlib" }
|
||||||
|
|
||||||
|
Properties
|
||||||
|
{
|
||||||
|
condition: qbs.targetOS.contains("linux") || qbs.targetOS.contains("astra")
|
||||||
|
cpp.includePaths: [ karchive.karchive_path + "/include/lin" ]
|
||||||
|
cpp.libraryPaths: [ karchive.libKF5ArchivePath + "/lin" ]
|
||||||
|
cpp.dynamicLibraries: "KF5Archive"
|
||||||
|
}
|
||||||
|
|
||||||
|
Properties {
|
||||||
|
condition: qbs.targetOS.contains("windows")
|
||||||
|
cpp.includePaths: [ karchive.karchive_path + "/include/win" ]
|
||||||
|
cpp.libraryPaths: [ karchive.libKF5ArchivePath + "/win" ]
|
||||||
|
cpp.dynamicLibraries: "KF5Archive"
|
||||||
|
}
|
||||||
|
|
||||||
|
InstallGroup {
|
||||||
|
name: "karchive_lin"
|
||||||
|
condition: qbs.targetOS.contains("linux") || qbs.targetOS.contains("astra")
|
||||||
|
files: [
|
||||||
|
karchive.libKF5ArchivePath + "/lin/libKF5Archive.so",
|
||||||
|
karchive.libKF5ArchivePath + "/lin/libKF5Archive.so.5",
|
||||||
|
karchive.libKF5ArchivePath + "/lin/libKF5Archive.so.5.108.0"
|
||||||
|
]
|
||||||
|
qbs.install: karchive.installFiles
|
||||||
|
qbs.installDir: glob.binInstallPrefix
|
||||||
|
}
|
||||||
|
|
||||||
|
InstallGroup {
|
||||||
|
name: "karchive_win"
|
||||||
|
condition: qbs.targetOS.contains("windows")
|
||||||
|
files: [
|
||||||
|
karchive.libKF5ArchivePath + "/win/KF5Archive.dll",
|
||||||
|
karchive.libKF5ArchivePath + "/win/KF5Archive.lib"
|
||||||
|
]
|
||||||
|
qbs.install: karchive.installFiles
|
||||||
|
qbs.installDir: glob.binInstallPrefix
|
||||||
|
}
|
||||||
|
zlib.installFiles: true
|
||||||
|
}
|
||||||
@@ -6,9 +6,13 @@ Project {
|
|||||||
"redkitty/qbs",
|
"redkitty/qbs",
|
||||||
"redkbuild",
|
"redkbuild",
|
||||||
"redkitty/lib/redkit-gen-integration/qbs",
|
"redkitty/lib/redkit-gen-integration/qbs",
|
||||||
|
"prerequisites/qbs",
|
||||||
]
|
]
|
||||||
|
|
||||||
references: [
|
references: [
|
||||||
|
"redkitty/lib/rsc/libs_impl/rdbaseimpl/rdbaseimpl.qbs",
|
||||||
|
"redkitty/lib/rcore/rcore.qbs",
|
||||||
|
|
||||||
"src/cpp-opds.qbs",
|
"src/cpp-opds.qbs",
|
||||||
"src/database/database.qbs",
|
"src/database/database.qbs",
|
||||||
"src/restapi/restapi.qbs",
|
"src/restapi/restapi.qbs",
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ PSApplication {
|
|||||||
Depends { name: "sqlite3" }
|
Depends { name: "sqlite3" }
|
||||||
Depends { name: "restapi" }
|
Depends { name: "restapi" }
|
||||||
Depends { name: "redkit_gen" }
|
Depends { name: "redkit_gen" }
|
||||||
|
Depends { name: "rdbase" }
|
||||||
|
|
||||||
cpp.cxxLanguageVersion: "c++20"
|
cpp.cxxLanguageVersion: "c++20"
|
||||||
|
|
||||||
|
|||||||
@@ -6,10 +6,10 @@
|
|||||||
|
|
||||||
#include "database_global.h"
|
#include "database_global.h"
|
||||||
|
|
||||||
#include <QString>
|
|
||||||
#include <QSharedPointer>
|
#include <QSharedPointer>
|
||||||
|
#include <QString>
|
||||||
#include <odb/core.hxx>
|
#include <odb/core.hxx>
|
||||||
|
#include <rcore/smarttypes.h>
|
||||||
|
|
||||||
#include "author_s.h"
|
#include "author_s.h"
|
||||||
|
|
||||||
@@ -19,8 +19,8 @@ class DATABASE_EXPORT Book_S
|
|||||||
public:
|
public:
|
||||||
Book_S() = default;
|
Book_S() = default;
|
||||||
|
|
||||||
Book_S(const QString & name,
|
Book_S(const QString& name,
|
||||||
const QSharedPointer<Author_S>& author,
|
const SH<Author_S>& author,
|
||||||
const int year)
|
const int year)
|
||||||
{
|
{
|
||||||
m_author = author;
|
m_author = author;
|
||||||
@@ -29,7 +29,7 @@ public:
|
|||||||
};
|
};
|
||||||
|
|
||||||
unsigned long long id() const { return m_id; }
|
unsigned long long id() const { return m_id; }
|
||||||
QSharedPointer<Author_S> author() const { return m_author; }
|
SH<Author_S> author() const { return m_author; }
|
||||||
QString name() const { return m_name; }
|
QString name() const { return m_name; }
|
||||||
int year() const { return m_year; }
|
int year() const { return m_year; }
|
||||||
|
|
||||||
@@ -40,7 +40,7 @@ private:
|
|||||||
#pragma db id auto
|
#pragma db id auto
|
||||||
unsigned long long m_id;
|
unsigned long long m_id;
|
||||||
|
|
||||||
QSharedPointer<Author_S> m_author;
|
SH<Author_S> m_author;
|
||||||
QString m_name;
|
QString m_name;
|
||||||
int m_year;
|
int m_year;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -34,19 +34,20 @@
|
|||||||
|
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
#include <random>
|
#include <random>
|
||||||
|
#include <rcore/smarttypes.h>
|
||||||
|
|
||||||
using odatabase = odb::sqlite::database;
|
using oDBase = odb::sqlite::database;
|
||||||
using unidb = std::unique_ptr<odatabase>;
|
using uDBase = U<oDBase>;
|
||||||
|
|
||||||
struct testData
|
struct testData
|
||||||
{
|
{
|
||||||
std::string first;
|
QString first;
|
||||||
std::string last;
|
QString last;
|
||||||
int age;
|
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::random_device rd;
|
||||||
static std::mt19937 gen(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);
|
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",
|
"John", "Jane", "Alex", "Chris", "Robert", "Emily", "James", "Linda", "David", "Sarah",
|
||||||
"Michael", "Elizabeth", "Daniel", "Samantha", "William", "Olivia", "Ethan", "Sophia", "Joshua", "Charlotte",
|
"Michael", "Elizabeth", "Daniel", "Samantha", "William", "Olivia", "Ethan", "Sophia", "Joshua", "Charlotte",
|
||||||
"Daniel", "Grace", "Benjamin", "Isabella", "Matthew", "Victoria", "Henry", "Abigail", "Samuel", "Megan",
|
"Daniel", "Grace", "Benjamin", "Isabella", "Matthew", "Victoria", "Henry", "Abigail", "Samuel", "Megan",
|
||||||
"Lucas", "Lily", "Andrew", "Madison", "Jackson", "Chloe", "Aiden", "Amelia", "Thomas", "Natalie",
|
"Lucas", "Lily", "Andrew", "Madison", "Jackson", "Chloe", "Aiden", "Amelia", "Thomas", "Natalie",
|
||||||
"Ryan", "Zoe", "Jack", "Harper", "Elijah", "Ava", "Isaac", "Mia", "Caleb", "Ella"
|
"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",
|
"Doe", "Smith", "Johnson", "Williams", "Jones", "Brown", "Davis", "Miller", "Wilson", "Moore",
|
||||||
"Taylor", "Anderson", "Thomas", "Jackson", "White", "Harris", "Martin", "Thompson", "Garcia", "Martinez",
|
"Taylor", "Anderson", "Thomas", "Jackson", "White", "Harris", "Martin", "Thompson", "Garcia", "Martinez",
|
||||||
"Roberts", "Clark", "Lewis", "Walker", "Young", "Allen", "King", "Wright", "Scott", "Adams",
|
"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"
|
"Rogers", "Gutierrez", "Ramirez", "Diaz", "Perez", "Ross", "Sanders", "Price", "Howard", "Cooper"
|
||||||
};
|
};
|
||||||
|
|
||||||
std::vector<testData> vecTest;
|
QVector<testData> vecTest;
|
||||||
|
|
||||||
for (int i = 0; i < 50; ++i)
|
for (int i = 0; i < 50; ++i)
|
||||||
{
|
{
|
||||||
std::string first_name = generate_random_string(first_names);
|
QString first_name = generate_random_string(first_names);
|
||||||
std::string last_name = generate_random_string(last_names);
|
QString last_name = generate_random_string(last_names);
|
||||||
int birth_year = generate_random_year(1900, 2000);
|
int birth_year = generate_random_year(1900, 2000);
|
||||||
|
|
||||||
vecTest.push_back({ first_name, last_name, birth_year });
|
vecTest.push_back({ first_name, last_name, birth_year });
|
||||||
@@ -94,16 +95,16 @@ std::vector<testData> fillDB()
|
|||||||
return vecTest;
|
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))
|
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());
|
odb::connection_ptr connect_ptr(db->connection());
|
||||||
|
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ PSLibrary {
|
|||||||
Depends { name: "cpp" }
|
Depends { name: "cpp" }
|
||||||
Depends { name: "odb.gen" }
|
Depends { name: "odb.gen" }
|
||||||
Depends { name: "redkit_gen" }
|
Depends { name: "redkit_gen" }
|
||||||
|
Depends { name: "rdbase" }
|
||||||
|
|
||||||
odb.gen.databases: "sqlite"
|
odb.gen.databases: "sqlite"
|
||||||
cpp.cxxLanguageVersion: "c++17"
|
cpp.cxxLanguageVersion: "c++17"
|
||||||
|
|||||||
46
src/main.cpp
46
src/main.cpp
@@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
#include <QPointer>
|
#include <QPointer>
|
||||||
#include <QSharedPointer>
|
#include <QSharedPointer>
|
||||||
|
#include <QString>
|
||||||
|
#include <QVector>
|
||||||
|
|
||||||
#include <restapi/restapiserver.h>
|
#include <restapi/restapiserver.h>
|
||||||
|
|
||||||
@@ -15,30 +17,30 @@
|
|||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
void fillBooksBD(unidb& db)
|
void fillBooksBD(uDBase& db)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
std::vector<QSharedPointer<Author_S>> authors = {
|
QVector<SH<Author_S>> authors = {
|
||||||
QSharedPointer<Author_S>::create("George", "Orwell", 142),
|
SH<Author_S>::create("George", "Orwell", 142),
|
||||||
QSharedPointer<Author_S>::create("J.K.", "Rowling", 56),
|
SH<Author_S>::create("J.K.", "Rowling", 56),
|
||||||
QSharedPointer<Author_S>::create("J.R.R.", "Tolkien", 81),
|
SH<Author_S>::create("J.R.R.", "Tolkien", 81),
|
||||||
QSharedPointer<Author_S>::create("Leo", "Tolstoy", 189),
|
SH<Author_S>::create("Leo", "Tolstoy", 189),
|
||||||
QSharedPointer<Author_S>::create("Fyodor", "Dostoevsky", 174),
|
SH<Author_S>::create("Fyodor", "Dostoevsky", 174),
|
||||||
QSharedPointer<Author_S>::create("Mark", "Twain", 183),
|
SH<Author_S>::create("Mark", "Twain", 183),
|
||||||
QSharedPointer<Author_S>::create("Charles", "Dickens", 208),
|
SH<Author_S>::create("Charles", "Dickens", 208),
|
||||||
QSharedPointer<Author_S>::create("Virginia", "Woolf", 134),
|
SH<Author_S>::create("Virginia", "Woolf", 134),
|
||||||
QSharedPointer<Author_S>::create("Ernest", "Hemingway", 122),
|
SH<Author_S>::create("Ernest", "Hemingway", 122),
|
||||||
QSharedPointer<Author_S>::create("Gabriel", "García Márquez", 98),
|
SH<Author_S>::create("Gabriel", "García Márquez", 98),
|
||||||
QSharedPointer<Author_S>::create("Franz", "Kafka", 100),
|
SH<Author_S>::create("Franz", "Kafka", 100),
|
||||||
QSharedPointer<Author_S>::create("Harper", "Lee", 64),
|
SH<Author_S>::create("Harper", "Lee", 64),
|
||||||
QSharedPointer<Author_S>::create("William", "Shakespeare", 459),
|
SH<Author_S>::create("William", "Shakespeare", 459),
|
||||||
QSharedPointer<Author_S>::create("Oscar", "Wilde", 155),
|
SH<Author_S>::create("Oscar", "Wilde", 155),
|
||||||
QSharedPointer<Author_S>::create("Aldous", "Huxley", 123),
|
SH<Author_S>::create("Aldous", "Huxley", 123),
|
||||||
QSharedPointer<Author_S>::create("Jane", "Austen", 210),
|
SH<Author_S>::create("Jane", "Austen", 210),
|
||||||
QSharedPointer<Author_S>::create("John", "Steinbeck", 116),
|
SH<Author_S>::create("John", "Steinbeck", 116),
|
||||||
QSharedPointer<Author_S>::create("Agatha", "Christie", 124),
|
SH<Author_S>::create("Agatha", "Christie", 124),
|
||||||
QSharedPointer<Author_S>::create("Isaac", "Asimov", 105)
|
SH<Author_S>::create("Isaac", "Asimov", 105)
|
||||||
};
|
};
|
||||||
|
|
||||||
// Список книг
|
// Список книг
|
||||||
@@ -124,7 +126,7 @@ 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";
|
||||||
unidb db(openDB(dbPath));
|
uDBase db(openDB(dbPath));
|
||||||
|
|
||||||
fillBooksBD(db);
|
fillBooksBD(db);
|
||||||
|
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ WPSLibrary {
|
|||||||
Depends { name: "database" }
|
Depends { name: "database" }
|
||||||
Depends { name: "odb.gen" }
|
Depends { name: "odb.gen" }
|
||||||
Depends { name: "redkit_gen" }
|
Depends { name: "redkit_gen" }
|
||||||
|
Depends { name: "rdbase" }
|
||||||
|
|
||||||
cpp.cxxLanguageVersion: "c++20"
|
cpp.cxxLanguageVersion: "c++20"
|
||||||
|
|
||||||
|
|||||||
@@ -25,7 +25,8 @@ void RestApiServer::start(quint16 port)
|
|||||||
}
|
}
|
||||||
else
|
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::readyRead, this, &RestApiServer::handleRequest);
|
||||||
connect(socket, &QTcpSocket::disconnected, socket, &QTcpSocket::deleteLater);
|
connect(socket, &QTcpSocket::disconnected, socket, &QTcpSocket::deleteLater);
|
||||||
|
|
||||||
|
qWarning() << "Есть входящее подключение" << socket->socketDescriptor();
|
||||||
}
|
}
|
||||||
|
|
||||||
void RestApiServer::handleRequest()
|
void RestApiServer::handleRequest()
|
||||||
@@ -59,7 +62,8 @@ 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";
|
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);
|
// QList<BookRecord> results = books.getBooksByAuthor(author);
|
||||||
|
|
||||||
QByteArray jsonResponse = "{ \"books\": [";
|
QByteArray jsonResponse = "{ \"books\": [";
|
||||||
|
|||||||
Reference in New Issue
Block a user