@ -29,6 +29,7 @@ using MediaBrowser.Server.Implementations.Devices;
using MediaBrowser.Server.Implementations.Playlists ;
using MediaBrowser.Server.Implementations.Playlists ;
using MediaBrowser.Model.Reflection ;
using MediaBrowser.Model.Reflection ;
using SQLitePCL.pretty ;
using SQLitePCL.pretty ;
using MediaBrowser.Model.System ;
namespace Emby.Server.Implementations.Data
namespace Emby.Server.Implementations.Data
{
{
@ -66,14 +67,14 @@ namespace Emby.Server.Implementations.Data
private readonly string _criticReviewsPath ;
private readonly string _criticReviewsPath ;
public const int LatestSchemaVersion = 109 ;
private readonly IMemoryStreamFactory _memoryStreamProvider ;
private readonly IMemoryStreamFactory _memoryStreamProvider ;
private readonly IFileSystem _fileSystem ;
private readonly IFileSystem _fileSystem ;
private readonly IEnvironmentInfo _environmentInfo ;
/// <summary>
/// <summary>
/// Initializes a new instance of the <see cref="SqliteItemRepository"/> class.
/// Initializes a new instance of the <see cref="SqliteItemRepository"/> class.
/// </summary>
/// </summary>
public SqliteItemRepository ( IServerConfigurationManager config , IJsonSerializer jsonSerializer , ILogger logger , IMemoryStreamFactory memoryStreamProvider , IAssemblyInfo assemblyInfo , IFileSystem fileSystem )
public SqliteItemRepository ( IServerConfigurationManager config , IJsonSerializer jsonSerializer , ILogger logger , IMemoryStreamFactory memoryStreamProvider , IAssemblyInfo assemblyInfo , IFileSystem fileSystem , IEnvironmentInfo environmentInfo )
: base ( logger )
: base ( logger )
{
{
if ( config = = null )
if ( config = = null )
@ -89,6 +90,7 @@ namespace Emby.Server.Implementations.Data
_jsonSerializer = jsonSerializer ;
_jsonSerializer = jsonSerializer ;
_memoryStreamProvider = memoryStreamProvider ;
_memoryStreamProvider = memoryStreamProvider ;
_fileSystem = fileSystem ;
_fileSystem = fileSystem ;
_environmentInfo = environmentInfo ;
_typeMapper = new TypeMapper ( assemblyInfo ) ;
_typeMapper = new TypeMapper ( assemblyInfo ) ;
_criticReviewsPath = Path . Combine ( _config . ApplicationPaths . DataPath , "critic-reviews" ) ;
_criticReviewsPath = Path . Combine ( _config . ApplicationPaths . DataPath , "critic-reviews" ) ;
@ -127,10 +129,18 @@ namespace Emby.Server.Implementations.Data
{
{
_connection = CreateConnection ( false ) ;
_connection = CreateConnection ( false ) ;
_connection . ExecuteAll ( string . Join ( ";" , new [ ]
{
"PRAGMA page_size=4096" ,
"PRAGMA default_temp_store=memory" ,
"PRAGMA temp_store=memory"
} ) ) ;
var createMediaStreamsTableCommand
var createMediaStreamsTableCommand
= "create table if not exists mediastreams (ItemId GUID, StreamIndex INT, StreamType TEXT, Codec TEXT, Language TEXT, ChannelLayout TEXT, Profile TEXT, AspectRatio TEXT, Path TEXT, IsInterlaced BIT, BitRate INT NULL, Channels INT NULL, SampleRate INT NULL, IsDefault BIT, IsForced BIT, IsExternal BIT, Height INT NULL, Width INT NULL, AverageFrameRate FLOAT NULL, RealFrameRate FLOAT NULL, Level FLOAT NULL, PixelFormat TEXT, BitDepth INT NULL, IsAnamorphic BIT NULL, RefFrames INT NULL, CodecTag TEXT NULL, Comment TEXT NULL, NalLengthSize TEXT NULL, IsAvc BIT NULL, Title TEXT NULL, TimeBase TEXT NULL, CodecTimeBase TEXT NULL, PRIMARY KEY (ItemId, StreamIndex))" ;
= "create table if not exists mediastreams (ItemId GUID, StreamIndex INT, StreamType TEXT, Codec TEXT, Language TEXT, ChannelLayout TEXT, Profile TEXT, AspectRatio TEXT, Path TEXT, IsInterlaced BIT, BitRate INT NULL, Channels INT NULL, SampleRate INT NULL, IsDefault BIT, IsForced BIT, IsExternal BIT, Height INT NULL, Width INT NULL, AverageFrameRate FLOAT NULL, RealFrameRate FLOAT NULL, Level FLOAT NULL, PixelFormat TEXT, BitDepth INT NULL, IsAnamorphic BIT NULL, RefFrames INT NULL, CodecTag TEXT NULL, Comment TEXT NULL, NalLengthSize TEXT NULL, IsAvc BIT NULL, Title TEXT NULL, TimeBase TEXT NULL, CodecTimeBase TEXT NULL, PRIMARY KEY (ItemId, StreamIndex))" ;
string [ ] queries = {
string [ ] queries = {
"PRAGMA locking_mode=NORMAL" ,
"create table if not exists TypedBaseItems (guid GUID primary key NOT NULL, type TEXT NOT NULL, data BLOB NULL, ParentId GUID NULL, Path TEXT NULL)" ,
"create table if not exists TypedBaseItems (guid GUID primary key NOT NULL, type TEXT NOT NULL, data BLOB NULL, ParentId GUID NULL, Path TEXT NULL)" ,
@ -184,7 +194,6 @@ namespace Emby.Server.Implementations.Data
AddColumn ( db , "TypedBaseItems" , "ProductionYear" , "INT" , existingColumnNames ) ;
AddColumn ( db , "TypedBaseItems" , "ProductionYear" , "INT" , existingColumnNames ) ;
AddColumn ( db , "TypedBaseItems" , "ParentId" , "GUID" , existingColumnNames ) ;
AddColumn ( db , "TypedBaseItems" , "ParentId" , "GUID" , existingColumnNames ) ;
AddColumn ( db , "TypedBaseItems" , "Genres" , "Text" , existingColumnNames ) ;
AddColumn ( db , "TypedBaseItems" , "Genres" , "Text" , existingColumnNames ) ;
AddColumn ( db , "TypedBaseItems" , "SchemaVersion" , "INT" , existingColumnNames ) ;
AddColumn ( db , "TypedBaseItems" , "SortName" , "Text" , existingColumnNames ) ;
AddColumn ( db , "TypedBaseItems" , "SortName" , "Text" , existingColumnNames ) ;
AddColumn ( db , "TypedBaseItems" , "RunTimeTicks" , "BIGINT" , existingColumnNames ) ;
AddColumn ( db , "TypedBaseItems" , "RunTimeTicks" , "BIGINT" , existingColumnNames ) ;
@ -196,7 +205,6 @@ namespace Emby.Server.Implementations.Data
AddColumn ( db , "TypedBaseItems" , "DateModified" , "DATETIME" , existingColumnNames ) ;
AddColumn ( db , "TypedBaseItems" , "DateModified" , "DATETIME" , existingColumnNames ) ;
AddColumn ( db , "TypedBaseItems" , "ForcedSortName" , "Text" , existingColumnNames ) ;
AddColumn ( db , "TypedBaseItems" , "ForcedSortName" , "Text" , existingColumnNames ) ;
AddColumn ( db , "TypedBaseItems" , "IsOffline" , "BIT" , existingColumnNames ) ;
AddColumn ( db , "TypedBaseItems" , "LocationType" , "Text" , existingColumnNames ) ;
AddColumn ( db , "TypedBaseItems" , "LocationType" , "Text" , existingColumnNames ) ;
AddColumn ( db , "TypedBaseItems" , "IsSeries" , "BIT" , existingColumnNames ) ;
AddColumn ( db , "TypedBaseItems" , "IsSeries" , "BIT" , existingColumnNames ) ;
@ -344,25 +352,26 @@ namespace Emby.Server.Implementations.Data
_connection . RunQueries ( postQueries ) ;
_connection . RunQueries ( postQueries ) ;
SqliteExtensions . Attach ( _connection , Path . Combine ( _config . ApplicationPaths . DataPath , "userdata_v2.db" ) , "UserDataDb" ) ;
//SqliteExtensions.Attach(_connection, Path.Combine(_config.ApplicationPaths.DataPath, "userdata_v2.db"), "UserDataDb") ;
userDataRepo . Initialize ( _connection, WriteLock) ;
userDataRepo . Initialize ( WriteLock) ;
//await Vacuum(_connection).ConfigureAwait(false);
//await Vacuum(_connection).ConfigureAwait(false);
}
}
protected override bool EnableConnectionPooling
{
get
{
return false ;
}
}
pr otected override bool EnableExclusiveMode
private SQLiteDatabaseConnection CreateConnection ( bool readOnly , bool attachUserdata )
{
{
get
Action < SQLiteDatabaseConnection > onConnect = null ;
if ( attachUserdata )
{
{
return true ;
onConnect =
c = > SqliteExtensions . Attach ( c , Path . Combine ( _config . ApplicationPaths . DataPath , "userdata_v2.db" ) ,
"UserDataDb" ) ;
}
}
var conn = CreateConnection ( readOnly , onConnect ) ;
return conn ;
}
}
private readonly string [ ] _retriveItemColumns =
private readonly string [ ] _retriveItemColumns =
@ -371,7 +380,6 @@ namespace Emby.Server.Implementations.Data
"data" ,
"data" ,
"StartDate" ,
"StartDate" ,
"EndDate" ,
"EndDate" ,
"IsOffline" ,
"ChannelId" ,
"ChannelId" ,
"IsMovie" ,
"IsMovie" ,
"IsSports" ,
"IsSports" ,
@ -519,7 +527,6 @@ namespace Emby.Server.Implementations.Data
"ParentId" ,
"ParentId" ,
"Genres" ,
"Genres" ,
"InheritedParentalRatingValue" ,
"InheritedParentalRatingValue" ,
"SchemaVersion" ,
"SortName" ,
"SortName" ,
"RunTimeTicks" ,
"RunTimeTicks" ,
"OfficialRatingDescription" ,
"OfficialRatingDescription" ,
@ -529,7 +536,6 @@ namespace Emby.Server.Implementations.Data
"DateCreated" ,
"DateCreated" ,
"DateModified" ,
"DateModified" ,
"ForcedSortName" ,
"ForcedSortName" ,
"IsOffline" ,
"LocationType" ,
"LocationType" ,
"PreferredMetadataLanguage" ,
"PreferredMetadataLanguage" ,
"PreferredMetadataCountryCode" ,
"PreferredMetadataCountryCode" ,
@ -635,14 +641,17 @@ namespace Emby.Server.Implementations.Data
CheckDisposed ( ) ;
CheckDisposed ( ) ;
using ( var connection = CreateConnection ( ) )
{
using ( WriteLock . Write ( ) )
using ( WriteLock . Write ( ) )
{
{
_ connection. RunInTransaction ( db = >
connection. RunInTransaction ( db = >
{
{
SaveItemsInTranscation ( db , items ) ;
SaveItemsInTranscation ( db , items ) ;
} ) ;
} ) ;
}
}
}
}
}
private void SaveItemsInTranscation ( IDatabaseConnection db , List < BaseItem > items )
private void SaveItemsInTranscation ( IDatabaseConnection db , List < BaseItem > items )
{
{
@ -777,7 +786,6 @@ namespace Emby.Server.Implementations.Data
}
}
saveItemStatement . TryBind ( "@InheritedParentalRatingValue" , item . GetInheritedParentalRatingValue ( ) ? ? 0 ) ;
saveItemStatement . TryBind ( "@InheritedParentalRatingValue" , item . GetInheritedParentalRatingValue ( ) ? ? 0 ) ;
saveItemStatement . TryBind ( "@SchemaVersion" , LatestSchemaVersion ) ;
saveItemStatement . TryBind ( "@SortName" , item . SortName ) ;
saveItemStatement . TryBind ( "@SortName" , item . SortName ) ;
saveItemStatement . TryBind ( "@RunTimeTicks" , item . RunTimeTicks ) ;
saveItemStatement . TryBind ( "@RunTimeTicks" , item . RunTimeTicks ) ;
@ -790,7 +798,6 @@ namespace Emby.Server.Implementations.Data
saveItemStatement . TryBind ( "@DateModified" , item . DateModified ) ;
saveItemStatement . TryBind ( "@DateModified" , item . DateModified ) ;
saveItemStatement . TryBind ( "@ForcedSortName" , item . ForcedSortName ) ;
saveItemStatement . TryBind ( "@ForcedSortName" , item . ForcedSortName ) ;
saveItemStatement . TryBind ( "@IsOffline" , item . IsOffline ) ;
saveItemStatement . TryBind ( "@LocationType" , item . LocationType . ToString ( ) ) ;
saveItemStatement . TryBind ( "@LocationType" , item . LocationType . ToString ( ) ) ;
saveItemStatement . TryBind ( "@PreferredMetadataLanguage" , item . PreferredMetadataLanguage ) ;
saveItemStatement . TryBind ( "@PreferredMetadataLanguage" , item . PreferredMetadataLanguage ) ;
@ -1169,10 +1176,12 @@ namespace Emby.Server.Implementations.Data
}
}
CheckDisposed ( ) ;
CheckDisposed ( ) ;
//Logger.Info("Retrieving item {0}", id.ToString("N"));
using ( WriteLock . Write ( ) )
using ( var connection = CreateConnection ( true ) )
{
using ( WriteLock . Read ( ) )
{
{
using ( var statement = _connection . PrepareStatement ( "select " + string . Join ( "," , _retriveItemColumns ) + " from TypedBaseItems where guid = @guid" ) )
using ( var statement = connection. PrepareStatement ( "select " + string . Join ( "," , _retriveItemColumns ) + " from TypedBaseItems where guid = @guid" ) )
{
{
statement . TryBind ( "@guid" , id ) ;
statement . TryBind ( "@guid" , id ) ;
@ -1182,6 +1191,7 @@ namespace Emby.Server.Implementations.Data
}
}
}
}
}
}
}
return null ;
return null ;
}
}
@ -1353,64 +1363,72 @@ namespace Emby.Server.Implementations.Data
if ( ! reader . IsDBNull ( 4 ) )
if ( ! reader . IsDBNull ( 4 ) )
{
{
item . IsOffline = reader . GetBoolean ( 4 ) ;
item . ChannelId = reader . GetString ( 4 ) ;
}
}
if ( ! reader . IsDBNull ( 5 ) )
var index = 5 ;
{
item . ChannelId = reader . GetString ( 5 ) ;
}
var hasProgramAttributes = item as IHasProgramAttributes ;
var hasProgramAttributes = item as IHasProgramAttributes ;
if ( hasProgramAttributes ! = null )
if ( hasProgramAttributes ! = null )
{
{
if ( ! reader . IsDBNull ( 6 ) )
if ( ! reader . IsDBNull ( index ) )
{
{
hasProgramAttributes . IsMovie = reader . GetBoolean ( 6 ) ;
hasProgramAttributes . IsMovie = reader . GetBoolean ( index ) ;
}
}
index + + ;
if ( ! reader . IsDBNull ( 7 ) )
if ( ! reader . IsDBNull ( index ) )
{
{
hasProgramAttributes . IsSports = reader . GetBoolean ( 7 ) ;
hasProgramAttributes . IsSports = reader . GetBoolean ( index ) ;
}
}
index + + ;
if ( ! reader . IsDBNull ( 8 ) )
if ( ! reader . IsDBNull ( index ) )
{
{
hasProgramAttributes . IsKids = reader . GetBoolean ( 8 ) ;
hasProgramAttributes . IsKids = reader . GetBoolean ( index ) ;
}
}
index + + ;
if ( ! reader . IsDBNull ( 9 ) )
if ( ! reader . IsDBNull ( index ) )
{
{
hasProgramAttributes . IsSeries = reader . GetBoolean ( 9 ) ;
hasProgramAttributes . IsSeries = reader . GetBoolean ( index ) ;
}
}
index + + ;
if ( ! reader . IsDBNull ( 10 ) )
if ( ! reader . IsDBNull ( index ) )
{
{
hasProgramAttributes . IsLive = reader . GetBoolean ( 10 ) ;
hasProgramAttributes . IsLive = reader . GetBoolean ( index ) ;
}
}
index + + ;
if ( ! reader . IsDBNull ( 11 ) )
if ( ! reader . IsDBNull ( index ) )
{
{
hasProgramAttributes . IsNews = reader . GetBoolean ( 11 ) ;
hasProgramAttributes . IsNews = reader . GetBoolean ( index ) ;
}
}
index + + ;
if ( ! reader . IsDBNull ( 12 ) )
if ( ! reader . IsDBNull ( index ) )
{
{
hasProgramAttributes . IsPremiere = reader . GetBoolean ( 12 ) ;
hasProgramAttributes . IsPremiere = reader . GetBoolean ( index ) ;
}
}
index + + ;
if ( ! reader . IsDBNull ( 13 ) )
if ( ! reader . IsDBNull ( index ) )
{
{
hasProgramAttributes . EpisodeTitle = reader . GetString ( 13 ) ;
hasProgramAttributes . EpisodeTitle = reader . GetString ( index ) ;
}
}
index + + ;
if ( ! reader . IsDBNull ( 14 ) )
if ( ! reader . IsDBNull ( index ) )
{
{
hasProgramAttributes . IsRepeat = reader . GetBoolean ( 14 ) ;
hasProgramAttributes . IsRepeat = reader . GetBoolean ( index ) ;
}
}
index + + ;
}
else
{
index + = 9 ;
}
}
var index = 15 ;
if ( ! reader . IsDBNull ( index ) )
if ( ! reader . IsDBNull ( index ) )
{
{
@ -1976,9 +1994,11 @@ namespace Emby.Server.Implementations.Data
var list = new List < ChapterInfo > ( ) ;
var list = new List < ChapterInfo > ( ) ;
using ( WriteLock . Write ( ) )
using ( var connection = CreateConnection ( true ) )
{
{
using ( var statement = _connection . PrepareStatement ( "select StartPositionTicks,Name,ImagePath,ImageDateModified from " + ChaptersTableName + " where ItemId = @ItemId order by ChapterIndex asc" ) )
using ( WriteLock . Read ( ) )
{
using ( var statement = connection . PrepareStatement ( "select StartPositionTicks,Name,ImagePath,ImageDateModified from " + ChaptersTableName + " where ItemId = @ItemId order by ChapterIndex asc" ) )
{
{
statement . TryBind ( "@ItemId" , id ) ;
statement . TryBind ( "@ItemId" , id ) ;
@ -1988,6 +2008,7 @@ namespace Emby.Server.Implementations.Data
}
}
}
}
}
}
}
return list ;
return list ;
}
}
@ -2007,9 +2028,11 @@ namespace Emby.Server.Implementations.Data
throw new ArgumentNullException ( "id" ) ;
throw new ArgumentNullException ( "id" ) ;
}
}
using ( WriteLock . Write ( ) )
using ( var connection = CreateConnection ( true ) )
{
{
using ( var statement = _connection . PrepareStatement ( "select StartPositionTicks,Name,ImagePath,ImageDateModified from " + ChaptersTableName + " where ItemId = @ItemId and ChapterIndex=@ChapterIndex" ) )
using ( WriteLock . Read ( ) )
{
using ( var statement = connection . PrepareStatement ( "select StartPositionTicks,Name,ImagePath,ImageDateModified from " + ChaptersTableName + " where ItemId = @ItemId and ChapterIndex=@ChapterIndex" ) )
{
{
statement . TryBind ( "@ItemId" , id ) ;
statement . TryBind ( "@ItemId" , id ) ;
statement . TryBind ( "@ChapterIndex" , index ) ;
statement . TryBind ( "@ChapterIndex" , index ) ;
@ -2020,6 +2043,7 @@ namespace Emby.Server.Implementations.Data
}
}
}
}
}
}
}
return null ;
return null ;
}
}
@ -2085,12 +2109,14 @@ namespace Emby.Server.Implementations.Data
var index = 0 ;
var index = 0 ;
using ( var connection = CreateConnection ( ) )
{
using ( WriteLock . Write ( ) )
using ( WriteLock . Write ( ) )
{
{
_ connection. RunInTransaction ( db = >
connection. RunInTransaction ( db = >
{
{
// First delete chapters
// First delete chapters
_connection . Execute ( "delete from " + ChaptersTableName + " where ItemId=@ItemId" , id . ToGuidParamValue ( ) ) ;
db . Execute ( "delete from " + ChaptersTableName + " where ItemId=@ItemId" , id . ToGuidParamValue ( ) ) ;
using ( var saveChapterStatement = db . PrepareStatement ( "replace into " + ChaptersTableName + " (ItemId, ChapterIndex, StartPositionTicks, Name, ImagePath, ImageDateModified) values (@ItemId, @ChapterIndex, @StartPositionTicks, @Name, @ImagePath, @ImageDateModified)" ) )
using ( var saveChapterStatement = db . PrepareStatement ( "replace into " + ChaptersTableName + " (ItemId, ChapterIndex, StartPositionTicks, Name, ImagePath, ImageDateModified) values (@ItemId, @ChapterIndex, @StartPositionTicks, @Name, @ImagePath, @ImageDateModified)" ) )
{
{
@ -2116,6 +2142,7 @@ namespace Emby.Server.Implementations.Data
} ) ;
} ) ;
}
}
}
}
}
protected override void CloseConnection ( )
protected override void CloseConnection ( )
{
{
@ -2343,6 +2370,8 @@ namespace Emby.Server.Implementations.Data
CheckDisposed ( ) ;
CheckDisposed ( ) ;
//Logger.Info("GetItemList: " + _environmentInfo.StackTrace);
var now = DateTime . UtcNow ;
var now = DateTime . UtcNow ;
var list = new List < BaseItem > ( ) ;
var list = new List < BaseItem > ( ) ;
@ -2383,9 +2412,11 @@ namespace Emby.Server.Implementations.Data
}
}
}
}
using ( WriteLock . Write ( ) )
using ( var connection = CreateConnection ( true , EnableJoinUserData ( query ) ) )
{
{
using ( var statement = _connection . PrepareStatement ( commandText ) )
using ( WriteLock . Read ( ) )
{
using ( var statement = connection . PrepareStatement ( commandText ) )
{
{
if ( EnableJoinUserData ( query ) )
if ( EnableJoinUserData ( query ) )
{
{
@ -2409,6 +2440,7 @@ namespace Emby.Server.Implementations.Data
LogQueryTime ( "GetItemList" , commandText , now ) ;
LogQueryTime ( "GetItemList" , commandText , now ) ;
}
}
}
// Hack for right now since we currently don't support filtering out these duplicates within a query
// Hack for right now since we currently don't support filtering out these duplicates within a query
if ( query . EnableGroupByMetadataKey )
if ( query . EnableGroupByMetadataKey )
@ -2505,6 +2537,7 @@ namespace Emby.Server.Implementations.Data
TotalRecordCount = returnList . Count
TotalRecordCount = returnList . Count
} ;
} ;
}
}
//Logger.Info("GetItems: " + _environmentInfo.StackTrace);
var now = DateTime . UtcNow ;
var now = DateTime . UtcNow ;
@ -2548,14 +2581,16 @@ namespace Emby.Server.Implementations.Data
}
}
}
}
using ( WriteLock . Write ( ) )
using ( var connection = CreateConnection ( true , EnableJoinUserData ( query ) ) )
{
using ( WriteLock . Read ( ) )
{
{
var totalRecordCount = 0 ;
var totalRecordCount = 0 ;
var isReturningZeroItems = query . Limit . HasValue & & query . Limit < = 0 ;
var isReturningZeroItems = query . Limit . HasValue & & query . Limit < = 0 ;
if ( ! isReturningZeroItems )
if ( ! isReturningZeroItems )
{
{
using ( var statement = _ connection. PrepareStatement ( commandText ) )
using ( var statement = connection. PrepareStatement ( commandText ) )
{
{
if ( EnableJoinUserData ( query ) )
if ( EnableJoinUserData ( query ) )
{
{
@ -2592,7 +2627,7 @@ namespace Emby.Server.Implementations.Data
commandText + = GetJoinUserDataText ( query ) ;
commandText + = GetJoinUserDataText ( query ) ;
commandText + = whereTextWithoutPaging ;
commandText + = whereTextWithoutPaging ;
using ( var statement = _ connection. PrepareStatement ( commandText ) )
using ( var statement = connection. PrepareStatement ( commandText ) )
{
{
if ( EnableJoinUserData ( query ) )
if ( EnableJoinUserData ( query ) )
{
{
@ -2616,6 +2651,7 @@ namespace Emby.Server.Implementations.Data
} ;
} ;
}
}
}
}
}
private string GetOrderByText ( InternalItemsQuery query )
private string GetOrderByText ( InternalItemsQuery query )
{
{
@ -2739,6 +2775,7 @@ namespace Emby.Server.Implementations.Data
}
}
CheckDisposed ( ) ;
CheckDisposed ( ) ;
//Logger.Info("GetItemIdsList: " + _environmentInfo.StackTrace);
var now = DateTime . UtcNow ;
var now = DateTime . UtcNow ;
@ -2774,9 +2811,11 @@ namespace Emby.Server.Implementations.Data
var list = new List < Guid > ( ) ;
var list = new List < Guid > ( ) ;
using ( WriteLock . Write ( ) )
using ( var connection = CreateConnection ( true , EnableJoinUserData ( query ) ) )
{
{
using ( var statement = _connection . PrepareStatement ( commandText ) )
using ( WriteLock . Read ( ) )
{
using ( var statement = connection . PrepareStatement ( commandText ) )
{
{
if ( EnableJoinUserData ( query ) )
if ( EnableJoinUserData ( query ) )
{
{
@ -2799,6 +2838,7 @@ namespace Emby.Server.Implementations.Data
return list ;
return list ;
}
}
}
}
}
public List < Tuple < Guid , string > > GetItemIdsWithPath ( InternalItemsQuery query )
public List < Tuple < Guid , string > > GetItemIdsWithPath ( InternalItemsQuery query )
{
{
@ -2842,9 +2882,11 @@ namespace Emby.Server.Implementations.Data
var list = new List < Tuple < Guid , string > > ( ) ;
var list = new List < Tuple < Guid , string > > ( ) ;
using ( WriteLock . Write ( ) )
using ( var connection = CreateConnection ( true , EnableJoinUserData ( query ) ) )
{
{
using ( var statement = _connection . PrepareStatement ( commandText ) )
using ( WriteLock . Read ( ) )
{
using ( var statement = connection . PrepareStatement ( commandText ) )
{
{
if ( EnableJoinUserData ( query ) )
if ( EnableJoinUserData ( query ) )
{
{
@ -2872,6 +2914,7 @@ namespace Emby.Server.Implementations.Data
return list ;
return list ;
}
}
}
}
}
public QueryResult < Guid > GetItemIds ( InternalItemsQuery query )
public QueryResult < Guid > GetItemIds ( InternalItemsQuery query )
{
{
@ -2891,6 +2934,7 @@ namespace Emby.Server.Implementations.Data
TotalRecordCount = returnList . Count
TotalRecordCount = returnList . Count
} ;
} ;
}
}
//Logger.Info("GetItemIds: " + _environmentInfo.StackTrace);
var now = DateTime . UtcNow ;
var now = DateTime . UtcNow ;
@ -2928,11 +2972,13 @@ namespace Emby.Server.Implementations.Data
var list = new List < Guid > ( ) ;
var list = new List < Guid > ( ) ;
using ( WriteLock . Write ( ) )
using ( var connection = CreateConnection ( true , EnableJoinUserData ( query ) ) )
{
using ( WriteLock . Read ( ) )
{
{
var totalRecordCount = 0 ;
var totalRecordCount = 0 ;
using ( var statement = _ connection. PrepareStatement ( commandText ) )
using ( var statement = connection. PrepareStatement ( commandText ) )
{
{
if ( EnableJoinUserData ( query ) )
if ( EnableJoinUserData ( query ) )
{
{
@ -2964,7 +3010,7 @@ namespace Emby.Server.Implementations.Data
commandText + = GetJoinUserDataText ( query ) ;
commandText + = GetJoinUserDataText ( query ) ;
commandText + = whereTextWithoutPaging ;
commandText + = whereTextWithoutPaging ;
using ( var statement = _ connection. PrepareStatement ( commandText ) )
using ( var statement = connection. PrepareStatement ( commandText ) )
{
{
if ( EnableJoinUserData ( query ) )
if ( EnableJoinUserData ( query ) )
{
{
@ -2988,6 +3034,7 @@ namespace Emby.Server.Implementations.Data
} ;
} ;
}
}
}
}
}
private List < string > GetWhereClauses ( InternalItemsQuery query , IStatement statement , string paramSuffix = "" )
private List < string > GetWhereClauses ( InternalItemsQuery query , IStatement statement , string paramSuffix = "" )
{
{
@ -3013,14 +3060,6 @@ namespace Emby.Server.Implementations.Data
statement . TryBind ( "@IsLocked" , query . IsLocked ) ;
statement . TryBind ( "@IsLocked" , query . IsLocked ) ;
}
}
}
}
if ( query . IsOffline . HasValue )
{
whereClauses . Add ( "IsOffline=@IsOffline" ) ;
if ( statement ! = null )
{
statement . TryBind ( "@IsOffline" , query . IsOffline ) ;
}
}
var exclusiveProgramAttribtues = ! ( query . IsMovie ? ? true ) | |
var exclusiveProgramAttribtues = ! ( query . IsMovie ? ? true ) | |
! ( query . IsSports ? ? true ) | |
! ( query . IsSports ? ? true ) | |
@ -4289,9 +4328,11 @@ namespace Emby.Server.Implementations.Data
var commandText = "select Guid,InheritedTags,(select group_concat(Tags, '|') from TypedBaseItems where (guid=outer.guid) OR (guid in (Select AncestorId from AncestorIds where ItemId=Outer.guid))) as NewInheritedTags from typedbaseitems as Outer where NewInheritedTags <> InheritedTags" ;
var commandText = "select Guid,InheritedTags,(select group_concat(Tags, '|') from TypedBaseItems where (guid=outer.guid) OR (guid in (Select AncestorId from AncestorIds where ItemId=Outer.guid))) as NewInheritedTags from typedbaseitems as Outer where NewInheritedTags <> InheritedTags" ;
using ( var connection = CreateConnection ( ) )
{
using ( WriteLock . Write ( ) )
using ( WriteLock . Write ( ) )
{
{
foreach ( var row in _ connection. Query ( commandText ) )
foreach ( var row in connection. Query ( commandText ) )
{
{
var id = row . GetGuid ( 0 ) ;
var id = row . GetGuid ( 0 ) ;
string value = row . IsDBNull ( 2 ) ? null : row . GetString ( 2 ) ;
string value = row . IsDBNull ( 2 ) ? null : row . GetString ( 2 ) ;
@ -4306,7 +4347,7 @@ namespace Emby.Server.Implementations.Data
}
}
// write lock here
// write lock here
using ( var statement = _ connection. PrepareStatement ( "Update TypedBaseItems set InheritedTags=@InheritedTags where Guid=@Guid" ) )
using ( var statement = connection. PrepareStatement ( "Update TypedBaseItems set InheritedTags=@InheritedTags where Guid=@Guid" ) )
{
{
foreach ( var item in newValues )
foreach ( var item in newValues )
{
{
@ -4320,6 +4361,7 @@ namespace Emby.Server.Implementations.Data
}
}
}
}
}
}
}
private static Dictionary < string , string [ ] > GetTypeMapDictionary ( )
private static Dictionary < string , string [ ] > GetTypeMapDictionary ( )
{
{
@ -4360,9 +4402,11 @@ namespace Emby.Server.Implementations.Data
CheckDisposed ( ) ;
CheckDisposed ( ) ;
using ( var connection = CreateConnection ( ) )
{
using ( WriteLock . Write ( ) )
using ( WriteLock . Write ( ) )
{
{
_ connection. RunInTransaction ( db = >
connection. RunInTransaction ( db = >
{
{
// Delete people
// Delete people
ExecuteWithSingleParam ( db , "delete from People where ItemId=@Id" , id . ToGuidParamValue ( ) ) ;
ExecuteWithSingleParam ( db , "delete from People where ItemId=@Id" , id . ToGuidParamValue ( ) ) ;
@ -4384,6 +4428,7 @@ namespace Emby.Server.Implementations.Data
} ) ;
} ) ;
}
}
}
}
}
private void ExecuteWithSingleParam ( IDatabaseConnection db , string query , byte [ ] value )
private void ExecuteWithSingleParam ( IDatabaseConnection db , string query , byte [ ] value )
{
{
@ -4417,9 +4462,11 @@ namespace Emby.Server.Implementations.Data
var list = new List < string > ( ) ;
var list = new List < string > ( ) ;
using ( WriteLock . Write ( ) )
using ( var connection = CreateConnection ( true ) )
{
using ( WriteLock . Read ( ) )
{
{
using ( var statement = _connection . PrepareStatement ( commandText ) )
using ( var statement = connection. PrepareStatement ( commandText ) )
{
{
// Run this again to bind the params
// Run this again to bind the params
GetPeopleWhereClauses ( query , statement ) ;
GetPeopleWhereClauses ( query , statement ) ;
@ -4432,6 +4479,7 @@ namespace Emby.Server.Implementations.Data
return list ;
return list ;
}
}
}
}
}
public List < PersonInfo > GetPeople ( InternalPeopleQuery query )
public List < PersonInfo > GetPeople ( InternalPeopleQuery query )
{
{
@ -4455,9 +4503,11 @@ namespace Emby.Server.Implementations.Data
var list = new List < PersonInfo > ( ) ;
var list = new List < PersonInfo > ( ) ;
using ( WriteLock . Write ( ) )
using ( var connection = CreateConnection ( true ) )
{
using ( WriteLock . Read ( ) )
{
{
using ( var statement = _connection . PrepareStatement ( commandText ) )
using ( var statement = connection. PrepareStatement ( commandText ) )
{
{
// Run this again to bind the params
// Run this again to bind the params
GetPeopleWhereClauses ( query , statement ) ;
GetPeopleWhereClauses ( query , statement ) ;
@ -4468,6 +4518,7 @@ namespace Emby.Server.Implementations.Data
}
}
}
}
}
}
}
return list ;
return list ;
}
}
@ -4667,9 +4718,11 @@ namespace Emby.Server.Implementations.Data
commandText + = " Group By CleanValue" ;
commandText + = " Group By CleanValue" ;
using ( WriteLock . Write ( ) )
using ( var connection = CreateConnection ( true ) )
{
using ( WriteLock . Read ( ) )
{
{
foreach ( var row in _connection . Query ( commandText ) )
foreach ( var row in connection. Query ( commandText ) )
{
{
if ( ! row . IsDBNull ( 0 ) )
if ( ! row . IsDBNull ( 0 ) )
{
{
@ -4677,6 +4730,7 @@ namespace Emby.Server.Implementations.Data
}
}
}
}
}
}
}
LogQueryTime ( "GetItemValueNames" , commandText , now ) ;
LogQueryTime ( "GetItemValueNames" , commandText , now ) ;
return list ;
return list ;
@ -4695,6 +4749,7 @@ namespace Emby.Server.Implementations.Data
}
}
CheckDisposed ( ) ;
CheckDisposed ( ) ;
//Logger.Info("GetItemValues: " + _environmentInfo.StackTrace);
var now = DateTime . UtcNow ;
var now = DateTime . UtcNow ;
@ -4825,11 +4880,13 @@ namespace Emby.Server.Implementations.Data
var list = new List < Tuple < BaseItem , ItemCounts > > ( ) ;
var list = new List < Tuple < BaseItem , ItemCounts > > ( ) ;
var count = 0 ;
var count = 0 ;
using ( WriteLock . Write ( ) )
using ( var connection = CreateConnection ( true , EnableJoinUserData ( query ) ) )
{
using ( WriteLock . Read ( ) )
{
{
if ( ! isReturningZeroItems )
if ( ! isReturningZeroItems )
{
{
using ( var statement = _ connection. PrepareStatement ( commandText ) )
using ( var statement = connection. PrepareStatement ( commandText ) )
{
{
statement . TryBind ( "@SelectType" , returnType ) ;
statement . TryBind ( "@SelectType" , returnType ) ;
if ( EnableJoinUserData ( query ) )
if ( EnableJoinUserData ( query ) )
@ -4867,7 +4924,7 @@ namespace Emby.Server.Implementations.Data
commandText + = GetJoinUserDataText ( query ) ;
commandText + = GetJoinUserDataText ( query ) ;
commandText + = whereText ;
commandText + = whereText ;
using ( var statement = _ connection. PrepareStatement ( commandText ) )
using ( var statement = connection. PrepareStatement ( commandText ) )
{
{
statement . TryBind ( "@SelectType" , returnType ) ;
statement . TryBind ( "@SelectType" , returnType ) ;
if ( EnableJoinUserData ( query ) )
if ( EnableJoinUserData ( query ) )
@ -4889,6 +4946,7 @@ namespace Emby.Server.Implementations.Data
}
}
}
}
}
}
}
if ( count = = 0 )
if ( count = = 0 )
{
{
@ -5043,15 +5101,16 @@ namespace Emby.Server.Implementations.Data
CheckDisposed ( ) ;
CheckDisposed ( ) ;
using ( WriteLock . Write ( ) )
using ( var connection = CreateConnection ( ) )
{
{
// First delete
using ( WriteLock . Write ( ) )
{ // First delete
// "delete from People where ItemId=?"
// "delete from People where ItemId=?"
_ connection. Execute ( "delete from People where ItemId=?" , itemId . ToGuidParamValue ( ) ) ;
connection. Execute ( "delete from People where ItemId=?" , itemId . ToGuidParamValue ( ) ) ;
var listIndex = 0 ;
var listIndex = 0 ;
using ( var statement = _ connection. PrepareStatement (
using ( var statement = connection. PrepareStatement (
"insert into People (ItemId, Name, Role, PersonType, SortOrder, ListOrder) values (@ItemId, @Name, @Role, @PersonType, @SortOrder, @ListOrder)" ) )
"insert into People (ItemId, Name, Role, PersonType, SortOrder, ListOrder) values (@ItemId, @Name, @Role, @PersonType, @SortOrder, @ListOrder)" ) )
{
{
foreach ( var person in people )
foreach ( var person in people )
@ -5074,6 +5133,7 @@ namespace Emby.Server.Implementations.Data
}
}
}
}
}
}
}
private PersonInfo GetPerson ( IReadOnlyList < IResultSetValue > reader )
private PersonInfo GetPerson ( IReadOnlyList < IResultSetValue > reader )
{
{
@ -5127,9 +5187,11 @@ namespace Emby.Server.Implementations.Data
cmdText + = " order by StreamIndex ASC" ;
cmdText + = " order by StreamIndex ASC" ;
using ( WriteLock . Write ( ) )
using ( var connection = CreateConnection ( true ) )
{
{
using ( var statement = _connection . PrepareStatement ( cmdText ) )
using ( WriteLock . Read ( ) )
{
using ( var statement = connection . PrepareStatement ( cmdText ) )
{
{
statement . TryBind ( "@ItemId" , query . ItemId . ToGuidParamValue ( ) ) ;
statement . TryBind ( "@ItemId" , query . ItemId . ToGuidParamValue ( ) ) ;
@ -5149,6 +5211,7 @@ namespace Emby.Server.Implementations.Data
}
}
}
}
}
}
}
return list ;
return list ;
}
}
@ -5169,12 +5232,13 @@ namespace Emby.Server.Implementations.Data
cancellationToken . ThrowIfCancellationRequested ( ) ;
cancellationToken . ThrowIfCancellationRequested ( ) ;
using ( WriteLock . Write ( ) )
using ( var connection = CreateConnection ( ) )
{
{
// First delete chapters
using ( WriteLock . Write ( ) )
_connection . Execute ( "delete from mediastreams where ItemId=@ItemId" , id . ToGuidParamValue ( ) ) ;
{ // First delete chapters
connection . Execute ( "delete from mediastreams where ItemId=@ItemId" , id . ToGuidParamValue ( ) ) ;
using ( var statement = _ connection. PrepareStatement ( string . Format ( "replace into mediastreams ({0}) values ({1})" ,
using ( var statement = connection. PrepareStatement ( string . Format ( "replace into mediastreams ({0}) values ({1})" ,
string . Join ( "," , _mediaStreamSaveColumns ) ,
string . Join ( "," , _mediaStreamSaveColumns ) ,
string . Join ( "," , _mediaStreamSaveColumns . Select ( i = > "@" + i ) . ToArray ( ) ) ) ) )
string . Join ( "," , _mediaStreamSaveColumns . Select ( i = > "@" + i ) . ToArray ( ) ) ) ) )
{
{
@ -5225,6 +5289,7 @@ namespace Emby.Server.Implementations.Data
}
}
}
}
}
}
}
/// <summary>
/// <summary>
/// Gets the chapter.
/// Gets the chapter.