Shift to master. Not done but database abstraction is nearly done. #3
			
				
			
		
		
		
	
							
								
								
									
										2
									
								
								main.cpp
								
								
								
								
							
							
						
						
									
										2
									
								
								main.cpp
								
								
								
								
							| 
						 | 
				
			
			@ -32,6 +32,8 @@ int main(int argc, char *argv[])
 | 
			
		|||
 | 
			
		||||
	recipeDB.executeSQL("SELECT * FROM recipeIngredient;").printData();
 | 
			
		||||
 | 
			
		||||
	Recipe reloadRec = recipeDB.retrieveRecipe("Example");
 | 
			
		||||
 | 
			
		||||
	w.loadFromRecipe(rec);
 | 
			
		||||
 | 
			
		||||
	return a.exec();
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -13,7 +13,7 @@ ResultTable Database::executeSQL(string statement){
 | 
			
		|||
	sqlite3_stmt* stmt;
 | 
			
		||||
	this->sql = statement;
 | 
			
		||||
	this->returnCode = sqlite3_prepare_v2(this->db, statement.c_str(), -1, &stmt, NULL);
 | 
			
		||||
	ResultTable t;
 | 
			
		||||
	ResultTable t(statement);
 | 
			
		||||
	if (this->returnCode != SQLITE_OK){
 | 
			
		||||
		fprintf(stderr, "Unable to successfully prepare SQL statement. Error code: %d\n\tError Message: %s\n", this->returnCode, sqlite3_errmsg(this->db));
 | 
			
		||||
		return t;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -93,6 +93,16 @@ bool RecipeDatabase::storeImage(QImage image, int recipeId){
 | 
			
		|||
	return FileUtils::saveImage(recipeId, image);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
Recipe RecipeDatabase::retrieveRecipe(string name){
 | 
			
		||||
	ResultTable t = this->selectFrom("recipe", "*", "name="+surroundString(name, "'"));
 | 
			
		||||
	if (t.isEmpty()){
 | 
			
		||||
		fprintf(stderr, "Error: No recipe with name %s found!\n", name.c_str());
 | 
			
		||||
		return Recipe();
 | 
			
		||||
	}
 | 
			
		||||
	t.printData();
 | 
			
		||||
	return Recipe();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void RecipeDatabase::ensureTablesExist(){
 | 
			
		||||
	//Make sure that foreign keys are enabled.
 | 
			
		||||
	this->executeSQL("PRAGMA foreign_keys = ON;");
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -27,7 +27,7 @@ class RecipeDatabase : public Database
 | 
			
		|||
		bool storeInstruction(Instruction instruction, int recipeId);
 | 
			
		||||
		bool storeImage(QImage image, int recipeId);
 | 
			
		||||
 | 
			
		||||
		vector<Recipe> retrieveRecipe(string name);
 | 
			
		||||
		Recipe retrieveRecipe(string name);
 | 
			
		||||
	private:
 | 
			
		||||
 | 
			
		||||
		//Utility methods.
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -4,6 +4,10 @@ ResultTable::ResultTable(){
 | 
			
		|||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
ResultTable::ResultTable(string query){
 | 
			
		||||
	this->originalQuery = query;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ResultTable::extractData(sqlite3_stmt *stmt){
 | 
			
		||||
	this->values.clear();
 | 
			
		||||
	int res = sqlite3_step(stmt);
 | 
			
		||||
| 
						 | 
				
			
			@ -30,7 +34,7 @@ void ResultTable::printData(){
 | 
			
		|||
		printf("Result table is empty.\n");
 | 
			
		||||
		return;
 | 
			
		||||
	}
 | 
			
		||||
	printf("Printing table: %d by %d\n", this->rowCount(), this->columnCount());
 | 
			
		||||
	printf("Printing table: [%d x %d]\t%s\n", this->rowCount(), this->columnCount(), this->originalQuery.c_str());
 | 
			
		||||
	for (unsigned int row = 0; row < this->rowCount(); row++){
 | 
			
		||||
		for (unsigned int col = 0; col < this->columnCount(); col++){
 | 
			
		||||
			printf("| %s ", this->values[row][col].c_str());
 | 
			
		||||
| 
						 | 
				
			
			@ -56,6 +60,10 @@ int ResultTable::getReturnCode(){
 | 
			
		|||
	return this->queryCode;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
string ResultTable::getOriginalQuery(){
 | 
			
		||||
	return this->originalQuery;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
unsigned int ResultTable::columnCount(){
 | 
			
		||||
	if (this->isEmpty()){
 | 
			
		||||
		return 0;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -17,6 +17,8 @@ class ResultTable
 | 
			
		|||
	public:
 | 
			
		||||
		//Constructs an empty table.
 | 
			
		||||
		ResultTable();
 | 
			
		||||
		//Constructs a table with the original query saved.
 | 
			
		||||
		ResultTable(string query);
 | 
			
		||||
 | 
			
		||||
		//Gets all the data from the result set and stores it internally as strings.
 | 
			
		||||
		void extractData(sqlite3_stmt* stmt);
 | 
			
		||||
| 
						 | 
				
			
			@ -28,11 +30,13 @@ class ResultTable
 | 
			
		|||
		bool isEmpty();
 | 
			
		||||
		string valueAt(unsigned int row, unsigned int col);
 | 
			
		||||
		int getReturnCode();
 | 
			
		||||
		string getOriginalQuery();
 | 
			
		||||
		unsigned int columnCount();
 | 
			
		||||
		unsigned int rowCount();
 | 
			
		||||
	private:
 | 
			
		||||
		vector<vector<string>> values;
 | 
			
		||||
		int queryCode;
 | 
			
		||||
		string originalQuery;
 | 
			
		||||
 | 
			
		||||
		//Utility methods.
 | 
			
		||||
		string convertToString(sqlite3_value* val);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue