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);
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-08 00:15:58 +08:00
|
|
|
TableSetBase::~TableSetBase()
|
|
|
|
|
{
|
2019-07-19 20:52:52 +08:00
|
|
|
// foreach (Table *t, data->tables)
|
|
|
|
|
// t->setParentTableSet(nullptr);
|
2019-07-08 22:53:02 +08:00
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
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-07-17 21:43:30 +08:00
|
|
|
foreach (Row<Table> t, data->childs) {
|
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)
|
2019-07-06 22:08:40 +08:00
|
|
|
#ifndef NUT_SHARED_POINTER
|
2017-09-10 22:18:20 +08:00
|
|
|
t->deleteLater();
|
2019-07-06 22:08:40 +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
|
|
|
}
|
|
|
|
|
|
2016-05-21 16:09:03 +08:00
|
|
|
void TableSetBase::clearChilds()
|
|
|
|
|
{
|
2019-06-18 23:37:03 +08:00
|
|
|
#ifndef NUT_SHARED_POINTER
|
2019-07-19 20:52:52 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2019-07-17 21:43:30 +08:00
|
|
|
//void TableSetBase::add(Table *t)
|
|
|
|
|
//{
|
|
|
|
|
// if(!data->tables.contains(get(t))){
|
|
|
|
|
// data.detach();
|
|
|
|
|
// data->tables.insert(get(t));
|
|
|
|
|
// data->childRows.append(get(t));
|
|
|
|
|
// }
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
//void TableSetBase::remove(Table *t)
|
|
|
|
|
//{
|
|
|
|
|
// data.detach();
|
|
|
|
|
// data->tables.remove(get(t));
|
|
|
|
|
// data->childRows.removeOne(get(t));
|
|
|
|
|
//}
|
2019-03-07 23:35:57 +08:00
|
|
|
|
2019-07-08 22:53:02 +08:00
|
|
|
void TableSetBase::add(Row<Table> t)
|
|
|
|
|
{
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TableSetBase::remove(Row<Table> t)
|
|
|
|
|
{
|
|
|
|
|
data.detach();
|
|
|
|
|
data->childs.removeOne(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
|