Compare commits

..

3 Commits

Author SHA1 Message Date
11ea3a8cd8 Пока не билдится 2025-02-02 11:58:56 +05:00
9751db9617 gitignore 2025-02-02 11:31:54 +05:00
0d313e578c build 2025-02-02 11:29:32 +05:00
8 changed files with 112 additions and 7 deletions

4
.gitignore vendored
View File

@@ -1,3 +1,7 @@
# user ignore
build
opds.db
# ---> C++ # ---> C++
# Prerequisites # Prerequisites
*.d *.d

View File

@@ -1,14 +1,58 @@
cmake_minimum_required(VERSION 3.14) cmake_minimum_required(VERSION 3.16)
project(cpp-opds) project(cpp-opds)
set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(ODB_INCLUDE_DIRS /usr/include)
set(ODB_LIBRARY_DIRS /usr/lib/x86_64-linux-gnu)
include_directories(${ODB_INCLUDE_DIRS})
link_directories(${ODB_LIBRARY_DIRS})
find_package(Qt6 REQUIRED COMPONENTS Core Sql Network) find_package(Qt6 REQUIRED COMPONENTS Core Sql Network)
# find_package(ODB REQUIRED)
# find_package(SQLite3 REQUIRED)
add_executable(cpp-opds find_library(ODB_LIBRARIES NAMES odb HINTS ${ODB_LIBRARY_DIRS})
find_library(ODB_SQLITE_LIBRARIES NAMES odb-sqlite HINTS ${ODB_LIBRARY_DIRS})
set(CMAKE_AUTOMOC ON) # <-- Включаем автоматическую генерацию MOC-файлов
# include_directories(${ODB_INCLUDE_DIRS} ${SQLite3_INCLUDE_DIRS})
# link_directories(${ODB_LIBRARY_DIRS} ${SQLite3_LIBRARY_DIRS})
# add_executable(cpp-opds
# src/main.cpp
# include/backend.cpp
# )
set(SOURCE_FILES
src/main.cpp src/main.cpp
src/backend.cpp # src/database.cpp
# src/book.cpp
# src/author.hpp
) )
set(ODB_SOURCES
src/book
src/author
)
# Генерация ODB файлов
foreach(file ${ODB_SOURCES})
add_custom_command(
OUTPUT ${file}.odb.cpp ${file}.odb.hpp
COMMAND odb --generate-query --generate-schema --database sqlite ${file}.hpp
DEPENDS ${file}.hpp
COMMENT "Running ODB on ${file}.hpp"
)
list(APPEND SOURCE_FILES ${file}.odb.cpp)
endforeach()
add_executable(cpp-opds ${SOURCE_FILES})
target_include_directories(cpp-opds PRIVATE include) target_include_directories(cpp-opds PRIVATE include)
target_link_libraries(cpp-opds Qt6::Core Qt6::Sql Qt6::Network) # target_link_libraries(cpp-opds Qt6::Core Qt6::Sql Qt6::Network ${ODB_LIBRARIES} ${SQLite3_LIBRARIES})
target_link_libraries(cpp-opds Qt6::Core Qt6::Sql Qt6::Network ${ODB_LIBRARIES} ${ODB_SQLITE_LIBRARIES})

View File

@@ -1,17 +1,24 @@
#include "backend.h" #include "backend.h"
#include <QtCore/QDebug> #include <QtCore/QDebug>
Backend::Backend(QObject *parent) : QObject(parent) Backend::Backend(QObject* parent) :
QObject(parent)
{ {
// Используем SQLite по умолчанию, для PostgreSQL потребуется изменить параметры подключения // Используем SQLite по умолчанию, для PostgreSQL потребуется изменить параметры подключения
db = QSqlDatabase::addDatabase("QSQLITE"); db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("opds.db"); db.setDatabaseName("opds.db");
if (!db.open()) { if (!db.open())
{
qDebug() << "Error: Unable to open database"; qDebug() << "Error: Unable to open database";
} }
} }
Backend::~Backend()
{
qDebug() << "Backend stoped.";
}
void Backend::start() void Backend::start()
{ {
qDebug() << "Backend started."; qDebug() << "Backend started.";

View File

@@ -10,6 +10,8 @@ class Backend : public QObject
public: public:
explicit Backend(QObject* parent = nullptr); explicit Backend(QObject* parent = nullptr);
~Backend() override;
void start(); void start();
private: private:

13
src/author.hpp Normal file
View File

@@ -0,0 +1,13 @@
#pragma db object
class Author {
public:
Author() = default;
Author(const std::string& name) : name(name) {}
const std::string& getName() const { return name; }
private:
#pragma db id auto
int id;
std::string name;
};

22
src/book.hpp Normal file
View File

@@ -0,0 +1,22 @@
#pragma db object
class Book {
public:
Book() = default;
Book(const std::string& title, const std::string& author, const std::string& filePath,
int fileSize, const std::string& format);
const std::string& getTitle() const { return title; }
const std::string& getAuthor() const { return author; }
const std::string& getFilePath() const { return filePath; }
int getFileSize() const { return fileSize; }
const std::string& getFormat() const { return format; }
private:
#pragma db id auto
int id;
std::string title;
std::string author;
std::string filePath;
int fileSize;
std::string format;
};

13
src/database.cpp Normal file
View File

@@ -0,0 +1,13 @@
#include "database.h"
#include <odb/sqlite/database.hxx>
#include <odb/schema-catalog.hxx>
#include "book-odb.hxx"
#include "author-odb.hxx"
std::unique_ptr<odb::sqlite::database> initializeDatabase(const std::string& dbPath) {
auto db = std::make_unique<odb::sqlite::database>(dbPath, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE);
odb::transaction t(db->begin());
odb::schema_catalog::create_schema(*db);
t.commit();
return db;
}

0
src/database.h Normal file
View File