Nut  0.1
tableset.h
1 /**************************************************************************
2 **
3 ** This file is part of Nut project.
4 ** https://github.com/HamedMasafi/Nut
5 **
6 ** Nut is free software: you can redistribute it and/or modify
7 ** it under the terms of the GNU Lesser General Public License as published by
8 ** the Free Software Foundation, either version 3 of the License, or
9 ** (at your option) any later version.
10 **
11 ** Nut is distributed in the hope that it will be useful,
12 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ** GNU Lesser General Public License for more details.
15 **
16 ** You should have received a copy of the GNU Lesser General Public License
17 ** along with Nut. If not, see <http://www.gnu.org/licenses/>.
18 **
19 **************************************************************************/
20 
21 #ifndef TABLESET_H
22 #define TABLESET_H
23 
24 #include <QtCore/qglobal.h>
25 #include <QtCore/QVariant>
26 #include <QtCore/QMetaMethod>
27 #include <QtSql/QSqlQuery>
28 
29 #include "tablesetbase_p.h"
30 #include "database.h"
31 #include "table.h"
32 
33 QT_BEGIN_NAMESPACE
34 
35 template<class T>
36 class Query;
37 
38 template<class T>
39 class NUT_EXPORT TableSet : public TableSetBase
40 {
41 public:
42  TableSet(Database *parent);
43  TableSet(Table *parent);
44 
45  void append(T *t);
46  void append(QList<T*> t);
47  void remove(T *t);
48  void remove(QList<T*> t);
49 
50  inline T type() const {}
51 
52  int length() const;
53  T *at(int i) const;
54  const T &operator[](int i) const;
55  Query<T> *createQuery();
56 };
57 
58 template<class T>
59 Q_OUTOFLINE_TEMPLATE TableSet<T>::TableSet(Database *parent) : TableSetBase(parent)
60 {
61  _childClassName = T::staticMetaObject.className();
62 }
63 
64 template<class T>
65 Q_OUTOFLINE_TEMPLATE TableSet<T>::TableSet(Table *parent) : TableSetBase(parent)
66 {
67  _childClassName = T::staticMetaObject.className();
68 }
69 
70 template<class T>
71 Q_OUTOFLINE_TEMPLATE Query<T> *TableSet<T>::createQuery()
72 {
73  Query<T> *q = new Query<T>(_database, this);
74 
75  return q;
76 }
77 
78 template<class T>
79 Q_OUTOFLINE_TEMPLATE int TableSet<T>::length() const
80 {
81  return _tables.count();
82 }
83 
84 template<class T>
85 Q_OUTOFLINE_TEMPLATE T *TableSet<T>::at(int i) const
86 {
87  return (T*)_tablesList.at(i);
88 }
89 
90 template<class T>
91 Q_OUTOFLINE_TEMPLATE const T &TableSet<T>::operator[](int i) const
92 {
93  return _tablesList[i];
94 }
95 
96 template<class T>
97 Q_OUTOFLINE_TEMPLATE void TableSet<T>::append(T *t)
98 {
99  _tables.insert(t);
100  _tablesList.append(t);
101 // rows.append(t);
102  t->setTableSet(this);
103  if(t->status() != Table::FeatchedFromDB)
104  t->setStatus(Table::Added);
105 }
106 
107 template<class T>
108 Q_OUTOFLINE_TEMPLATE void TableSet<T>::append(QList<T *> t)
109 {
110  foreach (T* i, t)
111  append(i);
112 }
113 
114 template<class T>
115 Q_OUTOFLINE_TEMPLATE void TableSet<T>::remove(T *t)
116 {
117  _tables.remove(t);
118  t->setStatus(Table::Deleted);
119 }
120 
121 template<class T>
122 Q_OUTOFLINE_TEMPLATE void TableSet<T>::remove(QList<T *> t)
123 {
124  foreach (T* i, t)
125  remove(i);
126 }
127 
128 QT_END_NAMESPACE
129 
130 #endif // TABLESET_H