diff --git a/CMakeLists.txt b/CMakeLists.txt index a462a39..40afeae 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,16 +1,58 @@ -cmake_minimum_required(VERSION 3.14) +cmake_minimum_required(VERSION 3.16) project(cpp-opds) 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(ODB REQUIRED) +# find_package(SQLite3 REQUIRED) + +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-файлов -add_executable(cpp-opds +# 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 - include/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_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}) diff --git a/src/author.hpp b/src/author.hpp new file mode 100644 index 0000000..69d1ce2 --- /dev/null +++ b/src/author.hpp @@ -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; +}; \ No newline at end of file diff --git a/src/book.hpp b/src/book.hpp new file mode 100644 index 0000000..f6b5c2b --- /dev/null +++ b/src/book.hpp @@ -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; +}; \ No newline at end of file diff --git a/src/database.cpp b/src/database.cpp new file mode 100644 index 0000000..4bbec8c --- /dev/null +++ b/src/database.cpp @@ -0,0 +1,13 @@ +#include "database.h" +#include +#include +#include "book-odb.hxx" +#include "author-odb.hxx" + +std::unique_ptr initializeDatabase(const std::string& dbPath) { + auto db = std::make_unique(dbPath, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE); + odb::transaction t(db->begin()); + odb::schema_catalog::create_schema(*db); + t.commit(); + return db; +} \ No newline at end of file diff --git a/src/database.h b/src/database.h new file mode 100644 index 0000000..e69de29