From 9ae95aef1cc3666706d9b1ed4e30d86916b9ede6 Mon Sep 17 00:00:00 2001 From: MassiveAtoms Date: Mon, 1 Jul 2019 21:51:23 -0300 Subject: [PATCH] almost done --- CMakeLists.txt | 4 +- Park_spot.cpp | 4 +- Query.cpp | 94 +++++++++++++++++++++++++++++++++++++++++++++ headers/Customer.h | 5 --- headers/Park_spot.h | 10 +++-- headers/Query.h | 21 ++++++++++ headers/data.h | 1 + main.cpp | 9 ++++- 8 files changed, 134 insertions(+), 14 deletions(-) create mode 100644 Query.cpp create mode 100644 headers/Query.h diff --git a/CMakeLists.txt b/CMakeLists.txt index fc51518..7dfd9a6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -23,8 +23,8 @@ add_executable(park headers/Park_spot.h Park_time.cpp headers/Park_time.h - # Query.cpp - # headers/Query.h + Query.cpp + headers/Query.h ) diff --git a/Park_spot.cpp b/Park_spot.cpp index 5724695..e64e8e1 100644 --- a/Park_spot.cpp +++ b/Park_spot.cpp @@ -7,8 +7,8 @@ Park_spot::Park_spot() save_db(); } -Park_spot::Park_spot(int id_, bool taken_, Customer& parked) - : parked_customer{parked.id}, +Park_spot::Park_spot(int id_, bool taken_, int parked) + : parked_customer{parked}, id{id_}, taken{taken_} // TODO: think about how init parked? {} diff --git a/Query.cpp b/Query.cpp new file mode 100644 index 0000000..4cd3840 --- /dev/null +++ b/Query.cpp @@ -0,0 +1,94 @@ +#include "headers/Query.h" + + +vector 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_times; + + SQLite::Statement query(data::db, + "SELECT * FROM Park_time WHERE customer_id = ?;"); + 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 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 result; + SQLite::Statement query( + data::db, + "SELECT id, name, password, verhicle FROM Customer WHERE name = ?;"); + query.bind(1, name); + while (query.executeStep()) { + int id = query.getColumn(0); + string name_ = query.getColumn(1); + string password = query.getColumn(2); + int verhicle = query.getColumn(3); // cast to verhicle + vector 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_instances = query_parktimes_for_customer(id); + Customer result{ + id, name, password, Verhicle_type(verhicle), park_instances}; + // DEBUG + // cout << "{" << result.id << "," < query_all_parking_spots() { + vector spots; + SQLite::Statement query(data::db, "SELECT * FROM Park_spot WHERE id > 2;"); + // query.bind(1, 2); + 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)); + spots.push_back({id, taken, cid}); + } + return spots; +} + diff --git a/headers/Customer.h b/headers/Customer.h index 34df04f..6a7b072 100644 --- a/headers/Customer.h +++ b/headers/Customer.h @@ -46,10 +46,5 @@ class Customer { int auto_increment_db(); }; -static vector park_customers; -// save the customers that are parked in here -// parking_spot uses pointers, so it's better to save the parked customers here -// where we know they'll be destroyed at the end of this scope, instead of too -// early and end up with dangling pointers #endif // CUSTOMER_H \ No newline at end of file diff --git a/headers/Park_spot.h b/headers/Park_spot.h index dd9127a..61a994c 100644 --- a/headers/Park_spot.h +++ b/headers/Park_spot.h @@ -1,5 +1,8 @@ -#include "Customer.h" +#ifndef PARK_SPOT_H +#define PARK_SPOT_H +#pragma once +#include "Customer.h" /* db representation: int id not null @@ -16,7 +19,7 @@ class Park_spot { bool taken; int parked_customer; Park_spot(); - Park_spot(int id_, bool taken_, Customer& parked); + Park_spot(int id_, bool taken_, int parked); void clock(Customer& c_customer); private: @@ -25,5 +28,4 @@ class Park_spot { void delete_db(); int auto_increment_db(); }; - -static vector parking_spots; // to save the parking spots in memory \ No newline at end of file +#endif // CUSTOMER_H \ No newline at end of file diff --git a/headers/Query.h b/headers/Query.h new file mode 100644 index 0000000..e92f467 --- /dev/null +++ b/headers/Query.h @@ -0,0 +1,21 @@ +#ifndef QUERY_H +#define QUERY_H +#pragma once + +#include "Park_spot.h" + +#include + +vector query_parktimes_for_customer(int cid); + +vector query_customer_with_name(string name); +Customer query_customer_with_id(int id); + +vector query_all_parking_spots(); // used for initializing the parking spots at start of the program + +static vector parking_spots = query_all_parking_spots(); // to save the parking spots in memory +static vector park_customers; +// save the customers that are parked in here + + +#endif // CUSTOMER_H \ No newline at end of file diff --git a/headers/data.h b/headers/data.h index fe6671c..c6855b4 100644 --- a/headers/data.h +++ b/headers/data.h @@ -6,6 +6,7 @@ namespace data { SQLite::Database start_db(); + static SQLite::Database db = start_db(); } // namespace data diff --git a/main.cpp b/main.cpp index 94bffcd..ec76f67 100644 --- a/main.cpp +++ b/main.cpp @@ -1,4 +1,4 @@ -#include "headers/Park_spot.h" +#include "headers/Query.h" #include #include @@ -30,6 +30,13 @@ a wait function where 1 sec represents 1 hour irl. } int main() { + // Customer sagar = query_customer_with_name("stefan udit")[0]; + Customer sagar = query_customer_with_id(2); + cout << sagar.id << "," << sagar.name << "," << sagar.password; + // cout << parking_spots.size(); + // for (auto i : parking_spots){ + // cout << i.id << "," << i.parked_customer; + // } }