Skip to content

Commit

Permalink
SQL query errors should get a line logged in main logfile instead of …
Browse files Browse the repository at this point in the history
…mysql_error.log

Also add a few more comments to Database.cs
  • Loading branch information
UnknownShadow200 committed Sep 26, 2020
1 parent 0b3dd57 commit 4d1893c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
6 changes: 3 additions & 3 deletions MCGalaxy/Chat/Chat.cs
Expand Up @@ -57,13 +57,13 @@ public static class Chat {
}
}

public static string Format(string message, Player p, bool tokens = true, bool emotes = true) {
public static string Format(string message, Player dst, bool tokens = true, bool emotes = true) {
message = Colors.Escape(message);
StringBuilder sb = new StringBuilder(message);
if (tokens) ChatTokens.Apply(sb, p);
if (tokens) ChatTokens.Apply(sb, dst);
if (!emotes) return sb.ToString();

if (p.parseEmotes) {
if (dst.parseEmotes) {
sb.Replace(":)", "(darksmile)");
sb.Replace(":D", "(smile)");
sb.Replace("<3", "(heart)");
Expand Down
21 changes: 13 additions & 8 deletions MCGalaxy/Database/Database.cs
Expand Up @@ -24,17 +24,22 @@ namespace MCGalaxy.SQL {
/// <summary> Callback function invoked on a row returned from an SQL query. </summary>
public delegate object ReaderCallback(IDataRecord record, object arg);

/// <summary> Abstracts a SQL database management engine. </summary>
public static class Database {
public static IDatabaseBackend Backend;
public static bool TableExists(string table) { return Backend.TableExists(table); }
public const string DateFormat = "yyyy-MM-dd HH:mm:ss";

static object ReadInt(IDataRecord record, object arg) { return record.GetInt32(0); }
/// <summary> Counts rows in the given table. </summary>
/// <param name="modifier"> Optional SQL to filter which rows are counted. </param>
public static int CountRows(string table, string modifier = "", params object[] args) {
return (int)Backend.ReadRows(table, "COUNT(*)", null, ReadInt, modifier, args);
}

static object ReadString(IDataRecord record, object arg) { return record.GetText(0); }
/// <summary> Returns value of first column in last row read from the given table. </summary>
/// <param name="modifier"> Optional SQL to filter which rows are read. </param>
public static string ReadString(string table, string column,
string modifier = "", params object[] args) {
return (string)Backend.ReadRows(table, column, null, ReadString, modifier, args);
Expand All @@ -56,6 +61,8 @@ public static class Database {
return arg;
}

/// <summary> Returns all columns of all rows read from the given table. </summary>
/// <param name="modifier"> Optional SQL to filter which rows are read. </param>
public static List<string[]> GetRows(string table, string columns,
string modifier = "", params object[] args) {
List<string[]> fields = new List<string[]>();
Expand Down Expand Up @@ -90,15 +97,13 @@ public static class Database {
e = ex; // try yet again
}
}

try {
File.AppendAllText("MySQL_error.log", DateTime.Now + " " + sql + "\r\n");
} catch { }
Logger.LogError(e);

Logger.LogError("Error executing SQL statement: " + sql, e);
return arg;
}


}

internal static class DatabaseExts {
internal static string GetText(this IDataRecord record, int col) {
return record.IsDBNull(col) ? "" : record.GetString(col);
}
Expand Down Expand Up @@ -127,6 +132,6 @@ public static class Database {
return Database.Backend.RawGetDateTime(record, col);
}
return record.GetValue(col).ToString();
}
}
}
}

0 comments on commit 4d1893c

Please sign in to comment.