mirror of
https://github.com/ClassiCube/MCGalaxy.git
synced 2025-09-22 12:05:51 -04:00
Finish modularising database backend, fixes #231
Also copy InventoryOrder in BlockDefinition
This commit is contained in:
parent
1b4a1c854d
commit
73caaeeebd
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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 + "`)");
|
||||
}
|
||||
|
||||
|
||||
|
@ -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);
|
||||
|
@ -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');
|
||||
|
@ -55,6 +55,10 @@ namespace MCGalaxy.SQL {
|
||||
/// for sql queries with no parameters. </summary>
|
||||
internal abstract ParameterisedQuery GetStaticParameterised();
|
||||
|
||||
public abstract string FastGetDateTime(IDataReader reader, int col);
|
||||
|
||||
internal virtual void ParseCreate(ref string cmd) { }
|
||||
|
||||
|
||||
// == Higher level table management functions ==
|
||||
|
||||
|
@ -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++) {
|
||||
|
@ -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 + "`)");
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user