diff --git a/MCGalaxy/Database/Backends/MySQL.cs b/MCGalaxy/Database/Backends/MySQL.cs index 6168679c1..62b253b72 100644 --- a/MCGalaxy/Database/Backends/MySQL.cs +++ b/MCGalaxy/Database/Backends/MySQL.cs @@ -78,6 +78,17 @@ namespace MCGalaxy.SQL { } } + public override List ColumnNames(string table) { + ValidateTable(table); + using (DataTable results = Database.Fill("DESCRIBE `" + table + "`")) { + List columns = new List(results.Rows.Count); + foreach (DataRow row in results.Rows) { + columns.Add(row["Field"].ToString()); + } + return columns; + } + } + public override void RenameTable(string srcTable, string dstTable) { ValidateTable(srcTable); ValidateTable(dstTable); diff --git a/MCGalaxy/Database/Backends/SQLite.cs b/MCGalaxy/Database/Backends/SQLite.cs index 7c39979fb..ee52423f6 100644 --- a/MCGalaxy/Database/Backends/SQLite.cs +++ b/MCGalaxy/Database/Backends/SQLite.cs @@ -75,6 +75,17 @@ namespace MCGalaxy.SQL { } } + public override List ColumnNames(string table) { + ValidateTable(table); + using (DataTable results = Database.Fill("PRAGMA table_info(`" + table + "`)")) { + List columns = new List(results.Rows.Count); + foreach (DataRow row in results.Rows) { + columns.Add(row["name"].ToString()); + } + return columns; + } + } + public override void RenameTable(string srcTable, string dstTable) { ValidateTable(srcTable); ValidateTable(dstTable); diff --git a/MCGalaxy/Database/IDatabaseBackend.cs b/MCGalaxy/Database/IDatabaseBackend.cs index b5fce51a8..6db0ed398 100644 --- a/MCGalaxy/Database/IDatabaseBackend.cs +++ b/MCGalaxy/Database/IDatabaseBackend.cs @@ -63,6 +63,9 @@ namespace MCGalaxy.SQL { /// Returns a list of all tables in this database. public abstract List AllTables(); + /// Returns a list of the column names in the given table. + public abstract List ColumnNames(string table); + /// Renames the source table to the given name. public abstract void RenameTable(string srcTable, string dstTable); diff --git a/MCGalaxy/Server/Server.DB.cs b/MCGalaxy/Server/Server.DB.cs index f5114abdd..79fd32bd8 100644 --- a/MCGalaxy/Server/Server.DB.cs +++ b/MCGalaxy/Server/Server.DB.cs @@ -16,6 +16,7 @@ permissions and limitations under the Licenses. */ using System; +using System.Collections.Generic; using System.Data; using System.IO; using MCGalaxy.SQL; @@ -81,29 +82,22 @@ namespace MCGalaxy { // Here, since SQLite is a NEW thing from 5.3.0.0, we do not have to check for existing tables in SQLite. if (!ServerConfig.UseMySQL) return; // Check if the color column exists. - DataTable colorExists = Database.Fill("SHOW COLUMNS FROM Players WHERE `Field`='color'"); - if (colorExists.Rows.Count == 0) { + + List columns = Database.Backend.ColumnNames("Players"); + if (columns.Count == 0) return; + + if (!columns.CaselessContains("Color")) { Database.Backend.AddColumn("Players", new ColumnDesc("color", ColumnType.VarChar, 6), "totalKicked"); } - colorExists.Dispose(); - - DataTable tcolorExists = Database.Fill("SHOW COLUMNS FROM Players WHERE `Field`='title_color'"); - if (tcolorExists.Rows.Count == 0) { + if (!columns.CaselessContains("Title_Color")) { Database.Backend.AddColumn("Players", new ColumnDesc("title_color", ColumnType.VarChar, 6), "color"); } - tcolorExists.Dispose(); - - DataTable timespent = Database.Fill("SHOW COLUMNS FROM Players WHERE `Field`='TimeSpent'"); - if (timespent.Rows.Count == 0) { + if (!columns.CaselessContains("TimeSpent")) { Database.Backend.AddColumn("Players", new ColumnDesc("TimeSpent", ColumnType.VarChar, 20), "totalKicked"); } - timespent.Dispose(); - - DataTable totalCuboided = Database.Fill("SHOW COLUMNS FROM Players WHERE `Field`='totalCuboided'"); - if (totalCuboided.Rows.Count == 0) { + if (!columns.CaselessContains("TotalCuboided")) { Database.Backend.AddColumn("Players", new ColumnDesc("totalCuboided", ColumnType.Int64), "totalBlocks"); } - totalCuboided.Dispose(); } } } \ No newline at end of file