Nut  0.1
tablemodel.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 TABLESCHEEMA_H
22 #define TABLESCHEEMA_H
23 
24 #include <QtCore/QVariant>
25 #include <QDebug>
26 class QJsonObject;
27 class TableModel;
28 
29 struct FieldModel{
30  FieldModel() : name(QString::null), length(0), defaultValue(QString::null),
31  notNull(false), isPrimaryKey(false), isAutoIncrement(false), isUnique(false)
32  {
33 
34  }
35 
36  QString name;
37  QVariant::Type type;
38  int length;
39  QString defaultValue;
40  bool notNull;
41  bool isPrimaryKey;
42  bool isAutoIncrement;
43  bool isUnique;
44 
45  bool operator ==(const FieldModel &f) const{
46 
47  bool b = name == f.name
48  && type == f.type
49  && length == f.length
50  && defaultValue == f.defaultValue
51  && notNull == f.notNull;
52 
53  return b;
54  }
55 
56  bool operator !=(const FieldModel &f) const{
57  return !(*this == f);
58  }
59 };
60 
62  QString className;
63  QString localColumn;
64  TableModel *table;
65  QString foregionColumn;
66 };
68 {
69 public:
70 
71  TableModel(int typeId, QString tableName);
72  TableModel(QJsonObject json, QString tableName);
73 
74  QJsonObject toJson() const;
75 
76 // static TableScheema *registerTable(int typeId, QString tableName);
77 // static void createForegionKeys();
78  static TableModel* model(QString className);
79 
80  FieldModel *field(QString name) const;
81  RelationModel *foregionKey(QString otherTable) const;
82 
83  QString toString() const;
84 
85  QString primaryKey() const;
86 
87  QString name() const;
88  void setName(const QString &name);
89 
90  QString className() const;
91  void setClassName(const QString &className);
92 
93  int typeId() const;
94  void setTypeId(const int &typeId);
95  QList<FieldModel *> fields() const;
96  QList<RelationModel *> foregionKeys() const;
97  QStringList fieldsNames() const;
98 
99  static TableModel *findByTypeId(int typeId);
100  static TableModel *findByName(QString name);
101  static TableModel *findByClassName(QString className);
102 
103  bool operator ==(const TableModel &t) const;
104  bool operator !=(const TableModel &t) const;
105 
106 private:
107  QString _name;
108  QString _className;
109  int _typeId;
110  QList<FieldModel*> _fields;
111  QList<RelationModel*> _foregionKeys;
112  static QSet<TableModel*>_allModels;
113 };
114 
115 #endif // TABLESCHEEMA_H