Modularise Database.cs

This commit is contained in:
UnknownShadow200 2016-06-30 10:18:12 +10:00
parent bd32c88d59
commit dcc1e2462c
3 changed files with 139 additions and 163 deletions

View File

@ -49,6 +49,7 @@ namespace MCGalaxy.Commands.Building {
else if (block == "water") { data.type = Block.water_portal; } else if (block == "water") { data.type = Block.water_portal; }
else if (block == "lava") { data.type = Block.lava_portal; } else if (block == "lava") { data.type = Block.lava_portal; }
else if (block == "show") { ShowPortals(p); return; } else if (block == "show") { ShowPortals(p); return; }
else if (block == "list") { ListPortals(p); return; }
else { Help(p); return; } else { Help(p); return; }
Player.Message(p, "Place an &aEntry block %Sfor the portal"); Player.Message(p, "Place an &aEntry block %Sfor the portal");
@ -94,13 +95,10 @@ namespace MCGalaxy.Commands.Building {
DataTable Portals = Database.fillData("SELECT * FROM `Portals" + pos.mapName + "` WHERE EntryX=" + (int)pos.x + " AND EntryY=" + (int)pos.y + " AND EntryZ=" + (int)pos.z); DataTable Portals = Database.fillData("SELECT * FROM `Portals" + pos.mapName + "` WHERE EntryX=" + (int)pos.x + " AND EntryY=" + (int)pos.y + " AND EntryZ=" + (int)pos.z);
Portals.Dispose(); Portals.Dispose();
if (Portals.Rows.Count == 0) if (Portals.Rows.Count == 0) {//safe against SQL injections because no user input is given here
{//safe against SQL injections because no user input is given here
Database.executeQuery("INSERT INTO `Portals" + pos.mapName + "` (EntryX, EntryY, EntryZ, ExitMap, ExitX, ExitY, ExitZ) VALUES (" Database.executeQuery("INSERT INTO `Portals" + pos.mapName + "` (EntryX, EntryY, EntryZ, ExitMap, ExitX, ExitY, ExitZ) VALUES ("
+ (int)pos.x + ", " + (int)pos.y + ", " + (int)pos.z + ", '" + p.level.name + "', " + (int)x + ", " + (int)y + ", " + (int)z + ")"); + (int)pos.x + ", " + (int)pos.y + ", " + (int)pos.z + ", '" + p.level.name + "', " + (int)x + ", " + (int)y + ", " + (int)z + ")");
} } else {//safe against SQL injections because no user input is given here
else
{//safe against SQL injections because no user input is given here
Database.executeQuery("UPDATE `Portals" + pos.mapName + "` SET ExitMap='" + p.level.name + "', ExitX=" + (int)x + ", ExitY=" + (int)y + ", ExitZ=" + Database.executeQuery("UPDATE `Portals" + pos.mapName + "` SET ExitMap='" + p.level.name + "', ExitX=" + (int)x + ", ExitY=" + (int)y + ", ExitZ=" +
(int)z + " WHERE EntryX=" + (int)pos.x + " AND EntryY=" + (int)pos.y + " AND EntryZ=" + (int)pos.z); (int)z + " WHERE EntryX=" + (int)pos.x + " AND EntryY=" + (int)pos.y + " AND EntryZ=" + (int)pos.z);
} }
@ -127,8 +125,7 @@ namespace MCGalaxy.Commands.Building {
DataTable Portals = Database.fillData("SELECT * FROM `Portals" + p.level.name + "`"); DataTable Portals = Database.fillData("SELECT * FROM `Portals" + p.level.name + "`");
if (p.showPortals) { if (p.showPortals) {
for (int i = 0; i < Portals.Rows.Count; i++) { foreach (DataRow row in Portals.Rows) {
DataRow row = Portals.Rows[i];
if (row["ExitMap"].ToString() == p.level.name) if (row["ExitMap"].ToString() == p.level.name)
p.SendBlockchange(U16(row["ExitX"]), U16(row["ExitY"]), U16(row["ExitZ"]), Block.red); 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); p.SendBlockchange(U16(row["EntryX"]), U16(row["EntryY"]), U16(row["EntryZ"]), Block.green);
@ -136,8 +133,7 @@ namespace MCGalaxy.Commands.Building {
Player.Message(p, "Now showing &a" + Portals.Rows.Count + " %Sportals."); Player.Message(p, "Now showing &a" + Portals.Rows.Count + " %Sportals.");
} else { } else {
for (int i = 0; i < Portals.Rows.Count; i++) { foreach (DataRow row in Portals.Rows) {
DataRow row = Portals.Rows[i];
if (row["ExitMap"].ToString() == p.level.name) if (row["ExitMap"].ToString() == p.level.name)
p.RevertBlock(U16(row["ExitX"]), U16(row["ExitY"]), U16(row["ExitZ"])); p.RevertBlock(U16(row["ExitX"]), U16(row["ExitY"]), U16(row["ExitZ"]));
p.RevertBlock(U16(row["EntryX"]), U16(row["EntryY"]), U16(row["EntryZ"])); p.RevertBlock(U16(row["EntryX"]), U16(row["EntryY"]), U16(row["EntryZ"]));
@ -147,6 +143,18 @@ namespace MCGalaxy.Commands.Building {
Portals.Dispose(); Portals.Dispose();
} }
void ListPortals(Player p) {
//safe against SQL injections because no user input is given here
DataTable Portals = Database.fillData("SELECT * FROM `Portals" + p.level.name + "`");
const string format = "({0},{1},{2}) to ({3},{4},{5}) on {6}";
foreach (DataRow row in Portals.Rows) {
Player.Message(p, format, U16(row["EntryX"]), U16(row["EntryY"]), U16(row["EntryZ"]),
U16(row["ExitX"]), U16(row["ExitY"]), U16(row["ExitZ"]), row["ExitMap"]);
}
Player.Message(p, "{0} portals in total.", Portals.Rows.Count);
Portals.Dispose();
}
static ushort U16(object x) { return Convert.ToUInt16(x); } static ushort U16(object x) { return Convert.ToUInt16(x); }
public override void Help(Player p) { public override void Help(Player p) {

View File

@ -36,9 +36,6 @@ namespace MCGalaxy.Commands
{ {
switch (message) switch (message)
{ {
case "": // To prevent '/server' from causing an error message
Help(p);
break;
case "restart": case "restart":
case "update": case "update":
case "shutdown": case "shutdown":
@ -92,7 +89,7 @@ namespace MCGalaxy.Commands
// Also important to save everything to a .zip file (Though we can rename the extention.) // Also 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. // When backing up, one option is to save all non-main program files.
// This means all folders, and files in these folders. // This means all folders, and files in these folders.
Player.Message(p, "Server backup (Everything): Started.\n\tPlease wait while backup finishes."); Player.Message(p, "Server backup (Everything): Started. Please wait while backup finishes.");
Save(true, p); Save(true, p);
break; break;
case "backup db": case "backup db":
@ -102,14 +99,14 @@ namespace MCGalaxy.Commands
// Also important to save everything to a .zip file (Though we can rename the extention.) // Also 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. // When backing up, one option is to save all non-main program files.
// This means all folders, and files in these folders. // This means all folders, and files in these folders.
Player.Message(p, "Server backup (Database): Started.\n\tPlease wait while backup finishes."); Player.Message(p, "Server backup (Database): Started. Please wait while backup finishes.");
Save(false, true, p); Save(false, true, p);
break; break;
case "backup allbutdb": case "backup allbutdb":
// Important to save everything to a .zip file (Though we can rename the extention.) // 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. // When backing up, one option is to save all non-main program files.
// This means all folders, and files in these folders. // This means all folders, and files in these folders.
Player.Message(p, "Server backup (Everything but Database): Started.\n\tPlease wait while backup finishes."); Player.Message(p, "Server backup (Everything but Database): Started. Please wait while backup finishes.");
Save(false, p); Save(false, p);
break; break;
case "restore": case "restore":
@ -122,11 +119,10 @@ namespace MCGalaxy.Commands
extract.Start(p); extract.Start(p);
break; break;
default: default:
if (message != "")
Player.Message(p, "/server " + message + " is not currently implemented."); Player.Message(p, "/server " + message + " is not currently implemented.");
goto case ""; Help(p);
//case "help": break;
// Help(p);
// break;
} }
} }
@ -265,7 +261,7 @@ namespace MCGalaxy.Commands
private static void SaveDatabase(string filename) private static void SaveDatabase(string filename)
{ {
using (StreamWriter sql = new StreamWriter(filename)) using (StreamWriter sql = new StreamWriter(filename))
Database.CopyDatabase(sql); Database.BackupDatabase(sql);
} }
private static List<Uri> GetAllFiles(DirectoryInfo currDir, Uri baseUri) private static List<Uri> GetAllFiles(DirectoryInfo currDir, Uri baseUri)

View File

@ -26,7 +26,7 @@ namespace MCGalaxy.SQL {
public static partial class Database { public static partial class Database {
public static void CopyDatabase(StreamWriter sql) { public static void BackupDatabase(StreamWriter sql) {
//We technically know all tables in the DB... But since this is MySQL, we can also get them all with a MySQL command //We technically know all tables in the DB... But since this is MySQL, we can also get them all with a MySQL command
//So we show the tables, and store the result. //So we show the tables, and store the result.
//Also output information data (Same format as phpMyAdmin's dump) //Also output information data (Same format as phpMyAdmin's dump)
@ -41,12 +41,14 @@ namespace MCGalaxy.SQL {
sql.WriteLine("-- Generation Time: {0} at {1}", DateTime.Now.Date, DateTime.Now.TimeOfDay); sql.WriteLine("-- Generation Time: {0} at {1}", DateTime.Now.Date, DateTime.Now.TimeOfDay);
sql.WriteLine("-- MCGalaxy Version: {0}", Server.Version); sql.WriteLine("-- MCGalaxy Version: {0}", Server.Version);
sql.WriteLine(); sql.WriteLine();
//Extra stuff goes here
sql.WriteLine(); sql.WriteLine();
//database here
List<String> sqlTables = (Database.getTables()); List<string> sqlTables = Database.getTables();
foreach (string tableName in sqlTables) foreach (string tableName in sqlTables)
{ BackupTable(tableName, sql);
}
static void BackupTable(string tableName, StreamWriter sql) {
//For each table, we iterate through all rows, (and save them) //For each table, we iterate through all rows, (and save them)
sql.WriteLine("-- --------------------------------------------------------"); sql.WriteLine("-- --------------------------------------------------------");
sql.WriteLine(); sql.WriteLine();
@ -54,48 +56,89 @@ namespace MCGalaxy.SQL {
sql.WriteLine("-- Table structure for table `{0}`", tableName); sql.WriteLine("-- Table structure for table `{0}`", tableName);
sql.WriteLine("--"); sql.WriteLine("--");
sql.WriteLine(); sql.WriteLine();
List<string[]> tableSchema = WriteTableSchema(tableName, sql);
using (DataTable data = Database.fillData("SELECT * FROM `" + tableName + "`")) {
if (data.Rows.Count == 0) {
sql.WriteLine("-- No data in table `{0}`!", tableName);
sql.WriteLine();
return;
}
sql.WriteLine("--");
sql.WriteLine("-- Dumping data for table `{0}`", tableName);
sql.WriteLine("--");
sql.WriteLine();
List<DataColumn> allCols = new List<DataColumn>();
foreach (DataColumn col in data.Columns)
allCols.Add(col);
foreach (DataRow row in data.Rows) { //We rely on the correct datatype being given here.
sql.WriteLine();
sql.Write("INSERT INTO `{0}` (`", tableName);
foreach (string[] rParams in tableSchema) {
sql.Write(rParams[0]);
sql.Write((tableSchema[tableSchema.Count - 1].Equals(rParams) ? "`) VALUES" : "`, `"));
}
sql.WriteLine();
sql.Write("(");
for (int col = 0; col < row.ItemArray.Length; col++) {
//The values themselves can be integers or strings, or null
Type eleType = allCols[col].DataType;
if (row.IsNull(col)) {
sql.Write("NULL");
} else if (eleType.Name.Equals("DateTime")) { // special format
DateTime dt = row.Field<DateTime>(col);
sql.Write("'{0:yyyy-MM-dd HH:mm:ss.ffff}'", dt);
} else if (eleType.Name.Equals("Boolean")) {
sql.Write(row.Field<Boolean>(col) ? "1" : "0");
} else if (eleType.Name.Equals("String")) { // Requires ''
sql.Write("'{0}'", row.Field<string>(col));
} else {
sql.Write(row.Field<Object>(col)); // We assume all other data is left as-is
//This includes numbers, blobs, etc. (As well as objects, but we don't save them into the database)
}
sql.Write((col < row.ItemArray.Length - 1 ? ", " : ");"));
}
}
sql.WriteLine();
}
}
static List<string[]> WriteTableSchema(string tableName, StreamWriter sql) {
List<string[]> tableSchema = new List<string[]>(); List<string[]> tableSchema = new List<string[]>();
if (Server.useMySQL) if (Server.useMySQL) {
{
string[] rowParams; string[] rowParams;
string pri; string pri;
sql.WriteLine("CREATE TABLE IF NOT EXISTS `{0}` (", tableName); sql.WriteLine("CREATE TABLE IF NOT EXISTS `{0}` (", tableName);
using (DataTable tableRowSchema = Database.fillData("DESCRIBE " + tableName)) using (DataTable schema = Database.fillData("DESCRIBE " + tableName)) {
{ rowParams = new string[schema.Columns.Count];
rowParams = new string[tableRowSchema.Columns.Count];
pri = ""; pri = "";
foreach (DataRow row in tableRowSchema.Rows) foreach (DataRow row in schema.Rows) {
{
//Save the info contained to file //Save the info contained to file
List<string> tmp = new List<string>(); List<string> tmp = new List<string>();
for (int col = 0; col < tableRowSchema.Columns.Count; col++) for (int col = 0; col < schema.Columns.Count; col++)
{
tmp.Add(row.Field<string>(col)); tmp.Add(row.Field<string>(col));
}// end:for(col)
rowParams = tmp.ToArray<string>(); rowParams = tmp.ToArray();
rowParams[2] = (rowParams[2].ToLower().Equals("no") ? "NOT " : "DEFAULT ") + "NULL"; rowParams[2] = (rowParams[2].ToLower().Equals("no") ? "NOT " : "DEFAULT ") + "NULL";
pri += (rowParams[3].ToLower().Equals("pri") ? rowParams[0] + ";" : ""); pri += (rowParams[3].ToLower().Equals("pri") ? rowParams[0] + ";" : "");
sql.WriteLine("`{0}` {1} {2}" + (rowParams[5].Equals("") ? "" : " {5}") + (pri.Equals("") && row == tableRowSchema.Rows[tableRowSchema.Rows.Count - 1] ? "" : ","), rowParams); sql.WriteLine("`{0}` {1} {2}" + (rowParams[5].Equals("") ? "" : " {5}") + (pri.Equals("") && row == schema.Rows[schema.Rows.Count - 1] ? "" : ","), rowParams);
tableSchema.Add(rowParams); tableSchema.Add(rowParams);
}// end:foreach(DataRow row)
} }
if (!pri.Equals("")) }
{
if (pri != "") {
string[] tmp = pri.Substring(0, pri.Length - 1).Split(';'); string[] tmp = pri.Substring(0, pri.Length - 1).Split(';');
sql.Write("PRIMARY KEY (`"); sql.Write("PRIMARY KEY (`");
foreach (string prim in tmp) foreach (string prim in tmp) {
{
sql.Write(prim); sql.Write(prim);
sql.Write("`" + (tmp[tmp.Count() - 1].Equals(prim) ? ")" : ", `")); sql.Write("`" + (tmp[tmp.Length - 1].Equals(prim) ? ")" : ", `"));
}
} }
} /*else {
sql.Flush();
sql.BaseStream.Seek(-3, SeekOrigin.Current);
}*/
sql.WriteLine(");"); sql.WriteLine(");");
} } else {
else
{
using (DataTable tableSQL = Database.fillData("SELECT sql FROM" + using (DataTable tableSQL = Database.fillData("SELECT sql FROM" +
" (SELECT * FROM sqlite_master UNION ALL" + " (SELECT * FROM sqlite_master UNION ALL" +
" SELECT * FROM sqlite_temp_master)" + " SELECT * FROM sqlite_temp_master)" +
@ -104,90 +147,19 @@ namespace MCGalaxy.SQL {
"ORDER BY substr(type,2,1), name")) "ORDER BY substr(type,2,1), name"))
{ {
//just print out the data in the table. //just print out the data in the table.
foreach (DataRow row in tableSQL.Rows) foreach (DataRow row in tableSQL.Rows) {
{
string tableSQLString = row.Field<string>(0); string tableSQLString = row.Field<string>(0);
sql.WriteLine(tableSQLString.Replace(" " + tableName, " `" + tableName + "`").Replace("CREATE TABLE `" + tableName + "`", "CREATE TABLE IF NOT EXISTS `" + tableName + "`") + ";"); sql.WriteLine(tableSQLString.Replace(" " + tableName, " `" + tableName + "`").Replace("CREATE TABLE `" + tableName + "`", "CREATE TABLE IF NOT EXISTS `" + tableName + "`") + ";");
//We parse this ourselves to find the actual types. //We parse this ourselves to find the actual types.
tableSchema = getSchema(tableSQLString); tableSchema = getSchema(tableSQLString);
} }
} }
} }
sql.WriteLine(); sql.WriteLine();
using (DataTable tableRowData = Database.fillData("SELECT * FROM `" + tableName + "`")) return tableSchema;
{
if (tableRowData.Rows.Count > 0)
{
sql.WriteLine("--");
sql.WriteLine("-- Dumping data for table `{0}`", tableName);
sql.WriteLine("--");
sql.WriteLine();
List<DataColumn> allCols = new List<DataColumn>();
foreach (DataColumn col in tableRowData.Columns)
{
allCols.Add(col);
}
foreach (DataRow row in tableRowData.Rows)
{ //We rely on the correct datatype being given here.
sql.WriteLine();
sql.Write("INSERT INTO `{0}` (`", tableName);
foreach (string[] rParams in tableSchema)
{
sql.Write(rParams[0]);
sql.Write((tableSchema.ElementAt<string[]>(tableSchema.Count() - 1).Equals(rParams) ? "`) VALUES" : "`, `"));
}
//Save the info contained to file
sql.WriteLine();
sql.Write("(");
for (int col = 0; col < row.ItemArray.Length; col++)
{
//The values themselves can be integers or strings, or null
Type eleType = allCols[col].DataType;
if (row.IsNull(col))
{
sql.Write("NULL");
}
else if (eleType.Name.Equals("DateTime"))
{ // special format
DateTime dt = row.Field<DateTime>(col);
sql.Write("'{0:yyyy-MM-dd HH:mm:ss.ffff}'", dt);
}
else if (eleType.Name.Equals("Boolean"))
{
sql.Write(row.Field<Boolean>(col) ? "1" : "0");
}
else if (eleType.Name.Equals("String"))
{ // Requires ''
sql.Write("'{0}'", row.Field<string>(col));
}
else
{
sql.Write(row.Field<Object>(col)); // We assume all other data is left as-is
//This includes numbers, blobs, etc. (As well as objects, but we don't save them into the database)
}
sql.Write((col < row.ItemArray.Length - 1 ? ", " : ");"));
}// end:for(col)
}// end:foreach(DataRow row)
}
else
{
sql.WriteLine("-- No data in table `{0}`!", tableName);
}
sql.WriteLine();
} }
}// end:foreach(DataRow sqlTablesRow) static List<string[]> getSchema(string tableSQLString) {
}
private static List<string[]> getSchema(string tableSQLString)
{
// All SQL for creating tables looks like "CREATE TABLE [IF NOT EXISTS] <TableName> (<ColumnDef>[, ... [, PRIMARY KEY (<ColumnName>[, ...])]]) // All SQL for creating tables looks like "CREATE TABLE [IF NOT EXISTS] <TableName> (<ColumnDef>[, ... [, PRIMARY KEY (<ColumnName>[, ...])]])
// <ColumnDef> = <name> <type> [[NOT|DEFAULT] NULL] [PRIMARY KEY] [AUTO_INCREMENT] // <ColumnDef> = <name> <type> [[NOT|DEFAULT] NULL] [PRIMARY KEY] [AUTO_INCREMENT]
List<string[]> schema = new List<string[]>(); List<string[]> schema = new List<string[]>();
@ -231,7 +203,7 @@ namespace MCGalaxy.SQL {
//Backup //Backup
using (FileStream backup = File.Create("backup.sql")) using (FileStream backup = File.Create("backup.sql"))
{ {
CopyDatabase(new StreamWriter(backup)); BackupDatabase(new StreamWriter(backup));
} }
//Delete old //Delete old
List<string> tables = getTables(); List<string> tables = getTables();