#include "databaselms.h"

#include <QtSql>
#include <QSqlDatabase>
#include <QMessageBox>

DataBaseLMS::DataBaseLMS():
    db(nullptr),
    transactionBegined(false)
{

}

DataBaseLMS::~DataBaseLMS()
{
    deleteConnection();
}

bool DataBaseLMS::createConnection()
{
    mtxAccess.lock();

    db = new QSqlDatabase(QSqlDatabase::addDatabase(dbType));
    db->setDatabaseName(dbName);
    db->setUserName(dbUserName);
    db->setPassword(dbPassword);
    if(!db->open())
    {
        mtxAccess.unlock();
        deleteConnection();
        return false;
    }
    else
    {
        mtxAccess.unlock();
        return true;
    }
}

void DataBaseLMS::deleteConnection()
{
    mtxAccess.lock();

    if(transactionBegined)
        QSqlDatabase::database().rollback();

    if(db != nullptr)
    {
        if(db->isOpen())
            db->close();

        delete  db;
        db = nullptr;
    }

    mtxAccess.unlock();
}

bool DataBaseLMS::isConnected()
{
    mtxAccess.lock();

    if(db == nullptr)
    {
        mtxAccess.unlock();
        return false;
    }
    else
    {
        if(db->isOpen())
        {
            mtxAccess.unlock();
            return true;
        }
    }

    mtxAccess.unlock();
    return false;
}

bool DataBaseLMS::transactionBegin()
{
    /*
    if(transactionBegined)
        QSqlDatabase::database().rollback();

    transactionBegined = true;

    return QSqlDatabase::database().transaction();
    */
    return true;
}

bool DataBaseLMS::transactionEnd()
{
    /*
    if(transactionBegined)
    {
        transactionBegined = false;

        return QSqlDatabase::database().commit();
    }
    return false;
    */
    return true;
}

QList<Instructor> DataBaseLMS::selectAllInstructors()
{
    QList<Instructor> listInstructors;

    QString queryStr = QString("SELECT instructor_id, name, login, password, is_admin, archived, logged_in "
                                                 "FROM public.instructors "
                                                 "ORDER BY instructor_id ASC");

    QSqlQuery query = QSqlQuery(*db);

    if(queryExec(queryStr, &query))
    {
        while (query.next())
        {//Инструктор
            Instructor instructor;
            instructor.setID(query.value(0).toInt());
            instructor.setName(query.value(1).toString());
            instructor.setLogin(query.value(2).toString());
            instructor.setPassword(query.value(3).toString());
            instructor.setIsAdmin(query.value(4).toBool());
            instructor.setArchived(query.value(5).toBool());
            instructor.setLoggedIn(query.value(6).toBool());

            listInstructors.append(instructor);
        }
    }

    return listInstructors;
}

QList<Trainee> DataBaseLMS::selectAllTrainees()
{
    QList<Trainee> listTrainees;

    QString queryStr = QString("SELECT trainees.trainee_id, trainees.name, trainees.login, trainees.password, trainees.archived, trainees.logged_in, "
                                                 "groups.group_id, groups.name, "
                                                 "computers.computer_id, computers.name, computers.ip_address, "
                                                 "classrooms.classroom_id, classrooms.name "
                                                 "FROM public.trainees JOIN public.groups ON groups.group_id = trainees.group_trainee "
                                                 "LEFT OUTER JOIN public.computers ON computers.computer_id = trainees.computer_trainee "
                                                 "LEFT OUTER JOIN public.classrooms ON classrooms.classroom_id = computers.classroom_computer "
                                                 "ORDER BY groups.name, trainees.name ASC");

    QSqlQuery query = QSqlQuery(*db);

    if(queryExec(queryStr, &query))
    {
        while (query.next())
        {//Обучаемый
            Trainee trainee;

            trainee.setID(query.value(0).toInt());
            trainee.setName(query.value(1).toString());
            trainee.setLogin(query.value(2).toString());
            trainee.setPassword(query.value(3).toString());
            trainee.setArchived(query.value(4).toBool());
            trainee.setLoggedIn(query.value(5).toBool());

            Group group = Group(query.value(6).toInt(), query.value(7).toString());
            trainee.setGroup(group);

            Classroom classroom = Classroom(query.value(11).toInt(), query.value(12).toString());
            Computer computer = Computer(query.value(8).toInt(), query.value(9).toString(), query.value(10).toString(), classroom);
            trainee.setComputer(computer);

            trainee.setTasks(selectTasksOfTrainee(trainee.getID()));

            listTrainees.append(trainee);
        }
    }

    return listTrainees;
}

