using LiteDB;
using System;
using System.Collections.Generic;
using System.IO;
namespace mdfinder
{
/// A database helper class.
public class DBHelper : PropertyChangedAlerter
{
#region Members
/// The default database file name.
private const string DEFAULT_DB_FILE_NAME = "mdfinder.db";
#endregion Members
#region Properties
/// Gets or sets the database.
/// The database.
private LiteDatabase Database { get; set; }
/// Gets a collection of file records.
/// A collection of file records.
private LiteCollection FileRecordCollection
{
get
{
return this.Database.GetCollection("FileRecords");
}
}
/// Gets the database statistics string.
/// The database statistics.
public string DbStatistics
{
get
{
return string.Format("{0} Files In Database.", this.FileRecordCollection.Count());
}
}
#endregion Properties
#region Constructors
/// Default constructor.
public DBHelper()
{
this.Database = new LiteDatabase(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), DEFAULT_DB_FILE_NAME));
}
/// Constructor.
/// The database.
public DBHelper(string database)
{
this.Database = new LiteDatabase(database);
}
#endregion Constructors
#region Methods
/// Inserts a file record.
/// Full pathname of the file.
/// The size.
/// The hash.
/// The hash provider.
public void InsertFileRecord(string path, long size, string hash, string hashProvider)
{
var fileRecord = new FileRecord(path, size, hash, hashProvider);
this.FileRecordCollection.Upsert(fileRecord);
this.OnPropertyChanged("DbStatistics");
}
/// Removes the file record described by its path.
/// The ID of the file.
public void RemoveFileRecord(string id)
{
this.FileRecordCollection.Delete(fr => fr.Id == id);
this.OnPropertyChanged("DbStatistics");
}
/// Gets the file records in this collection.
///
/// An enumerator that allows foreach to be used to process the file records in this collection.
///
public IEnumerable GetFileRecords()
{
return this.FileRecordCollection.FindAll();
}
/// Gets the file records in this collection.
/// The predicate.
///
/// An enumerator that allows foreach to be used to process the file records in this collection.
///
public IEnumerable GetFileRecords(Func predicate)
{
return this.FileRecordCollection.Find(fr => predicate(fr));
}
/// Clears the database to its blank/initial state.
public void Clear()
{
this.FileRecordCollection.Delete(Query.All());
this.OnPropertyChanged("DbStatistics");
}
#endregion Methods
}
}