Nut/src/tableset.h

143 lines
3.3 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/>.
**
**************************************************************************/
#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>
2016-05-12 14:08:58 +08:00
#include <QtSql/QSqlQuery>
#include "tablesetbase_p.h"
#include "table.h"
#include "bulkinserter.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;
class BulkInserter;
2016-05-12 14:08:58 +08:00
template<class T>
class NUT_EXPORT TableSet : public TableSetBase
{
public:
2018-02-26 18:14:36 +08:00
explicit TableSet(Database *parent);
explicit TableSet(Table *parent);
2016-05-12 14:08:58 +08:00
void append(T *t);
2018-01-09 17:33:54 +08:00
void append(QList<T *> t);
2016-05-12 14:08:58 +08:00
void remove(T *t);
2018-01-09 17:33:54 +08:00
void remove(QList<T *> t);
2016-05-12 14:08:58 +08:00
2018-01-09 17:33:54 +08:00
inline T *type() const {}
2016-05-21 16:09:03 +08:00
2016-05-12 14:08:58 +08:00
int length() const;
T *at(int i) const;
const 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);
BulkInserter *bulkInserter();
2016-05-12 14:08:58 +08:00
};
template<class T>
Q_OUTOFLINE_TEMPLATE TableSet<T>::TableSet(Database *parent) : TableSetBase(parent)
{
2018-01-09 21:04:21 +08:00
_childClassName = T::staticMetaObject.className();
2016-05-12 14:08:58 +08:00
}
template<class T>
Q_OUTOFLINE_TEMPLATE TableSet<T>::TableSet(Table *parent) : TableSetBase(parent)
{
2018-01-09 21:04:21 +08:00
_childClassName = 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)
{
Query<T> *q = new Query<T>(_database, this, autoDelete);
2016-05-12 14:08:58 +08:00
return q;
}
template<class T>
Q_OUTOFLINE_TEMPLATE BulkInserter *TableSet<T>::bulkInserter()
{
BulkInserter *bi = new BulkInserter(_childClassName);
return bi;
}
template<class T>
2016-05-12 14:08:58 +08:00
Q_OUTOFLINE_TEMPLATE int TableSet<T>::length() const
{
return _tables.count();
}
template<class T>
2018-01-09 17:33:54 +08:00
Q_OUTOFLINE_TEMPLATE T *TableSet<T >::at(int i) const
2016-05-12 14:08:58 +08:00
{
2019-01-09 23:49:50 +08:00
return reinterpret_cast<T*>(_tablesList.at(i));
2016-05-12 14:08:58 +08:00
}
template<class T>
Q_OUTOFLINE_TEMPLATE const T &TableSet<T>::operator[](int i) const
{
2016-05-21 16:09:03 +08:00
return _tablesList[i];
2016-05-12 14:08:58 +08:00
}
template<class T>
Q_OUTOFLINE_TEMPLATE void TableSet<T>::append(T *t)
{
_tables.insert(t);
2016-05-21 16:09:03 +08:00
_tablesList.append(t);
2016-05-12 14:08:58 +08:00
// rows.append(t);
2018-01-15 06:12:46 +08:00
t->setParentTableSet(this);
2016-05-12 14:08:58 +08:00
if(t->status() != Table::FeatchedFromDB)
t->setStatus(Table::Added);
}
template<class T>
Q_OUTOFLINE_TEMPLATE void TableSet<T>::append(QList<T *> t)
{
foreach (T* i, t)
append(i);
}
template<class T>
Q_OUTOFLINE_TEMPLATE void TableSet<T>::remove(T *t)
{
_tables.remove(t);
t->setStatus(Table::Deleted);
}
template<class T>
Q_OUTOFLINE_TEMPLATE void TableSet<T>::remove(QList<T *> t)
{
foreach (T* i, t)
remove(i);
}
2017-02-01 18:01:21 +08:00
NUT_END_NAMESPACE
2016-05-12 14:08:58 +08:00
#endif // TABLESET_H