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
data.cpp
headers/data.h
#[[Customer.cpp
Customer.cpp
headers/Customer.h
Park_spot.cpp
headers/Park_spot.h
Park_time.cpp
headers/Park_time.h]]
headers/Park_time.h
)

View File

@ -2,12 +2,12 @@
#include <iostream>
// constructors
Customer::Customer(string name_, Verhicle_type verhicle_, SQLite::Database& db):
Customer::Customer(string name_, Verhicle_type verhicle_):
name{name_}, verhicle{verhicle_},
card_code{gen_cardcode()}
{
id = auto_increment_db(db) + 1;
save_db(db);
id = auto_increment_db() + 1;
save_db();
}
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
void Customer::save_db(SQLite::Database& database) {
void Customer::save_db() {
string statement{"insert into Customer values (, '', '', );"};
// after ( = 28)
statement.insert(38, std::to_string(int(verhicle)));
statement.insert(36, card_code);
statement.insert(32, name);
statement.insert(29, "null");
SQLite::Transaction transaction(database);
database.exec(statement);
SQLite::Transaction transaction(data::db);
data::db.exec(statement);
transaction.commit();
}
void Customer::update_db(SQLite::Database& database) {
string statement = "UPDATE Customer SET name = \"\", card_code = \"\" where id = '';";
void Customer::update_db() {
string statement = "UPDATE Customer SET name = '', card_code = '' where id = '';";
statement.insert(58, std::to_string(id));
statement.insert(44, card_code);
statement.insert(28, name);
// 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= ;";
statement.insert(statement.length() - 2, std::to_string(id));
// std::cout << statement;
SQLite::Transaction transaction(database);
database.exec(statement);
SQLite::Transaction transaction(data::db);
data::db.exec(statement);
transaction.commit();
}
int Customer::auto_increment_db(SQLite::Database& database) {
SQLite::Statement max_id(database, "select max(id) from Customer;");
int Customer::auto_increment_db() {
SQLite::Statement max_id(data::db, "select max(id) from Customer;");
int id = 0;
max_id.executeStep();
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 }
, duration { 0 }
, start { high_resolution_clock::now() }
, id {auto_increment_db() + 1}
{
save_db();
}
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) {
end = high_resolution_clock::now();
duration = duration_cast<seconds>(end - start).count(); // use mins later
update_db();
} else {
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;
}
void Park_time::debug() {
int Park_time::start_to_int(){
auto start_to_epoch = start.time_since_epoch();
auto start_value = std::chrono::duration_cast<std::chrono::seconds>(start_to_epoch);
int start_seconds = start_value.count();
return start_seconds;
}
auto end_to_epoch = end.time_since_epoch();
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 << ">" ;
// db funcs -----------------------------------------------------------------------------
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 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;
}

View File

@ -7,6 +7,7 @@
#include <random>
#include <string>
#include <vector>
#include "data.h"
using std::string;
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 {
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_,
vector<Park_time> instances); // needed to construct from db
// 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_out(int s_id);
void update_db(SQLite::Database& database);
void delete_db(SQLite::Database& database);
void update_db();
void delete_db();
// void gen_weekly(); TODO: this
void gen_monthly();
@ -51,8 +52,8 @@ class Customer {
string gen_cardcode();
void save_db(SQLite::Database& database);
int auto_increment_db(SQLite::Database& database);
void save_db();
int auto_increment_db();
};
#endif // CUSTOMER_H

View File

@ -4,10 +4,13 @@
#include <chrono>
#include <iostream>
#include <string>
#include "data.h"
using namespace std::chrono;
using std::string;
using std::to_string;
/*
db repr of Park_time
int id (not null, auto increment)
@ -28,14 +31,18 @@ public:
int spot_id;
int duration;
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);
friend std::ostream& operator<<(std::ostream& os, const Park_time & pt);
void debug();
private:
high_resolution_clock::time_point start;
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();
static SQLite::Database db = start_db();
}
#endif

View File

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

BIN
test.db3

Binary file not shown.