2018-02-13 23:39:21 +08:00
|
|
|
/**************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** This file is part of Nut project.
|
|
|
|
|
** https://github.com/HamedMasafi/Nut
|
|
|
|
|
**
|
|
|
|
|
** Nut is free software: you can redistribute it and/or modify
|
|
|
|
|
** it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
|
** the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
** (at your option) any later version.
|
|
|
|
|
**
|
|
|
|
|
** Nut is distributed in the hope that it will be useful,
|
|
|
|
|
** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
** GNU Lesser General Public License for more details.
|
|
|
|
|
**
|
|
|
|
|
** You should have received a copy of the GNU Lesser General Public License
|
|
|
|
|
** along with Nut. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
**
|
|
|
|
|
**************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "database.h"
|
2020-08-06 23:41:02 +08:00
|
|
|
#include "abstracttableset.h"
|
2018-02-13 23:39:21 +08:00
|
|
|
#include "databasemodel.h"
|
2019-06-07 16:19:20 +08:00
|
|
|
#include "tablemodel.h"
|
|
|
|
|
#include "table.h"
|
2018-02-13 23:39:21 +08:00
|
|
|
#include "sqlmodel_p.h"
|
|
|
|
|
#include "sqlmodel.h"
|
2019-06-19 00:11:14 +08:00
|
|
|
#include "query.h"
|
2018-02-13 23:39:21 +08:00
|
|
|
|
|
|
|
|
NUT_BEGIN_NAMESPACE
|
|
|
|
|
|
2020-08-08 15:29:16 +08:00
|
|
|
SqlModelPrivate::SqlModelPrivate(SqlModel *parent) : q_ptr(parent)
|
|
|
|
|
, renderer(nullptr)
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(parent)
|
|
|
|
|
}
|
2019-06-19 00:11:14 +08:00
|
|
|
|
|
|
|
|
void SqlModel::setRenderer(const std::function<QVariant (int, QVariant)> &renderer)
|
|
|
|
|
{
|
2020-08-08 15:29:16 +08:00
|
|
|
Q_D(SqlModel);
|
|
|
|
|
d->renderer = renderer;
|
2019-06-19 00:11:14 +08:00
|
|
|
}
|
|
|
|
|
|
2020-08-06 23:41:02 +08:00
|
|
|
SqlModel::SqlModel(Database *database, AbstractTableSet *tableSet, QObject *parent)
|
2020-07-14 21:32:52 +08:00
|
|
|
: QAbstractTableModel(parent)
|
2020-08-08 15:29:16 +08:00
|
|
|
, d_ptr(new SqlModelPrivate(this))
|
2018-02-13 23:39:21 +08:00
|
|
|
{
|
2020-08-08 15:29:16 +08:00
|
|
|
Q_D(SqlModel);
|
2018-02-13 23:39:21 +08:00
|
|
|
d->model = database->model()
|
|
|
|
|
.tableByClassName(tableSet->childClassName());
|
|
|
|
|
d->tableName = d->model->name();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int SqlModel::rowCount(const QModelIndex &parent) const
|
|
|
|
|
{
|
2020-08-08 15:29:16 +08:00
|
|
|
Q_D(const SqlModel);
|
2020-04-04 18:25:35 +08:00
|
|
|
Q_UNUSED(parent)
|
2018-02-13 23:39:21 +08:00
|
|
|
return d->rows.count();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int SqlModel::columnCount(const QModelIndex &parent) const
|
|
|
|
|
{
|
2020-08-08 15:29:16 +08:00
|
|
|
Q_D(const SqlModel);
|
2020-04-04 18:25:35 +08:00
|
|
|
Q_UNUSED(parent)
|
2018-02-13 23:39:21 +08:00
|
|
|
return d->model->fields().count();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QVariant SqlModel::data(const QModelIndex &index, int role) const
|
|
|
|
|
{
|
2020-08-08 15:29:16 +08:00
|
|
|
Q_D(const SqlModel);
|
|
|
|
|
|
2018-02-13 23:39:21 +08:00
|
|
|
if (!index.isValid())
|
|
|
|
|
return QVariant();
|
|
|
|
|
|
|
|
|
|
if (index.row() >= d->rows.count() || index.row() < 0)
|
2020-07-30 23:05:11 +08:00
|
|
|
return QVariant::fromValue(QStringLiteral("-"));
|
2018-02-13 23:39:21 +08:00
|
|
|
|
|
|
|
|
if (role == Qt::DisplayRole) {
|
2019-06-19 00:11:14 +08:00
|
|
|
Row<Table> t = d->rows.at(index.row());
|
|
|
|
|
QVariant v = t->property(d->model->field(index.column())->name.toLocal8Bit().data());
|
2019-07-03 21:57:00 +08:00
|
|
|
|
2020-08-08 15:29:16 +08:00
|
|
|
if (d->renderer != nullptr)
|
|
|
|
|
v = d->renderer(index.column(), v);
|
2019-06-19 00:11:14 +08:00
|
|
|
return v;
|
2018-02-13 23:39:21 +08:00
|
|
|
}
|
|
|
|
|
return QVariant();
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-19 00:11:14 +08:00
|
|
|
void SqlModel::setRows(RowList<Table> rows)
|
|
|
|
|
{
|
2020-08-08 15:29:16 +08:00
|
|
|
Q_D(SqlModel);
|
|
|
|
|
|
2019-06-23 22:07:06 +08:00
|
|
|
if (d->rows.count()) {
|
|
|
|
|
beginRemoveRows(QModelIndex(), 0, d->rows.count());
|
|
|
|
|
d->rows.clear();
|
|
|
|
|
endRemoveRows();
|
|
|
|
|
}
|
2019-06-19 00:11:14 +08:00
|
|
|
beginInsertRows(QModelIndex(), 0, rows.count());
|
|
|
|
|
d->rows = rows;
|
|
|
|
|
endInsertRows();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SqlModel::append(Row<Table> table)
|
|
|
|
|
{
|
2020-08-08 15:29:16 +08:00
|
|
|
Q_D(SqlModel);
|
2019-06-19 00:11:14 +08:00
|
|
|
beginInsertRows(QModelIndex(), d->rows.count(), d->rows.count());
|
|
|
|
|
d->rows.append(table);
|
|
|
|
|
endInsertRows();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QVariant SqlModel::headerData(int section, Qt::Orientation orientation, int role) const
|
|
|
|
|
{
|
2020-08-08 15:29:16 +08:00
|
|
|
Q_D(const SqlModel);
|
2019-06-19 00:11:14 +08:00
|
|
|
if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
|
|
|
|
|
return d->model->field(section)->displayName;
|
|
|
|
|
}
|
|
|
|
|
return QAbstractItemModel::headerData(section, orientation, role);
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-19 13:17:10 +08:00
|
|
|
Row<Table> SqlModel::at(const int &i) const
|
2019-06-19 00:11:14 +08:00
|
|
|
{
|
2020-08-08 15:29:16 +08:00
|
|
|
Q_D(const SqlModel);
|
2019-06-19 13:17:10 +08:00
|
|
|
return d->rows.at(i);
|
2019-06-19 00:11:14 +08:00
|
|
|
}
|
|
|
|
|
|
2018-02-13 23:39:21 +08:00
|
|
|
NUT_END_NAMESPACE
|