Make more things use Database.Backend

This commit is contained in:
UnknownShadow200 2016-09-02 13:19:48 +10:00
parent 6d55e90700
commit 13cd3a56b3
7 changed files with 39 additions and 17 deletions

View File

@ -65,8 +65,7 @@ namespace MCGalaxy.Commands {
//safe against SQL injections because no user input is given here //safe against SQL injections because no user input is given here
if (num == -1) { if (num == -1) {
string syntax = Server.useMySQL ? "TRUNCATE TABLE `Inbox" + p.name + "`" : "DELETE FROM `Inbox" + p.name + "`"; Database.Backend.ClearTable("Inbox" + p.name);
Database.Execute(syntax);
} else { } else {
DataRow row = Inbox.Rows[num]; DataRow row = Inbox.Rows[num];
string syntax = "DELETE FROM `Inbox" + p.name + "` WHERE PlayerFrom=@0 AND TimeSent=@1"; string syntax = "DELETE FROM `Inbox" + p.name + "` WHERE PlayerFrom=@0 AND TimeSent=@1";

View File

@ -38,7 +38,7 @@ namespace MCGalaxy.Commands {
message = parts[1]; message = parts[1];
//DB //DB
if (message.Length >= 256 && Server.useMySQL) { if (message.Length >= 256 && Database.Backend.EnforcesTextLength) {
Player.Message(p, "Message was too long. It has been trimmed to:"); Player.Message(p, "Message was too long. It has been trimmed to:");
Player.Message(p, message.Substring(0, 255)); Player.Message(p, message.Substring(0, 255));
message = message.Substring(0, 255); message = message.Substring(0, 255);

View File

@ -42,10 +42,7 @@ namespace MCGalaxy.Commands.World {
if (args[0] == "clear") { if (args[0] == "clear") {
Player.Message(p, "Clearing &cALL %Sblock changes for &d{0}...", lvl.name); Player.Message(p, "Clearing &cALL %Sblock changes for &d{0}...", lvl.name);
if (Server.useMySQL) Database.Backend.ClearTable("Block" + lvl.name);
Database.Execute("TRUNCATE TABLE `Block" + lvl.name + "`");
else
Database.Execute("DELETE FROM `Block" + lvl.name + "`");
Player.Message(p, "Cleared &cALL %Sblock changes for &d" + lvl.name); Player.Message(p, "Cleared &cALL %Sblock changes for &d" + lvl.name);
} else if (args[0] == "disable") { } else if (args[0] == "disable") {
lvl.UseBlockDB = false; lvl.UseBlockDB = false;

View File

@ -25,6 +25,9 @@ namespace MCGalaxy.SQL {
/// (such as database name or file location) </summary> /// (such as database name or file location) </summary>
public abstract string ConnectionString { get; } public abstract string ConnectionString { get; }
/// <summary> Whether this backend enforces the character length in VARCHAR columns. </summary>
public abstract bool EnforcesTextLength { get; }
/// <summary> Returns a new BulkTransaction instance, which can be used to execute /// <summary> Returns a new BulkTransaction instance, which can be used to execute
/// many sql statements as one single transaction. </summary> /// many sql statements as one single transaction. </summary>
public abstract BulkTransaction CreateBulk(); public abstract BulkTransaction CreateBulk();
@ -42,5 +45,11 @@ namespace MCGalaxy.SQL {
/// <summary> Returns whether a table (case sensitive) exists by that name. </summary> /// <summary> Returns whether a table (case sensitive) exists by that name. </summary>
public abstract bool TableExists(string table); public abstract bool TableExists(string table);
/// <summary> Renames the source table to the given name. </summary>
public abstract void RenameTable(string srcTable, string dstTable);
/// <summary> Removes all entries from the given table. </summary>
public abstract void ClearTable(string table);
} }
} }

View File

@ -31,6 +31,7 @@ namespace MCGalaxy.SQL {
get { return String.Format(connFormat, Server.MySQLHost, Server.MySQLPort, get { return String.Format(connFormat, Server.MySQLHost, Server.MySQLPort,
Server.MySQLUsername, Server.MySQLPassword, Server.DatabasePooling); } Server.MySQLUsername, Server.MySQLPassword, Server.DatabasePooling); }
} }
public override bool EnforcesTextLength { get { return true; } }
public override BulkTransaction CreateBulk() { public override BulkTransaction CreateBulk() {
return new MySQLBulkTransaction(ConnectionString); return new MySQLBulkTransaction(ConnectionString);
@ -50,5 +51,15 @@ namespace MCGalaxy.SQL {
using (DataTable results = Database.Fill(syntax, table, Server.MySQLDatabaseName)) using (DataTable results = Database.Fill(syntax, table, Server.MySQLDatabaseName))
return results.Rows.Count > 0; return results.Rows.Count > 0;
} }
public override void RenameTable(string srcTable, string dstTable) {
string syntax = "RENAME TABLE `" + srcTable + "` TO `" + dstTable + "`";
Database.Execute(syntax);
}
public override void ClearTable(string table) {
string syntax = "TRUNCATE TABLE `" + table + "`";
Database.Execute(syntax);
}
} }
} }

View File

@ -30,6 +30,7 @@ namespace MCGalaxy.SQL {
public override string ConnectionString { public override string ConnectionString {
get { return String.Format(connFormat, Server.DatabasePooling); } get { return String.Format(connFormat, Server.DatabasePooling); }
} }
public override bool EnforcesTextLength { get { return false; } }
public override BulkTransaction CreateBulk() { public override BulkTransaction CreateBulk() {
return new SQLiteBulkTransaction(ConnectionString); return new SQLiteBulkTransaction(ConnectionString);
@ -49,5 +50,15 @@ namespace MCGalaxy.SQL {
using (DataTable results = Database.Fill(syntax, table)) using (DataTable results = Database.Fill(syntax, table))
return results.Rows.Count > 0; return results.Rows.Count > 0;
} }
public override void RenameTable(string srcTable, string dstTable) {
string syntax = "ALTER TABLE `" + srcTable + "` RENAME TO `" + dstTable + "`";
Database.Execute(syntax);
}
public override void ClearTable(string table) {
string syntax = "DELETE FROM `" + table + "`";
Database.Execute(syntax);
}
} }
} }

View File

@ -55,24 +55,19 @@ namespace MCGalaxy {
} }
BotsFile.MoveBots(src, dst); BotsFile.MoveBots(src, dst);
//safe against SQL injections because foundLevel is being checked and, Database.Backend.RenameTable("Block" + src, "Block" + dst);
//newName is being split and partly checked on illegal characters reserved for Windows.
string syntax = Server.useMySQL
? "RENAME TABLE `{2}{0}` TO `{2}{1}`" : "ALTER TABLE `{2}{0}` RENAME TO `{2}{1}`";
Database.Execute(String.Format(syntax, src, dst, "Block"));
object locker = ThreadSafeCache.DBCache.Get(src); object locker = ThreadSafeCache.DBCache.Get(src);
lock (locker) { lock (locker) {
if (Database.TableExists("Portals" + src)) { if (Database.TableExists("Portals" + src)) {
Database.Execute(String.Format(syntax, src, dst, "Portals")); Database.Backend.RenameTable("Portals" + src, "Portals" + dst);
string updateSyntax = "UPDATE `Portals" + dst + "` SET ExitMap=@1 WHERE ExitMap=@0"; string updateSyntax = "UPDATE `Portals" + dst + "` SET ExitMap=@1 WHERE ExitMap=@0";
Database.Execute(updateSyntax, src, dst); Database.Execute(updateSyntax, src, dst);
} }
if (Database.TableExists("Messages" + src)) { if (Database.TableExists("Messages" + src)) {
Database.Execute(String.Format(syntax, src, dst, "Messages")); Database.Backend.RenameTable("Messages" + src, "Messages" + dst);
} }
if (Database.TableExists("Zone" + src)) { if (Database.TableExists("Zone" + src)) {
Database.Execute(String.Format(syntax, src, dst, "Zone")); Database.Backend.RenameTable("Zone" + src, "Zone" + dst);
} }
} }
} }