Compare commits
3 Commits
4e4e3d8a45
...
11ea3a8cd8
| Author | SHA1 | Date | |
|---|---|---|---|
| 11ea3a8cd8 | |||
| 9751db9617 | |||
| 0d313e578c |
4
.gitignore
vendored
4
.gitignore
vendored
@@ -1,3 +1,7 @@
|
||||
# user ignore
|
||||
build
|
||||
opds.db
|
||||
|
||||
# ---> C++
|
||||
# Prerequisites
|
||||
*.d
|
||||
|
||||
@@ -1,14 +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)
|
||||
|
||||
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/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})
|
||||
|
||||
@@ -1,17 +1,24 @@
|
||||
#include "backend.h"
|
||||
#include <QtCore/QDebug>
|
||||
|
||||
Backend::Backend(QObject *parent) : QObject(parent)
|
||||
Backend::Backend(QObject* parent) :
|
||||
QObject(parent)
|
||||
{
|
||||
// Используем SQLite по умолчанию, для PostgreSQL потребуется изменить параметры подключения
|
||||
db = QSqlDatabase::addDatabase("QSQLITE");
|
||||
db.setDatabaseName("opds.db");
|
||||
|
||||
if (!db.open()) {
|
||||
if (!db.open())
|
||||
{
|
||||
qDebug() << "Error: Unable to open database";
|
||||
}
|
||||
}
|
||||
|
||||
Backend::~Backend()
|
||||
{
|
||||
qDebug() << "Backend stoped.";
|
||||
}
|
||||
|
||||
void Backend::start()
|
||||
{
|
||||
qDebug() << "Backend started.";
|
||||
|
||||
@@ -10,6 +10,8 @@ class Backend : public QObject
|
||||
|
||||
public:
|
||||
explicit Backend(QObject* parent = nullptr);
|
||||
~Backend() override;
|
||||
|
||||
void start();
|
||||
|
||||
private:
|
||||
|
||||
13
src/author.hpp
Normal file
13
src/author.hpp
Normal 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
22
src/book.hpp
Normal 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
13
src/database.cpp
Normal 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
0
src/database.h
Normal file
Reference in New Issue
Block a user