Билдим с odb из redkitty

This commit is contained in:
2025-08-03 00:14:45 +05:00
parent 3c2b8e7cf4
commit f4958e60bb
14 changed files with 117 additions and 158 deletions

View File

@@ -4,24 +4,22 @@
#ifndef AUTHOR_S_H
#define AUTHOR_S_H
// #include <QtCore/QObject>
#include "database_global.h"
#include <cstddef> // std::size_t
#include <memory>
#include <string>
#include <QString>
#include <odb/core.hxx>
#include <odb/database.hxx>
#include <odb/query.hxx>
#pragma db object
class __attribute__((visibility("default"))) Author_S
class DATABASE_EXPORT Author_S
{
public:
Author_S() = default;
Author_S(const std::string& first,
const std::string& last,
Author_S(const QString& first,
const QString& last,
const unsigned short age) :
m_first(first),
m_last(last),
@@ -30,11 +28,11 @@ public:
}
unsigned long long id() const { return m_id; }
std::string first() const { return m_first; }
std::string last() const { return m_last; }
QString first() const { return m_first; }
QString last() const { return m_last; }
unsigned short age() const { return m_age; }
std::string full_name() const { return m_first + " " + m_last; }
QString full_name() const { return m_first + " " + m_last; }
void age(unsigned short age) { m_age = age; }
@@ -44,8 +42,8 @@ private:
#pragma db id auto
unsigned long long m_id;
std::string m_first;
std::string m_last;
QString m_first;
QString m_last;
unsigned short m_age;
};

View File

@@ -4,23 +4,23 @@
#ifndef BOOK_S_H
#define BOOK_S_H
// #include <QtCore/QObject>
#include "database_global.h"
#include <memory>
#include <string>
#include <QString>
#include <QSharedPointer>
#include <odb/core.hxx>
#include "author_s.h"
#pragma db object
class __attribute__((visibility("default"))) Book_S
class DATABASE_EXPORT Book_S
{
public:
Book_S() = default;
Book_S(const std::string& name,
const std::shared_ptr<Author_S>& author,
Book_S(const QString & name,
const QSharedPointer<Author_S>& author,
const int year)
{
m_author = author;
@@ -29,8 +29,8 @@ public:
};
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; }
QSharedPointer<Author_S> author() const { return m_author; }
QString name() const { return m_name; }
int year() const { return m_year; }
private:
@@ -40,8 +40,8 @@ private:
#pragma db id auto
unsigned long long m_id;
std::shared_ptr<Author_S> m_author;
std::string m_name;
QSharedPointer<Author_S> m_author;
QString m_name;
int m_year;
};

View File

@@ -35,6 +35,9 @@
#include <filesystem>
#include <random>
using odatabase = odb::sqlite::database;
using unidb = std::unique_ptr<odatabase>;
struct testData
{
std::string first;
@@ -91,21 +94,23 @@ std::vector<testData> fillDB()
return vecTest;
}
inline std::unique_ptr<odb::database> openDB(std::string path_db)
inline unidb openDB(const std::string path_db)
{
// Открыть БД, если она существует
auto db_ptr = new odatabase(path_db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE);
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));
return unidb(db_ptr);
// И создать новую, если не существует
std::unique_ptr<odb::core::database> db(new odb::sqlite::database(path_db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE));
unidb db(db_ptr);
odb::core::connection_ptr connect_ptr(db->connection());
odb::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);
odb::transaction tans(db->begin());
odb::schema_catalog::create_schema(*db);
tans.commit();
connect_ptr->execute("PRAGMA foreign_keys=ON");

View File

@@ -8,45 +8,45 @@ PSLibrary {
// 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_SQLITE",
"DATABASE_LIBRARY"
]
consoleApplication: true
Depends { name: "Qt"; submodules: [ "core", "sql", "network" ] }
Depends { name: "cpp" }
Depends { name: "odbModule"}
Depends { name: "odb.gen" }
Depends { name: "redkit_gen" }
cpp.cxxLanguageVersion: "c++20"
odb.gen.databases: "sqlite"
cpp.cxxLanguageVersion: "c++17"
Group {
name: "cpp"
files: [
"books.*",
"databasemanager.*",
"*.cpp",
"*.h",
"*.hxx",
]
excludeFiles: odbs.files
}
Group {
id: odbs
name: "odb"
files: [
"author_s.h",
"book_s.h",
]
fileTags: ["odb"]
fileTags: ["hpp", "odbxx", "odb"]
}
cpp.includePaths: [
"/usr/include", // Общие заголовки
"/usr/include/odb" // Заголовки ODB
]
// Подключаем библиотеки ODB
cpp.libraryPaths: [
"/usr/lib" // Путь к библиотекам
]
cpp.dynamicLibraries: [
"odb-sqlite",
"odb-qt",
"odb",
"odb-sqlite"
"sqlite3"
]
}

View 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

View File

@@ -1,6 +1,8 @@
#ifndef DATABASEMANAGER_H
#define DATABASEMANAGER_H
#include "database_global.h"
#include <QDebug>
#include <QtSql>
@@ -35,7 +37,7 @@ struct SelectBuilder
}
};
class Q_DECL_EXPORT DatabaseManager
class DATABASE_EXPORT DatabaseManager
{
public:
static DatabaseManager& instance(); // Singleton