diff options
Diffstat (limited to 'Aktywator/Bws.cs')
-rw-r--r-- | Aktywator/Bws.cs | 179 |
1 files changed, 99 insertions, 80 deletions
diff --git a/Aktywator/Bws.cs b/Aktywator/Bws.cs index 1cc5e07..4bc107f 100644 --- a/Aktywator/Bws.cs +++ b/Aktywator/Bws.cs @@ -3,9 +3,7 @@ using System.Collections.Generic; using System.Text; using System.Data.OleDb; using System.Windows.Forms; -using MySql.Data.MySqlClient; -using mydata = MySql.Data.MySqlClient.MySqlDataReader; -using data = System.Data.OleDb.OleDbDataReader; +using System.Data.OleDb; namespace Aktywator { @@ -113,7 +111,7 @@ namespace Aktywator try { string s; - data d = sql.select("SELECT DISTINCT `Section` FROM " + table + " ORDER BY 1"); + OleDbDataReader d = sql.select("SELECT DISTINCT `Section` FROM " + table + " ORDER BY 1"); d.Read(); s = d[0].ToString(); while (d.Read()) @@ -341,26 +339,47 @@ namespace Aktywator name = Common.bezOgonkow(name); if (name.Length > 18) name = name.Substring(0, 18); - string actual = sql.selectOne("SELECT Name FROM PlayerNumbers WHERE `Section`=" + section + " AND `Table`=" + table - + " AND `Direction`='" + direction + "'"); - if (actual != name) + try + { + string actual = sql.selectOne("SELECT Name FROM PlayerNumbers WHERE `Section`=" + section + " AND `Table`=" + table + + " AND `Direction`='" + direction + "'", true); + if (actual != name) + { + sql.query("UPDATE PlayerNumbers SET Name='" + name + "', Updated=TRUE WHERE `Section`=" + section + " AND `Table`=" + table + + " AND `Direction`='" + direction + "'"); + return 1; + } + else return 0; + } + catch (OleDbRowMissingException) { - sql.query("UPDATE PlayerNumbers SET Name='" + name + "', Updated=TRUE WHERE `Section`=" + section + " AND `Table`=" + table - + " AND `Direction`='" + direction + "'"); + sql.query("INSERT INTO PlayerNumbers(`Section`, `Table`, Direction, Name, Updated) VALUES(" + + section + ", " + table + ", '" + direction + "', '" + name + "', TRUE)"); return 1; } - else return 0; + } + + private int getBWSNumber(OleDbDataReader reader, int index) + { + switch (Type.GetTypeCode(reader.GetFieldType(index))) + { + case TypeCode.Int16: + return reader.GetInt16(index); + case TypeCode.Int32: + return reader.GetInt32(index); + } + throw new InvalidCastException("Unable to read numeric value from BWS field"); } public void syncNames(Tournament tournament, bool interactive, string startRounds) { int count = 0, countNew = 0, SKOK_STOLOW = 100; - data d; + OleDbDataReader d; startRounds = startRounds.Trim(); - if (tournament.type == 1) + if (tournament.type != Tournament.TYPE_TEAMY) { - if (startRounds.Length > 0) + if (tournament.type == Tournament.TYPE_PARY && startRounds.Length > 0) { d = sql.select("SELECT `Section`, `Table`, NSPair, EWPair FROM RoundData WHERE NSPair>0 AND `Round` in (" + startRounds + ")"); } @@ -372,90 +391,90 @@ namespace Aktywator } else { - d = sql.select("SELECT `Section`, `Table`, NSPair, EWPair FROM RoundData WHERE `Table`<=100"); + d = sql.select("SELECT `Section`, `Table`, NSPair, EWPair FROM RoundData WHERE `Table`<=" + SKOK_STOLOW); } - while (d.Read()) + try { - string section = d.GetInt32(0).ToString(); - string table = d.GetInt32(1).ToString(); - string ns = d.GetInt32(2).ToString(); - string ew = d.GetInt32(3).ToString(); + Dictionary<int, List<String>> names = tournament.getNameList(); - StringBuilder query = new StringBuilder(); - if (tournament.type == 1) - { - query.Append("SELECT CONCAT(SUBSTR(imie,1,1),'.',nazw) name FROM zawodnicy WHERE idp="); - query.Append(ns); - query.Append(" OR idp="); - query.Append(ew); - query.Append(" ORDER BY idp "); - if (int.Parse(ew) < int.Parse(ns)) - query.Append("DESC"); - } - else + while (d.Read()) { - query.Append("SELECT fullname NAME FROM teams WHERE id="); - query.Append(ns); - query.Append(" UNION ALL SELECT ' ' UNION ALL"); - query.Append(" SELECT fullname NAME FROM teams WHERE id="); - query.Append(ew); - query.Append(" UNION ALL SELECT ' '; "); - } - mydata n = tournament.mysql.select(query.ToString()); + string section = this.getBWSNumber(d, 0).ToString(); + string table = this.getBWSNumber(d, 1).ToString(); + int ns = this.getBWSNumber(d, 2); + int ew = this.getBWSNumber(d, 3); - try - { - n.Read(); - countNew += updateName(section, table, "N", n.IsDBNull(0) ? "" : n.GetString(0)); - if (tournament.type == 2) - countNew += updateName(section, (int.Parse(table) + SKOK_STOLOW).ToString(), "E", n.IsDBNull(0) ? "" : n.GetString(0)); - n.Read(); - countNew += updateName(section, table, "S", n.IsDBNull(0) ? "" : n.GetString(0)); - if (tournament.type == 2) - countNew += updateName(section, (int.Parse(table) + SKOK_STOLOW).ToString(), "W", n.IsDBNull(0) ? "" : n.GetString(0)); - n.Read(); - countNew += updateName(section, table, "E", n.IsDBNull(0) ? "" : n.GetString(0)); - if (tournament.type == 2) - countNew += updateName(section, (int.Parse(table) + SKOK_STOLOW).ToString(), "N", n.IsDBNull(0) ? "" : n.GetString(0)); - n.Read(); - countNew += updateName(section, table, "W", n.IsDBNull(0) ? "" : n.GetString(0)); - if (tournament.type == 2) - countNew += updateName(section, (int.Parse(table) + SKOK_STOLOW).ToString(), "S", n.IsDBNull(0) ? "" : n.GetString(0)); - - if (tournament.type == 1) count += 4; - else count += 8; - } - catch (MySqlException ee) - { - if (interactive) + try { - if (ee.ErrorCode == -2147467259) + if (!names.ContainsKey(ns)) { - DialogResult dr = MessageBox.Show("W bws-ie jest para/team (" + ns + " albo " + ew - + "), który nie istnieje w wybranym turnieju. Może to nie ten turniej?", - "Zły turniej", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Warning); - if (dr == DialogResult.Abort) return; + throw new KeyNotFoundException(ns.ToString()); } - else + countNew += updateName(section, table, "N", names[ns][0]); + countNew += updateName(section, table, "S", names[ns][1]); + count += 2; + if (tournament.type == Tournament.TYPE_TEAMY) + { + countNew += updateName(section, (int.Parse(table) + SKOK_STOLOW).ToString(), "E", names[ns][0]); + countNew += updateName(section, (int.Parse(table) + SKOK_STOLOW).ToString(), "W", names[ns][1]); + count += 2; + } + } + catch (KeyNotFoundException keyE) + { + if (interactive) { - MessageBox.Show(ee.Message, "Błąd MySQL", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Warning); + DialogResult dr = MessageBox.Show("W bws-ie jest para/team (" + keyE.Message + ")" + + ", który nie istnieje w wybranym turnieju." + + "Może to nie ten turniej?" + "\n\n" + "Kontynuować wczytywanie?", + "Zły turniej", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); + if (dr == DialogResult.No) break; + } + } + try + { + if (!names.ContainsKey(ew)) + { + throw new KeyNotFoundException(ew.ToString()); + } + countNew += updateName(section, table, "E", names[ew][0]); + countNew += updateName(section, table, "W", names[ew][1]); + count += 2; + if (tournament.type == Tournament.TYPE_TEAMY) + { + countNew += updateName(section, (int.Parse(table) + SKOK_STOLOW).ToString(), "N", names[ew][0]); + countNew += updateName(section, (int.Parse(table) + SKOK_STOLOW).ToString(), "S", names[ew][1]); + count += 2; + } + } + catch (KeyNotFoundException keyE) + { + if (interactive) + { + DialogResult dr = MessageBox.Show("W bws-ie jest para/team (" + keyE.Message + ")" + + ", który nie istnieje w wybranym turnieju." + + "Może to nie ten turniej?" + "\n\n" + "Kontynuować wczytywanie?", + "Zły turniej", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); + if (dr == DialogResult.No) break; } } } - try + if (interactive) { - n.Close(); + MessageBox.Show("Synchronizacja zakończona!\nPrzejrzanych nazwisk: " + count + "\nZmienionych: " + countNew, + "Synchronizacja nazwisk", MessageBoxButtons.OK, MessageBoxIcon.Information); + if (sql.selectOne("SELECT BM2ShowPlayerNames FROM Settings") != "1") + MessageBox.Show("Pamiętaj żeby włączyć opcję \"pokazuj nazwiska\"!", "Brakujące ustawienie", + MessageBoxButtons.OK, MessageBoxIcon.Information); } - catch (Exception) { } } - if (interactive) + catch (Exception ee) { - MessageBox.Show("Synchronizacja zakończona!\nPrzejrzanych nazwisk: " + count + "\nZmienionych: " + countNew, - "Synchronizacja nazwisk", MessageBoxButtons.OK, MessageBoxIcon.Information); - if (sql.selectOne("SELECT BM2ShowPlayerNames FROM Settings") != "1") - MessageBox.Show("Pamiętaj żeby włączyć opcję \"pokazuj nazwiska\"!", "Brakujące ustawienie", - MessageBoxButtons.OK, MessageBoxIcon.Information); + if (interactive) + { + MessageBox.Show(ee.Message, "Błąd wczytywania nazwisk", MessageBoxButtons.OK, MessageBoxIcon.Error); + } } } |