-
Notifications
You must be signed in to change notification settings - Fork 0
/
SQL.cs
198 lines (180 loc) · 6.5 KB
/
SQL.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace TournamentManager
{
class SQL
{
static SqlConnection conn;
public static List<object>[] GetPlayersAndTeams()
{
return Select(@"SELECT players.id, players.name, teams.id, teams.name, FROM players
JOIN playersteams ON ( players.id = playersteams.player_id)
JOIN teams ON ( teams.id = playersteams.team_id)", 4);
}
static void Connect()
{
try
{
string dir = Directory.GetCurrentDirectory();
string s = Properties.Settings.Default.databaseConnectionString;
string connectionString = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=" + dir + @"\database.mdf;Integrated Security=True";
//MessageBox.Show(s);
conn = new SqlConnection(s);
conn.Open();
}
catch
{
conn.Close();
conn.Dispose();
}
}
internal static Dictionary<int, string> GetPlayers()
{
return Select2<int, string>("select id, name from players", "id", "name");
}
public static Dictionary<int, string> GetTeams()
{
return Select2<int, string>("select id, name from teams", "id", "name");
}
public static List<object>[] GetTournaments()
{
return Select(@"select tournaments.id, tournaments.name, tournaments.begin_date, tournaments.end_date, tournaments.game_id, games.name from tournaments
JOIN games on (games.id = tournaments.game_id)",
6);
}
static Dictionary<TKey, TValue> Select2<TKey, TValue>(string text, string key, string value)
{
Connect();
SqlCommand cmd = new SqlCommand(text, conn);
Dictionary<TKey, TValue> ret = new Dictionary<TKey, TValue>();
try
{
using (DbDataReader reader = cmd.ExecuteReader())
{
if (reader.HasRows)
{
while (reader.Read())
{
TKey k = (TKey)reader[0];
//int index = reader.GetOrdinal(key);
//TKey k = (TKey)reader.GetValue(index);
//index = reader.GetOrdinal(value);
TValue v = (TValue)reader[1];
ret.Add(k, v);
}
}
}
}
catch (Exception e)
{
return null;
}
finally
{
Disconnect();
}
return ret;
}
static List<object>[] Select(string text, int count)
{
Connect();
SqlCommand cmd = new SqlCommand(text, conn);
List<object>[] ret = new List<object>[count];
for (int i = 0; i < count; ++i)
ret[i] = new List<object>();
try
{
using (DbDataReader reader = cmd.ExecuteReader())
{
if (reader.HasRows)
{
while (reader.Read())
{
for (int i = 0; i < count; ++i)
{
//int index = reader.GetOrdinal(name[i]);
//object element = reader.GetValue(index);
ret[i].Add(reader[i]);
}
}
}
}
}
catch (Exception e)
{
return null;
}
finally
{
Disconnect();
}
return ret;
}
internal static Dictionary<int, string> GetPlayerTeams(int playerID)
{
return Select2<int, string>(@"SELECT teams.id, teams.name FROM teams
JOIN playersteams ON (teams.id = playersteams.team_id)
WHERE playersteams.player_id = " + playerID.ToString(), "teams.id", "teams.name");
}
internal static Dictionary<int, string> GetTeamPlayers(int teamID)
{
return Select2<int, string>(@"SELECT players.id, players.name FROM players
JOIN playersteams ON (players.id = playersteams.player_id)
WHERE playersteams.team_id = " + teamID.ToString(), "players.id", "players.name");
}
internal static Dictionary<int, string> GetTournamentTeams(int tournamentID)
{
return Select2<int, string>(@"SELECT teams.id, teams.name FROM teams
JOIN tournamentsteams ON (teams.id = tournamentsteams.team_id)
WHERE tournamentsteams.tournament_id = " + tournamentID.ToString(), "teams.id", "teams.name");
}
internal static List<object>[] GetMatches(int tournamentID)
{
return Select(@"SELECT matches.id, matches.team1score, matches.team2score, matches.team1_id, team1.name,
matches.team2_id, team2.name, matchformats.id, matchformats.name, matchcategories.id, matchcategories.name FROM matches
JOIN matchformats ON (matchformats.id = matches.match_format_id)
JOIN teams team1 ON (matches.team1_id = team1.id)
JOIN teams team2 ON (matches.team2_id = team2.id)
JOIN matchcategories ON (matches.match_category_id = matchcategories.id)
WHERE matches.tournament_id = " + tournamentID.ToString(), 11);
}
static void Disconnect()
{
conn.Close();
conn.Dispose();
}
public static bool AddPlayer(string name)
{
return ExecuteNonQuery("insert into players (name) values (@sub);", name, "@sub");
}
static bool ExecuteNonQuery(string text, string str, string sub)
{
Connect();
SqlCommand cmd = new SqlCommand(text, conn);
cmd.Parameters.Add(new SqlParameter(sub, str));
//cmd.Parameters[sub].Value = str;
try
{
cmd.ExecuteNonQuery();
}
catch (Exception e)
{
return false;
}
finally
{
Disconnect();
}
return true;
}
}
}