initialize db

This commit is contained in:
MassiveAtoms 2019-06-24 21:09:33 -03:00
parent 20c5e25619
commit 9a10e33279
6 changed files with 110 additions and 7 deletions

View File

@ -1,13 +1,37 @@
#include "headers/Customer.h"
#include <iostream>
#include <random>
#include <ctime>
// moet aangepast worden om een verhicle_type toe te voegen
Customer::Customer(int id_, string name_)
Customer::Customer(int id_, string name_, Verhicle_type verhicle_)
: id { id_ }
, name { name_ }
, card_code{gen_cardcode()}
, verhicle{verhicle_}
{
}
/*
generates a cardcode that can be stored on a card that can be used to authenticate.
Before that, some random generator starter settings.
*/
std::mt19937 mt(time(0));
std::uniform_int_distribution<int> dist(48, 90); // anything between and including ascii 48(0) and 90(z)
string Customer::gen_cardcode (){
string code;
for (int i = 0; i < 20; i++){
code += char(dist(mt));
}
return code;
}
/*
creert een park_time object met start time= nu, en voegt t toe aan een vector.
*/

21
db.cpp Normal file
View File

@ -0,0 +1,21 @@
#include "headers/db.h"
namespace db
{
static int callback(void *NotUsed, int argc, char **argv, char **azColName){
int i;
for(i=0; i<argc; i++)
{
std::cout<<azColName[i]<<" = " << (argv[i] ? argv[i] : "NULL")<<"\n";
}
std::cout<<"\n";
return 0;
}
// sqlite3_close(db);
}

View File

@ -39,11 +39,13 @@ public:
void clock_out(int s_id);
// void gen_weekly(); TODO: this
void gen_monthly();
Customer(int id_, string name_);
Customer(int id_, string name_, Verhicle_type verhicle_);
private:
Verhicle_type verhicle;
vector<Park_time> park_instances;
string gen_cardcode();
};

View File

@ -20,7 +20,6 @@ datetime end
Dit is gewoon een record van hoe lang, wie en waar iemand parkeert. Basically, een component van
de internal state van customer.
*/
class Park_time {
public:
int id;

56
headers/db.h Normal file
View File

@ -0,0 +1,56 @@
#include <sqlite3.h>
#include <string>
#include <iostream>
namespace db
{
static int callback(void *NotUsed, int argc, char **argv, char **azColName);
sqlite3* db;
char* error_message;
void open_db(sqlite3* db_){
int success = sqlite3_open("sqlite.db", &db);
if (!success) {
std::cout << "Can't open database: "<<sqlite3_errmsg(db)<<"\n";
}
}
// stupid lib
void initialize_db(sqlite3* db_){
// ugh, sqlite3_exec doesn't accept const strings, so i have to use const char*
const char* customer_table = "create table Customer (id int, name varchar(50), card_code varchar(20), verhicle int)";
const char* parkspot_table = "create table Park_spot (id int, taken boolean, customer_id int)";
const char* parktime_table = "create table Park_time (id int, customer_id int, spot_id int, start real, end real, duration real)";
int success;
success = sqlite3_exec(db_, customer_table, callback, 0, &error_message);
if( success!=SQLITE_OK )
{
std::cout<<"SQL error: "<<sqlite3_errmsg(db)<<"\n";
sqlite3_free(error_message);
return; // TODO: error handling instead of just logging
}
success = sqlite3_exec(db_, parkspot_table, callback, 0, &error_message);
if( success!=SQLITE_OK )
{
std::cout<<"SQL error: "<<sqlite3_errmsg(db)<<"\n";
sqlite3_free(error_message);
return; // TODO: error handling instead of just logging
}
success = sqlite3_exec(db_, parktime_table, callback, 0, &error_message);
if( success!=SQLITE_OK )
{
std::cout<<"SQL error: "<<sqlite3_errmsg(db)<<"\n";
sqlite3_free(error_message);
return; // TODO: error handling instead of just logging
}
}
}

View File

@ -4,6 +4,7 @@
#include <vector>
/*
Code strucure like this:
class declarations zijn in /headers/class_naam.h, en definitions van de member functs in /class_naam.cpp
@ -35,10 +36,10 @@ int main()
1, 2, 3, 4, 5
};
std::vector<Customer> customers {
{ 1, "Sagar Ram" },
{ 2, "Shaq" },
{ 3, "Josh" },
{ 4, "Stefano" }
{ 1, "Sagar Ram", Verhicle_type::small },
{ 2, "Shaq", Verhicle_type::medium },
{ 3, "Josh", Verhicle_type::large },
{ 4, "Stefano", Verhicle_type::small }
};
spots[1].clock(&customers[3]); // stefano parks at spot 2