public void AddRelation(Relation relation) { RelationName relationName = context.RelationNames.Where(x => x.Id == relation.RelationNameId).FirstOrDefault(); RelationName mirror = context.RelationNames.Where(x => x.Name == relationName.MirrorName).FirstOrDefault(); Relation mirrorRelation = new Relation() { SourceId = relation.TargetId, TargetId = relation.SourceId, RelationNameId = mirror.Id }; relation.TargetPerson = null; using (var transaction = context.Database.BeginTransaction()) { try { context.Relations.Add(relation); context.Relations.Add(mirrorRelation); context.SaveChanges(); transaction.Commit(); } catch (Exception ex) { transaction.Rollback(); } } }
[U] public void Null() { RelationName value = "foo"; (value == null).Should().BeFalse(); (null == value).Should().BeFalse(); }
protected virtual Command OnCreateTable(IMetadata sourceMetadata, IMetadata targetMetadata, IComparerContext context) { var command = new Command(); var isGtt = MetadataRelationType == MetadataRelationType.GLOBAL_TEMPORARY_PRESERVE || MetadataRelationType == MetadataRelationType.GLOBAL_TEMPORARY_DELETE; var isExternal = MetadataRelationType == MetadataRelationType.EXTERNAL; command.Append("CREATE "); if (isGtt) { command.Append("GLOBAL TEMPORARY "); } command.Append($"TABLE {RelationName.AsSqlIndentifier()} "); if (isExternal) { command.Append($"EXTERNAL '{SqlHelper.DoubleSingleQuotes(ExternalFile)}' "); } command.Append("("); command.AppendLine(); InsertColumns(command, sourceMetadata, targetMetadata, context, false); command.AppendLine(); command.Append(")"); if (isGtt) { command.AppendLine(); command.Append($"ON COMMIT {MetadataRelationType.ToDescription()} ROWS"); } return(command); }
protected override IEnumerable <Command> OnCreate(IMetadata sourceMetadata, IMetadata targetMetadata, IComparerContext context) { if (IsUserCreatedIndex) { var command = new Command(); command .Append($"CREATE {(Unique ? "UNIQUE " : string.Empty)}{(Descending ? "DESCENDING" : "ASCENDING")} INDEX {IndexName.AsSqlIndentifier()}") .AppendLine() .Append($" ON {RelationName.AsSqlIndentifier()}"); if (ExpressionSource == null) { command.Append($"({string.Join(",", Segments.Select(s => s.FieldName.AsSqlIndentifier()))})"); } else { command.Append($" COMPUTED BY {ExpressionSource}"); } yield return(command); if (Inactive) { yield return(HandleIndexActiveInactive(context)); } } }
protected virtual Command OnCreateView(IMetadata sourceMetadata, IMetadata targetMetadata, IComparerContext context) { var command = new Command(); command.Append($"CREATE OR ALTER VIEW {RelationName.AsSqlIndentifier()} ("); command.AppendLine(); InsertColumns(command, sourceMetadata, targetMetadata, context, true); command.Append(")"); command.AppendLine(); command.Append("AS"); if (context.EmptyBodiesEnabled) { command.AppendLine(); var fields = string.Join(", ", Fields.Select(x => SqlHelper.GetDataType(x.Field, sourceMetadata.MetadataCharacterSets.CharacterSetsById, sourceMetadata.MetadataDatabase.Database.CharacterSet.CharacterSetId)).Select(RelationField.CreateShimFieldContent)); command.Append($"SELECT {fields} FROM RDB$DATABASE"); } else { if (!char.IsWhiteSpace(ViewSource[0])) { command.AppendLine(); } command.Append(ViewSource); } return(command); }
protected virtual Command OnCreateTable(IMetadata sourceMetadata, IMetadata targetMetadata, IComparerContext context) { var command = new Command(); var isGtt = MetadataRelationType == MetadataRelationType.GlobalTemporaryPreserve || MetadataRelationType == MetadataRelationType.GlobalTemporaryDelete; var isExternal = MetadataRelationType == MetadataRelationType.External; command.Append("CREATE "); if (isGtt) { command.Append("GLOBAL TEMPORARY "); } command.Append($"TABLE {RelationName.AsSqlIndentifier()} "); if (isExternal) { command.Append($"EXTERNAL '{SqlHelper.DoubleSingleQuotes(ExternalFile)}' "); } command.Append("("); command.AppendLine(); InsertColumns(command, sourceMetadata, targetMetadata, context, false); command.AppendLine(); command.Append(")"); if (isGtt) { command.AppendLine(); command.Append($"ON COMMIT {MetadataRelationType.ToDescription()} ROWS"); } if (SqlSecurity != null) { command.AppendLine(); command.Append($"SQL SECURITY {SqlHelper.SqlSecurityString(SqlSecurity)}"); } return(command); }
public void UpdateRelation(Relation relation) { Relation oldRelation = GetRelation(relation.Id); Relation mirrorRelation = GetRelation(x => x.SourceId == oldRelation.TargetId && x.TargetId == oldRelation.SourceId); RelationName relationName = context.RelationNames.Where(x => x.Id == relation.RelationNameId).FirstOrDefault(); RelationName mirror = context.RelationNames.Where(x => x.Name == relationName.MirrorName).FirstOrDefault(); using (var transaction = context.Database.BeginTransaction()) { try { oldRelation.RelationNameId = relation.RelationNameId; mirrorRelation.RelationNameId = mirror.Id; context.SaveChanges(); transaction.Commit(); } catch (Exception ex) { transaction.Rollback(); } } }
private string PrintTag(RelationName tag) { string output = ""; if (tag < 0) { tag = ~tag; output += "~"; } return output + tag.ToString(); }
[U] public void NotEq() { RelationName name = "foo"; RelationName[] notEqual = { "bar", "foo ", " foo ", "x", "", " ", typeof(Project) }; foreach (var t in notEqual) { (t != name).ShouldBeTrue(t); t.Should().NotBe(name); } }
/// <summary> /// Returns the hash code for this instance. /// </summary> /// <returns>The hash code for the current object.</returns> public override int GetHashCode() { int hashCode = RelationName.GetHashCode(); foreach (ArffAttribute attribute in Attributes) { hashCode = unchecked (hashCode * 31 + attribute.GetHashCode()); } return(hashCode); }
/// <summary> /// Returns the hash code for this instance. /// </summary> /// <returns>The hash code for the current object.</returns> public override int GetHashCode() { int hashCode = RelationName.GetHashCode(); foreach (Feature feature in Features) { hashCode = unchecked (hashCode * 31 + feature.GetHashCode()); } return(hashCode); }
[U] public void Eq() { RelationName name = "foo"; RelationName[] equal = { "foo" }; foreach (var t in equal) { (t == name).ShouldBeTrue(t); t.Should().Be(name); } }
protected override IEnumerable <Command> OnCreate(IMetadata sourceMetadata, IMetadata targetMetadata, IComparerContext context) { var command = SqlHelper.IsValidExternalEngine(this) ? new Command() : new PSqlCommand(); command .Append($"CREATE OR ALTER TRIGGER {TriggerName.AsSqlIndentifier()}") .AppendLine() .Append($"{(Inactive ? "INACTIVE" : "ACTIVE")} "); switch (TriggerClass) { case TriggerClassType.DML: case TriggerClassType.DDL: command.Append($"{TriggerBeforeAfter.ToDescription()} {TriggerEvents[0].ToDescription()} "); foreach (var item in TriggerEvents.Skip(1)) { command.Append($"OR {item.ToDescription()} "); } break; case TriggerClassType.DB: command.Append($"ON {TriggerEvents[0].ToDescription()} "); break; } command .Append($"POSITION {TriggerSequence}") .AppendLine(); if (TriggerClass == TriggerClassType.DML) { command .Append($"ON {RelationName.AsSqlIndentifier()}") .AppendLine(); } if (SqlHelper.IsValidExternalEngine(this)) { command.Append($"EXTERNAL NAME '{SqlHelper.DoubleSingleQuotes(EntryPoint)}'"); command.AppendLine(); command.Append($"ENGINE {EngineName.AsSqlIndentifier()}"); } else { if (SqlSecurity != null) { command.Append($"SQL SECURITY {SqlHelper.SqlSecurityString(SqlSecurity)}"); command.AppendLine(); } command.Append(TriggerSource); } yield return(command); }
protected override IEnumerable <Command> OnCreate(IMetadata sourceMetadata, IMetadata targetMetadata, IComparerContext context) { var command = new PSqlCommand(); command .Append($"CREATE OR ALTER TRIGGER {TriggerName.AsSqlIndentifier()}") .AppendLine() .Append($"{(Inactive ? "INACTIVE" : "ACTIVE")} "); if (TriggerClass == TriggerClassType.DB) { command.Append($"ON {TriggerAction1.ToDescription()} "); } else if (TriggerClass == TriggerClassType.DML) { var beforeOrAfter = TriggerAction1.In(new[] { TriggerActionType.BEFORE_INSERT, TriggerActionType.BEFORE_UPDATE, TriggerActionType.BEFORE_DELETE }) ? "BEFORE" : "AFTER"; command.Append($"{beforeOrAfter} {TriggerAction1.ToDescription()} "); if (TriggerAction2 != null) { command.Append($"OR {TriggerAction2.ToDescription()} "); } if (TriggerAction3 != null) { command.Append($"OR {TriggerAction3.ToDescription()} "); } } else { throw new ArgumentOutOfRangeException($"Unknown trigger class {TriggerClass}."); } command .Append($"POSITION {TriggerSequence}") .AppendLine(); if (TriggerClass == TriggerClassType.DML) { command .Append($"ON {RelationName.AsSqlIndentifier()}") .AppendLine(); } command.Append(TriggerSource); yield return(command); }
public Parent(RelationName name) => Name = name;
protected virtual IEnumerable <Command> OnAlterTable(IMetadata sourceMetadata, IMetadata targetMetadata, IComparerContext context) { var otherRelation = FindOtherChecked(targetMetadata.MetadataRelations.Relations, RelationName, "table"); var fieldNames = sourceMetadata .MetadataRelations .Relations[RelationName] .Fields .Select(f => f.FieldName); var otherFieldNames = targetMetadata .MetadataRelations .Relations[RelationName] .Fields .Select(f => f.FieldName); // 1. new fields var newFieldNames = new HashSet <Identifier>(fieldNames); newFieldNames.ExceptWith(otherFieldNames); var newFields = newFieldNames .Select(fieldName => sourceMetadata.MetadataRelations.RelationFields[new RelationFieldKey(RelationName, fieldName)]); foreach (var field in newFields) { foreach (var command in field.Create(sourceMetadata, targetMetadata, context)) { yield return(command); } context.CreatedRelationFields.Add(new RelationFieldKey(field.RelationName, field.FieldName)); } // 2. altered fields var alteredFieldNames = new HashSet <Identifier>(fieldNames); alteredFieldNames.IntersectWith(otherFieldNames); var alteredFields = alteredFieldNames .Select(fieldName => new { Field = sourceMetadata.MetadataRelations.RelationFields[new RelationFieldKey(RelationName, fieldName)], OtherField = targetMetadata.MetadataRelations.RelationFields[new RelationFieldKey(RelationName, fieldName)] }) .Where(x => x.Field != x.OtherField) .Select(x => x.Field); foreach (var command in alteredFields.SelectMany(newField => newField.Alter(sourceMetadata, targetMetadata, context))) { yield return(command); } // 3. dropped fields var droppedFieldNames = new HashSet <Identifier>(otherFieldNames); droppedFieldNames.ExceptWith(fieldNames); var droppedFields = droppedFieldNames .Select(fieldName => targetMetadata.MetadataRelations.RelationFields[new RelationFieldKey(RelationName, fieldName)]); foreach (var field in droppedFields) { context.DeferredColumnsToDrop.Add(new CommandGroup().Append(field.Drop(sourceMetadata, targetMetadata, context))); } // 3. SQL SECURITY if (SqlSecurity == null && otherRelation.SqlSecurity != null) { yield return(new Command().Append($"ALTER TABLE {RelationName.AsSqlIndentifier()} DROP SQL SECURITY")); } else if (SqlSecurity != null && otherRelation.SqlSecurity != null && SqlSecurity != otherRelation.SqlSecurity) { yield return(new Command().Append($"ALTER TABLE {RelationName.AsSqlIndentifier()} ALTER SQL SECURITY {SqlHelper.SqlSecurityString(SqlSecurity)}")); } }
protected override IEnumerable <Command> OnDrop(IMetadata sourceMetadata, IMetadata targetMetadata, IComparerContext context) { yield return(new Command() .Append($"DROP {(MetadataRelationType == MetadataRelationType.View ? "VIEW" : "TABLE")} {RelationName.AsSqlIndentifier()}")); }
public Child(RelationName name, Id parent) { Name = name; ParentId = parent; }
public string RelationName(RelationName type) => RelationNameResolver.Resolve(type);
internal static bool IsConditionless(this RelationName marker) => marker == null || marker.Name.IsNullOrEmpty() && marker.Type == null;
public static string PrettyPrint(RelationName state, bool value) { return (value == true) ? state.ToString() : ""; //switch (state) //{ // case RelationName.IsSittingOn: return value ? " Iso" : ""; // case RelationName.IsFriendOf: return value ? " Ifo" : ""; // case RelationName.IsAdjacentTo: return value ? " Iat" : ""; // case RelationName.IsGuarding: return value ? " Igd" : ""; // case RelationName.IsInZone: return value ? " Iir" : ""; // case RelationName.FrontZone: return value ? " Dfz" : ""; // case RelationName.RearZone: return value ? " Drz" : ""; // default: return value ? " Xxx" : ""; //} }
public static JoinField Link(RelationName child, Id parentId) => new Child(child, parentId);
[U] public void TypedEq() { RelationName t1 = typeof(Project), t2 = typeof(Project); (t1 == t2).ShouldBeTrue(t2); }
private static void StoreCache( RelationName[] relations, IList<Tuple<IHasState, IHasState>> objs) { if (cacheRelations == null) cacheRelations = new Dictionary< RelationName[], IList<Tuple<IHasState, IHasState>>>(); cacheRelations[relations] = objs; }
private static IList<Tuple<IHasState, IHasState>> CheckCache(RelationName[] states) { if (cacheRelations == null) return null; IList<Tuple<IHasState, IHasState>> results; if (cacheRelations.TryGetValue(states, out results) == false) return null; return results; }
protected override IEnumerable <Command> OnCreate(IMetadata sourceMetadata, IMetadata targetMetadata, IComparerContext context) { var command = new Command(); command.Append($"ALTER TABLE {RelationName.AsSqlIndentifier()}"); if (!SqlHelper.IsImplicitIntegrityConstraintName(ConstraintName)) { command.Append($" ADD CONSTRAINT {ConstraintName.AsSqlIndentifier()}"); } else { command.Append(" ADD"); } switch (RelationConstraintType) { case RelationConstraintType.Check: command.Append($" {Triggers[0].TriggerSource}"); break; case RelationConstraintType.NotNull: break; case RelationConstraintType.ForeignKey: case RelationConstraintType.PrimaryKey: case RelationConstraintType.Unique: { var fields = Index .Segments .OrderBy(s => s.FieldPosition) .Select(s => s.FieldName); command.Append($" {RelationConstraintType.ToDescription()} ({string.Join(", ", fields)})"); if (RelationConstraintType == RelationConstraintType.ForeignKey) { var referenceConstraint = sourceMetadata.MetadataConstraints.ReferenceConstraintsByName[ConstraintName]; var referenceRelationConstraint = referenceConstraint.RelationConstraintUq; var primaryKeyFields = referenceRelationConstraint .Index .Segments .OrderBy(s => s.FieldPosition) .Select(s => s.FieldName); command .AppendLine() .Append($" REFERENCES {referenceRelationConstraint.RelationName} ({string.Join(", ", primaryKeyFields)})"); if (referenceConstraint.UpdateRule != ConstraintRule.Restrict) { command .AppendLine() .Append($" ON UPDATE {referenceConstraint.UpdateRule.ToDescription()}"); } if (referenceConstraint.DeleteRule != ConstraintRule.Restrict) { command .AppendLine() .Append($" ON DELETE {referenceConstraint.DeleteRule.ToDescription()}"); } if (referenceConstraint.UpdateRule != ConstraintRule.Restrict || referenceConstraint.DeleteRule != ConstraintRule.Restrict) { command.AppendLine(); } } command.Append(AddConstraintUsingIndex(sourceMetadata, targetMetadata, context)); break; } default: throw new ArgumentOutOfRangeException($"Unknown relation constraint type: {RelationConstraintType}."); } yield return(command); }
[U] public void TypedNotEq() { RelationName t1 = typeof(Project), t2 = typeof(CommitActivity); (t1 != t2).ShouldBeTrue(t2); }
protected override IEnumerable <Command> OnCreate(IMetadata sourceMetadata, IMetadata targetMetadata, IComparerContext context) { yield return(new Command() .Append($"ALTER TABLE {RelationName.AsSqlIndentifier()} ADD {CreateDefinition(sourceMetadata, targetMetadata, context)}")); }
protected override IEnumerable <Command> OnDrop(IMetadata sourceMetadata, IMetadata targetMetadata, IComparerContext context) { yield return(new Command() .Append($"ALTER TABLE {RelationName.AsSqlIndentifier()} DROP {FieldName.AsSqlIndentifier()}")); }
public static JoinField Root(RelationName parent) => new Parent(parent);