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.
68 lines
1.4 KiB
68 lines
1.4 KiB
using System.Collections.Concurrent;
|
|
using System.Reflection;
|
|
using Connected.Entities.Annotations;
|
|
using Connected.Entities.Storage;
|
|
|
|
namespace Connected.ServiceModel.Client.Data;
|
|
internal sealed class InsertCommandBuilder : CommandBuilder
|
|
{
|
|
private static readonly ConcurrentDictionary<string, StorageOperation> _cache;
|
|
|
|
static InsertCommandBuilder()
|
|
{
|
|
_cache = new();
|
|
}
|
|
|
|
private static ConcurrentDictionary<string, StorageOperation> Cache => _cache;
|
|
|
|
protected override StorageOperation OnBuild()
|
|
{
|
|
WriteLine($"INSERT [{Schema.Schema}].[{Schema.Name}] (");
|
|
|
|
WriteColumns();
|
|
WriteLine(")");
|
|
Write("VALUES (");
|
|
WriteValues();
|
|
WriteLine(");");
|
|
|
|
var result = new StorageOperation { CommandText = CommandText };
|
|
|
|
foreach (var parameter in Parameters)
|
|
result.AddParameter(parameter);
|
|
|
|
Cache.TryAdd(Entity.GetType().FullName, result);
|
|
|
|
return result;
|
|
}
|
|
|
|
private void WriteColumns()
|
|
{
|
|
foreach (var property in Properties)
|
|
{
|
|
CreateParameter(property);
|
|
|
|
Write($"{ColumnName(property)}, ");
|
|
}
|
|
|
|
Trim();
|
|
}
|
|
|
|
private void WriteValues()
|
|
{
|
|
foreach (var property in Properties)
|
|
{
|
|
if (property.GetCustomAttribute<PrimaryKeyAttribute>() is not null)
|
|
continue;
|
|
|
|
Write($"@{ColumnName(property)}, ");
|
|
}
|
|
|
|
Trim();
|
|
}
|
|
|
|
protected override bool TryGetExisting(out StorageOperation? result)
|
|
{
|
|
return Cache.TryGetValue(Entity.GetType().FullName, out result);
|
|
}
|
|
}
|