-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatabase.cs
135 lines (109 loc) · 3.51 KB
/
Database.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
using System.Collections.Generic;
using MySqlConnector;
namespace SponsorBoi
{
internal static class Database
{
private static string connectionString = "";
public struct SponsorEntry
{
public ulong discordID;
public string githubID;
public SponsorEntry(MySqlDataReader reader)
{
this.discordID = reader.GetUInt64("discord_id");
this.githubID = reader.GetString("github_id");
}
}
public static void Initialize()
{
connectionString = "server=" + Config.hostName +
";database=" + Config.database +
";port=" + Config.port +
";userid=" + Config.username +
";password=" + Config.password;
using MySqlConnection c = GetConnection();
c.Open();
MySqlCommand createTable = new MySqlCommand(
"CREATE TABLE IF NOT EXISTS sponsors(" +
"discord_id BIGINT UNSIGNED NOT NULL UNIQUE," +
"github_id VARCHAR(256) NOT NULL UNIQUE)",
c);
createTable.ExecuteNonQuery();
}
private static MySqlConnection GetConnection()
{
return new MySqlConnection(connectionString);
}
public static bool TryAddSponsor(SponsorEntry sponsorEntry)
{
using MySqlConnection c = GetConnection();
c.Open();
MySqlCommand cmd = new MySqlCommand(@"INSERT INTO sponsors (discord_id, github_id) VALUES (@discord_id, @github_id);", c);
cmd.Parameters.AddWithValue("@discord_id", sponsorEntry.discordID);
cmd.Parameters.AddWithValue("@github_id", sponsorEntry.githubID);
int output = cmd.ExecuteNonQuery();
return output > 0;
}
public static List<SponsorEntry> GetAllSponsors()
{
using MySqlConnection c = GetConnection();
c.Open();
MySqlCommand selection = new MySqlCommand(@"SELECT * FROM sponsors", c);
MySqlDataReader results = selection.ExecuteReader();
List<SponsorEntry> sponsors = new List<SponsorEntry>();
while (results.Read())
{
sponsors.Add(new SponsorEntry(results));
}
results.Close();
return sponsors;
}
public static bool TryGetSponsor(string githubID, out SponsorEntry sponsorEntry)
{
using MySqlConnection c = GetConnection();
c.Open();
MySqlCommand selection = new MySqlCommand(@"SELECT * FROM sponsors WHERE github_id=@github_id", c);
selection.Parameters.AddWithValue("@github_id", githubID);
selection.Prepare();
MySqlDataReader results = selection.ExecuteReader();
if (!results.Read())
{
sponsorEntry = new SponsorEntry();
results.Close();
return false;
}
sponsorEntry = new SponsorEntry(results);
results.Close();
return true;
}
public static bool TryGetSponsor(ulong discordID, out SponsorEntry sponsorEntry)
{
using MySqlConnection c = GetConnection();
c.Open();
MySqlCommand selection = new MySqlCommand(@"SELECT * FROM sponsors WHERE discord_id=@discord_id", c);
selection.Parameters.AddWithValue("@discord_id", discordID);
selection.Prepare();
MySqlDataReader results = selection.ExecuteReader();
if (!results.Read())
{
sponsorEntry = new SponsorEntry();
results.Close();
return false;
}
sponsorEntry = new SponsorEntry(results);
results.Close();
return true;
}
public static bool TryRemoveSponsor(ulong userID)
{
using MySqlConnection c = GetConnection();
c.Open();
MySqlCommand deletion = new MySqlCommand(@"DELETE FROM sponsors WHERE discord_id=@discord_id", c);
deletion.Parameters.AddWithValue("@discord_id", userID);
deletion.Prepare();
int output = deletion.ExecuteNonQuery();
return output > 0;
}
}
}