From c058783a1154e0fb7423983ec8a30bc9579ce6f3 Mon Sep 17 00:00:00 2001 From: TinyAtoms Date: Sat, 29 Jun 2019 22:30:47 -0300 Subject: [PATCH] ALL objects can be saved to db now --- Customer.cpp | 2 +- Park_spot.cpp | 56 ++++++++++++++++++++++++++++++++++++++++++-- Park_time.cpp | 1 - headers/Park_spot.h | 6 ++++- main.cpp | 21 +++++++++++++---- test.db3 | Bin 16384 -> 16384 bytes 6 files changed, 76 insertions(+), 10 deletions(-) diff --git a/Customer.cpp b/Customer.cpp index b88e7df..28afbf8 100644 --- a/Customer.cpp +++ b/Customer.cpp @@ -53,7 +53,7 @@ void Customer::save_db() { statement.insert(38, std::to_string(int(verhicle))); statement.insert(36, card_code); statement.insert(32, name); - statement.insert(29, "null"); + statement.insert(29, to_string(id)); SQLite::Transaction transaction(data::db); data::db.exec(statement); transaction.commit(); diff --git a/Park_spot.cpp b/Park_spot.cpp index 929a7d4..55243f2 100644 --- a/Park_spot.cpp +++ b/Park_spot.cpp @@ -1,9 +1,10 @@ #include "headers/Park_spot.h" -Park_spot::Park_spot(int id_){ +Park_spot::Park_spot(){ parked = nullptr; - id = id_; + id = auto_increment_db() + 1; taken = false; + save_db(); } // clock in en out, calls de juist(in/out) van de customer aan de hand van internal state van taken @@ -12,10 +13,61 @@ void Park_spot::clock(Customer* c_customer){ parked = c_customer; taken = true; parked->clock_in(id); + update_db(); } else{ taken = false; parked->clock_out(id); parked = nullptr; + update_db(); } +} + + +// --------------------- db functs + +void Park_spot::update_db() { + string statement = "UPDATE Park_spot SET taken = '', customer_id = '' where id = '';"; + // 29, 48, 62 + statement.insert(63, to_string(id)); + if (taken){ + statement.insert(49, to_string(parked->id)); + statement.insert(30, "true"); + } + else { + statement.insert(49, "NULL"); + statement.insert(30, "false"); + } + data::db.exec(statement); +} + + +void Park_spot::save_db() { + //(int id, bool taken, int customer_id) + string statement{"insert into Park_spot values ( , , );"}; + // after ( = 28) + statement.insert(34, "NULL"); + statement.insert(32, "false"); + statement.insert(30, to_string(id)); + SQLite::Transaction transaction(data::db); + data::db.exec(statement); + transaction.commit(); +} + +void Park_spot::delete_db() { + string statement = "delete from Park_spot where id= ;"; + statement.insert(statement.length() - 2, std::to_string(id)); + // std::cout << statement; + SQLite::Transaction transaction(data::db); + data::db.exec(statement); + transaction.commit(); +} + +int Park_spot::auto_increment_db() { + SQLite::Statement max_id(data::db, "select max(id) from Park_spot;"); + int id = 0; + max_id.executeStep(); + id = max_id.getColumn(0); + max_id.reset(); + return id; } \ No newline at end of file diff --git a/Park_time.cpp b/Park_time.cpp index e5cd542..84069fb 100644 --- a/Park_time.cpp +++ b/Park_time.cpp @@ -77,7 +77,6 @@ void Park_time::update_db() { statement.insert(53, std::to_string(id)); statement.insert(40, to_string(duration)); statement.insert(27, to_string(start_to_int() + duration)); - std::cout << statement; // TODO: set some logging here data::db.exec(statement); } diff --git a/headers/Park_spot.h b/headers/Park_spot.h index 4085152..86b9da0 100644 --- a/headers/Park_spot.h +++ b/headers/Park_spot.h @@ -15,7 +15,11 @@ class Park_spot { int id; bool taken; Customer* parked; //TODO: think about memory management - Park_spot(int id_); + Park_spot(); void clock(Customer* c_customer); private: + void save_db(); + void update_db(); + void delete_db(); + int auto_increment_db(); }; \ No newline at end of file diff --git a/main.cpp b/main.cpp index 4a5b372..f83c0ba 100644 --- a/main.cpp +++ b/main.cpp @@ -5,6 +5,8 @@ #include #include +#include + /* Code strucure like this: class declarations zijn in /headers/class_naam.h, en definitions van de member @@ -20,16 +22,25 @@ record die zegt dat een customer voor x tijd geparkeert heeft bij spot x, enz. De client clockt in en uit bij een spot. */ +void Wait(int sec) +/* +a wait function where 1 sec represents 1 hour irl. + */ +{ + std::this_thread::sleep_for(seconds{sec}); +} + + using std::cout; int main() { - class Customer sagar{"nonsense", Verhicle_type::medium}; + class Customer sagar{"Sagar Ramsaransing", Verhicle_type::medium}; sagar.update_db(); + Park_spot p1; + p1.clock(&sagar); + Wait(2); + p1.clock(&sagar); - Park_spot p1{1}; - p1.clock(&sagar); - p1.clock(&sagar); - sagar.gen_monthly(); } diff --git a/test.db3 b/test.db3 index c44648e656df65c1ee69b8f02d9ce8fe8d1c3da0..649423a0e0cade26f7a187d6d955b49f7faf155b 100644 GIT binary patch delta 143 zcmZo@U~Fh$oFL7}IZ?)$k#l3h5`H#D{!0w}mo^J3oZz?6VPs`6G-V7(sVw#?^{h;Z3QF?GWa4FDU|{6`$iV**sQWp;BrhWigSco~ oVoq_YU#O1{MEV^A|2v@c6Mi89Ms@~HP8Jr%SP6kCKs=2J0Fx;uSpWb4 delta 410 zcmZo@U~Fh$oFL68Fj2;tQD9@j5`H0eeijD)JN!HNr|=i>`|xYBDE%A?w^GeDn^9}Z_ za$!(2mBK!ND1)*d93`~8Tdc* zKjpv9f0BPYFp!q=dkV0!Gcab%7