public int InsertGame(Game GameToInsert)
{
//Add a game to the database
//Check if everything is correct
if (GameToInsert.Name == null || GameToInsert.InfoXML == null || GameToInsert.ReleaseDate == null || GameToInsert.scraper_gdb_id == 0)
{
LogInstance.WriteToFile(3, "Unable to add game to the database. This is critical!", LogSource);
LogInstance.WriteToFile(3, "GameToInsert - Contents:\n" + GameToInsert.Name + "\n" + GameToInsert.ReleaseDate.ToString() + "\n" + GameToInsert.scraper_gdb_id + "\n" + GameToInsert.InfoXML, LogSource);
return 0;
}
LogInstance.WriteToFile(1, "Adding game " + GameToInsert.Name + " to the database.", LogSource);
if (!connectionopen)
{
DBConnector.Open();
connectionopen = true;
}
FbTransaction DBTransaction = DBConnector.BeginTransaction();
string XMLString = GameToInsert.InfoXML.InnerXml.Replace(GameToInsert.InfoXML.InnerXml.Replace("<data>", ""), GameToInsert.InfoXML.InnerXml.Replace("</data>", ""));
XMLString = XMLString.Replace("'", "''");
FbCommand DBCommand;
if (GameToInsert.Platform == null)
DBCommand = new FbCommand("INSERT INTO games (name_of_game, info_xml, downloaded, snatched, wanted, ReleaseDate, TGDB_ID, Platform) VALUES ('" + GameToInsert.Name + "', '" + XMLString + "', 0, 0, 1, '" + GameToInsert.ReleaseDate.Day + "/" + GameToInsert.ReleaseDate.Month + "/" + GameToInsert.ReleaseDate.Year + "','" + GameToInsert.scraper_gdb_id + "','None')", DBConnector, DBTransaction);
else
DBCommand = new FbCommand("INSERT INTO games (name_of_game, info_xml, downloaded, snatched, wanted, ReleaseDate, TGDB_ID, Platform) VALUES ('" + GameToInsert.Name + "', '" + XMLString + "', 0, 0, 1, '" + GameToInsert.ReleaseDate.Day + "/" + GameToInsert.ReleaseDate.Month + "/" + GameToInsert.ReleaseDate.Year + "','" + GameToInsert.scraper_gdb_id + "','" + GameToInsert.Platform + "')", DBConnector, DBTransaction);
DBCommand.ExecuteNonQuery();
DBTransaction.Commit();
DBCommand = new FbCommand("SELECT id FROM games WHERE name_of_game = '" + GameToInsert.Name + "'", DBConnector, DBTransaction);
FbDataReader DBReader = DBCommand.ExecuteReader();
DBReader.Read();
DBConnector.Close();
connectionopen = false;
return DBReader.GetInt32(0);
}