78 lines
1.9 KiB
C#
78 lines
1.9 KiB
C#
|
|
using LiteDB;
|
|
|
|
namespace StorageServer.IO;
|
|
|
|
public class FileDatabase : IDatabase
|
|
{
|
|
private LiteDatabase liteDatabase;
|
|
private const string tableName = "files";
|
|
|
|
public FileDatabase(string fileName) {
|
|
this.liteDatabase = new LiteDatabase(new ConnectionString() {
|
|
Filename = fileName,
|
|
Connection = ConnectionType.Direct,
|
|
}, new() {
|
|
EnumAsInteger = true,
|
|
});
|
|
}
|
|
|
|
public void Dispose() {
|
|
this.liteDatabase.Dispose();
|
|
}
|
|
|
|
public long Count()
|
|
{
|
|
try {
|
|
return this.liteDatabase.GetCollection(tableName).LongCount();
|
|
} catch (LiteException e) {
|
|
throw new IOException(e.Message, e);
|
|
}
|
|
}
|
|
|
|
public bool Delete(long id)
|
|
{
|
|
try {
|
|
return this.liteDatabase.GetCollection(tableName).Delete(id);
|
|
} catch (LiteException e) {
|
|
throw new IOException(e.Message, e);
|
|
}
|
|
}
|
|
|
|
public long Insert<T>(T model)
|
|
{
|
|
try {
|
|
return this.liteDatabase.GetCollection<T>(tableName).Insert(model);
|
|
} catch (LiteException e) {
|
|
throw new IOException(e.Message, e);
|
|
}
|
|
}
|
|
|
|
public T Select<T>(long id)
|
|
{
|
|
try {
|
|
return this.liteDatabase.GetCollection<T>(tableName).FindById(id);
|
|
} catch (LiteException e) {
|
|
throw new IOException(e.Message, e);
|
|
}
|
|
}
|
|
|
|
public IEnumerable<T> SelectAll<T>()
|
|
{
|
|
try {
|
|
return this.liteDatabase.GetCollection<T>(tableName).FindAll();
|
|
} catch (LiteException e) {
|
|
throw new IOException(e.Message, e);
|
|
}
|
|
}
|
|
|
|
public bool Update<T>(T model)
|
|
{
|
|
try {
|
|
return this.liteDatabase.GetCollection<T>(tableName).Update(model);
|
|
} catch (LiteException e) {
|
|
throw new IOException(e.Message, e);
|
|
}
|
|
}
|
|
}
|