From d821da7c0bd5a9fb66751bdcb7e8454f20d61712 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Sat, 19 Nov 2016 21:42:58 -0500 Subject: fix param order --- .../Notifications/SqliteNotificationsRepository.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'Emby.Server.Implementations/Notifications/SqliteNotificationsRepository.cs') diff --git a/Emby.Server.Implementations/Notifications/SqliteNotificationsRepository.cs b/Emby.Server.Implementations/Notifications/SqliteNotificationsRepository.cs index ee900ef33..2f3bee788 100644 --- a/Emby.Server.Implementations/Notifications/SqliteNotificationsRepository.cs +++ b/Emby.Server.Implementations/Notifications/SqliteNotificationsRepository.cs @@ -279,7 +279,7 @@ namespace Emby.Server.Implementations.Notifications { connection.RunInTransaction(conn => { - conn.Execute("update Notifications set IsRead=? where UserId=?", userId.ToGuidParamValue(), isRead); + conn.Execute("update Notifications set IsRead=? where UserId=?", isRead, userId.ToGuidParamValue()); }); } } @@ -299,7 +299,7 @@ namespace Emby.Server.Implementations.Notifications foreach (var id in notificationIdList) { - conn.Execute("update Notifications set IsRead=? where UserId=? and Id=?", userIdParam, isRead, id); + conn.Execute("update Notifications set IsRead=? where UserId=? and Id=?", isRead, userIdParam, id); } }); } -- cgit v1.2.3 From 64d15be8390c6174eb7ded067715c226038b38fc Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Sun, 20 Nov 2016 00:59:36 -0500 Subject: update queries --- Emby.Server.Core/Data/SqliteItemRepository.cs | 4 +- .../Activity/ActivityRepository.cs | 27 +++-- .../Data/SqliteDisplayPreferencesRepository.cs | 51 +++++---- .../Data/SqliteExtensions.cs | 48 +++++++- .../Data/SqliteUserRepository.cs | 20 ++-- .../Notifications/SqliteNotificationsRepository.cs | 71 ++++++++---- .../Security/AuthenticationRepository.cs | 121 ++++++++++++++------- Emby.Server.Implementations/Sync/SyncRepository.cs | 34 ++++-- .../Persistence/IItemRepository.cs | 2 +- 9 files changed, 253 insertions(+), 125 deletions(-) (limited to 'Emby.Server.Implementations/Notifications/SqliteNotificationsRepository.cs') diff --git a/Emby.Server.Core/Data/SqliteItemRepository.cs b/Emby.Server.Core/Data/SqliteItemRepository.cs index 5b8f18088..97d703602 100644 --- a/Emby.Server.Core/Data/SqliteItemRepository.cs +++ b/Emby.Server.Core/Data/SqliteItemRepository.cs @@ -679,7 +679,7 @@ namespace Emby.Server.Core.Data throw new ArgumentNullException("item"); } - return SaveItems(new[] { item }, cancellationToken); + return SaveItems(new List { item }, cancellationToken); } /// @@ -693,7 +693,7 @@ namespace Emby.Server.Core.Data /// or /// cancellationToken /// - public async Task SaveItems(IEnumerable items, CancellationToken cancellationToken) + public async Task SaveItems(List items, CancellationToken cancellationToken) { if (items == null) { diff --git a/Emby.Server.Implementations/Activity/ActivityRepository.cs b/Emby.Server.Implementations/Activity/ActivityRepository.cs index 8f64f04db..aaa0b2f5d 100644 --- a/Emby.Server.Implementations/Activity/ActivityRepository.cs +++ b/Emby.Server.Implementations/Activity/ActivityRepository.cs @@ -57,18 +57,21 @@ namespace Emby.Server.Implementations.Activity { connection.RunInTransaction(db => { - var commandText = "replace into ActivityLogEntries (Id, Name, Overview, ShortOverview, Type, ItemId, UserId, DateCreated, LogSeverity) values (?, ?, ?, ?, ?, ?, ?, ?, ?)"; - - db.Execute(commandText, - entry.Id.ToGuidParamValue(), - entry.Name, - entry.Overview, - entry.ShortOverview, - entry.Type, - entry.ItemId, - entry.UserId, - entry.Date.ToDateTimeParamValue(), - entry.Severity.ToString()); + using (var statement = db.PrepareStatement("replace into ActivityLogEntries (Id, Name, Overview, ShortOverview, Type, ItemId, UserId, DateCreated, LogSeverity) values (@Id, @Name, @Overview, @ShortOverview, @Type, @ItemId, @UserId, @DateCreated, @LogSeverity)")) + { + statement.BindParameters.TryBind("@Id", entry.Id.ToGuidParamValue()); + statement.BindParameters.TryBind("@Name", entry.Name); + + statement.BindParameters.TryBind("@Overview", entry.Overview); + statement.BindParameters.TryBind("@ShortOverview", entry.ShortOverview); + statement.BindParameters.TryBind("@Type", entry.Type); + statement.BindParameters.TryBind("@ItemId", entry.ItemId); + statement.BindParameters.TryBind("@UserId", entry.UserId); + statement.BindParameters.TryBind("@DateCreated", entry.Date.ToDateTimeParamValue()); + statement.BindParameters.TryBind("@LogSeverity", entry.Severity.ToString()); + + statement.MoveNext(); + } }); } } diff --git a/Emby.Server.Implementations/Data/SqliteDisplayPreferencesRepository.cs b/Emby.Server.Implementations/Data/SqliteDisplayPreferencesRepository.cs index 1fbf9b0a9..1c592048e 100644 --- a/Emby.Server.Implementations/Data/SqliteDisplayPreferencesRepository.cs +++ b/Emby.Server.Implementations/Data/SqliteDisplayPreferencesRepository.cs @@ -100,14 +100,17 @@ namespace Emby.Server.Implementations.Data private void SaveDisplayPreferences(DisplayPreferences displayPreferences, Guid userId, string client, IDatabaseConnection connection) { - var commandText = "replace into userdisplaypreferences (id, userid, client, data) values (?, ?, ?, ?)"; - var serialized = _jsonSerializer.SerializeToBytes(displayPreferences, _memoryStreamProvider); - - connection.Execute(commandText, - displayPreferences.Id.ToGuidParamValue(), - userId.ToGuidParamValue(), - client, - serialized); + using (var statement = connection.PrepareStatement("replace into userdisplaypreferences (id, userid, client, data) values (@id, @userid, @client, @data)")) + { + var serialized = _jsonSerializer.SerializeToBytes(displayPreferences, _memoryStreamProvider); + + statement.BindParameters.TryBind("@id", displayPreferences.Id.ToGuidParamValue()); + statement.BindParameters.TryBind("@userId", userId.ToGuidParamValue()); + statement.BindParameters.TryBind("@client", client); + statement.BindParameters.TryBind("@data", serialized); + + statement.MoveNext(); + } } /// @@ -163,16 +166,16 @@ namespace Emby.Server.Implementations.Data { using (var connection = CreateConnection(true)) { - var commandText = "select data from userdisplaypreferences where id = ? and userId=? and client=?"; - - var paramList = new List(); - paramList.Add(guidId.ToGuidParamValue()); - paramList.Add(userId.ToGuidParamValue()); - paramList.Add(client); - - foreach (var row in connection.Query(commandText, paramList.ToArray())) + using (var statement = connection.PrepareStatement("select data from userdisplaypreferences where id = @id and userId=@userId and client=@client")) { - return Get(row); + statement.BindParameters.TryBind("@id", guidId.ToGuidParamValue()); + statement.BindParameters.TryBind("@userId", userId.ToGuidParamValue()); + statement.BindParameters.TryBind("@client", client); + + foreach (var row in statement.ExecuteQuery()) + { + return Get(row); + } } return new DisplayPreferences @@ -197,14 +200,14 @@ namespace Emby.Server.Implementations.Data { using (var connection = CreateConnection(true)) { - var commandText = "select data from userdisplaypreferences where userId=?"; - - var paramList = new List(); - paramList.Add(userId.ToGuidParamValue()); - - foreach (var row in connection.Query(commandText, paramList.ToArray())) + using (var statement = connection.PrepareStatement("select data from userdisplaypreferences where userId=@userId")) { - list.Add(Get(row)); + statement.BindParameters.TryBind("@userId", userId.ToGuidParamValue()); + + foreach (var row in statement.ExecuteQuery()) + { + list.Add(Get(row)); + } } } } diff --git a/Emby.Server.Implementations/Data/SqliteExtensions.cs b/Emby.Server.Implementations/Data/SqliteExtensions.cs index 014211924..1cc8a8a93 100644 --- a/Emby.Server.Implementations/Data/SqliteExtensions.cs +++ b/Emby.Server.Implementations/Data/SqliteExtensions.cs @@ -168,14 +168,54 @@ namespace Emby.Server.Implementations.Data return result[index].ToFloat(); } - public static DateTime GetDateTime(this IReadOnlyList result, int index) + public static Guid GetGuid(this IReadOnlyList result, int index) { - return result[index].ReadDateTime(); + return result[index].ReadGuid(); } - public static Guid GetGuid(this IReadOnlyList result, int index) + public static void TryBind(this IReadOnlyDictionary bindParameters, string name, string value) { - return result[index].ReadGuid(); + IBindParameter bindParam; + if (bindParameters.TryGetValue(name, out bindParam)) + { + bindParam.Bind(value); + } + } + + public static void TryBind(this IReadOnlyDictionary bindParameters, string name, bool value) + { + IBindParameter bindParam; + if (bindParameters.TryGetValue(name, out bindParam)) + { + bindParam.Bind(value); + } + } + + public static void TryBind(this IReadOnlyDictionary bindParameters, string name, byte[] value) + { + IBindParameter bindParam; + if (bindParameters.TryGetValue(name, out bindParam)) + { + bindParam.Bind(value); + } + } + + public static void TryBindNull(this IReadOnlyDictionary bindParameters, string name) + { + IBindParameter bindParam; + if (bindParameters.TryGetValue(name, out bindParam)) + { + bindParam.BindNull(); + } + } + + public static IEnumerable> ExecuteQuery( + this IStatement This) + { + while (This.MoveNext()) + { + yield return This.Current; + } } } } diff --git a/Emby.Server.Implementations/Data/SqliteUserRepository.cs b/Emby.Server.Implementations/Data/SqliteUserRepository.cs index f0e38f8c0..ee496b669 100644 --- a/Emby.Server.Implementations/Data/SqliteUserRepository.cs +++ b/Emby.Server.Implementations/Data/SqliteUserRepository.cs @@ -89,11 +89,12 @@ namespace Emby.Server.Implementations.Data { connection.RunInTransaction(db => { - var commandText = "replace into users (guid, data) values (?, ?)"; - - db.Execute(commandText, - user.Id.ToGuidParamValue(), - serialized); + using (var statement = db.PrepareStatement("replace into users (guid, data) values (@guid, @data)")) + { + statement.BindParameters.TryBind("@guid", user.Id.ToGuidParamValue()); + statement.BindParameters.TryBind("@data", serialized); + statement.MoveNext(); + } }); } } @@ -151,10 +152,11 @@ namespace Emby.Server.Implementations.Data { connection.RunInTransaction(db => { - var commandText = "delete from users where guid=?"; - - db.Execute(commandText, - user.Id.ToGuidParamValue()); + using (var statement = db.PrepareStatement("delete from users where guid=@id")) + { + statement.BindParameters.TryBind("@id", user.Id.ToGuidParamValue()); + statement.MoveNext(); + } }); } } diff --git a/Emby.Server.Implementations/Notifications/SqliteNotificationsRepository.cs b/Emby.Server.Implementations/Notifications/SqliteNotificationsRepository.cs index 2f3bee788..15322dcd3 100644 --- a/Emby.Server.Implementations/Notifications/SqliteNotificationsRepository.cs +++ b/Emby.Server.Implementations/Notifications/SqliteNotificationsRepository.cs @@ -107,17 +107,23 @@ namespace Emby.Server.Implementations.Notifications { using (var connection = CreateConnection(true)) { - foreach (var row in connection.Query("select Level from Notifications where UserId=? and IsRead=?", userId.ToGuidParamValue(), false)) + using (var statement = connection.PrepareStatement("select Level from Notifications where UserId=@UserId and IsRead=@IsRead")) { - var levels = new List(); + statement.BindParameters.TryBind("@IsRead", false); + statement.BindParameters.TryBind("@UserId", userId.ToGuidParamValue()); - levels.Add(GetLevel(row, 0)); + foreach (var row in statement.ExecuteQuery()) + { + var levels = new List(); - result.UnreadCount = levels.Count; + levels.Add(GetLevel(row, 0)); - if (levels.Count > 0) - { - result.MaxUnreadNotificationLevel = levels.Max(); + result.UnreadCount = levels.Count; + + if (levels.Count > 0) + { + result.MaxUnreadNotificationLevel = levels.Max(); + } } } @@ -220,17 +226,21 @@ namespace Emby.Server.Implementations.Notifications { connection.RunInTransaction(conn => { - conn.Execute("replace into Notifications (Id, UserId, Date, Name, Description, Url, Level, IsRead, Category, RelatedId) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", - notification.Id.ToGuidParamValue(), - notification.UserId.ToGuidParamValue(), - notification.Date.ToDateTimeParamValue(), - notification.Name, - notification.Description, - notification.Url, - notification.Level.ToString(), - notification.IsRead, - string.Empty, - string.Empty); + using (var statement = conn.PrepareStatement("replace into Notifications (Id, UserId, Date, Name, Description, Url, Level, IsRead, Category, RelatedId) values (@Id, @UserId, @Date, @Name, @Description, @Url, @Level, @IsRead, @Category, @RelatedId)")) + { + statement.BindParameters.TryBind("@Id", notification.Id.ToGuidParamValue()); + statement.BindParameters.TryBind("@UserId", notification.UserId.ToGuidParamValue()); + statement.BindParameters.TryBind("@Date", notification.Date.ToDateTimeParamValue()); + statement.BindParameters.TryBind("@Name", notification.Name); + statement.BindParameters.TryBind("@Description", notification.Description); + statement.BindParameters.TryBind("@Url", notification.Url); + statement.BindParameters.TryBind("@Level", notification.Level.ToString()); + statement.BindParameters.TryBind("@IsRead", notification.IsRead); + statement.BindParameters.TryBind("@Category", string.Empty); + statement.BindParameters.TryBind("@RelatedId", string.Empty); + + statement.MoveNext(); + } }); } } @@ -279,7 +289,13 @@ namespace Emby.Server.Implementations.Notifications { connection.RunInTransaction(conn => { - conn.Execute("update Notifications set IsRead=? where UserId=?", isRead, userId.ToGuidParamValue()); + using (var statement = conn.PrepareStatement("update Notifications set IsRead=@IsRead where UserId=@UserId")) + { + statement.BindParameters.TryBind("@IsRead", isRead); + statement.BindParameters.TryBind("@UserId", userId.ToGuidParamValue()); + + statement.MoveNext(); + } }); } } @@ -295,12 +311,21 @@ namespace Emby.Server.Implementations.Notifications { connection.RunInTransaction(conn => { - var userIdParam = userId.ToGuidParamValue(); - - foreach (var id in notificationIdList) + using (var statement = conn.PrepareStatement("update Notifications set IsRead=@IsRead where UserId=@UserId and Id=@Id")) { - conn.Execute("update Notifications set IsRead=? where UserId=? and Id=?", isRead, userIdParam, id); + statement.BindParameters.TryBind("@IsRead", isRead); + statement.BindParameters.TryBind("@UserId", userId.ToGuidParamValue()); + + foreach (var id in notificationIdList) + { + statement.Reset(); + + statement.BindParameters.TryBind("@Id", id.ToGuidParamValue()); + + statement.MoveNext(); + } } + }); } } diff --git a/Emby.Server.Implementations/Security/AuthenticationRepository.cs b/Emby.Server.Implementations/Security/AuthenticationRepository.cs index f6163b80a..160e0f5d2 100644 --- a/Emby.Server.Implementations/Security/AuthenticationRepository.cs +++ b/Emby.Server.Implementations/Security/AuthenticationRepository.cs @@ -69,19 +69,30 @@ namespace Emby.Server.Implementations.Security { connection.RunInTransaction(db => { - var commandText = "replace into AccessTokens (Id, AccessToken, DeviceId, AppName, AppVersion, DeviceName, UserId, IsActive, DateCreated, DateRevoked) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; - - db.Execute(commandText, - info.Id.ToGuidParamValue(), - info.AccessToken, - info.DeviceId, - info.AppName, - info.AppVersion, - info.DeviceName, - info.UserId, - info.IsActive, - info.DateCreated.ToDateTimeParamValue(), - info.DateRevoked.HasValue ? info.DateRevoked.Value.ToDateTimeParamValue() : null); + using (var statement = db.PrepareStatement("replace into AccessTokens (Id, AccessToken, DeviceId, AppName, AppVersion, DeviceName, UserId, IsActive, DateCreated, DateRevoked) values (@Id, @AccessToken, @DeviceId, @AppName, @AppVersion, @DeviceName, @UserId, @IsActive, @DateCreated, @DateRevoked)")) + { + statement.BindParameters.TryBind("@Id", info.Id.ToGuidParamValue()); + statement.BindParameters.TryBind("@AccessToken", info.AccessToken); + + statement.BindParameters.TryBind("@DeviceId", info.DeviceId); + statement.BindParameters.TryBind("@AppName", info.AppName); + statement.BindParameters.TryBind("@AppVersion", info.AppVersion); + statement.BindParameters.TryBind("@DeviceName", info.DeviceName); + statement.BindParameters.TryBind("@UserId", info.UserId); + statement.BindParameters.TryBind("@IsActive", info.IsActive); + statement.BindParameters.TryBind("@DateCreated", info.DateCreated.ToDateTimeParamValue()); + + if (info.DateRevoked.HasValue) + { + statement.BindParameters.TryBind("@DateRevoked", info.DateRevoked.Value.ToDateTimeParamValue()); + } + else + { + statement.BindParameters.TryBindNull("@DateRevoked"); + } + + statement.MoveNext(); + } }); } } @@ -89,6 +100,29 @@ namespace Emby.Server.Implementations.Security private const string BaseSelectText = "select Id, AccessToken, DeviceId, AppName, AppVersion, DeviceName, UserId, IsActive, DateCreated, DateRevoked from AccessTokens"; + private void BindAuthenticationQueryParams(AuthenticationInfoQuery query, IStatement statement) + { + if (!string.IsNullOrWhiteSpace(query.AccessToken)) + { + statement.BindParameters.TryBind("@AccessToken", query.AccessToken); + } + + if (!string.IsNullOrWhiteSpace(query.UserId)) + { + statement.BindParameters.TryBind("@UserId", query.UserId); + } + + if (!string.IsNullOrWhiteSpace(query.DeviceId)) + { + statement.BindParameters.TryBind("@DeviceId", query.DeviceId); + } + + if (query.IsActive.HasValue) + { + statement.BindParameters.TryBind("@IsActive", query.IsActive.Value); + } + } + public QueryResult Get(AuthenticationInfoQuery query) { if (query == null) @@ -99,7 +133,6 @@ namespace Emby.Server.Implementations.Security using (var connection = CreateConnection(true)) { var commandText = BaseSelectText; - var paramList = new List(); var whereClauses = new List(); @@ -107,26 +140,22 @@ namespace Emby.Server.Implementations.Security if (!string.IsNullOrWhiteSpace(query.AccessToken)) { - whereClauses.Add("AccessToken=?"); - paramList.Add(query.AccessToken); + whereClauses.Add("AccessToken=@AccessToken"); } if (!string.IsNullOrWhiteSpace(query.UserId)) { - whereClauses.Add("UserId=?"); - paramList.Add(query.UserId); + whereClauses.Add("UserId=@UserId"); } if (!string.IsNullOrWhiteSpace(query.DeviceId)) { - whereClauses.Add("DeviceId=?"); - paramList.Add(query.DeviceId); + whereClauses.Add("DeviceId=@DeviceId"); } if (query.IsActive.HasValue) { - whereClauses.Add("IsActive=?"); - paramList.Add(query.IsActive.Value); + whereClauses.Add("IsActive=@IsActive"); } if (query.HasUser.HasValue) @@ -171,20 +200,30 @@ namespace Emby.Server.Implementations.Security var list = new List(); - foreach (var row in connection.Query(commandText, paramList.ToArray())) + using (var statement = connection.PrepareStatement(commandText)) { - list.Add(Get(row)); - } + BindAuthenticationQueryParams(query, statement); - var count = connection.Query("select count (Id) from AccessTokens" + whereTextWithoutPaging, paramList.ToArray()) - .SelectScalarInt() - .First(); + foreach (var row in statement.ExecuteQuery()) + { + list.Add(Get(row)); + } - return new QueryResult() - { - Items = list.ToArray(), - TotalRecordCount = count - }; + using (var totalCountStatement = connection.PrepareStatement("select count (Id) from AccessTokens" + whereTextWithoutPaging)) + { + BindAuthenticationQueryParams(query, totalCountStatement); + + var count = totalCountStatement.ExecuteQuery() + .SelectScalarInt() + .First(); + + return new QueryResult() + { + Items = list.ToArray(), + TotalRecordCount = count + }; + } + } } } @@ -199,16 +238,18 @@ namespace Emby.Server.Implementations.Security { using (var connection = CreateConnection(true)) { - var commandText = BaseSelectText + " where Id=?"; - var paramList = new List(); - - paramList.Add(id.ToGuidParamValue()); + var commandText = BaseSelectText + " where Id=@Id"; - foreach (var row in connection.Query(commandText, paramList.ToArray())) + using (var statement = connection.PrepareStatement(commandText)) { - return Get(row); + statement.BindParameters["@Id"].Bind(id.ToGuidParamValue()); + + foreach (var row in statement.ExecuteQuery()) + { + return Get(row); + } + return null; } - return null; } } } diff --git a/Emby.Server.Implementations/Sync/SyncRepository.cs b/Emby.Server.Implementations/Sync/SyncRepository.cs index 2877a8ffd..bbd23831c 100644 --- a/Emby.Server.Implementations/Sync/SyncRepository.cs +++ b/Emby.Server.Implementations/Sync/SyncRepository.cs @@ -492,14 +492,11 @@ namespace Emby.Server.Implementations.Sync using (var connection = CreateConnection(true)) { var commandText = "select ItemId,Status,Progress from SyncJobItems"; - var whereClauses = new List(); - var paramList = new List(); if (!string.IsNullOrWhiteSpace(query.TargetId)) { - whereClauses.Add("TargetId=?"); - paramList.Add(query.TargetId); + whereClauses.Add("TargetId=@TargetId"); } if (query.Statuses.Length > 0) @@ -514,22 +511,39 @@ namespace Emby.Server.Implementations.Sync commandText += " where " + string.Join(" AND ", whereClauses.ToArray()); } - foreach (var row in connection.Query(commandText, paramList.ToArray())) + using (var statement = connection.PrepareStatement(commandText)) { - AddStatusResult(row, result, false); + if (!string.IsNullOrWhiteSpace(query.TargetId)) + { + statement.BindParameters.TryBind("@TargetId", query.TargetId); + } + + foreach (var row in statement.ExecuteQuery()) + { + AddStatusResult(row, result, false); + } + LogQueryTime("GetSyncedItemProgresses", commandText, now); } - LogQueryTime("GetSyncedItemProgresses", commandText, now); commandText = commandText .Replace("select ItemId,Status,Progress from SyncJobItems", "select ItemIds,Status,Progress from SyncJobs") .Replace("'Synced'", "'Completed','CompletedWithError'"); now = DateTime.UtcNow; - foreach (var row in connection.Query(commandText, paramList.ToArray())) + + using (var statement = connection.PrepareStatement(commandText)) { - AddStatusResult(row, result, true); + if (!string.IsNullOrWhiteSpace(query.TargetId)) + { + statement.BindParameters.TryBind("@TargetId", query.TargetId); + } + + foreach (var row in statement.ExecuteQuery()) + { + AddStatusResult(row, result, true); + } + LogQueryTime("GetSyncedItemProgresses", commandText, now); } - LogQueryTime("GetSyncedItemProgresses", commandText, now); } } diff --git a/MediaBrowser.Controller/Persistence/IItemRepository.cs b/MediaBrowser.Controller/Persistence/IItemRepository.cs index 87937869d..0de048865 100644 --- a/MediaBrowser.Controller/Persistence/IItemRepository.cs +++ b/MediaBrowser.Controller/Persistence/IItemRepository.cs @@ -51,7 +51,7 @@ namespace MediaBrowser.Controller.Persistence /// The items. /// The cancellation token. /// Task. - Task SaveItems(IEnumerable items, CancellationToken cancellationToken); + Task SaveItems(List items, CancellationToken cancellationToken); /// /// Retrieves the item. -- cgit v1.2.3 From 7f62a99ab508551b83568051a874703ddd50b563 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Sun, 20 Nov 2016 02:10:07 -0500 Subject: update extensions --- .../Activity/ActivityRepository.cs | 20 ++-- .../Data/SqliteDisplayPreferencesRepository.cs | 16 +-- .../Data/SqliteExtensions.cs | 115 ++++++++++++++++++--- .../Data/SqliteUserDataRepository.cs | 34 +++--- .../Data/SqliteUserRepository.cs | 6 +- .../Notifications/SqliteNotificationsRepository.cs | 34 +++--- .../Security/AuthenticationRepository.cs | 30 +++--- Emby.Server.Implementations/Sync/SyncRepository.cs | 4 +- 8 files changed, 173 insertions(+), 86 deletions(-) (limited to 'Emby.Server.Implementations/Notifications/SqliteNotificationsRepository.cs') diff --git a/Emby.Server.Implementations/Activity/ActivityRepository.cs b/Emby.Server.Implementations/Activity/ActivityRepository.cs index aaa0b2f5d..ebf93fa33 100644 --- a/Emby.Server.Implementations/Activity/ActivityRepository.cs +++ b/Emby.Server.Implementations/Activity/ActivityRepository.cs @@ -59,16 +59,16 @@ namespace Emby.Server.Implementations.Activity { using (var statement = db.PrepareStatement("replace into ActivityLogEntries (Id, Name, Overview, ShortOverview, Type, ItemId, UserId, DateCreated, LogSeverity) values (@Id, @Name, @Overview, @ShortOverview, @Type, @ItemId, @UserId, @DateCreated, @LogSeverity)")) { - statement.BindParameters.TryBind("@Id", entry.Id.ToGuidParamValue()); - statement.BindParameters.TryBind("@Name", entry.Name); - - statement.BindParameters.TryBind("@Overview", entry.Overview); - statement.BindParameters.TryBind("@ShortOverview", entry.ShortOverview); - statement.BindParameters.TryBind("@Type", entry.Type); - statement.BindParameters.TryBind("@ItemId", entry.ItemId); - statement.BindParameters.TryBind("@UserId", entry.UserId); - statement.BindParameters.TryBind("@DateCreated", entry.Date.ToDateTimeParamValue()); - statement.BindParameters.TryBind("@LogSeverity", entry.Severity.ToString()); + statement.TryBind("@Id", entry.Id.ToGuidParamValue()); + statement.TryBind("@Name", entry.Name); + + statement.TryBind("@Overview", entry.Overview); + statement.TryBind("@ShortOverview", entry.ShortOverview); + statement.TryBind("@Type", entry.Type); + statement.TryBind("@ItemId", entry.ItemId); + statement.TryBind("@UserId", entry.UserId); + statement.TryBind("@DateCreated", entry.Date.ToDateTimeParamValue()); + statement.TryBind("@LogSeverity", entry.Severity.ToString()); statement.MoveNext(); } diff --git a/Emby.Server.Implementations/Data/SqliteDisplayPreferencesRepository.cs b/Emby.Server.Implementations/Data/SqliteDisplayPreferencesRepository.cs index 1c592048e..184caa4d4 100644 --- a/Emby.Server.Implementations/Data/SqliteDisplayPreferencesRepository.cs +++ b/Emby.Server.Implementations/Data/SqliteDisplayPreferencesRepository.cs @@ -104,10 +104,10 @@ namespace Emby.Server.Implementations.Data { var serialized = _jsonSerializer.SerializeToBytes(displayPreferences, _memoryStreamProvider); - statement.BindParameters.TryBind("@id", displayPreferences.Id.ToGuidParamValue()); - statement.BindParameters.TryBind("@userId", userId.ToGuidParamValue()); - statement.BindParameters.TryBind("@client", client); - statement.BindParameters.TryBind("@data", serialized); + statement.TryBind("@id", displayPreferences.Id.ToGuidParamValue()); + statement.TryBind("@userId", userId.ToGuidParamValue()); + statement.TryBind("@client", client); + statement.TryBind("@data", serialized); statement.MoveNext(); } @@ -168,9 +168,9 @@ namespace Emby.Server.Implementations.Data { using (var statement = connection.PrepareStatement("select data from userdisplaypreferences where id = @id and userId=@userId and client=@client")) { - statement.BindParameters.TryBind("@id", guidId.ToGuidParamValue()); - statement.BindParameters.TryBind("@userId", userId.ToGuidParamValue()); - statement.BindParameters.TryBind("@client", client); + statement.TryBind("@id", guidId.ToGuidParamValue()); + statement.TryBind("@userId", userId.ToGuidParamValue()); + statement.TryBind("@client", client); foreach (var row in statement.ExecuteQuery()) { @@ -202,7 +202,7 @@ namespace Emby.Server.Implementations.Data { using (var statement = connection.PrepareStatement("select data from userdisplaypreferences where userId=@userId")) { - statement.BindParameters.TryBind("@userId", userId.ToGuidParamValue()); + statement.TryBind("@userId", userId.ToGuidParamValue()); foreach (var row in statement.ExecuteQuery()) { diff --git a/Emby.Server.Implementations/Data/SqliteExtensions.cs b/Emby.Server.Implementations/Data/SqliteExtensions.cs index 0f4f703a0..657dadfe5 100644 --- a/Emby.Server.Implementations/Data/SqliteExtensions.cs +++ b/Emby.Server.Implementations/Data/SqliteExtensions.cs @@ -173,69 +173,156 @@ namespace Emby.Server.Implementations.Data return result[index].ReadGuid(); } - public static void TryBind(this IReadOnlyDictionary bindParameters, string name, double value) + public static void TryBind(this IStatement statement, string name, double value) { IBindParameter bindParam; - if (bindParameters.TryGetValue(name, out bindParam)) + if (statement.BindParameters.TryGetValue(name, out bindParam)) { bindParam.Bind(value); } } - public static void TryBind(this IReadOnlyDictionary bindParameters, string name, string value) + public static void TryBind(this IStatement statement, string name, string value) { IBindParameter bindParam; - if (bindParameters.TryGetValue(name, out bindParam)) + if (statement.BindParameters.TryGetValue(name, out bindParam)) { bindParam.Bind(value); } } - public static void TryBind(this IReadOnlyDictionary bindParameters, string name, bool value) + public static void TryBind(this IStatement statement, string name, bool value) { IBindParameter bindParam; - if (bindParameters.TryGetValue(name, out bindParam)) + if (statement.BindParameters.TryGetValue(name, out bindParam)) { bindParam.Bind(value); } } - public static void TryBind(this IReadOnlyDictionary bindParameters, string name, int value) + public static void TryBind(this IStatement statement, string name, float value) { IBindParameter bindParam; - if (bindParameters.TryGetValue(name, out bindParam)) + if (statement.BindParameters.TryGetValue(name, out bindParam)) { bindParam.Bind(value); } } - public static void TryBind(this IReadOnlyDictionary bindParameters, string name, long value) + public static void TryBind(this IStatement statement, string name, int value) { IBindParameter bindParam; - if (bindParameters.TryGetValue(name, out bindParam)) + if (statement.BindParameters.TryGetValue(name, out bindParam)) { bindParam.Bind(value); } } - public static void TryBind(this IReadOnlyDictionary bindParameters, string name, byte[] value) + public static void TryBind(this IStatement statement, string name, Guid value) { IBindParameter bindParam; - if (bindParameters.TryGetValue(name, out bindParam)) + if (statement.BindParameters.TryGetValue(name, out bindParam)) + { + bindParam.Bind(value.ToGuidParamValue()); + } + } + + public static void TryBind(this IStatement statement, string name, DateTime value) + { + IBindParameter bindParam; + if (statement.BindParameters.TryGetValue(name, out bindParam)) + { + bindParam.Bind(value.ToDateTimeParamValue()); + } + } + + public static void TryBind(this IStatement statement, string name, long value) + { + IBindParameter bindParam; + if (statement.BindParameters.TryGetValue(name, out bindParam)) { bindParam.Bind(value); } } - public static void TryBindNull(this IReadOnlyDictionary bindParameters, string name) + public static void TryBind(this IStatement statement, string name, byte[] value) { IBindParameter bindParam; - if (bindParameters.TryGetValue(name, out bindParam)) + if (statement.BindParameters.TryGetValue(name, out bindParam)) + { + bindParam.Bind(value); + } + } + + public static void TryBindNull(this IStatement statement, string name) + { + IBindParameter bindParam; + if (statement.BindParameters.TryGetValue(name, out bindParam)) { bindParam.BindNull(); } } + public static void TryBind(this IStatement statement, string name, DateTime? value) + { + if (value.HasValue) + { + TryBind(statement, name, value.Value); + } + else + { + TryBindNull(statement, name); + } + } + + public static void TryBind(this IStatement statement, string name, Guid? value) + { + if (value.HasValue) + { + TryBind(statement, name, value.Value); + } + else + { + TryBindNull(statement, name); + } + } + + public static void TryBind(this IStatement statement, string name, int? value) + { + if (value.HasValue) + { + TryBind(statement, name, value.Value); + } + else + { + TryBindNull(statement, name); + } + } + + public static void TryBind(this IStatement statement, string name, float? value) + { + if (value.HasValue) + { + TryBind(statement, name, value.Value); + } + else + { + TryBindNull(statement, name); + } + } + + public static void TryBind(this IStatement statement, string name, bool? value) + { + if (value.HasValue) + { + TryBind(statement, name, value.Value); + } + else + { + TryBindNull(statement, name); + } + } + public static IEnumerable> ExecuteQuery( this IStatement This) { diff --git a/Emby.Server.Implementations/Data/SqliteUserDataRepository.cs b/Emby.Server.Implementations/Data/SqliteUserDataRepository.cs index 8aa3698b8..c6b08c4b3 100644 --- a/Emby.Server.Implementations/Data/SqliteUserDataRepository.cs +++ b/Emby.Server.Implementations/Data/SqliteUserDataRepository.cs @@ -152,48 +152,48 @@ // { // using (var statement = _connection.PrepareStatement("replace into userdata (key, userId, rating,played,playCount,isFavorite,playbackPositionTicks,lastPlayedDate,AudioStreamIndex,SubtitleStreamIndex) values (@key, @userId, @rating,@played,@playCount,@isFavorite,@playbackPositionTicks,@lastPlayedDate,@AudioStreamIndex,@SubtitleStreamIndex)")) // { -// statement.BindParameters.TryBind("@UserId", userId.ToGuidParamValue()); -// statement.BindParameters.TryBind("@Key", key); +// statement.TryBind("@UserId", userId.ToGuidParamValue()); +// statement.TryBind("@Key", key); // if (userData.Rating.HasValue) // { -// statement.BindParameters.TryBind("@rating", userData.Rating.Value); +// statement.TryBind("@rating", userData.Rating.Value); // } // else // { -// statement.BindParameters.TryBindNull("@rating"); +// statement.TryBindNull("@rating"); // } -// statement.BindParameters.TryBind("@played", userData.Played); -// statement.BindParameters.TryBind("@playCount", userData.PlayCount); -// statement.BindParameters.TryBind("@isFavorite", userData.IsFavorite); -// statement.BindParameters.TryBind("@playbackPositionTicks", userData.PlaybackPositionTicks); +// statement.TryBind("@played", userData.Played); +// statement.TryBind("@playCount", userData.PlayCount); +// statement.TryBind("@isFavorite", userData.IsFavorite); +// statement.TryBind("@playbackPositionTicks", userData.PlaybackPositionTicks); // if (userData.LastPlayedDate.HasValue) // { -// statement.BindParameters.TryBind("@lastPlayedDate", userData.LastPlayedDate.Value.ToDateTimeParamValue()); +// statement.TryBind("@lastPlayedDate", userData.LastPlayedDate.Value.ToDateTimeParamValue()); // } // else // { -// statement.BindParameters.TryBindNull("@lastPlayedDate"); +// statement.TryBindNull("@lastPlayedDate"); // } // if (userData.AudioStreamIndex.HasValue) // { -// statement.BindParameters.TryBind("@AudioStreamIndex", userData.AudioStreamIndex.Value); +// statement.TryBind("@AudioStreamIndex", userData.AudioStreamIndex.Value); // } // else // { -// statement.BindParameters.TryBindNull("@AudioStreamIndex"); +// statement.TryBindNull("@AudioStreamIndex"); // } // if (userData.SubtitleStreamIndex.HasValue) // { -// statement.BindParameters.TryBind("@SubtitleStreamIndex", userData.SubtitleStreamIndex.Value); +// statement.TryBind("@SubtitleStreamIndex", userData.SubtitleStreamIndex.Value); // } // else // { -// statement.BindParameters.TryBindNull("@SubtitleStreamIndex"); +// statement.TryBindNull("@SubtitleStreamIndex"); // } // statement.MoveNext(); @@ -243,8 +243,8 @@ // using (var statement = _connection.PrepareStatement("select key,userid,rating,played,playCount,isFavorite,playbackPositionTicks,lastPlayedDate,AudioStreamIndex,SubtitleStreamIndex from userdata where key =@Key and userId=@UserId")) // { -// statement.BindParameters.TryBind("@UserId", userId.ToGuidParamValue()); -// statement.BindParameters.TryBind("@Key", key); +// statement.TryBind("@UserId", userId.ToGuidParamValue()); +// statement.TryBind("@Key", key); // foreach (var row in statement.ExecuteQuery()) // { @@ -292,7 +292,7 @@ // { // using (var statement = _connection.PrepareStatement("select key,userid,rating,played,playCount,isFavorite,playbackPositionTicks,lastPlayedDate,AudioStreamIndex,SubtitleStreamIndex from userdata where userId=@UserId")) // { -// statement.BindParameters.TryBind("@UserId", userId.ToGuidParamValue()); +// statement.TryBind("@UserId", userId.ToGuidParamValue()); // foreach (var row in statement.ExecuteQuery()) // { diff --git a/Emby.Server.Implementations/Data/SqliteUserRepository.cs b/Emby.Server.Implementations/Data/SqliteUserRepository.cs index ee496b669..5e148d95a 100644 --- a/Emby.Server.Implementations/Data/SqliteUserRepository.cs +++ b/Emby.Server.Implementations/Data/SqliteUserRepository.cs @@ -91,8 +91,8 @@ namespace Emby.Server.Implementations.Data { using (var statement = db.PrepareStatement("replace into users (guid, data) values (@guid, @data)")) { - statement.BindParameters.TryBind("@guid", user.Id.ToGuidParamValue()); - statement.BindParameters.TryBind("@data", serialized); + statement.TryBind("@guid", user.Id.ToGuidParamValue()); + statement.TryBind("@data", serialized); statement.MoveNext(); } }); @@ -154,7 +154,7 @@ namespace Emby.Server.Implementations.Data { using (var statement = db.PrepareStatement("delete from users where guid=@id")) { - statement.BindParameters.TryBind("@id", user.Id.ToGuidParamValue()); + statement.TryBind("@id", user.Id.ToGuidParamValue()); statement.MoveNext(); } }); diff --git a/Emby.Server.Implementations/Notifications/SqliteNotificationsRepository.cs b/Emby.Server.Implementations/Notifications/SqliteNotificationsRepository.cs index 15322dcd3..5a3b64dbb 100644 --- a/Emby.Server.Implementations/Notifications/SqliteNotificationsRepository.cs +++ b/Emby.Server.Implementations/Notifications/SqliteNotificationsRepository.cs @@ -109,8 +109,8 @@ namespace Emby.Server.Implementations.Notifications { using (var statement = connection.PrepareStatement("select Level from Notifications where UserId=@UserId and IsRead=@IsRead")) { - statement.BindParameters.TryBind("@IsRead", false); - statement.BindParameters.TryBind("@UserId", userId.ToGuidParamValue()); + statement.TryBind("@IsRead", false); + statement.TryBind("@UserId", userId.ToGuidParamValue()); foreach (var row in statement.ExecuteQuery()) { @@ -228,16 +228,16 @@ namespace Emby.Server.Implementations.Notifications { using (var statement = conn.PrepareStatement("replace into Notifications (Id, UserId, Date, Name, Description, Url, Level, IsRead, Category, RelatedId) values (@Id, @UserId, @Date, @Name, @Description, @Url, @Level, @IsRead, @Category, @RelatedId)")) { - statement.BindParameters.TryBind("@Id", notification.Id.ToGuidParamValue()); - statement.BindParameters.TryBind("@UserId", notification.UserId.ToGuidParamValue()); - statement.BindParameters.TryBind("@Date", notification.Date.ToDateTimeParamValue()); - statement.BindParameters.TryBind("@Name", notification.Name); - statement.BindParameters.TryBind("@Description", notification.Description); - statement.BindParameters.TryBind("@Url", notification.Url); - statement.BindParameters.TryBind("@Level", notification.Level.ToString()); - statement.BindParameters.TryBind("@IsRead", notification.IsRead); - statement.BindParameters.TryBind("@Category", string.Empty); - statement.BindParameters.TryBind("@RelatedId", string.Empty); + statement.TryBind("@Id", notification.Id.ToGuidParamValue()); + statement.TryBind("@UserId", notification.UserId.ToGuidParamValue()); + statement.TryBind("@Date", notification.Date.ToDateTimeParamValue()); + statement.TryBind("@Name", notification.Name); + statement.TryBind("@Description", notification.Description); + statement.TryBind("@Url", notification.Url); + statement.TryBind("@Level", notification.Level.ToString()); + statement.TryBind("@IsRead", notification.IsRead); + statement.TryBind("@Category", string.Empty); + statement.TryBind("@RelatedId", string.Empty); statement.MoveNext(); } @@ -291,8 +291,8 @@ namespace Emby.Server.Implementations.Notifications { using (var statement = conn.PrepareStatement("update Notifications set IsRead=@IsRead where UserId=@UserId")) { - statement.BindParameters.TryBind("@IsRead", isRead); - statement.BindParameters.TryBind("@UserId", userId.ToGuidParamValue()); + statement.TryBind("@IsRead", isRead); + statement.TryBind("@UserId", userId.ToGuidParamValue()); statement.MoveNext(); } @@ -313,14 +313,14 @@ namespace Emby.Server.Implementations.Notifications { using (var statement = conn.PrepareStatement("update Notifications set IsRead=@IsRead where UserId=@UserId and Id=@Id")) { - statement.BindParameters.TryBind("@IsRead", isRead); - statement.BindParameters.TryBind("@UserId", userId.ToGuidParamValue()); + statement.TryBind("@IsRead", isRead); + statement.TryBind("@UserId", userId.ToGuidParamValue()); foreach (var id in notificationIdList) { statement.Reset(); - statement.BindParameters.TryBind("@Id", id.ToGuidParamValue()); + statement.TryBind("@Id", id.ToGuidParamValue()); statement.MoveNext(); } diff --git a/Emby.Server.Implementations/Security/AuthenticationRepository.cs b/Emby.Server.Implementations/Security/AuthenticationRepository.cs index 160e0f5d2..a9141f153 100644 --- a/Emby.Server.Implementations/Security/AuthenticationRepository.cs +++ b/Emby.Server.Implementations/Security/AuthenticationRepository.cs @@ -71,24 +71,24 @@ namespace Emby.Server.Implementations.Security { using (var statement = db.PrepareStatement("replace into AccessTokens (Id, AccessToken, DeviceId, AppName, AppVersion, DeviceName, UserId, IsActive, DateCreated, DateRevoked) values (@Id, @AccessToken, @DeviceId, @AppName, @AppVersion, @DeviceName, @UserId, @IsActive, @DateCreated, @DateRevoked)")) { - statement.BindParameters.TryBind("@Id", info.Id.ToGuidParamValue()); - statement.BindParameters.TryBind("@AccessToken", info.AccessToken); + statement.TryBind("@Id", info.Id.ToGuidParamValue()); + statement.TryBind("@AccessToken", info.AccessToken); - statement.BindParameters.TryBind("@DeviceId", info.DeviceId); - statement.BindParameters.TryBind("@AppName", info.AppName); - statement.BindParameters.TryBind("@AppVersion", info.AppVersion); - statement.BindParameters.TryBind("@DeviceName", info.DeviceName); - statement.BindParameters.TryBind("@UserId", info.UserId); - statement.BindParameters.TryBind("@IsActive", info.IsActive); - statement.BindParameters.TryBind("@DateCreated", info.DateCreated.ToDateTimeParamValue()); + statement.TryBind("@DeviceId", info.DeviceId); + statement.TryBind("@AppName", info.AppName); + statement.TryBind("@AppVersion", info.AppVersion); + statement.TryBind("@DeviceName", info.DeviceName); + statement.TryBind("@UserId", info.UserId); + statement.TryBind("@IsActive", info.IsActive); + statement.TryBind("@DateCreated", info.DateCreated.ToDateTimeParamValue()); if (info.DateRevoked.HasValue) { - statement.BindParameters.TryBind("@DateRevoked", info.DateRevoked.Value.ToDateTimeParamValue()); + statement.TryBind("@DateRevoked", info.DateRevoked.Value.ToDateTimeParamValue()); } else { - statement.BindParameters.TryBindNull("@DateRevoked"); + statement.TryBindNull("@DateRevoked"); } statement.MoveNext(); @@ -104,22 +104,22 @@ namespace Emby.Server.Implementations.Security { if (!string.IsNullOrWhiteSpace(query.AccessToken)) { - statement.BindParameters.TryBind("@AccessToken", query.AccessToken); + statement.TryBind("@AccessToken", query.AccessToken); } if (!string.IsNullOrWhiteSpace(query.UserId)) { - statement.BindParameters.TryBind("@UserId", query.UserId); + statement.TryBind("@UserId", query.UserId); } if (!string.IsNullOrWhiteSpace(query.DeviceId)) { - statement.BindParameters.TryBind("@DeviceId", query.DeviceId); + statement.TryBind("@DeviceId", query.DeviceId); } if (query.IsActive.HasValue) { - statement.BindParameters.TryBind("@IsActive", query.IsActive.Value); + statement.TryBind("@IsActive", query.IsActive.Value); } } diff --git a/Emby.Server.Implementations/Sync/SyncRepository.cs b/Emby.Server.Implementations/Sync/SyncRepository.cs index bbd23831c..6fb918f15 100644 --- a/Emby.Server.Implementations/Sync/SyncRepository.cs +++ b/Emby.Server.Implementations/Sync/SyncRepository.cs @@ -515,7 +515,7 @@ namespace Emby.Server.Implementations.Sync { if (!string.IsNullOrWhiteSpace(query.TargetId)) { - statement.BindParameters.TryBind("@TargetId", query.TargetId); + statement.TryBind("@TargetId", query.TargetId); } foreach (var row in statement.ExecuteQuery()) @@ -535,7 +535,7 @@ namespace Emby.Server.Implementations.Sync { if (!string.IsNullOrWhiteSpace(query.TargetId)) { - statement.BindParameters.TryBind("@TargetId", query.TargetId); + statement.TryBind("@TargetId", query.TargetId); } foreach (var row in statement.ExecuteQuery()) -- cgit v1.2.3