WIP query functs

This commit is contained in:
MassiveAtoms 2019-07-01 11:04:35 -03:00
parent 5f4a09d018
commit 450ffc9588
7 changed files with 94 additions and 16 deletions

View File

@ -21,6 +21,8 @@ add_executable(park
headers/Park_time.h headers/Park_time.h
encrypt.cpp encrypt.cpp
headers/encrypt.h headers/encrypt.h
Query.cpp
headers/Query.h
) )

View File

@ -19,7 +19,6 @@ Park_time::Park_time(int id_, int customer_id_, int spot_id_, int start_,
end = time_point<system_clock>(seconds(start_ + duration_)); end = time_point<system_clock>(seconds(start_ + duration_));
} }
Park_time::~Park_time() { update_db(); }
void Park_time::clock_out(int c_id, int s_id) { void Park_time::clock_out(int c_id, int s_id) {

50
Query.cpp Normal file
View File

@ -0,0 +1,50 @@
#include "headers/Query.h"
vector<Park_time> 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_time> 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<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;
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<Park_time> 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_time> park_instances = query_Parktime_for_customer(id);
// result.push_back(Customer{id, name, password, Verhicle_type(verhicle), park_instances});
}
return result;
}

View File

@ -24,7 +24,6 @@ class Park_time {
Park_time(int c_id, int s_id); Park_time(int c_id, int s_id);
Park_time(int id_, int customer_id_, int spot_id_, int start_, Park_time(int id_, int customer_id_, int spot_id_, int start_,
int duration_); int duration_);
~Park_time();
int id; int id;
int customer_id; int customer_id;
int spot_id; int spot_id;

14
headers/Query.h Normal file
View File

@ -0,0 +1,14 @@
#ifndef QUERY_H
#define QUERY_H
#pragma once
#include "Park_spot.h"
#include <exception>
#include <array>
vector<Park_time> query_Parktime_for_customer(int cid);
vector<Customer> query_customer_with_name(string name);
#endif // CUSTOMER_H

View File

@ -1,5 +1,6 @@
#include "headers/Park_spot.h" #include "headers/Query.h"
#include <array>
#include <thread> #include <thread>
@ -28,20 +29,33 @@ a wait function where 1 sec represents 1 hour irl.
int main() { int main() {
class Customer sagar { class Customer sagar {
"Sagar Ramsaransing", Verhicle_type::bike "query", Verhicle_type::bike
}; };
sagar.update_db(); sagar.update_db();
Park_spot p1; Park_spot p1;
p1.clock(&sagar); // in
Wait(2);
p1.clock(&sagar); p1.clock(&sagar);
// Wait(2); Wait(2);
// p1.clock(&sagar); p1.clock(&sagar); // in
string a = hash_password("test"); Wait(2);
string b = hash_password("test"); p1.clock(&sagar);
string c = hash_password("test"); Wait(2);
string d = hash_password("tast"); p1.clock(&sagar); // in
cout << a << "\n" << b << "\n" << c << "\n"; Wait(2);
cout << verify_password(a, "test") << "," p1.clock(&sagar);
<< verify_password(b, "test") << ", " Wait(2);
<< verify_password(c, "test") << ", "
<< verify_password(d, "test"); vector<Customer> 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?";
}
}

BIN
test.db3

Binary file not shown.