Nut/src/nut/core/table.cpp

233 lines
5.6 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/>.
**
**************************************************************************/
2020-07-29 22:11:19 +08:00
#include <QtCore/QMetaMethod>
#include <QtCore/QVariant>
#include <QtSql/QSqlQuery>
2019-06-07 16:19:20 +08:00
2016-05-12 14:08:58 +08:00
#include "table.h"
2019-02-28 16:10:41 +08:00
#include "table_p.h"
2016-05-12 14:08:58 +08:00
#include "database.h"
2018-01-11 17:36:48 +08:00
#include "databasemodel.h"
2020-08-06 23:19:27 +08:00
#include "abstractsqlgenerator.h"
#include "abstracttableset.h"
#include "propertysignalmapper.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
*/
/*!
* \class Table
* \brief Base class for all tables
*/
2019-02-16 21:36:38 +08:00
Table::Table(QObject *parent) : QObject(parent),
2019-07-21 23:28:20 +08:00
d(new TablePrivate)
2019-06-07 16:19:20 +08:00
{ }
2016-05-12 14:08:58 +08:00
Table::~Table()
{
//Q_D(Table);
2019-07-17 21:43:30 +08:00
// if (d->parentTableSet)
// d->parentTableSet->remove(this);
}
void Table::add(AbstractTableSet *t)
2016-05-12 14:08:58 +08:00
{
//Q_D(Table);
2019-02-28 16:10:41 +08:00
d->childTableSets.insert(t);
2016-05-12 14:08:58 +08:00
}
2019-06-07 16:19:20 +08:00
//QString Table::primaryKey() const
//{
// //Q_D(const Table);
2019-06-07 16:19:20 +08:00
// return d->model->primaryKey();
//}
2016-05-12 14:08:58 +08:00
2019-06-07 16:19:20 +08:00
//bool Table::isPrimaryKeyAutoIncrement() const
//{
// //Q_D(const Table);
2019-06-07 16:19:20 +08:00
// FieldModel *pk = d->model->field(d->model->primaryKey());
// if (!pk)
// return false;
// return pk->isAutoIncrement;
//}
2016-05-12 14:08:58 +08:00
2016-05-21 16:09:03 +08:00
2019-06-07 16:19:20 +08:00
//QVariant Table::primaryValue() const
//{
// return property(primaryKey().toLatin1().data());
//}
2016-05-12 14:08:58 +08:00
2019-02-10 22:11:22 +08:00
void Table::propertyChanged(const QString &propName)
2016-05-12 14:08:58 +08:00
{
//Q_D(Table);
2019-06-06 21:00:03 +08:00
// if (!d->model)
// d->model = TableModel::findByClassName(metaObject()->className());
2018-01-16 04:04:42 +08:00
2019-06-06 21:00:03 +08:00
// if (!d->model)
// qFatal ("model for class '%s' not found", qPrintable(metaObject()->className()));
2018-02-13 16:54:16 +08:00
2021-03-14 16:42:04 +08:00
// Q_FOREACH (FieldModel *f, d->model->fields())
2019-06-06 21:00:03 +08:00
// if(f->isPrimaryKey && propName == f->name && f->isAutoIncrement)
// return;
2016-05-12 14:08:58 +08:00
d.detach();
2019-02-28 16:10:41 +08:00
d->changedProperties.insert(propName);
if (d->status == FetchedFromDB)
2019-02-28 16:10:41 +08:00
d->status = Modified;
2016-05-12 14:08:58 +08:00
2019-02-28 16:10:41 +08:00
if (d->status == NewCreated)
d->status = Added;
2016-05-12 14:08:58 +08:00
}
2019-06-07 16:19:20 +08:00
void Table::setModel(TableModel *model)
{
//Q_D(Table);
2019-06-07 16:19:20 +08:00
d->model = model;
}
2017-06-05 00:04:17 +08:00
void Table::clear()
{
//Q_D(Table);
2019-02-28 16:10:41 +08:00
d->changedProperties.clear();
2017-06-05 00:04:17 +08:00
}
2016-05-12 14:08:58 +08:00
QSet<QString> Table::changedProperties() const
{
//Q_D(const Table);
2019-02-28 16:10:41 +08:00
return d->changedProperties;
2016-05-12 14:08:58 +08:00
}
2019-06-07 16:19:20 +08:00
bool Table::setParentTable(Table *master, TableModel *masterModel, TableModel *model)
2016-05-12 14:08:58 +08:00
{
//Q_D(Table);
d.detach();
2019-02-28 16:10:41 +08:00
2020-07-30 23:05:11 +08:00
QString masterClassName = QString::fromUtf8(master->metaObject()->className());
2019-06-06 21:00:03 +08:00
d->refreshModel();
2016-05-12 14:08:58 +08:00
2019-06-07 16:19:20 +08:00
// if (!d->model)
// d->model = TableModel::findByClassName(metaObject()->className());
2019-06-06 19:02:02 +08:00
2021-03-14 16:42:04 +08:00
Q_FOREACH (RelationModel *r, model->foreignKeys())
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(),
2019-06-07 16:19:20 +08:00
master->property(masterModel->primaryKey().toUtf8().data()));
2019-02-28 16:10:41 +08:00
d->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
}
void Table::propertyChanged()
{
auto pname = PropertySignalMapper::changedProperty(this, senderSignalIndex());
propertyChanged(pname);
}
void Table::init()
{
PropertySignalMapper::map(this);
}
AbstractTableSet *Table::parentTableSet() const
2016-05-12 14:08:58 +08:00
{
//Q_D(const Table);
2019-02-28 16:10:41 +08:00
return d->parentTableSet;
2016-05-12 14:08:58 +08:00
}
void Table::setParentTableSet(AbstractTableSet *parent)
2016-05-12 14:08:58 +08:00
{
//Q_D(Table);
2019-02-28 16:10:41 +08:00
d->parentTableSet = parent;
2019-07-17 21:43:30 +08:00
// if (parent)
// d->parentTableSet->add(this);
2018-01-15 06:12:46 +08:00
}
AbstractTableSet *Table::childTableSet(const QString &name) const
2018-01-15 06:12:46 +08:00
{
//Q_D(const Table);
2021-03-14 16:42:04 +08:00
Q_FOREACH (AbstractTableSet *t, d->childTableSets)
2018-01-15 06:12:46 +08:00
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
{
//Q_D(Table);
2019-02-28 16:10:41 +08:00
2020-07-30 23:05:11 +08:00
QSqlQuery q = db->exec(db->sqlGenerator()->saveRecord(this, db->tableName(QString::fromUtf8(metaObject()->className()))));
2016-05-12 14:08:58 +08:00
2020-07-30 23:05:11 +08:00
auto model = db->model().tableByClassName(QString::fromUtf8(metaObject()->className()));
if(status() == Added && model->isPrimaryKeyAutoIncrement())
setProperty(model->primaryKey().toLatin1().data(), q.lastInsertId());
2016-05-12 14:08:58 +08:00
foreach(AbstractTableSet *ts, d->childTableSets)
2016-05-12 14:08:58 +08:00
ts->save(db);
setStatus(FetchedFromDB);
2017-05-28 23:08:59 +08:00
return q.numRowsAffected();
2016-05-12 14:08:58 +08:00
}
Table::Status Table::status() const
{
//Q_D(const Table);
2019-02-28 16:10:41 +08:00
return static_cast<Status>(d->status);
2016-05-12 14:08:58 +08:00
}
void Table::setStatus(const Status &status)
{
//Q_D(Table);
2019-02-28 16:10:41 +08:00
d->status = status;
}
2019-07-21 23:28:20 +08:00
TablePrivate::TablePrivate() : QSharedData(),
2019-06-06 19:02:02 +08:00
model(nullptr), status(Table::NewCreated), parentTableSet(nullptr)
2019-02-28 16:10:41 +08:00
{
2016-05-12 14:08:58 +08:00
}
2019-06-06 21:00:03 +08:00
void TablePrivate::refreshModel()
{
// Q_Q(Table);
2019-06-07 16:19:20 +08:00
// if (!model)
// model = TableModel::findByClassName(q->metaObject()->className());
2019-06-06 21:00:03 +08:00
}
2017-02-01 18:01:21 +08:00
NUT_END_NAMESPACE