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>
|
2019-06-07 16:19:20 +08:00
|
|
|
#include <QSqlQuery>
|
|
|
|
|
|
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"
|
2017-10-07 18:26:05 +08:00
|
|
|
#include "generators/sqlgeneratorbase_p.h"
|
2019-06-07 16:19:20 +08:00
|
|
|
#include "tablesetbase_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-28 16:10:41 +08:00
|
|
|
d_ptr(new TablePrivate(this))
|
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()
|
|
|
|
|
{
|
|
|
|
|
Q_D(Table);
|
|
|
|
|
|
|
|
|
|
if (d->parentTableSet)
|
|
|
|
|
d->parentTableSet->remove(this);
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-12 14:08:58 +08:00
|
|
|
void Table::add(TableSetBase *t)
|
|
|
|
|
{
|
2019-02-28 16:10:41 +08:00
|
|
|
Q_D(Table);
|
|
|
|
|
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);
|
|
|
|
|
// 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);
|
|
|
|
|
// 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-02-28 16:10:41 +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
|
|
|
|
2019-06-06 21:00:03 +08:00
|
|
|
// foreach (FieldModel *f, d->model->fields())
|
|
|
|
|
// if(f->isPrimaryKey && propName == f->name && f->isAutoIncrement)
|
|
|
|
|
// return;
|
2016-05-12 14:08:58 +08:00
|
|
|
|
2019-02-28 16:10:41 +08:00
|
|
|
d->changedProperties.insert(propName);
|
|
|
|
|
if (d->status == FeatchedFromDB)
|
|
|
|
|
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);
|
|
|
|
|
d->model = model;
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-05 00:04:17 +08:00
|
|
|
void Table::clear()
|
|
|
|
|
{
|
2019-02-28 16:10:41 +08:00
|
|
|
Q_D(Table);
|
|
|
|
|
d->changedProperties.clear();
|
2017-06-05 00:04:17 +08:00
|
|
|
}
|
|
|
|
|
|
2016-05-12 14:08:58 +08:00
|
|
|
QSet<QString> Table::changedProperties() const
|
|
|
|
|
{
|
2019-02-28 16:10:41 +08:00
|
|
|
Q_D(const Table);
|
|
|
|
|
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-02-28 16:10:41 +08:00
|
|
|
Q_D(Table);
|
|
|
|
|
|
2016-05-12 14:08:58 +08:00
|
|
|
QString masterClassName = 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
|
|
|
|
2019-06-19 17:36:49 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2018-01-15 06:12:46 +08:00
|
|
|
TableSetBase *Table::parentTableSet() const
|
2016-05-12 14:08:58 +08:00
|
|
|
{
|
2019-02-28 16:10:41 +08:00
|
|
|
Q_D(const Table);
|
|
|
|
|
return d->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
|
|
|
{
|
2019-02-28 16:10:41 +08:00
|
|
|
Q_D(Table);
|
|
|
|
|
d->parentTableSet = parent;
|
2019-03-08 00:15:58 +08:00
|
|
|
|
|
|
|
|
if (parent)
|
|
|
|
|
d->parentTableSet->add(this);
|
2018-01-15 06:12:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TableSetBase *Table::childTableSet(const QString &name) const
|
|
|
|
|
{
|
2019-02-28 16:10:41 +08:00
|
|
|
Q_D(const Table);
|
|
|
|
|
foreach (TableSetBase *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
|
|
|
{
|
2019-02-28 16:10:41 +08:00
|
|
|
Q_D(Table);
|
|
|
|
|
|
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
|
|
|
|
2019-06-06 19:02:02 +08:00
|
|
|
auto model = db->model().tableByClassName(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
|
|
|
|
2019-02-28 16:10:41 +08:00
|
|
|
foreach(TableSetBase *ts, d->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
|
|
|
|
|
{
|
2019-02-28 16:10:41 +08:00
|
|
|
Q_D(const Table);
|
|
|
|
|
return static_cast<Status>(d->status);
|
2016-05-12 14:08:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Table::setStatus(const Status &status)
|
|
|
|
|
{
|
2019-02-28 16:10:41 +08:00
|
|
|
Q_D(Table);
|
|
|
|
|
d->status = status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
TablePrivate::TablePrivate(Table *parent) : q_ptr(parent),
|
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
|