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(); } } } }