Nut/src/table.cpp

163 lines
4.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 <QMetaMethod>
#include <QVariant>
#include "table.h"
#include "database.h"
2018-01-11 17:36:48 +08:00
#include "databasemodel.h"
2017-10-07 18:26:05 +08:00
#include "generators/sqlgeneratorbase_p.h"
2016-05-12 14:08:58 +08:00
2017-02-01 18:01:21 +08:00
NUT_BEGIN_NAMESPACE
2016-05-12 14:08:58 +08:00
2018-01-16 04:04:42 +08:00
/*
* FIXME:
* Qt can not access metaObject inside of constructor
* so, if we can't initalize myModel inside of ctor. in
* other side myModel inited in propertyChanged signal, so
* any method that uses myModel (like: primaryKey, ...) can't
* be accessed before any property set. So ugly, but there are
* no other way for now.
*
* This should be fixed to v1.2
*/
2019-02-16 21:36:38 +08:00
Table::Table(QObject *parent) : QObject(parent),
2019-02-10 22:11:22 +08:00
_status(NewCreated), _parentTableSet(nullptr)
2019-02-16 21:36:38 +08:00
{
myModel = TableModel::findByClassName(metaObject()->className());
}
2016-05-12 14:08:58 +08:00
void Table::add(TableSetBase *t)
{
2018-01-15 06:12:46 +08:00
this->childTableSets.insert(t);
2016-05-12 14:08:58 +08:00
}
QString Table::primaryKey() const
{
2018-01-16 04:04:42 +08:00
return myModel->primaryKey();
2016-05-12 14:08:58 +08:00
}
2016-05-21 16:09:03 +08:00
bool Table::isPrimaryKeyAutoIncrement() const
2016-05-12 14:08:58 +08:00
{
2019-01-09 23:49:50 +08:00
FieldModel *pk = myModel->field(myModel->primaryKey());
if (!pk)
return false;
return pk->isAutoIncrement;
2016-05-12 14:08:58 +08:00
}
2016-05-21 16:09:03 +08:00
2016-05-12 14:08:58 +08:00
QVariant Table::primaryValue() const
{
return property(primaryKey().toLatin1().data());
}
2019-02-10 22:11:22 +08:00
void Table::propertyChanged(const QString &propName)
2016-05-12 14:08:58 +08:00
{
2018-01-16 04:04:42 +08:00
if (!myModel)
myModel = TableModel::findByClassName(metaObject()->className());
2018-02-13 16:54:16 +08:00
if (!myModel)
2019-01-09 23:49:50 +08:00
qFatal ("model for class '%s' not found", qPrintable(metaObject()->className()));
2018-02-13 16:54:16 +08:00
2018-07-12 14:47:41 +08:00
foreach (FieldModel *f, myModel->fields())
if(f->isPrimaryKey && propName == f->name && f->isAutoIncrement)
return;
2016-05-12 14:08:58 +08:00
_changedProperties.insert(propName);
2018-01-16 04:04:42 +08:00
if (_status == FeatchedFromDB)
2016-05-12 14:08:58 +08:00
_status = Modified;
2018-01-16 04:04:42 +08:00
if (_status == NewCreated)
2016-05-12 14:08:58 +08:00
_status = Added;
}
2017-06-05 00:04:17 +08:00
void Table::clear()
{
_changedProperties.clear();
}
2016-05-12 14:08:58 +08:00
QSet<QString> Table::changedProperties() const
{
return _changedProperties;
}
bool Table::setParentTable(Table *master)
{
QString masterClassName = master->metaObject()->className();
2016-05-21 16:09:03 +08:00
foreach (RelationModel *r, myModel->foregionKeys())
2018-01-13 23:59:55 +08:00
if(r->masterClassName == masterClassName)
2016-05-12 14:08:58 +08:00
{
2018-01-16 04:04:42 +08:00
setProperty(QString(r->localColumn).toLatin1().data(),
master->primaryValue());
2016-05-12 14:08:58 +08:00
_changedProperties.insert(r->localColumn);
2016-05-21 16:09:03 +08:00
return true;
2016-05-12 14:08:58 +08:00
}
2016-05-21 16:09:03 +08:00
return false;
2016-05-12 14:08:58 +08:00
}
2018-01-15 06:12:46 +08:00
TableSetBase *Table::parentTableSet() const
2016-05-12 14:08:58 +08:00
{
2018-01-15 06:12:46 +08:00
return _parentTableSet;
2016-05-12 14:08:58 +08:00
}
2018-01-15 06:12:46 +08:00
void Table::setParentTableSet(TableSetBase *parent)
2016-05-12 14:08:58 +08:00
{
2018-01-15 06:12:46 +08:00
_parentTableSet = parent;
_parentTableSet->add(this);
}
TableSetBase *Table::childTableSet(const QString &name) const
{
foreach (TableSetBase *t, childTableSets)
if (t->childClassName() == name)
return t;
return Q_NULLPTR;
2016-05-12 14:08:58 +08:00
}
2017-05-28 23:08:59 +08:00
int Table::save(Database *db)
2016-05-12 14:08:58 +08:00
{
2016-05-21 16:09:03 +08:00
QSqlQuery q = db->exec(db->sqlGenertor()->saveRecord(this, db->tableName(metaObject()->className())));
2016-05-12 14:08:58 +08:00
2016-05-21 16:09:03 +08:00
if(status() == Added && isPrimaryKeyAutoIncrement())
2016-05-12 14:08:58 +08:00
setProperty(primaryKey().toLatin1().data(), q.lastInsertId());
2018-01-15 06:12:46 +08:00
foreach(TableSetBase *ts, childTableSets)
2016-05-12 14:08:58 +08:00
ts->save(db);
setStatus(FeatchedFromDB);
2017-05-28 23:08:59 +08:00
return q.numRowsAffected();
2016-05-12 14:08:58 +08:00
}
Table::Status Table::status() const
{
return _status;
}
void Table::setStatus(const Status &status)
{
_status = status;
}
2017-02-01 18:01:21 +08:00
NUT_END_NAMESPACE