|
|
|
@ -10,6 +10,7 @@ using System.Threading; |
|
|
|
using System.Threading.Tasks; |
|
|
|
using System.Threading.Tasks; |
|
|
|
using GBase.Api; |
|
|
|
using GBase.Api; |
|
|
|
using GBase.Attributes; |
|
|
|
using GBase.Attributes; |
|
|
|
|
|
|
|
using GBase.Exceptions; |
|
|
|
using GBase.Factories; |
|
|
|
using GBase.Factories; |
|
|
|
using GBase.FileHandling.Factories; |
|
|
|
using GBase.FileHandling.Factories; |
|
|
|
using GBase.Helpers; |
|
|
|
using GBase.Helpers; |
|
|
|
@ -66,6 +67,18 @@ namespace GBase |
|
|
|
/// </summary> |
|
|
|
/// </summary> |
|
|
|
public List<T> Entries { get; } |
|
|
|
public List<T> Entries { get; } |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private int CurrentLastKey |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
get |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
T lastEntry = Entries.LastOrDefault(); |
|
|
|
|
|
|
|
if (lastEntry == null) |
|
|
|
|
|
|
|
return -1; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return lastEntry.Key; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary> |
|
|
|
/// <summary> |
|
|
|
/// Initialize this <see cref="IGBase"/> |
|
|
|
/// Initialize this <see cref="IGBase"/> |
|
|
|
@ -83,11 +96,13 @@ namespace GBase |
|
|
|
//TODO: Init columns list depending on GBaseColumnAttributes set for this GBaseTable |
|
|
|
//TODO: Init columns list depending on GBaseColumnAttributes set for this GBaseTable |
|
|
|
foreach (var property in type.GetProperties()) |
|
|
|
foreach (var property in type.GetProperties()) |
|
|
|
{ |
|
|
|
{ |
|
|
|
GBaseColumnAttribute gBaseColumnAttribute = property.GetCustomAttribute<GBaseColumnAttribute>(); |
|
|
|
GBaseColumnAttribute gBaseColumnAttribute = property.GetCustomAttribute<GBaseColumnAttribute>() ?? |
|
|
|
|
|
|
|
property.GetAttributeFromInheritedInterfaces<GBaseColumnAttribute>(); |
|
|
|
|
|
|
|
|
|
|
|
if (gBaseColumnAttribute == null) |
|
|
|
if (gBaseColumnAttribute == null) |
|
|
|
continue; |
|
|
|
continue; |
|
|
|
|
|
|
|
|
|
|
|
IGBaseColumn gBaseColumn = _gBaseColumnFactory.Create(property.Name, property.PropertyType); |
|
|
|
IGBaseColumn gBaseColumn = _gBaseColumnFactory.Create(property.Name, property.PropertyType, gBaseColumnAttribute.IsKey); |
|
|
|
AddColumn(gBaseColumn); |
|
|
|
AddColumn(gBaseColumn); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ -102,7 +117,7 @@ namespace GBase |
|
|
|
T entry = new T(); |
|
|
|
T entry = new T(); |
|
|
|
|
|
|
|
|
|
|
|
List<object> parameters = new List<object>(); |
|
|
|
List<object> parameters = new List<object>(); |
|
|
|
foreach (var column in Columns) |
|
|
|
foreach (var column in Columns.Where(c => !c.IsKey)) |
|
|
|
{ |
|
|
|
{ |
|
|
|
using IPoolItem<IDataHandler> dataHandlerItem = await _dataHandlerPool.RequestDataHandler(this, false, cancellationToken); |
|
|
|
using IPoolItem<IDataHandler> dataHandlerItem = await _dataHandlerPool.RequestDataHandler(this, false, cancellationToken); |
|
|
|
|
|
|
|
|
|
|
|
@ -112,8 +127,18 @@ namespace GBase |
|
|
|
typeof(T), column.Type, |
|
|
|
typeof(T), column.Type, |
|
|
|
file.File, column.Name, cancellationToken)); |
|
|
|
file.File, column.Name, cancellationToken)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
IGBaseColumn keyColumn = Columns.FirstOrDefault(c => c.IsKey); |
|
|
|
|
|
|
|
if (keyColumn == null) |
|
|
|
|
|
|
|
throw new MissingKeyColumnException<T>(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
GBaseKey key = new GBaseKey(); |
|
|
|
|
|
|
|
using (IPoolItem<IDataHandler> dataHandlerItem = await _dataHandlerPool.RequestDataHandler(this, false, cancellationToken)) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
key.Key = await dataHandlerItem.Use().GetValue<T, int>(file.File, keyColumn.Name, cancellationToken); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
entry.Initialize(parameters); |
|
|
|
entry.Initialize(key, parameters); |
|
|
|
|
|
|
|
|
|
|
|
file.Entry = entry; |
|
|
|
file.Entry = entry; |
|
|
|
Entries.Add(entry); |
|
|
|
Entries.Add(entry); |
|
|
|
@ -158,6 +183,13 @@ namespace GBase |
|
|
|
/// <returns>True if successful, false if not</returns> |
|
|
|
/// <returns>True if successful, false if not</returns> |
|
|
|
public async Task<bool> AddEntry(T entry, CancellationToken cancellationToken) |
|
|
|
public async Task<bool> AddEntry(T entry, CancellationToken cancellationToken) |
|
|
|
{ |
|
|
|
{ |
|
|
|
|
|
|
|
entry.Key ??= new GBaseKey(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (entry.Key != -1) |
|
|
|
|
|
|
|
throw new InvalidKeyException("Key of entry is already set. Make sure it is -1 when initializing."); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
entry.Key.Key = CurrentLastKey + 1; //set next possible key |
|
|
|
|
|
|
|
|
|
|
|
Entries.Add(entry); |
|
|
|
Entries.Add(entry); |
|
|
|
|
|
|
|
|
|
|
|
using IGBaseFile file = _fileHandler.CreateEntryFile(entry, this); |
|
|
|
using IGBaseFile file = _fileHandler.CreateEntryFile(entry, this); |
|
|
|
|