almost all internals done after i fix segfault
This commit is contained in:
70
Query.cpp
70
Query.cpp
@ -1,6 +1,7 @@
|
||||
#include "headers/Query.h"
|
||||
|
||||
vector<Park_time> query_Parktime_for_customer(int cid) {
|
||||
|
||||
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.
|
||||
@ -31,20 +32,67 @@ vector<Customer> query_customer_with_name(string name) {
|
||||
2. multiple customers could be returned with the same name.
|
||||
*/
|
||||
vector<Customer> result;
|
||||
SQLite::Statement query(data::db,
|
||||
"SELECT * FROM Customer WHERE name = '?';");
|
||||
SQLite::Statement query(
|
||||
data::db,
|
||||
"SELECT id, name, password, verhicle FROM Customer WHERE name = ?;");
|
||||
query.bind(1, name);
|
||||
while (query.executeStep()) {
|
||||
// (id integer primary key, name text, card_code varchar(20), verhicle
|
||||
// int) (int id_, string name_, string card_code_, Verhicle_type
|
||||
// verhicle_, vector<Park_time> instances)
|
||||
|
||||
int id = query.getColumn(0);
|
||||
string name = query.getColumn(1);
|
||||
string name_ = query.getColumn(1);
|
||||
string password = query.getColumn(2);
|
||||
// int verhicle = query.getColumn(3); // cast to verhicle
|
||||
vector<Park_time> park_instances = query_Parktime_for_customer(id);
|
||||
// result.push_back(Customer{id, name, password, Verhicle_type(verhicle), park_instances});
|
||||
int verhicle = query.getColumn(3); // cast to verhicle
|
||||
vector<Park_time> park_instances = query_parktimes_for_customer(id);
|
||||
result.push_back(Customer{
|
||||
id, name_, password, Verhicle_type(verhicle), park_instances});
|
||||
}
|
||||
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);
|
||||
int verhicle = query.getColumn(3); // cast to verhicle
|
||||
vector<Park_time> park_instances = query_parktimes_for_customer(id);
|
||||
Customer result{
|
||||
id, name, password, Verhicle_type(verhicle), park_instances};
|
||||
// DEBUG
|
||||
cout << "{" << result.id << "," <<result.password <<"," << int(verhicle) << "}\n";
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
void query_all_parking_spots() {
|
||||
SQLite::Statement query(data::db, "SELECT * FROM Park_spot WHERE id > ?;");
|
||||
query.bind(1, 0);
|
||||
while (query.executeStep()) {
|
||||
int id = query.getColumn(0);
|
||||
int taken = query.getColumn(1);
|
||||
int cid = query.getColumn(2);
|
||||
park_customers.push_back(query_customer_with_id(cid));
|
||||
parking_spots.push_back(
|
||||
Park_spot{get_customer_ptr_for_parkspot(cid), id, bool(taken)});
|
||||
}
|
||||
}
|
||||
|
||||
Customer* get_customer_ptr_for_parkspot(int id) {
|
||||
if (!id) {
|
||||
return nullptr;
|
||||
}
|
||||
for (int i = 0; i < park_customers.size(); i++) {
|
||||
if (park_customers[i].id == id) {
|
||||
|
||||
return &park_customers[i];
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
Reference in New Issue
Block a user