@ -16,69 +16,29 @@ namespace MediaBrowser.Server.Implementations.Persistence
{
public class SqliteFileOrganizationRepository : BaseSqliteRepository , IFileOrganizationRepository , IDisposable
{
private IDbConnection _connection ;
private readonly IServerApplicationPaths _appPaths ;
private readonly CultureInfo _usCulture = new CultureInfo ( "en-US" ) ;
private IDbCommand _saveResultCommand ;
private IDbCommand _deleteResultCommand ;
private IDbCommand _deleteAllCommand ;
public SqliteFileOrganizationRepository ( ILogManager logManager , IServerApplicationPaths appPaths , IDbConnector connector ) : base ( logManager , connector )
{
_appPaths = appPaths ;
DbFilePath = Path . Combine ( appPaths . DataPath , "fileorganization.db" ) ;
}
/// <summary>
/// Opens the connection to the database
/// </summary>
/// <returns>Task.</returns>
public async Task Initialize ( IDbConnector dbConnector )
public async Task Initialize ( )
{
using ( var connection = await CreateConnection ( ) . ConfigureAwait ( false ) )
{
var dbFile = Path . Combine ( _appPaths . DataPath , "fileorganization.db" ) ;
_connection = await dbConnector . Connect ( dbFile , false ) . ConfigureAwait ( false ) ;
string [ ] queries = {
"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)"
} ;
_connection . RunQueries ( queries , Logger ) ;
PrepareStatements ( ) ;
connection . RunQueries ( queries , Logger ) ;
}
private void PrepareStatements ( )
{
_saveResultCommand = _connection . CreateCommand ( ) ;
_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)" ;
_saveResultCommand . Parameters . Add ( _saveResultCommand , "@ResultId" ) ;
_saveResultCommand . Parameters . Add ( _saveResultCommand , "@OriginalPath" ) ;
_saveResultCommand . Parameters . Add ( _saveResultCommand , "@TargetPath" ) ;
_saveResultCommand . Parameters . Add ( _saveResultCommand , "@FileLength" ) ;
_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" ) ;
_saveResultCommand . Parameters . Add ( _saveResultCommand , "@ExtractedSeasonNumber" ) ;
_saveResultCommand . Parameters . Add ( _saveResultCommand , "@ExtractedEpisodeNumber" ) ;
_saveResultCommand . Parameters . Add ( _saveResultCommand , "@ExtractedEndingEpisodeNumber" ) ;
_saveResultCommand . Parameters . Add ( _saveResultCommand , "@DuplicatePaths" ) ;
_deleteResultCommand = _connection . CreateCommand ( ) ;
_deleteResultCommand . CommandText = "delete from FileOrganizerResults where ResultId = @ResultId" ;
_deleteResultCommand . Parameters . Add ( _saveResultCommand , "@ResultId" ) ;
_deleteAllCommand = _connection . CreateCommand ( ) ;
_deleteAllCommand . CommandText = "delete from FileOrganizerResults" ;
}
public async Task SaveResult ( FileOrganizationResult result , CancellationToken cancellationToken )
@ -90,34 +50,53 @@ namespace MediaBrowser.Server.Implementations.Persistence
cancellationToken . ThrowIfCancellationRequested ( ) ;
await WriteLock . WaitAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
using ( var connection = await CreateConnection ( ) . ConfigureAwait ( false ) )
{
using ( var saveResultCommand = connection . CreateCommand ( ) )
{
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)" ;
saveResultCommand . Parameters . Add ( saveResultCommand , "@ResultId" ) ;
saveResultCommand . Parameters . Add ( saveResultCommand , "@OriginalPath" ) ;
saveResultCommand . Parameters . Add ( saveResultCommand , "@TargetPath" ) ;
saveResultCommand . Parameters . Add ( saveResultCommand , "@FileLength" ) ;
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" ) ;
saveResultCommand . Parameters . Add ( saveResultCommand , "@ExtractedSeasonNumber" ) ;
saveResultCommand . Parameters . Add ( saveResultCommand , "@ExtractedEpisodeNumber" ) ;
saveResultCommand . Parameters . Add ( saveResultCommand , "@ExtractedEndingEpisodeNumber" ) ;
saveResultCommand . Parameters . Add ( saveResultCommand , "@DuplicatePaths" ) ;
IDbTransaction transaction = null ;
try
{
transaction = _connection . BeginTransaction ( ) ;
transaction = connection. BeginTransaction ( ) ;
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 ( ) ) ;
_ saveResultCommand. Transaction = transaction ;
_ saveResultCommand. ExecuteNonQuery ( ) ;
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 ( ) ) ;
saveResultCommand. Transaction = transaction ;
saveResultCommand. ExecuteNonQuery ( ) ;
transaction . Commit ( ) ;
}
@ -147,8 +126,8 @@ namespace MediaBrowser.Server.Implementations.Persistence
{
transaction . Dispose ( ) ;
}
WriteLock . Release ( ) ;
}
}
}
}
@ -159,19 +138,25 @@ namespace MediaBrowser.Server.Implementations.Persistence
throw new ArgumentNullException ( "id" ) ;
}
await WriteLock . WaitAsync ( ) . ConfigureAwait ( false ) ;
using ( var connection = await CreateConnection ( ) . ConfigureAwait ( false ) )
{
using ( var deleteResultCommand = connection . CreateCommand ( ) )
{
deleteResultCommand . CommandText = "delete from FileOrganizerResults where ResultId = @ResultId" ;
deleteResultCommand . Parameters . Add ( deleteResultCommand , "@ResultId" ) ;
IDbTransaction transaction = null ;
try
{
transaction = _connection . BeginTransaction ( ) ;
transaction = connection. BeginTransaction ( ) ;
_ deleteResultCommand. GetParameter ( 0 ) . Value = new Guid ( id ) ;
deleteResultCommand. GetParameter ( 0 ) . Value = new Guid ( id ) ;
_ deleteResultCommand. Transaction = transaction ;
deleteResultCommand. Transaction = transaction ;
_ deleteResultCommand. ExecuteNonQuery ( ) ;
deleteResultCommand. ExecuteNonQuery ( ) ;
transaction . Commit ( ) ;
}
@ -201,24 +186,28 @@ namespace MediaBrowser.Server.Implementations.Persistence
{
transaction . Dispose ( ) ;
}
WriteLock . Release ( ) ;
}
}
}
}
public async Task DeleteAll ( )
{
await WriteLock . WaitAsync ( ) . ConfigureAwait ( false ) ;
using ( var connection = await CreateConnection ( ) . ConfigureAwait ( false ) )
{
using ( var cmd = connection . CreateCommand ( ) )
{
cmd . CommandText = "delete from FileOrganizerResults" ;
IDbTransaction transaction = null ;
try
{
transaction = _ connection. BeginTransaction ( ) ;
transaction = connection. BeginTransaction ( ) ;
_deleteAllComman d. Transaction = transaction ;
cm d. Transaction = transaction ;
_deleteAllComman d. ExecuteNonQuery ( ) ;
cm d. ExecuteNonQuery ( ) ;
transaction . Commit ( ) ;
}
@ -248,8 +237,8 @@ namespace MediaBrowser.Server.Implementations.Persistence
{
transaction . Dispose ( ) ;
}
WriteLock . Release ( ) ;
}
}
}
}
@ -260,7 +249,9 @@ namespace MediaBrowser.Server.Implementations.Persistence
throw new ArgumentNullException ( "query" ) ;
}
using ( var cmd = _connection . CreateCommand ( ) )
using ( var connection = CreateConnection ( true ) . Result )
{
using ( var cmd = connection . CreateCommand ( ) )
{
cmd . CommandText = "SELECT ResultId, OriginalPath, TargetPath, FileLength, OrganizationDate, Status, OrganizationType, StatusMessage, ExtractedName, ExtractedYear, ExtractedSeasonNumber, ExtractedEpisodeNumber, ExtractedEndingEpisodeNumber, DuplicatePaths from FileOrganizerResults" ;
@ -302,6 +293,7 @@ namespace MediaBrowser.Server.Implementations.Persistence
} ;
}
}
}
public FileOrganizationResult GetResult ( string id )
{
@ -310,9 +302,11 @@ namespace MediaBrowser.Server.Implementations.Persistence
throw new ArgumentNullException ( "id" ) ;
}
using ( var connection = CreateConnection ( true ) . Result )
{
var guid = new Guid ( id ) ;
using ( var cmd = _ connection. CreateCommand ( ) )
using ( var cmd = connection. CreateCommand ( ) )
{
cmd . CommandText = "select ResultId, OriginalPath, TargetPath, FileLength, OrganizationDate, Status, OrganizationType, StatusMessage, ExtractedName, ExtractedYear, ExtractedSeasonNumber, ExtractedEpisodeNumber, ExtractedEndingEpisodeNumber, DuplicatePaths from FileOrganizerResults where ResultId=@Id" ;
@ -329,6 +323,7 @@ namespace MediaBrowser.Server.Implementations.Persistence
return null ;
}
}
public FileOrganizationResult GetResult ( IDataReader reader )
{
@ -409,19 +404,5 @@ namespace MediaBrowser.Server.Implementations.Persistence
return result ;
}
protected override void CloseConnection ( )
{
if ( _connection ! = null )
{
if ( _connection . IsOpen ( ) )
{
_connection . Close ( ) ;
}
_connection . Dispose ( ) ;
_connection = null ;
}
}
}
}