From 0f7d421bc696905d3e9fa9bdcc9c5f61c7c5051d Mon Sep 17 00:00:00 2001 From: White Date: Mon, 23 Mar 2015 12:00:37 +1030 Subject: [PATCH] SQL log actually ensures its table exists --- TShockAPI/SqlLog.cs | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/TShockAPI/SqlLog.cs b/TShockAPI/SqlLog.cs index 50a7e1bc..f2af1510 100644 --- a/TShockAPI/SqlLog.cs +++ b/TShockAPI/SqlLog.cs @@ -22,6 +22,7 @@ using System.Data; using System.Diagnostics; using System.Globalization; using System.Linq; +using MySql.Data.MySqlClient; using TShockAPI.DB; namespace TShockAPI @@ -55,7 +56,7 @@ namespace TShockAPI /// /// Sets the database connection and the initial log level. /// - /// + /// Database connection /// File path to a backup text log in case the SQL log fails /// public SqlLog(IDbConnection db, string textlogFilepath, bool clearTextLog) @@ -63,6 +64,20 @@ namespace TShockAPI FileName = string.Format("{0}://database", db.GetSqlType()); _database = db; _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) @@ -232,9 +247,8 @@ namespace TShockAPI return; } - _database.Query("INSERT INTO Logs (LogLevel, TimeStamp, Caller, Message) VALUES (@0, @1, @2, @3)", - level, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture), - caller, message); + _database.Query("INSERT INTO Logs (TimeStamp, Caller, LogLevel, Message) VALUES (@0, @1, @2, @3)", + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture), caller, (int)level, message); var success = true; while (_failures.Count > 0 && success) @@ -243,8 +257,8 @@ namespace TShockAPI try { - _database.Query("INSERT INTO Logs (LogLevel, TimeStamp, Caller, Message) VALUES (@0, @1, @2, @3)", - info.logLevel, info.timestamp, info.caller, info.message); + _database.Query("INSERT INTO Logs (TimeStamp, Caller, LogLevel, Message) VALUES (@0, @1, @2, @3)", + info.timestamp, info.caller, (int)info.logLevel, info.message); } catch (Exception ex) {