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/DefaultDrop.cs

53 lines
1.0 KiB

using System.Text;
namespace Connected.Data.Schema.Sql
{
internal class DefaultDrop : ColumnTransaction
{
public DefaultDrop(ISchemaColumn column) : base(column)
{
}
protected override async Task OnExecute()
{
if (string.IsNullOrWhiteSpace(DefaultName))
return;
await Context.Execute(CommandText);
}
private string CommandText
{
get
{
var text = new StringBuilder();
text.AppendLine($"ALTER TABLE {Escape(Context.Schema.SchemaName(), Context.Schema.Name)}");
text.AppendLine($"DROP CONSTRAINT {DefaultName};");
return text.ToString();
}
}
private string? DefaultName
{
get
{
if (Context.ExistingSchema is null)
return null;
foreach (var constraint in Context.ExistingSchema.Descriptor.Constraints)
{
if (constraint.ConstraintType == ConstraintType.Default)
{
if (constraint.Columns.Count == 1 && string.Equals(constraint.Columns[0], Column.Name, StringComparison.OrdinalIgnoreCase))
return constraint.Name;
}
}
return null;
}
}
}
}