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.
57 lines
1.2 KiB
57 lines
1.2 KiB
using System.Collections.Concurrent;
|
|
using System.Reflection;
|
|
using Connected.Entities.Annotations;
|
|
using Connected.Entities.Storage;
|
|
|
|
namespace Connected.ServiceModel.Client.Data;
|
|
internal sealed 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);
|
|
}
|
|
}
|