Nut/src/table.cpp

158 lines
3.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 <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
Table::Table(QObject *parent) : QObject(parent)
{
setStatus(NewCreated);
}
void Table::add(TableSetBase *t)
{
this->tableSets.insert(t);
}
QString Table::primaryKey() const
{
2016-05-21 16:09:03 +08:00
// static QString ret = QString::null;
// if(ret == QString::null){
// for(int i = 0; i < metaObject()->classInfoCount(); i++){
// QMetaClassInfo ci = metaObject()->classInfo(i);
// QString ciName = ci.name();
// if(ciName.startsWith(__nut_NAME_PERFIX))
// ciName.remove(__nut_NAME_PERFIX);
// if(ciName.contains(" ")){
// QStringList parts = ciName.split(" ");
// QString propName = parts.at(1);
// if(propName == __nut_PRIMARY_KEY)
// ret = parts.at(0);
// }
// }
// if(ret == QString::null)
// ret = "";
// }
// return ret;
2017-02-01 18:01:21 +08:00
return TableModel::findByClassName(metaObject()->className())->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
{
2018-01-11 18:08:45 +08:00
auto m = TableModel::findByClassName(metaObject()->className());
auto pk = m->primaryKey();
auto f = m->field(pk);
return f->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());
}
void Table::propertyChanged(QString propName)
{
if(propName == primaryKey())
return;
_changedProperties.insert(propName);
if(_status == FeatchedFromDB)
_status = Modified;
if(_status == NewCreated)
_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
TableModel *myModel = TableModel::findByClassName(metaObject()->className());
2016-05-12 14:08:58 +08:00
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
{
setProperty(QString(r->localColumn).toLatin1().data(), master->primaryValue());
_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
}
TableSetBase *Table::tableSet() const
{
return _tableSet;
}
void Table::setTableSet(TableSetBase *parent)
{
_tableSet = parent;
_tableSet->add(this);
}
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());
foreach(TableSetBase *ts, tableSets)
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