diff --git a/NadekoBot/Classes/DBHandler.cs b/NadekoBot/Classes/DBHandler.cs index 3f2963cc..db8dee81 100644 --- a/NadekoBot/Classes/DBHandler.cs +++ b/NadekoBot/Classes/DBHandler.cs @@ -13,129 +13,74 @@ namespace NadekoBot.Classes private string FilePath { get; } = "data/nadekobot.sqlite"; + public SQLiteConnection Connection { get; set; } + static DbHandler() { } public DbHandler() { - using (var conn = new SQLiteConnection(FilePath)) + Connection = new SQLiteConnection(FilePath); + Connection.CreateTable(); + Connection.CreateTable(); + Connection.CreateTable(); + Connection.CreateTable(); + Connection.CreateTable(); + Connection.CreateTable(); + Connection.CreateTable(); + Connection.CreateTable(); + Connection.CreateTable(); + Connection.CreateTable(); + Connection.CreateTable(); + Connection.CreateTable(); + Connection.CreateTable(); + Connection.CreateTable(); + Connection.CreateTable(); + Connection.Execute(Queries.TransactionTriggerQuery); + try { - conn.CreateTable(); - conn.CreateTable(); - conn.CreateTable(); - conn.CreateTable(); - conn.CreateTable(); - conn.CreateTable(); - conn.CreateTable(); - conn.CreateTable(); - conn.CreateTable(); - conn.CreateTable(); - conn.CreateTable(); - conn.CreateTable(); - conn.CreateTable(); - conn.CreateTable(); - conn.CreateTable(); - conn.Execute(Queries.TransactionTriggerQuery); - try - { - conn.Execute(Queries.DeletePlaylistTriggerQuery); - } - catch (Exception ex) - { - Console.WriteLine(ex); - } + Connection.Execute(Queries.DeletePlaylistTriggerQuery); + } + catch (Exception ex) + { + Console.WriteLine(ex); } } internal T FindOne(Expression> p) where T : IDataModel, new() { - using (var conn = new SQLiteConnection(FilePath)) - { - return conn.Table().Where(p).FirstOrDefault(); - } + return Connection.Table().Where(p).FirstOrDefault(); + } internal IList FindAll(Expression> p) where T : IDataModel, new() { - using (var conn = new SQLiteConnection(FilePath)) - { - return conn.Table().Where(p).ToList(); - } - } - internal void DeleteAll() where T : IDataModel - { - using (var conn = new SQLiteConnection(FilePath)) - { - conn.DeleteAll(); - } + return Connection.Table().Where(p).ToList(); + } internal void DeleteWhere(Expression> p) where T : IDataModel, new() { - using (var conn = new SQLiteConnection(FilePath)) - { - var id = conn.Table().Where(p).FirstOrDefault()?.Id; - if (id.HasValue) - conn.Delete(id); - } - } - - internal void InsertData(T o) where T : IDataModel - { - using (var conn = new SQLiteConnection(FilePath)) - { - conn.Insert(o, typeof(T)); - } - } - - internal void InsertMany(T objects) where T : IEnumerable - { - using (var conn = new SQLiteConnection(FilePath)) - { - conn.InsertAll(objects); - } - } - - internal void UpdateData(T o) where T : IDataModel - { - using (var conn = new SQLiteConnection(FilePath)) - { - conn.Update(o, typeof(T)); - } - } - - internal void UpdateAll(IEnumerable objs) where T : IDataModel - { - using (var conn = new SQLiteConnection(FilePath)) - { - conn.UpdateAll(objs); - } + var id = Connection.Table().Where(p).FirstOrDefault()?.Id; + if (id.HasValue) + Connection.Delete(id); } internal HashSet GetAllRows() where T : IDataModel, new() { - using (var conn = new SQLiteConnection(FilePath)) - { - return new HashSet(conn.Table()); - } + return new HashSet(Connection.Table()); } internal CurrencyState GetStateByUserId(long id) { - using (var conn = new SQLiteConnection(FilePath)) - { - return conn.Table().Where(x => x.UserId == id).FirstOrDefault(); - } + return Connection.Table().Where(x => x.UserId == id).FirstOrDefault(); } internal T Delete(int id) where T : IDataModel, new() { - using (var conn = new SQLiteConnection(FilePath)) - { - var found = conn.Find(id); - if (found != null) - conn.Delete(found.Id); - return found; - } + var found = Connection.Find(id); + if (found != null) + Connection.Delete(found.Id); + return found; } /// @@ -143,14 +88,11 @@ namespace NadekoBot.Classes /// internal void Save(T o) where T : IDataModel, new() { - using (var conn = new SQLiteConnection(FilePath)) - { - var found = conn.Find(o.Id); - if (found == null) - conn.Insert(o, typeof(T)); - else - conn.Update(o, typeof(T)); - } + var found = Connection.Find(o.Id); + if (found == null) + Connection.Insert(o, typeof(T)); + else + Connection.Update(o, typeof(T)); } /// @@ -158,20 +100,14 @@ namespace NadekoBot.Classes /// internal void SaveAll(IEnumerable ocol) where T : IDataModel, new() { - using (var conn = new SQLiteConnection(FilePath)) - { - foreach (var o in ocol) - conn.InsertOrReplace(o); - } + foreach (var o in ocol) + Connection.InsertOrReplace(o); } internal T GetRandom(Expression> p) where T : IDataModel, new() { - using (var conn = new SQLiteConnection(FilePath)) - { - var r = new Random(); - return conn.Table().Where(p).ToList().OrderBy(x => r.Next()).FirstOrDefault(); - } + var r = new Random(); + return Connection.Table().Where(p).ToList().OrderBy(x => r.Next()).FirstOrDefault(); } /// /// @@ -180,24 +116,19 @@ namespace NadekoBot.Classes /// internal List GetPlaylistData(int num) { - using (var conn = new SQLiteConnection(FilePath)) - { - return conn.Query( + return Connection.Query( @"SELECT mp.Name as 'Name',mp.Id as 'Id', mp.CreatorName as 'Creator', Count(*) as 'SongCnt' FROM MusicPlaylist as mp INNER JOIN PlaylistSongInfo as psi ON mp.Id = psi.PlaylistId Group BY mp.Name Order By mp.DateAdded desc Limit 20 OFFSET ?", num * 20); - } + } internal IEnumerable GetTopRichest(int n = 10) { - using (var conn = new SQLiteConnection(FilePath)) - { - return conn.Table().OrderByDescending(cs => cs.Value).Take(n).ToList(); - } + return Connection.Table().OrderByDescending(cs => cs.Value).Take(n).ToList(); } } } diff --git a/NadekoBot/Classes/FlowersHandler.cs b/NadekoBot/Classes/FlowersHandler.cs index 6fa042ea..57191c63 100644 --- a/NadekoBot/Classes/FlowersHandler.cs +++ b/NadekoBot/Classes/FlowersHandler.cs @@ -10,7 +10,7 @@ namespace NadekoBot.Classes return; await Task.Run(() => { - DbHandler.Instance.InsertData(new DataModels.CurrencyTransaction + DbHandler.Instance.Connection.Insert(new DataModels.CurrencyTransaction { Reason = reason, UserId = (long)u.Id, @@ -36,7 +36,7 @@ namespace NadekoBot.Classes if (state.Value < amount) return false; - DbHandler.Instance.InsertData(new DataModels.CurrencyTransaction + DbHandler.Instance.Connection.Insert(new DataModels.CurrencyTransaction { Reason = reason, UserId = (long)u.Id, diff --git a/NadekoBot/Classes/IncidentsHandler.cs b/NadekoBot/Classes/IncidentsHandler.cs index 5d8190fe..3042f824 100644 --- a/NadekoBot/Classes/IncidentsHandler.cs +++ b/NadekoBot/Classes/IncidentsHandler.cs @@ -19,7 +19,7 @@ namespace NadekoBot.Classes Read = false }; - DbHandler.Instance.InsertData(incident); + DbHandler.Instance.Connection.Insert(incident, typeof(Incident)); } } } diff --git a/NadekoBot/Classes/NadekoStats.cs b/NadekoBot/Classes/NadekoStats.cs index 3ccbc272..3ba14adb 100644 --- a/NadekoBot/Classes/NadekoStats.cs +++ b/NadekoBot/Classes/NadekoStats.cs @@ -56,7 +56,8 @@ namespace NadekoBot ------------------------------------- "); } - catch { + catch + { Console.WriteLine("Command errored errorring"); } }; @@ -208,7 +209,7 @@ namespace NadekoBot .ConfigureAwait(false); var connectedServers = NadekoBot.Client.Servers.Count(); - Classes.DbHandler.Instance.InsertData(new DataModels.Stats + Classes.DbHandler.Instance.Connection.Insert(new DataModels.Stats { OnlineUsers = onlineUsers, RealOnlineUsers = realOnlineUsers, @@ -252,7 +253,7 @@ namespace NadekoBot try { commandsRan++; - Classes.DbHandler.Instance.InsertData(new DataModels.Command + Classes.DbHandler.Instance.Connection.Insert(new DataModels.Command { ServerId = (long)(e.Server?.Id ?? 0), ServerName = e.Server?.Name ?? "--Direct Message--", diff --git a/NadekoBot/Modules/Administration/AdministrationModule.cs b/NadekoBot/Modules/Administration/AdministrationModule.cs index 036687aa..5ee9568b 100644 --- a/NadekoBot/Modules/Administration/AdministrationModule.cs +++ b/NadekoBot/Modules/Administration/AdministrationModule.cs @@ -842,7 +842,7 @@ namespace NadekoBot.Modules.Administration if (donator == null) return; try { - DbHandler.Instance.InsertData(new Donator + DbHandler.Instance.Connection.Insert(new Donator { Amount = amount, UserName = donator.Name, diff --git a/NadekoBot/Modules/Administration/Commands/IncidentsCommands.cs b/NadekoBot/Modules/Administration/Commands/IncidentsCommands.cs index a5854708..be4d2414 100644 --- a/NadekoBot/Modules/Administration/Commands/IncidentsCommands.cs +++ b/NadekoBot/Modules/Administration/Commands/IncidentsCommands.cs @@ -20,7 +20,7 @@ namespace NadekoBot.Modules.Administration.Commands { var sid = (long)e.Server.Id; var incs = DbHandler.Instance.FindAll(i => i.ServerId == sid && i.Read == false); - DbHandler.Instance.UpdateAll(incs.Select(i => { i.Read = true; return i; })); + DbHandler.Instance.Connection.UpdateAll(incs.Select(i => { i.Read = true; return i; })); await e.User.SendMessage(string.Join("\n----------------------", incs.Select(i => i.Text))); }); @@ -33,7 +33,7 @@ namespace NadekoBot.Modules.Administration.Commands { var sid = (long)e.Server.Id; var incs = DbHandler.Instance.FindAll(i => i.ServerId == sid); - DbHandler.Instance.UpdateAll(incs.Select(i => { i.Read = true; return i; })); + DbHandler.Instance.Connection.UpdateAll(incs.Select(i => { i.Read = true; return i; })); var data = string.Join("\n----------------------\n", incs.Select(i => i.Text)); MemoryStream ms = new MemoryStream(); var sw = new StreamWriter(ms); diff --git a/NadekoBot/Modules/Conversations/Conversations.cs b/NadekoBot/Modules/Conversations/Conversations.cs index 7432803f..3aa69631 100644 --- a/NadekoBot/Modules/Conversations/Conversations.cs +++ b/NadekoBot/Modules/Conversations/Conversations.cs @@ -8,7 +8,6 @@ using NadekoBot.Modules.Permissions.Classes; using System; using System.Diagnostics; using System.IO; -using System.Linq; using System.Text; using System.Threading.Tasks; @@ -42,7 +41,7 @@ namespace NadekoBot.Modules.Conversations if (string.IsNullOrWhiteSpace(text)) return; await Task.Run(() => - Classes.DbHandler.Instance.InsertData(new DataModels.UserQuote() + Classes.DbHandler.Instance.Connection.Insert(new DataModels.UserQuote() { DateAdded = DateTime.Now, Keyword = e.GetArg("keyword").ToLowerInvariant(), diff --git a/NadekoBot/Modules/Games/Commands/SpeedTyping.cs b/NadekoBot/Modules/Games/Commands/SpeedTyping.cs index c224de1d..fa2b3f28 100644 --- a/NadekoBot/Modules/Games/Commands/SpeedTyping.cs +++ b/NadekoBot/Modules/Games/Commands/SpeedTyping.cs @@ -182,7 +182,7 @@ namespace NadekoBot.Modules.Games.Commands { if (!NadekoBot.IsOwner(e.User.Id) || string.IsNullOrWhiteSpace(e.GetArg("text"))) return; - DbHandler.Instance.InsertData(new TypingArticle + DbHandler.Instance.Connection.Insert(new TypingArticle { Text = e.GetArg("text"), DateAdded = DateTime.Now diff --git a/NadekoBot/Modules/Music/MusicModule.cs b/NadekoBot/Modules/Music/MusicModule.cs index d85d550e..45c3a2df 100644 --- a/NadekoBot/Modules/Music/MusicModule.cs +++ b/NadekoBot/Modules/Music/MusicModule.cs @@ -614,11 +614,11 @@ namespace NadekoBot.Modules.Music }; DbHandler.Instance.SaveAll(songInfos); DbHandler.Instance.Save(playlist); - DbHandler.Instance.InsertMany(songInfos.Select(s => new PlaylistSongInfo + DbHandler.Instance.Connection.InsertAll(songInfos.Select(s => new PlaylistSongInfo { PlaylistId = playlist.Id.Value, SongInfoId = s.Id.Value - })); + }), typeof(PlaylistSongInfo)); await e.Channel.SendMessage($"🎵 `Saved playlist as {name}-{playlist.Id}`").ConfigureAwait(false); diff --git a/NadekoBot/Modules/Pokemon/PokemonModule.cs b/NadekoBot/Modules/Pokemon/PokemonModule.cs index ae3ae0d9..7956b5f3 100644 --- a/NadekoBot/Modules/Pokemon/PokemonModule.cs +++ b/NadekoBot/Modules/Pokemon/PokemonModule.cs @@ -319,11 +319,11 @@ namespace NadekoBot.Modules.Pokemon DbHandler.Instance.Delete(Dict[(long)e.User.Id]); } - DbHandler.Instance.InsertData(new UserPokeTypes + DbHandler.Instance.Connection.Insert(new UserPokeTypes { UserId = (long)e.User.Id, type = targetType.Name - }); + }, typeof(UserPokeTypes)); //Now for the response diff --git a/NadekoBot/Modules/Utility/Commands/Remind.cs b/NadekoBot/Modules/Utility/Commands/Remind.cs index dabd4158..bfaca560 100644 --- a/NadekoBot/Modules/Utility/Commands/Remind.cs +++ b/NadekoBot/Modules/Utility/Commands/Remind.cs @@ -171,7 +171,7 @@ namespace NadekoBot.Modules.Utility.Commands UserId = (long)e.User.Id, ServerId = (long)e.Server.Id }; - DbHandler.Instance.InsertData(rem); + DbHandler.Instance.Connection.Insert(rem); reminders.Add(StartNewReminder(rem)); diff --git a/NadekoBot/NadekoBot.csproj b/NadekoBot/NadekoBot.csproj index 1b2e9305..23852358 100644 --- a/NadekoBot/NadekoBot.csproj +++ b/NadekoBot/NadekoBot.csproj @@ -161,8 +161,23 @@ False lib\ScaredFingers.UnitsConversion.dll + + ..\packages\sqlite-net-pcl.1.1.2\lib\portable-net45+wp8+wpa81+win8+MonoAndroid10+MonoTouch10+Xamarin.iOS10\SQLite-net.dll + True + + + ..\packages\SQLitePCL.bundle_green.0.9.2\lib\net45\SQLitePCL.batteries.dll + True + + + ..\packages\SQLitePCL.raw.0.9.2\lib\net45\SQLitePCL.raw.dll + True + + + ..\packages\SQLitePCL.plugin.sqlite3.net45.0.9.2\lib\net45\SQLitePCLPlugin_esqlite3.dll + True + - @@ -296,8 +311,6 @@ Resources.resx - - @@ -550,6 +563,13 @@ + + + This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. + + + +