Fix /top timespent on mysql backend, closes #423.

This commit is contained in:
UnknownShadow200 2017-05-12 11:14:23 +10:00
parent 651268bce1
commit 8da596c0fd
3 changed files with 25 additions and 9 deletions

View File

@ -49,7 +49,7 @@ namespace MCGalaxy.Commands.Info {
string strLimit = " LIMIT " + offset + "," + limit;
DataTable db = Database.Backend.GetRows(stat.Table, "DISTINCT Name, " + stat.Column,
"ORDER BY " + stat.OrderBy + strLimit);
"ORDER BY" + stat.OrderBy + strLimit);
Player.Message(p, "&a{0}:", stat.Title());
for (int i = 0; i < db.Rows.Count; i++) {

View File

@ -39,7 +39,7 @@ namespace MCGalaxy.DB {
OrderBy = orderBy;
if (OrderBy == null)
OrderBy = " " + col + " ";
OrderBy += (ascending ? " asc" : " desc");
OrderBy += (ascending ? "asc" : "desc");
}
/// <summary> List of stats that can be ordered. </summary>
@ -85,7 +85,7 @@ namespace MCGalaxy.DB {
new TopStat("TimeSpent", PlayerData.DBTable,
PlayerData.ColumnTimeSpent,
() => "Most time spent", FormatTimespan,
false, " CAST(TimeSpent as BIGINT) "),
false, " CAST(TimeSpent as unsigned) "),
};
public static string FormatInteger(string input) {

View File

@ -117,10 +117,18 @@ namespace MCGalaxy {
internal static void ImportSql(Stream sql) {
// Import data (we only have CREATE TABLE and INSERT INTO statements)
using (StreamReader reader = new StreamReader(sql))
using (BulkTransaction helper = Database.Backend.CreateBulk())
{
List<string> buffer = new List<string>();
using (StreamReader reader = new StreamReader(sql)) {
ImportBulk(reader);
}
}
static void ImportBulk(StreamReader reader) {
BulkTransaction helper = null;
List<string> buffer = new List<string>();
try {
helper = Database.Backend.CreateBulk();
while (!reader.EndOfStream) {
string cmd = NextStatement(reader, buffer);
if (cmd == null || cmd.Length == 0) continue;
@ -129,9 +137,17 @@ namespace MCGalaxy {
if (index > -1) ParseCreate(ref cmd, index);
//Run the command in the transaction.
helper.Execute(cmd);
if (helper.Execute(cmd)) continue;
// Something went wrong.. commit what we've imported so far.
// We need to recreate connection otherwise every helper.Execute fails
helper.Commit();
helper.Dispose();
helper = Database.Backend.CreateBulk();
}
helper.Commit();
} finally {
if (helper != null) helper.Dispose();
}
}
@ -164,7 +180,7 @@ namespace MCGalaxy {
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
before = before.Substring(0, before.LastIndexOfAny(sepChars)); // get rid of column type
int nameStart = before.LastIndexOfAny(startChars) + 1;
string name = before.Substring(nameStart);