244 lines
6.7 KiB
C++
Executable File
244 lines
6.7 KiB
C++
Executable File
/**
|
|
* @RDE_HEADER@
|
|
* DBUpdate - Global updates model
|
|
* $Id$
|
|
* $HeadURL$
|
|
* $Author$
|
|
*
|
|
* Created by Iuliana Ojog, 2021-02-17
|
|
*/
|
|
|
|
#include "global_updates_model.h"
|
|
#include <ngldb/nsqlquery.h>
|
|
#include <ngltools/ngltools_dtg.h>
|
|
#include <QStringList>
|
|
|
|
namespace DBUpdate
|
|
{
|
|
GlobalUpdatesModel::GlobalUpdatesModel(QObject *parent)
|
|
: QAbstractTableModel(parent)
|
|
, m_logger(getLogger("GlobalUpdatesModel"))
|
|
{
|
|
}
|
|
|
|
GlobalUpdatesModel::~GlobalUpdatesModel()
|
|
{
|
|
}
|
|
|
|
bool GlobalUpdatesModel::initModel(const QSqlDatabase &db, const QString &version, QStringList schemas)
|
|
{
|
|
beginResetModel();
|
|
m_vecData.clear();
|
|
endResetModel();
|
|
|
|
// no global updates
|
|
if (schemas.contains(QStringLiteral("NONE"), Qt::CaseInsensitive))
|
|
return true;
|
|
|
|
// all global schemas
|
|
if (schemas.contains(QStringLiteral("ALL"), Qt::CaseInsensitive))
|
|
schemas = global_schemas;
|
|
|
|
NSqlQuery q(db);
|
|
q.setForwardOnly(false);
|
|
|
|
int upperVersion = version.split(QLatin1Char('.')).at(0).toInt();
|
|
int versionInt = version.section(QLatin1Char('.'), 1).toInt();
|
|
DEBUG_LOG(m_logger) << "versionInt: " << versionInt;
|
|
DEBUG_LOG(m_logger) << "upperVersion: " << upperVersion;
|
|
|
|
for (const QString &schema: qAsConst(schemas))
|
|
{
|
|
GlobalUpdate gu;
|
|
gu.schema = schema;
|
|
gu.isChecked = false;
|
|
QString stmt = QStringLiteral("SELECT id, version, description, updated, success FROM ") + schema
|
|
+ QStringLiteral(".db_version WHERE success = true ORDER BY cast(regexp_replace(split_part(version,'.',1),'[^0-9]', '', 'g') as integer ) DESC"
|
|
", cast(regexp_replace(split_part(version,'.',2),'[^0-9]', '', 'g') as integer ) DESC, updated DESC LIMIT 1;");
|
|
if (q.execAndFetch(stmt))
|
|
{
|
|
gu.version = q.value(1).toString();
|
|
gu.description = q.value(2).toString();
|
|
gu.updated = q.value(3).toLongLong();
|
|
}
|
|
int upperGuVersion = gu.version.split(QLatin1Char('.')).at(0).toInt();
|
|
int guVersion = gu.version.section(QLatin1Char('.'), 1).toInt();
|
|
DEBUG_LOG(m_logger) << "guVersion: " << guVersion;
|
|
DEBUG_LOG(m_logger) << "upperGuVersion: " << upperGuVersion;
|
|
|
|
if (upperGuVersion <= upperVersion && guVersion < versionInt)
|
|
gu.isChecked = true;
|
|
|
|
int lastUpperVersion = m_lastUpdate.split(QLatin1Char('.')).at(0).toInt();
|
|
int lastUpdateInt = m_lastUpdate.section(QLatin1Char('.'), 1).toInt();
|
|
DEBUG_LOG(m_logger) << "lastUpdateInt: " << lastUpdateInt;
|
|
DEBUG_LOG(m_logger) << "lastUpperVersion: " << lastUpperVersion;
|
|
DEBUG_LOG(m_logger) << "gu.version: " << gu.version << ", lastUpdate: " << m_lastUpdate;
|
|
|
|
if ( upperGuVersion > lastUpperVersion )
|
|
m_lastUpdate = gu.version;
|
|
else if ( upperGuVersion == lastUpperVersion && guVersion >= lastUpdateInt)
|
|
m_lastUpdate = gu.version;
|
|
insertRow(gu);
|
|
}
|
|
|
|
INFO_LOG(m_logger) << "initModel - #global updates: " << m_vecData.size();
|
|
|
|
return true;
|
|
}
|
|
|
|
|
|
QVariant GlobalUpdatesModel::data(const QModelIndex &index, int role) const
|
|
{
|
|
if (!index.isValid() || index.row() >= m_vecData.size())
|
|
return QVariant();
|
|
|
|
const GlobalUpdate &gu = m_vecData.at(index.row());
|
|
|
|
if (role == Qt::DisplayRole)
|
|
{
|
|
switch (index.column())
|
|
{
|
|
case Schema:
|
|
return QString(gu.schema);
|
|
case Version:
|
|
return QString(gu.version);
|
|
case Description:
|
|
return QString(gu.description);
|
|
case Updated:
|
|
return DTG::toStringMSec(gu.updated);
|
|
}
|
|
}
|
|
else if (role == Qt::CheckStateRole)
|
|
{
|
|
// CheckBoxes only for columns that are actually checkable
|
|
if (!flags(index).testFlag(Qt::ItemIsUserCheckable))
|
|
return QVariant();
|
|
|
|
if (index.column() == Columns::Schema)
|
|
return gu.isChecked ? Qt::Checked : Qt::Unchecked;
|
|
}
|
|
return QVariant();
|
|
}
|
|
|
|
int GlobalUpdatesModel::rowCount(const QModelIndex &parent) const
|
|
{
|
|
return parent.isValid() ? 0 : m_vecData.size();
|
|
}
|
|
|
|
int GlobalUpdatesModel::columnCount(const QModelIndex &parent) const
|
|
{
|
|
return parent.isValid() ? 0 : Columns::Updated + 1;
|
|
}
|
|
|
|
QVariant GlobalUpdatesModel::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 Schema:
|
|
return tr("Schema");
|
|
case Version:
|
|
return tr("Version");
|
|
case Description:
|
|
return tr("Description");
|
|
case Updated:
|
|
return tr("Updated");
|
|
}
|
|
}
|
|
return QAbstractTableModel::headerData(section, orientation, role);
|
|
}
|
|
|
|
GlobalUpdate GlobalUpdatesModel::getRowData(int row) const
|
|
{
|
|
if (row < 0 || row >= m_vecData.size())
|
|
return GlobalUpdate();
|
|
return m_vecData.at(row);
|
|
}
|
|
|
|
bool GlobalUpdatesModel::insertRow(const GlobalUpdate &item)
|
|
{
|
|
beginInsertRows(QModelIndex(), m_vecData.size(), m_vecData.size());
|
|
m_vecData.append(item);
|
|
endInsertRows();
|
|
return true;
|
|
}
|
|
|
|
Qt::ItemFlags GlobalUpdatesModel::flags(const QModelIndex &index) const
|
|
{
|
|
auto f = QAbstractTableModel::flags(index);
|
|
if (!index.isValid() || index.row() >= m_vecData.size())
|
|
return f;
|
|
if (index.column() == Columns::Schema)
|
|
f.setFlag(Qt::ItemIsUserCheckable);
|
|
//if (!m_vecData[index.row()].isChecked)
|
|
// f.setFlag(Qt::ItemIsEnabled, false);
|
|
f.setFlag(Qt::ItemIsEditable, false);
|
|
f.setFlag(Qt::ItemIsSelectable, true);
|
|
return f;
|
|
}
|
|
|
|
bool GlobalUpdatesModel::setData(const QModelIndex &index, const QVariant &value, int role)
|
|
{
|
|
if (!index.isValid())
|
|
return false;
|
|
|
|
if (role != Qt::CheckStateRole)
|
|
return false;
|
|
|
|
if (!value.canConvert<bool>())
|
|
return false;
|
|
|
|
bool bVal = value.toBool();
|
|
|
|
GlobalUpdate &data = m_vecData[index.row()];
|
|
if (data.isChecked != bVal)
|
|
{
|
|
data.isChecked = bVal;
|
|
emit dataChanged(index, index);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
QStringList GlobalUpdatesModel::getSchemasToUpdate()
|
|
{
|
|
QStringList schemas;
|
|
|
|
for(const GlobalUpdate &gu : qAsConst(m_vecData))
|
|
{
|
|
if (gu.isChecked)
|
|
schemas << gu.schema;
|
|
}
|
|
|
|
return schemas;
|
|
}
|
|
|
|
void GlobalUpdatesProxyModel::setSourceModel(QAbstractItemModel *sourceModel)
|
|
{
|
|
m_sourceModel = qobject_cast<GlobalUpdatesModel*>(sourceModel);
|
|
QSortFilterProxyModel::setSourceModel(sourceModel);
|
|
}
|
|
|
|
bool GlobalUpdatesProxyModel::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 GlobalUpdatesModel::Updated:
|
|
return dataLeft.updated < dataRight.updated;
|
|
}
|
|
return QSortFilterProxyModel::lessThan(source_left, source_right);
|
|
}
|
|
|
|
|
|
} // namespace
|