/// <summary> /// Returns true if this attribute specifies the same name and configuration as the given attribute. /// </summary> /// <param name="other">The attribute to compare.</param> /// <returns>True if the other object is equal to this object; otherwise false.</returns> protected virtual bool Equals(IndexAttribute other) { return(_name == other._name && _order == other._order && _isClustered.Equals(other._isClustered) && _isUnique.Equals(other._isUnique)); }
public void TypeId_returns_different_values_for_different_instances() { Assert.NotEqual(new IndexAttribute().TypeId, new IndexAttribute().TypeId); var index = new IndexAttribute(); Assert.Equal(index.TypeId, index.TypeId); }
public void IsCompatibleWith_returns_true_if_other_is_same_or_null() { var attribute = new IndexAttribute(); Assert.True(attribute.IsCompatibleWith(attribute)); Assert.True(attribute.IsCompatibleWith(null)); }
public void MergeWith_returns_same_instance_if_other_is_same_or_null() { var attribute = new IndexAttribute(); Assert.Same(attribute, attribute.MergeWith(attribute)); Assert.Same(attribute, attribute.MergeWith(null)); }
/// <summary> /// Returns true if this attribute specifies the same name and configuration as the given attribute. /// </summary> /// <param name="other">The attribute to compare.</param> /// <returns>True if the other object is equal to this object; otherwise false.</returns> protected virtual bool Equals(IndexAttribute other) { if (this._name == other._name && this._order == other._order && this._isClustered.Equals((object)other._isClustered)) { return(this._isUnique.Equals((object)other._isUnique)); } return(false); }
public ConsolidatedIndex(string table, IndexAttribute index) { DebugCheck.NotEmpty(table); DebugCheck.NotNull(index); _table = table; _index = index; }
public void IsCompatibleWith_returns_false_for_non_matching_properties() { var result = new IndexAttribute("MrsPandy").IsCompatibleWith(new IndexAttribute("EekyBear")); Assert.False(result); Assert.Equal(Strings.ConflictingIndexAttributeProperty("Name", "MrsPandy", "EekyBear"), result.ErrorMessage); result = new IndexAttribute().IsCompatibleWith(new IndexAttribute("EekyBear")); Assert.False(result); Assert.Equal(Strings.ConflictingIndexAttributeProperty("Name", "", "EekyBear"), result.ErrorMessage); result = new IndexAttribute("MrsPandy").IsCompatibleWith(new IndexAttribute()); Assert.False(result); Assert.Equal(Strings.ConflictingIndexAttributeProperty("Name", "MrsPandy", ""), result.ErrorMessage); result = new IndexAttribute { Order = 7 }.IsCompatibleWith(new IndexAttribute { Order = 8 }); Assert.False(result); Assert.Equal(Strings.ConflictingIndexAttributeProperty("Order", "7", "8"), result.ErrorMessage); result = new IndexAttribute { IsClustered = false }.IsCompatibleWith(new IndexAttribute { IsClustered = true }); Assert.False(result); Assert.Equal(Strings.ConflictingIndexAttributeProperty("IsClustered", "False", "True"), result.ErrorMessage); result = new IndexAttribute { IsUnique = true }.IsCompatibleWith(new IndexAttribute { IsUnique = false }); Assert.False(result); Assert.Equal(Strings.ConflictingIndexAttributeProperty("IsUnique", "True", "False"), result.ErrorMessage); result = new IndexAttribute("MrsPandy", 8) { IsClustered = true, IsUnique = false } .IsCompatibleWith(new IndexAttribute("EekyBear", 7) { IsClustered = false, IsUnique = true }); Assert.False(result); Assert.Equal( Strings.ConflictingIndexAttributeProperty("Name", "MrsPandy", "EekyBear") + Environment.NewLine + "\t" + Strings.ConflictingIndexAttributeProperty("Order", "8", "7") + Environment.NewLine + "\t" + Strings.ConflictingIndexAttributeProperty("IsClustered", "True", "False") + Environment.NewLine + "\t" + Strings.ConflictingIndexAttributeProperty("IsUnique", "False", "True"), result.ErrorMessage); }
public ConsolidatedIndex(string table, string column, IndexAttribute index) : this(table, index) { DebugCheck.NotEmpty(table); DebugCheck.NotEmpty(column); DebugCheck.NotNull(index); _columns[index.Order] = column; }
// For example: "{ Name: 'Xyz', Order: 1, IsClustered: True, IsUnique: False }" internal static string SerializeIndexAttribute(IndexAttribute indexAttribute) { DebugCheck.NotNull(indexAttribute); var builder = new StringBuilder("{ "); if (!string.IsNullOrWhiteSpace(indexAttribute.Name)) { builder .Append("Name: ") .Append( indexAttribute.Name .Replace(",", @"\,") .Replace("{", @"\{")); } if (indexAttribute.Order != -1) { if (builder.Length > 2) { builder.Append(", "); } builder.Append("Order: ").Append(indexAttribute.Order); } if (indexAttribute.IsClusteredConfigured) { if (builder.Length > 2) { builder.Append(", "); } builder.Append("IsClustered: ").Append(indexAttribute.IsClustered); } if (indexAttribute.IsUniqueConfigured) { if (builder.Length > 2) { builder.Append(", "); } builder.Append("IsUnique: ").Append(indexAttribute.IsUnique); } if (builder.Length > 2) { builder.Append(" "); } builder.Append("}"); return builder.ToString(); }
internal static IndexAttribute MergeWith(this IndexAttribute me, IndexAttribute other, bool ignoreOrder = false) { DebugCheck.NotNull(me); if (ReferenceEquals(me, other) || other == null) { return me; } var isCompatible = me.IsCompatibleWith(other, ignoreOrder); if (!isCompatible) { throw new InvalidOperationException( Strings.ConflictingIndexAttribute(me.Name, Environment.NewLine + "\t" + isCompatible.ErrorMessage)); } var merged = me.Name != null ? new IndexAttribute(me.Name) : other.Name != null ? new IndexAttribute(other.Name) : new IndexAttribute(); if (!ignoreOrder) { if (me.Order != -1) { merged.Order = me.Order; } else if (other.Order != -1) { merged.Order = other.Order; } } if (me.IsClusteredConfigured) { merged.IsClustered = me.IsClustered; } else if (other.IsClusteredConfigured) { merged.IsClustered = other.IsClustered; } if (me.IsUniqueConfigured) { merged.IsUnique = me.IsUnique; } else if (other.IsUniqueConfigured) { merged.IsUnique = other.IsUnique; } return merged; }
public void Equals_returns_true_when_attributes_match() { Assert.True(new IndexAttribute().Equals(new IndexAttribute())); Assert.True(new IndexAttribute("MrsPandy").Equals(new IndexAttribute("MrsPandy"))); Assert.True(new IndexAttribute { Order = 7 }.Equals(new IndexAttribute { Order = 7 })); Assert.True(new IndexAttribute { IsClustered = true }.Equals(new IndexAttribute { IsClustered = true })); Assert.True(new IndexAttribute { IsClustered = false }.Equals(new IndexAttribute { IsClustered = false })); Assert.True(new IndexAttribute { IsUnique = true }.Equals(new IndexAttribute { IsUnique = true })); Assert.True(new IndexAttribute { IsUnique = false }.Equals(new IndexAttribute { IsUnique = false })); Assert.True( new IndexAttribute("MrsPandy", 7) { IsUnique = true, IsClustered = false } .Equals(new IndexAttribute("MrsPandy", 7) { IsUnique = true, IsClustered = false })); var attribute = new IndexAttribute(); Assert.True(attribute.Equals(attribute)); }
internal static CompatibilityResult IsCompatibleWith(this IndexAttribute me, IndexAttribute other, bool ignoreOrder = false) { DebugCheck.NotNull(me); if (ReferenceEquals(me, other) || other == null) { return new CompatibilityResult(true, null); } string errorMessage = null; if (me.Name != other.Name) { errorMessage = Strings.ConflictingIndexAttributeProperty("Name", me.Name, other.Name); } if (!ignoreOrder && me.Order != -1 && other.Order != -1 && me.Order != other.Order) { errorMessage = errorMessage == null ? "" : errorMessage + (Environment.NewLine + "\t"); errorMessage += Strings.ConflictingIndexAttributeProperty("Order", me.Order, other.Order); } if (me.IsClusteredConfigured && other.IsClusteredConfigured && me.IsClustered != other.IsClustered) { errorMessage = errorMessage == null ? "" : errorMessage + (Environment.NewLine + "\t"); errorMessage += Strings.ConflictingIndexAttributeProperty("IsClustered", me.IsClustered, other.IsClustered); } if (me.IsUniqueConfigured && other.IsUniqueConfigured && me.IsUnique != other.IsUnique) { errorMessage = errorMessage == null ? "" : errorMessage + (Environment.NewLine + "\t"); errorMessage += Strings.ConflictingIndexAttributeProperty("IsUnique", me.IsUnique, other.IsUnique); } return new CompatibilityResult(errorMessage == null, errorMessage); }
/// <summary> /// Returns true if this attribute does not conflict with the given attribute such that /// the two can be combined together using the <see cref="MergeWith"/> method. /// </summary> /// <remarks> /// Two attributes are considered compatible if they have the same name and all other properties /// (<see cref="IsUnique"/>, <see cref="IsClustered"/>, and <see cref="Order"/>) are either not specified /// on this attribute or the other attribute or are specified with the same value on both. /// </remarks> /// <param name="other">The attribute to compare.</param> /// <returns>A CompatibilityResult indicating whether or not this attribute is compatible with the other.</returns> public virtual CompatibilityResult IsCompatibleWith(IndexAttribute other) { if (ReferenceEquals(this, other) || other == null) { return(new CompatibilityResult(true, null)); } string errorMessage = null; if (_name != other._name) { errorMessage = Strings.ConflictingIndexAttributeProperty("Name", _name, other._name); } if (_order != -1 && other._order != -1 && _order != other._order) { errorMessage = errorMessage == null ? "" : errorMessage + (Environment.NewLine + "\t"); errorMessage += Strings.ConflictingIndexAttributeProperty("Order", _order, other._order); } if (_isClustered != null && other._isClustered != null && _isClustered != other._isClustered) { errorMessage = errorMessage == null ? "" : errorMessage + (Environment.NewLine + "\t"); errorMessage += Strings.ConflictingIndexAttributeProperty("IsClustered", _isClustered, other._isClustered); } if (_isUnique != null && other._isUnique != null && _isUnique != other._isUnique) { errorMessage = errorMessage == null ? "" : errorMessage + (Environment.NewLine + "\t"); errorMessage += Strings.ConflictingIndexAttributeProperty("IsUnique", _isUnique, other._isUnique); } return(new CompatibilityResult(errorMessage == null, errorMessage)); }
/// <summary> /// Merges this attribute with the given attribute and returns a new attribute containing the merged properties. /// </summary> /// <remarks> /// The other attribute must have the same name as this attribute. For other properties, if neither attribute /// specifies a value, then the property on the merged attribute is also unspecified. If one /// attribute but not the other specifies a value, then the property on the merged attribute gets that value. /// If both properties specify a value, then those values must match and the property on the merged /// attribute gets that value. /// </remarks> /// <param name="other">The attribute to merge with this one.</param> /// <returns>A new attribute with properties merged.</returns> /// <exception cref="InvalidOperationException"> /// The other attribute is not compatible with this attribute as determined by the <see cref="IsCompatibleWith"/> method. /// </exception> public virtual IndexAttribute MergeWith(IndexAttribute other) { if (ReferenceEquals(this, other) || other == null) { return(this); } var isCompatible = IsCompatibleWith(other); if (!isCompatible) { throw new InvalidOperationException( Strings.ConflictingIndexAttribute(_name, Environment.NewLine + "\t" + isCompatible.ErrorMessage)); } return(new IndexAttribute( _name ?? other._name, _order != -1 ? _order : other._order, _isClustered ?? other._isClustered, _isUnique ?? other._isUnique)); }
public void Add(string columnName, IndexAttribute index) { DebugCheck.NotEmpty(columnName); DebugCheck.NotNull(index); Debug.Assert(_index.Name == index.Name); if (_columns.ContainsKey(index.Order)) { throw new InvalidOperationException( Strings.OrderConflictWhenConsolidating(index.Name, _table, index.Order, _columns[index.Order], columnName)); } _columns[index.Order] = columnName; var compat = _index.IsCompatibleWith(index, ignoreOrder: true); if (!compat) { throw new InvalidOperationException(Strings.ConflictWhenConsolidating(index.Name, _table, compat.ErrorMessage)); } _index = _index.MergeWith(index, ignoreOrder: true); }
public static void BuildModel(DbModelBuilder modelBuilder) { ConfigureConventions(modelBuilder); ConfigureRelations(modelBuilder); ConfigureConstraints(modelBuilder); EntityTypeConfiguration<User> userConfiguration = modelBuilder.Entity<User>().ToTable("User"); userConfiguration.HasMany(u => u.UserRoles).WithRequired().HasForeignKey(role => role.UserId); userConfiguration.HasMany(u => u.Claims).WithRequired().HasForeignKey(claim => claim.UserId); userConfiguration.HasMany(u => u.Logins).WithRequired().HasForeignKey(login => login.UserId); StringPropertyConfiguration propertyUserNameConfiguration = userConfiguration.Property(u => u.UserName).IsRequired().HasMaxLength(256); string annotationUserName = "******"; IndexAttribute annotationUserNameIndexAttribute = new IndexAttribute("UserNameIndex") { IsUnique = true }; IndexAnnotation annotationUserNameIndex = new IndexAnnotation(annotationUserNameIndexAttribute); propertyUserNameConfiguration.HasColumnAnnotation(annotationUserName, annotationUserNameIndex); userConfiguration.Property(u => u.Email).HasMaxLength(256); modelBuilder.Entity<UserRole>().HasKey(role => new { role.UserId, role.RoleId }); modelBuilder.Entity<UserRole>().ToTable("UsersRoles"); modelBuilder.Entity<UserLogin>().HasKey(login => new { login.LoginProvider, login.ProviderKey, login.UserId }); modelBuilder.Entity<UserLogin>().ToTable("UserLogin"); modelBuilder.Entity<UserClaim>().ToTable("UserClaim"); EntityTypeConfiguration<Role> roleConfiguration = modelBuilder.Entity<Role>().ToTable("Role"); StringPropertyConfiguration propertyNameConfiguration = roleConfiguration.Property(r => r.Name).IsRequired().HasMaxLength(256); string annotationName = "Index"; IndexAttribute annotationNameIndexAttribute = new IndexAttribute("RoleNameIndex") { IsUnique = true }; IndexAnnotation annotationNameIndex = new IndexAnnotation(annotationNameIndexAttribute); propertyNameConfiguration.HasColumnAnnotation(annotationName, annotationNameIndex); roleConfiguration.HasMany(role => role.UserRoles).WithRequired().HasForeignKey(userRole => userRole.RoleId); }
public void MergeWith_merges_properties_of_compatible_attributes() { Assert.Null(new IndexAttribute().MergeWith(new IndexAttribute()).Name); Assert.Equal("MrsPandy", new IndexAttribute("MrsPandy").MergeWith(new IndexAttribute("MrsPandy")).Name); Assert.Equal(7, new IndexAttribute { Order = 7 }.MergeWith(new IndexAttribute()).Order); Assert.Equal(7, new IndexAttribute().MergeWith(new IndexAttribute { Order = 7 }).Order); Assert.Equal(7, new IndexAttribute { Order = 7 }.MergeWith(new IndexAttribute { Order = 7 }).Order); Assert.True(new IndexAttribute { IsClustered = true }.MergeWith(new IndexAttribute()).IsClusteredConfigured); Assert.True(new IndexAttribute().MergeWith(new IndexAttribute { IsClustered = true }).IsClusteredConfigured); Assert.True(new IndexAttribute { IsClustered = true }.MergeWith(new IndexAttribute { IsClustered = true }).IsClusteredConfigured); Assert.True(new IndexAttribute { IsClustered = true }.MergeWith(new IndexAttribute()).IsClustered); Assert.True(new IndexAttribute().MergeWith(new IndexAttribute { IsClustered = true }).IsClustered); Assert.True(new IndexAttribute { IsClustered = true }.MergeWith(new IndexAttribute { IsClustered = true }).IsClustered); Assert.True(new IndexAttribute { IsClustered = false }.MergeWith(new IndexAttribute()).IsClusteredConfigured); Assert.True(new IndexAttribute().MergeWith(new IndexAttribute { IsClustered = false }).IsClusteredConfigured); Assert.True(new IndexAttribute { IsClustered = false }.MergeWith(new IndexAttribute { IsClustered = false }).IsClusteredConfigured); Assert.False(new IndexAttribute { IsClustered = false }.MergeWith(new IndexAttribute()).IsClustered); Assert.False(new IndexAttribute().MergeWith(new IndexAttribute { IsClustered = false }).IsClustered); Assert.False(new IndexAttribute { IsClustered = false }.MergeWith(new IndexAttribute { IsClustered = false }).IsClustered); Assert.True(new IndexAttribute { IsUnique = true }.MergeWith(new IndexAttribute()).IsUniqueConfigured); Assert.True(new IndexAttribute().MergeWith(new IndexAttribute { IsUnique = true }).IsUniqueConfigured); Assert.True(new IndexAttribute { IsUnique = true }.MergeWith(new IndexAttribute { IsUnique = true }).IsUniqueConfigured); Assert.True(new IndexAttribute { IsUnique = true }.MergeWith(new IndexAttribute()).IsUnique); Assert.True(new IndexAttribute().MergeWith(new IndexAttribute { IsUnique = true }).IsUnique); Assert.True(new IndexAttribute { IsUnique = true }.MergeWith(new IndexAttribute { IsUnique = true }).IsUnique); Assert.True(new IndexAttribute { IsUnique = false }.MergeWith(new IndexAttribute()).IsUniqueConfigured); Assert.True(new IndexAttribute().MergeWith(new IndexAttribute { IsUnique = false }).IsUniqueConfigured); Assert.True(new IndexAttribute { IsUnique = false }.MergeWith(new IndexAttribute { IsUnique = false }).IsUniqueConfigured); Assert.False(new IndexAttribute { IsUnique = false }.MergeWith(new IndexAttribute()).IsUnique); Assert.False(new IndexAttribute().MergeWith(new IndexAttribute { IsUnique = false }).IsUnique); Assert.False(new IndexAttribute { IsUnique = false }.MergeWith(new IndexAttribute { IsUnique = false }).IsUnique); var merged = new IndexAttribute("MrsPandy", 7) { IsUnique = true, IsClustered = false } .MergeWith(new IndexAttribute("MrsPandy")); Assert.Equal("MrsPandy", merged.Name); Assert.Equal(7, merged.Order); Assert.True(merged.IsClusteredConfigured); Assert.False(merged.IsClustered); Assert.True(merged.IsUniqueConfigured); Assert.True(merged.IsUnique); merged = new IndexAttribute("MrsPandy") .MergeWith(new IndexAttribute("MrsPandy", 7) { IsUnique = true, IsClustered = false }); Assert.Equal("MrsPandy", merged.Name); Assert.Equal(7, merged.Order); Assert.True(merged.IsClusteredConfigured); Assert.False(merged.IsClustered); Assert.True(merged.IsUniqueConfigured); Assert.True(merged.IsUnique); merged = new IndexAttribute("MrsPandy", 7) { IsUnique = true, IsClustered = false } .MergeWith(new IndexAttribute("MrsPandy", 7) { IsUnique = true, IsClustered = false }); Assert.Equal("MrsPandy", merged.Name); Assert.Equal(7, merged.Order); Assert.True(merged.IsClusteredConfigured); Assert.False(merged.IsClustered); Assert.True(merged.IsUniqueConfigured); Assert.True(merged.IsUnique); }
/// <summary> /// Deserializes the given string back into an <see cref="IndexAnnotation" /> object. /// </summary> /// <param name="name">The name of the annotation that is being deserialized.</param> /// <param name="value">The string to deserialize.</param> /// <returns>The deserialized annotation value.</returns> /// <exception cref="FormatException">If there is an error reading the serialized value.</exception> public virtual object Deserialize(string name, string value) { Check.NotEmpty(name, "name"); Check.NotEmpty(value, "value"); value = value.Trim(); if (!value.StartsWith("{", StringComparison.Ordinal) || !value.EndsWith("}", StringComparison.Ordinal)) { throw BuildFormatException(value); } var indexes = new List<IndexAttribute>(); var indexStrings = _indexesSplitter.Split(value).Select(s => s.Trim()).ToList(); indexStrings[0] = indexStrings[0].Substring(1); var lastIndex = indexStrings.Count - 1; indexStrings[lastIndex] = indexStrings[lastIndex].Substring(0, indexStrings[lastIndex].Length - 1); foreach (var indexString in indexStrings) { var indexAttribute = new IndexAttribute(); if (!string.IsNullOrWhiteSpace(indexString)) { foreach (var indexPart in _indexPartsSplitter.Split(indexString).Select(s => s.Trim())) { if (indexPart.StartsWith("Name:", StringComparison.Ordinal)) { var indexName = indexPart.Substring(5).Trim(); if (string.IsNullOrWhiteSpace(indexName) || !string.IsNullOrWhiteSpace(indexAttribute.Name)) { throw BuildFormatException(value); } indexAttribute.Name = indexName.Replace(@"\,", ",").Replace(@"\{", "{"); } else if (indexPart.StartsWith("Order:", StringComparison.Ordinal)) { int order; if (!int.TryParse(indexPart.Substring(6).Trim(), out order) || indexAttribute.Order != -1) { throw BuildFormatException(value); } indexAttribute.Order = order; } else if (indexPart.StartsWith("IsClustered:", StringComparison.Ordinal)) { bool isClustered; if (!bool.TryParse(indexPart.Substring(12).Trim(), out isClustered) || indexAttribute.IsClusteredConfigured) { throw BuildFormatException(value); } indexAttribute.IsClustered = isClustered; } else if (indexPart.StartsWith("IsUnique:", StringComparison.Ordinal)) { bool isUnique; if (!bool.TryParse(indexPart.Substring(9).Trim(), out isUnique) || indexAttribute.IsUniqueConfigured) { throw BuildFormatException(value); } indexAttribute.IsUnique = isUnique; } else { throw BuildFormatException(value); } } } indexes.Add(indexAttribute); } return new IndexAnnotation(indexes); }
/// <summary> /// Creates a new annotation for the given index. /// </summary> /// <param name="indexAttribute">An index attributes representing an index.</param> public IndexAnnotation(IndexAttribute indexAttribute) { Check.NotNull(indexAttribute, "indexAttribute"); _indexes.Add(indexAttribute); }
/// <summary> /// Returns true if this attribute specifies the same name and configuration as the given attribute. /// </summary> /// <param name="other">The attribute to compare.</param> /// <returns>True if the other object is equal to this object; otherwise false.</returns> protected virtual bool Equals(IndexAttribute other) { return _name == other._name && _order == other._order && _isClustered.Equals(other._isClustered) && _isUnique.Equals(other._isUnique); }