-
Notifications
You must be signed in to change notification settings - Fork 8
/
EBData.cs
143 lines (136 loc) · 6.19 KB
/
EBData.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
using System.Data;
using System.IO;
using Mono.Data.Sqlite;
using MySql.Data.MySqlClient;
using TShockAPI.DB;
namespace ExtendedBans
{
class EBData
{
public static string EBDB = Path.Combine(ExtendedBans.EBDir, "xbans.sqlite");
public static SqlTableCreator SQLWriter;
public static SqliteConnection DBSqlite;
public static MySqlConnection DBMysql;
public static void InitXBansDB()
{
if (!EBConfig.UseMysql)
{
string sql = Path.Combine(EBDB);
if (!File.Exists(EBDB))
{
SqliteConnection.CreateFile(EBDB);
}
DBSqlite = new SqliteConnection(string.Format("uri=file://{0},Version=3", sql));
CheckTables(DBSqlite);
DBSqlite.Dispose();
}
else
{
DBMysql = new MySqlConnection(string.Format("Data Source={0};User Id={1};Password={2}", EBConfig.MysqlHost, EBConfig.MysqlLogin, EBConfig.MysqlPassword));
string CMD = string.Format("CREATE DATABASE IF NOT EXISTS {0}", EBConfig.MysqlDatabase);
RunExec(CMD);
DBMysql = new MySqlConnection(string.Format("Database={0};Data Source={1};User Id={2};Password={3}", EBConfig.MysqlDatabase, EBConfig.MysqlHost, EBConfig.MysqlLogin, EBConfig.MysqlPassword));
CheckTables(DBMysql);
ImportToMysql();
}
}
internal static void CheckTables(IDbConnection connection)
{
SQLWriter = new SqlTableCreator(connection, new MysqlQueryCreator());
var table = new SqlTable("BannedIP",
new SqlColumn("IP", MySqlDbType.Text),
new SqlColumn("BanDate", MySqlDbType.Int32),
new SqlColumn("UnbanDate", MySqlDbType.Int32),
new SqlColumn("BannedBy", MySqlDbType.Text),
new SqlColumn("Reason", MySqlDbType.Text)
);
SQLWriter.EnsureExists(table);
table = new SqlTable("BannedPlayer",
new SqlColumn("Player", MySqlDbType.Text),
new SqlColumn("BanDate", MySqlDbType.Int32),
new SqlColumn("UnbanDate", MySqlDbType.Int32),
new SqlColumn("BannedBy", MySqlDbType.Text),
new SqlColumn("Reason", MySqlDbType.Text)
);
SQLWriter.EnsureExists(table);
table = new SqlTable("MutedPlayer",
new SqlColumn("Player", MySqlDbType.Text),
new SqlColumn("MuteDate", MySqlDbType.Int32),
new SqlColumn("UnmuteDate", MySqlDbType.Int32),
new SqlColumn("MutedBy", MySqlDbType.Text),
new SqlColumn("Reason", MySqlDbType.Text)
);
SQLWriter.EnsureExists(table);
}
public static void RunExec(string query)
{
if (EBConfig.UseMysql)
{
DBMysql.Open();
var CMD = new MySqlCommand(query, DBMysql);
CMD.ExecuteNonQuery();
DBMysql.Close();
}
else
{
DBSqlite.Query(query);
DBSqlite.Dispose();
}
}
public static MySqlDataReader RunMysqlQuery(string query)
{
MySqlDataReader result;
DBMysql.Open();
var CMD = new MySqlCommand(query, DBMysql);
result = CMD.ExecuteReader();
return result;
}
internal static void ImportToMysql()
{
string sql = Path.Combine(EBDB);
if (File.Exists(EBDB))
{
string[] baninfo = new string[5];
DBSqlite = new SqliteConnection(string.Format("uri=file://{0},Version=3", sql));
var DBQuery = DBSqlite.QueryReader("SELECT * FROM BannedIP");
while (DBQuery.Read())
{
baninfo[0] = DBQuery.Get<string>("IP");
baninfo[1] = DBQuery.Get<int>("BanDate").ToString();
baninfo[2] = DBQuery.Get<int>("UnbanDate").ToString();
baninfo[3] = DBQuery.Get<string>("BannedBy");
baninfo[4] = DBQuery.Get<string>("Reason");
RunExec("INSERT INTO BannedIP (IP, BanDate, UnbanDate, BannedBy, Reason) VALUES ('" + baninfo[0] + "', '" + int.Parse(baninfo[1]) + "', '" + int.Parse(baninfo[2]) + "', '" + baninfo[3] + "', '" + baninfo[4] + "')");
}
DBSqlite.Dispose();
DBQuery.Dispose();
DBQuery = DBSqlite.QueryReader("SELECT * FROM BannedPlayer");
while (DBQuery.Read())
{
baninfo[0] = DBQuery.Get<string>("Player");
baninfo[1] = DBQuery.Get<int>("BanDate").ToString();
baninfo[2] = DBQuery.Get<int>("UnbanDate").ToString();
baninfo[3] = DBQuery.Get<string>("BannedBy");
baninfo[4] = DBQuery.Get<string>("Reason");
RunExec("INSERT INTO BannedPlayer (Player, BanDate, UnbanDate, BannedBy, Reason) VALUES ('" + baninfo[0] + "', '" + int.Parse(baninfo[1]) + "', '" + int.Parse(baninfo[2]) + "', '" + baninfo[3] + "', '" + baninfo[4] + "')");
}
DBSqlite.Dispose();
DBQuery.Dispose();
DBQuery = DBSqlite.QueryReader("SELECT * FROM MutedPlayer");
while (DBQuery.Read())
{
baninfo[0] = DBQuery.Get<string>("Player");
baninfo[1] = DBQuery.Get<int>("MuteDate").ToString();
baninfo[2] = DBQuery.Get<int>("UnmuteDate").ToString();
baninfo[3] = DBQuery.Get<string>("BannedBy");
baninfo[4] = DBQuery.Get<string>("MutedBy");
RunExec("INSERT INTO MutedPlayer (Player, MuteDate, UnmuteDate, MutedBy, Reason) VALUES ('" + baninfo[0] + "', '" + int.Parse(baninfo[1]) + "', '" + int.Parse(baninfo[2]) + "', '" + baninfo[3] + "', '" + baninfo[4] + "')");
}
DBSqlite.Dispose();
DBSqlite.Close();
DBQuery.Dispose();
File.Delete(EBDB);
}
}
}
}