2014-01-20 09:09:53 -07:00
using MediaBrowser.Controller ;
using MediaBrowser.Controller.Persistence ;
using MediaBrowser.Model.FileOrganization ;
using MediaBrowser.Model.Logging ;
2014-01-20 11:46:08 -07:00
using MediaBrowser.Model.Querying ;
2014-01-20 09:09:53 -07:00
using System ;
2014-01-20 23:10:58 -07:00
using System.Collections.Generic ;
2014-01-20 09:09:53 -07:00
using System.Data ;
2014-01-20 23:10:58 -07:00
using System.Globalization ;
2014-01-20 09:09:53 -07:00
using System.IO ;
2014-01-23 14:09:00 -07:00
using System.Linq ;
2014-01-20 09:09:53 -07:00
using System.Threading ;
using System.Threading.Tasks ;
namespace MediaBrowser.Server.Implementations.Persistence
{
2015-05-02 15:59:01 -07:00
public class SqliteFileOrganizationRepository : BaseSqliteRepository , IFileOrganizationRepository , IDisposable
2014-01-20 09:09:53 -07:00
{
private IDbConnection _connection ;
private readonly IServerApplicationPaths _appPaths ;
2014-01-20 23:10:58 -07:00
private readonly CultureInfo _usCulture = new CultureInfo ( "en-US" ) ;
private IDbCommand _saveResultCommand ;
private IDbCommand _deleteResultCommand ;
2014-01-22 10:05:06 -07:00
private IDbCommand _deleteAllCommand ;
2014-01-20 23:10:58 -07:00
2015-05-02 15:59:01 -07:00
public SqliteFileOrganizationRepository ( ILogManager logManager , IServerApplicationPaths appPaths ) : base ( logManager )
2014-01-20 09:09:53 -07:00
{
_appPaths = appPaths ;
}
/// <summary>
/// Opens the connection to the database
/// </summary>
/// <returns>Task.</returns>
2016-05-01 14:48:37 -07:00
public async Task Initialize ( IDbConnector dbConnector )
2014-01-20 09:09:53 -07:00
{
var dbFile = Path . Combine ( _appPaths . DataPath , "fileorganization.db" ) ;
2016-05-01 14:48:37 -07:00
_connection = await dbConnector . Connect ( dbFile ) . ConfigureAwait ( false ) ;
2014-01-20 09:09:53 -07:00
string [ ] queries = {
2014-02-19 09:24:06 -07:00
"create table if not exists FileOrganizerResults (ResultId GUID PRIMARY KEY, OriginalPath TEXT, TargetPath TEXT, FileLength INT, OrganizationDate datetime, Status TEXT, OrganizationType TEXT, StatusMessage TEXT, ExtractedName TEXT, ExtractedYear int null, ExtractedSeasonNumber int null, ExtractedEpisodeNumber int null, ExtractedEndingEpisodeNumber, DuplicatePaths TEXT int null)" ,
"create index if not exists idx_FileOrganizerResults on FileOrganizerResults(ResultId)" ,
2014-01-20 23:10:58 -07:00
2014-01-20 09:09:53 -07:00
//pragmas
"pragma temp_store = memory" ,
"pragma shrink_memory"
} ;
2015-05-02 15:59:01 -07:00
_connection . RunQueries ( queries , Logger ) ;
2014-01-20 09:09:53 -07:00
PrepareStatements ( ) ;
}
private void PrepareStatements ( )
{
2014-01-20 23:10:58 -07:00
_saveResultCommand = _connection . CreateCommand ( ) ;
2014-02-19 09:24:06 -07:00
_saveResultCommand . CommandText = "replace into FileOrganizerResults (ResultId, OriginalPath, TargetPath, FileLength, OrganizationDate, Status, OrganizationType, StatusMessage, ExtractedName, ExtractedYear, ExtractedSeasonNumber, ExtractedEpisodeNumber, ExtractedEndingEpisodeNumber, DuplicatePaths) values (@ResultId, @OriginalPath, @TargetPath, @FileLength, @OrganizationDate, @Status, @OrganizationType, @StatusMessage, @ExtractedName, @ExtractedYear, @ExtractedSeasonNumber, @ExtractedEpisodeNumber, @ExtractedEndingEpisodeNumber, @DuplicatePaths)" ;
2014-01-20 23:10:58 -07:00
_saveResultCommand . Parameters . Add ( _saveResultCommand , "@ResultId" ) ;
_saveResultCommand . Parameters . Add ( _saveResultCommand , "@OriginalPath" ) ;
_saveResultCommand . Parameters . Add ( _saveResultCommand , "@TargetPath" ) ;
2014-02-19 09:24:06 -07:00
_saveResultCommand . Parameters . Add ( _saveResultCommand , "@FileLength" ) ;
2014-01-20 23:10:58 -07:00
_saveResultCommand . Parameters . Add ( _saveResultCommand , "@OrganizationDate" ) ;
_saveResultCommand . Parameters . Add ( _saveResultCommand , "@Status" ) ;
_saveResultCommand . Parameters . Add ( _saveResultCommand , "@OrganizationType" ) ;
_saveResultCommand . Parameters . Add ( _saveResultCommand , "@StatusMessage" ) ;
_saveResultCommand . Parameters . Add ( _saveResultCommand , "@ExtractedName" ) ;
_saveResultCommand . Parameters . Add ( _saveResultCommand , "@ExtractedYear" ) ;
2014-01-21 09:24:12 -07:00
_saveResultCommand . Parameters . Add ( _saveResultCommand , "@ExtractedSeasonNumber" ) ;
_saveResultCommand . Parameters . Add ( _saveResultCommand , "@ExtractedEpisodeNumber" ) ;
_saveResultCommand . Parameters . Add ( _saveResultCommand , "@ExtractedEndingEpisodeNumber" ) ;
2014-01-23 14:09:00 -07:00
_saveResultCommand . Parameters . Add ( _saveResultCommand , "@DuplicatePaths" ) ;
2014-01-20 23:10:58 -07:00
_deleteResultCommand = _connection . CreateCommand ( ) ;
2014-02-19 09:24:06 -07:00
_deleteResultCommand . CommandText = "delete from FileOrganizerResults where ResultId = @ResultId" ;
2014-01-20 23:10:58 -07:00
_deleteResultCommand . Parameters . Add ( _saveResultCommand , "@ResultId" ) ;
2014-01-22 10:05:06 -07:00
_deleteAllCommand = _connection . CreateCommand ( ) ;
2014-02-19 09:24:06 -07:00
_deleteAllCommand . CommandText = "delete from FileOrganizerResults" ;
2014-01-20 23:10:58 -07:00
}
public async Task SaveResult ( FileOrganizationResult result , CancellationToken cancellationToken )
{
if ( result = = null )
{
throw new ArgumentNullException ( "result" ) ;
}
cancellationToken . ThrowIfCancellationRequested ( ) ;
2015-05-02 15:59:01 -07:00
await WriteLock . WaitAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
2014-01-20 23:10:58 -07:00
IDbTransaction transaction = null ;
try
{
transaction = _connection . BeginTransaction ( ) ;
2014-02-19 09:24:06 -07:00
var index = 0 ;
_saveResultCommand . GetParameter ( index + + ) . Value = new Guid ( result . Id ) ;
_saveResultCommand . GetParameter ( index + + ) . Value = result . OriginalPath ;
_saveResultCommand . GetParameter ( index + + ) . Value = result . TargetPath ;
_saveResultCommand . GetParameter ( index + + ) . Value = result . FileSize ;
_saveResultCommand . GetParameter ( index + + ) . Value = result . Date ;
_saveResultCommand . GetParameter ( index + + ) . Value = result . Status . ToString ( ) ;
_saveResultCommand . GetParameter ( index + + ) . Value = result . Type . ToString ( ) ;
_saveResultCommand . GetParameter ( index + + ) . Value = result . StatusMessage ;
_saveResultCommand . GetParameter ( index + + ) . Value = result . ExtractedName ;
_saveResultCommand . GetParameter ( index + + ) . Value = result . ExtractedYear ;
_saveResultCommand . GetParameter ( index + + ) . Value = result . ExtractedSeasonNumber ;
_saveResultCommand . GetParameter ( index + + ) . Value = result . ExtractedEpisodeNumber ;
_saveResultCommand . GetParameter ( index + + ) . Value = result . ExtractedEndingEpisodeNumber ;
_saveResultCommand . GetParameter ( index ) . Value = string . Join ( "|" , result . DuplicatePaths . ToArray ( ) ) ;
2014-01-20 23:10:58 -07:00
_saveResultCommand . Transaction = transaction ;
_saveResultCommand . ExecuteNonQuery ( ) ;
transaction . Commit ( ) ;
}
catch ( OperationCanceledException )
{
if ( transaction ! = null )
{
transaction . Rollback ( ) ;
}
throw ;
}
catch ( Exception e )
{
2015-05-02 15:59:01 -07:00
Logger . ErrorException ( "Failed to save FileOrganizationResult:" , e ) ;
2014-01-20 23:10:58 -07:00
if ( transaction ! = null )
{
transaction . Rollback ( ) ;
}
throw ;
}
finally
{
if ( transaction ! = null )
{
transaction . Dispose ( ) ;
}
2015-05-02 15:59:01 -07:00
WriteLock . Release ( ) ;
2014-01-20 23:10:58 -07:00
}
2014-01-20 09:09:53 -07:00
}
2014-01-20 23:10:58 -07:00
public async Task Delete ( string id )
2014-01-20 09:09:53 -07:00
{
2014-01-20 23:10:58 -07:00
if ( string . IsNullOrEmpty ( id ) )
{
throw new ArgumentNullException ( "id" ) ;
}
2015-05-02 15:59:01 -07:00
await WriteLock . WaitAsync ( ) . ConfigureAwait ( false ) ;
2014-01-20 23:10:58 -07:00
IDbTransaction transaction = null ;
try
{
transaction = _connection . BeginTransaction ( ) ;
_deleteResultCommand . GetParameter ( 0 ) . Value = new Guid ( id ) ;
_deleteResultCommand . Transaction = transaction ;
_deleteResultCommand . ExecuteNonQuery ( ) ;
transaction . Commit ( ) ;
}
catch ( OperationCanceledException )
{
if ( transaction ! = null )
{
transaction . Rollback ( ) ;
}
throw ;
}
catch ( Exception e )
{
2015-05-02 15:59:01 -07:00
Logger . ErrorException ( "Failed to delete FileOrganizationResult:" , e ) ;
2014-01-20 23:10:58 -07:00
if ( transaction ! = null )
{
transaction . Rollback ( ) ;
}
throw ;
}
finally
{
if ( transaction ! = null )
{
transaction . Dispose ( ) ;
}
2015-05-02 15:59:01 -07:00
WriteLock . Release ( ) ;
2014-01-20 23:10:58 -07:00
}
2014-01-20 09:09:53 -07:00
}
2014-01-22 10:05:06 -07:00
public async Task DeleteAll ( )
{
2015-05-02 15:59:01 -07:00
await WriteLock . WaitAsync ( ) . ConfigureAwait ( false ) ;
2014-01-22 10:05:06 -07:00
IDbTransaction transaction = null ;
try
{
transaction = _connection . BeginTransaction ( ) ;
_deleteAllCommand . Transaction = transaction ;
_deleteAllCommand . ExecuteNonQuery ( ) ;
transaction . Commit ( ) ;
}
catch ( OperationCanceledException )
{
if ( transaction ! = null )
{
transaction . Rollback ( ) ;
}
throw ;
}
catch ( Exception e )
{
2015-05-02 15:59:01 -07:00
Logger . ErrorException ( "Failed to delete results" , e ) ;
2014-01-22 10:05:06 -07:00
if ( transaction ! = null )
{
transaction . Rollback ( ) ;
}
throw ;
}
finally
{
if ( transaction ! = null )
{
transaction . Dispose ( ) ;
}
2015-05-02 15:59:01 -07:00
WriteLock . Release ( ) ;
2014-01-22 10:05:06 -07:00
}
}
2014-01-20 11:46:08 -07:00
public QueryResult < FileOrganizationResult > GetResults ( FileOrganizationResultQuery query )
2014-01-20 09:09:53 -07:00
{
2014-01-20 23:10:58 -07:00
if ( query = = null )
{
throw new ArgumentNullException ( "query" ) ;
}
using ( var cmd = _connection . CreateCommand ( ) )
{
2014-02-19 09:24:06 -07:00
cmd . CommandText = "SELECT ResultId, OriginalPath, TargetPath, FileLength, OrganizationDate, Status, OrganizationType, StatusMessage, ExtractedName, ExtractedYear, ExtractedSeasonNumber, ExtractedEpisodeNumber, ExtractedEndingEpisodeNumber, DuplicatePaths from FileOrganizerResults" ;
2014-01-20 23:10:58 -07:00
if ( query . StartIndex . HasValue & & query . StartIndex . Value > 0 )
{
2014-02-19 09:24:06 -07:00
cmd . CommandText + = string . Format ( " WHERE ResultId NOT IN (SELECT ResultId FROM FileOrganizerResults ORDER BY OrganizationDate desc LIMIT {0})" ,
2014-01-20 23:10:58 -07:00
query . StartIndex . Value . ToString ( _usCulture ) ) ;
}
cmd . CommandText + = " ORDER BY OrganizationDate desc" ;
if ( query . Limit . HasValue )
{
cmd . CommandText + = " LIMIT " + query . Limit . Value . ToString ( _usCulture ) ;
}
2014-02-19 09:24:06 -07:00
cmd . CommandText + = "; select count (ResultId) from FileOrganizerResults" ;
2014-01-20 23:10:58 -07:00
var list = new List < FileOrganizationResult > ( ) ;
var count = 0 ;
using ( var reader = cmd . ExecuteReader ( CommandBehavior . SequentialAccess ) )
{
while ( reader . Read ( ) )
{
list . Add ( GetResult ( reader ) ) ;
}
if ( reader . NextResult ( ) & & reader . Read ( ) )
{
count = reader . GetInt32 ( 0 ) ;
}
}
return new QueryResult < FileOrganizationResult > ( )
{
Items = list . ToArray ( ) ,
TotalRecordCount = count
} ;
}
}
public FileOrganizationResult GetResult ( string id )
{
if ( string . IsNullOrEmpty ( id ) )
{
throw new ArgumentNullException ( "id" ) ;
}
var guid = new Guid ( id ) ;
using ( var cmd = _connection . CreateCommand ( ) )
{
2014-02-19 09:24:06 -07:00
cmd . CommandText = "select ResultId, OriginalPath, TargetPath, FileLength, OrganizationDate, Status, OrganizationType, StatusMessage, ExtractedName, ExtractedYear, ExtractedSeasonNumber, ExtractedEpisodeNumber, ExtractedEndingEpisodeNumber, DuplicatePaths from FileOrganizerResults where ResultId=@Id" ;
2014-01-20 23:10:58 -07:00
cmd . Parameters . Add ( cmd , "@Id" , DbType . Guid ) . Value = guid ;
using ( var reader = cmd . ExecuteReader ( CommandBehavior . SequentialAccess | CommandBehavior . SingleResult | CommandBehavior . SingleRow ) )
{
if ( reader . Read ( ) )
{
return GetResult ( reader ) ;
}
}
}
return null ;
}
public FileOrganizationResult GetResult ( IDataReader reader )
{
2014-02-19 09:24:06 -07:00
var index = 0 ;
2014-01-20 23:10:58 -07:00
var result = new FileOrganizationResult
{
Id = reader . GetGuid ( 0 ) . ToString ( "N" )
} ;
2014-02-19 09:24:06 -07:00
index + + ;
if ( ! reader . IsDBNull ( index ) )
2014-01-20 23:10:58 -07:00
{
2014-02-19 09:24:06 -07:00
result . OriginalPath = reader . GetString ( index ) ;
2014-01-20 23:10:58 -07:00
}
2014-02-19 09:24:06 -07:00
index + + ;
if ( ! reader . IsDBNull ( index ) )
2014-01-20 23:10:58 -07:00
{
2014-02-19 09:24:06 -07:00
result . TargetPath = reader . GetString ( index ) ;
2014-01-20 23:10:58 -07:00
}
2014-02-19 09:24:06 -07:00
index + + ;
result . FileSize = reader . GetInt64 ( index ) ;
index + + ;
result . Date = reader . GetDateTime ( index ) . ToUniversalTime ( ) ;
index + + ;
result . Status = ( FileSortingStatus ) Enum . Parse ( typeof ( FileSortingStatus ) , reader . GetString ( index ) , true ) ;
index + + ;
result . Type = ( FileOrganizerType ) Enum . Parse ( typeof ( FileOrganizerType ) , reader . GetString ( index ) , true ) ;
2014-01-20 23:10:58 -07:00
2014-02-19 09:24:06 -07:00
index + + ;
if ( ! reader . IsDBNull ( index ) )
2014-01-20 23:10:58 -07:00
{
2014-02-19 09:24:06 -07:00
result . StatusMessage = reader . GetString ( index ) ;
2014-01-20 23:10:58 -07:00
}
result . OriginalFileName = Path . GetFileName ( result . OriginalPath ) ;
2014-02-19 09:24:06 -07:00
index + + ;
if ( ! reader . IsDBNull ( index ) )
2014-01-20 23:10:58 -07:00
{
2014-02-19 09:24:06 -07:00
result . ExtractedName = reader . GetString ( index ) ;
2014-01-20 23:10:58 -07:00
}
2014-02-19 09:24:06 -07:00
index + + ;
if ( ! reader . IsDBNull ( index ) )
2014-01-20 23:10:58 -07:00
{
2014-02-19 09:24:06 -07:00
result . ExtractedYear = reader . GetInt32 ( index ) ;
2014-01-20 23:10:58 -07:00
}
2014-02-19 09:24:06 -07:00
index + + ;
if ( ! reader . IsDBNull ( index ) )
2014-01-21 09:24:12 -07:00
{
2014-02-19 09:24:06 -07:00
result . ExtractedSeasonNumber = reader . GetInt32 ( index ) ;
2014-01-21 09:24:12 -07:00
}
2014-02-19 09:24:06 -07:00
index + + ;
if ( ! reader . IsDBNull ( index ) )
2014-01-21 09:24:12 -07:00
{
2014-02-19 09:24:06 -07:00
result . ExtractedEpisodeNumber = reader . GetInt32 ( index ) ;
2014-01-21 09:24:12 -07:00
}
2014-02-19 09:24:06 -07:00
index + + ;
if ( ! reader . IsDBNull ( index ) )
2014-01-21 09:24:12 -07:00
{
2014-02-19 09:24:06 -07:00
result . ExtractedEndingEpisodeNumber = reader . GetInt32 ( index ) ;
2014-01-21 09:24:12 -07:00
}
2014-02-19 09:24:06 -07:00
index + + ;
if ( ! reader . IsDBNull ( index ) )
2014-01-23 14:09:00 -07:00
{
2014-02-19 09:24:06 -07:00
result . DuplicatePaths = reader . GetString ( index ) . Split ( '|' ) . Where ( i = > ! string . IsNullOrEmpty ( i ) ) . ToList ( ) ;
2014-01-23 14:09:00 -07:00
}
2014-01-20 23:10:58 -07:00
return result ;
2014-01-20 09:09:53 -07:00
}
2015-05-02 15:59:01 -07:00
protected override void CloseConnection ( )
2014-01-20 09:09:53 -07:00
{
2015-05-02 15:59:01 -07:00
if ( _connection ! = null )
2014-01-20 09:09:53 -07:00
{
2015-05-02 15:59:01 -07:00
if ( _connection . IsOpen ( ) )
2014-01-20 09:09:53 -07:00
{
2015-05-02 15:59:01 -07:00
_connection . Close ( ) ;
2014-01-20 09:09:53 -07:00
}
2015-05-02 15:59:01 -07:00
_connection . Dispose ( ) ;
_connection = null ;
2014-01-20 09:09:53 -07:00
}
}
}
}