This repository was 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
Expand file tree
/
Copy pathdbcolumn.h
More file actions
241 lines (200 loc) · 5.69 KB
/
dbcolumn.h
File metadata and controls
241 lines (200 loc) · 5.69 KB
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/**
* @file dbcolumn.h
* @brief Declarations for DbColumn 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_DBCOLUMN_H_INCLUDE
#define GUARD_DBCOLUMN_H_INCLUDE
#include <dbstruct/dbstruct-config.h>
#include <dbstruct/dbobject.h>
#include <QString>
#include <QVariant>
QT_BEGIN_NAMESPACE
class QSqlTableModel;
class QSqlRecord;
QT_END_NAMESPACE
class DbTaew;
//! The column of a database.
class DBSTRUCT_EXPORT DbColumn : public DbObject {
public:
enum ForeignBehavior {
FB_CHOOSE, /**< the user can choose values from the other table */
FB_CHOOSE_ADD /**< the user can choose values from or add values to the other table */
};
enum DataType {
DTY_INVALID = -1, /**< invalid data type */
DTY_BIGINT,
DTY_BINARY,
DTY_BIT,
DTY_TRISTATE,
DTY_CHAR,
DTY_CHOICE,
DTY_DATE,
DTY_DATETIME,
DTY_DATETIME2,
DTY_DATETIMEOFFSET,
DTY_DECIMAL,
DTY_DECIMALSCALE,
DTY_FLOAT,
DTY_HIERARCHYID,
DTY_IMAGE,
DTY_INTEGER,
DTY_MONEY,
DTY_NCHAR,
DTY_NTEXT,
DTY_NUMERIC,
DTY_NUMERICSCALE,
DTY_NVARCHAR,
DTY_REAL,
DTY_ROWVERSION,
DTY_SMALLDATETIME,
DTY_SMALLINT,
DTY_SMALLMONEY,
DTY_SQL,
DTY_TEXT,
DTY_TIME,
DTY_TINYINT,
DTY_UNIQUEIDENTIFIER,
DTY_VARBINARY,
DTY_VARCHAR,
DTY_XML,
DTY_CALLBACK, /**< dynamically computed value or retrieved from database */
DTY_MAX /**< first invalid value */
};
enum BoolFormat {
BF_Y_UPPER,
BF_T_UPPER,
BF_YES_CAMEL,
BF_YES_LOWER,
BF_YES_UPPER,
BF_ON_CAMEL,
BF_ON_LOWER,
BF_ON_UPPER,
BF_TRUE_CAMEL,
BF_TRUE_LOWER,
BF_TRUE_UPPER,
BF_STRING_ON
};
//! Callback used to retrieve the value for a DTY_CALLBACK column.
typedef QVariant (*Callback) (
const DbTaew & table,
const DbColumn & colorig,
const QSqlRecord & rec,
int role,
void * user_data);
union ColFormat {
// cppcheck-suppress unusedStructMember
BoolFormat bit_; /**< for `DTY_BIT` data-type it is one of BoolFormat */
// cppcheck-suppress unusedStructMember
int width_; /**< field width if applicable */
Callback callback_; /**< for `DTY_CALLBACK` data-type it a callback */
};
QString col_name_;
int col_id_;
int real_col_id_;
int length_;
QString col_label_;
DataType datatype_;
bool nulls_;
bool autoincrement_;
QString default_value_;
bool read_only_;
int virtrefcol_;
QString foreign_table_; /**< The table that this column references */
QString foreign_key_; /**< Name of the column in the referenced table */
QString foreign_ref_; /**< The columns that should replace this column */
ForeignBehavior foreign_behavior_; /**< how are we going to interact with foreign table */
QString original_format_; /**< actual format passed to the constructor */
// following values are extracted from original_format_ in constructor
ColFormat format_; /**< column format (depends on data-type */
QChar fill_char_; /**< character used for padding, if any */
char nr_format_; /**< number format (e, E, f, g, G) */
int precision_; /**< number of significant digits for real numbers, base for integers */
//! Default constructor.
DbColumn ();
//! Default constructor.
DbColumn (
const QString & col_name,
int col_id,
int real_col_id,
int length,
const QString & col_label,
DataType datatype,
bool nulls,
bool autoincrement,
const QString & default_value,
const QString & format,
bool read_only,
int virtrefcol,
const QString & foreign_table,
const QString & foreign_key,
const QString & foreign_ref,
ForeignBehavior foreign_behavior) ;
//! Destructor.
virtual ~DbColumn();
//! The type of this object.
virtual Type
type () const {
return DBO_COLUMN;
}
//! Tell if this column has a foreign key.
inline bool
isForeignKey () const {
return !foreign_table_.isEmpty();
}
//! Tell if this column is a virtual one.
inline bool
isVirtual () const {
return virtrefcol_ != -1;
}
//! Tell if this column is a virtual one.
inline bool
isDynamic () const {
return datatype_ == DTY_CALLBACK;
}
//! Real column id.
inline int
columnRealId () const {
return real_col_id_;
}
//! Column id.
inline int
columnId () const {
return col_id_;
}
//!
inline const QString &
columnLabel () const {
return col_label_;
}
//!
inline const QString &
columnName () const {
return col_name_;
}
//!
inline bool
readOnly () const {
return read_only_;
}
//!
inline DataType
columnType () const {
return datatype_;
}
//! Retrieve the data using the callback.
QVariant kbData (
const DbTaew & table,
const QSqlRecord & rec,
int role = Qt::DisplayRole,
void * user_data = NULL) const;
QVariant
formattedData (
const QVariant &original_value) const;
protected:
private:
};
#endif // GUARD_DBCOLUMN_H_INCLUDE