Finish modularising database backend, fixes #231

Also copy InventoryOrder in BlockDefinition
This commit is contained in:
UnknownShadow200 2017-10-08 21:12:31 +11:00
parent 1b4a1c854d
commit 73caaeeebd
7 changed files with 42 additions and 38 deletions

View File

@ -67,6 +67,7 @@ namespace MCGalaxy {
def.Version2 = Version2; def.Version2 = Version2;
def.LeftTex = LeftTex; def.RightTex = RightTex; def.LeftTex = LeftTex; def.RightTex = RightTex;
def.FrontTex = FrontTex; def.BackTex = BackTex; def.FrontTex = FrontTex; def.BackTex = BackTex;
def.InventoryOrder = InventoryOrder;
return def; return def;
} }

View File

@ -58,6 +58,32 @@ namespace MCGalaxy.SQL {
internal override ParameterisedQuery GetStaticParameterised() { internal override ParameterisedQuery GetStaticParameterised() {
return queryInstance; 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 + "`)");
} }

View File

@ -56,6 +56,10 @@ namespace MCGalaxy.SQL {
return queryInstance; 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) { public override bool TableExists(string table) {
ValidateTable(table); ValidateTable(table);

View File

@ -160,7 +160,7 @@ namespace MCGalaxy.DB {
void UpdateTimestamp(IDataReader reader) { void UpdateTimestamp(IDataReader reader) {
// date is in format yyyy-MM-dd hh:mm:ss // 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 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 month = (date[5] - '0') * 10 + (date[6] - '0');
int day = (date[8] - '0') * 10 + (date[9] - '0'); int day = (date[8] - '0') * 10 + (date[9] - '0');

View File

@ -55,6 +55,10 @@ namespace MCGalaxy.SQL {
/// for sql queries with no parameters. </summary> /// for sql queries with no parameters. </summary>
internal abstract ParameterisedQuery GetStaticParameterised(); internal abstract ParameterisedQuery GetStaticParameterised();
public abstract string FastGetDateTime(IDataReader reader, int col);
internal virtual void ParseCreate(ref string cmd) { }
// == Higher level table management functions == // == Higher level table management functions ==

View File

@ -69,7 +69,7 @@ namespace MCGalaxy.SQL {
value = value.Replace("'", "''"); value = value.Replace("'", "''");
sql.Write("'" + value + "'"); sql.Write("'" + value + "'");
} else if (rowTypes[col] == typeof(DateTime)) { } else if (rowTypes[col] == typeof(DateTime)) {
string date = GetDate(reader, col); string date = Database.Backend.FastGetDateTime(reader, col);
sql.Write("'" + date + "'"); sql.Write("'" + date + "'");
} else { } else {
long value = reader.GetInt64(col); // TODO: try to use GetInt32 where possible long value = reader.GetInt64(col); // TODO: try to use GetInt32 where possible
@ -80,16 +80,6 @@ namespace MCGalaxy.SQL {
sql.WriteLine(); 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) { static string FormatInsertColumns(string[] cols, string name) {
string sql = "INSERT INTO `" + name + "` (`"; string sql = "INSERT INTO `" + name + "` (`";
for (int i = 0; i < cols.Length; i++) { for (int i = 0; i < cols.Length; i++) {

View File

@ -88,7 +88,11 @@ namespace MCGalaxy {
if (cmd == null || cmd.Length == 0) continue; if (cmd == null || cmd.Length == 0) continue;
int index = cmd.ToUpper().IndexOf("CREATE TABLE"); 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. //Run the command in the transaction.
if (helper.Execute(cmd)) continue; if (helper.Execute(cmd)) continue;
@ -119,30 +123,5 @@ namespace MCGalaxy {
} }
return buffer.Join(""); 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 + "`)");
}
} }
} }