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"
|
2020-08-06 23:41:02 +08:00
|
|
|
#include "abstracttableset.h"
|
2021-01-27 22:50:31 +08:00
|
|
|
#include "propertysignalmapper.h"
|
2016-05-12 14:08:58 +08:00
|
|
|
|
2022-04-28 19:53:42 +08:00
|
|
|
QT_BEGIN_NAMESPACE
|
|
|
|
|
|
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
|
|
|
|
|
*/
|
|
|
|
|
|
2020-08-12 21:46:56 +08:00
|
|
|
/*!
|
|
|
|
|
* \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
|
|
|
|
2019-03-07 23:35:57 +08:00
|
|
|
Table::~Table()
|
|
|
|
|
{
|
2019-07-17 20:41:33 +08:00
|
|
|
//Q_D(Table);
|
2019-03-07 23:35:57 +08:00
|
|
|
|
2019-07-17 21:43:30 +08:00
|
|
|
// if (d->parentTableSet)
|
|
|
|
|
// d->parentTableSet->remove(this);
|
2019-03-07 23:35:57 +08:00
|
|
|
}
|
|
|
|
|
|
2020-08-06 23:41:02 +08:00
|
|
|
void Table::add(AbstractTableSet *t)
|
2016-05-12 14:08:58 +08:00
|
|
|
{
|
2019-07-17 20:41:33 +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
|
|
|
|
|
//{
|
2019-07-17 20:41:33 +08:00
|
|
|
// //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
|
|
|
|
|
//{
|
2019-07-17 20:41:33 +08:00
|
|
|
// //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
|
|
|
{
|
2019-07-17 20:41:33 +08:00
|
|
|
d.detach();
|
2019-02-28 16:10:41 +08:00
|
|
|
d->changedProperties.insert(propName);
|
2020-07-14 21:32:52 +08:00
|
|
|
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)
|
|
|
|
|
{
|
2019-07-17 20:41:33 +08:00
|
|
|
//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()
|
|
|
|
|
{
|
2019-07-17 20:41:33 +08:00
|
|
|
//Q_D(Table);
|
2019-02-28 16:10:41 +08:00
|
|
|
d->changedProperties.clear();
|
2017-06-05 00:04:17 +08:00
|
|
|
}
|
|
|
|
|
|
2022-04-28 19:53:42 +08:00
|
|
|
const QSet<QString> &Table::changedProperties() const
|
2016-05-12 14:08:58 +08:00
|
|
|
{
|
2019-07-17 20:41:33 +08:00
|
|
|
//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
|
|
|
{
|
2019-07-17 20:41:33 +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
|
|
|
|
2022-04-28 19:53:42 +08:00
|
|
|
for (auto &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
|
|
|
}
|
|
|
|
|
|
2021-01-27 22:50:31 +08:00
|
|
|
void Table::propertyChanged()
|
|
|
|
|
{
|
|
|
|
|
auto pname = PropertySignalMapper::changedProperty(this, senderSignalIndex());
|
|
|
|
|
propertyChanged(pname);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Table::init()
|
|
|
|
|
{
|
|
|
|
|
PropertySignalMapper::map(this);
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-06 23:41:02 +08:00
|
|
|
AbstractTableSet *Table::parentTableSet() const
|
2016-05-12 14:08:58 +08:00
|
|
|
{
|
2019-07-17 20:41:33 +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
|
|
|
}
|
|
|
|
|
|
2020-08-06 23:41:02 +08:00
|
|
|
void Table::setParentTableSet(AbstractTableSet *parent)
|
2016-05-12 14:08:58 +08:00
|
|
|
{
|
2019-07-17 20:41:33 +08:00
|
|
|
//Q_D(Table);
|
2019-02-28 16:10:41 +08:00
|
|
|
d->parentTableSet = parent;
|
2019-03-08 00:15:58 +08:00
|
|
|
|
2019-07-17 21:43:30 +08:00
|
|
|
// if (parent)
|
|
|
|
|
// d->parentTableSet->add(this);
|
2018-01-15 06:12:46 +08:00
|
|
|
}
|
|
|
|
|
|
2020-08-06 23:41:02 +08:00
|
|
|
AbstractTableSet *Table::childTableSet(const QString &name) const
|
2018-01-15 06:12:46 +08:00
|
|
|
{
|
2019-07-17 20:41:33 +08:00
|
|
|
//Q_D(const Table);
|
2022-04-28 19:53:42 +08:00
|
|
|
for (auto &t: qAsConst(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
|
|
|
{
|
2019-07-17 20:41:33 +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()));
|
2019-06-06 04:43:36 +08:00
|
|
|
if(status() == Added && model->isPrimaryKeyAutoIncrement())
|
|
|
|
|
setProperty(model->primaryKey().toLatin1().data(), q.lastInsertId());
|
2016-05-12 14:08:58 +08:00
|
|
|
|
2020-08-06 23:41:02 +08:00
|
|
|
foreach(AbstractTableSet *ts, d->childTableSets)
|
2016-05-12 14:08:58 +08:00
|
|
|
ts->save(db);
|
2020-07-14 21:32:52 +08:00
|
|
|
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
|
|
|
|
|
{
|
2019-07-17 20:41:33 +08:00
|
|
|
//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)
|
|
|
|
|
{
|
2019-07-17 20:41:33 +08:00
|
|
|
//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()
|
|
|
|
|
{
|
2019-07-17 20:41:33 +08:00
|
|
|
// 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
|
2022-04-28 19:53:42 +08:00
|
|
|
|
|
|
|
|
QT_END_NAMESPACE
|