most features are implemented #4

Merged
MassiveAtoms merged 20 commits from sqlitecpp into master 2019-06-30 18:34:59 +00:00
11 changed files with 82 additions and 37 deletions
Showing only changes of commit 0eee67d3d9 - Show all commits

View File

@ -13,12 +13,12 @@ add_executable(park
main.cpp main.cpp
data.cpp data.cpp
headers/data.h headers/data.h
#[[Customer.cpp Customer.cpp
headers/Customer.h headers/Customer.h
Park_spot.cpp Park_spot.cpp
headers/Park_spot.h headers/Park_spot.h
Park_time.cpp Park_time.cpp
headers/Park_time.h]] headers/Park_time.h
) )

View File

@ -2,12 +2,12 @@
#include <iostream> #include <iostream>
// constructors // constructors
Customer::Customer(string name_, Verhicle_type verhicle_, SQLite::Database& db): Customer::Customer(string name_, Verhicle_type verhicle_):
name{name_}, verhicle{verhicle_}, name{name_}, verhicle{verhicle_},
card_code{gen_cardcode()} card_code{gen_cardcode()}
{ {
id = auto_increment_db(db) + 1; id = auto_increment_db() + 1;
save_db(db); save_db();
} }
Customer::Customer(int id_, string name_, string card_code_, Verhicle_type verhicle_, vector<Park_time> instances) Customer::Customer(int id_, string name_, string card_code_, Verhicle_type verhicle_, vector<Park_time> instances)
@ -47,38 +47,38 @@ void Customer::gen_monthly() {
//================================================================================================ //================================================================================================
// functions that interact with the database // functions that interact with the database
void Customer::save_db(SQLite::Database& database) { void Customer::save_db() {
string statement{"insert into Customer values (, '', '', );"}; string statement{"insert into Customer values (, '', '', );"};
// after ( = 28) // after ( = 28)
statement.insert(38, std::to_string(int(verhicle))); statement.insert(38, std::to_string(int(verhicle)));
statement.insert(36, card_code); statement.insert(36, card_code);
statement.insert(32, name); statement.insert(32, name);
statement.insert(29, "null"); statement.insert(29, "null");
SQLite::Transaction transaction(database); SQLite::Transaction transaction(data::db);
database.exec(statement); data::db.exec(statement);
transaction.commit(); transaction.commit();
} }
void Customer::update_db(SQLite::Database& database) { void Customer::update_db() {
string statement = "UPDATE Customer SET name = \"\", card_code = \"\" where id = '';"; string statement = "UPDATE Customer SET name = '', card_code = '' where id = '';";
statement.insert(58, std::to_string(id)); statement.insert(58, std::to_string(id));
statement.insert(44, card_code); statement.insert(44, card_code);
statement.insert(28, name); statement.insert(28, name);
// std::cout << statement; TODO: set some logging here // std::cout << statement; TODO: set some logging here
database.exec(statement); data::db.exec(statement);
} }
void Customer::delete_db(SQLite::Database& database) { void Customer::delete_db() {
string statement = "delete from Customer where id= ;"; string statement = "delete from Customer where id= ;";
statement.insert(statement.length() - 2, std::to_string(id)); statement.insert(statement.length() - 2, std::to_string(id));
// std::cout << statement; // std::cout << statement;
SQLite::Transaction transaction(database); SQLite::Transaction transaction(data::db);
database.exec(statement); data::db.exec(statement);
transaction.commit(); transaction.commit();
} }
int Customer::auto_increment_db(SQLite::Database& database) { int Customer::auto_increment_db() {
SQLite::Statement max_id(database, "select max(id) from Customer;"); SQLite::Statement max_id(data::db, "select max(id) from Customer;");
int id = 0; int id = 0;
max_id.executeStep(); max_id.executeStep();
id = max_id.getColumn(0); id = max_id.getColumn(0);

View File

@ -8,7 +8,9 @@ Park_time::Park_time(int c_id, int s_id)
, spot_id { s_id } , spot_id { s_id }
, duration { 0 } , duration { 0 }
, start { high_resolution_clock::now() } , start { high_resolution_clock::now() }
, id {auto_increment_db() + 1}
{ {
save_db();
} }
void Park_time::clock_out(int c_id, int s_id) void Park_time::clock_out(int c_id, int s_id)
@ -26,6 +28,7 @@ void Park_time::clock_out(int c_id, int s_id)
if (!duration) { if (!duration) {
end = high_resolution_clock::now(); end = high_resolution_clock::now();
duration = duration_cast<seconds>(end - start).count(); // use mins later duration = duration_cast<seconds>(end - start).count(); // use mins later
update_db();
} else { } else {
std::cout << "Already clocked out. Something is wrong \n"; std::cout << "Already clocked out. Something is wrong \n";
@ -44,13 +47,47 @@ std::ostream& operator<<(std::ostream& os, const Park_time & pt){
return os; return os;
} }
void Park_time::debug() {
int Park_time::start_to_int(){
auto start_to_epoch = start.time_since_epoch(); auto start_to_epoch = start.time_since_epoch();
auto start_value = std::chrono::duration_cast<std::chrono::seconds>(start_to_epoch); auto start_value = std::chrono::duration_cast<std::chrono::seconds>(start_to_epoch);
int start_seconds = start_value.count(); int start_seconds = start_value.count();
return start_seconds;
}
auto end_to_epoch = end.time_since_epoch(); // db funcs -----------------------------------------------------------------------------
auto end_value = std::chrono::duration_cast<std::chrono::seconds>(start_to_epoch);
int end_seconds = end_value.count();
std::cout << "<" << start_seconds << "-" << end_seconds << ">" ;
void Park_time::save_db() {
string statement{"insert into Park_time values ( , , , , , );"};
statement.insert(41, "NULL");
statement.insert(39, "NULL");
statement.insert(37, to_string(start_to_int()) );
statement.insert(35, to_string(spot_id));
statement.insert(33, to_string(customer_id));
statement.insert(31, to_string(id));
SQLite::Transaction transaction(data::db);
data::db.exec(statement);
transaction.commit();
}
void Park_time::update_db() {
string statement = "UPDATE Park_time SET end = , duration = where id = '';";
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);
}
// to get id on first save to db
int Park_time::auto_increment_db() {
SQLite::Statement max_id(data::db, "select max(id) from Park_time;");
int id = 0;
max_id.executeStep();
id = max_id.getColumn(0);
max_id.reset();
return id;
} }

View File

@ -7,7 +7,7 @@ SQLite::Database start_db() {
db.exec("create table if not exists Customer (id integer primary key, name text, card_code varchar(20), verhicle int)"); db.exec("create table if not exists Customer (id integer primary key, name text, card_code varchar(20), verhicle int)");
db.exec("create table if not exists Park_spot (id integer primary key, taken boolean, customer_id int)"); db.exec("create table if not exists Park_spot (id integer primary key, taken boolean, customer_id int)");
db.exec("create table if not exists Park_time (id integer primary key, customer_id int, spot_id int, start real, end real, duration real)"); db.exec("create table if not exists Park_time (id integer primary key, customer_id int, spot_id int, start int, end int, duration int)");
return db; return db;
} }

View File

@ -7,6 +7,7 @@
#include <random> #include <random>
#include <string> #include <string>
#include <vector> #include <vector>
#include "data.h"
using std::string; using std::string;
using std::vector; using std::vector;
@ -28,7 +29,7 @@ clock in en out creeert en compleet een park_time object. Voegt het toe aan een
class Customer { class Customer {
public: public:
Customer(string name_, Verhicle_type verhicle_, SQLite::Database& db); Customer(string name_, Verhicle_type verhicle_);
Customer(int id_, string name_, string card_code_, Verhicle_type verhicle_, Customer(int id_, string name_, string card_code_, Verhicle_type verhicle_,
vector<Park_time> instances); // needed to construct from db vector<Park_time> instances); // needed to construct from db
// potentially: add a destructor that calls update_db() before being destroyed // potentially: add a destructor that calls update_db() before being destroyed
@ -39,8 +40,8 @@ class Customer {
void clock_in(int s_id); void clock_in(int s_id);
void clock_out(int s_id); void clock_out(int s_id);
void update_db(SQLite::Database& database); void update_db();
void delete_db(SQLite::Database& database); void delete_db();
// void gen_weekly(); TODO: this // void gen_weekly(); TODO: this
void gen_monthly(); void gen_monthly();
@ -51,8 +52,8 @@ class Customer {
string gen_cardcode(); string gen_cardcode();
void save_db(SQLite::Database& database); void save_db();
int auto_increment_db(SQLite::Database& database); int auto_increment_db();
}; };
#endif // CUSTOMER_H #endif // CUSTOMER_H

View File

@ -4,10 +4,13 @@
#include <chrono> #include <chrono>
#include <iostream> #include <iostream>
#include <string>
#include "data.h"
using namespace std::chrono; using namespace std::chrono;
using std::string;
using std::to_string;
/* /*
db repr of Park_time db repr of Park_time
int id (not null, auto increment) int id (not null, auto increment)
@ -28,14 +31,18 @@ public:
int spot_id; int spot_id;
int duration; int duration;
Park_time(int c_id, int s_id); Park_time(int c_id, int s_id);
// Park_time(int c_id, int s_id );
void clock_out(int c_id, int s_id); void clock_out(int c_id, int s_id);
friend std::ostream& operator<<(std::ostream& os, const Park_time & pt); friend std::ostream& operator<<(std::ostream& os, const Park_time & pt);
void debug();
private: private:
high_resolution_clock::time_point start; high_resolution_clock::time_point start;
high_resolution_clock::time_point end; high_resolution_clock::time_point end;
//TODO: discuss pros cons of using chrono, ctime, or 3th party lib void save_db();
void update_db();
int auto_increment_db(); // helper
int start_to_int(); // helper
}; };

View File

@ -6,7 +6,6 @@ namespace data {
SQLite::Database start_db(); SQLite::Database start_db();
static SQLite::Database db = start_db(); static SQLite::Database db = start_db();
} }
#endif #endif

View File

@ -1,5 +1,6 @@
#include "thirdparty/SQLiteCpp/include/SQLiteCpp/SQLiteCpp.h" #include "thirdparty/SQLiteCpp/include/SQLiteCpp/SQLiteCpp.h"
#include "headers/data.h" #include "headers/data.h"
#include "headers/Park_spot.h"
#include <iostream> #include <iostream>
#include <vector> #include <vector>
#include <string> #include <string>
@ -23,12 +24,12 @@ De client clockt in en uit bij een spot.
using std::cout; using std::cout;
int main() { int main() {
SQLite::Statement test(data::db, "SELECT * FROM Customer WHERE id > 2"); class Customer sagar{"nonsense", Verhicle_type::medium};
while (test.executeStep()){ sagar.update_db();
int id = test.getColumn(0);
std::string name = test.getColumn(1);
cout << id << ", " << name << std::endl;
} Park_spot p1{1};
p1.clock(&sagar);
p1.clock(&sagar);
sagar.gen_monthly();
} }

BIN
test.db3

Binary file not shown.