-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.cpp
192 lines (138 loc) · 5.12 KB
/
db.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
#include "db.h"
DB::DB()
{
}
//============================================
DB::~DB()
{
delete querly;
delete model;
}
//==============================================
bool DB::GreateDb()
{
bool fl{false};
QDir databasePath;
QString path = databasePath.currentPath()+"test.db";
db = QSqlDatabase::addDatabase("QSQLITE");
if(!QFile(path).exists()){
db.setDatabaseName(path);
if (db.open()) {
querly = new QSqlQuery(db);
fl = true;
querly->exec("CREATE TABLE product ""(id integer primary key, ""name varchar(30), ""col integer,"
"""date varchar(10), ""price double)");
querly->exec("CREATE TABLE productsales ""(id integer, ""name varchar(30), ""col integer,"
"""date varchar(10), ""price double, ""pricesales double )");
}
}
else {
db.setDatabaseName(path);
if (db.open()) {fl = true; querly = new QSqlQuery(db);}
}
return fl;
}
//==========================================================================
QSqlQueryModel* DB::GetProduct()
{
model = new QSqlQueryModel();
model->setQuery("SELECT * FROM product");
model->setHeaderData(0,Qt::Horizontal,"Номер позиции");
model->setHeaderData(1,Qt::Horizontal,"Наименование товара");
model->setHeaderData(2,Qt::Horizontal,"В наличии");
model->setHeaderData(3,Qt::Horizontal,"Дата производства");
model->setHeaderData(4,Qt::Horizontal,"Цена на реализацию за ед. товара");
return model;
}
//=========================================================================
void DB::addProduct(QVector<QString> const &v)
{
querly->prepare("INSERT INTO product(name, col, date, price) VALUES(:name, :col, :date, :price)");
auto it = v.begin();
querly->bindValue(":name",*it);
std::advance(it,1);
querly->bindValue(":col",it->toInt());
std::advance(it,1);
querly->bindValue(":date",*it);
std::advance(it,1);
querly->bindValue(":price",it->toDouble());
querly->exec();
}
//=====================================================
bool DB::delProduct(int const & id)
{
querly->prepare("DELETE FROM product WHERE id=?");
querly->bindValue(0,id);
return querly->exec();
}
//===========================================================
QVector<QString>& DB::getPoroductInfo(int const & id)
{
querly->prepare("SELECT name, col, date, price FROM product WHERE id=?");
querly->bindValue(0,id);
vec.clear();
if(querly->exec()){
querly->next();
vec.push_back(querly->value(0).toString());
vec.push_back(querly->value(1).toString());
vec.push_back(querly->value(2).toString());
vec.push_back(querly->value(3).toString());
vec.push_back(QString::number(id));
}
return vec;
}
//=======================================================================
bool DB::updateProduct(QVector<QString> const & vec)
{
querly->prepare("UPDATE product SET name = ?, col = ?, date = ?, price = ? WHERE id = ?");
auto it = vec.begin();
querly->bindValue(0,*it);
std::advance(it,1);
querly->bindValue(1,it->toInt());
std::advance(it,1);
querly->bindValue(2,*it);
std::advance(it,1);
querly->bindValue(3,it->toDouble());
std::advance(it,1);
querly->bindValue(4,it->toInt());
return querly->exec();;
}
//===========================================================================
void DB::addProductSales(QVector<QString> const & vec)
{
querly->prepare("INSERT INTO productsales(name, col, date, price, pricesales, id)"
" VALUES(:name, :col, :date, :price, :pricesales, :id)");
auto it = vec.begin();
querly->bindValue(":name",*it);
std::advance(it,1);
querly->bindValue(":col",it->toInt());
std::advance(it,1);
querly->bindValue(":date",*it);
std::advance(it,1);
querly->bindValue(":price",it->toDouble());
std::advance(it,1);
querly->bindValue(":pricesales",it->toDouble());
std::advance(it,1);
querly->bindValue(":id",it->toInt());
querly->exec();
}
//==========================================================================================
QSqlTableModel* DB::GetProductSales()
{
tmodel = new QSqlTableModel();
tmodel->setTable("productsales");
tmodel->select();
tmodel->setHeaderData(0,Qt::Horizontal,"Номер позиции");
tmodel->setHeaderData(1,Qt::Horizontal,"Наименование товара");
tmodel->setHeaderData(2,Qt::Horizontal,"Кол. реализованного товара");
tmodel->setHeaderData(3,Qt::Horizontal,"Дата реализации");
tmodel->setHeaderData(4,Qt::Horizontal,"Цена на реализацию за ед. товара");
tmodel->setHeaderData(5,Qt::Horizontal,"Сумма за реализацию");
if(querly->exec("SELECT * FROM productsales")){
while (querly->next()) {
total += querly->value(5).toDouble();
}
}
return tmodel;
}
double total{};