2018-02-02 12:27:57 +00:00
|
|
|
#ifndef DATABASE_H
|
|
|
|
#define DATABASE_H
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2018-03-02 10:30:16 +00:00
|
|
|
#include <numeric>
|
2018-02-02 12:27:57 +00:00
|
|
|
|
|
|
|
#include "SQLite/sqlite3.h"
|
2018-02-12 14:15:04 +00:00
|
|
|
#include "model/recipe/ingredients/ingredient.h"
|
2018-03-01 16:19:13 +00:00
|
|
|
#include "resulttable.h"
|
2018-02-02 12:27:57 +00:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2018-03-01 16:19:13 +00:00
|
|
|
/**
|
|
|
|
* @brief The Database class is responsible for generic abstraction of commonly used database features.
|
|
|
|
*/
|
|
|
|
|
2018-02-02 12:27:57 +00:00
|
|
|
class Database
|
|
|
|
{
|
|
|
|
public:
|
2018-03-01 16:19:13 +00:00
|
|
|
//Creates and opens a database connection to a file of the given name. If not there, this will generate a database.
|
2018-02-02 12:27:57 +00:00
|
|
|
Database(string filename);
|
2018-03-01 16:19:13 +00:00
|
|
|
~Database();
|
2018-02-02 12:27:57 +00:00
|
|
|
|
2018-03-01 16:19:13 +00:00
|
|
|
//Executes an SQL string statement in a safe way and returns the result.
|
|
|
|
ResultTable executeSQL(string statement);
|
2018-05-22 21:06:11 +00:00
|
|
|
//Inserts into a table.
|
2018-03-02 10:30:16 +00:00
|
|
|
bool insertInto(string tableName, vector<string> columnNames, vector<string> values);
|
2018-05-22 21:06:11 +00:00
|
|
|
bool insertInto(string tableName, string columnName, string value);
|
|
|
|
//Selects from a table.
|
2018-03-03 07:38:32 +00:00
|
|
|
ResultTable selectFrom(string tableName, string columnNames, string conditions);
|
2018-05-22 21:06:11 +00:00
|
|
|
//Deletes from a table.
|
2018-03-29 09:21:00 +00:00
|
|
|
bool deleteFrom(string tableName, string conditions);
|
2018-02-12 13:24:11 +00:00
|
|
|
|
2018-03-01 16:19:13 +00:00
|
|
|
bool tableExists(string tableName);
|
2018-03-30 12:33:48 +00:00
|
|
|
int getLastInsertedRowId() const;
|
|
|
|
|
|
|
|
unsigned long getQueryCount() const;
|
2018-03-03 07:38:32 +00:00
|
|
|
|
2018-03-29 14:09:58 +00:00
|
|
|
void closeConnection();
|
|
|
|
|
2018-03-31 10:53:31 +00:00
|
|
|
void beginTransaction();
|
|
|
|
void commitTransaction();
|
|
|
|
void rollbackTransaction();
|
|
|
|
|
2018-03-03 07:38:32 +00:00
|
|
|
protected:
|
|
|
|
string surroundString(string s, string surround);
|
|
|
|
|
2018-02-02 12:27:57 +00:00
|
|
|
private:
|
2018-02-12 13:24:11 +00:00
|
|
|
//SQL Instance variables.
|
2018-02-02 12:27:57 +00:00
|
|
|
string filename;
|
|
|
|
sqlite3* db;
|
2018-02-12 13:24:11 +00:00
|
|
|
bool dbIsOpen;
|
2018-02-02 12:27:57 +00:00
|
|
|
int returnCode;
|
|
|
|
string sql;
|
|
|
|
char* errorMsg;
|
|
|
|
|
2018-03-30 12:33:48 +00:00
|
|
|
//Data tracking.
|
|
|
|
unsigned long queryCount;
|
|
|
|
|
2018-02-02 12:27:57 +00:00
|
|
|
void openConnection();
|
2018-03-02 10:30:16 +00:00
|
|
|
std::string combineVector(std::vector<std::string> strings, std::string mid);
|
2018-02-02 12:27:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // DATABASE_H
|