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"
|
2020-08-06 23:41:02 +08:00
|
|
|
#include "abstracttableset.h"
|
2016-05-12 14:08:58 +08:00
|
|
|
#include "databasemodel.h"
|
2020-08-06 23:41:02 +08:00
|
|
|
#include "abstracttablesetdata.h"
|
2016-05-12 14:08:58 +08:00
|
|
|
|
2017-02-01 18:01:21 +08:00
|
|
|
NUT_BEGIN_NAMESPACE
|
|
|
|
|
|
2020-08-06 23:41:02 +08:00
|
|
|
AbstractTableSet::AbstractTableSet(Database *parent) : QObject(parent),
|
|
|
|
|
data(new AbstractTableSetData(parent))
|
2016-05-12 14:08:58 +08:00
|
|
|
{
|
|
|
|
|
parent->add(this);
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-06 23:41:02 +08:00
|
|
|
AbstractTableSet::AbstractTableSet(Table *parent) : QObject(parent),
|
|
|
|
|
data(new AbstractTableSetData(parent))
|
2016-05-12 14:08:58 +08:00
|
|
|
{
|
|
|
|
|
parent->add(this);
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-06 23:41:02 +08:00
|
|
|
AbstractTableSet::~AbstractTableSet()
|
2019-03-08 00:15:58 +08:00
|
|
|
{
|
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);
|
2019-03-08 00:15:58 +08:00
|
|
|
}
|
|
|
|
|
|
2020-08-06 23:41:02 +08:00
|
|
|
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) {
|
2020-07-14 21:32:52 +08:00
|
|
|
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
|
|
|
|
2020-07-14 21:32:52 +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);
|
2020-07-14 21:32:52 +08:00
|
|
|
if (cleanUp)
|
2020-08-12 21:46:56 +08:00
|
|
|
#ifdef NUT_RAW_POINTER
|
2019-07-21 15:11:34 +08:00
|
|
|
t->deleteLater();
|
2020-08-12 21:46:56 +08:00
|
|
|
#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)
|
2019-07-19 20:52:52 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2020-08-06 23:41:02 +08:00
|
|
|
void AbstractTableSet::clearChilds()
|
2016-05-21 16:09:03 +08:00
|
|
|
{
|
2020-08-12 21:46:56 +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
|
2019-07-19 20:52:52 +08:00
|
|
|
data->childs.clear();
|
2016-05-21 16:09:03 +08:00
|
|
|
}
|
|
|
|
|
|
2020-08-06 23:41:02 +08:00
|
|
|
void AbstractTableSet::add(Row<Table> t)
|
2019-07-08 22:53:02 +08:00
|
|
|
{
|
|
|
|
|
data.detach();
|
|
|
|
|
data->childs.append(t);
|
2019-07-17 21:43:30 +08:00
|
|
|
t->setParentTableSet(this);
|
2019-07-08 22:53:02 +08:00
|
|
|
}
|
|
|
|
|
|
2020-08-06 23:41:02 +08:00
|
|
|
void AbstractTableSet::remove(Row<Table> t)
|
2019-07-08 22:53:02 +08:00
|
|
|
{
|
|
|
|
|
data.detach();
|
2019-07-21 15:11:34 +08:00
|
|
|
data->childs.removeAll(t);
|
2019-07-08 22:53:02 +08:00
|
|
|
}
|
|
|
|
|
|
2020-08-06 23:41:02 +08:00
|
|
|
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
|
|
|
|
2020-08-06 23:41:02 +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
|
|
|
}
|
|
|
|
|
|
2020-08-06 23:41:02 +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
|