-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsqlmodel.cpp
executable file
·127 lines (100 loc) · 3.4 KB
/
sqlmodel.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
#include "sqlmodel.h"
sqlModel::sqlModel(QObject *parent, QSqlDatabase &db) : QSqlQueryModel(parent),
db(db),
queryString("SELECT abb1, code1, abb2, code2 FROM red_folder WHERE abb1='DA' AND abb2='MA'")
{
roles = QSqlQueryModel::roleNames();
roles[abb1] = "abb1";
roles[code1] = "code1";
roles[abb2] = "abb2";
roles[code2] = "code2";
}
bool sqlModel::refreshModel()
{
QSqlQuery *qry = new QSqlQuery(db);
qry->prepare(queryString);
if(qry->exec())
{
this->setQuery(*qry);
return true;
}
else
return false;
}
QHash<int, QByteArray> sqlModel::roleNames() const Q_DECL_OVERRIDE {
return roles;
}
QVariant sqlModel::data(const QModelIndex &index, int role) const Q_DECL_OVERRIDE {
if (!index.isValid())
return QVariant();
if(role < Qt::UserRole)
{
return QSqlQueryModel::data(index, role);
}
else
{
QString fieldName;
switch (role) {
case abb1: fieldName = QStringLiteral("abb1"); break;
case code1: fieldName = QStringLiteral("code1"); break;
case abb2: fieldName = QStringLiteral("abb2"); break;
case code2: fieldName = QStringLiteral("code2"); break;
}
if (!this->record().isGenerated(fieldName))
return QVariant();
else {
QModelIndex item = indexInQuery(index);
if ( !this->query().seek(item.row()) )
return QVariant();
return this->query().value(fieldName);
}
}
return QVariant();
}
Qt::ItemFlags sqlModel::flags(const QModelIndex &index) const Q_DECL_OVERRIDE
{
if(!index.isValid())
return Qt::ItemIsEnabled;
return QSqlQueryModel::flags(index) | Qt::ItemIsEditable;
}
bool sqlModel::setData(const QModelIndex &index, const QVariant &value, int role) Q_DECL_OVERRIDE
{
if (index.isValid() && role == Qt::EditRole)
{
int column = index.column();
int row = index.row();
QString field = headerData(column,Qt::Horizontal, Qt::DisplayRole).toString();
qDebug() << field << data(this->index(row,0), Qt::DisplayRole).toString() << data(this->index(row, 2), Qt::DisplayRole).toString();
QSqlQuery *qry = new QSqlQuery(db);
qry->prepare(QString("UPDATE RED_FOLDER SET %1 = :value WHERE abb1 = :abb1 AND abb2 = :abb2 and code1='original'").arg(field));
qry->bindValue(":value", value.toString());
qry->bindValue(":abb1", data(this->index(row,0), Qt::DisplayRole).toString());
qry->bindValue(":abb2", data(this->index(row, 2), Qt::DisplayRole).toString());
if(qry->exec())
{
qDebug() << "updated";
refreshModel();
//emit(dataChanged(index, index));
return true;
}
else
{
return false;
}
}
return false;
}
bool sqlModel::insertRows(int position, int rows, const QModelIndex &index) Q_DECL_OVERRIDE
{
Q_UNUSED(index);
beginInsertRows(QModelIndex(), position, position + rows - 1);
endInsertRows();
return true;
}
bool sqlModel::removeRows(int position, int rows, const QModelIndex &index) Q_DECL_OVERRIDE
{
Q_UNUSED(index);
beginInsertRows(QModelIndex(), position, position + rows - 1);
endInsertRows();
return true;
}