QList<Group> DataBaseLMS::selectAllGroups()
{
    QList<Group> listGroups;

    QString queryStr = QString("SELECT group_id, name "
                                                 "FROM public.groups "
                                                 "ORDER BY group_id ASC");

    QSqlQuery query = QSqlQuery(*db);

    if(queryExec(queryStr, &query))
    {
        while (query.next())
        {//Группа
            Group group;
            group.setID(query.value(0).toInt());
            group.setName(query.value(1).toString());

            listGroups.append(group);
        }
    }

    return listGroups;
}

Instructor DataBaseLMS::selectInstructor(int id_instructor)
{
    Instructor instructor;

    QString queryStr = QString("SELECT instructor_id, name, login, password, is_admin, archived, logged_in "
                                                 "FROM public.instructors "
                                                 "WHERE instructors.instructor_id = %1 ").arg(
                                                        id_instructor);

    QSqlQuery query = QSqlQuery(*db);

    if(queryExec(queryStr, &query))
    {
        if (query.first())
        {//Инструктор
            instructor.setID(query.value(0).toInt());
            instructor.setName(query.value(1).toString());
            instructor.setLogin(query.value(2).toString());
            instructor.setPassword(query.value(3).toString());
            instructor.setIsAdmin(query.value(4).toBool());
            instructor.setArchived(query.value(5).toBool());
            instructor.setLoggedIn(query.value(6).toBool());
        }
    }

    return instructor;
}

int DataBaseLMS::selectInstructorID(QString login, QString password)
{
    QString queryStr;

    if(password != QStringLiteral(""))
    {
        queryStr = QString("SELECT instructors.instructor_id "
                                        "FROM public.instructors "
                                        "WHERE login = '%1' AND password = '%2' ").arg(
                                               login,
                                               password );
    }
    else
    {
        queryStr = QString("SELECT instructors.instructor_id "
                                        "FROM public.instructors "
                                        "WHERE login = '%1' ").arg(
                                               login );
    }

    return queryExecInt(queryStr);
}

QString DataBaseLMS::selectInstructorNameByLogin(QString login)
{
    QString queryStr = QString("SELECT instructors.name "
                                                  "FROM public.instructors "
                                                  "WHERE instructors.login = '%1' ").arg(
                                                         login );

    return queryExecString(queryStr);
}

bool DataBaseLMS::selectInstructorIsAdmin(int id_instructor)
{
    QString queryStr = QString("SELECT instructors.is_admin "
                                                  "FROM public.instructors "
                                                  "WHERE instructor_id = %1 ").arg(
                                                         id_instructor );

    return queryExecBool(queryStr);
}

bool DataBaseLMS::selectInstructorLoggedIn(int id_instructor)
{
    QString queryStr = QString("SELECT instructors.logged_in "
                                                  "FROM public.instructors "
                                                  "WHERE instructor_id = %1 ").arg(
                                                         id_instructor );

    return queryExecBool(queryStr);
}

bool DataBaseLMS::selectInstructorArchived(int id_instructor)
{
    QString queryStr = QString("SELECT instructors.archived "
                                                  "FROM public.instructors "
                                                  "WHERE instructor_id = %1 ").arg(
                                                         id_instructor );

    return queryExecBool(queryStr);
}

int DataBaseLMS::updateInstructorLoggedIn(int id_instructor, bool loggedIn)
{
    QString queryStr = QString("UPDATE public.instructors "
                                                 "SET logged_in = %1 "
                                                 "WHERE instructor_id = %2 "
                                                 "RETURNING instructors.instructor_id").arg(
                                                        loggedIn ? "true" : "false",
                                                        QString::number(id_instructor) );

    return queryExecInt(queryStr);
}

bool DataBaseLMS::updateAllInstructorsLoggedIn(bool loggedIn)
{
    QString queryStr = QString("UPDATE public.instructors "
                                                 "SET logged_in = %1 ").arg(
                                                        loggedIn ? "true" : "false");

    QSqlQuery query = QSqlQuery(*db);

    return queryExec(queryStr, &query);
}

