Разнос по библиотекам + прикручиваем odb из redkitty
This commit was merged in pull request #5.
This commit is contained in:
64
src/database/author_s.h
Normal file
64
src/database/author_s.h
Normal file
@@ -0,0 +1,64 @@
|
||||
// file : hello/person.hxx
|
||||
// copyright : not copyrighted - public domain
|
||||
|
||||
#ifndef AUTHOR_S_H
|
||||
#define AUTHOR_S_H
|
||||
|
||||
#include "database_global.h"
|
||||
|
||||
#include <QString>
|
||||
|
||||
#include <odb/core.hxx>
|
||||
#include <odb/database.hxx>
|
||||
#include <odb/query.hxx>
|
||||
|
||||
#pragma db object
|
||||
class DATABASE_EXPORT Author_S
|
||||
{
|
||||
public:
|
||||
Author_S() = default;
|
||||
|
||||
Author_S(const QString& firstN,
|
||||
const QString& lastN,
|
||||
const unsigned short age) :
|
||||
m_firstName(firstN),
|
||||
m_lastName(lastN),
|
||||
m_age(age)
|
||||
{
|
||||
}
|
||||
|
||||
unsigned long long id() const { return m_id; }
|
||||
QString firstName() const { return m_firstName; }
|
||||
QString lastName() const { return m_lastName; }
|
||||
unsigned short age() const { return m_age; }
|
||||
|
||||
QString full_name() const { return m_firstName + " " + m_lastName; }
|
||||
|
||||
void age(unsigned short age) { m_age = age; }
|
||||
|
||||
private:
|
||||
friend class odb::access;
|
||||
|
||||
private:
|
||||
#pragma db id auto
|
||||
quint64 m_id;
|
||||
|
||||
QString m_firstName;
|
||||
QString m_lastName;
|
||||
quint8 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
|
||||
64
src/database/book_s.h
Normal file
64
src/database/book_s.h
Normal file
@@ -0,0 +1,64 @@
|
||||
// file : hello/person.hxx
|
||||
// copyright : not copyrighted - public domain
|
||||
|
||||
#ifndef BOOK_S_H
|
||||
#define BOOK_S_H
|
||||
|
||||
#include "database_global.h"
|
||||
|
||||
#include <QSharedPointer>
|
||||
#include <QString>
|
||||
#include <odb/core.hxx>
|
||||
#include <rcore/smarttypes.h>
|
||||
|
||||
#include "author_s.h"
|
||||
|
||||
#pragma db object
|
||||
class DATABASE_EXPORT Book_S
|
||||
{
|
||||
public:
|
||||
Book_S() = default;
|
||||
|
||||
Book_S(const QString& name,
|
||||
const SH<Author_S>& author,
|
||||
const int year)
|
||||
{
|
||||
m_author = author;
|
||||
m_name = name;
|
||||
m_year = year;
|
||||
};
|
||||
|
||||
unsigned long long id() const { return m_id; }
|
||||
SH<Author_S> author() const { return m_author; }
|
||||
QString name() const { return m_name; }
|
||||
int year() const { return m_year; }
|
||||
|
||||
private:
|
||||
friend class odb::access;
|
||||
|
||||
private:
|
||||
#pragma db id auto
|
||||
quint64 m_id;
|
||||
|
||||
SH<Author_S> m_author;
|
||||
QString m_name;
|
||||
quint8 m_year;
|
||||
};
|
||||
|
||||
// #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
|
||||
183
src/database/database.hxx
Normal file
183
src/database/database.hxx
Normal file
@@ -0,0 +1,183 @@
|
||||
// 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>
|
||||
#include <rcore/smarttypes.h>
|
||||
|
||||
using oDBase = odb::sqlite::database;
|
||||
using uDBase = U<oDBase>;
|
||||
|
||||
struct testData
|
||||
{
|
||||
QString first;
|
||||
QString last;
|
||||
int age;
|
||||
};
|
||||
|
||||
// Функция для генерации случайной строки (имени или фамилии)
|
||||
QString generate_random_string(const QVector<QString>& 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);
|
||||
}
|
||||
|
||||
QVector<testData> fillDB()
|
||||
{
|
||||
QVector<QString> 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"
|
||||
};
|
||||
QVector<QString> 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"
|
||||
};
|
||||
|
||||
QVector<testData> vecTest;
|
||||
|
||||
for (int i = 0; i < 50; ++i)
|
||||
{
|
||||
QString first_name = generate_random_string(first_names);
|
||||
QString 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 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
|
||||
@@ -1,16 +1,55 @@
|
||||
import qbs
|
||||
|
||||
/*!
|
||||
\qmltype cpp-opds
|
||||
\inherits Project
|
||||
\brief Описание
|
||||
*/
|
||||
PSLibrary {
|
||||
Depends { name: "Qt"; submodules: "sql"}
|
||||
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: "model" }
|
||||
Depends { name: "sql_builder" }
|
||||
Depends { name: "Qt"; submodules: [ "core", "sql", "network" ] }
|
||||
Depends { name: "cpp" }
|
||||
|
||||
Depends { name: "odb.gen" }
|
||||
Depends { name: "rdbase" }
|
||||
|
||||
Depends { name: "redkit_gen" }
|
||||
redkit_gen.includeModules: ["JsonSerializer"]
|
||||
|
||||
odb.gen.databases: "sqlite"
|
||||
cpp.cxxLanguageVersion: "c++17"
|
||||
|
||||
Group {
|
||||
name: "cpp"
|
||||
files: [
|
||||
"**/*.h",
|
||||
"**/*.cpp"
|
||||
"databasemanager.*",
|
||||
"*.cpp",
|
||||
"*.h",
|
||||
"*.hxx",
|
||||
]
|
||||
excludeFiles: odbs.files
|
||||
}
|
||||
|
||||
Group {
|
||||
id: odbs
|
||||
name: "odb"
|
||||
files: [
|
||||
"author_s.h",
|
||||
"book_s.h",
|
||||
]
|
||||
fileTags: ["hpp", "odbxx"]
|
||||
}
|
||||
|
||||
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
|
||||
@@ -1,38 +0,0 @@
|
||||
#include "databasemanager.h"
|
||||
|
||||
#include <model/author.h>
|
||||
#include <model/book.h>
|
||||
|
||||
DatabaseManager::DatabaseManager() {}
|
||||
|
||||
DatabaseManager::~DatabaseManager()
|
||||
{
|
||||
if (db.isOpen())
|
||||
db.close();
|
||||
}
|
||||
|
||||
DatabaseManager& DatabaseManager::instance()
|
||||
{
|
||||
static DatabaseManager instance;
|
||||
return instance;
|
||||
}
|
||||
|
||||
bool DatabaseManager::connect(const QString& dbPath)
|
||||
{
|
||||
db = QSqlDatabase::addDatabase("QSQLITE");
|
||||
db.setDatabaseName(dbPath);
|
||||
return db.open();
|
||||
}
|
||||
|
||||
QSqlDatabase& DatabaseManager::database()
|
||||
{
|
||||
return db;
|
||||
}
|
||||
|
||||
void DatabaseManager::initializeDatabase()
|
||||
{
|
||||
QSqlQuery query;
|
||||
|
||||
query.exec(model::createTableBook());
|
||||
query.exec(model::createTableAuthor());
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
#ifndef DATABASEMANAGER_H
|
||||
#define DATABASEMANAGER_H
|
||||
|
||||
#include <QDebug>
|
||||
#include <QtSql>
|
||||
|
||||
class Q_DECL_EXPORT DatabaseManager
|
||||
{
|
||||
public:
|
||||
static DatabaseManager& instance();
|
||||
bool connect(const QString& dbPath);
|
||||
QSqlDatabase& database();
|
||||
void initializeDatabase();
|
||||
|
||||
private:
|
||||
DatabaseManager();
|
||||
~DatabaseManager();
|
||||
QSqlDatabase db;
|
||||
};
|
||||
|
||||
#endif // DATABASEMANAGER_H
|
||||
Reference in New Issue
Block a user