public override int GetHashCode() { unchecked { var hashCode = ColumnRef.GetHashCode(); hashCode = (hashCode * 397) ^ (Transform != null ? Transform.GetHashCode() : 0); return(hashCode); } }
public override IEnumerable <string> SqlCommands(IDbConnection connection) { foreach (var cmd in base.SqlCommands(connection)) { yield return(cmd); } // if pk column, drop referencing foreign keys, any covering indexes, and then the pk itself IEnumerable <ForeignKeyRef> referencingFKs = null; bool inPK = false; bool rebuildFKs = false; string pkConstraint; bool isClustered; CreateTable ct = new CreateTable(_newColumn.ModelType); if (_newColumn.InPrimaryKey(connection, out pkConstraint, out isClustered) || ct.InPrimaryKey(_newColumn.ColumnName, out pkConstraint)) { inPK = true; referencingFKs = connection.GetReferencingForeignKeys(_objectId); if (referencingFKs.Any(fk => fk.Parent.ColumnName.Equals(_newColumn.ColumnName))) { rebuildFKs = true; foreach (var fk in referencingFKs) { yield return($"ALTER TABLE [{fk.Child.Schema}].[{fk.Child.TableName}] DROP CONSTRAINT [{fk.ConstraintName}]"); } } // todo: indexes yield return($"ALTER TABLE [{_newColumn.DbObject.Schema}].[{_newColumn.DbObject.Name}] DROP CONSTRAINT [{pkConstraint}]"); } // alter desired column bool withCollation = ColumnRef.IsCollationChanged(_oldColumn, _newColumn); yield return($"ALTER TABLE [{_newColumn.DbObject.Schema}].[{_newColumn.DbObject.Name}] ALTER COLUMN [{_newColumn.ColumnName}] {_newColumn.GetDataTypeSyntax(withCollation)}"); // rebuild pk and foreign keys if (inPK) { string clustering = (isClustered) ? "CLUSTERED" : "NONCLUSTERED"; yield return($"ALTER TABLE [{_newColumn.DbObject.Schema}].[{_newColumn.DbObject.Name}] ADD CONSTRAINT [{pkConstraint}] PRIMARY KEY {clustering} ({ct.PrimaryKeyColumnSyntax()})"); if (rebuildFKs) { foreach (var fk in referencingFKs) { yield return($"ALTER TABLE [{fk.Child.Schema}].[{fk.Child.TableName}] ADD CONSTRAINT [{fk.ConstraintName}] FOREIGN KEY ([{fk.Child.ColumnName}]) REFERENCES [{fk.Parent.Schema}].[{fk.Parent.TableName}] ([{fk.Parent.ColumnName}])"); } } } }
public ColumnRawModel(ColumnRef dbName, int ordinalPosition, bool identity, bool nullable, string typeName, string?defaultValue, int?size, int?precision, int?scale) { this.DbName = dbName; this.OrdinalPosition = ordinalPosition; this.Identity = identity; this.Nullable = nullable; this.TypeName = typeName; this.DefaultValue = defaultValue; this.Size = size; this.Precision = precision; this.Scale = scale; }
public override bool Equals(object obj) { ColumnRef test = obj as ColumnRef; if (test != null) { return (test.Schema.ToLower().Equals(this.Schema.ToLower()) && test.TableName.ToLower().Equals(this.TableName.ToLower()) && test.ColumnName.ToLower().Equals(this.ColumnName.ToLower())); } return(false); }
//private readonly IEnumerable<ForeignKeyRef> _foreignKeys; you won't be dropping the key usually, so there's really no need to drop dependent FKs internal DropColumn(ColumnRef columnRef, IDbConnection connection) : base(MergeObjectType.Column, MergeActionType.Delete, columnRef.ToString()) { _columnRef = columnRef; ForeignKeyRef fk; if (columnRef.IsForeignKey(connection, out fk)) { _dropFK = fk; } //_foreignKeys = GetReferencingForeignKeys(connection, columnRef.ObjectID); _cn = connection; }
public static ClusteringSpec ReadXml(XmlReader reader) { var xElement = (XElement)XNode.ReadFrom(reader); var values = new List <ValueSpec>(); foreach (var elValue in xElement.Elements(EL_VALUE)) { var role = elValue.Attribute(ATTR_ROLE)?.Value; if (role == null) { continue; } ColumnRef columnRef = null; var columnid = elValue.Attribute(ATTR_COLUMN_ID)?.Value; if (columnid != null) { columnRef = new ColumnRef(new ColumnId(columnid)); } else { columnRef = new ColumnRef(PropertyPath.Parse(elValue.Attribute(ATTR_PROPERTY_PATH)?.Value)); } values.Add(new ValueSpec(columnRef, role)); } var spec = new ClusteringSpec(values); var attrDistanceMetric = xElement.Attribute(ATTR_DISTANCE_METRIC); if (attrDistanceMetric != null) { spec = spec.ChangeDistanceMetric(attrDistanceMetric.Value); } return(spec); }
public DecryptColumnCommand(ColumnRef column, bool statusCheck = false) { Column = column; StatusCheck = statusCheck; }
public string DataTypeComparison(ColumnRef columnRef) { return($"{this.DataTypeSyntax()} -> {columnRef.PropertyInfo.SqlColumnType()}"); }
public static ClusteringSpec GetDefaultClusteringSpec(CancellationToken cancellationToken, ReportResults reportResults, PivotedProperties pivotedProperties) { var values = new List <ValueSpec>(); foreach (var seriesGroup in pivotedProperties.SeriesGroups) { foreach (var series in seriesGroup.SeriesList) { var columnRef = ColumnRef.FromPivotedPropertySeries(series); if (columnRef == null) { continue; } if (reportResults.RowCount >= MIN_ROWS_TO_ASSUME_HEADER && EqualValuesInAllRows(cancellationToken, reportResults, series)) { values.Add(new ValueSpec(columnRef, ClusterRole.COLUMNHEADER)); } else { var transform = ClusterRole.IsNumericType(series.PropertyType) ? ClusterRole.ZSCORE : ClusterRole.BOOLEAN; values.Add(new ValueSpec(columnRef, transform)); } } } using (var propertyEnumerator = pivotedProperties.UngroupedProperties.GetEnumerator()) { while (propertyEnumerator.MoveNext()) { var columnRef = ColumnRef.FromPropertyDescriptor(propertyEnumerator.Current); if (columnRef == null) { continue; } values.Insert(0, new ValueSpec(columnRef, ClusterRole.ROWHEADER)); break; } if (values.Count == 1) { while (propertyEnumerator.MoveNext()) { var propertyDescriptor = propertyEnumerator.Current; if (!ClusterRole.IsNumericType(propertyDescriptor?.PropertyType)) { continue; } var columnRef = ColumnRef.FromPropertyDescriptor(propertyDescriptor); if (columnRef == null) { continue; } values.Add(new ValueSpec(columnRef, ClusterRole.RAW)); } if (values.Count == 1) { return(null); } } } if (values.Count == 0) { return(null); } return(new ClusteringSpec(values).ChangeDistanceMetric(ClusterMetricType.EUCLIDEAN.Name)); }
public DropColumn(ColumnRef columnRef, Type modelType) : base(MergeObjectType.Column, MergeActionType.Drop, $"Drop column {columnRef}", nameof(DropColumn)) { _columnRef = columnRef; _modelType = modelType; }
public ValueSpec(ColumnRef columnRef, string transform) { ColumnRef = columnRef; Transform = transform; }
private static string ToColCrlName(ColumnRef columnRef) { return(StringHelper.DeSnake(columnRef.Name)); }
public ValueSpec(ColumnRef columnRef, ClusterRole transform) : this(columnRef, transform.Name) { }
protected bool Equals(ColumnRef other) { return(Equals(PropertyPath, other.PropertyPath) && Equals(ColumnId, other.ColumnId)); }
public RetypeColumn(ColumnRef fromSchemaColumn, ColumnRef toModelColumn) : base(MergeObjectType.Column, MergeActionType.Retype, $"{toModelColumn.ToString()}: {fromSchemaColumn.DataTypeComparison(toModelColumn)}") { _modelColumn = toModelColumn.PropertyInfo; }
public EncryptColumnCommand(ColumnRef column, ColumnEncryptionFlags encryptionFlags, bool statusCheck = false) { Column = column; EncryptionFlags = encryptionFlags; StatusCheck = statusCheck; }
public AlterColumn(ColumnRef newColumn, ColumnRef oldColumn) : base(MergeObjectType.Column, MergeActionType.Alter, $"{newColumn.ToString()}: {ColumnRef.CompareSyntaxes(oldColumn, newColumn)}", nameof(AlterColumn)) { _newColumn = newColumn; _oldColumn = oldColumn; _objectId = oldColumn.ObjectID; // this comes from GetSchemaColumns, so it has the ObjectID }