int DataBaseLMS::updateInstructorArchived(int id_instructor, bool archived)
{
    QString queryStr = QString("UPDATE public.instructors "
                                                 "SET archived = %1 "
                                                 "WHERE instructor_id = %2 "
                                                 "RETURNING instructors.instructor_id").arg(
                                                        archived ? "true" : "false",
                                                        QString::number(id_instructor) );

    return queryExecInt(queryStr);
}

int DataBaseLMS::insertInstructor()
{
    QString queryStr = QString("INSERT INTO public.instructors "
                                                 "DEFAULT VALUES "
                                                 "RETURNING instructor_id");

    return queryExecInt(queryStr);
}

int DataBaseLMS::insertInstructor(Instructor instructor)
{
    QString queryStr = QString("INSERT INTO public.instructors (name, login, password, is_admin, archived, logged_in) "
                                                 "VALUES ('%1', '%2', '%3', %4, %5, %6) "
                                                 "RETURNING instructor_id").arg(
                                                        instructor.getName(),
                                                        instructor.getLogin(),
                                                        instructor.getPassword(),
                                                        instructor.getIsAdmin() ? "true" : "false",
                                                        instructor.getArchived() ? "true" : "false",
                                                        instructor.getLoggedIn() ? "true" : "false");

    return queryExecInt(queryStr);
}

int DataBaseLMS::deleteInstructor(int id_instructor)
{
    QString queryStr = QString("DELETE FROM public.instructors "
                                                 "WHERE instructor_id = %1 "
                                                 "RETURNING instructors.instructor_id").arg(
                                                        QString::number(id_instructor));

    return queryExecInt(queryStr);
}

int DataBaseLMS::updateInstructor(Instructor instructor)
{
    QString queryStr = QString("UPDATE public.instructors "
                                                 "SET name = '%1', login = '%2', password = '%3', is_admin = %4, archived = %5, logged_in = %6 "
                                                 "WHERE instructor_id = %7 "
                                                 "RETURNING instructors.instructor_id").arg(
                                                        instructor.getName(),
                                                        instructor.getLogin(),
                                                        instructor.getPassword(),
                                                        instructor.getIsAdmin() ? "true" : "false",
                                                        instructor.getArchived() ? "true" : "false",
                                                        instructor.getLoggedIn() ? "true" : "false",
                                                        QString::number(instructor.getID()) );

    return queryExecInt(queryStr);
}

Group DataBaseLMS::selectGroup(int id_group)
{
    Group group;

    QString queryStr = QString("SELECT group_id, name "
                                                 "FROM public.groups "
                                                 "WHERE groups.group_id = %1 ").arg(
                                                        id_group);

    QSqlQuery query = QSqlQuery(*db);

    if(queryExec(queryStr, &query))
    {
        if (query.first())
        {//Инструктор
            group.setID(query.value(0).toInt());
            group.setName(query.value(1).toString());
        }
    }

    return group;
}

int DataBaseLMS::insertGroup()
{
    QString queryStr = QString("INSERT INTO public.groups "
                                                 "DEFAULT VALUES "
                                                 "RETURNING group_id");

    return queryExecInt(queryStr);
}

int DataBaseLMS::insertGroup(Group group)
{
    QString queryStr = QString("INSERT INTO public.groups (name) "
                                                 "VALUES ('%1') "
                                                 "RETURNING groups.group_id").arg(
                                                        group.getName());

    return queryExecInt(queryStr);
}

int DataBaseLMS::deleteGroup(int group_id)
{
    QString queryStr = QString("DELETE FROM public.groups "
                                                 "WHERE group_id = %1 "
                                                 "RETURNING groups.group_id").arg(
                                                        QString::number(group_id));

    return queryExecInt(queryStr);
}

int DataBaseLMS::updateGroup(Group group)
{
    QString queryStr = QString("UPDATE public.groups SET name = '%1' "
                                                 "WHERE group_id = %2 "
                                                 "RETURNING groups.group_id").arg(
                                                        group.getName(),
                                                        QString::number(group.getID()) );

    return queryExecInt(queryStr);
}

