178 lines
5.8 KiB
C++
178 lines
5.8 KiB
C++
|
|
// 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>
|
||
|
|
|
||
|
|
struct testData
|
||
|
|
{
|
||
|
|
std::string first;
|
||
|
|
std::string last;
|
||
|
|
int age;
|
||
|
|
};
|
||
|
|
|
||
|
|
// Функция для генерации случайной строки (имени или фамилии)
|
||
|
|
std::string generate_random_string(const std::vector<std::string>& 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);
|
||
|
|
}
|
||
|
|
|
||
|
|
std::vector<testData> fillDB()
|
||
|
|
{
|
||
|
|
std::vector<std::string> 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 = {
|
||
|
|
"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"
|
||
|
|
};
|
||
|
|
|
||
|
|
std::vector<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);
|
||
|
|
int birth_year = generate_random_year(1900, 2000);
|
||
|
|
|
||
|
|
vecTest.push_back({ first_name, last_name, birth_year });
|
||
|
|
}
|
||
|
|
|
||
|
|
return vecTest;
|
||
|
|
}
|
||
|
|
|
||
|
|
inline std::unique_ptr<odb::database> openDB(std::string path_db)
|
||
|
|
{
|
||
|
|
// Открыть БД, если она существует
|
||
|
|
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));
|
||
|
|
|
||
|
|
// И создать новую, если не существует
|
||
|
|
std::unique_ptr<odb::core::database> db(new odb::sqlite::database(path_db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE));
|
||
|
|
|
||
|
|
odb::core::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);
|
||
|
|
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
|