1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
|
#include "querymodel.h"
#include "db/db.h"
#include "common/unused.h"
#include "db/sqlquery.h"
QueryModel::QueryModel(Db* db, QObject *parent) :
QAbstractTableModel(parent), db(db)
{
}
void QueryModel::refresh()
{
if (!db || !db->isOpen())
return;
beginResetModel();
loadedRows.clear();
SqlQueryPtr results = db->exec(query);
for (SqlResultsRowPtr& row : results->getAll())
loadedRows += row;
columns = results->columnCount();
endResetModel();
emit refreshed();
}
QVariant QueryModel::data(const QModelIndex& index, int role) const
{
if (!index.isValid())
return QVariant();
if (role != Qt::DisplayRole)
return QVariant();
int rowIdx = index.row();
if (rowIdx < loadedRows.size())
return loadedRows[rowIdx]->value(index.column());
return QVariant();
}
int QueryModel::rowCount(const QModelIndex& parent) const
{
UNUSED(parent);
return loadedRows.size();
}
int QueryModel::columnCount(const QModelIndex& parent) const
{
UNUSED(parent);
return columns;
}
QString QueryModel::getQuery() const
{
return query;
}
void QueryModel::setQuery(const QString& value)
{
query = value;
refresh();
}
|