Подключаем еще некоторые привычные вещи работы с odb
This commit is contained in:
@@ -6,10 +6,10 @@
|
||||
|
||||
#include "database_global.h"
|
||||
|
||||
#include <QString>
|
||||
#include <QSharedPointer>
|
||||
|
||||
#include <QString>
|
||||
#include <odb/core.hxx>
|
||||
#include <rcore/smarttypes.h>
|
||||
|
||||
#include "author_s.h"
|
||||
|
||||
@@ -19,8 +19,8 @@ class DATABASE_EXPORT Book_S
|
||||
public:
|
||||
Book_S() = default;
|
||||
|
||||
Book_S(const QString & name,
|
||||
const QSharedPointer<Author_S>& author,
|
||||
Book_S(const QString& name,
|
||||
const SH<Author_S>& author,
|
||||
const int year)
|
||||
{
|
||||
m_author = author;
|
||||
@@ -29,8 +29,8 @@ public:
|
||||
};
|
||||
|
||||
unsigned long long id() const { return m_id; }
|
||||
QSharedPointer<Author_S> author() const { return m_author; }
|
||||
QString name() const { return m_name; }
|
||||
SH<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;
|
||||
|
||||
QSharedPointer<Author_S> m_author;
|
||||
QString m_name;
|
||||
SH<Author_S> m_author;
|
||||
QString m_name;
|
||||
int m_year;
|
||||
};
|
||||
|
||||
|
||||
@@ -34,19 +34,20 @@
|
||||
|
||||
#include <filesystem>
|
||||
#include <random>
|
||||
#include <rcore/smarttypes.h>
|
||||
|
||||
using odatabase = odb::sqlite::database;
|
||||
using unidb = std::unique_ptr<odatabase>;
|
||||
using oDBase = odb::sqlite::database;
|
||||
using uDBase = U<oDBase>;
|
||||
|
||||
struct testData
|
||||
{
|
||||
std::string first;
|
||||
std::string last;
|
||||
QString first;
|
||||
QString last;
|
||||
int age;
|
||||
};
|
||||
|
||||
// Функция для генерации случайной строки (имени или фамилии)
|
||||
std::string generate_random_string(const std::vector<std::string>& pool)
|
||||
QString generate_random_string(const QVector<QString>& pool)
|
||||
{
|
||||
static std::random_device rd;
|
||||
static std::mt19937 gen(rd());
|
||||
@@ -63,16 +64,16 @@ int generate_random_year(int min_year = 1900, int max_year = 2020)
|
||||
return dis(gen);
|
||||
}
|
||||
|
||||
std::vector<testData> fillDB()
|
||||
QVector<testData> fillDB()
|
||||
{
|
||||
std::vector<std::string> first_names = {
|
||||
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"
|
||||
};
|
||||
std::vector<std::string> last_names = {
|
||||
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",
|
||||
@@ -80,12 +81,12 @@ std::vector<testData> fillDB()
|
||||
"Rogers", "Gutierrez", "Ramirez", "Diaz", "Perez", "Ross", "Sanders", "Price", "Howard", "Cooper"
|
||||
};
|
||||
|
||||
std::vector<testData> vecTest;
|
||||
QVector<testData> vecTest;
|
||||
|
||||
for (int i = 0; i < 50; ++i)
|
||||
{
|
||||
std::string first_name = generate_random_string(first_names);
|
||||
std::string last_name = generate_random_string(last_names);
|
||||
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 });
|
||||
@@ -94,16 +95,16 @@ std::vector<testData> fillDB()
|
||||
return vecTest;
|
||||
}
|
||||
|
||||
inline unidb openDB(const std::string path_db)
|
||||
inline uDBase openDB(const std::string path_db)
|
||||
{
|
||||
// Открыть БД, если она существует
|
||||
auto db_ptr = new odatabase(path_db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE);
|
||||
auto db_ptr = new oDBase(path_db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE);
|
||||
|
||||
if (std::filesystem::is_regular_file(path_db))
|
||||
return unidb(db_ptr);
|
||||
return uDBase(db_ptr);
|
||||
|
||||
// И создать новую, если не существует
|
||||
unidb db(db_ptr);
|
||||
uDBase db(db_ptr);
|
||||
|
||||
odb::connection_ptr connect_ptr(db->connection());
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@ PSLibrary {
|
||||
Depends { name: "cpp" }
|
||||
Depends { name: "odb.gen" }
|
||||
Depends { name: "redkit_gen" }
|
||||
Depends { name: "rdbase" }
|
||||
|
||||
odb.gen.databases: "sqlite"
|
||||
cpp.cxxLanguageVersion: "c++17"
|
||||
|
||||
Reference in New Issue
Block a user