From 450ffc95881fab87b3be6d2b4df9e74e4f3679c3 Mon Sep 17 00:00:00 2001 From: MassiveAtoms Date: Mon, 1 Jul 2019 11:04:35 -0300 Subject: [PATCH] WIP query functs --- CMakeLists.txt | 2 ++ Park_time.cpp | 1 - Query.cpp | 50 ++++++++++++++++++++++++++++++++++++++++++++ headers/Park_time.h | 1 - headers/Query.h | 14 +++++++++++++ main.cpp | 42 ++++++++++++++++++++++++------------- test.db3 | Bin 16384 -> 16384 bytes 7 files changed, 94 insertions(+), 16 deletions(-) create mode 100644 Query.cpp create mode 100644 headers/Query.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 1d935c9..d7e62b3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -21,6 +21,8 @@ add_executable(park headers/Park_time.h encrypt.cpp headers/encrypt.h + Query.cpp + headers/Query.h ) diff --git a/Park_time.cpp b/Park_time.cpp index 963a53d..f2a4311 100644 --- a/Park_time.cpp +++ b/Park_time.cpp @@ -19,7 +19,6 @@ Park_time::Park_time(int id_, int customer_id_, int spot_id_, int start_, end = time_point(seconds(start_ + duration_)); } -Park_time::~Park_time() { update_db(); } void Park_time::clock_out(int c_id, int s_id) { diff --git a/Query.cpp b/Query.cpp new file mode 100644 index 0000000..6cda385 --- /dev/null +++ b/Query.cpp @@ -0,0 +1,50 @@ +#include "headers/Query.h" + +vector query_Parktime_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; +} + +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 * 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 instances) + + 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_Parktime_for_customer(id); + // result.push_back(Customer{id, name, password, Verhicle_type(verhicle), park_instances}); + } + return result; +} \ No newline at end of file diff --git a/headers/Park_time.h b/headers/Park_time.h index 49ab9ea..0c295d0 100644 --- a/headers/Park_time.h +++ b/headers/Park_time.h @@ -24,7 +24,6 @@ class Park_time { Park_time(int c_id, int s_id); Park_time(int id_, int customer_id_, int spot_id_, int start_, int duration_); - ~Park_time(); int id; int customer_id; int spot_id; diff --git a/headers/Query.h b/headers/Query.h new file mode 100644 index 0000000..c618d31 --- /dev/null +++ b/headers/Query.h @@ -0,0 +1,14 @@ +#ifndef QUERY_H +#define QUERY_H +#pragma once + +#include "Park_spot.h" + +#include +#include + +vector query_Parktime_for_customer(int cid); +vector query_customer_with_name(string name); + + +#endif // CUSTOMER_H \ No newline at end of file diff --git a/main.cpp b/main.cpp index 8ca82c6..c6a9b89 100644 --- a/main.cpp +++ b/main.cpp @@ -1,5 +1,6 @@ -#include "headers/Park_spot.h" +#include "headers/Query.h" +#include #include @@ -28,20 +29,33 @@ a wait function where 1 sec represents 1 hour irl. int main() { class Customer sagar { - "Sagar Ramsaransing", Verhicle_type::bike + "query", Verhicle_type::bike }; sagar.update_db(); Park_spot p1; + p1.clock(&sagar); // in + Wait(2); p1.clock(&sagar); - // Wait(2); - // p1.clock(&sagar); - string a = hash_password("test"); - string b = hash_password("test"); - string c = hash_password("test"); - string d = hash_password("tast"); - cout << a << "\n" << b << "\n" << c << "\n"; - cout << verify_password(a, "test") << "," - << verify_password(b, "test") << ", " - << verify_password(c, "test") << ", " - << verify_password(d, "test"); -} + Wait(2); + p1.clock(&sagar); // in + Wait(2); + p1.clock(&sagar); + Wait(2); + p1.clock(&sagar); // in + Wait(2); + p1.clock(&sagar); + Wait(2); + + vector test = query_customer_with_name("query"); + if (!test.size()){ + cout << "No customers with this name found;"; + } + else if (test.size()==1) { + cout << "1 customer found;"; + } + else { + cout << "MORE customers found?"; + } + + +} \ No newline at end of file diff --git a/test.db3 b/test.db3 index d1b5a9b64ceae30a9cb177ca0b247d311b179261..126855f7f89a7be97fdf08acd460af0157cb43d2 100644 GIT binary patch delta 2029 zcmZ9~TTEMZ90zdvr?fpc2LEu8#t?{(Te%-foyr`S-sovdX$zFvmQqfEjo#^?jJELL zgE7k_sF}tsE@;eRcpzjkG1JTkW2O(r2OrcBW6Waq;Dbgpm+0cpder~v!}lcL&;OU4 zoZtWK>?n426fbL$;)}h<$KZLi@b;-ngQ{U)UE^%gJkWfmxu#js@ajkEyXsfeA$6DP zH`N{0OR5>wu&Uu`_pK^pv!SY@QBS3^VmcSGJJMsz^S*pMD_U0Ojk$~?(meeCd3w&7 z84o6w=c2Cs_-ts^K9TV7`l^c2{XK16YsUP4)^W{^Xv?CFB2|16;C`4?Y6}L06#1EEFiNRvQ7;(*6 z-Lc7NA-R$Dn#}w_`IO(6u+HbF%%;$I%p9^?pO0_Y`Tp`LuX!`>wS=Z}&ceD7O@<2| zdyMZZpR&2uCWVdp8GmGAbaNv)njKGh`QGxWMYm^b&LIewe3nI{$6{J4Y>x9i?!7)}Z6e0TX&!j>vb1SgGaCSxp<-m+M-J~ywErkelVz13@-Ubfl& z3)$3#Wv4$SCY_7AuKlpKu|ZheSQ^crk3{X(cx*M43XK`Jb)BW7OwhI+n92p$E+!q9 zwk@WKurH_UC>_Pa+uPpdptu@Y_2;s4=8$7@QP=+W(eX&rr>aT==iXPhI9sZRs!vxp zRI9n)x%=EV+~?eF?vT6AUEy}P6t~E^k9JSkB4Qw?n5q^fWI=5uhY0tP_Ab~>gnLOl0DA}F9?3f4Dl1nY=MnBEEdnMYtdq=K k8Vmu35$=+(qXW7R;zziXG%wgJ!X2bdflZ_1rjZ}fUwE6o9RL6T delta 79 zcmZo@U~Fh$oFK)-aC)MQ6Oi1Pu!Nskol|8pyMX`3#&aA@+Uk=NrNlSeN*-rpR_Ak` g%&q_wf6KesS??etvpTQjWOf6fz+s-vlD5y80KYpJlK=n!