SQL log actually ensures its table exists

This commit is contained in:
White 2015-03-23 12:00:37 +10:30
parent c7ce6f3f78
commit 0f7d421bc6

View file

@ -22,6 +22,7 @@ using System.Data;
using System.Diagnostics; using System.Diagnostics;
using System.Globalization; using System.Globalization;
using System.Linq; using System.Linq;
using MySql.Data.MySqlClient;
using TShockAPI.DB; using TShockAPI.DB;
namespace TShockAPI namespace TShockAPI
@ -55,7 +56,7 @@ namespace TShockAPI
/// <summary> /// <summary>
/// Sets the database connection and the initial log level. /// Sets the database connection and the initial log level.
/// </summary> /// </summary>
/// <param name="db"></param> /// <param name="db">Database connection</param>
/// <param name="textlogFilepath">File path to a backup text log in case the SQL log fails</param> /// <param name="textlogFilepath">File path to a backup text log in case the SQL log fails</param>
/// <param name="clearTextLog"></param> /// <param name="clearTextLog"></param>
public SqlLog(IDbConnection db, string textlogFilepath, bool clearTextLog) public SqlLog(IDbConnection db, string textlogFilepath, bool clearTextLog)
@ -63,6 +64,20 @@ namespace TShockAPI
FileName = string.Format("{0}://database", db.GetSqlType()); FileName = string.Format("{0}://database", db.GetSqlType());
_database = db; _database = db;
_backupLog = new TextLog(textlogFilepath, clearTextLog); _backupLog = new TextLog(textlogFilepath, clearTextLog);
var table = new SqlTable("Logs",
new SqlColumn("ID", MySqlDbType.Int32) {AutoIncrement = true, Primary = true},
new SqlColumn("TimeStamp", MySqlDbType.Text),
new SqlColumn("LogLevel", MySqlDbType.Int32),
new SqlColumn("Caller", MySqlDbType.Text),
new SqlColumn("Message", MySqlDbType.Text)
);
var creator = new SqlTableCreator(db,
db.GetSqlType() == SqlType.Sqlite
? (IQueryBuilder)new SqliteQueryCreator()
: new MysqlQueryCreator());
creator.EnsureTableStructure(table);
} }
public bool MayWriteType(TraceLevel type) public bool MayWriteType(TraceLevel type)
@ -232,9 +247,8 @@ namespace TShockAPI
return; return;
} }
_database.Query("INSERT INTO Logs (LogLevel, TimeStamp, Caller, Message) VALUES (@0, @1, @2, @3)", _database.Query("INSERT INTO Logs (TimeStamp, Caller, LogLevel, Message) VALUES (@0, @1, @2, @3)",
level, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture), DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture), caller, (int)level, message);
caller, message);
var success = true; var success = true;
while (_failures.Count > 0 && success) while (_failures.Count > 0 && success)
@ -243,8 +257,8 @@ namespace TShockAPI
try try
{ {
_database.Query("INSERT INTO Logs (LogLevel, TimeStamp, Caller, Message) VALUES (@0, @1, @2, @3)", _database.Query("INSERT INTO Logs (TimeStamp, Caller, LogLevel, Message) VALUES (@0, @1, @2, @3)",
info.logLevel, info.timestamp, info.caller, info.message); info.timestamp, info.caller, (int)info.logLevel, info.message);
} }
catch (Exception ex) catch (Exception ex)
{ {