From 7ac352770d643fe95b511071a1e5e237a8ed3f79 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sun, 27 Sep 2020 00:16:07 +1000 Subject: [PATCH] Try to cleanup DB api - DatabaseBackend now mainly just returns SQL query strings, with Database static class responsible for actually executing them --- MCGalaxy/Blocks/Extended/MessageBlock.cs | 22 +-- MCGalaxy/Blocks/Extended/Portal.cs | 35 +++-- MCGalaxy/Commands/Chat/CmdInbox.cs | 6 +- MCGalaxy/Commands/Chat/CmdSend.cs | 6 +- MCGalaxy/Commands/Maintenance/CmdBlockDB.cs | 2 +- MCGalaxy/Commands/Maintenance/CmdInfoSwap.cs | 6 +- .../Commands/Maintenance/CmdPlayerEdit.cs | 4 +- MCGalaxy/Database/Backends/MySQL.cs | 24 ++-- MCGalaxy/Database/Backends/SQLite.cs | 25 ++-- .../Database/BlockDB/BlockDBTableDumper.cs | 4 +- MCGalaxy/Database/BlockDB/NameConverter.cs | 2 +- MCGalaxy/Database/Database.cs | 129 ++++++++++++++++-- MCGalaxy/Database/IDatabaseBackend.cs | 102 +++++--------- MCGalaxy/Database/PlayerDB.cs | 8 +- MCGalaxy/Database/PlayerData.cs | 8 +- MCGalaxy/Database/TableDumper.cs | 2 +- MCGalaxy/Economy/Economy.DB.cs | 14 +- MCGalaxy/Games/CTF/CtfGame.DB.cs | 12 +- MCGalaxy/Games/CTF/CtfGame.cs | 2 +- MCGalaxy/Games/ZombieSurvival/ZSGame.DB.cs | 16 +-- MCGalaxy/Games/ZombieSurvival/ZSGame.cs | 2 +- MCGalaxy/Levels/LevelActions.cs | 12 +- MCGalaxy/Levels/LevelDB.cs | 4 +- MCGalaxy/Levels/LevelInfo.cs | 2 +- MCGalaxy/Player/Player.Handlers.cs | 4 +- MCGalaxy/Player/Player.Login.cs | 5 +- MCGalaxy/Player/Player.cs | 10 +- MCGalaxy/Player/PlayerInfo.cs | 7 +- MCGalaxy/Server/Maintenance/Backup.cs | 2 +- MCGalaxy/Server/Server.DB.cs | 16 +-- MCGalaxy/Server/Tasks/UpgradeTasks.cs | 2 +- 31 files changed, 278 insertions(+), 217 deletions(-) diff --git a/MCGalaxy/Blocks/Extended/MessageBlock.cs b/MCGalaxy/Blocks/Extended/MessageBlock.cs index 735fd98d2..6f12463a5 100644 --- a/MCGalaxy/Blocks/Extended/MessageBlock.cs +++ b/MCGalaxy/Blocks/Extended/MessageBlock.cs @@ -112,27 +112,27 @@ namespace MCGalaxy.Blocks.Extended { List coords = new List(); if (!ExistsInDB(map)) return coords; - Database.Backend.ReadRows("Messages" + map, "X,Y,Z", coords, Portal.ReadCoords); + Database.ReadRows("Messages" + map, "X,Y,Z", coords, Portal.ReadCoords); return coords; } /// Deletes all message blocks for the given map. public static void DeleteAll(string map) { if (!ExistsInDB(map)) return; - Database.Backend.DeleteTable("Messages" + map); + Database.DeleteTable("Messages" + map); } /// Copies all message blocks from the given map to another map. public static void CopyAll(string src, string dst) { if (!ExistsInDB(src)) return; - Database.Backend.CreateTable("Messages" + dst, LevelDB.createMessages); - Database.Backend.CopyAllRows("Messages" + src, "Messages" + dst); + Database.CreateTable("Messages" + dst, LevelDB.createMessages); + Database.CopyAllRows("Messages" + src, "Messages" + dst); } /// Moves all message blocks from the given map to another map. public static void MoveAll(string src, string dst) { if (!ExistsInDB(src)) return; - Database.Backend.RenameTable("Messages" + src, "Messages" + dst); + Database.RenameTable("Messages" + src, "Messages" + dst); } @@ -149,8 +149,8 @@ namespace MCGalaxy.Blocks.Extended { /// Deletes the given message block from the given map. public static void Delete(string map, ushort x, ushort y, ushort z) { - Database.Backend.DeleteRows("Messages" + map, - "WHERE X=@0 AND Y=@1 AND Z=@2", x, y, z); + Database.DeleteRows("Messages" + map, + "WHERE X=@0 AND Y=@1 AND Z=@2", x, y, z); } /// Creates or updates the given message block in the given map. @@ -159,15 +159,15 @@ namespace MCGalaxy.Blocks.Extended { contents = Colors.Escape(contents); contents = contents.UnicodeToCp437(); - Database.Backend.CreateTable("Messages" + map, LevelDB.createMessages); + Database.CreateTable("Messages" + map, LevelDB.createMessages); int count = Database.CountRows("Messages" + map, "WHERE X=@0 AND Y=@1 AND Z=@2", x, y, z); if (count == 0) { - Database.Backend.AddRow("Messages" + map, "X, Y, Z, Message", x, y, z, contents); + Database.AddRow("Messages" + map, "X, Y, Z, Message", x, y, z, contents); } else { - Database.Backend.UpdateRows("Messages" + map, "Message=@3", - "WHERE X=@0 AND Y=@1 AND Z=@2", x, y, z, contents); + Database.UpdateRows("Messages" + map, "Message=@3", + "WHERE X=@0 AND Y=@1 AND Z=@2", x, y, z, contents); } Level lvl = LevelInfo.FindExact(map); diff --git a/MCGalaxy/Blocks/Extended/Portal.cs b/MCGalaxy/Blocks/Extended/Portal.cs index 4cd711051..2debf01db 100644 --- a/MCGalaxy/Blocks/Extended/Portal.cs +++ b/MCGalaxy/Blocks/Extended/Portal.cs @@ -90,7 +90,7 @@ namespace MCGalaxy.Blocks.Extended { List coords = new List(); if (!ExistsInDB(map)) return coords; - Database.Backend.ReadRows("Portals" + map, "EntryX,EntryY,EntryZ", coords, ReadCoords); + Database.ReadRows("Portals" + map, "EntryX,EntryY,EntryZ", coords, ReadCoords); return coords; } @@ -99,61 +99,60 @@ namespace MCGalaxy.Blocks.Extended { List exits = new List(); if (!ExistsInDB(map)) return exits; - Database.Backend.ReadRows("Portals" + map, "ExitMap,ExitX,ExitY,ExitZ", exits, ReadAllExits); + Database.ReadRows("Portals" + map, "ExitMap,ExitX,ExitY,ExitZ", exits, ReadAllExits); return exits; } /// Deletes all portals for the given map. public static void DeleteAll(string map) { if (!ExistsInDB(map)) return; - Database.Backend.DeleteTable("Portals" + map); + Database.DeleteTable("Portals" + map); } /// Copies all portals from the given map to another map. public static void CopyAll(string src, string dst) { if (!ExistsInDB(src)) return; - Database.Backend.CreateTable("Portals" + dst, LevelDB.createPortals); - Database.Backend.CopyAllRows("Portals" + src, "Portals" + dst); + Database.CreateTable("Portals" + dst, LevelDB.createPortals); + Database.CopyAllRows("Portals" + src, "Portals" + dst); // Fixup portal exists that go to the same map - Database.Backend.UpdateRows("Portals" + dst, "ExitMap=@1", "WHERE ExitMap=@0", src, dst); + Database.UpdateRows("Portals" + dst, "ExitMap=@1", "WHERE ExitMap=@0", src, dst); } /// Moves all portals from the given map to another map. public static void MoveAll(string src, string dst) { if (!ExistsInDB(src)) return; - Database.Backend.RenameTable("Portals" + src, "Portals" + dst); + Database.RenameTable("Portals" + src, "Portals" + dst); } /// Returns the exit details for the given portal in the given map. /// Returns null if the given portal does not actually exist. public static PortalExit Get(string map, ushort x, ushort y, ushort z) { - object raw = Database.Backend.ReadRows("Portals" + map, "ExitMap,ExitX,ExitY,ExitZ", - null, ReadExit, - "WHERE EntryX=@0 AND EntryY=@1 AND EntryZ=@2", x, y, z); + object raw = Database.ReadRows("Portals" + map, "ExitMap,ExitX,ExitY,ExitZ", null, ReadExit, + "WHERE EntryX=@0 AND EntryY=@1 AND EntryZ=@2", x, y, z); return (PortalExit)raw; } /// Deletes the given portal from the given map. public static void Delete(string map, ushort x, ushort y, ushort z) { - Database.Backend.DeleteRows("Portals" + map, - "WHERE EntryX=@0 AND EntryY=@1 AND EntryZ=@2", x, y, z); + Database.DeleteRows("Portals" + map, + "WHERE EntryX=@0 AND EntryY=@1 AND EntryZ=@2", x, y, z); } /// Creates or updates the given portal in the given map. public static void Set(string map, ushort x, ushort y, ushort z, ushort exitX, ushort exitY, ushort exitZ, string exitMap) { - Database.Backend.CreateTable("Portals" + map, LevelDB.createPortals); + Database.CreateTable("Portals" + map, LevelDB.createPortals); int count = Database.CountRows("Portals" + map, "WHERE EntryX=@0 AND EntryY=@1 AND EntryZ=@2", x, y, z); if (count == 0) { - Database.Backend.AddRow("Portals" + map, "EntryX, EntryY, EntryZ, ExitX, ExitY, ExitZ, ExitMap", - x, y, z, exitX, exitY, exitZ, exitMap); + Database.AddRow("Portals" + map, "EntryX, EntryY, EntryZ, ExitX, ExitY, ExitZ, ExitMap", + x, y, z, exitX, exitY, exitZ, exitMap); } else { - Database.Backend.UpdateRows("Portals" + map, "ExitMap=@6, ExitX=@3, ExitY=@4, ExitZ=@5", - "WHERE EntryX=@0 AND EntryY=@1 AND EntryZ=@2", x, y, z, - exitX, exitY, exitZ, exitMap); + Database.UpdateRows("Portals" + map, "ExitMap=@6, ExitX=@3, ExitY=@4, ExitZ=@5", + "WHERE EntryX=@0 AND EntryY=@1 AND EntryZ=@2", x, y, z, + exitX, exitY, exitZ, exitMap); } Level lvl = LevelInfo.FindExact(map); diff --git a/MCGalaxy/Commands/Chat/CmdInbox.cs b/MCGalaxy/Commands/Chat/CmdInbox.cs index 4047a972c..914a0c9c0 100644 --- a/MCGalaxy/Commands/Chat/CmdInbox.cs +++ b/MCGalaxy/Commands/Chat/CmdInbox.cs @@ -45,7 +45,7 @@ namespace MCGalaxy.Commands.Chatting { if (args.Length == 1) { p.Message("You need to provide either \"all\" or a number."); return; } else if (args[1].CaselessEq("all")) { - Database.Backend.DeleteRows("Inbox" + p.name); + Database.DeleteRows("Inbox" + p.name); p.Message("Deleted all messages."); } else { DeleteByID(p, args[1], entries); @@ -63,8 +63,8 @@ namespace MCGalaxy.Commands.Chatting { p.Message("Message #{0} does not exist.", num); } else { string[] entry = entries[num - 1]; - Database.Backend.DeleteRows("Inbox" + p.name, - "WHERE PlayerFrom=@0 AND TimeSent=@1", entry[i_from], entry[i_sent]); + Database.DeleteRows("Inbox" + p.name, + "WHERE PlayerFrom=@0 AND TimeSent=@1", entry[i_from], entry[i_sent]); p.Message("Deleted message #{0}", num); } } diff --git a/MCGalaxy/Commands/Chat/CmdSend.cs b/MCGalaxy/Commands/Chat/CmdSend.cs index 5d9d243f8..939eaa359 100644 --- a/MCGalaxy/Commands/Chat/CmdSend.cs +++ b/MCGalaxy/Commands/Chat/CmdSend.cs @@ -41,9 +41,9 @@ namespace MCGalaxy.Commands.Chatting { p.Message(message); } - Database.Backend.CreateTable("Inbox" + name, createInbox); - Database.Backend.AddRow("Inbox" + name, "PlayerFrom, TimeSent, Contents", - p.name, DateTime.Now.ToString(Database.DateFormat), message); + Database.CreateTable("Inbox" + name, createInbox); + Database.AddRow("Inbox" + name, "PlayerFrom, TimeSent, Contents", + p.name, DateTime.Now.ToString(Database.DateFormat), message); Player target = PlayerInfo.FindExact(name); p.Message("Message sent to {0}%S.", p.FormatNick(name)); diff --git a/MCGalaxy/Commands/Maintenance/CmdBlockDB.cs b/MCGalaxy/Commands/Maintenance/CmdBlockDB.cs index 6ac98939b..38372a749 100644 --- a/MCGalaxy/Commands/Maintenance/CmdBlockDB.cs +++ b/MCGalaxy/Commands/Maintenance/CmdBlockDB.cs @@ -43,7 +43,7 @@ namespace MCGalaxy.Commands.Maintenance { if (args[0].CaselessEq("clear")) { p.Message("Clearing &cALL %Sblock changes for {0}%S...", lvl.ColoredName); if (Database.TableExists("Block" + lvl.name)) - Database.Backend.DeleteTable("Block" + lvl.name); + Database.DeleteTable("Block" + lvl.name); lvl.BlockDB.DeleteBackingFile(); p.Message("Cleared &cALL %Sblock changes for " + lvl.ColoredName); } else if (args[0].CaselessEq("disable")) { diff --git a/MCGalaxy/Commands/Maintenance/CmdInfoSwap.cs b/MCGalaxy/Commands/Maintenance/CmdInfoSwap.cs index 5f05c11a2..37a4a6c6e 100644 --- a/MCGalaxy/Commands/Maintenance/CmdInfoSwap.cs +++ b/MCGalaxy/Commands/Maintenance/CmdInfoSwap.cs @@ -60,9 +60,9 @@ namespace MCGalaxy.Commands.Maintenance { int tmpNum = new Random().Next(0, 10000000); string tmpName = "-tmp" + tmpNum + "-"; - Database.Backend.UpdateRows("Players", "Name=@1", "WHERE Name=@0", dst, tmpName); // PLAYERS[dst] = tmp - Database.Backend.UpdateRows("Players", "Name=@1", "WHERE Name=@0", src, dst); // PLAYERS[src] = dst - Database.Backend.UpdateRows("Players", "Name=@1", "WHERE Name=@0", tmpName, src); // PLAYERS[tmp] = src + Database.UpdateRows("Players", "Name=@1", "WHERE Name=@0", dst, tmpName); // PLAYERS[dst] = tmp + Database.UpdateRows("Players", "Name=@1", "WHERE Name=@0", src, dst); // PLAYERS[src] = dst + Database.UpdateRows("Players", "Name=@1", "WHERE Name=@0", tmpName, src); // PLAYERS[tmp] = src } static void SwapGroups(string src, string dst, Group srcGroup, Group dstGroup) { diff --git a/MCGalaxy/Commands/Maintenance/CmdPlayerEdit.cs b/MCGalaxy/Commands/Maintenance/CmdPlayerEdit.cs index 9867d72a6..4e5c5c3d5 100644 --- a/MCGalaxy/Commands/Maintenance/CmdPlayerEdit.cs +++ b/MCGalaxy/Commands/Maintenance/CmdPlayerEdit.cs @@ -179,8 +179,8 @@ namespace MCGalaxy.Commands.Maintenance { static object ReadLong(IDataRecord record, object arg) { return record.GetInt64(0); } static long GetLong(string name, string column) { - return (long)Database.Backend.ReadRows("Players", column, null, ReadLong, - "WHERE Name=@0", name); + return (long)Database.ReadRows("Players", column, null, ReadLong, + "WHERE Name=@0", name); } static void SetInteger(Player p, string[] args, string column, int max, Player who, diff --git a/MCGalaxy/Database/Backends/MySQL.cs b/MCGalaxy/Database/Backends/MySQL.cs index 7b8ef1750..bc3e4707a 100644 --- a/MCGalaxy/Database/Backends/MySQL.cs +++ b/MCGalaxy/Database/Backends/MySQL.cs @@ -84,25 +84,21 @@ namespace MCGalaxy.SQL { static object IterateExists(IDataRecord record, object arg) { return ""; } public override bool TableExists(string table) { - ValidateTable(table); return Database.Iterate("SHOW TABLES LIKE '" + table + "'", null, IterateExists) != null; } public override List AllTables() { - return Database.GetStrings("SHOW TABLES"); + return GetStrings("SHOW TABLES"); } public override List ColumnNames(string table) { - ValidateTable(table); - return Database.GetStrings("DESCRIBE `" + table + "`"); + Database.ValidateName(table); + return GetStrings("DESCRIBE `" + table + "`"); } - public override void RenameTable(string srcTable, string dstTable) { - ValidateTable(srcTable); - ValidateTable(dstTable); - string sql = "RENAME TABLE `" + srcTable + "` TO `" + dstTable + "`"; - Database.Execute(sql, null); + public override string RenameTableSql(string srcTable, string dstTable) { + return "RENAME TABLE `" + srcTable + "` TO `" + dstTable + "`"; } protected override void CreateTableColumns(StringBuilder sql, ColumnDesc[] columns) { @@ -148,16 +144,14 @@ namespace MCGalaxy.SQL { w.WriteLine(");"); } - public override void AddColumn(string table, ColumnDesc col, string colAfter) { - ValidateTable(table); + public override string AddColumnSql(string table, ColumnDesc col, string colAfter) { string sql = "ALTER TABLE `" + table + "` ADD COLUMN " + col.Column + " " + col.FormatType(); if (colAfter.Length > 0) sql += " AFTER " + colAfter; - Database.Execute(sql, null); + return sql; } - public override void AddOrReplaceRow(string table, string columns, params object[] args) { - ValidateTable(table); - DoInsert("REPLACE INTO", table, columns, args); + public override string AddOrReplaceRowSql(string table, string columns, object[] args) { + return InsertSql("REPLACE INTO", table, columns, args); } } } diff --git a/MCGalaxy/Database/Backends/SQLite.cs b/MCGalaxy/Database/Backends/SQLite.cs index c70f35095..78a8acf9d 100644 --- a/MCGalaxy/Database/Backends/SQLite.cs +++ b/MCGalaxy/Database/Backends/SQLite.cs @@ -59,14 +59,13 @@ namespace MCGalaxy.SQL { } public override bool TableExists(string table) { - ValidateTable(table); return Database.CountRows("sqlite_master", "WHERE type='table' AND name=@0", table) > 0; } public override List AllTables() { const string sql = "SELECT name from sqlite_master WHERE type='table'"; - List tables = Database.GetStrings(sql); + List tables = GetStrings(sql); // exclude sqlite built-in database tables for (int i = tables.Count - 1; i >= 0; i--) { @@ -82,7 +81,7 @@ namespace MCGalaxy.SQL { } public override List ColumnNames(string table) { - ValidateTable(table); + Database.ValidateName(table); List columns = new List(); Database.Iterate("PRAGMA table_info(`" + table + "`)", @@ -90,11 +89,8 @@ namespace MCGalaxy.SQL { return columns; } - public override void RenameTable(string srcTable, string dstTable) { - ValidateTable(srcTable); - ValidateTable(dstTable); - string sql = "ALTER TABLE `" + srcTable + "` RENAME TO `" + dstTable + "`"; - Database.Execute(sql, null); + public override string RenameTableSql(string srcTable, string dstTable) { + return "ALTER TABLE `" + srcTable + "` RENAME TO `" + dstTable + "`"; } protected override void CreateTableColumns(StringBuilder sql, ColumnDesc[] columns) { @@ -124,7 +120,7 @@ namespace MCGalaxy.SQL { public override void PrintSchema(string table, TextWriter w) { string sql = "SELECT sql from sqlite_master WHERE tbl_name = @0 AND type = 'table'"; - List all = Database.GetStrings(sql + CaselessWhereSuffix, table); + List all = GetStrings(sql + CaselessWhereSuffix, table); for (int i = 0; i < all.Count; i++) { sql = all[i].Replace(" " + table, " `" + table + "`"); @@ -133,15 +129,12 @@ namespace MCGalaxy.SQL { } } - public override void AddColumn(string table, ColumnDesc col, string colAfter) { - ValidateTable(table); - string sql = "ALTER TABLE `" + table + "` ADD COLUMN " + col.Column + " " + col.FormatType(); - Database.Execute(sql, null); + public override string AddColumnSql(string table, ColumnDesc col, string colAfter) { + return "ALTER TABLE `" + table + "` ADD COLUMN " + col.Column + " " + col.FormatType(); } - public override void AddOrReplaceRow(string table, string columns, params object[] args) { - ValidateTable(table); - DoInsert("INSERT OR REPLACE INTO", table, columns, args); + public override string AddOrReplaceRowSql(string table, string columns, object[] args) { + return InsertSql("INSERT OR REPLACE INTO", table, columns, args); } } diff --git a/MCGalaxy/Database/BlockDB/BlockDBTableDumper.cs b/MCGalaxy/Database/BlockDB/BlockDBTableDumper.cs index 402e72335..dd1af4934 100644 --- a/MCGalaxy/Database/BlockDB/BlockDBTableDumper.cs +++ b/MCGalaxy/Database/BlockDB/BlockDBTableDumper.cs @@ -45,7 +45,7 @@ namespace MCGalaxy.DB { mapName = table.Substring("Block".Length); try { - Database.Backend.ReadRows(table, "*", null, DumpRow); + Database.ReadRows(table, "*", null, DumpRow); WriteBuffer(true); AppendCbdbFile(); SaveCbdbFile(); @@ -55,7 +55,7 @@ namespace MCGalaxy.DB { } if (errorOccurred) return; - Database.Backend.DeleteTable(table); + Database.DeleteTable(table); } object DumpRow(IDataRecord record, object arg) { diff --git a/MCGalaxy/Database/BlockDB/NameConverter.cs b/MCGalaxy/Database/BlockDB/NameConverter.cs index 5b1adab8d..f161cccc8 100644 --- a/MCGalaxy/Database/BlockDB/NameConverter.cs +++ b/MCGalaxy/Database/BlockDB/NameConverter.cs @@ -50,7 +50,7 @@ namespace MCGalaxy.DB { int i = Server.invalidIds.IndexOf(name); if (i >= 0) ids.Add(MaxPlayerID - i); - Database.Backend.ReadRows("Players", "ID", ids, ListIds, "WHERE Name=@0", name); + Database.ReadRows("Players", "ID", ids, ListIds, "WHERE Name=@0", name); return ids.ToArray(); } diff --git a/MCGalaxy/Database/Database.cs b/MCGalaxy/Database/Database.cs index 6dc8edcf6..b8b876b70 100644 --- a/MCGalaxy/Database/Database.cs +++ b/MCGalaxy/Database/Database.cs @@ -27,14 +27,13 @@ namespace MCGalaxy.SQL { /// Abstracts a SQL database management engine. public static class Database { public static IDatabaseBackend Backend; - public static bool TableExists(string table) { return Backend.TableExists(table); } - public const string DateFormat = "yyyy-MM-dd HH:mm:ss"; - + public const string DateFormat = "yyyy-MM-dd HH:mm:ss"; + static object ReadInt(IDataRecord record, object arg) { return record.GetInt32(0); } /// Counts rows in the given table. /// Optional SQL to filter which rows are counted. public static int CountRows(string table, string modifier = "", params object[] args) { - return (int)Backend.ReadRows(table, "COUNT(*)", null, ReadInt, modifier, args); + return (int)ReadRows(table, "COUNT(*)", null, ReadInt, modifier, args); } static object ReadString(IDataRecord record, object arg) { return record.GetText(0); } @@ -42,17 +41,12 @@ namespace MCGalaxy.SQL { /// Optional SQL to filter which rows are read. public static string ReadString(string table, string column, string modifier = "", params object[] args) { - return (string)Backend.ReadRows(table, column, null, ReadString, modifier, args); + return (string)ReadRows(table, column, null, ReadString, modifier, args); } internal static object ReadList(IDataRecord record, object arg) { ((List)arg).Add(record.GetText(0)); return arg; } - internal static List GetStrings(string sql, params object[] args) { - List values = new List(); - Iterate(sql, values, ReadList, args); - return values; - } internal static object ReadFields(IDataRecord record, object arg) { string[] field = new string[record.FieldCount]; @@ -66,11 +60,108 @@ namespace MCGalaxy.SQL { public static List GetRows(string table, string columns, string modifier = "", params object[] args) { List fields = new List(); - Backend.ReadRows(table, columns, fields, ReadFields, modifier, args); + ReadRows(table, columns, fields, ReadFields, modifier, args); return fields; } + #region High level table management + + /// Returns whether a table (case sensitive) exists by that name. + public static bool TableExists(string table) { + ValidateName(table); + return Backend.TableExists(table); + } + + /// Creates a new table in the database (unless it already exists). + public static void CreateTable(string table, ColumnDesc[] columns) { + ValidateName(table); + string sql = Backend.CreateTableSql(table, columns); + Execute(sql, null); + } + + /// Renames the source table to the given name. + public static void RenameTable(string srcTable, string dstTable) { + ValidateName(srcTable); + ValidateName(dstTable); + string sql = Backend.RenameTableSql(srcTable, dstTable); + Execute(sql, null); + } + + /// Completely removes the given table. + public static void DeleteTable(string table) { + ValidateName(table); + string sql = Backend.DeleteTableSql(table); + Execute(sql, null); + } + + /// Adds a new coloumn to the given table. + /// Note colAfter is only a hint - some database backends ignore this. + public static void AddColumn(string table, ColumnDesc col, string colAfter) { + ValidateName(table); + string sql = Backend.AddColumnSql(table, col, colAfter); + Execute(sql, null); + } + #endregion + + + #region High level functions + + /// Inserts/Copies all the rows from the source table into the destination table. + /// May NOT work correctly if the tables have different schema. + public static void CopyAllRows(string srcTable, string dstTable) { + ValidateName(srcTable); + ValidateName(dstTable); + string sql = Backend.CopyAllRowsSql(srcTable, dstTable); + Execute(sql, null); + } + + /// Iterates over read rows for the given table. + /// Optional SQL to filter which rows are read, + /// return rows in a certain order, etc. + public static object ReadRows(string table, string columns, object arg, + ReaderCallback callback, string modifier = "", params object[] args) { + ValidateName(table); + string sql = Backend.ReadRowsSql(table, columns, modifier); + return Iterate(sql, arg, callback, args); + } + + /// Updates rows for the given table. + /// Optional SQL to filter which rows are updated. + public static void UpdateRows(string table, string columns, + string modifier = "", params object[] args) { + ValidateName(table); + string sql = Backend.UpdateRowsSql(table, columns, modifier); + Execute(sql, args); + } + + /// Deletes rows for the given table. + /// Optional SQL to filter which rows are deleted. + public static void DeleteRows(string table, string modifier = "", params object[] args) { + ValidateName(table); + string sql = Backend.DeleteRowsSql(table, modifier); + Execute(sql, args); + } + + /// Adds a row to the given table. + public static void AddRow(string table, string columns, params object[] args) { + ValidateName(table); + string sql = Backend.AddRowSql(table, columns, args); + Execute(sql, args); + } + + /// Adds or replaces a row (same primary key) in the given table. + public static void AddOrReplaceRow(string table, string columns, params object[] args) { + ValidateName(table); + string sql = Backend.AddOrReplaceRowSql(table, columns, args); + Execute(sql, args); + } + + #endregion + + + #region Low level functions + /// Executes an SQL command that does not return any results. public static void Execute(string sql, params object[] args) { Do(sql, false, null, null, args); @@ -101,6 +192,22 @@ namespace MCGalaxy.SQL { Logger.LogError("Error executing SQL statement: " + sql, e); return arg; } + #endregion + + internal static bool ValidNameChar(char c) { + return + c > ' ' && c != '"' && c != '%' && c != '&' && + c != '\'' && c != '*' && c != '/' && c != ':' && + c != '<' && c != '>' && c != '?' && c != '\\' && + c != '`' && c != '|' && c <= '~'; + } + + internal static void ValidateName(string table) { + foreach (char c in table) { + if (ValidNameChar(c)) continue; + throw new ArgumentException("Invalid character in table name: " + c); + } + } } internal static class DatabaseExts { diff --git a/MCGalaxy/Database/IDatabaseBackend.cs b/MCGalaxy/Database/IDatabaseBackend.cs index 767fc4ce8..9233cd2a6 100644 --- a/MCGalaxy/Database/IDatabaseBackend.cs +++ b/MCGalaxy/Database/IDatabaseBackend.cs @@ -48,6 +48,12 @@ namespace MCGalaxy.SQL { protected internal virtual void ParseCreate(ref string cmd) { } + protected static List GetStrings(string sql, params object[] args) { + List values = new List(); + Database.Iterate(sql, values, Database.ReadList, args); + return values; + } + // == Higher level table management functions == @@ -60,93 +66,72 @@ namespace MCGalaxy.SQL { /// 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); + /// Returns SQL for renaming the source table to the given name. + public abstract string RenameTableSql(string srcTable, string dstTable); - /// Creates a new table in the database (unless it already exists). - public virtual void CreateTable(string table, ColumnDesc[] columns) { - ValidateTable(table); + /// Returns SQL for creating a new table (unless it already exists). + public virtual string CreateTableSql(string table, ColumnDesc[] columns) { StringBuilder sql = new StringBuilder(); sql.AppendLine("CREATE TABLE if not exists `" + table + "` ("); CreateTableColumns(sql, columns); sql.AppendLine(");"); - Database.Execute(sql.ToString(), null); + return sql.ToString(); } protected abstract void CreateTableColumns(StringBuilder sql, ColumnDesc[] columns); - /// Completely removes the given table. - public virtual void DeleteTable(string table) { - ValidateTable(table); - string sql = "DROP TABLE `" + table + "`"; - Database.Execute(sql, null); + /// Returns SQL for completely removing the given table. + public virtual string DeleteTableSql(string table) { + return "DROP TABLE `" + table + "`"; } /// Prints/dumps the table schema of the given table. public abstract void PrintSchema(string table, TextWriter w); - - // == Higher level column functions == - - /// Adds a new coloumn to the given table. + /// Returns SQL for adding a new coloumn to the given table. /// Note colAfter is only a hint - some database backends ignore this. - public abstract void AddColumn(string table, ColumnDesc col, string colAfter); + public abstract string AddColumnSql(string table, ColumnDesc col, string colAfter); // == Higher level functions == - /// Inserts/Copies all the rows from the source table into the destination table. - /// Note: This may work incorrectly if the tables have different schema. - public virtual void CopyAllRows(string srcTable, string dstTable) { - ValidateTable(srcTable); - ValidateTable(dstTable); - string sql = "INSERT INTO `" + dstTable + "` SELECT * FROM `" + srcTable + "`"; - Database.Execute(sql, null); + /// Returns SQL for copying all the rows from the source table into the destination table. + public virtual string CopyAllRowsSql(string srcTable, string dstTable) { + return "INSERT INTO `" + dstTable + "` SELECT * FROM `" + srcTable + "`"; } - /// Iterates over read rows for the given table. - /// modifier is optional SQL which can be used to read only certain rows, - /// return rows in a certain order, etc. - public virtual object ReadRows(string table, string columns, object arg, - ReaderCallback callback, string modifier = "", params object[] args) { - ValidateTable(table); + /// Returns SQL for reading rows from the given table. + public virtual string ReadRowsSql(string table, string columns, string modifier) { string sql = "SELECT " + columns + " FROM `" + table + "`"; if (modifier.Length > 0) sql += " " + modifier; - return Database.Iterate(sql, arg, callback, args); + return sql; } - /// Updates rows for the given table. - /// modifier is optional SQL which can be used to update only certain rows. - public virtual void UpdateRows(string table, string columns, - string modifier = "", params object[] args) { - ValidateTable(table); + /// Returns SQL for updating rows for the given table. + public virtual string UpdateRowsSql(string table, string columns, string modifier) { string sql = "UPDATE `" + table + "` SET " + columns; if (modifier.Length > 0) sql += " " + modifier; - Database.Execute(sql, args); + return sql; } - /// Deletes rows for the given table. - /// modifier is optional SQL which can be used to delete only certain rows. - public virtual void DeleteRows(string table, string modifier = "", params object[] args) { - ValidateTable(table); + /// Returns SQL for deleting rows for the given table. + public virtual string DeleteRowsSql(string table, string modifier) { string sql = "DELETE FROM `" + table + "`"; if (modifier.Length > 0) sql += " " + modifier; - Database.Execute(sql, args); + return sql; } - /// Adds a row to the given table. - public virtual void AddRow(string table, string columns, params object[] args) { - ValidateTable(table); - DoInsert("INSERT INTO", table, columns, args); + /// Returns SQL for adding a row to the given table. + public virtual string AddRowSql(string table, string columns, object[] args) { + return InsertSql("INSERT INTO", table, columns, args); } - /// Adds or replaces a row (same primary key) in the given table. - public abstract void AddOrReplaceRow(string table, string columns, params object[] args); + /// Returns SQL for adding or replacing a row (same primary key) in the given table. + public abstract string AddOrReplaceRowSql(string table, string columns, object[] args); - protected void DoInsert(string command, string table, - string columns, params object[] args) { - StringBuilder sql = new StringBuilder(command); + protected string InsertSql(string cmd, string table, string columns, object[] args) { + StringBuilder sql = new StringBuilder(cmd); sql.Append(" `").Append(table).Append("` "); sql.Append('(').Append(columns).Append(')'); @@ -157,22 +142,7 @@ namespace MCGalaxy.SQL { if (i < args.Length - 1) sql.Append(", "); else sql.Append(")"); } - Database.Execute(sql.ToString(), args); - } - - internal static bool ValidNameChar(char c) { - return - c > ' ' && c != '"' && c != '%' && c != '&' && - c != '\'' && c != '*' && c != '/' && c != ':' && - c != '<' && c != '>' && c != '?' && c != '\\' && - c != '`' && c != '|' && c <= '~'; - } - - protected static void ValidateTable(string name) { - foreach (char c in name) { - if (ValidNameChar(c)) continue; - throw new ArgumentException("Invalid character in table name: " + c); - } + return sql.ToString(); } } } diff --git a/MCGalaxy/Database/PlayerDB.cs b/MCGalaxy/Database/PlayerDB.cs index 33c0f367c..04a2e75be 100644 --- a/MCGalaxy/Database/PlayerDB.cs +++ b/MCGalaxy/Database/PlayerDB.cs @@ -94,7 +94,7 @@ namespace MCGalaxy.DB { public static void Update(string name, string column, string value) { - Database.Backend.UpdateRows("Players", column + "=@1", "WHERE Name=@0", name, value); + Database.UpdateRows("Players", column + "=@1", "WHERE Name=@0", name, value); } public static string FindColor(Player p) { @@ -138,9 +138,9 @@ namespace MCGalaxy.DB { static void MatchMulti(string name, string columns, object arg, ReaderCallback callback) { string suffix = Database.Backend.CaselessLikeSuffix; - Database.Backend.ReadRows("Players", columns, arg, callback, - "WHERE Name LIKE @0 ESCAPE '#' LIMIT 21" + suffix, - "%" + name.Replace("_", "#_") + "%"); + Database.ReadRows("Players", columns, arg, callback, + "WHERE Name LIKE @0 ESCAPE '#' LIMIT 21" + suffix, + "%" + name.Replace("_", "#_") + "%"); } } } \ No newline at end of file diff --git a/MCGalaxy/Database/PlayerData.cs b/MCGalaxy/Database/PlayerData.cs index d482ccb9e..11fc1ec7c 100644 --- a/MCGalaxy/Database/PlayerData.cs +++ b/MCGalaxy/Database/PlayerData.cs @@ -59,11 +59,11 @@ namespace MCGalaxy.DB { p.TimesVisited = 1; string now = DateTime.Now.ToString(Database.DateFormat); - Database.Backend.AddRow("Players", "Name, IP, FirstLogin, LastLogin, totalLogin, Title, " + - "totalDeaths, Money, totalBlocks, totalKicked, Messages, TimeSpent", - p.name, p.ip, now, now, 1, "", 0, 0, 0, 0, 0, (long)p.TotalTime.TotalSeconds); + Database.AddRow("Players", "Name, IP, FirstLogin, LastLogin, totalLogin, Title, " + + "totalDeaths, Money, totalBlocks, totalKicked, Messages, TimeSpent", + p.name, p.ip, now, now, 1, "", 0, 0, 0, 0, 0, (long)p.TotalTime.TotalSeconds); - object id = Database.Backend.ReadRows("Players", "ID", null, ReadID, "WHERE Name=@0", p.name); + object id = Database.ReadRows("Players", "ID", null, ReadID, "WHERE Name=@0", p.name); if (id != null) { p.DatabaseID = (int)id; } else { diff --git a/MCGalaxy/Database/TableDumper.cs b/MCGalaxy/Database/TableDumper.cs index e991c4639..2a51baf51 100644 --- a/MCGalaxy/Database/TableDumper.cs +++ b/MCGalaxy/Database/TableDumper.cs @@ -31,7 +31,7 @@ namespace MCGalaxy.SQL { gottenRows = false; this.sql = sql; this.table = table; - Database.Backend.ReadRows(table, "*", null, DumpRow); + Database.ReadRows(table, "*", null, DumpRow); if (!gottenRows) { sql.WriteLine("-- No data in table `{0}`!", table); diff --git a/MCGalaxy/Economy/Economy.DB.cs b/MCGalaxy/Economy/Economy.DB.cs index baa80438a..3276de79b 100644 --- a/MCGalaxy/Economy/Economy.DB.cs +++ b/MCGalaxy/Economy/Economy.DB.cs @@ -42,11 +42,11 @@ namespace MCGalaxy.Eco { } public static void LoadDatabase() { - Database.Backend.CreateTable("Economy", createEconomy); + Database.CreateTable("Economy", createEconomy); // money used to be in the Economy table, move it back to the Players table List outdated = new List(); - Database.Backend.ReadRows("Economy", "*", outdated, ListOld, "WHERE money > 0"); + Database.ReadRows("Economy", "*", outdated, ListOld, "WHERE money > 0"); if (outdated.Count == 0) return; Logger.Log(LogType.SystemActivity, "Upgrading economy stats.."); @@ -73,9 +73,9 @@ namespace MCGalaxy.Eco { } public static void UpdateStats(EcoStats stats) { - Database.Backend.AddOrReplaceRow("Economy", "player, money, total, purchase, payment, salary, fine", - stats.Player, 0, stats.TotalSpent, stats.Purchase, - stats.Payment, stats.Salary, stats.Fine); + Database.AddOrReplaceRow("Economy", "player, money, total, purchase, payment, salary, fine", + stats.Player, 0, stats.TotalSpent, stats.Purchase, + stats.Payment, stats.Salary, stats.Fine); } static EcoStats ParseStats(IDataRecord record) { @@ -100,8 +100,8 @@ namespace MCGalaxy.Eco { public static EcoStats RetrieveStats(string name) { EcoStats stats = default(EcoStats); stats.Player = name; - return (EcoStats)Database.Backend.ReadRows("Economy", "*", stats, ReadStats, - "WHERE player=@0", name); + return (EcoStats)Database.ReadRows("Economy", "*", stats, ReadStats, + "WHERE player=@0", name); } } } \ No newline at end of file diff --git a/MCGalaxy/Games/CTF/CtfGame.DB.cs b/MCGalaxy/Games/CTF/CtfGame.DB.cs index d163a88aa..f3fd09b9c 100644 --- a/MCGalaxy/Games/CTF/CtfGame.DB.cs +++ b/MCGalaxy/Games/CTF/CtfGame.DB.cs @@ -45,8 +45,8 @@ namespace MCGalaxy.Games { static CtfStats LoadStats(string name) { CtfStats stats = default(CtfStats); - return (CtfStats)Database.Backend.ReadRows("CTF", "*", stats, - ReadStats, "WHERE Name=@0", name); + return (CtfStats)Database.ReadRows("CTF", "*", stats, + ReadStats, "WHERE Name=@0", name); } protected override void SaveStats(Player p) { @@ -55,11 +55,11 @@ namespace MCGalaxy.Games { int count = Database.CountRows("CTF", "WHERE Name=@0", p.name); if (count == 0) { - Database.Backend.AddRow("CTF", "Points, Captures, tags, Name", - data.Points, data.Captures, data.Tags, p.name); + Database.AddRow("CTF", "Points, Captures, tags, Name", + data.Points, data.Captures, data.Tags, p.name); } else { - Database.Backend.UpdateRows("CTF", "Points=@0, Captures=@1, tags=@2", "WHERE Name=@3", - data.Points, data.Captures, data.Tags, p.name); + Database.UpdateRows("CTF", "Points=@0, Captures=@1, tags=@2", "WHERE Name=@3", + data.Points, data.Captures, data.Tags, p.name); } } } diff --git a/MCGalaxy/Games/CTF/CtfGame.cs b/MCGalaxy/Games/CTF/CtfGame.cs index 89ed6bd0f..e024208a7 100644 --- a/MCGalaxy/Games/CTF/CtfGame.cs +++ b/MCGalaxy/Games/CTF/CtfGame.cs @@ -113,7 +113,7 @@ namespace MCGalaxy.Games { Red.RespawnFlag(Map); ResetTeams(); - Database.Backend.CreateTable("CTF", createSyntax); + Database.CreateTable("CTF", createSyntax); } protected override void EndGame() { diff --git a/MCGalaxy/Games/ZombieSurvival/ZSGame.DB.cs b/MCGalaxy/Games/ZombieSurvival/ZSGame.DB.cs index 617ba5b9d..36aea0702 100644 --- a/MCGalaxy/Games/ZombieSurvival/ZSGame.DB.cs +++ b/MCGalaxy/Games/ZombieSurvival/ZSGame.DB.cs @@ -114,8 +114,8 @@ namespace MCGalaxy.Games { static ZombieStats LoadStats(string name) { ZombieStats stats = default(ZombieStats); - return (ZombieStats)Database.Backend.ReadRows("ZombieStats", "*", stats, - ReadStats, "WHERE Name=@0", name); + return (ZombieStats)Database.ReadRows("ZombieStats", "*", stats, + ReadStats, "WHERE Name=@0", name); } protected override void SaveStats(Player p) { @@ -124,13 +124,13 @@ namespace MCGalaxy.Games { int count = Database.CountRows("ZombieStats", "WHERE Name=@0", p.name); if (count == 0) { - Database.Backend.AddRow("ZombieStats", "TotalRounds, MaxRounds, TotalInfected, MaxInfected, Name", - data.TotalRoundsSurvived, data.MaxRoundsSurvived, - data.TotalInfected, data.MaxInfected, p.name); + Database.AddRow("ZombieStats", "TotalRounds, MaxRounds, TotalInfected, MaxInfected, Name", + data.TotalRoundsSurvived, data.MaxRoundsSurvived, + data.TotalInfected, data.MaxInfected, p.name); } else { - Database.Backend.UpdateRows("ZombieStats", "TotalRounds=@0, MaxRounds=@1, TotalInfected=@2, MaxInfected=@3", - "WHERE Name=@4", data.TotalRoundsSurvived, data.MaxRoundsSurvived, - data.TotalInfected, data.MaxInfected, p.name); + Database.UpdateRows("ZombieStats", "TotalRounds=@0, MaxRounds=@1, TotalInfected=@2, MaxInfected=@3", + "WHERE Name=@4", data.TotalRoundsSurvived, data.MaxRoundsSurvived, + data.TotalInfected, data.MaxInfected, p.name); } } } diff --git a/MCGalaxy/Games/ZombieSurvival/ZSGame.cs b/MCGalaxy/Games/ZombieSurvival/ZSGame.cs index 9f8a33527..0208ef52b 100644 --- a/MCGalaxy/Games/ZombieSurvival/ZSGame.cs +++ b/MCGalaxy/Games/ZombieSurvival/ZSGame.cs @@ -126,7 +126,7 @@ namespace MCGalaxy.Games { } protected override void StartGame() { - Database.Backend.CreateTable("ZombieStats", createSyntax); + Database.CreateTable("ZombieStats", createSyntax); HookStats(); } diff --git a/MCGalaxy/Levels/LevelActions.cs b/MCGalaxy/Levels/LevelActions.cs index c8246d387..03b6c5c09 100644 --- a/MCGalaxy/Levels/LevelActions.cs +++ b/MCGalaxy/Levels/LevelActions.cs @@ -94,7 +94,7 @@ namespace MCGalaxy { static void RenameDatabaseTables(Player p, string src, string dst) { if (Database.TableExists("Block" + src)) { - Database.Backend.RenameTable("Block" + src, "Block" + dst); + Database.RenameTable("Block" + src, "Block" + dst); } object srcLocker = ThreadSafeCache.DBCache.GetLocker(src); object dstLocker = ThreadSafeCache.DBCache.GetLocker(dst); @@ -106,7 +106,7 @@ namespace MCGalaxy { MessageBlock.MoveAll(src, dst); if (Database.TableExists("Zone" + src)) { - Database.Backend.RenameTable("Zone" + src, "Zone" + dst); + Database.RenameTable("Zone" + src, "Zone" + dst); } } @@ -115,8 +115,8 @@ namespace MCGalaxy { foreach (string table in tables) { if (!table.StartsWith("Portals")) continue; - Database.Backend.UpdateRows(table, "ExitMap=@1", - "WHERE ExitMap=@0", src, dst); + Database.UpdateRows(table, "ExitMap=@1", + "WHERE ExitMap=@0", src, dst); } } @@ -176,7 +176,7 @@ namespace MCGalaxy { static void DeleteDatabaseTables(string map) { if (Database.TableExists("Block" + map)) { - Database.Backend.DeleteTable("Block" + map); + Database.DeleteTable("Block" + map); } object locker = ThreadSafeCache.DBCache.GetLocker(map); @@ -185,7 +185,7 @@ namespace MCGalaxy { MessageBlock.DeleteAll(map); if (Database.TableExists("Zone" + map)) { - Database.Backend.DeleteTable("Zone" + map); + Database.DeleteTable("Zone" + map); } } } diff --git a/MCGalaxy/Levels/LevelDB.cs b/MCGalaxy/Levels/LevelDB.cs index b0699e40b..016a6552c 100644 --- a/MCGalaxy/Levels/LevelDB.cs +++ b/MCGalaxy/Levels/LevelDB.cs @@ -60,7 +60,7 @@ namespace MCGalaxy { if (!Database.TableExists("Zone" + map)) return; List zones = new List(); - Database.Backend.ReadRows("Zone" + map, "*", zones, ListZones); + Database.ReadRows("Zone" + map, "*", zones, ListZones); bool changedPerbuild = false; for (int i = 0; i < zones.Count; i++) { @@ -86,7 +86,7 @@ namespace MCGalaxy { if (changedPerbuild) level.SaveSettings(); if (level.Zones.Count > 0 && !level.Save(true)) return; - Database.Backend.DeleteTable("Zone" + map); + Database.DeleteTable("Zone" + map); Logger.Log(LogType.SystemActivity, "Upgraded zones for map " + map); } diff --git a/MCGalaxy/Levels/LevelInfo.cs b/MCGalaxy/Levels/LevelInfo.cs index ac30ccdca..4c7e9808b 100644 --- a/MCGalaxy/Levels/LevelInfo.cs +++ b/MCGalaxy/Levels/LevelInfo.cs @@ -149,7 +149,7 @@ namespace MCGalaxy { public static bool ValidName(string map) { foreach (char c in map) { - if (!IDatabaseBackend.ValidNameChar(c)) return false; + if (!Database.ValidNameChar(c)) return false; } return true; } diff --git a/MCGalaxy/Player/Player.Handlers.cs b/MCGalaxy/Player/Player.Handlers.cs index c41e01f52..4d3c86d3a 100644 --- a/MCGalaxy/Player/Player.Handlers.cs +++ b/MCGalaxy/Player/Player.Handlers.cs @@ -677,8 +677,8 @@ namespace MCGalaxy { try { //opstats patch (since 5.5.11) if (Server.Opstats.CaselessContains(cmd) || (cmd.CaselessEq("review") && message.CaselessEq("next") && Server.reviewlist.Count > 0)) { - Database.Backend.AddRow("Opstats", "Time, Name, Cmd, Cmdmsg", - DateTime.Now.ToString(Database.DateFormat), name, cmd, message); + Database.AddRow("Opstats", "Time, Name, Cmd, Cmdmsg", + DateTime.Now.ToString(Database.DateFormat), name, cmd, message); } } catch { } diff --git a/MCGalaxy/Player/Player.Login.cs b/MCGalaxy/Player/Player.Login.cs index b1d9dc1a4..8d3dcbb49 100644 --- a/MCGalaxy/Player/Player.Login.cs +++ b/MCGalaxy/Player/Player.Login.cs @@ -209,9 +209,8 @@ namespace MCGalaxy { } void GetPlayerStats() { - object raw = Database.Backend.ReadRows("Players", "*", - null, PlayerData.Read, - "WHERE Name=@0", name); + object raw = Database.ReadRows("Players", "*", null, PlayerData.Read, + "WHERE Name=@0", name); if (raw == null) { PlayerData.Create(this); Chat.MessageFrom(this, "λNICK %Shas connected for the first time!"); diff --git a/MCGalaxy/Player/Player.cs b/MCGalaxy/Player/Player.cs index c43f99c35..42c8777fc 100644 --- a/MCGalaxy/Player/Player.cs +++ b/MCGalaxy/Player/Player.cs @@ -164,11 +164,11 @@ namespace MCGalaxy { if (!gotSQLData) return; long blocks = PlayerData.Pack(TotalPlaced, TotalModified); long drawn = PlayerData.Pack(TotalDeleted, TotalDrawn); - Database.Backend.UpdateRows("Players", "IP=@0, LastLogin=@1, totalLogin=@2, totalDeaths=@3, Money=@4, " + - "totalBlocks=@5, totalCuboided=@6, totalKicked=@7, TimeSpent=@8, Messages=@9", "WHERE Name=@10", - ip, LastLogin.ToString(Database.DateFormat), - TimesVisited, TimesDied, money, blocks, - drawn, TimesBeenKicked, (long)TotalTime.TotalSeconds, TotalMessagesSent, name); + Database.UpdateRows("Players", "IP=@0, LastLogin=@1, totalLogin=@2, totalDeaths=@3, Money=@4, " + + "totalBlocks=@5, totalCuboided=@6, totalKicked=@7, TimeSpent=@8, Messages=@9", "WHERE Name=@10", + ip, LastLogin.ToString(Database.DateFormat), + TimesVisited, TimesDied, money, blocks, + drawn, TimesBeenKicked, (long)TotalTime.TotalSeconds, TotalMessagesSent, name); } public bool CanUse(Command cmd) { return group.Commands.Contains(cmd); } diff --git a/MCGalaxy/Player/PlayerInfo.cs b/MCGalaxy/Player/PlayerInfo.cs index b9f581479..7d37da378 100644 --- a/MCGalaxy/Player/PlayerInfo.cs +++ b/MCGalaxy/Player/PlayerInfo.cs @@ -95,9 +95,8 @@ namespace MCGalaxy { public static PlayerData FindData(string name) { string suffix = Database.Backend.CaselessWhereSuffix; - object raw = Database.Backend.ReadRows("Players", "*", - null, PlayerData.Read, - "WHERE Name=@0" + suffix, name); + object raw = Database.ReadRows("Players", "*", null, PlayerData.Read, + "WHERE Name=@0" + suffix, name); return (PlayerData)raw; } @@ -130,7 +129,7 @@ namespace MCGalaxy { /// This is current IP for online players, last IP for offline players from the database. public static List FindAccounts(string ip) { List names = new List(); - Database.Backend.ReadRows("Players", "Name", names, ReadAccounts, "WHERE IP=@0", ip); + Database.ReadRows("Players", "Name", names, ReadAccounts, "WHERE IP=@0", ip); // TODO: should we instead do save() when the player logs in // by checking online players we avoid a DB write though diff --git a/MCGalaxy/Server/Maintenance/Backup.cs b/MCGalaxy/Server/Maintenance/Backup.cs index 692c5b1ca..a64d29b77 100644 --- a/MCGalaxy/Server/Maintenance/Backup.cs +++ b/MCGalaxy/Server/Maintenance/Backup.cs @@ -199,7 +199,7 @@ namespace MCGalaxy { List tables = Database.Backend.AllTables(); foreach (string table in tables) { - Database.Backend.DeleteTable(table); + Database.DeleteTable(table); } ImportSql(sql); } diff --git a/MCGalaxy/Server/Server.DB.cs b/MCGalaxy/Server/Server.DB.cs index 617b66439..f14be168e 100644 --- a/MCGalaxy/Server/Server.DB.cs +++ b/MCGalaxy/Server/Server.DB.cs @@ -62,8 +62,8 @@ namespace MCGalaxy { return; } - Database.Backend.CreateTable("Opstats", createOpstats); - Database.Backend.CreateTable("Players", createPlayers); + Database.CreateTable("Opstats", createOpstats); + Database.CreateTable("Players", createPlayers); //since 5.5.11 we are cleaning up the table Playercmds //if Playercmds exists copy-filter to Opstats and remove Playercmds @@ -72,26 +72,26 @@ namespace MCGalaxy { foreach (string cmd in Server.Opstats) Database.Execute(string.Format(sql, "cmd = '" + cmd + "'")); Database.Execute(string.Format(sql, "cmd = 'review' AND cmdmsg = 'next'")); - Database.Backend.DeleteTable("Playercmds"); + Database.DeleteTable("Playercmds"); } 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"); + Database.AddColumn("Players", new ColumnDesc("color", ColumnType.VarChar, 6), "totalKicked"); } if (!columns.CaselessContains("Title_Color")) { - Database.Backend.AddColumn("Players", new ColumnDesc("title_color", ColumnType.VarChar, 6), "color"); + Database.AddColumn("Players", new ColumnDesc("title_color", ColumnType.VarChar, 6), "color"); } if (!columns.CaselessContains("TimeSpent")) { - Database.Backend.AddColumn("Players", new ColumnDesc("TimeSpent", ColumnType.VarChar, 20), "totalKicked"); + Database.AddColumn("Players", new ColumnDesc("TimeSpent", ColumnType.VarChar, 20), "totalKicked"); } if (!columns.CaselessContains("TotalCuboided")) { - Database.Backend.AddColumn("Players", new ColumnDesc("totalCuboided", ColumnType.Int64), "totalBlocks"); + Database.AddColumn("Players", new ColumnDesc("totalCuboided", ColumnType.Int64), "totalBlocks"); } if (!columns.CaselessContains("Messages")) { - Database.Backend.AddColumn("Players", new ColumnDesc("Messages", ColumnType.UInt24), "title_color"); + Database.AddColumn("Players", new ColumnDesc("Messages", ColumnType.UInt24), "title_color"); } } } diff --git a/MCGalaxy/Server/Tasks/UpgradeTasks.cs b/MCGalaxy/Server/Tasks/UpgradeTasks.cs index 4380323fe..8fc620d94 100644 --- a/MCGalaxy/Server/Tasks/UpgradeTasks.cs +++ b/MCGalaxy/Server/Tasks/UpgradeTasks.cs @@ -181,7 +181,7 @@ namespace MCGalaxy.Tasks { static void DumpPlayerTimeSpents() { playerIds = new List(); playerSeconds = new List(); - Database.Backend.ReadRows("Players", "ID,TimeSpent", null, ReadTimeSpent); + Database.ReadRows("Players", "ID,TimeSpent", null, ReadTimeSpent); } static object ReadTimeSpent(IDataRecord record, object arg) {