-
Notifications
You must be signed in to change notification settings - Fork 1
/
Database.h
96 lines (78 loc) · 2.46 KB
/
Database.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
//
// Created by Anaid Gakhokidze on 2017-05-09.
//
#ifndef TASKSCHEDULER_DATABASE_H
#define TASKSCHEDULER_DATABASE_H
#include <string>
#include <sqlite3.h>
#include "Log.h"
#include <list>
// This class is used for communicating with the database.
class Database {
private:
/*
* sqlite3 database connection.
* */
sqlite3 * m_database;
/*
* Name of the database.
* */
std::string m_dbName;
/*
* Log file that stores raw sql queries that failed to be executed by the database
* when trying to insert a record.
* */
Log m_recordsLog;
/*
* Log file that contains actions that database failed to execute,
* such as failing to create a table or alter a table.
* */
Log m_stmLog;
/*
* Construct a query in the following format
* "insert into tableName (col1.... colN) values (val1.... valN)"
* */
std::string constructInsertQuery(std::string & tableName, std::unordered_map<std::string, double> & values);
/*
* Execute a sqlite statement.
* @param error - what the error message in the log should contain.
* */
bool executeStm(std::string & stm, std::string & error);
/*
* Checks if the column exists in tableName.
* */
bool columnExists(std::string &tableName, std::string &colName);
public:
Database(std::string databaseName);
/*
* Returns true if a database connection has been established successfully, false otherwise.
* The user is responsible for calling deinitialize() even if initialize() returned false.
* Needs to be invoked after the constructor.
* */
bool initialize();
/*
* Needs to be invoked before the destructor.
* */
void deinitialize();
/*
* Insert a record with 'values' into 'tableName'.
* */
bool insertRecord(std::string & tableName, std::unordered_map<std::string, double> & values);
/*
* Create a table 'tableName' with an initial extra column for one metric.
* */
bool createTable(std::string &tableName);
/*
* Create a table to store aggregate values for each metric.
* */
bool createAggregateMetricTable();
/*
* Update aggregate metrics.
* */
bool updateAggregateMetric(std::string metricType, std::string & tableName, std::string colName);
/*
* Alter a table to add an extra column.
* */
bool addColumn(std::string & tableName, std::string colName);
};
#endif //TASKSCHEDULER_DATABASE_H