Nut/src/tablesetbase.cpp

118 lines
2.9 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 "tablesetbase_p.h"
#include "databasemodel.h"
2019-06-18 23:37:03 +08:00
#include "tablesetbasedata.h"
2016-05-12 14:08:58 +08:00
2017-02-01 18:01:21 +08:00
NUT_BEGIN_NAMESPACE
2019-02-07 23:52:57 +08:00
TableSetBase::TableSetBase(Database *parent) : QObject(parent),
2019-06-18 23:37:03 +08:00
data(new TableSetBaseData(parent))
2016-05-12 14:08:58 +08:00
{
parent->add(this);
}
2019-02-07 23:52:57 +08:00
TableSetBase::TableSetBase(Table *parent) : QObject(parent),
2019-06-18 23:37:03 +08:00
data(new TableSetBaseData(parent))
2016-05-12 14:08:58 +08:00
{
parent->add(this);
}
TableSetBase::~TableSetBase()
{
2019-06-18 23:37:03 +08:00
foreach (Table *t, data->tables)
t->setParentTableSet(nullptr);
}
2017-09-10 22:18:20 +08:00
int TableSetBase::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)
masterModel = db->model().tableByClassName(data->table->metaObject()->className());
2019-06-07 16:19:20 +08:00
2019-06-19 04:32:27 +08:00
foreach (Table *t, data->childRows) {
2019-06-18 23:37:03 +08:00
if(data->table)
t->setParentTable(data->table,
masterModel,
2019-06-07 16:19:20 +08:00
db->model().tableByClassName(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);
2017-09-10 22:18:20 +08:00
if(cleanUp)
t->deleteLater();
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-06-18 23:37:03 +08:00
data->childRows.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
}
2016-05-21 16:09:03 +08:00
void TableSetBase::clearChilds()
{
2019-06-18 23:37:03 +08:00
#ifndef NUT_SHARED_POINTER
foreach (Table *t, data->_childRows)
2017-09-10 22:18:20 +08:00
t->deleteLater();
2019-06-18 23:37:03 +08:00
#endif
data->childRows.clear();
2016-05-21 16:09:03 +08:00
}
2019-06-19 04:32:27 +08:00
void TableSetBase::add(Table *t)
2016-05-12 14:08:58 +08:00
{
2019-06-19 04:32:27 +08:00
if(!data->tables.contains(get(t))){
data.detach();
data->tables.insert(get(t));
data->childRows.append(get(t));
2016-05-21 16:09:03 +08:00
}
2016-05-12 14:08:58 +08:00
}
2019-06-19 04:32:27 +08:00
void TableSetBase::remove(Table *t)
{
2019-06-19 04:32:27 +08:00
data.detach();
data->tables.remove(get(t));
data->childRows.removeOne(get(t));
}
2016-05-12 14:08:58 +08:00
QString TableSetBase::childClassName() const
{
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 *TableSetBase::database() const
{
2019-06-18 23:37:03 +08:00
return data->database;
2016-05-21 16:09:03 +08:00
}
void TableSetBase::setDatabase(Database *database)
{
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
NUT_END_NAMESPACE