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"
|
|
|
|
|
|
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-07 16:19:20 +08:00
|
|
|
_database(parent), _table(nullptr)//, _tableName(QString())
|
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-07 16:19:20 +08:00
|
|
|
_database(nullptr), _table(parent)//, _tableName(QString())
|
2016-05-12 14:08:58 +08:00
|
|
|
{
|
|
|
|
|
parent->add(this);
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-08 00:15:58 +08:00
|
|
|
TableSetBase::~TableSetBase()
|
|
|
|
|
{
|
|
|
|
|
foreach (Table *t, _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;
|
|
|
|
|
if (_table)
|
|
|
|
|
masterModel = db->model().tableByClassName(_table->metaObject()->className());
|
|
|
|
|
|
|
|
|
|
foreach (Table *t, _childRows) {
|
2016-05-12 14:08:58 +08:00
|
|
|
if(_table)
|
2019-06-07 16:19:20 +08:00
|
|
|
t->setParentTable(_table, masterModel,
|
|
|
|
|
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-07 16:19:20 +08:00
|
|
|
_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-07 16:19:20 +08:00
|
|
|
foreach (Table *t, _childRows)
|
2017-09-10 22:18:20 +08:00
|
|
|
t->deleteLater();
|
2019-06-07 16:19:20 +08:00
|
|
|
_childRows.clear();
|
2016-05-21 16:09:03 +08:00
|
|
|
}
|
|
|
|
|
|
2016-05-12 14:08:58 +08:00
|
|
|
void TableSetBase::add(Table *t)
|
|
|
|
|
{
|
2016-05-21 16:09:03 +08:00
|
|
|
if(!_tables.contains(t)){
|
|
|
|
|
_tables.insert(t);
|
2019-06-07 16:19:20 +08:00
|
|
|
_childRows.append(t);
|
2016-05-21 16:09:03 +08:00
|
|
|
}
|
2016-05-12 14:08:58 +08:00
|
|
|
}
|
|
|
|
|
|
2019-03-07 23:35:57 +08:00
|
|
|
void TableSetBase::remove(Table *t)
|
|
|
|
|
{
|
|
|
|
|
_tables.remove(t);
|
2019-06-07 16:19:20 +08:00
|
|
|
_childRows.removeOne(t);
|
2019-03-07 23:35:57 +08:00
|
|
|
}
|
|
|
|
|
|
2016-05-12 14:08:58 +08:00
|
|
|
QString TableSetBase::childClassName() const
|
|
|
|
|
{
|
|
|
|
|
return _childClassName;
|
|
|
|
|
}
|
2016-05-21 16:09:03 +08:00
|
|
|
|
|
|
|
|
Database *TableSetBase::database() const
|
|
|
|
|
{
|
|
|
|
|
return _database;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TableSetBase::setDatabase(Database *database)
|
|
|
|
|
{
|
|
|
|
|
_database = database;
|
|
|
|
|
}
|
2017-02-01 18:01:21 +08:00
|
|
|
|
|
|
|
|
NUT_END_NAMESPACE
|