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
|
/*
* database.cpp is a part of ProductInventory
* An inventory system designed for makeup and related things
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "productinventory.h"
#include "ui_productinventory.h"
/*!
* connect to database
* \return connect success or fail
*/
bool
ProductInventory::dbConnect()
{
db.setHostName(ui->HostInput->text());
db.setPort(ui->portInput->value());
db.setDatabaseName(ui->databaseInput->text());
db.setUserName(ui->usernameInput->text());
db.setPassword(ui->passwordInput->text());
bool status = db.open();
if (status)
{
isConnected = true;
conStatus = "Connected";
conColor = "darkgreen";
statusMessage("", 0);
ui->connectButton->setText("Disconnect");
return true;
}
qDebug() << "sql connection failed:" << db.lastError().text();
genericMessageBox(db.lastError().text(), "server error");
return false;
}
/*!
* disconnect from database
* \return disconnect success or fail
*/
bool
ProductInventory::dbDisconnect()
{
db.close();
isConnected = false;
conStatus = "Disconnected";
conColor = "red";
statusMessage("", 0);
ui->connectButton->setText("Connect");
return true;
}
bool
ProductInventory::getConnectionStatus()
{
return isConnected;
}
/*!
* runs a generic query on the database and returns a pointer to a new
* QSqlQuery which must be deleted
* \param query the query to run
* \return
*/
QSqlQuery *
ProductInventory::genericQuery(QString query)
{
QSqlQuery *q = new QSqlQuery;
if (!q->exec(query))
{
qDebug() << "query execute failed:" << q->lastError().text();
genericMessageBox(q->lastError().text(), "query error");
delete q;
return nullptr;
}
return q;
}
/*!
* populates the user interface elements with data from the database
*/
void
ProductInventory::populateInterface()
{
// ui->filterCategoryComboBox->clear();
QSqlQuery *query = genericQuery("SELECT * FROM category ORDER BY name;");
if (query == nullptr)
{
qDebug() << "failed to populate interface";
return;
}
else
{
int x = 0;
int y = 0;
while (query->next())
{
if (x >= 3)
{
y++;
x = 0;
}
QString name = query->value(2).toString();
//ui->filterCategoryComboBox->addItem(name);
QCheckBox *chk = new QCheckBox;
chk->setText(query->value(2).toString());
checkboxes.append(chk);
ui->filterCategoryGrid->addWidget(chk, y, x);
x++;
}
delete query;
}
//query = genericQuery("SELECT brand, color, comment FROM product");
query = genericQuery("SELECT categoryid, name FROM category");
if (query == nullptr)
{
qDebug() << "failed to populate interface";
return;
}
else
{
while (query->next())
{
int t = createTable(query->value(1).toString());
if (t == -1)
{
qDebug() << "failed to populate interface";
delete query;
return;
}
QSqlQuery *inner = genericQuery("SELECT brand, color, comment, DATE_FORMAT(dateAdded, "
"\"%d-%m-%Y %h:%i %p\") FROM product WHERE deleted=0 AND categoryid="
+query->value(0).toString()+" ORDER BY brand DESC");
if (inner == nullptr)
{
qDebug() << "failed to populate interface";
delete query;
return;
}
while (inner->next())
{
QList<QString> item;
item.append(inner->value(0).toString());
item.append(inner->value(1).toString());
item.append(inner->value(2).toString());
item.append("images here");
item.append(inner->value(3).toString());
addItemToTable(tables.at(t), &item);
}
if (tables.at(t)->rowCount() == 0)
{
tables.at(t)->deleteLater();
tables.removeAt(t);
labels.at(t)->deleteLater();
labels.removeAt(t);
}
delete inner;
}
}
resizeRows();
delete query;
}
|