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/Schema/Sql/IndexCreate.cs

45 lines
975 B

2 years ago
using System.Text;
namespace Connected.Data.Schema.Sql
{
internal class IndexCreate : TableTransaction
{
public IndexCreate(IndexDescriptor index)
{
Index = index;
}
private IndexDescriptor Index { get; }
protected override async Task OnExecute()
{
if (Index.Unique)
await new UniqueConstraintAdd(Index).Execute(Context);
else
await Context.Execute(CommandText);
}
private string CommandText
{
get
{
var text = new StringBuilder();
text.AppendLine($"CREATE NONCLUSTERED INDEX [{Context.GenerateConstraintName(Context.Schema.Schema, Context.Schema.Name, ConstraintNameType.Index)}] ON {Escape(Context.Schema.SchemaName(), Context.Schema.Name)}(");
var comma = string.Empty;
foreach (var column in Index.Columns)
{
text.AppendLine($"{comma}{Escape(column)} ASC");
comma = ",";
}
text.AppendLine($") ON {Escape(SchemaExtensions.FileGroup)}");
return text.ToString();
}
}
}
}