2021-05-20 12:28:18 -07:00
#nullable disable
2019-11-01 10:38:54 -07:00
#pragma warning disable CS1591
2019-01-06 13:50:43 -07:00
using System ;
2016-11-20 02:46:07 -07:00
using System.Collections.Generic ;
using System.IO ;
using System.Threading ;
2020-05-12 19:25:45 -07:00
using Jellyfin.Data.Entities ;
2023-04-14 04:43:56 -07:00
using MediaBrowser.Controller.Configuration ;
2016-11-20 02:46:07 -07:00
using MediaBrowser.Controller.Entities ;
2019-01-13 12:20:16 -07:00
using MediaBrowser.Controller.Library ;
2016-11-20 02:46:07 -07:00
using MediaBrowser.Controller.Persistence ;
2023-08-21 03:13:32 -07:00
using Microsoft.Data.Sqlite ;
2018-12-13 06:18:25 -07:00
using Microsoft.Extensions.Logging ;
2016-11-20 02:46:07 -07:00
namespace Emby.Server.Implementations.Data
{
public class SqliteUserDataRepository : BaseSqliteRepository , IUserDataRepository
{
2023-04-14 04:43:56 -07:00
private readonly IUserManager _userManager ;
2019-01-17 15:55:05 -07:00
public SqliteUserDataRepository (
2019-09-25 04:24:39 -07:00
ILogger < SqliteUserDataRepository > logger ,
2023-04-14 04:43:56 -07:00
IServerConfigurationManager config ,
IUserManager userManager )
2019-09-25 04:24:39 -07:00
: base ( logger )
2016-11-20 02:46:07 -07:00
{
2023-04-14 04:43:56 -07:00
_userManager = userManager ;
DbFilePath = Path . Combine ( config . ApplicationPaths . DataPath , "library.db" ) ;
2016-11-20 02:46:07 -07:00
}
/// <summary>
2019-09-25 04:24:39 -07:00
/// Opens the connection to the database.
2016-11-20 02:46:07 -07:00
/// </summary>
2023-04-14 04:43:56 -07:00
public override void Initialize ( )
2016-11-20 02:46:07 -07:00
{
2023-04-14 04:43:56 -07:00
base . Initialize ( ) ;
2019-04-02 13:15:18 -07:00
2019-02-26 10:30:13 -07:00
using ( var connection = GetConnection ( ) )
2016-11-20 20:52:58 -07:00
{
2018-09-12 10:26:21 -07:00
var userDatasTableExists = TableExists ( connection , "UserDatas" ) ;
var userDataTableExists = TableExists ( connection , "userdata" ) ;
2016-11-20 20:52:58 -07:00
2023-04-14 04:43:56 -07:00
var users = userDatasTableExists ? null : _userManager . Users ;
2023-08-21 05:12:49 -07:00
using var transaction = connection . BeginTransaction ( ) ;
2023-08-21 13:12:08 -07:00
connection . Execute ( string . Join (
';' ,
2023-08-21 05:12:49 -07:00
"create table if not exists UserDatas (key nvarchar not null, userId INT not null, rating float null, played bit not null, playCount int not null, isFavorite bit not null, playbackPositionTicks bigint not null, lastPlayedDate datetime null, AudioStreamIndex INT, SubtitleStreamIndex INT)" ,
"drop index if exists idx_userdata" ,
"drop index if exists idx_userdata1" ,
"drop index if exists idx_userdata2" ,
"drop index if exists userdataindex1" ,
"drop index if exists userdataindex" ,
"drop index if exists userdataindex3" ,
"drop index if exists userdataindex4" ,
"create unique index if not exists UserDatasIndex1 on UserDatas (key, userId)" ,
"create index if not exists UserDatasIndex2 on UserDatas (key, userId, played)" ,
"create index if not exists UserDatasIndex3 on UserDatas (key, userId, playbackPositionTicks)" ,
2023-08-21 13:12:08 -07:00
"create index if not exists UserDatasIndex4 on UserDatas (key, userId, isFavorite)" ) ) ;
2023-08-21 05:12:49 -07:00
if ( ! userDataTableExists )
{
2023-08-21 13:12:08 -07:00
transaction . Commit ( ) ;
2023-08-21 05:12:49 -07:00
return ;
}
2016-11-20 02:46:07 -07:00
2023-08-21 05:12:49 -07:00
var existingColumnNames = GetColumnNames ( connection , "userdata" ) ;
AddColumn ( connection , "userdata" , "InternalUserId" , "int" , existingColumnNames ) ;
AddColumn ( connection , "userdata" , "AudioStreamIndex" , "int" , existingColumnNames ) ;
AddColumn ( connection , "userdata" , "SubtitleStreamIndex" , "int" , existingColumnNames ) ;
if ( userDatasTableExists )
{
return ;
}
ImportUserIds ( connection , users ) ;
2023-08-21 12:37:18 -07:00
connection . Execute ( "INSERT INTO UserDatas (key, userId, rating, played, playCount, isFavorite, playbackPositionTicks, lastPlayedDate, AudioStreamIndex, SubtitleStreamIndex) SELECT key, InternalUserId, rating, played, playCount, isFavorite, playbackPositionTicks, lastPlayedDate, AudioStreamIndex, SubtitleStreamIndex from userdata where InternalUserId not null" ) ;
2023-08-21 05:12:49 -07:00
transaction . Commit ( ) ;
2016-11-27 12:36:56 -07:00
}
}
2023-08-21 03:13:32 -07:00
private void ImportUserIds ( SqliteConnection db , IEnumerable < User > users )
2016-11-21 13:22:43 -07:00
{
2018-09-12 10:26:21 -07:00
var userIdsWithUserData = GetAllUserIdsWithUserData ( db ) ;
using ( var statement = db . PrepareStatement ( "update userdata set InternalUserId=@InternalUserId where UserId=@UserId" ) )
2016-11-21 13:22:43 -07:00
{
2018-09-12 10:26:21 -07:00
foreach ( var user in users )
{
if ( ! userIdsWithUserData . Contains ( user . Id ) )
{
continue ;
}
2016-11-21 13:22:43 -07:00
2022-02-15 14:27:38 -07:00
statement . TryBind ( "@UserId" , user . Id ) ;
2018-09-12 10:26:21 -07:00
statement . TryBind ( "@InternalUserId" , user . InternalId ) ;
2016-11-21 13:22:43 -07:00
2023-08-21 03:13:32 -07:00
statement . ExecuteNonQuery ( ) ;
2018-09-12 10:26:21 -07:00
}
2016-11-21 13:22:43 -07:00
}
2018-09-12 10:26:21 -07:00
}
2016-11-21 13:22:43 -07:00
2023-08-21 03:13:32 -07:00
private List < Guid > GetAllUserIdsWithUserData ( SqliteConnection db )
2018-09-12 10:26:21 -07:00
{
2019-01-13 13:37:13 -07:00
var list = new List < Guid > ( ) ;
2016-11-21 13:22:43 -07:00
2018-09-12 10:26:21 -07:00
using ( var statement = PrepareStatement ( db , "select DISTINCT UserId from UserData where UserId not null" ) )
2016-11-21 13:22:43 -07:00
{
2018-09-12 10:26:21 -07:00
foreach ( var row in statement . ExecuteQuery ( ) )
2016-11-21 13:22:43 -07:00
{
2018-09-12 10:26:21 -07:00
try
{
2023-08-21 03:13:32 -07:00
list . Add ( row . GetGuid ( 0 ) ) ;
2018-09-12 10:26:21 -07:00
}
2019-03-01 09:12:22 -07:00
catch ( Exception ex )
2018-09-12 10:26:21 -07:00
{
2019-03-01 09:12:22 -07:00
Logger . LogError ( ex , "Error while getting user" ) ;
2018-09-12 10:26:21 -07:00
}
2016-11-21 13:22:43 -07:00
}
2018-09-12 10:26:21 -07:00
}
return list ;
2016-11-21 13:22:43 -07:00
}
2021-09-03 09:46:34 -07:00
/// <inheritdoc />
public void SaveUserData ( long userId , string key , UserItemData userData , CancellationToken cancellationToken )
2016-11-20 02:46:07 -07:00
{
2022-10-06 11:21:23 -07:00
ArgumentNullException . ThrowIfNull ( userData ) ;
2020-06-15 14:43:52 -07:00
2021-09-03 09:46:34 -07:00
if ( userId < = 0 )
2016-11-20 02:46:07 -07:00
{
2021-09-03 09:46:34 -07:00
throw new ArgumentNullException ( nameof ( userId ) ) ;
2016-11-20 02:46:07 -07:00
}
2020-06-15 14:43:52 -07:00
2022-10-13 10:08:00 -07:00
ArgumentException . ThrowIfNullOrEmpty ( key ) ;
2016-11-20 02:46:07 -07:00
2021-09-03 09:46:34 -07:00
PersistUserData ( userId , key , userData , cancellationToken ) ;
2016-11-20 02:46:07 -07:00
}
2021-09-03 09:46:34 -07:00
/// <inheritdoc />
public void SaveAllUserData ( long userId , UserItemData [ ] userData , CancellationToken cancellationToken )
2016-11-20 02:46:07 -07:00
{
2022-10-06 11:21:23 -07:00
ArgumentNullException . ThrowIfNull ( userData ) ;
2020-06-15 14:43:52 -07:00
2021-09-03 09:46:34 -07:00
if ( userId < = 0 )
2016-11-20 02:46:07 -07:00
{
2021-09-03 09:46:34 -07:00
throw new ArgumentNullException ( nameof ( userId ) ) ;
2016-11-20 02:46:07 -07:00
}
2021-09-03 09:46:34 -07:00
PersistAllUserData ( userId , userData , cancellationToken ) ;
2016-11-20 02:46:07 -07:00
}
/// <summary>
/// Persists the user data.
/// </summary>
2019-01-06 13:50:43 -07:00
/// <param name="internalUserId">The user id.</param>
2016-11-20 02:46:07 -07:00
/// <param name="key">The key.</param>
/// <param name="userData">The user data.</param>
/// <param name="cancellationToken">The cancellation token.</param>
2018-09-12 10:26:21 -07:00
public void PersistUserData ( long internalUserId , string key , UserItemData userData , CancellationToken cancellationToken )
2016-11-20 02:46:07 -07:00
{
cancellationToken . ThrowIfCancellationRequested ( ) ;
2019-02-26 10:30:13 -07:00
using ( var connection = GetConnection ( ) )
2023-08-21 05:12:49 -07:00
using ( var transaction = connection . BeginTransaction ( ) )
2016-11-20 02:46:07 -07:00
{
2023-08-21 05:12:49 -07:00
SaveUserData ( connection , internalUserId , key , userData ) ;
transaction . Commit ( ) ;
2016-11-20 02:46:07 -07:00
}
}
2023-08-21 03:13:32 -07:00
private static void SaveUserData ( SqliteConnection db , long internalUserId , string key , UserItemData userData )
2016-11-20 02:46:07 -07:00
{
2018-09-12 10:26:21 -07:00
using ( var statement = db . PrepareStatement ( "replace into UserDatas (key, userId, rating,played,playCount,isFavorite,playbackPositionTicks,lastPlayedDate,AudioStreamIndex,SubtitleStreamIndex) values (@key, @userId, @rating,@played,@playCount,@isFavorite,@playbackPositionTicks,@lastPlayedDate,@AudioStreamIndex,@SubtitleStreamIndex)" ) )
2016-11-20 02:46:07 -07:00
{
2018-09-12 10:26:21 -07:00
statement . TryBind ( "@userId" , internalUserId ) ;
2016-11-21 11:49:07 -07:00
statement . TryBind ( "@key" , key ) ;
2016-11-20 02:46:07 -07:00
if ( userData . Rating . HasValue )
{
statement . TryBind ( "@rating" , userData . Rating . Value ) ;
}
else
{
statement . TryBindNull ( "@rating" ) ;
}
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 . TryBind ( "@lastPlayedDate" , userData . LastPlayedDate . Value . ToDateTimeParamValue ( ) ) ;
}
else
{
statement . TryBindNull ( "@lastPlayedDate" ) ;
}
if ( userData . AudioStreamIndex . HasValue )
{
statement . TryBind ( "@AudioStreamIndex" , userData . AudioStreamIndex . Value ) ;
}
else
{
statement . TryBindNull ( "@AudioStreamIndex" ) ;
}
if ( userData . SubtitleStreamIndex . HasValue )
{
statement . TryBind ( "@SubtitleStreamIndex" , userData . SubtitleStreamIndex . Value ) ;
}
else
{
statement . TryBindNull ( "@SubtitleStreamIndex" ) ;
}
2023-08-21 05:12:49 -07:00
statement . ExecuteNonQuery ( ) ;
2016-11-20 02:46:07 -07:00
}
}
/// <summary>
2020-06-15 15:37:52 -07:00
/// Persist all user data for the specified user.
2016-11-20 02:46:07 -07:00
/// </summary>
2018-09-12 10:26:21 -07:00
private void PersistAllUserData ( long internalUserId , UserItemData [ ] userDataList , CancellationToken cancellationToken )
2016-11-20 02:46:07 -07:00
{
cancellationToken . ThrowIfCancellationRequested ( ) ;
2019-02-26 10:30:13 -07:00
using ( var connection = GetConnection ( ) )
2023-08-21 05:12:49 -07:00
using ( var transaction = connection . BeginTransaction ( ) )
2016-11-20 02:46:07 -07:00
{
2023-08-21 05:12:49 -07:00
foreach ( var userItemData in userDataList )
{
SaveUserData ( connection , internalUserId , userItemData . Key , userItemData ) ;
}
transaction . Commit ( ) ;
2016-11-20 02:46:07 -07:00
}
}
/// <summary>
/// Gets the user data.
/// </summary>
2021-09-03 09:46:34 -07:00
/// <param name="userId">The user id.</param>
2016-11-20 02:46:07 -07:00
/// <param name="key">The key.</param>
/// <returns>Task{UserItemData}.</returns>
2019-01-13 13:37:13 -07:00
/// <exception cref="ArgumentNullException">
2016-11-20 02:46:07 -07:00
/// userId
/// or
2021-09-03 09:46:34 -07:00
/// key.
2016-11-20 02:46:07 -07:00
/// </exception>
2021-09-03 09:46:34 -07:00
public UserItemData GetUserData ( long userId , string key )
2016-11-20 02:46:07 -07:00
{
2021-09-03 09:46:34 -07:00
if ( userId < = 0 )
2016-11-20 02:46:07 -07:00
{
2021-09-03 09:46:34 -07:00
throw new ArgumentNullException ( nameof ( userId ) ) ;
2016-11-20 02:46:07 -07:00
}
2019-02-20 06:26:49 -07:00
2022-10-13 10:08:00 -07:00
ArgumentException . ThrowIfNullOrEmpty ( key ) ;
2016-11-20 02:46:07 -07:00
2023-08-21 12:49:39 -07:00
using ( var connection = GetConnection ( ) )
2016-11-20 02:46:07 -07:00
{
2019-02-20 06:26:49 -07:00
using ( var statement = connection . PrepareStatement ( "select key,userid,rating,played,playCount,isFavorite,playbackPositionTicks,lastPlayedDate,AudioStreamIndex,SubtitleStreamIndex from UserDatas where key =@Key and userId=@UserId" ) )
2016-11-20 02:46:07 -07:00
{
2021-09-03 09:46:34 -07:00
statement . TryBind ( "@UserId" , userId ) ;
2019-02-20 06:26:49 -07:00
statement . TryBind ( "@Key" , key ) ;
2016-12-11 22:49:19 -07:00
2019-02-20 06:26:49 -07:00
foreach ( var row in statement . ExecuteQuery ( ) )
{
return ReadRow ( row ) ;
2016-12-13 08:44:34 -07:00
}
2016-11-20 02:46:07 -07:00
}
2019-02-20 06:26:49 -07:00
return null ;
2016-11-20 02:46:07 -07:00
}
}
2021-09-03 09:46:34 -07:00
public UserItemData GetUserData ( long userId , List < string > keys )
2016-11-20 02:46:07 -07:00
{
2022-10-06 11:21:23 -07:00
ArgumentNullException . ThrowIfNull ( keys ) ;
2016-11-20 02:46:07 -07:00
if ( keys . Count = = 0 )
{
return null ;
}
2021-09-03 09:46:34 -07:00
return GetUserData ( userId , keys [ 0 ] ) ;
2016-11-20 02:46:07 -07:00
}
/// <summary>
2020-06-15 15:37:52 -07:00
/// Return all user-data associated with the given user.
2016-11-20 02:46:07 -07:00
/// </summary>
2021-09-03 09:46:34 -07:00
/// <param name="userId">The internal user id.</param>
2021-08-28 15:32:50 -07:00
/// <returns>The list of user item data.</returns>
2021-09-03 09:46:34 -07:00
public List < UserItemData > GetAllUserData ( long userId )
2016-11-20 02:46:07 -07:00
{
2021-09-03 09:46:34 -07:00
if ( userId < = 0 )
2016-11-20 02:46:07 -07:00
{
2021-09-03 09:46:34 -07:00
throw new ArgumentNullException ( nameof ( userId ) ) ;
2016-11-20 02:46:07 -07:00
}
var list = new List < UserItemData > ( ) ;
2019-02-26 10:30:13 -07:00
using ( var connection = GetConnection ( ) )
2016-11-20 02:46:07 -07:00
{
2019-02-20 06:26:49 -07:00
using ( var statement = connection . PrepareStatement ( "select key,userid,rating,played,playCount,isFavorite,playbackPositionTicks,lastPlayedDate,AudioStreamIndex,SubtitleStreamIndex from UserDatas where userId=@UserId" ) )
2016-11-20 02:46:07 -07:00
{
2021-09-03 09:46:34 -07:00
statement . TryBind ( "@UserId" , userId ) ;
2016-11-20 20:52:58 -07:00
2019-02-20 06:26:49 -07:00
foreach ( var row in statement . ExecuteQuery ( ) )
{
list . Add ( ReadRow ( row ) ) ;
2016-11-20 02:46:07 -07:00
}
}
}
return list ;
}
/// <summary>
2020-06-15 15:37:52 -07:00
/// Read a row from the specified reader into the provided userData object.
2016-11-20 02:46:07 -07:00
/// </summary>
2021-08-28 15:32:50 -07:00
/// <param name="reader">The list of result set values.</param>
/// <returns>The user item data.</returns>
2023-08-21 03:13:32 -07:00
private UserItemData ReadRow ( SqliteDataReader reader )
2016-11-20 02:46:07 -07:00
{
var userData = new UserItemData ( ) ;
userData . Key = reader [ 0 ] . ToString ( ) ;
2020-06-14 02:11:11 -07:00
// userData.UserId = reader[1].ReadGuidFromBlob();
2016-11-20 02:46:07 -07:00
2021-05-16 05:49:11 -07:00
if ( reader . TryGetDouble ( 2 , out var rating ) )
2016-11-20 02:46:07 -07:00
{
2021-05-16 05:49:11 -07:00
userData . Rating = rating ;
2016-11-20 02:46:07 -07:00
}
2023-08-21 03:13:32 -07:00
userData . Played = reader . GetBoolean ( 3 ) ;
userData . PlayCount = reader . GetInt32 ( 4 ) ;
userData . IsFavorite = reader . GetBoolean ( 5 ) ;
userData . PlaybackPositionTicks = reader . GetInt64 ( 6 ) ;
2016-11-20 02:46:07 -07:00
2021-05-16 05:49:11 -07:00
if ( reader . TryReadDateTime ( 7 , out var lastPlayedDate ) )
2016-11-20 02:46:07 -07:00
{
2021-05-16 05:49:11 -07:00
userData . LastPlayedDate = lastPlayedDate ;
2016-11-20 02:46:07 -07:00
}
2021-05-16 10:05:56 -07:00
if ( reader . TryGetInt32 ( 8 , out var audioStreamIndex ) )
2016-11-20 02:46:07 -07:00
{
2021-05-16 05:49:11 -07:00
userData . AudioStreamIndex = audioStreamIndex ;
2016-11-20 02:46:07 -07:00
}
2021-05-16 10:05:56 -07:00
if ( reader . TryGetInt32 ( 9 , out var subtitleStreamIndex ) )
2016-11-20 02:46:07 -07:00
{
2021-05-16 05:49:11 -07:00
userData . SubtitleStreamIndex = subtitleStreamIndex ;
2016-11-20 02:46:07 -07:00
}
return userData ;
}
}
2018-12-13 06:18:25 -07:00
}