using LiteDB;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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
#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");
}
}
#endregion
#region Constructors
/// Default constructor.
public DBHelper()
{
this.Database = new LiteDatabase(DEFAULT_DB_FILE_NAME);
}
/// Constructor.
/// The database.
public DBHelper(string database)
{
this.Database = new LiteDatabase(database);
}
#endregion
#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 = new Uri(path), Size = size, Hash = hash, HashProvider = hashProvider };
this.FileRecordCollection.Insert(fileRecord);
}
/// 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());
}
#endregion
}
}