This repository has been archived by the owner on Dec 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbstruct.h
214 lines (161 loc) · 4.78 KB
/
dbstruct.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/**
* @file dbstruct.h
* @brief Declarations for DbStruct class
* @author Nicu Tofan <nicu.tofan@gmail.com>
* @copyright Copyright 2015 piles contributors. All rights reserved.
* This file is released under the
* [MIT License](http://opensource.org/licenses/mit-license.html)
*/
#ifndef GUARD_DBSTRUCT_H_INCLUDE
#define GUARD_DBSTRUCT_H_INCLUDE
#include <dbstruct/dbstruct-config.h>
#include <dbstruct/dbobject.h>
#include <dbstruct/dbtable.h>
#include <dbstruct/dbview.h>
#include <QSqlDatabase>
class DbTaew;
//! The structure of a database.
class DBSTRUCT_EXPORT DbStructMeta : public DbObject {
public:
//! Default constructor.
DbStructMeta ();
//! Copy constructor.
DbStructMeta (const DbStructMeta & /*other*/)
{}
//! Destructor.
virtual ~DbStructMeta ();
//! Assignment operator
DbStructMeta& operator= (const DbStructMeta& /*other*/) {
return *this;
}
//! The type of this object.
virtual Type
type () const {
return DBO_STRUCT;
}
//! Create a table or view based on a table or view name.
virtual DbTaew *
taew (
const QString & value) {
return taew (idFromName (value));
}
/* - - - - - - - - - - - - - - - - */
/** @name Interface
* This is the list of method that subclasses must
* implement.
*/
///@{
//! The name of this database.
virtual QString
databaseName () const = 0;
//! Get the name of the table or view based on its identifier.
virtual QString
componentName (
int index) const = 0;
//! Get the id based on a table or view name.
virtual int
idFromName (
const QString & value) const = 0;
//! Create a table or view based on a table or view id.
virtual DbTaew *
taew (
int index) const = 0;
/* -- T A B L E S -- */
//! Convert between component identifier and table identifier.
virtual int
comp2table (
int value) const = 0;
//! Convert between table identifier and component identifier.
virtual int
table2comp (
int value) const = 0;
//! Get the id based on a table name.
virtual int
tableIdFromName (
const QString & value) const = 0;
//! Get the name of the table based on its identifier.
virtual QString
tableName (
int index) const = 0;
/* -- V I E W S -- */
//! Convert between component identifier and view identifier.
virtual int
comp2view (
int value) const = 0;
//! Convert between view identifier and component identifier.
virtual int
view2comp (
int value) const = 0;
//! Get the id based on a view name.
virtual int
viewIdFromName (
const QString & value) const = 0;
//! Get the name of the view based on its identifier.
virtual QString
viewName (
int index) const = 0;
///@}
/* - - - - - - - - - - - - - - - - */
protected:
private:
}; /* class DbStructMeta */
//! The structure of a database.
class DBSTRUCT_EXPORT DbStruct {
QSqlDatabase db_; /**< accesor */
public:
//! Default constructor.
DbStruct() :
db_()
{}
//! Copy constructor.
DbStruct (const DbStruct & other) :
db_(other.db_)
{}
//! Constructor that also initializes the database.
explicit DbStruct (const QSqlDatabase & db) :
db_(db)
{}
//! Destructor.
virtual ~DbStruct () {}
//! assignment operator
DbStruct& operator= (const DbStruct& other) {
db_ = other.db_;
return *this;
}
//! Retrieve the database.
inline QSqlDatabase &
database () {
return db_;
}
//! Retrieve the database.
inline const QSqlDatabase &
database () const {
return db_;
}
//! Set the database.
inline void
setDatabase (const QSqlDatabase & value) {
db_ = value;
}
//! Get metadata instance.
virtual DbStructMeta *
metaDatabase () = 0;
}; /* class DbStructMeta */
//! Default constructors for inheritants of DbStruct
#define META_DATA_BASE_CTORS(__name__) \
__name__() : __name__ ## Meta(), DbStruct() {} \
explicit __name__(const QSqlDatabase & db) : __name__ ## Meta(), DbStruct(db) {}
//! Inheritants of DbStruct must implement a `metaDatabase()` method.
#define META_DATA_BASE_GETTER \
virtual DbStructMeta * \
metaDatabase () { \
return static_cast<DbStructMeta*>(this); \
}
//! A complete implementation for a DbStruct inheritant.
#define META_DATA_BASE(__name__) \
class %(EXPORT)s __name__ : public __name__ ## Meta, public DbStruct { \
public: \
META_DATA_BASE_CTORS(__name__); \
META_DATA_BASE_GETTER; \
};
#endif // GUARD_DBSTRUCT_H_INCLUDE