Parkmanne/Query.cpp

174 lines
5.7 KiB
C++
Raw Permalink Normal View History

2019-07-02 00:51:23 +00:00
#include "headers/Query.h"
vector<Park_time> query_parktimes_for_customer(int cid) {
/*
This is needed to initialize the park_instances for the customer constructor
that is supposed to create a customer from data in the db.
This should not be called on on it's own outside query_customer();
*/
vector<Park_time> park_times;
2019-07-08 20:57:09 +00:00
SQLite::Statement query(data::db, "SELECT * FROM Park_time WHERE customer_id = ?;");
2019-07-02 00:51:23 +00:00
query.bind(1, cid);
while (query.executeStep()) {
int id = query.getColumn(0);
int spot_id = query.getColumn(2);
int start = query.getColumn(3);
int duration = query.getColumn(5);
Park_time result{id, cid, spot_id, start, duration};
park_times.push_back(result);
}
query.reset();
return park_times;
}
//--------------------------------------------- customers
vector<Customer> query_customer_with_name(string name) {
/*
We use this instead of plain customers because:
1. no error handling needed here if there are no customers
2. multiple customers could be returned with the same name.
*/
vector<Customer> result;
2019-07-08 20:57:09 +00:00
SQLite::Statement query(data::db,
"SELECT id, name, password, vehicle FROM Customer WHERE name = ?;");
2019-07-02 00:51:23 +00:00
query.bind(1, name);
while (query.executeStep()) {
int id = query.getColumn(0);
string name_ = query.getColumn(1);
string password = query.getColumn(2);
2019-07-06 14:52:01 +00:00
int vehicle = query.getColumn(3); // cast to vehicle
2019-07-08 20:57:09 +00:00
string telephone = query.getColumn(4);
2019-07-02 00:51:23 +00:00
vector<Park_time> park_instances = query_parktimes_for_customer(id);
2019-07-08 20:57:09 +00:00
result.push_back(
Customer{id, name_, password, Vehicle_type(vehicle), park_instances, telephone});
2019-07-02 00:51:23 +00:00
}
return result;
}
Customer query_customer_with_id(int id) {
/* do not call this function if you are not certain a customer with this id
exists.
// the only legitimate caller of this function is query_parkspot_x
// there is no error handling in this function
// for when this function doesn't find the customer with this id !!!!
*/
SQLite::Statement query(data::db, "SELECT * FROM Customer WHERE id = ?;");
query.bind(1, id);
while (query.executeStep()) {
string name = query.getColumn(1);
string password = query.getColumn(2);
2019-07-06 14:52:01 +00:00
int vehicle = query.getColumn(3); // cast to vehicle
2019-07-08 20:57:09 +00:00
string telephone = query.getColumn(4);
2019-07-02 00:51:23 +00:00
vector<Park_time> park_instances = query_parktimes_for_customer(id);
2019-07-08 20:57:09 +00:00
Customer result{id, name, password, Vehicle_type(vehicle), park_instances, telephone};
2019-07-02 00:51:23 +00:00
return result;
}
}
int query_role_customer(int id) {
SQLite::Statement query(data::db, "SELECT * FROM Customer WHERE id = ?;");
query.bind(1, id);
while (query.executeStep()) {
int role = query.getColumn(5);
return role;
}
}
2019-07-07 18:15:51 +00:00
//------------------------------- parkspot info
2019-07-06 16:32:00 +00:00
2019-07-20 13:33:19 +00:00
// -- parkspots info, report gen
2019-07-07 18:15:51 +00:00
Park_spot query_parkspot_with_id(int id, vector<Park_spot>& parkspots) {
for (Park_spot& i : parkspots) {
if (i.id == id) {
2019-07-06 16:32:00 +00:00
return i;
}
}
}
2019-07-20 23:32:47 +00:00
void reports_from_parkspot(int spotid, pair<int, int> period) {
2019-07-07 18:15:51 +00:00
vector<Park_time> park_times;
SQLite::Statement query(data::db,
"SELECT * FROM Park_time WHERE spot_id = ? AND start > ? AND end < ?;");
2019-07-07 18:15:51 +00:00
query.bind(1, spotid);
2019-07-20 23:32:47 +00:00
query.bind(2, period.first);
query.bind(3, period.second);
2019-07-07 18:15:51 +00:00
while (query.executeStep()) {
int id = query.getColumn(0);
int cid = query.getColumn(1);
int start = query.getColumn(3);
int duration = query.getColumn(5);
Park_time result{id, cid, spotid, start, duration};
park_times.push_back(result);
}
query.reset();
for (auto i : park_times) {
cout << i;
}
}
2019-07-02 00:51:23 +00:00
2019-07-20 23:32:47 +00:00
void reports_from_allparkspots(pair<int, int> period) {
2019-07-07 18:15:51 +00:00
vector<Park_time> park_times;
SQLite::Statement query(data::db, "SELECT * FROM Park_time WHERE start > ? AND end < ?;");
2019-07-20 23:32:47 +00:00
query.bind(1, period.first);
query.bind(2, period.second);
2019-07-07 18:15:51 +00:00
while (query.executeStep()) {
int id = query.getColumn(0);
int cid = query.getColumn(1);
int spotid = query.getColumn(2);
int start = query.getColumn(3);
int duration = query.getColumn(5);
Park_time result{id, cid, spotid, start, duration};
park_times.push_back(result);
}
query.reset();
for (auto i : park_times) {
cout << i;
}
}
2019-07-08 20:57:09 +00:00
void current_status_parkspots(vector<Park_spot>& spots) {
2019-07-22 00:25:51 +00:00
cout << "P.spot \t\tStatus\t\t Customer\n";
2019-07-08 20:57:09 +00:00
for (auto& i : spots) {
2019-07-22 02:04:26 +00:00
cout << "\n" << i.id << "\t\t" << ((i.taken) ? "true" : "false");
2019-07-08 20:57:09 +00:00
if (i.taken) {
2019-07-22 00:25:51 +00:00
cout << "\t\t" << i.parked_customer;
2019-07-07 18:15:51 +00:00
}
}
2019-07-22 00:25:51 +00:00
cout << "\n";
2019-07-07 18:15:51 +00:00
}
vector<Park_time> reports_from_customer(int cid, pair<int, int> period) {
vector<Park_time> park_times;
2019-07-22 02:04:26 +00:00
int verhicle = int(query_customer_with_id(cid).vehicle);
float sum = 0;
SQLite::Statement query(
2019-07-22 02:04:26 +00:00
data::db, "SELECT * FROM Park_time WHERE customer_id = ? AND start > ? AND end < ?;");
query.bind(1, cid);
query.bind(2, period.first);
query.bind(3, period.second);
while (query.executeStep()) {
2019-07-22 02:04:26 +00:00
int id = query.getColumn(0);
int spotid = query.getColumn(2);
int start = query.getColumn(3);
2019-07-22 02:04:26 +00:00
int duration = query.getColumn(5);
Park_time result{id, cid, spotid, start, duration};
park_times.push_back(result);
2019-07-22 03:34:26 +00:00
sum += duration/3600;
}
query.reset();
for (auto i : park_times) {
2019-07-22 12:39:56 +00:00
cout << std::setprecision(2) << i;
2019-07-22 03:34:26 +00:00
sum += i.duration / 3600.0;
}
2019-07-22 12:39:56 +00:00
cout << "Your fees for this month: $" << std::setprecision(4) << sum * verhicle << "\n";
return park_times;
}