-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqlquerymodel.cpp
executable file
·341 lines (302 loc) · 9.85 KB
/
sqlquerymodel.cpp
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing/
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** You may use this file under the terms of the BSD license as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of The Qt Company Ltd nor the names of its
** contributors may be used to endorse or promote products derived
** from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QtSql>
#include "sqlquerymodel.h"
SqlQueryModel::SqlQueryModel(QObject *parent)
: QSqlQueryModel(parent)
{
}
Qt::ItemFlags SqlQueryModel::flags(const QModelIndex &index) const
{
Qt::ItemFlags flags = QSqlQueryModel::flags(index);
if (index.column() > 0 && index.column() < 11)
flags |= Qt::ItemIsEditable;
return flags;
}
QVariant SqlQueryModel::data(const QModelIndex &index, int role) const
{
QVariant value = QSqlQueryModel::data(index, role);
if (value.isValid() && role == Qt::DisplayRole) {
//is_valid
if (index.column() == 12) {
if(value.toBool()) {
return QVariant("VALIDO");
} else {
return QVariant("SCADUTO");
}
}
//date
if (index.column() == 13) {
if(value.toInt() != 0) {
return QVariant(QDate::fromJulianDay(value.toInt()).toString("d/M/yyyy"));
} else {
return QVariant("");
}
}
//last_notification
if (index.column() == 14) {
if(value.toInt() != -1) {
return QVariant(QDate::fromJulianDay(value.toInt()));
} else {
return QVariant("MAI");
}
}
}
return value;
}
bool SqlQueryModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (index.column() < 1 || index.column() > 10) {
return false;
}
QModelIndex primaryKeyIndex = QSqlQueryModel::index(index.row(), 0);
int id = data(primaryKeyIndex, role).toInt();
bool ok;
//COGNOME
if (index.column() == 1) {
ok = setLastName(id, value.toString().trimmed().toUpper());
}
//NOME
if (index.column() == 2) {
ok = setFirstName(id, value.toString().trimmed().toUpper());
}
//INDIRIZZO
if (index.column() == 3) {
ok = setStreet(id, value.toString().trimmed().toUpper());
}
//CAP
if (index.column() == 4) {
ok = setZip(id, value.toString().trimmed().toUpper());
}
//COMUNE
if (index.column() == 5) {
ok = setMunicipality(id, value.toString().trimmed().toUpper());
}
//PROVINCIA
if (index.column() == 6) {
ok = setProvince(id, value.toString().trimmed().toUpper());
}
//CODICE FISCALE
if (index.column() == 7) {
ok = setPersonalCode(id, value.toString().trimmed().toUpper());
}
//PROFESSIONE
if (index.column() == 8) {
ok = setJob(id, value.toString().trimmed().toUpper());
}
//EMAIL
if (index.column() == 9) {
ok = setEmail(id, value.toString().trimmed().toLower());
}
//TELEFONO
if (index.column() == 10) {
ok = setPhone(id, value.toString().trimmed());
}
return ok;
}
bool SqlQueryModel::setLastName(int personId, const QString &lastName)
{
Validator v;
if(v.LastNameValidator(lastName)) {
QSqlQuery query;
query.prepare("UPDATE people SET last_name = :last_name WHERE id = :id");
query.bindValue(":last_name", QVariant(lastName));
query.bindValue(":id", QVariant(personId));
if(query.exec()) {
emit lastNameChanged(personId, lastName);
return true;
} else {
return false;
}
}
return false;
}
bool SqlQueryModel::setFirstName(int personId, const QString &firstName)
{
Validator v;
if(v.FirstNameValidator(firstName)) {
QSqlQuery query;
query.prepare("UPDATE people SET first_name = :first_name WHERE id = :id");
query.bindValue(":first_name", QVariant(firstName));
query.bindValue(":id", QVariant(personId));
if(query.exec()) {
emit firstNameChanged(personId, firstName);
return true;
} else {
return false;
} }
return false;
}
bool SqlQueryModel::setStreet(int personId, const QString &street)
{
Validator v;
if(v.ViaValidator(street)) {
QSqlQuery query;
query.prepare("UPDATE address SET street_name = :street WHERE person = :id");
query.bindValue(":street", QVariant(street));
query.bindValue(":id", QVariant(personId));
if(query.exec()) {
emit streetChanged(personId, street);
return true;
} else {
return false;
}
}
return false;
}
bool SqlQueryModel::setZip(int personId, const QString &zip)
{
Validator v;
if(v.CapValidator(zip)) {
QSqlQuery query;
query.prepare("UPDATE address SET zip = :zip WHERE person = :id");
query.bindValue(":zip", QVariant(zip));
query.bindValue(":id", QVariant(personId));
if(query.exec()) {
emit zipChanged(personId, zip);
return true;
} else {
return false;
}
}
return false;
}
bool SqlQueryModel::setMunicipality(int personId, const QString &municipality)
{
Validator v;
if(v.ComuneValidator(municipality)) {
QSqlQuery query;
query.prepare("UPDATE address SET municipality = :municipality WHERE person = :id");
query.bindValue(":municipality", QVariant(municipality));
query.bindValue(":id", QVariant(personId));
if(query.exec()) {
emit municipalityChanged(personId, municipality);
return true;
} else {
return false;
}
}
return false;
}
bool SqlQueryModel::setProvince(int personId, const QString &province)
{
Validator v;
if(v.ProvinciaValidator(province)) {
QSqlQuery query;
query.prepare("UPDATE address SET province = :province WHERE person = :id");
query.bindValue(":province", QVariant(province));
query.bindValue(":id", QVariant(personId));
if(query.exec()) {
emit provinceChanged(personId, province);
return true;
} else {
return false;
}
}
return false;
}
bool SqlQueryModel::setPersonalCode(int personId, const QString &personal_code)
{
Validator v;
if(v.CodiceFiscaleValidator(personal_code)) {
QSqlQuery query;
query.prepare("UPDATE people SET personal_code = :personal_code WHERE id = :id");
query.bindValue(":personal_code", QVariant(personal_code));
query.bindValue(":id", QVariant(personId));
if(query.exec()) {
emit personalCodeChanged(personId, personal_code);
return true;
} else {
return false;
}
}
return false;
}
bool SqlQueryModel::setJob(int personId, const QString &job)
{
Validator v;
if(v.ProfessioneValidator(job)) {
QSqlQuery query;
query.prepare("UPDATE people SET job = :job WHERE id = :id");
query.bindValue(":job", QVariant(job));
query.bindValue(":id", QVariant(personId));
if(query.exec()) {
emit jobChanged(personId, job);
return true;
} else {
return false;
}
}
return false;
}
bool SqlQueryModel::setEmail(int personId, const QString &email)
{
Validator v;
if(v.EmailValidator(email)) {
QSqlQuery query;
query.prepare("UPDATE people SET email = :email WHERE id = :id");
query.bindValue(":email", QVariant(email));
query.bindValue(":id", QVariant(personId));
if(query.exec()) {
emit emailChanged(personId, email);
return true;
} else {
return false;
}
}
return false;
}
bool SqlQueryModel::setPhone(int personId, const QString &phone)
{
Validator v;
if(v.TelefonoValidator(phone)) {
QSqlQuery query;
query.prepare("UPDATE people SET phone = :phone WHERE id = :id");
query.bindValue(":phone", QVariant(phone));
query.bindValue(":id", QVariant(personId));
if(query.exec()) {
emit phoneChanged(personId, phone);
return true;
} else {
return false;
}
}
return false;
}