public override IEnumerable <string> SqlCommands(IDbConnection connection) { foreach (var cmd in base.SqlCommands(connection)) { yield return(cmd); } DbObject obj = DbObject.FromType(_propertyInfo.DeclaringType); obj.SquareBraces = false; yield return($"EXEC sp_rename '{obj}.{_attr.OldName}', '{_propertyInfo.SqlColumnName()}', 'COLUMN'"); ForeignKeyAttribute fkAttr = _propertyInfo.GetAttribute <ForeignKeyAttribute>(); if (fkAttr != null) { yield return($"ALTER TABLE [{obj.Schema}].[{obj.Name}] DROP CONSTRAINT [FK_{obj.ConstraintName()}_{_attr.OldName}]"); if (fkAttr.CreateIndex) { yield return($"DROP INDEX [IX_{DbObject.ConstraintName(_propertyInfo.DeclaringType)}_{_attr.OldName}] ON [{obj.Schema}].[{obj.Name}]"); } CreateForeignKey fk = new CreateForeignKey(_propertyInfo); foreach (var cmd in fk.SqlCommands(connection)) { yield return(cmd); } } }
private IEnumerable <string> CreateTableUniqueConstraints(ClusterAttribute clusterAttribute) { List <string> results = new List <string>(); if (PrimaryKeyColumns(markedOnly: true).Any()) { results.Add($"CONSTRAINT [U_{DbObject.ConstraintName(_modelType)}_Id] UNIQUE {clusterAttribute.Syntax(ClusterOption.Identity)}([Id])"); } results.AddRange(_modelType.GetProperties().Where(pi => pi.HasAttribute <UniqueKeyAttribute>()).Select(pi => { UniqueKeyAttribute attr = pi.GetCustomAttribute <UniqueKeyAttribute>(); return($"CONSTRAINT [U_{DbObject.ConstraintName(_modelType)}_{pi.SqlColumnName()}] UNIQUE {attr.GetClusteredSyntax()}([{pi.SqlColumnName()}])"); })); results.AddRange(_modelType.GetCustomAttributes <UniqueKeyAttribute>().Select((u, i) => { string constrainName = (string.IsNullOrEmpty(u.ConstraintName)) ? $"U_{DbObject.ConstraintName(_modelType)}_{i}" : u.ConstraintName; return($"CONSTRAINT [{constrainName}] UNIQUE {u.GetClusteredSyntax()}({string.Join(", ", u.ColumnNames.Select(col => $"[{col}]"))})"); })); return(results); }
public static string ForeignKeyName(this PropertyInfo propertyInfo) { var fk = GetForeignKeyAttribute(propertyInfo); return($"FK_{DbObject.ConstraintName(propertyInfo.DeclaringType)}_{propertyInfo.SqlColumnName()}"); }
internal bool InPrimaryKey(string columnName, out string pkName) { pkName = $"PK_{DbObject.ConstraintName(_modelType)}"; return(_modelType.GetProperties().Any(pi => pi.SqlColumnName().Equals(columnName) && pi.HasAttribute <PrimaryKeyAttribute>())); }
internal string CreateTablePrimaryKey(ClusterAttribute clusterAttribute) { return($"CONSTRAINT [PK_{DbObject.ConstraintName(_modelType)}] PRIMARY KEY {clusterAttribute.Syntax(ClusterOption.PrimaryKey)}({string.Join(", ", PrimaryKeyColumns().Select(col => $"[{col}]"))})"); }
public override IEnumerable <string> SqlCommands(IDbConnection connection) { foreach (var cmd in base.SqlCommands(connection)) { yield return(cmd); } foreach (var cmd in connection.GetFKDropStatements(_object.ObjectId)) { yield return(cmd); } yield return($"ALTER TABLE [{_object.Schema}].[{_object.Name}] DROP CONSTRAINT [PK_{DbObject.ConstraintName(_object.ModelType)}]"); }