Nut/src/tableset.h

164 lines
3.9 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"
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;
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>
class NUT_EXPORT TableSet : public TableSetBase
{
public:
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;
Row<T> at(int i) const;
const 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);
BulkInserter *bulkInserter();
2016-05-12 14:08:58 +08:00
};
template<class T>
Q_OUTOFLINE_TEMPLATE TableSet<T>::TableSet(Database *parent) : TableSetBase(parent)
{
2019-06-18 23:37:03 +08:00
data->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)
{
2019-06-18 23:37:03 +08:00
data->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)
{
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>
Q_OUTOFLINE_TEMPLATE BulkInserter *TableSet<T>::bulkInserter()
{
2019-06-18 23:37:03 +08:00
BulkInserter *bi = new BulkInserter(data->database, data->childClassName);
return bi;
}
template<class T>
2016-05-12 14:08:58 +08:00
Q_OUTOFLINE_TEMPLATE int TableSet<T>::length() const
{
return data->childs.count();
2016-05-12 14:08:58 +08:00
}
template<class T>
Q_OUTOFLINE_TEMPLATE Row<T> TableSet<T >::at(int i) const
2016-05-12 14:08:58 +08:00
{
#ifdef NUT_SHARED_POINTER
return data->childs.at(i).objectCast<T>();
#else
return reinterpret_cast<T*>(data->childs.at(i));
#endif
2016-05-12 14:08:58 +08:00
}
template<class T>
Q_OUTOFLINE_TEMPLATE const Row<T> TableSet<T>::operator[](int i) const
2016-05-12 14:08:58 +08:00
{
#ifdef NUT_SHARED_POINTER
return data->childs.at(i).objectCast<T>();
#else
return reinterpret_cast<T*>(data->childs.at(i));
#endif
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
{
data.detach();
data->childs.append(t);
// 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);
2016-05-12 14:08:58 +08:00
if(t->status() != Table::FeatchedFromDB)
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
{
data.detach();
// data->childs.removeOne(t.data());
// data->tables.remove(t.data());
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