162 lines
3.9 KiB
C++
Executable File
162 lines
3.9 KiB
C++
Executable File
/**
|
|
* @RDE_HEADER@
|
|
* DBUpdate
|
|
* $Id$
|
|
* $HeadURL$
|
|
* $Author$
|
|
* Created by Iuliana Ojog, 2021-02-17
|
|
*/
|
|
|
|
#include "history_model.h"
|
|
#include <ngldb/nsqlquery.h>
|
|
#include <ngltools/ngltools_dtg.h>
|
|
|
|
namespace DBUpdate
|
|
{
|
|
HistoryModel::HistoryModel(QObject *parent)
|
|
: QAbstractTableModel(parent)
|
|
, m_logger(getLogger("HistoryModel"))
|
|
{
|
|
}
|
|
|
|
HistoryModel::~HistoryModel()
|
|
{
|
|
}
|
|
|
|
bool HistoryModel::initModel(const QSqlDatabase &db, const QString &schema)
|
|
{
|
|
beginResetModel();
|
|
m_vecData.clear();
|
|
endResetModel();
|
|
|
|
NSqlQuery q(db);
|
|
|
|
QString stmt = QStringLiteral("SELECT id, version, description, updated, success, message FROM ") + schema + QStringLiteral(".db_version ORDER BY version DESC, updated DESC;");
|
|
|
|
if (!q.exec(stmt))
|
|
return false;
|
|
|
|
while (q.next())
|
|
{
|
|
HistoryUpdate hu;
|
|
hu.schema = schema;
|
|
hu.id = q.value(0).toInt();
|
|
hu.version = q.value(1).toString();
|
|
hu.description = q.value(2).toString();
|
|
hu.updated = q.value(3).toLongLong();
|
|
hu.success = q.value(4).toBool();
|
|
hu.message = q.value(5).toString();
|
|
|
|
insertRow(hu);
|
|
}
|
|
|
|
INFO_LOG(m_logger) << "initModel - #history updates: " << m_vecData.size();
|
|
return true;
|
|
}
|
|
|
|
QVariant HistoryModel::data(const QModelIndex &index, int role) const
|
|
{
|
|
if (!index.isValid() || index.row() >= m_vecData.size())
|
|
return QVariant();
|
|
|
|
const HistoryUpdate &hu = m_vecData.at(index.row());
|
|
|
|
if (role == Qt::DisplayRole)
|
|
{
|
|
switch (index.column())
|
|
{
|
|
case Id:
|
|
return hu.id;
|
|
case Schema:
|
|
return QString(hu.schema);
|
|
case Version:
|
|
return QString(hu.version);
|
|
case Description:
|
|
return QString(hu.description);
|
|
case Updated:
|
|
return DTG::toStringMSec(hu.updated);
|
|
case Success:
|
|
return hu.success;
|
|
case Message:
|
|
return hu.message;
|
|
}
|
|
}
|
|
return QVariant();
|
|
}
|
|
|
|
int HistoryModel::rowCount(const QModelIndex &parent) const
|
|
{
|
|
return parent.isValid() ? 0 : m_vecData.size();
|
|
}
|
|
|
|
int HistoryModel::columnCount(const QModelIndex &parent) const
|
|
{
|
|
return parent.isValid() ? 0 : Columns::Message + 1;
|
|
}
|
|
|
|
QVariant HistoryModel::headerData(int section, Qt::Orientation orientation, int role) const
|
|
{
|
|
if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
|
|
{
|
|
Columns col = static_cast<Columns>(section);
|
|
switch (col)
|
|
{
|
|
case Id:
|
|
return tr("Id");
|
|
case Schema:
|
|
return tr("Schema");
|
|
case Version:
|
|
return tr("Version");
|
|
case Description:
|
|
return tr("Description");
|
|
case Updated:
|
|
return tr("Updated");
|
|
case Message:
|
|
return tr("Message");
|
|
case Success:
|
|
return tr("Success");
|
|
}
|
|
}
|
|
return QAbstractTableModel::headerData(section, orientation, role);
|
|
}
|
|
|
|
HistoryUpdate HistoryModel::getRowData(int row) const
|
|
{
|
|
if (row < 0 || row >= m_vecData.size())
|
|
return HistoryUpdate();
|
|
return m_vecData.at(row);
|
|
}
|
|
|
|
bool HistoryModel::insertRow(const HistoryUpdate &item)
|
|
{
|
|
beginInsertRows(QModelIndex(), m_vecData.size(), m_vecData.size());
|
|
m_vecData.append(item);
|
|
endInsertRows();
|
|
return true;
|
|
}
|
|
|
|
void HistoryProxyModel::setSourceModel(QAbstractItemModel *sourceModel)
|
|
{
|
|
m_sourceModel = qobject_cast<HistoryModel*>(sourceModel);
|
|
QSortFilterProxyModel::setSourceModel(sourceModel);
|
|
}
|
|
|
|
bool HistoryProxyModel::lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const
|
|
{
|
|
if (!m_sourceModel ||
|
|
source_left.column() != source_right.column() ||
|
|
!source_left.isValid() ||
|
|
!source_right.isValid())
|
|
return QSortFilterProxyModel::lessThan(source_left, source_right);
|
|
const auto &dataLeft = m_sourceModel->m_vecData.at(source_left.row());
|
|
const auto dataRight = m_sourceModel->m_vecData.at(source_right.row());
|
|
switch (source_left.column())
|
|
{
|
|
case HistoryModel::Updated:
|
|
return dataLeft.updated < dataRight.updated;
|
|
}
|
|
return QSortFilterProxyModel::lessThan(source_left, source_right);
|
|
}
|
|
|
|
} // namespace
|