You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Connected.Framework/Connected.Data/Update/DeleteCommandBuilder.cs

58 lines
1.4 KiB

2 years ago
using Connected.Entities.Annotations;
using Connected.Entities.Storage;
using System.Collections.Concurrent;
using System.Reflection;
namespace Connected.Data.Update;
internal class DeleteCommandBuilder : CommandBuilder
{
private static readonly ConcurrentDictionary<string, StorageOperation> _cache;
static DeleteCommandBuilder()
{
_cache = new();
}
private static ConcurrentDictionary<string, StorageOperation> Cache => _cache;
protected override StorageOperation OnBuild()
{
WriteLine($"DELETE [{Schema.Schema}].[{Schema.Name}] (");
WriteWhere();
var result = new StorageOperation { CommandText = CommandText };
foreach (var parameter in Parameters)
result.AddParameter(parameter);
Cache.TryAdd(Entity.GetType().FullName, result);
return result;
}
private void WriteWhere()
{
Write("WHERE ");
foreach (var property in Properties)
{
if (property.GetCustomAttribute<PrimaryKeyAttribute>() is not null)
{
var columnName = ColumnName(property);
CreateParameter(property);
Write($"{ColumnName} = @{ColumnName}");
}
}
Write(";");
}
protected override bool TryGetExisting(out StorageOperation? result)
{
return Cache.TryGetValue(Entity.GetType().FullName, out result);
}
}