private void ExecuteInsertGameData(DbGame game) { game.IsSynchronised = true; LocalConnection.Update(game); Guid gameId = game.Id; List <DbAttackGoal> goals = new List <DbAttackGoal>(); List <DbAttack> attacks = new List <DbAttack>(); List <DbAttackRebound> rebounds = new List <DbAttackRebound>(); List <DbAttackShot> shots = new List <DbAttackShot>(); MySqlCommand remoteCommand = new MySqlCommand(); MySqlTransaction transaction = RemoteConnection.BeginTransaction(); remoteCommand.Transaction = transaction; try { var localAttacks = LocalConnection; string query = "SELECT * FROM Attack WHERE GameId = ?"; SQLiteCommand command = LocalConnection.CreateCommand(query, gameId); attacks = command.ExecuteQuery <DbAttack>(); string getGoalsQuery = "SELECT ag.* FROM Attack a" + " JOIN AttackGoal ag" + " on a.GoalId = ag.Id" + " WHERE a.GameId = ? AND a.IsSynchronised = 0"; command = LocalConnection.CreateCommand(getGoalsQuery, gameId); goals = command.ExecuteQuery <DbAttackGoal>(); string insertGoalsQuery = CreateInsertStatement(goals, "Attack_Goal"); string insertAttacksQuery = CreateInsertStatement(attacks, "Attack"); string localShotsQuery = "SELECT ash.* FROM Attack a" + " join AttackShot ash" + " on a.Id = ash.AttackId" + " WHERE a.GameId = ? AND a.IsSynchronised = 0"; command = LocalConnection.CreateCommand(localShotsQuery, gameId); shots = command.ExecuteQuery <DbAttackShot>(); string insertShotsQuery = CreateInsertStatement(shots, "Attack_Shot"); string localReboundsQuery = "SELECT ash.* FROM Attack a" + " join AttackShot ash " + " on a.Id = ash.AttackId" + " WHERE a.GameId = ? AND a.IsSynchronised = 0"; command = LocalConnection.CreateCommand(localReboundsQuery, gameId); rebounds = command.ExecuteQuery <DbAttackRebound>(); string insertReboundsQuery = CreateInsertStatement(rebounds, "Attack_Rebound"); remoteCommand.Connection = RemoteConnection; remoteCommand.CommandText = insertGoalsQuery; remoteCommand.ExecuteNonQuery(); remoteCommand.CommandText = insertAttacksQuery; remoteCommand.ExecuteNonQuery(); remoteCommand.CommandText = insertReboundsQuery; remoteCommand.ExecuteNonQuery(); remoteCommand.CommandText = insertShotsQuery; remoteCommand.ExecuteNonQuery(); transaction.Commit(); goals.ForEach(g => g.IsSynchronised = true); attacks.ForEach(a => a.IsSynchronised = true); rebounds.ForEach(r => r.IsSynchronised = true); shots.ForEach(s => s.IsSynchronised = true); LocalConnection.UpdateAll(goals); LocalConnection.UpdateAll(attacks); LocalConnection.UpdateAll(rebounds); LocalConnection.UpdateAll(shots); } catch (Exception e) { transaction.Rollback(); throw new Exception(e.Message); } }
public void InsertAttack(IAttack attack) { bool isGoal = attack.Goal != null; string attackGoalInsert = string.Empty; if (isGoal) { attackGoalInsert = CreateInsertStatement(attack.Goal, "Attack_Goal"); } bool insertShot = attack.Shots.Count > 0; string attackInsert = CreateInsertStatement(attack.DbAttack, "Attack"); string attackShotInsert = string.Empty; if (insertShot) { attackShotInsert = CreateInsertStatement(attack.Shots, "Attack_Shot"); } string attackReboundInsert = string.Empty; bool insertRebounds = attack.Rebounds.Count > 0; if (insertRebounds) { CreateInsertStatement(attack.Rebounds, "Attack_Rebound"); } MySqlCommand command = new MySqlCommand(); command.Connection = RemoteConnection; MySqlTransaction transaction = RemoteConnection.BeginTransaction(); command.Transaction = transaction; try { if (isGoal) { command.CommandText = attackGoalInsert; command.ExecuteNonQuery(); } command.CommandText = attackInsert; command.ExecuteNonQuery(); if (insertShot) { command.CommandText = attackShotInsert; command.ExecuteNonQuery(); } if (insertRebounds) { command.CommandText = attackReboundInsert; command.ExecuteNonQuery(); } transaction.Commit(); } catch (Exception e) { transaction.Rollback(); Console.WriteLine(e.Message); } }