Ejemplo n.º 1
0
void GameModel::SaveGame()
{
	using namespace ManaCraft::Database;

	// Game Table
	Query query = DatabaseAPI::getQuery();
	query.clear();
	query << "SELECT * FROM Game WHERE ID = " << mysqlpp::quote << id;

	if (UseQueryResult result = query.use()) {
		if (Row row = result.fetch_row()) {
			DeleteGame();
		}
	}

	try {
		query = DatabaseAPI::getQuery();
		query.clear();
		query << "INSERT INTO Game VALUES(" << mysqlpp::quote << id << ", " << mysqlpp::quote << "Another Game" << ")";
		query.execute();

		teams->SaveTeams(id);
	}
	catch (mysqlpp::BadConversion e) {
		std::cout << e.what() << "\n";
	}
	catch (mysqlpp::BadIndex e) {
		std::cout << e.what() << "\n";
	}
	catch (Exception e) {
		throw e;
	}

}
Ejemplo n.º 2
0
void GameModel::DeleteGame() {
	using namespace ManaCraft::Database;

	try {

		this->teams->DeleteTeams();

		Query query = DatabaseAPI::getQuery();
		query.clear();
		query << "DELETE FROM Game WHERE ID = " << mysqlpp::quote << this->id;
		query.execute();

		query = DatabaseAPI::getQuery();
		query.clear();
		query << "DELETE FROM Game_Teams WHERE ID = " << mysqlpp::quote << this->id;
		query.execute();

		query = DatabaseAPI::getQuery();
		query.clear();
		query << "DELETE FROM Game_Player_Towers WHERE ID = " << mysqlpp::quote << this->id;
		query.execute();
	}
	catch (mysqlpp::BadConversion e) {
		std::cout << e.what() << "\n";
	}
	catch (mysqlpp::BadIndex e) {
		std::cout << e.what() << "\n";
	}
	catch (Exception e) {
		throw e;
	}
}
Ejemplo n.º 3
0
void TestDatabase::testDelete()
{
	DatabaseAPI::connectToDatabase();

	Query query = DatabaseAPI::getQuery();
	query.clear();
	query << "DELETE FROM Test_Table WHERE ID = 1";
	query.execute();

	query = DatabaseAPI::getQuery();
	query.clear();
	query << "DELETE FROM Test_Table WHERE ID = 2";
	query.execute();
}
Ejemplo n.º 4
0
void GameModel::LoadNextID() {
	using namespace ManaCraft::Database;

	try {
		Query query = DatabaseAPI::getQuery();
		query.clear();
		query << "SELECT MAX(ID) FROM Game";

		if (UseQueryResult result = query.use()) {
			if (Row row = result.fetch_row()) {
				nextID = atoi(row[TableInfo::Game::ID].c_str());
			}
			else {	// Table is empty
				nextID = 0;
			}
		}
	}
	catch (mysqlpp::BadConversion e) {
		std::cout << e.what() << "\n";
	}
	catch (mysqlpp::BadIndex e) {
		std::cout << e.what() << "\n";
	}
	catch (Exception e) {
		throw e;
	}
}
Ejemplo n.º 5
0
std::vector<Minion*> Minion::fetchAllFromDB() {
	using namespace ManaCraft::Database;

	try {
		std::cout << "Querying Minion Table\n";

		Query query = DatabaseAPI::getQuery();
		query.clear();
		query << "SELECT * FROM Minions";

		std::vector<Minion*> minions = std::vector<Minion*>();

		if (UseQueryResult result = query.use()) {
			std::cout << "Obtained Result from Minion query\n";
			Row row;
			
			while (row = result.fetch_row()) {
				Minion* e = buildFromRow(row);
				minions.push_back(e);
			}
		}
		return minions;
	}
	catch (Exception e) {
		throw e;
	}
	return std::vector<Minion*>();
}
Ejemplo n.º 6
0
void FileUtils::loadQueriesList(std::string& filePath,
		std::vector<Query>& list) {

	// Initialize local variables
	list.clear();
	std::ifstream inputFileStream;
	Query query;

	// Open file
	inputFileStream.open(filePath.c_str(), std::fstream::in);

	// Check file
	if (inputFileStream.good() == false) {
		throw std::runtime_error(
				"Error while opening file [" + filePath + "] for reading");
	}

	// Load list from file
	while (inputFileStream >> query.name >> query.x1 >> query.y1 >> query.x2
			>> query.y2) {
		list.push_back(query);
		// Clear variable holding temporary query
		query.clear();
	}

	// Close file
	inputFileStream.close();

}
Ejemplo n.º 7
0
std::vector<Element*> Element::fetchAllFromDB() {
	using namespace ManaCraft::Database;

	try {
		std::cout << "Querying Element Table\n";

		Query query = DatabaseAPI::getQuery();
		query.clear();
		query << "SELECT * FROM Elements";

		std::vector<Element*> elements = std::vector<Element*>();
		
		if (UseQueryResult result = query.use()) {
			std::cout << "Obtained Result from Element query\n";
			Row row;
		
			while (row = result.fetch_row()) {
				Element* e = buildFromRow(row);
				elements.push_back(e);
			}
		}
		return elements;
	}
	catch (Exception e) {
		throw e;
	}

	return std::vector<Element*>();
}
Ejemplo n.º 8
0
Query* Parser::E() {
  Query* par = new Query(SIGN_OR);
  par->add(T());
  while (!token.compare("OR") || !token.compare("|")) {  // T OR E
    if (!token.compare("OR"))
      match("OR");
    else
      match("|");
    par->add(T());
  }
  if (par->size() == 1) {
    Query* ret = par->get(0);
    par->clear();
    delete par;
    return ret;
  }
  return par;
}
Ejemplo n.º 9
0
Query* Parser::T() {
  Query* par = new Query(SIGN_AND);
  par->add(S());
  while (!token.compare("AND") || !token.compare("&")) {  // S AND T
    if (!token.compare("AND"))
      match("AND");
    else
      match("&");
    par->add(S());
  }
  if (par->size() == 1) {
    Query* ret = par->get(0);
    par->clear();
    delete par;
    return ret;
  }
  return par;
}
Ejemplo n.º 10
0
GameModel* GameModel::LoadGameByID(unsigned int _id) {
	using namespace ManaCraft::Database;

	GameModel* temp = new GameModel();
	temp->id = _id;

	try {
		Query query = DatabaseAPI::getQuery();
		
		// Game Table
		query.clear();
		query << "SELECT * FROM Game WHERE ID = " << mysqlpp::quote << _id;

		if (UseQueryResult result = query.use()) {
			if (Row row = result.fetch_row()) {
				// ASSIGN VALUES HERE once there is something to assign
			}
			else {
				throw DatabaseAPI::IDNotFoundException();
			}
		}

		// Game_Teams table
		int teamIDs[2];
		query.clear();
		query << "SELECT * FROM Game_Teams WHERE GameID = " << mysqlpp::quote << _id;

		if (UseQueryResult result = query.use()) {
			for (int i = 0; i < 2; ++i) {
				if (Row row = result.fetch_row()) {
					teamIDs[i] = atoi(row[TableInfo::GameTeam::TEAM_ID].c_str());	// Save team IDs
				}
				else {
					throw DatabaseAPI::IDNotFoundException();
				}
			}
		}

		// Load teams
		temp->teams = Teams::LoadTeamsByIDs(teamIDs[0], teamIDs[1]);

		if (temp->teams == nullptr) {
			// it dun broke, throw jank
		}

		// For each team
		for (int i = 0; i < 2; ++i) {
			vector<Player*>* team = &(i == 0 ? temp->teams->Team1 : temp->teams->Team2);
			
			// For each player
			for (size_t i = 0; i < team->size(); ++i) {
				Player* player = team->at(i);

				// Load towers
				query.clear();
				query << "SELECT * FROM Game_Player_Towers WHERE GameID = " << mysqlpp::quote << _id << "AND PlayerID = " << mysqlpp::quote << player->id;

				if (UseQueryResult result = query.use()) {
					while (Row row = result.fetch_row()) {
						Tower* tower = new Tower();
						tower->id = atoi(row[TableInfo::GamePlayerTowers::TOWER_ID].c_str());
						tower->xPos = atoi(row[TableInfo::GamePlayerTowers::X_POS].c_str());
						tower->yPos = atoi(row[TableInfo::GamePlayerTowers::Y_POS].c_str());

						// Add to lists
						player->towers.push_back(tower);
						temp->towers.push_back(tower);

						// After the GameModel is loaded, base tower info, which was loaded from the DB on startup, must be passed into their respective towers
						// This cannot be done from this static implementation
					}
				}
			}

		}


		return temp;
	}
	catch (mysqlpp::BadConversion e) {
		std::cout << e.what() << "\n";
	}
	catch (mysqlpp::BadIndex e) {
		std::cout << e.what() << "\n";
	}
	catch (Exception e) {
		throw e;
	}

	delete temp;
	return nullptr;
}
Ejemplo n.º 11
0
void TestDatabase::testQuery() {
	DatabaseAPI::connectToDatabase();

	Query query = DatabaseAPI::getQuery();

	try {																		// No errors on CREATE TABLE
		query << "CREATE TABLE testTable ( num int, str varchar(4) )";
		query.execute();
		++testSuccesses;
	}
	catch (Exception e) {
		++testFailures;
	}

	try {																		// No errors on INSERT
		query.clear();
		query << "INSERT INTO testTable VALUES (" << mysqlpp::quote << 1 << ", " << mysqlpp::quote << "one" << ")";
		query.execute();
		++testSuccesses;
	}
	catch (Exception e) {
		++testFailures;
	}

	try {																		// No errors on SELECT
		query.clear();
		query << "SELECT * FROM testTable";
		UseQueryResult result = query.use();
		Row row;

		if (row = result.fetch_row()) {											// Row was inserted
			++testSuccesses;

			if (row[0] == "1") {												// Proper int inserted
				++testSuccesses;
			}
			else ++testFailures;

			if (row[1] == "one") {												// Proper string inserted
				++testSuccesses;
			}
			else ++testFailures;
		}
		else ++testFailures;
	}
	catch (Exception e) {
		++testFailures;
		std::cout << "EXC" << std::endl;
	}

	try {																		// No errors on DROP TABLE
		query.clear();
		query << "DROP TABLE testTable";
		query.execute();
		++testSuccesses;
	}
	catch (Exception e) {
		++testFailures;
	}

	std::cout << "Query tests complete. Successes: " << testSuccesses << "\tFailures: " << testFailures << std::endl;
	resetTestCounts();
}