Trainee DataBaseLMS::selectTrainee(int id_trainee)
{
    Trainee trainee;

    QString queryStr = QString("SELECT trainees.trainee_id, trainees.name, trainees.login, trainees.password, trainees.archived, trainees.logged_in, "
                                                 "groups.group_id, groups.name, "
                                                 "computers.computer_id, computers.name, computers.ip_address, "
                                                 "classrooms.classroom_id, classrooms.name "
                                                 "FROM public.trainees JOIN public.groups ON groups.group_id = trainees.group_trainee "
                                                 "LEFT OUTER JOIN public.computers ON computers.computer_id = trainees.computer_trainee "
                                                 "LEFT OUTER JOIN public.classrooms ON classrooms.classroom_id = computers.classroom_computer "
                                                 "WHERE trainees.trainee_id = %1 "
                                                 "ORDER BY groups.name, trainees.name ASC").arg(
                                                        id_trainee);

    QSqlQuery query = QSqlQuery(*db);

    if(queryExec(queryStr, &query))
    {
        if (query.first())
        {//Инструктор
            trainee.setID(query.value(0).toInt());
            trainee.setName(query.value(1).toString());
            trainee.setLogin(query.value(2).toString());
            trainee.setPassword(query.value(3).toString());
            trainee.setArchived(query.value(4).toBool());
            trainee.setLoggedIn(query.value(5).toBool());

            Group group = Group(query.value(6).toInt(), query.value(7).toString());
            trainee.setGroup(group);

            Classroom classroom = Classroom(query.value(11).toInt(), query.value(12).toString());
            Computer computer = Computer(query.value(8).toInt(), query.value(9).toString(), query.value(10).toString(), classroom);
            trainee.setComputer(computer);

            trainee.setTasks(selectTasksOfTrainee(trainee.getID()));
        }
    }

    return trainee;
}

QList<Trainee> DataBaseLMS::selectAllTraineesInGroup(int id_group)
{
    QList<Trainee> listTrainees;

    QString queryStr = QString("SELECT trainees.trainee_id, trainees.name, trainees.login, trainees.password, trainees.archived, trainees.logged_in, "
                                                 "groups.group_id, groups.name, "
                                                 "computers.computer_id, computers.name, computers.ip_address, "
                                                 "classrooms.classroom_id, classrooms.name "
                                                 "FROM public.trainees JOIN public.groups ON groups.group_id = trainees.group_trainee "
                                                 "LEFT OUTER JOIN public.computers ON computers.computer_id = trainees.computer_trainee "
                                                 "LEFT OUTER JOIN public.classrooms ON classrooms.classroom_id = computers.classroom_computer "
                                                 "WHERE trainees.group_trainee = %1 "
                                                 "ORDER BY groups.name, trainees.name ASC").arg(
                                                        id_group);

    QSqlQuery query = QSqlQuery(*db);

    if(queryExec(queryStr, &query))
    {
        while (query.next())
        {//Обучаемый
            Trainee trainee;

            trainee.setID(query.value(0).toInt());
            trainee.setName(query.value(1).toString());
            trainee.setLogin(query.value(2).toString());
            trainee.setPassword(query.value(3).toString());
            trainee.setArchived(query.value(4).toBool());
            trainee.setLoggedIn(query.value(5).toBool());

            Group group = Group(query.value(6).toInt(), query.value(7).toString());
            trainee.setGroup(group);

            Classroom classroom = Classroom(query.value(11).toInt(), query.value(12).toString());
            Computer computer = Computer(query.value(8).toInt(), query.value(9).toString(), query.value(10).toString(), classroom);
            trainee.setComputer(computer);

            trainee.setTasks(selectTasksOfTrainee(trainee.getID()));

            listTrainees.append(trainee);
        }
    }

    return listTrainees;
}

int DataBaseLMS::selectTraineeID(QString login, QString password)
{
    QString queryStr;

    if(password != QStringLiteral(""))
    {
        queryStr = QString("SELECT trainees.trainee_id "
                                        "FROM public.trainees "
                                        "WHERE login = '%1' AND password = '%2' ").arg(
                                               login,
                                               password );
    }
    else
    {
        queryStr = QString("SELECT trainees.trainee_id "
                                        "FROM public.trainees "
                                        "WHERE login = '%1' ").arg(
                                               login );
    }

    return queryExecInt(queryStr);
}

QString DataBaseLMS::selectTraineeNameByLogin(QString login)
{
    QString queryStr = QString("SELECT trainees.name "
                                                  "FROM public.trainees "
                                                  "WHERE trainees.login = '%1' ").arg(
                                                         login );

    return queryExecString(queryStr);
}

