Compare commits
8 Commits
multiLib
...
8cb56837f5
| Author | SHA1 | Date | |
|---|---|---|---|
| 8cb56837f5 | |||
| 96bdf1e5e8 | |||
| 0198f6d022 | |||
| 905dd309d0 | |||
| 65059351f7 | |||
| 594b3a936f | |||
| f8e504bb07 | |||
| b16ae39263 |
6
.gitmodules
vendored
Normal file
6
.gitmodules
vendored
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
[submodule "redkbuild"]
|
||||||
|
path = redkbuild
|
||||||
|
url = git@gitea.redkit-lab.work:redkit-lab/redkbuild.git
|
||||||
|
[submodule "redkitty"]
|
||||||
|
path = redkitty
|
||||||
|
url = git@gitea.redkit-lab.work:redkit-lab/redkitty.git
|
||||||
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
|
||||||
|
}
|
||||||
18
project.qbs
18
project.qbs
@@ -3,17 +3,19 @@ import qbs
|
|||||||
Project {
|
Project {
|
||||||
name: "opds++"
|
name: "opds++"
|
||||||
qbsSearchPaths: [
|
qbsSearchPaths: [
|
||||||
"qbs",
|
"redkitty/qbs",
|
||||||
// "redkitty/qbs",
|
"redkbuild",
|
||||||
// // "guiness/qbs",
|
"redkitty/lib/redkit-gen-integration/qbs",
|
||||||
// // "prerequisites/qbs",
|
"prerequisites/qbs",
|
||||||
// // "report/qbs",
|
|
||||||
// "redkbuild",
|
|
||||||
// // "ext_libs/openssl/qbs",
|
|
||||||
// "redkitty/lib/redkit-gen-integration/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/model/model.qbs",
|
||||||
|
"src/restapi/restapi.qbs",
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,48 +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 + "/../"),
|
|
||||||
input.filePath,
|
|
||||||
]);
|
|
||||||
cmd.description = "Generating ODB files for " + input.fileName;
|
|
||||||
|
|
||||||
console.error(cmd.arguments)
|
|
||||||
|
|
||||||
return [cmd];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
cpp.includePaths: [ project.sourceDirectory, product.Qt.core.qmDir ]
|
|
||||||
}
|
|
||||||
1
redkbuild
Submodule
1
redkbuild
Submodule
Submodule redkbuild added at 3adf92aad6
1
redkitty
Submodule
1
redkitty
Submodule
Submodule redkitty added at 2316ddea72
@@ -1,63 +0,0 @@
|
|||||||
// file : hello/person.hxx
|
|
||||||
// copyright : not copyrighted - public domain
|
|
||||||
|
|
||||||
#ifndef AUTHOR_S_H
|
|
||||||
#define AUTHOR_S_H
|
|
||||||
|
|
||||||
#include <cstddef> // std::size_t
|
|
||||||
#include <memory>
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
#include <odb/core.hxx>
|
|
||||||
#include <odb/database.hxx>
|
|
||||||
#include <odb/query.hxx>
|
|
||||||
|
|
||||||
#pragma db object
|
|
||||||
class Author_S
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
Author_S() = default;
|
|
||||||
|
|
||||||
Author_S(const std::string& first,
|
|
||||||
const std::string& last,
|
|
||||||
const unsigned short age) :
|
|
||||||
m_first(first),
|
|
||||||
m_last(last),
|
|
||||||
m_age(age)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned long long id() const { return m_id; }
|
|
||||||
std::string first() const { return m_first; }
|
|
||||||
std::string last() const { return m_last; }
|
|
||||||
unsigned short age() const { return m_age; }
|
|
||||||
|
|
||||||
std::string full_name() const { return m_first + " " + m_last; }
|
|
||||||
|
|
||||||
void age(unsigned short age) { m_age = age; }
|
|
||||||
|
|
||||||
private:
|
|
||||||
friend class odb::access;
|
|
||||||
|
|
||||||
#pragma db id auto
|
|
||||||
unsigned long long m_id;
|
|
||||||
|
|
||||||
std::string m_first;
|
|
||||||
std::string m_last;
|
|
||||||
unsigned short m_age;
|
|
||||||
};
|
|
||||||
|
|
||||||
#pragma db view object(Author_S)
|
|
||||||
struct person_stat
|
|
||||||
{
|
|
||||||
#pragma db column("count(" + Author_S::m_id + ")")
|
|
||||||
std::size_t count;
|
|
||||||
|
|
||||||
#pragma db column("min(" + Author_S::m_age + ")")
|
|
||||||
unsigned short min_age;
|
|
||||||
|
|
||||||
#pragma db column("max(" + Author_S::m_age + ")")
|
|
||||||
unsigned short max_age;
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif // AUTHOR_S_H
|
|
||||||
46
src/book_s.h
46
src/book_s.h
@@ -1,46 +0,0 @@
|
|||||||
// file : hello/person.hxx
|
|
||||||
// copyright : not copyrighted - public domain
|
|
||||||
|
|
||||||
#ifndef BOOK_S_H
|
|
||||||
#define BOOK_S_H
|
|
||||||
|
|
||||||
#include <memory>
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
#include <odb/core.hxx>
|
|
||||||
|
|
||||||
#include "author_s.h"
|
|
||||||
|
|
||||||
#pragma db object
|
|
||||||
class Book_S
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
Book_S() = default;
|
|
||||||
|
|
||||||
Book_S(const std::string& name,
|
|
||||||
const std::shared_ptr<Author_S>& author,
|
|
||||||
const int year)
|
|
||||||
{
|
|
||||||
m_author = author;
|
|
||||||
m_name = name;
|
|
||||||
m_year = year;
|
|
||||||
};
|
|
||||||
|
|
||||||
unsigned long long id() const { return m_id; }
|
|
||||||
std::shared_ptr<Author_S> author() const { return m_author; }
|
|
||||||
std::string name() const { return m_name; }
|
|
||||||
int year() const { return m_year; }
|
|
||||||
|
|
||||||
private:
|
|
||||||
friend class odb::access;
|
|
||||||
|
|
||||||
private:
|
|
||||||
#pragma db id auto
|
|
||||||
unsigned long long m_id;
|
|
||||||
|
|
||||||
std::shared_ptr<Author_S> m_author;
|
|
||||||
std::string m_name;
|
|
||||||
int m_year;
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif // BOOK_S_H
|
|
||||||
@@ -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.
|
||||||
@@ -15,44 +15,29 @@ Application {
|
|||||||
|
|
||||||
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: "database" }
|
||||||
|
Depends { name: "odb.gen" }
|
||||||
// Depends { name: "redkit_gen" }
|
Depends { name: "sqlite3" }
|
||||||
|
Depends { name: "restapi" }
|
||||||
|
Depends { name: "redkit_gen" }
|
||||||
|
Depends { name: "rdbase" }
|
||||||
|
Depends { name: "model" }
|
||||||
|
|
||||||
cpp.cxxLanguageVersion: "c++20"
|
cpp.cxxLanguageVersion: "c++20"
|
||||||
|
|
||||||
Group {
|
Group {
|
||||||
name: "cpp"
|
name: "cpp"
|
||||||
files: [
|
files: [
|
||||||
"books.*",
|
|
||||||
"databasemanager.*",
|
|
||||||
"main.cpp",
|
"main.cpp",
|
||||||
"restapiserver.*",
|
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
Group {
|
|
||||||
name: "odb"
|
|
||||||
files: [
|
|
||||||
"author_s.h",
|
|
||||||
"book_s.h",
|
|
||||||
]
|
|
||||||
fileTags: ["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"
|
||||||
]
|
]
|
||||||
|
|
||||||
// }
|
// }
|
||||||
} // Project
|
} // Project
|
||||||
|
|||||||
177
src/database.hxx
177
src/database.hxx
@@ -1,177 +0,0 @@
|
|||||||
// file : hello/database.hxx
|
|
||||||
// copyright : not copyrighted - public domain
|
|
||||||
|
|
||||||
//
|
|
||||||
// Create concrete database instance based on the DATABASE_* macros.
|
|
||||||
//
|
|
||||||
|
|
||||||
#ifndef DATABASE_HXX
|
|
||||||
#define DATABASE_HXX
|
|
||||||
|
|
||||||
#include <cstdlib> // std::exit
|
|
||||||
#include <iostream>
|
|
||||||
#include <memory> // std::unique_ptr
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
#include <odb/database.hxx>
|
|
||||||
|
|
||||||
#if defined(DATABASE_MYSQL)
|
|
||||||
#include <odb/mysql/database.hxx>
|
|
||||||
#elif defined(DATABASE_SQLITE)
|
|
||||||
#include <odb/connection.hxx>
|
|
||||||
#include <odb/schema-catalog.hxx>
|
|
||||||
#include <odb/sqlite/database.hxx>
|
|
||||||
#include <odb/transaction.hxx>
|
|
||||||
#elif defined(DATABASE_PGSQL)
|
|
||||||
#include <odb/pgsql/database.hxx>
|
|
||||||
#elif defined(DATABASE_ORACLE)
|
|
||||||
#include <odb/oracle/database.hxx>
|
|
||||||
#elif defined(DATABASE_MSSQL)
|
|
||||||
#include <odb/mssql/database.hxx>
|
|
||||||
#else
|
|
||||||
#error unknown database; did you forget to define the DATABASE_* macros?
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include <filesystem>
|
|
||||||
#include <random>
|
|
||||||
|
|
||||||
struct testData
|
|
||||||
{
|
|
||||||
std::string first;
|
|
||||||
std::string last;
|
|
||||||
int age;
|
|
||||||
};
|
|
||||||
|
|
||||||
// Функция для генерации случайной строки (имени или фамилии)
|
|
||||||
std::string generate_random_string(const std::vector<std::string>& pool)
|
|
||||||
{
|
|
||||||
static std::random_device rd;
|
|
||||||
static std::mt19937 gen(rd());
|
|
||||||
std::uniform_int_distribution<> dis(0, pool.size() - 1);
|
|
||||||
return pool[dis(gen)];
|
|
||||||
}
|
|
||||||
|
|
||||||
// Функция для генерации случайного года
|
|
||||||
int generate_random_year(int min_year = 1900, int max_year = 2020)
|
|
||||||
{
|
|
||||||
static std::random_device rd;
|
|
||||||
static std::mt19937 gen(rd());
|
|
||||||
std::uniform_int_distribution<> dis(min_year, max_year);
|
|
||||||
return dis(gen);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::vector<testData> fillDB()
|
|
||||||
{
|
|
||||||
std::vector<std::string> 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 = {
|
|
||||||
"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",
|
|
||||||
"Baker", "Gonzalez", "Nelson", "Carter", "Mitchell", "Perez", "Robinson", "Hughes", "Flores", "Cook",
|
|
||||||
"Rogers", "Gutierrez", "Ramirez", "Diaz", "Perez", "Ross", "Sanders", "Price", "Howard", "Cooper"
|
|
||||||
};
|
|
||||||
|
|
||||||
std::vector<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);
|
|
||||||
int birth_year = generate_random_year(1900, 2000);
|
|
||||||
|
|
||||||
vecTest.push_back({ first_name, last_name, birth_year });
|
|
||||||
}
|
|
||||||
|
|
||||||
return vecTest;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline std::unique_ptr<odb::database> openDB(std::string 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));
|
|
||||||
|
|
||||||
// И создать новую, если не существует
|
|
||||||
std::unique_ptr<odb::core::database> db(new odb::sqlite::database(path_db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE));
|
|
||||||
|
|
||||||
odb::core::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);
|
|
||||||
tans.commit();
|
|
||||||
|
|
||||||
connect_ptr->execute("PRAGMA foreign_keys=ON");
|
|
||||||
|
|
||||||
return db;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline std::unique_ptr<odb::database> create_database(int& argc, char* argv[])
|
|
||||||
{
|
|
||||||
using namespace std;
|
|
||||||
using namespace odb::core;
|
|
||||||
|
|
||||||
if (argc > 1 && argv[1] == string("--help"))
|
|
||||||
{
|
|
||||||
cout << "Usage: " << argv[0] << " [options]" << endl
|
|
||||||
<< "Options:" << endl;
|
|
||||||
|
|
||||||
#if defined(DATABASE_MYSQL)
|
|
||||||
odb::mysql::database::print_usage(cout);
|
|
||||||
#elif defined(DATABASE_SQLITE)
|
|
||||||
odb::sqlite::database::print_usage(cout);
|
|
||||||
#elif defined(DATABASE_PGSQL)
|
|
||||||
odb::pgsql::database::print_usage(cout);
|
|
||||||
#elif defined(DATABASE_ORACLE)
|
|
||||||
odb::oracle::database::print_usage(cout);
|
|
||||||
#elif defined(DATABASE_MSSQL)
|
|
||||||
odb::mssql::database::print_usage(cout);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
exit(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
#if defined(DATABASE_MYSQL)
|
|
||||||
unique_ptr<database> db(new odb::mysql::database(argc, argv));
|
|
||||||
#elif defined(DATABASE_SQLITE)
|
|
||||||
|
|
||||||
unique_ptr<database> db(new odb::sqlite::database(argc,
|
|
||||||
argv,
|
|
||||||
false,
|
|
||||||
SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE));
|
|
||||||
|
|
||||||
// Create the database schema. Due to bugs in SQLite foreign key
|
|
||||||
// support for DDL statements, we need to temporarily disable
|
|
||||||
// foreign keys.
|
|
||||||
//
|
|
||||||
{
|
|
||||||
connection_ptr connect_ptr(db->connection());
|
|
||||||
|
|
||||||
connect_ptr->execute("PRAGMA foreign_keys=OFF");
|
|
||||||
|
|
||||||
transaction tans(connect_ptr->begin());
|
|
||||||
schema_catalog::create_schema(*db);
|
|
||||||
tans.commit();
|
|
||||||
|
|
||||||
connect_ptr->execute("PRAGMA foreign_keys=ON");
|
|
||||||
}
|
|
||||||
#elif defined(DATABASE_PGSQL)
|
|
||||||
unique_ptr<database> db(new odb::pgsql::database(argc, argv));
|
|
||||||
#elif defined(DATABASE_ORACLE)
|
|
||||||
unique_ptr<database> db(new odb::oracle::database(argc, argv));
|
|
||||||
#elif defined(DATABASE_MSSQL)
|
|
||||||
unique_ptr<database> db(
|
|
||||||
new odb::mssql::database(argc, argv, false, "TrustServerCertificate=yes"));
|
|
||||||
#endif
|
|
||||||
|
|
||||||
return db;
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif // DATABASE_HXX
|
|
||||||
105
src/database/database.hxx
Normal file
105
src/database/database.hxx
Normal file
@@ -0,0 +1,105 @@
|
|||||||
|
// file : hello/database.hxx
|
||||||
|
// copyright : not copyrighted - public domain
|
||||||
|
|
||||||
|
//
|
||||||
|
// Create concrete database instance based on the DATABASE_* macros.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef DATABASE_HXX
|
||||||
|
#define DATABASE_HXX
|
||||||
|
|
||||||
|
#include <cstdlib> // std::exit
|
||||||
|
#include <iostream>
|
||||||
|
#include <memory> // std::unique_ptr
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include <filesystem>
|
||||||
|
|
||||||
|
#include "database_utils.h"
|
||||||
|
|
||||||
|
inline uDBase openDB(const std::string path_db)
|
||||||
|
{
|
||||||
|
// Открыть БД, если она существует
|
||||||
|
auto db_ptr = new oDBase(path_db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE);
|
||||||
|
|
||||||
|
if (std::filesystem::is_regular_file(path_db))
|
||||||
|
return uDBase(db_ptr);
|
||||||
|
|
||||||
|
// И создать новую, если не существует
|
||||||
|
uDBase db(db_ptr);
|
||||||
|
|
||||||
|
odb::connection_ptr connect_ptr(db->connection());
|
||||||
|
|
||||||
|
connect_ptr->execute("PRAGMA foreign_keys=OFF");
|
||||||
|
|
||||||
|
odb::transaction tans(db->begin());
|
||||||
|
odb::schema_catalog::create_schema(*db);
|
||||||
|
tans.commit();
|
||||||
|
|
||||||
|
connect_ptr->execute("PRAGMA foreign_keys=ON");
|
||||||
|
|
||||||
|
return db;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline std::unique_ptr<odb::database> create_database(int& argc, char* argv[])
|
||||||
|
{
|
||||||
|
using namespace std;
|
||||||
|
using namespace odb::core;
|
||||||
|
|
||||||
|
if (argc > 1 && argv[1] == string("--help"))
|
||||||
|
{
|
||||||
|
cout << "Usage: " << argv[0] << " [options]" << endl
|
||||||
|
<< "Options:" << endl;
|
||||||
|
|
||||||
|
#if defined(DATABASE_MYSQL)
|
||||||
|
odb::mysql::database::print_usage(cout);
|
||||||
|
#elif defined(DATABASE_SQLITE)
|
||||||
|
odb::sqlite::database::print_usage(cout);
|
||||||
|
#elif defined(DATABASE_PGSQL)
|
||||||
|
odb::pgsql::database::print_usage(cout);
|
||||||
|
#elif defined(DATABASE_ORACLE)
|
||||||
|
odb::oracle::database::print_usage(cout);
|
||||||
|
#elif defined(DATABASE_MSSQL)
|
||||||
|
odb::mssql::database::print_usage(cout);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
#if defined(DATABASE_MYSQL)
|
||||||
|
unique_ptr<database> db(new odb::mysql::database(argc, argv));
|
||||||
|
#elif defined(DATABASE_SQLITE)
|
||||||
|
|
||||||
|
unique_ptr<database> db(new odb::sqlite::database(argc,
|
||||||
|
argv,
|
||||||
|
false,
|
||||||
|
SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE));
|
||||||
|
|
||||||
|
// Create the database schema. Due to bugs in SQLite foreign key
|
||||||
|
// support for DDL statements, we need to temporarily disable
|
||||||
|
// foreign keys.
|
||||||
|
//
|
||||||
|
{
|
||||||
|
connection_ptr connect_ptr(db->connection());
|
||||||
|
|
||||||
|
connect_ptr->execute("PRAGMA foreign_keys=OFF");
|
||||||
|
|
||||||
|
transaction tans(connect_ptr->begin());
|
||||||
|
schema_catalog::create_schema(*db);
|
||||||
|
tans.commit();
|
||||||
|
|
||||||
|
connect_ptr->execute("PRAGMA foreign_keys=ON");
|
||||||
|
}
|
||||||
|
#elif defined(DATABASE_PGSQL)
|
||||||
|
unique_ptr<database> db(new odb::pgsql::database(argc, argv));
|
||||||
|
#elif defined(DATABASE_ORACLE)
|
||||||
|
unique_ptr<database> db(new odb::oracle::database(argc, argv));
|
||||||
|
#elif defined(DATABASE_MSSQL)
|
||||||
|
unique_ptr<database> db(
|
||||||
|
new odb::mssql::database(argc, argv, false, "TrustServerCertificate=yes"));
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return db;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // DATABASE_HXX
|
||||||
43
src/database/database.qbs
Normal file
43
src/database/database.qbs
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
/*!
|
||||||
|
\qmltype cpp-opds
|
||||||
|
\inherits Project
|
||||||
|
\brief Описание
|
||||||
|
*/
|
||||||
|
PSLibrary {
|
||||||
|
cpp.defines: [
|
||||||
|
// 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_LIBRARY"
|
||||||
|
]
|
||||||
|
consoleApplication: true
|
||||||
|
|
||||||
|
Depends { name: "Qt"; submodules: [ "core", "network" ] }
|
||||||
|
Depends { name: "cpp" }
|
||||||
|
|
||||||
|
Depends { name: "odb.gen" }
|
||||||
|
Depends { name: "rdbase" }
|
||||||
|
Depends { name: "model" }
|
||||||
|
|
||||||
|
Depends { name: "redkit_gen" }
|
||||||
|
|
||||||
|
odb.gen.databases: "sqlite"
|
||||||
|
cpp.cxxLanguageVersion: "c++17"
|
||||||
|
|
||||||
|
Group {
|
||||||
|
name: "cpp"
|
||||||
|
files: [
|
||||||
|
"**/*.h",
|
||||||
|
"**/*.hxx",
|
||||||
|
"**/*.cpp",
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
cpp.dynamicLibraries: [
|
||||||
|
"odb-sqlite",
|
||||||
|
"odb-qt",
|
||||||
|
"odb",
|
||||||
|
"sqlite3"
|
||||||
|
]
|
||||||
|
}
|
||||||
12
src/database/database_global.h
Normal file
12
src/database/database_global.h
Normal 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
|
||||||
55
src/database/database_utils.cpp
Normal file
55
src/database/database_utils.cpp
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
#include "database_utils.h"
|
||||||
|
|
||||||
|
void addBook(oDBase& db, const QString title, const QVector<QString>& authors)
|
||||||
|
{
|
||||||
|
odb::transaction t(db.begin());
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// 1. Создаем/получаем книгу
|
||||||
|
auto books = db.query<Book_S>(odb::query<Book_S>::title == title);
|
||||||
|
SH<Book_S> book;
|
||||||
|
|
||||||
|
if (books.empty())
|
||||||
|
{
|
||||||
|
book = SH<Book_S>::create();
|
||||||
|
book->setTitle(title);
|
||||||
|
|
||||||
|
db.persist(book);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
book = books.begin().load(); // Берем первую найденную книгу
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. Обрабатываем авторов
|
||||||
|
for (const auto& name : authors)
|
||||||
|
{
|
||||||
|
auto authors = db.query<Author_S>(odb::query<Author_S>::fullName == name);
|
||||||
|
SH<Author_S> author = authors.empty() ? SH<Author_S>::create() : authors.begin().load();
|
||||||
|
|
||||||
|
if (authors.empty())
|
||||||
|
{
|
||||||
|
author->setFullName(name);
|
||||||
|
db.persist(author);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Проверяем и создаем связь
|
||||||
|
|
||||||
|
auto links = db.query<AuthorBook_S>(
|
||||||
|
odb::query<AuthorBook_S>::author->id == author->id()
|
||||||
|
&& odb::query<AuthorBook_S>::book->id == book->id());
|
||||||
|
|
||||||
|
if (links.empty())
|
||||||
|
{
|
||||||
|
db.persist(SH<AuthorBook_S>::create(author, book));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
t.commit();
|
||||||
|
}
|
||||||
|
catch (...)
|
||||||
|
{
|
||||||
|
t.rollback();
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
38
src/database/database_utils.h
Normal file
38
src/database/database_utils.h
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
#include <QDebug>
|
||||||
|
#include <QString>
|
||||||
|
#include <QVector>
|
||||||
|
|
||||||
|
#include <odb/database.hxx>
|
||||||
|
|
||||||
|
#if defined(DATABASE_MYSQL)
|
||||||
|
#include <odb/mysql/database.hxx>
|
||||||
|
#elif defined(DATABASE_SQLITE)
|
||||||
|
#include <odb/connection.hxx>
|
||||||
|
#include <odb/schema-catalog.hxx>
|
||||||
|
#include <odb/sqlite/database.hxx>
|
||||||
|
#include <odb/transaction.hxx>
|
||||||
|
#elif defined(DATABASE_PGSQL)
|
||||||
|
#include <odb/pgsql/database.hxx>
|
||||||
|
#elif defined(DATABASE_ORACLE)
|
||||||
|
#include <odb/oracle/database.hxx>
|
||||||
|
#elif defined(DATABASE_MSSQL)
|
||||||
|
#include <odb/mssql/database.hxx>
|
||||||
|
#else
|
||||||
|
#error unknown database; did you forget to define the DATABASE_* macros?
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <rcore/smarttypes.h>
|
||||||
|
|
||||||
|
#include "database_global.h"
|
||||||
|
|
||||||
|
using oDBase = odb::sqlite::database;
|
||||||
|
using uDBase = U<oDBase>;
|
||||||
|
|
||||||
|
#include <model/author_book_s-odb.hxx>
|
||||||
|
#include <model/author_book_s.h>
|
||||||
|
#include <model/author_s-odb.hxx>
|
||||||
|
#include <model/author_s.h>
|
||||||
|
#include <model/book_s-odb.hxx>
|
||||||
|
#include <model/book_s.h>
|
||||||
|
|
||||||
|
void DATABASE_EXPORT addBook(oDBase& db, const QString title, const QVector<QString>& authors);
|
||||||
@@ -1,118 +0,0 @@
|
|||||||
#include "databasemanager.h"
|
|
||||||
|
|
||||||
// Singleton instance
|
|
||||||
DatabaseManager& DatabaseManager::instance()
|
|
||||||
{
|
|
||||||
static DatabaseManager instance;
|
|
||||||
return instance;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Конструктор: создаем подключение
|
|
||||||
DatabaseManager::DatabaseManager()
|
|
||||||
{
|
|
||||||
db = QSqlDatabase::addDatabase("QSQLITE");
|
|
||||||
db.setDatabaseName("mydatabase.db");
|
|
||||||
|
|
||||||
if (!db.open())
|
|
||||||
{
|
|
||||||
qDebug() << "Ошибка открытия БД:" << db.lastError().text();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
qDebug() << "База данных успешно открыта";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Деструктор: закрываем подключение
|
|
||||||
DatabaseManager::~DatabaseManager()
|
|
||||||
{
|
|
||||||
if (db.isOpen())
|
|
||||||
{
|
|
||||||
db.close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Возвращает ссылку на объект БД
|
|
||||||
QSqlDatabase& DatabaseManager::database()
|
|
||||||
{
|
|
||||||
return db;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Выполняет SQL-запрос (без параметров)
|
|
||||||
bool DatabaseManager::executeQuery(const QString& query)
|
|
||||||
{
|
|
||||||
QSqlQuery q;
|
|
||||||
if (!q.exec(query))
|
|
||||||
{
|
|
||||||
qDebug() << "Ошибка выполнения запроса:" << q.lastError().text();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Выполняет подготовленный SQL-запрос с параметрами
|
|
||||||
bool DatabaseManager::executePreparedQuery(const QString& query, const QStringList& values)
|
|
||||||
{
|
|
||||||
QSqlQuery q;
|
|
||||||
q.prepare(query);
|
|
||||||
|
|
||||||
for (const auto& value : values)
|
|
||||||
q.addBindValue(value);
|
|
||||||
|
|
||||||
if (!q.exec())
|
|
||||||
{
|
|
||||||
qDebug() << "Ошибка выполнения подготовленного запроса:" << q.lastError().text();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
QList<QVariantList> DatabaseManager::executeSelectQuery(const QString& query, const QStringList& values)
|
|
||||||
{
|
|
||||||
QSqlQuery q;
|
|
||||||
q.prepare(query);
|
|
||||||
|
|
||||||
for (const auto& value : values)
|
|
||||||
q.addBindValue(value);
|
|
||||||
|
|
||||||
if (!q.exec())
|
|
||||||
{
|
|
||||||
qDebug() << "Ошибка выполнения SELECT запроса:" << q.lastError().text();
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
|
|
||||||
QList<QVariantList> results;
|
|
||||||
while (q.next())
|
|
||||||
{
|
|
||||||
QVariantList row;
|
|
||||||
for (int i = 0; i < q.record().count(); ++i)
|
|
||||||
row.append(q.value(i));
|
|
||||||
results.append(row);
|
|
||||||
}
|
|
||||||
|
|
||||||
return results;
|
|
||||||
}
|
|
||||||
|
|
||||||
QList<QVariantList> DatabaseManager::executeSelect(const SelectBuilder& selectBuilder)
|
|
||||||
{
|
|
||||||
const auto queryString = selectBuilder.getSelect();
|
|
||||||
qDebug() << "Подготовленный запрос: " << queryString;
|
|
||||||
|
|
||||||
QSqlQuery q;
|
|
||||||
if (!q.prepare(queryString))
|
|
||||||
qDebug() << "Ошибка при подготовке запроса:" << q.lastError().text();
|
|
||||||
|
|
||||||
if (!q.exec())
|
|
||||||
qDebug() << "Ошибка при выполнении запроса:" << q.lastError().text();
|
|
||||||
|
|
||||||
QList<QVariantList> results;
|
|
||||||
while (q.next()) // Проходимся по строкам
|
|
||||||
{
|
|
||||||
QVariantList row;
|
|
||||||
for (int i = 0; i < q.record().count(); ++i) // Проходимся по столбцам
|
|
||||||
row.append(q.value(i));
|
|
||||||
results.append(row);
|
|
||||||
}
|
|
||||||
|
|
||||||
return results;
|
|
||||||
}
|
|
||||||
@@ -1,59 +0,0 @@
|
|||||||
#ifndef DATABASEMANAGER_H
|
|
||||||
#define DATABASEMANAGER_H
|
|
||||||
|
|
||||||
#include <QDebug>
|
|
||||||
#include <QtSql>
|
|
||||||
|
|
||||||
struct SelectBuilder
|
|
||||||
{
|
|
||||||
QString tableName;
|
|
||||||
QStringList rows = { "*" };
|
|
||||||
QString where;
|
|
||||||
|
|
||||||
QString getSelect() const
|
|
||||||
{
|
|
||||||
QString allRows;
|
|
||||||
|
|
||||||
auto ri = rows.begin();
|
|
||||||
while (ri != rows.end())
|
|
||||||
{
|
|
||||||
allRows += (*ri++);
|
|
||||||
|
|
||||||
if (ri != rows.end())
|
|
||||||
allRows += ", ";
|
|
||||||
}
|
|
||||||
|
|
||||||
QString queryString = QString("SELECT %1").arg(allRows);
|
|
||||||
|
|
||||||
queryString += QString(" FROM %1").arg(tableName);
|
|
||||||
|
|
||||||
if (!where.isEmpty())
|
|
||||||
queryString += QString(" WHERE %1").arg(where);
|
|
||||||
|
|
||||||
queryString += ';';
|
|
||||||
return queryString;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
class DatabaseManager
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
static DatabaseManager& instance(); // Singleton
|
|
||||||
|
|
||||||
QSqlDatabase& database();
|
|
||||||
|
|
||||||
bool executeQuery(const QString& query);
|
|
||||||
bool executePreparedQuery(const QString& query, const QStringList& values);
|
|
||||||
|
|
||||||
QList<QVariantList> executeSelectQuery(const QString& query, const QStringList& values = {});
|
|
||||||
|
|
||||||
QList<QVariantList> executeSelect(const SelectBuilder& selectBuilder);
|
|
||||||
|
|
||||||
private:
|
|
||||||
DatabaseManager();
|
|
||||||
~DatabaseManager();
|
|
||||||
|
|
||||||
QSqlDatabase db;
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif // DATABASEMANAGER_H
|
|
||||||
222
src/main.cpp
222
src/main.cpp
@@ -1,115 +1,102 @@
|
|||||||
#include <QCoreApplication>
|
#include <QCoreApplication>
|
||||||
|
|
||||||
#include "restapiserver.h"
|
#include <QPointer>
|
||||||
|
#include <QSharedPointer>
|
||||||
|
#include <QString>
|
||||||
|
#include <QVector>
|
||||||
|
|
||||||
|
#include <restapi/restapiserver.h>
|
||||||
|
|
||||||
/* Опыты с odb */
|
/* Опыты с odb */
|
||||||
#include "author_s-odb.hxx" // Должен быть здесь
|
#include <database/database.hxx> // create_database
|
||||||
#include "author_s.h"
|
#include <database/database_utils.h>
|
||||||
|
|
||||||
#include "book_s-odb.hxx" // Должен быть здесь
|
|
||||||
#include "book_s.h"
|
|
||||||
|
|
||||||
#include <odb/database.hxx>
|
#include <odb/database.hxx>
|
||||||
|
|
||||||
#include "database.hxx" // create_database
|
#include <iostream>
|
||||||
|
|
||||||
using unidb = std::unique_ptr<odb::database>;
|
void fillBooksBD(uDBase& db)
|
||||||
|
|
||||||
void fillBooksBD(unidb& db)
|
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
std::vector<std::shared_ptr<Author_S>> authors = {
|
QVector<Author_S> authors = {
|
||||||
std::make_shared<Author_S>("George", "Orwell", 142),
|
Author_S("George Orwell", "en"),
|
||||||
std::make_shared<Author_S>("J.K.", "Rowling", 56),
|
Author_S("J.K. Rowling", "en"),
|
||||||
std::make_shared<Author_S>("J.R.R.", "Tolkien", 81),
|
Author_S("J.R.R. Tolkien", "en"),
|
||||||
std::make_shared<Author_S>("Leo", "Tolstoy", 189),
|
Author_S("Leo Tolstoy", "ru"),
|
||||||
std::make_shared<Author_S>("Fyodor", "Dostoevsky", 174),
|
Author_S("Fyodor Dostoevsky", "ru"),
|
||||||
std::make_shared<Author_S>("Mark", "Twain", 183),
|
Author_S("Mark Twain", "en"),
|
||||||
std::make_shared<Author_S>("Charles", "Dickens", 208),
|
Author_S("Charles Dickens", "en"),
|
||||||
std::make_shared<Author_S>("Virginia", "Woolf", 134),
|
Author_S("Virginia Woolf", "en"),
|
||||||
std::make_shared<Author_S>("Ernest", "Hemingway", 122),
|
Author_S("Ernest Hemingway", "en"),
|
||||||
std::make_shared<Author_S>("Gabriel", "García Márquez", 98),
|
Author_S("Gabriel García Márquez", "en"),
|
||||||
std::make_shared<Author_S>("Franz", "Kafka", 100),
|
Author_S("Franz Kafka", "de"),
|
||||||
std::make_shared<Author_S>("Harper", "Lee", 64),
|
Author_S("Harper Lee", "en"),
|
||||||
std::make_shared<Author_S>("William", "Shakespeare", 459),
|
Author_S("William Shakespeare", "en"),
|
||||||
std::make_shared<Author_S>("Oscar", "Wilde", 155),
|
Author_S("Oscar Wilde", "en"),
|
||||||
std::make_shared<Author_S>("Aldous", "Huxley", 123),
|
Author_S("Aldous Huxley", "en"),
|
||||||
std::make_shared<Author_S>("Jane", "Austen", 210),
|
Author_S("Jane Austen", "en"),
|
||||||
std::make_shared<Author_S>("John", "Steinbeck", 116),
|
Author_S("John Steinbeck", "en"),
|
||||||
std::make_shared<Author_S>("Agatha", "Christie", 124),
|
Author_S("Agatha Christie", "en"),
|
||||||
std::make_shared<Author_S>("Isaac", "Asimov", 105)
|
Author_S("Isaac Asimov", "ru"),
|
||||||
};
|
};
|
||||||
|
|
||||||
// Список книг
|
addBook(*db, "Очень странная книжка", { "Автор 1", "Автор 2", "Авторк 3" });
|
||||||
std::vector<Book_S> 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),
|
|
||||||
Book_S("War and Peace", authors[3], 1869),
|
|
||||||
Book_S("Crime and Punishment", authors[4], 1866),
|
|
||||||
Book_S("Adventures of Huckleberry Finn", authors[5], 1884),
|
|
||||||
Book_S("A Tale of Two Cities", authors[6], 1859),
|
|
||||||
Book_S("Mrs. Dalloway", authors[7], 1925),
|
|
||||||
Book_S("The Old Man and the Sea", authors[8], 1952),
|
|
||||||
Book_S("One Hundred Years of Solitude", authors[9], 1967),
|
|
||||||
Book_S("The Trial", authors[10], 1925),
|
|
||||||
Book_S("To Kill a Mockingbird", authors[11], 1960),
|
|
||||||
Book_S("Macbeth", authors[12], 1606),
|
|
||||||
Book_S("The Picture of Dorian Gray", authors[13], 1890),
|
|
||||||
Book_S("Brave New World", authors[14], 1932),
|
|
||||||
Book_S("Pride and Prejudice", authors[15], 1813),
|
|
||||||
Book_S("The Brothers Karamazov", authors[4], 1880),
|
|
||||||
Book_S("The Grapes of Wrath", authors[16], 1939),
|
|
||||||
Book_S("Murder on the Orient Express", authors[17], 1934),
|
|
||||||
Book_S("I, Robot", authors[18], 1950),
|
|
||||||
Book_S("Animal Farm", authors[0], 1945),
|
|
||||||
Book_S("Harry Potter and the Chamber of Secrets", authors[1], 1998),
|
|
||||||
Book_S("The Lord of the Rings: The Fellowship of the Ring", authors[2], 1954),
|
|
||||||
Book_S("Anna Karenina", authors[3], 1878),
|
|
||||||
Book_S("The Idiot", authors[4], 1869),
|
|
||||||
Book_S("The Adventures of Tom Sawyer", authors[5], 1876),
|
|
||||||
Book_S("Oliver Twist", authors[6], 1837),
|
|
||||||
Book_S("To the Lighthouse", authors[7], 1927),
|
|
||||||
Book_S("For Whom the Bell Tolls", authors[8], 1940),
|
|
||||||
Book_S("Love in the Time of Cholera", authors[9], 1985),
|
|
||||||
Book_S("The Metamorphosis", authors[10], 1915),
|
|
||||||
Book_S("Go Set a Watchman", authors[11], 2015),
|
|
||||||
Book_S("King Lear", authors[12], 1605),
|
|
||||||
Book_S("The Importance of Being Earnest", authors[13], 1895),
|
|
||||||
Book_S("Brave New World Revisited", authors[14], 1958),
|
|
||||||
Book_S("Emma", authors[15], 1815),
|
|
||||||
Book_S("The Double", authors[4], 1846),
|
|
||||||
Book_S("Of Mice and Men", authors[16], 1937),
|
|
||||||
Book_S("And Then There Were None", authors[17], 1939),
|
|
||||||
Book_S("The Foundation Trilogy", authors[18], 1951),
|
|
||||||
Book_S("Down and Out in Paris and London", authors[0], 1933),
|
|
||||||
Book_S("Harry Potter and the Prisoner of Azkaban", authors[1], 1999),
|
|
||||||
Book_S("The Lord of the Rings: The Two Towers", authors[2], 1954),
|
|
||||||
Book_S("War and Peace", authors[3], 1869),
|
|
||||||
Book_S("The Brothers Karamazov", authors[4], 1880),
|
|
||||||
Book_S("The Prince and the Pauper", authors[5], 1881),
|
|
||||||
Book_S("David Copperfield", authors[6], 1850),
|
|
||||||
Book_S("The Waves", authors[7], 1931),
|
|
||||||
Book_S("A Farewell to Arms", authors[8], 1929),
|
|
||||||
Book_S("Chronicle of a Death Foretold", authors[9], 1981)
|
|
||||||
};
|
|
||||||
|
|
||||||
// Сохранение авторов в базу данных
|
addBook(*db, "1984", { authors[0].fullName() });
|
||||||
{
|
addBook(*db, "Harry Potter and the Philosopher's Stone", { authors[1].fullName() });
|
||||||
odb::core::transaction t(db->begin());
|
addBook(*db, "The Hobbit", { authors[2].fullName() });
|
||||||
for (auto& author : authors)
|
addBook(*db, "War and Peace", { authors[3].fullName() });
|
||||||
db->persist(author.get()); // Используем get() для получения сырого указателя
|
addBook(*db, "Crime and Punishment", { authors[4].fullName() });
|
||||||
t.commit();
|
addBook(*db, "Adventures of Huckleberry Finn", { authors[5].fullName() });
|
||||||
}
|
addBook(*db, "A Tale of Two Cities", { authors[6].fullName() });
|
||||||
|
addBook(*db, "Mrs. Dalloway", { authors[7].fullName() });
|
||||||
|
addBook(*db, "The Old Man and the Sea", { authors[8].fullName() });
|
||||||
|
addBook(*db, "One Hundred Years of Solitude", { authors[9].fullName() });
|
||||||
|
addBook(*db, "The Trial", { authors[10].fullName() });
|
||||||
|
addBook(*db, "To Kill a Mockingbird", { authors[11].fullName() });
|
||||||
|
addBook(*db, "Macbeth", { authors[12].fullName() });
|
||||||
|
addBook(*db, "The Picture of Dorian Gray", { authors[13].fullName() });
|
||||||
|
addBook(*db, "Brave New World", { authors[14].fullName() });
|
||||||
|
addBook(*db, "Pride and Prejudice", { authors[15].fullName() });
|
||||||
|
addBook(*db, "The Brothers Karamazov", { authors[4].fullName() });
|
||||||
|
addBook(*db, "The Grapes of Wrath", { authors[16].fullName() });
|
||||||
|
addBook(*db, "Murder on the Orient Express", { authors[17].fullName() });
|
||||||
|
addBook(*db, "I, Robot", { authors[18].fullName() });
|
||||||
|
addBook(*db, "Animal Farm", { authors[0].fullName() });
|
||||||
|
addBook(*db, "Harry Potter and the Chamber of Secrets", { authors[1].fullName() });
|
||||||
|
addBook(*db, "The Lord of the Rings: The Fellowship of the Ring", { authors[2].fullName() });
|
||||||
|
addBook(*db, "Anna Karenina", { authors[3].fullName() });
|
||||||
|
addBook(*db, "The Idiot", { authors[4].fullName() });
|
||||||
|
addBook(*db, "The Adventures of Tom Sawyer", { authors[5].fullName() });
|
||||||
|
addBook(*db, "Oliver Twist", { authors[6].fullName() });
|
||||||
|
addBook(*db, "To the Lighthouse", { authors[7].fullName() });
|
||||||
|
addBook(*db, "For Whom the Bell Tolls", { authors[8].fullName() });
|
||||||
|
addBook(*db, "Love in the Time of Cholera", { authors[9].fullName() });
|
||||||
|
addBook(*db, "The Metamorphosis", { authors[10].fullName() });
|
||||||
|
addBook(*db, "Go Set a Watchman", { authors[11].fullName() });
|
||||||
|
addBook(*db, "King Lear", { authors[12].fullName() });
|
||||||
|
addBook(*db, "The Importance of Being Earnest", { authors[13].fullName() });
|
||||||
|
addBook(*db, "Brave New World Revisited", { authors[14].fullName() });
|
||||||
|
addBook(*db, "Emma", { authors[15].fullName() });
|
||||||
|
addBook(*db, "The Double", { authors[4].fullName() });
|
||||||
|
addBook(*db, "Of Mice and Men", { authors[16].fullName() });
|
||||||
|
addBook(*db, "And Then There Were None", { authors[17].fullName() });
|
||||||
|
addBook(*db, "The Foundation Trilogy", { authors[18].fullName() });
|
||||||
|
addBook(*db, "Down and Out in Paris and London", { authors[0].fullName() });
|
||||||
|
addBook(*db, "Harry Potter and the Prisoner of Azkaban", { authors[1].fullName() });
|
||||||
|
addBook(*db, "The Lord of the Rings: The Two Towers", { authors[2].fullName() });
|
||||||
|
addBook(*db, "War and Peace", { authors[3].fullName() });
|
||||||
|
addBook(*db, "The Brothers Karamazov", { authors[4].fullName() });
|
||||||
|
addBook(*db, "The Prince and the Pauper", { authors[5].fullName() });
|
||||||
|
addBook(*db, "David Copperfield", { authors[6].fullName() });
|
||||||
|
addBook(*db, "The Waves", { authors[7].fullName() });
|
||||||
|
addBook(*db, "A Farewell to Arms", { authors[8].fullName() });
|
||||||
|
addBook(*db, "Chronicle of a Death Foretold", { authors[9].fullName() });
|
||||||
|
|
||||||
// Сохранение книг в базу данных
|
addBook(*db, "1984", { "George Orwell" });
|
||||||
{
|
addBook(*db, "Harry Potter and the Philosopher's Stone", { "J.K. Rowling" });
|
||||||
odb::core::transaction t(db->begin());
|
|
||||||
for (auto& book : books)
|
addBook(*db, "Очень странная книжка 2", { "Автор 4", "Авторк 3" });
|
||||||
db->persist(book);
|
|
||||||
t.commit();
|
|
||||||
}
|
|
||||||
|
|
||||||
std::cout << "Test data added successfully." << std::endl;
|
std::cout << "Test data added successfully." << std::endl;
|
||||||
}
|
}
|
||||||
@@ -124,47 +111,14 @@ 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));
|
uDBase db(openDB(dbPath));
|
||||||
|
|
||||||
// fillBooksBD(db);
|
// TODO Как-то нужно выполнять лишь раз
|
||||||
|
fillBooksBD(db);
|
||||||
|
|
||||||
RestApiServer server(*db);
|
RestApiServer server(*db);
|
||||||
server.start(8080);
|
server.start(8080);
|
||||||
|
|
||||||
/* Опыты с odb */
|
|
||||||
|
|
||||||
// const std::string dbPath = "test_db.sqlite";
|
|
||||||
|
|
||||||
// std::vector<Author_S> authors;
|
|
||||||
|
|
||||||
// odb::core::transaction trans(db->begin());
|
|
||||||
// for (auto data : fillDB())
|
|
||||||
// {
|
|
||||||
// auto a = Author_S(data.first, data.last, data.age);
|
|
||||||
// db->persist(a);
|
|
||||||
// }
|
|
||||||
// trans.commit();
|
|
||||||
|
|
||||||
// try
|
|
||||||
// {
|
|
||||||
// odb::core::transaction tr(db->begin());
|
|
||||||
// odb::query<Author_S> query;
|
|
||||||
// odb::result<Author_S> result;
|
|
||||||
|
|
||||||
// odb::result<Author_S> r(db->query<Author_S>(odb::query<Author_S>::age >= 2000));
|
|
||||||
|
|
||||||
// for (const auto& author : r)
|
|
||||||
// {
|
|
||||||
// std::cout << "First: " << author.first() << ", Last: " << author.last() << ", Year: " << author.age() << std::endl;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// tr.commit();
|
|
||||||
// }
|
|
||||||
// catch (const odb::exception& e)
|
|
||||||
// {
|
|
||||||
// std::cerr << "ODB error: " << e.what() << std::endl;
|
|
||||||
// return 1;
|
|
||||||
// }
|
|
||||||
// Set up code that uses the Qt event loop here.
|
// Set up code that uses the Qt event loop here.
|
||||||
// Call a.quit() or a.exit() to quit the application.
|
// Call a.quit() or a.exit() to quit the application.
|
||||||
// A not very useful example would be including
|
// A not very useful example would be including
|
||||||
|
|||||||
46
src/model/author_book_s.h
Normal file
46
src/model/author_book_s.h
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
#ifndef AUTHOR_BOOK_H
|
||||||
|
#define AUTHOR_BOOK_H
|
||||||
|
|
||||||
|
#include "model_global.h"
|
||||||
|
|
||||||
|
#include "author_s.h"
|
||||||
|
#include "book_s.h"
|
||||||
|
|
||||||
|
#pragma db object
|
||||||
|
class MODEL_EXPORT AuthorBook_S
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
AuthorBook_S() = default;
|
||||||
|
|
||||||
|
public:
|
||||||
|
AuthorBook_S(SH<Author_S> author,
|
||||||
|
SH<Book_S> book) :
|
||||||
|
m_author(author),
|
||||||
|
m_book(book) {}
|
||||||
|
|
||||||
|
quint64 id() const { return m_id; }
|
||||||
|
void setId(quint64 newId) { m_id = newId; }
|
||||||
|
|
||||||
|
SH<Author_S> author() const { return m_author; }
|
||||||
|
void setAuthor(const SH<Author_S>& newAuthor) { m_author = newAuthor; }
|
||||||
|
|
||||||
|
SH<Book_S> book() const { return m_book; }
|
||||||
|
void setBook(const SH<Book_S>& newBook) { m_book = newBook; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
friend class odb::access;
|
||||||
|
|
||||||
|
private:
|
||||||
|
#pragma db id auto
|
||||||
|
quint64 m_id;
|
||||||
|
|
||||||
|
#pragma db not_null
|
||||||
|
SH<Author_S> m_author;
|
||||||
|
|
||||||
|
#pragma db not_null
|
||||||
|
SH<Book_S> m_book;
|
||||||
|
|
||||||
|
#pragma db index("author_book_unique") member(m_author) member(m_book) unique
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // AUTHOR_BOOK_H
|
||||||
47
src/model/author_s.h
Normal file
47
src/model/author_s.h
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
#ifndef AUTHOR_S_H
|
||||||
|
#define AUTHOR_S_H
|
||||||
|
|
||||||
|
#include "model_global.h"
|
||||||
|
|
||||||
|
// @JsonSerializer
|
||||||
|
class Author_S;
|
||||||
|
|
||||||
|
#pragma db object
|
||||||
|
class MODEL_EXPORT Author_S
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Author_S() = default;
|
||||||
|
Author_S(const QString& fullName,
|
||||||
|
const QString& langCode) :
|
||||||
|
m_fullName(fullName),
|
||||||
|
m_langCode(langCode)
|
||||||
|
{}
|
||||||
|
|
||||||
|
quint64 id() const { return m_id; }
|
||||||
|
void setId(quint64 newId) { m_id = newId; }
|
||||||
|
|
||||||
|
QString fullName() const { return m_fullName; }
|
||||||
|
void setFullName(const QString& newFullName) { m_fullName = newFullName; }
|
||||||
|
|
||||||
|
QString langCode() const { return m_langCode; }
|
||||||
|
void setLangCode(const QString& newLangCode) { m_langCode = newLangCode; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
friend class odb::access;
|
||||||
|
|
||||||
|
public: // for Redkit-gen
|
||||||
|
#pragma db id auto
|
||||||
|
quint64 m_id;
|
||||||
|
|
||||||
|
QString m_fullName;
|
||||||
|
QString m_langCode;
|
||||||
|
};
|
||||||
|
|
||||||
|
#pragma db view object(Author_S)
|
||||||
|
struct person_stat
|
||||||
|
{
|
||||||
|
#pragma db column("count(" + Author_S::m_id + ")")
|
||||||
|
std::size_t count;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // AUTHOR_S_H
|
||||||
90
src/model/book_s.h
Normal file
90
src/model/book_s.h
Normal file
@@ -0,0 +1,90 @@
|
|||||||
|
#ifndef BOOK_S_H
|
||||||
|
#define BOOK_S_H
|
||||||
|
|
||||||
|
#include "model_global.h"
|
||||||
|
|
||||||
|
#include <QDateTime>
|
||||||
|
|
||||||
|
#include "author_s.h"
|
||||||
|
#include "genre_s.h"
|
||||||
|
#include "series_s.h"
|
||||||
|
|
||||||
|
struct Book_SZ
|
||||||
|
{
|
||||||
|
QString title;
|
||||||
|
SH<Series_S> series;
|
||||||
|
quint8 year;
|
||||||
|
SH<Genre_S> genre;
|
||||||
|
QDateTime lastModified = QDateTime::currentDateTime();
|
||||||
|
QString lang;
|
||||||
|
};
|
||||||
|
|
||||||
|
#pragma db object
|
||||||
|
class MODEL_EXPORT Book_S
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Book_S() = default;
|
||||||
|
Book_S(const Book_SZ& book)
|
||||||
|
{
|
||||||
|
m_title = book.title;
|
||||||
|
m_series = book.series;
|
||||||
|
m_year = book.year;
|
||||||
|
m_genre = book.genre;
|
||||||
|
m_lastModified = book.lastModified;
|
||||||
|
m_lang = book.lang;
|
||||||
|
}
|
||||||
|
|
||||||
|
quint64 id() const { return m_id; }
|
||||||
|
void setId(const quint64& newId) { m_id = newId; }
|
||||||
|
|
||||||
|
QString title() const { return m_title; }
|
||||||
|
void setTitle(const QString& newTitle) { m_title = newTitle; }
|
||||||
|
|
||||||
|
SH<Series_S> series() const { return m_series; }
|
||||||
|
void setSeries(const SH<Series_S>& newSeries) { m_series = newSeries; }
|
||||||
|
|
||||||
|
quint8 year() const { return m_year; }
|
||||||
|
void setYear(const quint8& newYear) { m_year = newYear; }
|
||||||
|
|
||||||
|
SH<Genre_S> genre() const { return m_genre; }
|
||||||
|
void setGenre(const SH<Genre_S>& newGenre) { m_genre = newGenre; }
|
||||||
|
|
||||||
|
QDateTime lastModified() const { return m_lastModified; }
|
||||||
|
void setLastModified(const QDateTime& newLastModified) { m_lastModified = newLastModified; }
|
||||||
|
|
||||||
|
QString lang() const { return m_lang; }
|
||||||
|
void setLang(const QString& newLang) { m_lang = newLang; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
friend class odb::access;
|
||||||
|
|
||||||
|
// public: // for Redkit-gen
|
||||||
|
private:
|
||||||
|
#pragma db id auto
|
||||||
|
quint64 m_id;
|
||||||
|
|
||||||
|
QString m_title;
|
||||||
|
SH<Series_S> m_series;
|
||||||
|
quint8 m_year;
|
||||||
|
SH<Genre_S> m_genre;
|
||||||
|
QDateTime m_lastModified;
|
||||||
|
QString m_lang;
|
||||||
|
};
|
||||||
|
|
||||||
|
// #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
|
||||||
28
src/model/genre_s.h
Normal file
28
src/model/genre_s.h
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
#ifndef GENRE_H
|
||||||
|
#define GENRE_H
|
||||||
|
|
||||||
|
#include "model_global.h"
|
||||||
|
|
||||||
|
#pragma db object
|
||||||
|
class MODEL_EXPORT Genre_S
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Genre_S() = default;
|
||||||
|
|
||||||
|
quint64 id() const { return m_id; }
|
||||||
|
void setId(const quint64& newId) { m_id = newId; }
|
||||||
|
|
||||||
|
QString name() const { return m_name; }
|
||||||
|
void setName(const QString& newName) { m_name = newName; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
friend class odb::access;
|
||||||
|
|
||||||
|
private:
|
||||||
|
#pragma db id auto
|
||||||
|
quint64 m_id;
|
||||||
|
|
||||||
|
QString m_name;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // GENRE_H
|
||||||
44
src/model/model.qbs
Normal file
44
src/model/model.qbs
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
/*!
|
||||||
|
\qmltype cpp-opds
|
||||||
|
\inherits Project
|
||||||
|
\brief Описание
|
||||||
|
*/
|
||||||
|
PSLibrary {
|
||||||
|
cpp.defines: [
|
||||||
|
// 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",
|
||||||
|
"MODEL_LIBRARY"
|
||||||
|
]
|
||||||
|
consoleApplication: true
|
||||||
|
|
||||||
|
Depends { name: "Qt"; submodules: [ "core", "network" ] }
|
||||||
|
Depends { name: "cpp" }
|
||||||
|
|
||||||
|
Depends { name: "odb.gen" }
|
||||||
|
Depends { name: "rdbase" }
|
||||||
|
|
||||||
|
Depends { name: "redkit_gen" }
|
||||||
|
redkit_gen.includeModules: [redkit_gen.module.JsonSerializer,
|
||||||
|
redkit_gen.module.SettingsSerializer,]
|
||||||
|
|
||||||
|
odb.gen.databases: "sqlite"
|
||||||
|
cpp.cxxLanguageVersion: "c++17"
|
||||||
|
|
||||||
|
Group {
|
||||||
|
id: odbs
|
||||||
|
name: "odb"
|
||||||
|
files: [
|
||||||
|
"**/*.h",
|
||||||
|
]
|
||||||
|
fileTags: ["hpp", "odbxx", "rgen"]
|
||||||
|
}
|
||||||
|
|
||||||
|
cpp.dynamicLibraries: [
|
||||||
|
"odb-sqlite",
|
||||||
|
"odb-qt",
|
||||||
|
"odb",
|
||||||
|
"sqlite3"
|
||||||
|
]
|
||||||
|
}
|
||||||
15
src/model/model_global.h
Normal file
15
src/model/model_global.h
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
#ifndef MODEL_GLOBAL_H
|
||||||
|
#define MODEL_GLOBAL_H
|
||||||
|
|
||||||
|
#include <QtCore/qglobal.h>
|
||||||
|
|
||||||
|
#if defined(MODEL_LIBRARY)
|
||||||
|
#define MODEL_EXPORT Q_DECL_EXPORT
|
||||||
|
#else
|
||||||
|
#define MODEL_EXPORT Q_DECL_IMPORT
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <odb/core.hxx>
|
||||||
|
#include <rcore/smarttypes.h>
|
||||||
|
|
||||||
|
#endif // MODEL_GLOBAL_H
|
||||||
28
src/model/series_s.h
Normal file
28
src/model/series_s.h
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
#ifndef SERIES_H
|
||||||
|
#define SERIES_H
|
||||||
|
|
||||||
|
#include "model_global.h"
|
||||||
|
|
||||||
|
#pragma db object
|
||||||
|
class MODEL_EXPORT Series_S
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Series_S() = default;
|
||||||
|
|
||||||
|
quint64 id() const { return m_id; }
|
||||||
|
void setId(const quint64& newId) { m_id = newId; }
|
||||||
|
|
||||||
|
QString serName() const { return m_serName; }
|
||||||
|
void setSerName(const QString& newSerName) { m_serName = newSerName; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
friend class odb::access;
|
||||||
|
|
||||||
|
private:
|
||||||
|
#pragma db id auto
|
||||||
|
quint64 m_id;
|
||||||
|
|
||||||
|
QString m_serName;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // SERIES_H
|
||||||
39
src/restapi/restapi.qbs
Normal file
39
src/restapi/restapi.qbs
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
/*!
|
||||||
|
\qmltype cpp-opds
|
||||||
|
\inherits Project
|
||||||
|
\brief Описание
|
||||||
|
*/
|
||||||
|
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.
|
||||||
|
//"QT_DISABLE_DEPRECATED_BEFORE=0x060000" // disables all the APIs deprecated before Qt 6.0.0
|
||||||
|
"DATABASE_SQLITE"
|
||||||
|
]
|
||||||
|
consoleApplication: true
|
||||||
|
|
||||||
|
Depends { name: "Qt"; submodules: [ "core", "sql", "network" ] }
|
||||||
|
Depends { name: "cpp" }
|
||||||
|
Depends { name: "database" }
|
||||||
|
Depends { name: "odb.gen" }
|
||||||
|
Depends { name: "redkit_gen" }
|
||||||
|
Depends { name: "rdbase" }
|
||||||
|
Depends { name: "model" }
|
||||||
|
|
||||||
|
cpp.cxxLanguageVersion: "c++20"
|
||||||
|
|
||||||
|
Group {
|
||||||
|
name: "cpp"
|
||||||
|
files: [
|
||||||
|
"restapiserver.*",
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
cpp.dynamicLibraries: [
|
||||||
|
"odb-sqlite",
|
||||||
|
"odb-qt",
|
||||||
|
"odb",
|
||||||
|
"sqlite3"
|
||||||
|
]
|
||||||
|
|
||||||
|
} // Project
|
||||||
@@ -5,15 +5,19 @@
|
|||||||
#include <QJsonObject>
|
#include <QJsonObject>
|
||||||
#include <QJsonValue>
|
#include <QJsonValue>
|
||||||
|
|
||||||
#include "book_s-odb.hxx"
|
#include <model/author_s-odb.hxx> // Должен быть здесь
|
||||||
#include "book_s.h"
|
#include <model/author_s.h>
|
||||||
|
#include <model/book_s-odb.hxx> // Должен быть здесь
|
||||||
|
#include <model/book_s.h>
|
||||||
|
|
||||||
#include <odb/core.hxx>
|
#include <odb/core.hxx>
|
||||||
#include <odb/database.hxx>
|
#include <odb/database.hxx>
|
||||||
#include <odb/query.hxx>
|
#include <odb/query.hxx>
|
||||||
|
|
||||||
|
#include <redkit-gen-modules/json_serializer.h>
|
||||||
|
|
||||||
RestApiServer::RestApiServer(odb::database& db, QObject* parent) :
|
RestApiServer::RestApiServer(odb::database& db, QObject* parent) :
|
||||||
m_db(db), QTcpServer(parent) {}
|
QTcpServer(parent), m_db(db) {}
|
||||||
|
|
||||||
void RestApiServer::start(quint16 port)
|
void RestApiServer::start(quint16 port)
|
||||||
{
|
{
|
||||||
@@ -23,7 +27,8 @@ void RestApiServer::start(quint16 port)
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
qDebug() << "REST API сервер запущен на порту:" << port;
|
qDebug() << "REST API сервер запущен по адресу:";
|
||||||
|
qDebug() << QString("http://127.0.0.1:%1").arg(port);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -34,6 +39,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()
|
||||||
@@ -52,28 +59,45 @@ void RestApiServer::handleRequest()
|
|||||||
|
|
||||||
QByteArray RestApiServer::processRequest(const QString& request)
|
QByteArray RestApiServer::processRequest(const QString& request)
|
||||||
{
|
{
|
||||||
|
// qWarning() << request << "\n\n";
|
||||||
|
|
||||||
if (request.startsWith("GET /books/author/"))
|
if (request.startsWith("GET /books/author/"))
|
||||||
{
|
{
|
||||||
QString author = request.section(' ', 1, 1).section('/', 3, 3).replace("%20", " ");
|
QString author = request.section(' ', 1, 1).section('/', 3, 3).replace("%20", " ");
|
||||||
|
// quint64 ageReq = author.toInt();
|
||||||
|
|
||||||
// Books books;
|
QStringList nameParts = author.split(' ');
|
||||||
// QList<BookRecord> results = books.getBooksByAuthor(author);
|
QString firstName = nameParts.size() > 0 ? nameParts[0] : "";
|
||||||
|
QString lastName = nameParts.size() > 1 ? nameParts[1] : "";
|
||||||
|
|
||||||
QByteArray jsonResponse = "{ \"books\": [";
|
odb::transaction t(m_db.begin());
|
||||||
// for (const BookRecord& book : results)
|
|
||||||
|
// auto books = m_db.query<Book_S>(odb::query<Book_S>::author->id == ageReq);
|
||||||
|
|
||||||
|
QJsonArray jArray;
|
||||||
|
// for (const auto& book : books)
|
||||||
// {
|
// {
|
||||||
// jsonResponse += QString("{ \"id\": %1, \"title\": \"%2\", \"author\": \"%3\", \"year\": %4 },")
|
// QJsonObject j;
|
||||||
// .arg(book.id)
|
// j["id"] = QString::number(book.id());
|
||||||
// .arg(book.title)
|
|
||||||
// .arg(book.author)
|
|
||||||
// .arg(book.year)
|
|
||||||
// .toUtf8();
|
|
||||||
// }
|
|
||||||
// if (results.size() > 0)
|
|
||||||
// jsonResponse.chop(1); // Убираем последнюю запятую
|
|
||||||
// jsonResponse += "] }";
|
|
||||||
|
|
||||||
return "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n" + jsonResponse;
|
// QJsonObject author;
|
||||||
|
// author["id"] = QString::number(book.author()->id());
|
||||||
|
// author["fullName"] = book.author()->fullName();
|
||||||
|
// author["langCode"] = book.author()->langCode();
|
||||||
|
// j["author"] = author;
|
||||||
|
// // j["series"] = book.series()->serName();
|
||||||
|
// // j["genre"] = book.genre()->name();
|
||||||
|
// j["title"] = book.title();
|
||||||
|
// j["year"] = book.year();
|
||||||
|
// j["lastModified"] = book.lastModified().toString();
|
||||||
|
// j["lang"] = book.lang();
|
||||||
|
// jArray.push_back(j);
|
||||||
|
// }
|
||||||
|
auto jDoc = QJsonDocument(jArray);
|
||||||
|
|
||||||
|
t.commit();
|
||||||
|
|
||||||
|
return "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n" + jDoc.toJson(QJsonDocument::Indented);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (request.startsWith("GET /books"))
|
if (request.startsWith("GET /books"))
|
||||||
@@ -82,20 +106,27 @@ QByteArray RestApiServer::processRequest(const QString& request)
|
|||||||
odb::result<Book_S> res(m_db.query<Book_S>());
|
odb::result<Book_S> res(m_db.query<Book_S>());
|
||||||
|
|
||||||
QJsonArray jArray;
|
QJsonArray jArray;
|
||||||
|
|
||||||
for (auto it = res.begin(); it != res.end(); ++it)
|
for (auto it = res.begin(); it != res.end(); ++it)
|
||||||
{
|
{
|
||||||
const auto& book = *it;
|
const auto& book = *it;
|
||||||
|
|
||||||
QJsonObject j;
|
QJsonObject j;
|
||||||
j["id"] = QString::number(book.id());
|
j["id"] = QString::number(book.id());
|
||||||
j["title"] = QString::fromStdString(book.name());
|
|
||||||
j["author"] = QString::fromStdString(book.author()->full_name());
|
// QJsonObject author;
|
||||||
j["year"] = book.year();
|
// author["id"] = QString::number(book.author()->id());
|
||||||
jArray.push_back(j);
|
// author["fullName"] = book.author()->fullName();
|
||||||
|
// author["langCode"] = book.author()->langCode();
|
||||||
|
// j["author"] = author;
|
||||||
|
// // j["series"] = book.series()->serName();
|
||||||
|
// // j["genre"] = book.genre()->name();
|
||||||
|
// j["title"] = book.title();
|
||||||
|
// j["year"] = book.year();
|
||||||
|
// j["lastModified"] = book.lastModified().toString();
|
||||||
|
// j["lang"] = book.lang();
|
||||||
|
// jArray.push_back(j);
|
||||||
}
|
}
|
||||||
auto jDoc = QJsonDocument(jArray);
|
auto jDoc = QJsonDocument(jArray);
|
||||||
|
|
||||||
t.commit();
|
t.commit();
|
||||||
|
|
||||||
return "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n" + jDoc.toJson(QJsonDocument::Indented);
|
return "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n" + jDoc.toJson(QJsonDocument::Indented);
|
||||||
@@ -107,21 +138,19 @@ QByteArray RestApiServer::processRequest(const QString& request)
|
|||||||
odb::result<Author_S> res(m_db.query<Author_S>());
|
odb::result<Author_S> res(m_db.query<Author_S>());
|
||||||
|
|
||||||
QJsonArray jArray;
|
QJsonArray jArray;
|
||||||
|
|
||||||
for (auto it = res.begin(); it != res.end(); ++it)
|
for (auto it = res.begin(); it != res.end(); ++it)
|
||||||
{
|
{
|
||||||
const auto& author = *it;
|
const auto& author = *it;
|
||||||
|
|
||||||
QJsonObject j;
|
QJsonObject j;
|
||||||
j["id"] = QString::number(author.id());
|
j["id"] = QString::number(author.id());
|
||||||
j["first"] = QString::fromStdString(author.first());
|
j["fullName"] = author.fullName();
|
||||||
j["last"] = QString::fromStdString(author.last());
|
j["langCode"] = author.langCode();
|
||||||
j["age"] = author.age();
|
|
||||||
jArray.push_back(j);
|
jArray.push_back(j);
|
||||||
}
|
}
|
||||||
auto jDoc = QJsonDocument(jArray);
|
auto jDoc = QJsonDocument(jArray);
|
||||||
|
|
||||||
t.commit();
|
t.commit();
|
||||||
|
|
||||||
return "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n" + jDoc.toJson(QJsonDocument::Indented);
|
return "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n" + jDoc.toJson(QJsonDocument::Indented);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -3,10 +3,11 @@
|
|||||||
|
|
||||||
#include <odb/database.hxx>
|
#include <odb/database.hxx>
|
||||||
|
|
||||||
|
#include <QObject>
|
||||||
#include <QTcpServer>
|
#include <QTcpServer>
|
||||||
#include <QTcpSocket>
|
#include <QTcpSocket>
|
||||||
|
|
||||||
class RestApiServer : public QTcpServer
|
class Q_DECL_EXPORT RestApiServer : public QTcpServer
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
Reference in New Issue
Block a user