diff --git a/Commands/other/CmdServer.cs b/Commands/other/CmdServer.cs index e4f276f8f..b5bdeb0ec 100644 --- a/Commands/other/CmdServer.cs +++ b/Commands/other/CmdServer.cs @@ -32,8 +32,8 @@ namespace MCGalaxy.Commands { public CmdServer() { } public override void Use(Player p, string message) { - string[] args = message.ToLower().Split(' '); - switch (args[0]) { + string[] args = message.Split(' '); + switch (args[0].ToLower()) { case "public": Server.pub = true; Player.Message(p, "Server is now public!"); @@ -75,7 +75,8 @@ namespace MCGalaxy.Commands { Player.Message(p, "Settings reloaded! You may need to restart the server, however."); break; case "backup": - if (args.Length == 1 || args[1] == "all") { + string type = args.Length == 1 ? "" : args[1].ToLower(); + if (type == "" || type == "all") { // Backup Everything. // Create SQL statements for this. The SQL will assume the settings for the current configuration are correct. // This means we use the currently defined port, database, user, password, and pooling. @@ -84,7 +85,7 @@ namespace MCGalaxy.Commands { // This means all folders, and files in these folders. Player.Message(p, "Server backup (Everything) started. Please wait while backup finishes."); Save(true, true, p); - } else if (args[1] == "db") { + } else if (type == "db") { // Backup database only. // Create SQL statements for this. The SQL will assume the settings for the current configuration are correct. // This means we use the currently defined port, database, user, password, and pooling. @@ -93,15 +94,16 @@ namespace MCGalaxy.Commands { // This means all folders, and files in these folders. Player.Message(p, "Server backup (Database) started. Please wait while backup finishes."); Save(false, true, p); - } else if (args[1] == "allbutdb") { + } else if (type == "allbutdb") { // Important to save everything to a .zip file (Though we can rename the extention.) // When backing up, one option is to save all non-main program files. // This means all folders, and files in these folders. Player.Message(p, "Server backup (Everything but Database) started. Please wait while backup finishes."); Save(true, false, p); - } else if (args[1] == "table") { - if (args.Length == 2) { Player.Message(p, "You need to provide the table name to backup."); return; } - if (!ValidName(p, args[2], "table")) return; + } else if (type == "table") { + if (args.Length == 2) { Player.Message(p, "You need to provide the table name to backup."); return; } + if (!ValidName(p, args[2], "table")) return; + if (!Database.TableExists(args[2])) { Player.Message(p, "Table \"{0}\" does not exist.", args[2]); return; } Player.Message(p, "Backing up table {0} started. Please wait while backup finishes.", args[2]); using (StreamWriter sql = new StreamWriter(args[2] + ".sql")) diff --git a/Database/Database.cs b/Database/Database.cs index d037d8683..2b7aa35ee 100644 --- a/Database/Database.cs +++ b/Database/Database.cs @@ -79,6 +79,16 @@ namespace MCGalaxy.SQL { return results; } } + + public static bool TableExists(string tableName) { + ParameterisedQuery query = ParameterisedQuery.Create(); + query.AddParam("@Name", tableName); + query.AddParam("@DB", Server.MySQLDatabaseName); + + string syntax = Server.useMySQL ? MySQL.TableExists : SQLite.TableExists; + using (DataTable table = fillData(query, syntax)) + return table.Rows.Count > 0; + } } public static class MySQL { //: Database //Extending for future improvement (Making it object oriented later) @@ -90,6 +100,8 @@ namespace MCGalaxy.SQL { public static void AddParams(string name, object param) { query.AddParam(name, param); } public static void ClearParams() { query.ClearParams(); } + + public const string TableExists = "SELECT * FROM information_schema.tables WHERE table_schema = @DB AND table_name = @Name"; } public static class SQLite { @@ -101,5 +113,7 @@ namespace MCGalaxy.SQL { public static void AddParams(string name, object param) { query.AddParam(name, param); } public static void ClearParams() { query.ClearParams(); } + + public const string TableExists = "SELECT name FROM sqlite_master WHERE type='table' AND name=@Name"; } } \ No newline at end of file