-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqlite_helper.cpp
More file actions
398 lines (299 loc) · 9.29 KB
/
sqlite_helper.cpp
File metadata and controls
398 lines (299 loc) · 9.29 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
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
#include <iostream>
#include "header.h"
using namespace std;
//Open the connection to the database
sqlite3* openDB() {
sqlite3* db;
if (sqlite3_open("inventory.db", &db) != SQLITE_OK)
{
cout << "Failed to open the database!!\n";
return nullptr;
}
return db;
}
//Creating all tables
void CreateTables(sqlite3* db)
{
const char* productsSql =
"CREATE TABLE IF NOT EXISTS Products("
"Product_id INTEGER PRIMARY KEY AUTOINCREMENT,"
"Name TEXT,"
"Subgroup TEXT,"
"Price REAL);";
const char* Orders =
"CREATE TABLE IF NOT EXISTS Orders ("
"Order_id INTEGER PRIMARY KEY AUTOINCREMENT,"
"Time INTEGER,"
"Customer_name TEXT);";
const char* ordered_items =
"CREATE TABLE IF NOT EXISTS OrderItems("
"Sr INTEGER PRIMARY KEY AUTOINCREMENT,"
"Order_id INT,"
"Product_id INT,"
"Quantity INT,"
"is_ready INT DEFAULT 0);";
char* err = nullptr;
sqlite3_exec(db, productsSql, nullptr, nullptr, &err);
sqlite3_exec(db, Orders, nullptr, nullptr, &err);
sqlite3_exec(db, ordered_items, nullptr, nullptr, &err);
}
//Adding products
void addProduct(sqlite3* db, string name, string subgroup, double price)
{
const char* sql = "INSERT INTO Products (Name, Subgroup, Price) VALUES (?,?,?);";
sqlite3_stmt* stmt;
sqlite3_prepare_v2(db, sql, -1, &stmt, nullptr);
sqlite3_bind_text(stmt, 1, name.c_str(), -1, SQLITE_TRANSIENT);
sqlite3_bind_text(stmt, 2, subgroup.c_str(), -1, SQLITE_TRANSIENT);
sqlite3_bind_double(stmt, 3, price);
if (sqlite3_step(stmt) == SQLITE_DONE)
{
cout << "Product Added!!\n";
}
sqlite3_finalize(stmt);
}
//Updating products
void updateProduct(sqlite3* db, int id, string name, string subgroup, double price)
{
const char* sql =
"UPDATE Products SET Name = ?, Subgroup = ?, Price = ? WHERE Product_id = ?;";
sqlite3_stmt* stmt;
sqlite3_prepare_v2(db, sql, -1, &stmt, nullptr);
sqlite3_bind_text(stmt, 1, name.c_str(), -1, SQLITE_TRANSIENT);
sqlite3_bind_text(stmt, 2, subgroup.c_str(), -1, SQLITE_TRANSIENT);
sqlite3_bind_double(stmt, 3, price);
sqlite3_bind_int(stmt, 4, id);
sqlite3_step(stmt);
sqlite3_finalize(stmt);
cout << "Product Updated!!";
}
//Selecting all products
vector<Product> selectAllProducts(sqlite3* db)
{
vector<Product> list;
const char* sql = "SELECT * FROM Products;";
sqlite3_stmt* stmt;
sqlite3_prepare_v2(db, sql, -1, &stmt, nullptr);
while (sqlite3_step(stmt) == SQLITE_ROW)
{
Product p;
p.id = sqlite3_column_int(stmt, 0);
p.name = (const char*)sqlite3_column_text(stmt, 1);
p.subgroup = (const char*)sqlite3_column_text(stmt, 2);
p.price = sqlite3_column_double(stmt, 3);
list.push_back(p);
}
sqlite3_finalize(stmt);
return list;
}
//Selecting a product by id
Product selectProductById(sqlite3* db, int id)
{
const char* sql = "SELECT * FROM Products WHERE Product_id = ?;";
sqlite3_stmt* stmt;
sqlite3_prepare_v2(db, sql, -1, &stmt, nullptr);
sqlite3_bind_int(stmt, 1, id);
sqlite3_step(stmt);
Product p;
p.id = sqlite3_column_int(stmt, 0);
p.name = (const char*)sqlite3_column_text(stmt, 1);
p.subgroup = (const char*)sqlite3_column_text(stmt, 2);
p.price = sqlite3_column_double(stmt, 3);
sqlite3_finalize(stmt);
return p;
}
//Add orders
int addOrder(sqlite3* db, int timeValue, string name)
{
const char* sql = "INSERT INTO Orders (Time, Customer_name) VALUES (?,?);";
sqlite3_stmt* stmt;
sqlite3_prepare_v2(db, sql, -1, &stmt, nullptr);
sqlite3_bind_int(stmt, 1, timeValue);
sqlite3_bind_text(stmt, 2, name.c_str(), -1, SQLITE_TRANSIENT);
sqlite3_step(stmt);
sqlite3_finalize(stmt);
return sqlite3_last_insert_rowid(db);
}
//Add Products to order
string addProductToOrder(sqlite3* db, int order_id, int product_id, int quantity)
{
const char* select = "SELECT Products.Subgroup FROM Products WHERE Products.Product_id = ?;";
sqlite3_stmt* select_stmt;
sqlite3_prepare_v2(db, select, -1, &select_stmt, nullptr);
sqlite3_bind_int(select_stmt, 1, product_id);
sqlite3_step(select_stmt);
string subgroup = (const char*)sqlite3_column_text(select_stmt, 0);
sqlite3_finalize(select_stmt);
const char* sql = "INSERT INTO OrderItems (Order_id, Product_id, Quantity) VALUES (?,?,?);";
sqlite3_stmt* stmt;
sqlite3_prepare_v2(db, sql, -1, &stmt, nullptr);
sqlite3_bind_int(stmt, 1, order_id);
sqlite3_bind_int(stmt, 2, product_id);
sqlite3_bind_int(stmt, 3, quantity);
sqlite3_step(stmt);
sqlite3_finalize(stmt);
cout << "Product added to order!\n";
return subgroup;
}
//Updating status of order
void updateStatus(sqlite3* db, int sr, int ready_count)
{
const char* sql =
"UPDATE OrderItems SET is_ready = is_ready + ? "
"WHERE Sr = ?;";
sqlite3_stmt* stmt;
sqlite3_prepare_v2(db, sql, -1, &stmt, nullptr);
sqlite3_bind_int(stmt, 1, ready_count);
sqlite3_bind_int(stmt, 2, sr);
sqlite3_step(stmt);
sqlite3_finalize(stmt);
cout << "Product status updated to READY\n";
}
//Function to delete a product from the inventory
void deleteProduct(sqlite3* db, int id)
{
const char* sql = "DELETE FROM Products WHERE Product_id = ?;";
sqlite3_stmt * stmt;
int rc = sqlite3_prepare_v2(db, sql, -1, &stmt, nullptr);
if (rc != SQLITE_OK)
{
cout<<"Prepare Failed!"<<sqlite3_errmsg(db)<<endl;
return;
}
sqlite3_bind_int(stmt, 1, id);
rc = sqlite3_step(stmt);
if (rc == SQLITE_DONE) {
cout << "Product deleted successfully!\n";
}
else {
cerr << "Delete failed: " << sqlite3_errmsg(db) << endl;
}
sqlite3_finalize(stmt);
}
//Function to return the pending orders for each specific subgroup
vector<Product_Count> getOrderedProductsBySubgroup(sqlite3* db, const string& subgroup)
{
vector<Product_Count> result;
const char* sql =
"SELECT Products.Product_id, Products.Name, SUM(OrderItems.Quantity), SUM(OrderItems.is_ready) "
"FROM OrderItems "
"JOIN Products ON OrderItems.Product_id = Products.Product_id "
"WHERE Products.Subgroup = ? AND OrderItems.is_ready < OrderItems.Quantity "
"GROUP BY Products.Product_id, Products.Name;";
sqlite3_stmt* stmt;
int rc = sqlite3_prepare_v2(db, sql, -1, &stmt, nullptr);
if (rc != SQLITE_OK)
{
cout << "Prepare failed: " << sqlite3_errmsg(db) << endl;
return result;
}
sqlite3_bind_text(stmt, 1, subgroup.c_str(), -1, SQLITE_TRANSIENT);
while (sqlite3_step(stmt) == SQLITE_ROW)
{
Product_Count pc;
pc.product_id = sqlite3_column_int(stmt, 0);
const unsigned char* txt = sqlite3_column_text(stmt, 1);
pc.name = txt ? (const char*)txt : "";
pc.total_quantity = sqlite3_column_int(stmt, 2) - sqlite3_column_int(stmt, 3);
result.push_back(pc);
}
sqlite3_finalize(stmt);
return result;
}
//Function to update the oldest order just by name
int markOldestPendingReady(sqlite3* db, const int product_id, const int ready_count)
{
// SQL: find the oldest pending OrderItems row for this product
const char* sql =
"SELECT Sr, Order_id "
"FROM OrderItems "
"WHERE Product_id = ? AND is_ready < quantity "
"ORDER BY Order_id ASC "
"LIMIT 1;";
sqlite3_stmt* stmt;
if (sqlite3_prepare_v2(db, sql, -1, &stmt, nullptr) != SQLITE_OK)
{
cout << "Prepare failed: " << sqlite3_errmsg(db) << endl;
return -1;
}
sqlite3_bind_int(stmt, 1, product_id);
int order_id{-1};
int sr{ -1 };
if (sqlite3_step(stmt) == SQLITE_ROW) {
sr = sqlite3_column_int(stmt, 0);
order_id = sqlite3_column_int(stmt, 1);
}
sqlite3_finalize(stmt);
// calls a predefined function
if (sr == -1)
return -1;
cout << "Proceeding to add " << ready_count << " to product_id: " << product_id
<< " with order_id: " << order_id << endl;
updateStatus(db, sr, ready_count);
return order_id;
}
//Function to display the order info to a customer on his device
Order getOrderById(sqlite3* db, int order_id)
{
Order order;
order.order_id = order_id;
// First, get order info from Orders table
const char* orderSql = "SELECT Customer_name, Time FROM Orders WHERE Order_id = ?;";
sqlite3_stmt* stmt;
if (sqlite3_prepare_v2(db, orderSql, -1, &stmt, nullptr) != SQLITE_OK)
{
cout << "Prepare failed: " << sqlite3_errmsg(db) << endl;
return order;
}
sqlite3_bind_int(stmt, 1, order_id);
if (sqlite3_step(stmt) == SQLITE_ROW)
{
const unsigned char* nameText = sqlite3_column_text(stmt, 0);
order.customer_name = nameText ? (const char*)nameText : "";
order.time = sqlite3_column_int(stmt, 1);
}
else
{
cout << "Order not found.\n";
sqlite3_finalize(stmt);
return order;
}
sqlite3_finalize(stmt);
// Now get all items for this order
const char* itemsSql =
"SELECT Products.Product_id, Products.Name, OrderItems.Quantity, OrderItems.is_ready "
"FROM OrderItems "
"JOIN Products ON OrderItems.Product_id = Products.Product_id "
"WHERE OrderItems.Order_id = ?;";
if (sqlite3_prepare_v2(db, itemsSql, -1, &stmt, nullptr) != SQLITE_OK)
{
cout << "Prepare failed: " << sqlite3_errmsg(db) << endl;
return order;
}
sqlite3_bind_int(stmt, 1, order_id);
while (sqlite3_step(stmt) == SQLITE_ROW)
{
OrderItem item;
item.product_id = sqlite3_column_int(stmt, 0);
const unsigned char* nameText = sqlite3_column_text(stmt, 1);
item.name = nameText ? (const char*)nameText : "";
item.quantity = sqlite3_column_int(stmt, 2);
item.is_ready = sqlite3_column_int(stmt, 3);
order.items.push_back(item);
}
sqlite3_finalize(stmt);
return order;
}
//Function to close the database
void closeDB(sqlite3* db)
{
if (db != nullptr)
{
sqlite3_close(db);
cout << "Database closed successfully.\n";
}
else
{
cout << "Database is already null.\n";
}
}