RecipeDB/model/database/database.h

66 lines
1.6 KiB
C
Raw Normal View History

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