-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdbdriver.cpp
More file actions
executable file
·216 lines (209 loc) · 8.08 KB
/
Copy pathdbdriver.cpp
File metadata and controls
executable file
·216 lines (209 loc) · 8.08 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
#include "dbdriver.h"
dbDriver::dbDriver(QObject *parent) :
QObject(parent)
{
dbase = QSqlDatabase::addDatabase("QSQLITE");
status = false;
numberOfInserts = 0;
multipleQuery = "INSERT OR REPLACE INTO main ( art, desc, price, vendor_id) "
"SELECT '%1', '%2', '%3', '%4' ";
}
void dbDriver::init(const QString &path)
{
if(! QFile::exists(path)){
emit SIGNALdbMessage(QString::fromUtf8("Была создана новая база данных"));
}else{
emit SIGNALdbMessage(QString::fromUtf8("База данных была перезаписана"));
/*
QMessageBox msgBox;
msgBox.setWindowTitle(QString::fromLocal8Bit("Перезапись БД"));
msgBox.setText(QString::fromLocal8Bit("Перезаписать БД?"));
msgBox.setIcon(QMessageBox::Information);
msgBox.setInformativeText(QString::fromLocal8Bit("Обнаружена ранее созданая база данных перезаписать её для нового голосования?"));
msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
msgBox.setDefaultButton(QMessageBox::Cancel);
int ret = msgBox.exec();
switch (ret) {
case QMessageBox::Cancel:
break;
case QMessageBox::Ok:
QFile(path).remove();
emit SIGNALdbMessage(QString::fromLocal8Bit("База данных была перезаписана"));
break;
default:
break;
}
*/
}
dbase.setDatabaseName(path);
bool globalError = false;
if(!dbase.open()){
emit SIGNALdbError(QString::fromUtf8("Ощибка при открытии базы данных ") + dbase.lastError().text());
globalError = true;
}
QSqlQuery a_query;
QString str;
bool flag;
str = "CREATE TABLE IF NOT EXISTS vendors (id INTEGER PRIMARY KEY NOT NULL, name TEXT);";
flag = a_query.exec(str);
if(!flag){
emit SIGNALdbError(QString::fromUtf8("Ощибка при создании таблицы vendors ") + a_query.lastError().text());
globalError = true;
}
if(!a_query.exec("PRAGMA foreign_keys = ON;")){
emit SIGNALdbError(QString::fromUtf8("Ощибка при установке связных ключей ") + a_query.lastError().text());
globalError = true;
}
str = "CREATE TABLE IF NOT EXISTS main ("
"art TEXT NOT NULL, "
"desc TEXT, "
"price REAL NOT NULL, "
"vendor_id INTEGER NOT NULL, "
"PRIMARY KEY(art, vendor_id),"
"FOREIGN KEY(vendor_id) REFERENCES vendors(id)"
");";
flag = a_query.exec(str);
emit SIGNALdbError(QString::fromUtf8("Ощибка при создании таблицы main ") + a_query.lastError().text());
if(!flag){
emit SIGNALdbError(QString::fromUtf8("Ощибка при создании таблицы main ") + a_query.lastError().text());
globalError = true;
}
str = "CREATE INDEX main_id_index on main(id)";
if(!a_query.exec(str)){
emit SIGNALdbError(QString::fromUtf8("Ощибка при создании индекса ") + a_query.lastError().text());
globalError = true;
}
dbModel = new QSqlRelationalTableModel;
dbModel->setTable("main");
dbModel->setEditStrategy(QSqlTableModel::OnRowChange);
dbModel->setRelation(4, QSqlRelation("vendors", "id", "name"));
dbModel->select();
if(globalError){
emit SIGNALdbError(QString::fromUtf8("Ощибка базы данных"));
status = false;
}else{
status = true;
}
addQuery = new QSqlQuery;
//addQuery->prepare("INSERT OR REPLACE INTO main(id, art, desc, price, vendor_id) VALUES(NULL, ?, ?, ?, ?);");
}
void dbDriver::SLOTaddItem(const QString &art, const QString &desc, const float price)
{
/*
QString str;
QSqlQuery a_query;
str = "INSERT OR REPLACE INTO main(art, desc, price, vendor_id) VALUES('%1', '%2', '%3', '%4');";
str = str.arg(art).arg(desc).arg(price).arg(vendorId);
a_query.exec(str);
str = QString::fromUtf8("Добавлен артикль %1 - %2");
str = str.arg(art).arg(desc);
emit SIGNALdbMessage(str);
*/
/*
arts << art;
descs << desc;
prices << price;
vendorIds << vendorId;
QString str;
str = QString::fromUtf8("Добавлен артикль %1 - %2");
str = str.arg(art).arg(desc);
*/
qDebug() << price;
QString str;
str = QString::fromUtf8("Добавлен артикль %1 - %2");
str = str.arg(art).arg(desc);
emit SIGNALdbMessage(str);
if(numberOfInserts < 400){
if(numberOfInserts == 0){
multipleQuery = multipleQuery.arg(art).arg(desc).arg(price).arg(vendorId);
numberOfInserts++;
}else{
str = "UNION ALL SELECT '%1', '%2', '%3', '%4' ";
str = str.arg(art).arg(desc).arg(price).arg(vendorId);
multipleQuery += str;
numberOfInserts++;
}
}else{
QSqlQuery a_query;
str = "UNION ALL SELECT '%1', '%2', '%3', '%4'";
str = str.arg(art).arg(desc).arg(price).arg(vendorId);
multipleQuery += str;
if(!a_query.exec(multipleQuery)){
emit SIGNALdbError(QString::fromUtf8("Ощибка при добавлении данных ") + a_query.lastError().text());
}
multipleQuery = "INSERT OR REPLACE INTO main( art, desc, price, vendor_id) "
"SELECT '%1', '%2', '%3', '%4' ";
numberOfInserts = 0;
}
}
void dbDriver::SLOTsetVendor(const QString &vendorName)
{
QSqlQuery a_query;
QString str;
str = "SELECT id FROM vendors WHERE name = '%1';";
str = str.arg(vendorName);
a_query.exec(str);
QSqlRecord rec = a_query.record();
if(!a_query.next()) {
str = "INSERT INTO vendors(id, name) VALUES (NULL, '%1');";
str = str.arg(vendorName);
a_query.exec(str);
}
str = "SELECT id FROM vendors WHERE name = '%1';";
str = str.arg(vendorName);
a_query.exec(str);
rec = a_query.record();
a_query.next();
vendorId = a_query.value(rec.indexOf("id")).toInt();
}
void dbDriver::SLOTgetItems(const QStringList list)
{
QSqlQuery a_query;
QString str;
QStringList::const_iterator iter = list.begin();
QSqlRecord rec;
bool flag;
for(;iter!=list.end();iter++){
str = "SELECT * FROM main WHERE art = '%1';";
str = str.arg(*iter);
a_query.exec(str);
rec = a_query.record();
flag = false;
while(a_query.next()){
flag = true;
emit SIGNALtoModel(*iter, a_query.value(rec.indexOf("desc")).toString(), a_query.value(rec.indexOf("price")).toFloat(), a_query.value(rec.indexOf("vendor_id")).toInt());
}
if(!flag){
emit SIGNALtoModel(*iter, QString(""), 0, 0);
}
}
}
void dbDriver::SLOTgetVendors()
{
QSqlQuery a_query;
QString str;
str = "SELECT * FROM vendors";
a_query.exec(str);
QSqlRecord rec;
rec = a_query.record();
while(a_query.next()){
emit SIGNALvendor(a_query.value(rec.indexOf("id")).toInt(), a_query.value(rec.indexOf("name")).toString());
}
}
void dbDriver::SLOTfinalAdding()
{
//addQuery->addBindValue(arts);
//addQuery->addBindValue(descs);
//addQuery->addBindValue(prices);
//addQuery->addBindValue(vendorIds);
//if(!addQuery->execBatch()){
// qDebug() << addQuery->lastError();
// }
QSqlQuery a_query;
if(!a_query.exec(multipleQuery)){
emit SIGNALdbError(QString::fromUtf8("Ощибка при добавлении данных ") + a_query.lastError().text());
}
multipleQuery = "INSERT OR REPLACE INTO main( art, desc, price, vendor_id) "
"SELECT '%1', '%2', '%3', '%4' ";
numberOfInserts = 0;
}