Can retrieve columns name for a database table now.

This commit is contained in:
UnknownShadow200 2017-08-26 11:53:28 +10:00
parent 2b245672d4
commit 26e2483c66
4 changed files with 34 additions and 15 deletions

View File

@ -78,6 +78,17 @@ namespace MCGalaxy.SQL {
} }
} }
public override List<string> ColumnNames(string table) {
ValidateTable(table);
using (DataTable results = Database.Fill("DESCRIBE `" + table + "`")) {
List<string> columns = new List<string>(results.Rows.Count);
foreach (DataRow row in results.Rows) {
columns.Add(row["Field"].ToString());
}
return columns;
}
}
public override void RenameTable(string srcTable, string dstTable) { public override void RenameTable(string srcTable, string dstTable) {
ValidateTable(srcTable); ValidateTable(srcTable);
ValidateTable(dstTable); ValidateTable(dstTable);

View File

@ -75,6 +75,17 @@ namespace MCGalaxy.SQL {
} }
} }
public override List<string> ColumnNames(string table) {
ValidateTable(table);
using (DataTable results = Database.Fill("PRAGMA table_info(`" + table + "`)")) {
List<string> columns = new List<string>(results.Rows.Count);
foreach (DataRow row in results.Rows) {
columns.Add(row["name"].ToString());
}
return columns;
}
}
public override void RenameTable(string srcTable, string dstTable) { public override void RenameTable(string srcTable, string dstTable) {
ValidateTable(srcTable); ValidateTable(srcTable);
ValidateTable(dstTable); ValidateTable(dstTable);

View File

@ -63,6 +63,9 @@ namespace MCGalaxy.SQL {
/// <summary> Returns a list of all tables in this database. </summary> /// <summary> Returns a list of all tables in this database. </summary>
public abstract List<string> AllTables(); public abstract List<string> AllTables();
/// <summary> Returns a list of the column names in the given table. </summary>
public abstract List<string> ColumnNames(string table);
/// <summary> Renames the source table to the given name. </summary> /// <summary> Renames the source table to the given name. </summary>
public abstract void RenameTable(string srcTable, string dstTable); public abstract void RenameTable(string srcTable, string dstTable);

View File

@ -16,6 +16,7 @@
permissions and limitations under the Licenses. permissions and limitations under the Licenses.
*/ */
using System; using System;
using System.Collections.Generic;
using System.Data; using System.Data;
using System.IO; using System.IO;
using MCGalaxy.SQL; 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. // 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; if (!ServerConfig.UseMySQL) return;
// Check if the color column exists. // Check if the color column exists.
DataTable colorExists = Database.Fill("SHOW COLUMNS FROM Players WHERE `Field`='color'");
if (colorExists.Rows.Count == 0) { List<string> 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"); Database.Backend.AddColumn("Players", new ColumnDesc("color", ColumnType.VarChar, 6), "totalKicked");
} }
colorExists.Dispose(); if (!columns.CaselessContains("Title_Color")) {
DataTable tcolorExists = Database.Fill("SHOW COLUMNS FROM Players WHERE `Field`='title_color'");
if (tcolorExists.Rows.Count == 0) {
Database.Backend.AddColumn("Players", new ColumnDesc("title_color", ColumnType.VarChar, 6), "color"); Database.Backend.AddColumn("Players", new ColumnDesc("title_color", ColumnType.VarChar, 6), "color");
} }
tcolorExists.Dispose(); if (!columns.CaselessContains("TimeSpent")) {
DataTable timespent = Database.Fill("SHOW COLUMNS FROM Players WHERE `Field`='TimeSpent'");
if (timespent.Rows.Count == 0) {
Database.Backend.AddColumn("Players", new ColumnDesc("TimeSpent", ColumnType.VarChar, 20), "totalKicked"); Database.Backend.AddColumn("Players", new ColumnDesc("TimeSpent", ColumnType.VarChar, 20), "totalKicked");
} }
timespent.Dispose(); if (!columns.CaselessContains("TotalCuboided")) {
DataTable totalCuboided = Database.Fill("SHOW COLUMNS FROM Players WHERE `Field`='totalCuboided'");
if (totalCuboided.Rows.Count == 0) {
Database.Backend.AddColumn("Players", new ColumnDesc("totalCuboided", ColumnType.Int64), "totalBlocks"); Database.Backend.AddColumn("Players", new ColumnDesc("totalCuboided", ColumnType.Int64), "totalBlocks");
} }
totalCuboided.Dispose();
} }
} }
} }