Nut/src/table.cpp

147 lines
3.7 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"
#include "sqlgeneratorbase_p.h"
QT_BEGIN_NAMESPACE
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;
return TableModel::model(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
{
2016-05-21 16:09:03 +08:00
return TableModel::model(metaObject()->className())->field(primaryKey())->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;
}
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())
2016-05-12 14:08:58 +08:00
if(r->className == masterClassName)
{
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);
}
void Table::save(Database *db)
{
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);
}
Table::Status Table::status() const
{
return _status;
}
void Table::setStatus(const Status &status)
{
_status = status;
}
QT_END_NAMESPACE