QString DataBaseLMS::selectTraineeNameOnComputer(QString computer_name)
{
    QString queryStr = QString("SELECT trainees.name "
                                                 "FROM public.trainees JOIN public.computers ON computers.computer_id = trainees.computer_trainee "
                                                 "WHERE computers.name = '%1' ").arg(
                                                        computer_name);

    return queryExecString(queryStr);
}

Trainee DataBaseLMS::selectTraineeOnComputer(QString computer_name)
{
    Trainee trainee;

    QString queryStr = QString("SELECT trainees.trainee_id, trainees.name, trainees.login, trainees.password, trainees.archived, trainees.logged_in, "
                                                 "groups.group_id, groups.name, "
                                                 "computers.computer_id, computers.name, computers.ip_address, "
                                                 "classrooms.classroom_id, classrooms.name "
                                                 "FROM public.trainees JOIN public.groups ON groups.group_id = trainees.group_trainee "
                                                 "LEFT OUTER JOIN public.computers ON computers.computer_id = trainees.computer_trainee "
                                                 "LEFT OUTER JOIN public.classrooms ON classrooms.classroom_id = computers.classroom_computer "
                                                 "WHERE computers.name = '%1' ").arg(
                                                        computer_name);

    QSqlQuery query = QSqlQuery(*db);

    if(queryExec(queryStr, &query))
    {
        if (query.first())
        {//Инструктор
            trainee.setID(query.value(0).toInt());
            trainee.setName(query.value(1).toString());
            trainee.setLogin(query.value(2).toString());
            trainee.setPassword(query.value(3).toString());
            trainee.setArchived(query.value(4).toBool());
            trainee.setLoggedIn(query.value(5).toBool());

            Group group = Group(query.value(6).toInt(), query.value(7).toString());
            trainee.setGroup(group);

            Classroom classroom = Classroom(query.value(11).toInt(), query.value(12).toString());
            Computer computer = Computer(query.value(8).toInt(), query.value(9).toString(), query.value(10).toString(), classroom);
            trainee.setComputer(computer);

            trainee.setTasks(selectTasksOfTrainee(trainee.getID()));
        }
    }

    return trainee;
}

bool DataBaseLMS::selectTraineeArchived(int id_trainee)
{
    QString queryStr = QString("SELECT trainees.archived "
                                                  "FROM public.trainees "
                                                  "WHERE trainee_id = %1 ").arg(
                                                         id_trainee );

    return queryExecBool(queryStr);
}

bool DataBaseLMS::selectTraineeLoggedIn(int id_trainee)
{
    QString queryStr = QString("SELECT trainees.logged_in "
                                                  "FROM public.trainees "
                                                  "WHERE trainee_id = %1 ").arg(
                                                         id_trainee );

    return queryExecBool(queryStr);
}

int DataBaseLMS::updateTraineeLoggedIn(int id_trainee, bool loggedIn)
{
    QString queryStr = QString("UPDATE public.trainees "
                                                 "SET logged_in = %1 "
                                                 "WHERE trainee_id = %2 "
                                                 "RETURNING trainees.trainee_id").arg(
                                                        loggedIn ? "true" : "false",
                                                        QString::number(id_trainee) );

    return queryExecInt(queryStr);
}

bool DataBaseLMS::updateAllTraineesLoggedIn(bool loggedIn)
{
    QString queryStr = QString("UPDATE public.trainees "
                                                 "SET logged_in = %1 ").arg(
                                                        loggedIn ? "true" : "false");

    QSqlQuery query = QSqlQuery(*db);

    return queryExec(queryStr, &query);
}

int DataBaseLMS::updateTraineeArchived(int id_trainee, bool archived)
{
    QString queryStr = QString("UPDATE public.trainees "
                                                 "SET archived = %1 "
                                                 "WHERE trainee_id = %2 "
                                                 "RETURNING trainees.trainee_id").arg(
                                                        archived ? "true" : "false",
                                                        QString::number(id_trainee) );

    return queryExecInt(queryStr);
}

int DataBaseLMS::insertTrainee(int id_group)
{
    QString queryStr = QString("INSERT INTO public.trainees (name, login, password, archived, logged_in, group_trainee) "
                                                 "VALUES (DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, %1) "
                                                 "RETURNING trainees.trainee_id").arg(
                                                        QString::number(id_group));

    return queryExecInt(queryStr);
}

