This repository has been archived by the owner on Jan 23, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
DBManager.cs
151 lines (134 loc) · 5.55 KB
/
DBManager.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
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using Logger = Rocket.Core.Logging.Logger;
namespace ExPresidents.Loadout
{
public class DBManager
{
private bool DebugMode;
#region Schemas
public DBManager()
{
CheckSchema();
}
private void CheckSchema()
{
using (MySqlConnection Connection = CreateConnection())
{
DebugMode = Loadout.Instance.Configuration.Instance.DebugMode;
if (DebugMode)
Logger.Log("MySql connection created.");
Connection.Open();
using (MySqlCommand Command = Connection.CreateCommand())
{
Command.CommandText = "show tables like 'loadout'";
if (Command.ExecuteScalar() == null)
{
Command.CommandText = "CREATE TABLE `loadout`" +
" (`servername` varchar(64) NOT NULL," +
"`dictionary` BLOB NOT NULL);";
Command.ExecuteNonQuery();
if (DebugMode)
Logger.Log("No loadout table found, created one.");
}
else
if (DebugMode)
Logger.Log("Loadout table found.");
}
Connection.Close();
}
}
#endregion Schemas
#region Commands
private MySqlCommand SaveDictionaryCommand(MySqlConnection Connection, String ServerName)
{
MySqlCommand Cmd = Connection.CreateCommand();
Cmd.Parameters.AddWithValue("@servername", ServerName);
Cmd.Parameters.AddWithValue("@dictionary", BArrayManager.ToArray(Loadout.Instance.playerInvs));
Cmd.CommandText = "Insert into loadout " +
"(servername, dictionary) " +
"values " +
"(@servername, @dictionary);";
return Cmd;
}
private MySqlCommand UpdateDictionaryCommand(MySqlConnection Connection, String ServerName)
{
MySqlCommand Cmd = Connection.CreateCommand();
Cmd.Parameters.AddWithValue("@servername", ServerName);
Cmd.Parameters.AddWithValue("@dictionary", BArrayManager.ToArray(Loadout.Instance.playerInvs));
Cmd.CommandText = "Update loadout set dictionary = @dictionary where servername = @servername";
return Cmd;
}
#endregion Commands
private MySqlConnection CreateConnection()
{
Configuration config = Loadout.Instance.Configuration.Instance;
return new MySqlConnection(String.Format("SERVER={0};DATABASE={1};UID={2};PASSWORD={3};PORT={4};", config.DatabaseAddress, config.DatabaseName, config.DatabaseUsername, config.DatabasePassword, config.DatabasePort));
}
public void SaveDictionary(String ServerName)
{
using (MySqlConnection Connection = CreateConnection())
{
Connection.Open();
using (MySqlCommand Cmd = UpdateDictionaryCommand(Connection, ServerName))
{
if (Cmd.ExecuteNonQuery() == 0)
{
using (MySqlCommand Command = SaveDictionaryCommand(Connection, ServerName))
Command.ExecuteNonQuery();
if (DebugMode)
Logger.Log("No dictionary for " + ServerName + " found, creating one.");
}
Logger.Log("Dictionary saved for " + ServerName + ".");
}
Connection.Close();
}
}
public void LoadDictionary(String ServerName)
{
using (MySqlConnection Connection = CreateConnection())
{
Connection.Open();
using (MySqlCommand Cmd = Connection.CreateCommand())
{
Cmd.CommandText = "Select * from loadout where servername = " + ServerName + ";";
object Result = Cmd.ExecuteNonQuery();
using (MySqlDataReader Reader = Cmd.ExecuteReader())
{
if (Reader.HasRows)
{
if (Reader.Read())
Loadout.Instance.playerInvs = (Dictionary<ulong, LoadoutList>)BArrayManager.ToObject((byte[])Reader.GetValue(1));
}
Reader.Close();
}
}
Connection.Close();
}
}
public bool CheckDictionary(String ServerName)
{
using (MySqlConnection Connection = CreateConnection())
{
bool retval;
Connection.Open();
using (MySqlCommand Cmd = Connection.CreateCommand())
{
Cmd.CommandText = "Select * from loadout where servername = " + ServerName + ";";
object Result = Cmd.ExecuteNonQuery();
using (MySqlDataReader Reader = Cmd.ExecuteReader())
{
if (!Reader.HasRows)
retval = false;
else
retval = true;
Reader.Close();
}
}
Connection.Close();
return retval;
}
}
}
}