Cleanup DB code a bit more.

This commit is contained in:
UnknownShadow200 2016-09-05 14:24:41 +10:00
parent 6a66f6ee0c
commit 4be7974142
7 changed files with 66 additions and 45 deletions

View File

@ -69,7 +69,7 @@ namespace MCGalaxy.Commands.Building {
string[] parts = message.SplitSpaces(2);
string alias = parts[0], cmdArgs = "";
Command.Search(ref alias, ref cmdArgs);
foreach (Command cmd in Command.all.commands) {
if (cmd.defaultRank <= p.Rank && (allCmds || !cmd.type.Contains("mod"))) continue;
@ -132,24 +132,31 @@ namespace MCGalaxy.Commands.Building {
void ShowMessageBlocks(Player p) {
p.showMBs = !p.showMBs;
//safe against SQL injections because no user input is given here
using (DataTable table = Database.Fill("SELECT * FROM `Messages" + p.level.name + "`")) {
using (DataTable table = Database.Backend.GetAllRows("Messages" + p.level.name, "*")) {
if (p.showMBs) {
for (int i = 0; i < table.Rows.Count; i++) {
DataRow row = table.Rows[i];
p.SendBlockchange(ushort.Parse(row["X"].ToString()), ushort.Parse(row["Y"].ToString()), ushort.Parse(row["Z"].ToString()), Block.MsgWhite);
}
Player.Message(p, "Now showing &a" + table.Rows.Count + " %SMBs.");
ShowMessageBlocks(p, table);
} else {
for (int i = 0; i < table.Rows.Count; i++) {
DataRow row = table.Rows[i];
p.RevertBlock(ushort.Parse(row["X"].ToString()), ushort.Parse(row["Y"].ToString()), ushort.Parse(row["Z"].ToString()));
}
Player.Message(p, "Now hiding MBs.");
HideMessageBlocks(p, table);
}
}
}
static void ShowMessageBlocks(Player p, DataTable table) {
foreach (DataRow row in table.Rows) {
p.SendBlockchange(U16(row["X"]), U16(row["Y"]), U16(row["Z"]), Block.green);
}
Player.Message(p, "Now showing &a" + table.Rows.Count + " %SMBs.");
}
static void HideMessageBlocks(Player p, DataTable table) {
foreach (DataRow row in table.Rows) {
p.RevertBlock(U16(row["X"]), U16(row["Y"]), U16(row["Z"]));
}
Player.Message(p, "Now hiding MBs.");
}
static ushort U16(object x) { return Convert.ToUInt16(x); }
public override void Help(Player p) {
Player.Message(p, "%T/mb [block] [message]");
Player.Message(p, "%HPlaces a message in your next block.");

View File

@ -122,26 +122,32 @@ namespace MCGalaxy.Commands.Building {
void ShowPortals(Player p) {
p.showPortals = !p.showPortals;
//safe against SQL injections because no user input is given here
DataTable Portals = Database.Fill("SELECT * FROM `Portals" + p.level.name + "`");
if (p.showPortals) {
foreach (DataRow row in Portals.Rows) {
if (row["ExitMap"].ToString() == p.level.name)
p.SendBlockchange(U16(row["ExitX"]), U16(row["ExitY"]), U16(row["ExitZ"]), Block.red);
p.SendBlockchange(U16(row["EntryX"]), U16(row["EntryY"]), U16(row["EntryZ"]), Block.green);
using (DataTable table = Database.Backend.GetAllRows("Portals" + p.level.name, "*")) {
if (p.showPortals) {
ShowPortals(p, table);
} else {
HidePortals(p, table);
}
Player.Message(p, "Now showing &a" + Portals.Rows.Count + " %Sportals.");
} else {
foreach (DataRow row in Portals.Rows) {
if (row["ExitMap"].ToString() == p.level.name)
p.RevertBlock(U16(row["ExitX"]), U16(row["ExitY"]), U16(row["ExitZ"]));
p.RevertBlock(U16(row["EntryX"]), U16(row["EntryY"]), U16(row["EntryZ"]));
}
Player.Message(p, "Now hiding portals.");
}
Portals.Dispose();
}
static void ShowPortals(Player p, DataTable table) {
foreach (DataRow row in table.Rows) {
if (row["ExitMap"].ToString() == p.level.name)
p.SendBlockchange(U16(row["ExitX"]), U16(row["ExitY"]), U16(row["ExitZ"]), Block.red);
p.SendBlockchange(U16(row["EntryX"]), U16(row["EntryY"]), U16(row["EntryZ"]), Block.green);
}
Player.Message(p, "Now showing &a" + table.Rows.Count + " %Sportals.");
}
static void HidePortals(Player p, DataTable table) {
foreach (DataRow row in table.Rows) {
if (row["ExitMap"].ToString() == p.level.name)
p.RevertBlock(U16(row["ExitX"]), U16(row["ExitY"]), U16(row["ExitZ"]));
p.RevertBlock(U16(row["EntryX"]), U16(row["EntryY"]), U16(row["EntryZ"]));
}
Player.Message(p, "Now hiding portals.");
}
static ushort U16(object x) { return Convert.ToUInt16(x); }

View File

@ -16,6 +16,7 @@
permissions and limitations under the Licenses.
*/
using System;
using System.Data;
namespace MCGalaxy.SQL {
@ -52,9 +53,9 @@ namespace MCGalaxy.SQL {
/// <summary> Removes all entries from the given table. </summary>
public abstract void ClearTable(string table);
/// <summary> Inserts/Copies all the entries from the source table into the destination table. </summary>
/// <summary> Inserts/Copies all the rows from the source table into the destination table. </summary>
/// <remarks> Note: This may work incorrectly if the tables have different schema. </remarks>
public virtual void CopyAllEntries(string srcTable, string dstTable) {
public virtual void CopyAllRows(string srcTable, string dstTable) {
string syntax = "INSERT INTO `" + dstTable + "` SELECT * FROM `" + srcTable + "`";
Database.Execute(syntax);
}
@ -64,5 +65,11 @@ namespace MCGalaxy.SQL {
string syntax = "DROP TABLE `" + table + "`";
Database.Execute(syntax);
}
/// <summary> Retrieves all rows for the given table from the database. </summary>
public virtual DataTable GetAllRows(string table, string columns) {
string syntax = "SELECT " + columns + " FROM `" + table + "`";
return Database.Fill(syntax);
}
}
}

View File

@ -57,8 +57,9 @@ PRIMARY KEY(player)
public static void LoadDatabase() {
Database.Execute(createTable);
DataTable eco = Database.Fill("SELECT * FROM Economy");
foreach (DataRow row in eco.Rows) {
using (DataTable eco = Database.Backend.GetAllRows("Economy", "*"))
foreach (DataRow row in eco.Rows)
{
int money = PlayerData.ParseInt(row["money"].ToString());
if (money == 0) continue;
@ -69,7 +70,7 @@ PRIMARY KEY(player)
stats.Salary = row["salary"].ToString();
stats.Fine = row["fine"].ToString();
stats.TotalSpent = PlayerData.ParseInt(row["total"].ToString());
UpdateMoney(stats.Player, money);
UpdateStats(stats);
}

View File

@ -208,18 +208,18 @@ namespace MCGalaxy {
{
if (Database.TableExists("Portals" + src)) {
Database.Execute(String.Format(LevelDB.createPortals, dst));
Database.Backend.CopyAllEntries("Portals" + src, "Portals" + dst);
Database.Backend.CopyAllRows("Portals" + src, "Portals" + dst);
string updateSyntax = "UPDATE `Portals" + dst + "` SET ExitMap=@1 WHERE ExitMap=@0";
Database.Execute(updateSyntax, src, dst);
}
if (Database.TableExists("Messages" + src)) {
Database.Execute(String.Format(LevelDB.createMessages, dst));
Database.Backend.CopyAllEntries("Messages" + src, "Messages" + dst);
Database.Backend.CopyAllRows("Messages" + src, "Messages" + dst);
}
if (Database.TableExists("Zone" + src)) {
Database.Execute(String.Format(LevelDB.createZones, dst));
Database.Backend.CopyAllEntries("Zone" + src, "Zone" + dst);
Database.Backend.CopyAllRows("Zone" + src, "Zone" + dst);
}
}
}

View File

@ -94,7 +94,7 @@ namespace MCGalaxy {
internal static void LoadZones(Level level, string name) {
if (!Database.TableExists("Zone" + name)) return;
using (DataTable table = Database.Fill("SELECT * FROM `Zone" + name + "`")) {
using (DataTable table = Database.Backend.GetAllRows("Zone" + name, "*")) {
Level.Zone Zn;
foreach (DataRow row in table.Rows) {
Zn.smallX = ushort.Parse(row["SmallX"].ToString());
@ -111,7 +111,7 @@ namespace MCGalaxy {
internal static void LoadPortals(Level level, string name) {
if (!Database.TableExists("Portals" + name)) return;
using (DataTable table = Database.Fill("SELECT * FROM `Portals" + name + "`")) {
using (DataTable table = Database.Backend.GetAllRows("Portals" + name, "*")) {
foreach (DataRow row in table.Rows) {
byte tile = level.GetTile(ushort.Parse(row["EntryX"].ToString()),
ushort.Parse(row["EntryY"].ToString()),
@ -126,7 +126,7 @@ namespace MCGalaxy {
internal static void LoadMessages(Level level, string name) {
if (!Database.TableExists("Messages" + name)) return;
using (DataTable table = Database.Fill("SELECT * FROM `Messages" + name + "`")) {
using (DataTable table = Database.Backend.GetAllRows("Messages" + name, "*")) {
foreach (DataRow row in table.Rows) {
byte tile = level.GetTile(ushort.Parse(row["X"].ToString()),
ushort.Parse(row["Y"].ToString()),

View File

@ -59,9 +59,9 @@ namespace MCGalaxy {
try {
if (group.commands.Contains("inbox") && Database.TableExists("Inbox" + name) ) {
//safe against SQL injections because no user input is given here
using (DataTable Inbox = Database.Fill("SELECT * FROM `Inbox" + name + "`")) {
if (Inbox.Rows.Count > 0)
SendMessage("You have &a" + Inbox.Rows.Count + " %Smessages in /inbox");
using (DataTable table = Database.Backend.GetAllRows("Inbox" + name, "*")) {
if (table.Rows.Count > 0)
SendMessage("You have &a" + table.Rows.Count + " %Smessages in /inbox");
}
}
} catch {