int DataBaseLMS::insertTrainee(Trainee trainee)
{
    QString queryStr = QString("INSERT INTO public.trainees (name, login, password, archived, logged_in, group_trainee) "
                                                 "VALUES ('%1', '%2', '%3', %4, %5, %6) "
                                                 "RETURNING trainees.trainee_id").arg(
                                                        trainee.getName(),
                                                        trainee.getLogin(),
                                                        trainee.getPassword(),
                                                        trainee.getArchived() ? "true" : "false",
                                                        trainee.getLoggedIn() ? "true" : "false",
                                                        QString::number(trainee.getGroup().getID()));

    return queryExecInt(queryStr);
}

int DataBaseLMS::deleteTrainee(int trainee_id)
{
    QString queryStr = QString("DELETE FROM public.trainees "
                                                 "WHERE trainee_id = %1 "
                                                 "RETURNING trainees.trainee_id").arg(
                                                        QString::number(trainee_id));

    return queryExecInt(queryStr);
}

int DataBaseLMS::updateTrainee(Trainee trainee)
{
    QString computer_id = (trainee.getComputer().getID() == 0 ? QStringLiteral("null") : QString::number(trainee.getComputer().getID()));

    QString queryStr = QString("UPDATE public.trainees "
                                                 "SET name = '%1', login = '%2', password = '%3', archived = %4, logged_in = %5, "
                                                 "group_trainee = %6, "
                                                 "computer_trainee = %7 "
                                                 "WHERE trainee_id = %8 "
                                                 "RETURNING trainees.trainee_id").arg(
                                                        trainee.getName(),
                                                        trainee.getLogin(),
                                                        trainee.getPassword(),
                                                        trainee.getArchived() ? "true" : "false",
                                                        trainee.getLoggedIn() ? "true" : "false",
                                                        QString::number(trainee.getGroup().getID()),
                                                        computer_id,
                                                        QString::number(trainee.getID()) );

    return queryExecInt(queryStr);
}

QList<Task> DataBaseLMS::selectTasksOfTrainee(int trainee_id)
{
    QList<Task> tasks;

    QString queryStr = QString("SELECT tasks.task_id, tasks.name "
                                                 "FROM public.trainees "
                                                 "JOIN public.trainees_tasks ON trainees_tasks.trainee_id = trainees.trainee_id "
                                                 "JOIN public.tasks ON tasks.task_id = trainees_tasks.task_id "
                                                 "WHERE trainees.trainee_id = %1 "
                                                 "ORDER BY tasks.name ASC").arg(
                                                        trainee_id);

    QSqlQuery query = QSqlQuery(*db);

    if(queryExec(queryStr, &query))
    {
        while (query.next())
        {//Задача
            Task task;
            task.setID(query.value(0).toInt());
            task.setName(query.value(1).toString());
            tasks.append(task);
        }
    }

    return tasks;
}

int DataBaseLMS::queryExecInt(QString queryStr)
{
    QSqlQuery query = QSqlQuery(*db);

    if(queryExec(queryStr, &query))
    {
        if(query.first())
            return query.value(0).toInt();
    }

    return 0;
}

QString DataBaseLMS::queryExecString(QString queryStr)
{
    QSqlQuery query = QSqlQuery(*db);

    if(queryExec(queryStr, &query))
    {
        if(query.first())
            return query.value(0).toString();
    }

    return QStringLiteral("");
}

bool DataBaseLMS::queryExecBool(QString queryStr)
{
    QSqlQuery query = QSqlQuery(*db);

    if(queryExec(queryStr, &query))
    {
        if(query.first())
            return query.value(0).toBool();
    }

    return false;
}

bool DataBaseLMS::queryExec(QString queryStr, QSqlQuery *query)
{
    bool res  = false;
    mtxAccess.lock();
    if(! (res = query->exec(queryStr)) )
            messageWarningErrorQuery(queryStr, query);
    mtxAccess.unlock();
    return res;
}

void DataBaseLMS::messageWarningErrorQuery(QString queryStr, QSqlQuery* query)
{
    QMessageBox::warning(nullptr, dbName,
                         "Error query:\n" + query->lastError().text() + "\n" +
                         "String of query:\n" + queryStr + "\n" +
                         "Executed query:\n" + query->executedQuery());
}
