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 _cache; static DeleteCommandBuilder() { _cache = new(); } private static ConcurrentDictionary 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() 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); } }