diff --git a/MCGalaxy/Blocks/BlockDefinitions.cs b/MCGalaxy/Blocks/BlockDefinitions.cs index a39f7e2d7..c9c548232 100644 --- a/MCGalaxy/Blocks/BlockDefinitions.cs +++ b/MCGalaxy/Blocks/BlockDefinitions.cs @@ -67,6 +67,7 @@ namespace MCGalaxy { def.Version2 = Version2; def.LeftTex = LeftTex; def.RightTex = RightTex; def.FrontTex = FrontTex; def.BackTex = BackTex; + def.InventoryOrder = InventoryOrder; return def; } diff --git a/MCGalaxy/Database/Backends/MySQL.cs b/MCGalaxy/Database/Backends/MySQL.cs index 7b657de51..62679c8a8 100644 --- a/MCGalaxy/Database/Backends/MySQL.cs +++ b/MCGalaxy/Database/Backends/MySQL.cs @@ -58,6 +58,32 @@ namespace MCGalaxy.SQL { internal override ParameterisedQuery GetStaticParameterised() { return queryInstance; + } + + public override string FastGetDateTime(IDataReader reader, int col) { + DateTime date = reader.GetDateTime(col); + return date.ToString("yyyy-MM-dd HH:mm:ss"); + } + + internal override void ParseCreate(ref string cmd) { + // MySQL does not support the format used by the SQLite backend for the primary key + const string priKey = " PRIMARY KEY AUTOINCREMENT"; + int priIndex = cmd.ToUpper().IndexOf(priKey); + if (priIndex == -1) return; + + // Find the name of this column + char[] sepChars = new char[] { '\t', ' ' }; // chars that separate part of a column definition + char[] startChars = new char[] { '`', '(', ' ', ',', '\t' }; // chars that can start a column definition + string before = cmd.Substring(0, priIndex); + before = before.Substring(0, before.LastIndexOfAny(sepChars)); // get rid of column type + int nameStart = before.LastIndexOfAny(startChars) + 1; + string name = before.Substring(nameStart); + + // Replace the 'PRIMARY KEY AUTOINCREMENT' with just 'AUTO_INCREMENT'; + cmd = cmd.Remove(priIndex, priKey.Length); + cmd = cmd.Insert(priIndex, " AUTO_INCREMENT"); + // Insert 'PRIMARY KEY' at end of columns definition + cmd = cmd.Insert(cmd.LastIndexOf(")"), ", PRIMARY KEY (`" + name + "`)"); } diff --git a/MCGalaxy/Database/Backends/SQLite.cs b/MCGalaxy/Database/Backends/SQLite.cs index 284f33c9e..bb1351baa 100644 --- a/MCGalaxy/Database/Backends/SQLite.cs +++ b/MCGalaxy/Database/Backends/SQLite.cs @@ -56,6 +56,10 @@ namespace MCGalaxy.SQL { return queryInstance; } + public override string FastGetDateTime(IDataReader reader, int col) { + return reader.GetString(col); // reader.GetDateTime is extremely slow so avoid it + } + public override bool TableExists(string table) { ValidateTable(table); diff --git a/MCGalaxy/Database/BlockDB/BlockDBTableDumper.cs b/MCGalaxy/Database/BlockDB/BlockDBTableDumper.cs index dd02caa5a..b41bbab48 100644 --- a/MCGalaxy/Database/BlockDB/BlockDBTableDumper.cs +++ b/MCGalaxy/Database/BlockDB/BlockDBTableDumper.cs @@ -160,7 +160,7 @@ namespace MCGalaxy.DB { void UpdateTimestamp(IDataReader reader) { // date is in format yyyy-MM-dd hh:mm:ss - string date = TableDumper.GetDate(reader, 1); + string date = Database.Backend.FastGetDateTime(reader, 1); int year = (date[0] - '0') * 1000 + (date[1] - '0') * 100 + (date[2] - '0') * 10 + (date[3] - '0'); int month = (date[5] - '0') * 10 + (date[6] - '0'); int day = (date[8] - '0') * 10 + (date[9] - '0'); diff --git a/MCGalaxy/Database/IDatabaseBackend.cs b/MCGalaxy/Database/IDatabaseBackend.cs index 622996672..629844e50 100644 --- a/MCGalaxy/Database/IDatabaseBackend.cs +++ b/MCGalaxy/Database/IDatabaseBackend.cs @@ -55,6 +55,10 @@ namespace MCGalaxy.SQL { /// for sql queries with no parameters. internal abstract ParameterisedQuery GetStaticParameterised(); + public abstract string FastGetDateTime(IDataReader reader, int col); + + internal virtual void ParseCreate(ref string cmd) { } + // == Higher level table management functions == diff --git a/MCGalaxy/Database/TableDumper.cs b/MCGalaxy/Database/TableDumper.cs index ed63f0fe0..0ad8b510f 100644 --- a/MCGalaxy/Database/TableDumper.cs +++ b/MCGalaxy/Database/TableDumper.cs @@ -69,7 +69,7 @@ namespace MCGalaxy.SQL { value = value.Replace("'", "''"); sql.Write("'" + value + "'"); } else if (rowTypes[col] == typeof(DateTime)) { - string date = GetDate(reader, col); + string date = Database.Backend.FastGetDateTime(reader, col); sql.Write("'" + date + "'"); } else { long value = reader.GetInt64(col); // TODO: try to use GetInt32 where possible @@ -80,16 +80,6 @@ namespace MCGalaxy.SQL { sql.WriteLine(); } - - public static string GetDate(IDataReader reader, int col) { - if (ServerConfig.UseMySQL) { - DateTime date = reader.GetDateTime(col); - return date.ToString("yyyy-MM-dd HH:mm:ss"); - } else { - return reader.GetString(col); // GetDateTime is extremely slow so avoid it - } - } - static string FormatInsertColumns(string[] cols, string name) { string sql = "INSERT INTO `" + name + "` (`"; for (int i = 0; i < cols.Length; i++) { diff --git a/MCGalaxy/Server/BackupDB.cs b/MCGalaxy/Server/BackupDB.cs index 50ccdefea..a2cd8528f 100644 --- a/MCGalaxy/Server/BackupDB.cs +++ b/MCGalaxy/Server/BackupDB.cs @@ -88,7 +88,11 @@ namespace MCGalaxy { if (cmd == null || cmd.Length == 0) continue; int index = cmd.ToUpper().IndexOf("CREATE TABLE"); - if (index > -1) ParseCreate(ref cmd, index); + if (index > -1) { + cmd = cmd.Remove(0, index); + cmd = cmd.Replace(" unsigned", " UNSIGNED"); + Database.Backend.ParseCreate(ref cmd); + } //Run the command in the transaction. if (helper.Execute(cmd)) continue; @@ -119,30 +123,5 @@ namespace MCGalaxy { } return buffer.Join(""); } - - static void ParseCreate(ref string cmd, int index) { - cmd = cmd.Remove(0, index); - cmd = cmd.Replace(" unsigned", " UNSIGNED"); - if (!ServerConfig.UseMySQL) return; - - // MySQL does not support the format used by the SQLite backend for the primary key - const string priKey = " PRIMARY KEY AUTOINCREMENT"; - int priIndex = cmd.ToUpper().IndexOf(priKey); - if (priIndex == -1) return; - - // Find the name of this column - char[] sepChars = new char[] { '\t', ' ' }; // chars that separate part of a column definition - char[] startChars = new char[] { '`', '(', ' ', ',', '\t' }; // chars that can start a column definition - string before = cmd.Substring(0, priIndex); - before = before.Substring(0, before.LastIndexOfAny(sepChars)); // get rid of column type - int nameStart = before.LastIndexOfAny(startChars) + 1; - string name = before.Substring(nameStart); - - // Replace the 'PRIMARY KEY AUTOINCREMENT' with just 'AUTO_INCREMENT'; - cmd = cmd.Remove(priIndex, priKey.Length); - cmd = cmd.Insert(priIndex, " AUTO_INCREMENT"); - // Insert 'PRIMARY KEY' at end of columns definition - cmd = cmd.Insert(cmd.LastIndexOf(")"), ", PRIMARY KEY (`" + name + "`)"); - } } } \ No newline at end of file