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/>.
|
|
|
|
|
**
|
|
|
|
|
**************************************************************************/
|
|
|
|
|
|
|
|
|
|
#ifndef TABLESET_H
|
|
|
|
|
#define TABLESET_H
|
|
|
|
|
|
2018-07-14 20:37:53 +08:00
|
|
|
#include <QtCore/QtGlobal>
|
2016-05-12 14:08:58 +08:00
|
|
|
#include <QtCore/QMetaMethod>
|
2018-01-09 00:59:16 +08:00
|
|
|
#include <QtCore/QMetaType>
|
2018-07-14 20:37:53 +08:00
|
|
|
#include <QtCore/QVariant>
|
2020-07-29 22:11:19 +08:00
|
|
|
#include <QtCore/QSharedPointer>
|
|
|
|
|
|
2016-05-12 14:08:58 +08:00
|
|
|
#include <QtSql/QSqlQuery>
|
|
|
|
|
|
2020-08-06 23:19:27 +08:00
|
|
|
#include "tablesetbase.h"
|
2016-05-12 14:08:58 +08:00
|
|
|
#include "table.h"
|
2019-03-07 23:35:57 +08:00
|
|
|
#include "bulkinserter.h"
|
2019-06-07 16:19:20 +08:00
|
|
|
#include "databasemodel.h"
|
2019-06-18 23:37:03 +08:00
|
|
|
#include "tablesetbasedata.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
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
|
class Query;
|
|
|
|
|
|
2019-03-07 23:35:57 +08:00
|
|
|
class BulkInserter;
|
2019-06-07 16:19:20 +08:00
|
|
|
class Database;
|
2019-06-19 04:32:27 +08:00
|
|
|
|
2016-05-12 14:08:58 +08:00
|
|
|
template<class T>
|
2020-07-28 16:25:06 +08:00
|
|
|
class TableSet : public TableSetBase
|
2016-05-12 14:08:58 +08:00
|
|
|
{
|
|
|
|
|
public:
|
2019-06-19 14:52:00 +08:00
|
|
|
typedef T value_type;
|
|
|
|
|
typedef T *pointer;
|
|
|
|
|
typedef T &reference;
|
|
|
|
|
|
2018-02-26 18:14:36 +08:00
|
|
|
explicit TableSet(Database *parent);
|
|
|
|
|
explicit TableSet(Table *parent);
|
2016-05-12 14:08:58 +08:00
|
|
|
|
2019-06-18 23:37:03 +08:00
|
|
|
void append(Row<T> t);
|
|
|
|
|
void append(RowList<T> t);
|
2019-06-19 17:16:55 +08:00
|
|
|
void remove(Row<T> t);
|
|
|
|
|
void remove(RowList<T> t);
|
2016-05-12 14:08:58 +08:00
|
|
|
|
|
|
|
|
int length() const;
|
2019-07-19 20:52:52 +08:00
|
|
|
Row<T> at(int i) const;
|
2020-07-14 21:32:52 +08:00
|
|
|
Row<T> operator[](int i) const;
|
2018-07-14 20:37:53 +08:00
|
|
|
|
2019-01-17 00:35:49 +08:00
|
|
|
Query<T> *query(bool autoDelete = true);
|
2019-03-07 23:35:57 +08:00
|
|
|
BulkInserter *bulkInserter();
|
2016-05-12 14:08:58 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
|
Q_OUTOFLINE_TEMPLATE TableSet<T>::TableSet(Database *parent) : TableSetBase(parent)
|
|
|
|
|
{
|
2020-07-30 23:05:11 +08:00
|
|
|
data->childClassName = QString::fromUtf8(T::staticMetaObject.className());
|
2016-05-12 14:08:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
|
Q_OUTOFLINE_TEMPLATE TableSet<T>::TableSet(Table *parent) : TableSetBase(parent)
|
|
|
|
|
{
|
2020-07-30 23:05:11 +08:00
|
|
|
data->childClassName = QString::fromUtf8(T::staticMetaObject.className());
|
2016-05-12 14:08:58 +08:00
|
|
|
}
|
|
|
|
|
|
2017-06-01 00:10:35 +08:00
|
|
|
template<class T>
|
|
|
|
|
Q_OUTOFLINE_TEMPLATE Query<T> *TableSet<T>::query(bool autoDelete)
|
|
|
|
|
{
|
2019-06-18 23:37:03 +08:00
|
|
|
Query<T> *q = new Query<T>(data->database, this, autoDelete);
|
2016-05-12 14:08:58 +08:00
|
|
|
|
|
|
|
|
return q;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<class T>
|
2019-03-07 23:35:57 +08:00
|
|
|
Q_OUTOFLINE_TEMPLATE BulkInserter *TableSet<T>::bulkInserter()
|
|
|
|
|
{
|
2019-06-18 23:37:03 +08:00
|
|
|
BulkInserter *bi = new BulkInserter(data->database, data->childClassName);
|
2019-03-07 23:35:57 +08:00
|
|
|
return bi;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<class T>
|
2016-05-12 14:08:58 +08:00
|
|
|
Q_OUTOFLINE_TEMPLATE int TableSet<T>::length() const
|
|
|
|
|
{
|
2019-07-19 20:52:52 +08:00
|
|
|
return data->childs.count();
|
2016-05-12 14:08:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<class T>
|
2020-07-14 21:32:52 +08:00
|
|
|
Q_OUTOFLINE_TEMPLATE Row<T> TableSet<T>::at(int i) const
|
2016-05-12 14:08:58 +08:00
|
|
|
{
|
2019-07-19 20:52:52 +08:00
|
|
|
#ifdef NUT_SHARED_POINTER
|
2019-08-04 15:18:15 +08:00
|
|
|
return data->childs.at(i).template objectCast<T>();
|
2019-07-19 20:52:52 +08:00
|
|
|
#else
|
|
|
|
|
return reinterpret_cast<T*>(data->childs.at(i));
|
|
|
|
|
#endif
|
2016-05-12 14:08:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<class T>
|
2020-07-14 21:32:52 +08:00
|
|
|
Q_OUTOFLINE_TEMPLATE Row<T> TableSet<T>::operator[](int i) const
|
2016-05-12 14:08:58 +08:00
|
|
|
{
|
2020-07-14 21:32:52 +08:00
|
|
|
return at(i);
|
2016-05-12 14:08:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<class T>
|
2019-06-18 23:37:03 +08:00
|
|
|
Q_OUTOFLINE_TEMPLATE void TableSet<T>::append(Row<T> t)
|
2016-05-12 14:08:58 +08:00
|
|
|
{
|
2019-06-19 14:52:00 +08:00
|
|
|
data.detach();
|
|
|
|
|
data->childs.append(t);
|
2019-07-19 20:52:52 +08:00
|
|
|
// data->tables.insert(t.data());
|
|
|
|
|
// data->childRows.append(t.data());
|
2019-06-07 16:19:20 +08:00
|
|
|
|
|
|
|
|
// if (_database)
|
|
|
|
|
// t->setModel(_database->model().tableByClassName(t->metaObject()->className()));
|
|
|
|
|
|
2018-01-15 06:12:46 +08:00
|
|
|
t->setParentTableSet(this);
|
2020-07-14 21:32:52 +08:00
|
|
|
if(t->status() != Table::FetchedFromDB)
|
2016-05-12 14:08:58 +08:00
|
|
|
t->setStatus(Table::Added);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<class T>
|
2019-06-18 23:37:03 +08:00
|
|
|
Q_OUTOFLINE_TEMPLATE void TableSet<T>::append(RowList<T> t)
|
2016-05-12 14:08:58 +08:00
|
|
|
{
|
2019-06-18 23:37:03 +08:00
|
|
|
foreach (Row<T> i, t)
|
2016-05-12 14:08:58 +08:00
|
|
|
append(i);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<class T>
|
2019-06-19 17:16:55 +08:00
|
|
|
Q_OUTOFLINE_TEMPLATE void TableSet<T>::remove(Row<T> t)
|
2016-05-12 14:08:58 +08:00
|
|
|
{
|
2019-06-19 14:52:00 +08:00
|
|
|
data.detach();
|
2019-07-19 20:52:52 +08:00
|
|
|
// data->childs.removeOne(t.data());
|
|
|
|
|
// data->tables.remove(t.data());
|
2019-06-19 14:52:00 +08:00
|
|
|
data->childs.removeOne(t);
|
2016-05-12 14:08:58 +08:00
|
|
|
t->setStatus(Table::Deleted);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<class T>
|
2019-06-19 17:16:55 +08:00
|
|
|
Q_OUTOFLINE_TEMPLATE void TableSet<T>::remove(RowList<T> t)
|
2016-05-12 14:08:58 +08:00
|
|
|
{
|
2019-06-19 17:16:55 +08:00
|
|
|
foreach (Row<T> i, t)
|
2016-05-12 14:08:58 +08:00
|
|
|
remove(i);
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-01 18:01:21 +08:00
|
|
|
NUT_END_NAMESPACE
|
2016-05-12 14:08:58 +08:00
|
|
|
|
|
|
|
|
#endif // TABLESET_H
|