Nut/src/nut/core/abstracttableset.cpp

124 lines
3.0 KiB
C++
Raw Normal View History

2016-05-12 14:08:58 +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 "table.h"
#include "database.h"
#include "abstracttableset.h"
2016-05-12 14:08:58 +08:00
#include "databasemodel.h"
#include "abstracttablesetdata.h"
2016-05-12 14:08:58 +08:00
2017-02-01 18:01:21 +08:00
NUT_BEGIN_NAMESPACE
AbstractTableSet::AbstractTableSet(Database *parent) : QObject(parent),
data(new AbstractTableSetData(parent))
2016-05-12 14:08:58 +08:00
{
parent->add(this);
}
AbstractTableSet::AbstractTableSet(Table *parent) : QObject(parent),
data(new AbstractTableSetData(parent))
2016-05-12 14:08:58 +08:00
{
parent->add(this);
}
AbstractTableSet::~AbstractTableSet()
{
2021-03-14 16:42:04 +08:00
Q_FOREACH (Row<Table> t, data->childs)
2019-07-17 21:43:30 +08:00
if (t)
t->setParentTableSet(nullptr);
}
int AbstractTableSet::save(Database *db, bool cleanUp)
2016-05-12 14:08:58 +08:00
{
2017-05-28 23:08:59 +08:00
int rowsAffected = 0;
2019-06-07 16:19:20 +08:00
TableModel *masterModel = nullptr;
2019-06-18 23:37:03 +08:00
if (data->table)
2020-07-30 23:05:11 +08:00
masterModel = db->model().tableByClassName(QString::fromUtf8(data->table->metaObject()->className()));
2019-06-07 16:19:20 +08:00
2021-03-14 16:42:04 +08:00
Q_FOREACH (Row<Table> t, data->childs) {
if (data->table)
2019-06-18 23:37:03 +08:00
t->setParentTable(data->table,
masterModel,
2020-07-30 23:05:11 +08:00
db->model().tableByClassName(QString::fromUtf8(t->metaObject()->className())));
2016-05-12 14:08:58 +08:00
if (t->status() == Table::Added
|| t->status() == Table::Modified
|| t->status() == Table::Deleted) {
2017-05-28 23:08:59 +08:00
rowsAffected += t->save(db);
if (cleanUp)
#ifdef NUT_RAW_POINTER
2019-07-21 15:11:34 +08:00
t->deleteLater();
#else
remove(t);
2019-07-03 01:30:29 +08:00
#endif
2016-05-12 14:08:58 +08:00
}
}
2017-05-28 23:08:59 +08:00
2017-09-10 22:18:20 +08:00
if (cleanUp)
data->childs.clear();
2017-09-10 22:18:20 +08:00
2017-05-28 23:08:59 +08:00
return rowsAffected;
2016-05-12 14:08:58 +08:00
}
void AbstractTableSet::clearChilds()
2016-05-21 16:09:03 +08:00
{
#ifdef NUT_RAW_POINTER
2021-03-14 16:42:04 +08:00
Q_FOREACH (Table *t, data->childs)
2017-09-10 22:18:20 +08:00
t->deleteLater();
2019-06-18 23:37:03 +08:00
#endif
data->childs.clear();
2016-05-21 16:09:03 +08:00
}
void AbstractTableSet::add(Row<Table> t)
{
data.detach();
data->childs.append(t);
2019-07-17 21:43:30 +08:00
t->setParentTableSet(this);
}
void AbstractTableSet::remove(Row<Table> t)
{
data.detach();
2019-07-21 15:11:34 +08:00
data->childs.removeAll(t);
}
QString AbstractTableSet::childClassName() const
2016-05-12 14:08:58 +08:00
{
2019-06-18 23:37:03 +08:00
return data->childClassName;
2016-05-12 14:08:58 +08:00
}
2016-05-21 16:09:03 +08:00
Database *AbstractTableSet::database() const
2016-05-21 16:09:03 +08:00
{
2019-06-18 23:37:03 +08:00
return data->database;
2016-05-21 16:09:03 +08:00
}
void AbstractTableSet::setDatabase(Database *database)
2016-05-21 16:09:03 +08:00
{
2019-06-19 04:32:27 +08:00
data.detach();
2019-06-18 23:37:03 +08:00
data->database = database;
2016-05-21 16:09:03 +08:00
}
2017-02-01 18:01:21 +08:00
2021-05-31 14:29:23 +08:00
int AbstractTableSet::size() const
{
return data->childs.size();
}
2017-02-01 18:01:21 +08:00
NUT_END_NAMESPACE