protected override void WriteDeleteRule(ForeignKeyRule deleteRule)
 {
     if (deleteRule != ForeignKeyRule.Cascade)
     {
         return;
     }
     Write(" ON DELETE CASCADE");
 }
Beispiel #2
0
 public ForeignKeyConstraint(Db2SourceContext context, string owner, string schema, string name, string tableSchema, string tableName, string refSchema, string refConstraint, ForeignKeyRule updateRule, ForeignKeyRule deleteRule, bool isNoName, bool deferrable, bool deferred)
     : base(context, owner, schema, name, tableSchema, tableName, isNoName, deferrable, deferred)
 {
     ReferenceSchemaName     = refSchema;
     ReferenceConstraintName = refConstraint;
     UpdateRule = updateRule;
     DeleteRule = deleteRule;
 }
 internal ForeignKeyConstraint(ObjectName name, Table table, ConstraintType constraintType, IEnumerable<Column> keyColumns,
     TableConstraint uniqueConstraint, ForeignKeyRule updateRule, ForeignKeyRule deleteRule, bool? isDisabled)
     : base(name, table, constraintType, keyColumns)
 {
     _uniqueConstraint = uniqueConstraint;
     _updateRule = updateRule;
     _deleteRule = deleteRule;
     _isDisabled = isDisabled;
 }
Beispiel #4
0
        public ForeignKey(Table childTable, string[] childFieldNames, Table parentTable, string[] parentFieldNames, ForeignKeyRule foreignKeyRule)
        {
            this.childTable      = childTable;
            this.childFieldNames = childFieldNames;

            this.parentTable      = parentTable;
            this.parentFieldNames = parentFieldNames;

            this.foreignKeyRule = foreignKeyRule;
        }
Beispiel #5
0
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (!(value is ForeignKeyRule))
            {
                return(null);
            }
            ForeignKeyRule rule = (ForeignKeyRule)value;

            return(_ruleToText[rule]);
        }
        private async Task<Document> AddRelationshipAsync(IPropertySymbol dbTable, string name, IPropertySymbol foreignKey, IPropertySymbol refTable,
            string description, ForeignKeyRule deleteRule, ForeignKeyRule updateRule, CancellationToken ct = default(CancellationToken))
        {
            var editor = await DocumentEditor.CreateAsync(Document, ct);
            var g = editor.Generator;
            var declarationAttributeType = Compilation.GetKnownType(KnownTypes.RelationshipAttribute);
            var ruleType = deleteRule != ForeignKeyRule.None || updateRule != ForeignKeyRule.None ? Compilation.GetKnownType(KnownTypes.ForeignKeyRule) : null;
            var implementationAttributeType = Compilation.GetKnownType(KnownTypes._RelationshipAttribute);
            var modelType = dbTable.GetModelType();
            var keyMappingType = Compilation.GetKnownType(KnownTypes.KeyMapping);
            var imports = new NamespaceSet
            {
                declarationAttributeType,
                implementationAttributeType,
                modelType,
                keyMappingType
            };
            if (ruleType != null)
                imports.Add(ruleType);

            var paramName = ModelParamName;
            var methodBody = GenerateImplementationMethodBody();
            var method = g.MethodDeclaration(name, new SyntaxNode[] { g.ParameterDeclaration(paramName, g.IdentifierName(modelType.Name)) }, null,
                g.IdentifierName(keyMappingType.Name), Accessibility.Private, default(DeclarationModifiers), statements: new SyntaxNode[] { methodBody });
            method = GenerateAttribute(method, implementationAttributeType.Name.ToAttributeName()).WithAdditionalAnnotations(Formatter.Annotation);

            editor.AddMember(DbClass, method);

            var argument = g.NameOfExpression(g.IdentifierName(name));
            var arguments = g.AttributeArgument(argument).Concat(GenerateAdditionalArguments()).ToArray();
            var dbTableNode = GetSyntaxNode(dbTable);
            editor.ReplaceNode(dbTableNode, GenerateAttribute(dbTableNode, declarationAttributeType.Name.ToAttributeName(), GetLeadingWhitespaceTrivia(dbTableNode), arguments));

            await AddMissingNamespaces(editor, imports, ct);

            return await editor.FormatAsync(ct);

            IEnumerable<SyntaxNode> GenerateAdditionalArguments()
            {
                if (!string.IsNullOrWhiteSpace(description))
                    yield return g.AttributeArgument("Description", g.LiteralExpression(description));
                if (deleteRule != ForeignKeyRule.None)
                    yield return g.AttributeArgument("DeleteRule", g.DottedName(string.Format("{0}.{1}", ruleType.Name, deleteRule)));
                if (updateRule != ForeignKeyRule.None)
                    yield return g.AttributeArgument("UpdateRule", g.DottedName(string.Format("{0}.{1}", ruleType.Name, updateRule)));
            }

            SyntaxNode GenerateImplementationMethodBody()
            {
                var fkExpr = g.DottedName(string.Format("{0}.{1}", paramName, foreignKey.Name));
                var joinParam = dbTable == refTable ? g.IdentifierName(paramName) : GenerateJoinParam(g, refTable);
                return g.ReturnStatement(g.InvocationExpression(g.MemberAccessExpression(fkExpr, "Join"), joinParam));
            }
        }
Beispiel #7
0
        internal DbForeignKeyConstraint(string name, string description, CandidateKey foreignKey, CandidateKey referencedKey, ForeignKeyRule deleteRule, ForeignKeyRule updateRule)
            : base(name, description)
        {
            Debug.Assert(foreignKey != null);
            Debug.Assert(referencedKey != null);
            Debug.Assert(foreignKey.GetType() == referencedKey.GetType());

            ForeignKey    = foreignKey;
            ReferencedKey = referencedKey;
            DeleteRule    = deleteRule;
            UpdateRule    = updateRule;
        }
Beispiel #8
0
        public ForeignKey(Table table, string name, IEnumerable <string> columnNames,
                          string relatedTableName, string[] relatedColumnNames,
                          ForeignKeyRule updateRule, ForeignKeyRule deleteRule)
            : base(table, name, columnNames)
        {
            RelatedTableName   = relatedTableName;
            RelatedColumnNames = relatedColumnNames;
            UpdateRule         = updateRule;
            DeleteRule         = deleteRule;

            foreach (Column column in Columns)
            {
                column.SetAttribute(ColumnAttribute.FKColumn);
            }
        }
Beispiel #9
0
        private static string GetRuleText(ForeignKeyRule rule)
        {
            switch (rule)
            {
            case ForeignKeyRule.Cascade:
                return("CASCADE");

            case ForeignKeyRule.SetDefault:
                return("SET DEFAULT");

            case ForeignKeyRule.SetNull:
                return("SET NULL");

            default:
                return(string.Empty);
            }
        }
Beispiel #10
0
 private static void GenerateForeignKeyRule(this ForeignKeyRule rule, IndentedStringBuilder sqlBuilder, string changeType)
 {
     Debug.Assert(changeType == "DELETE" || changeType == "UPDATE");
     sqlBuilder.Append("ON ");
     sqlBuilder.Append(changeType);
     if (rule == ForeignKeyRule.None)
     {
         sqlBuilder.Append(" NO ACTION");
     }
     else if (rule == ForeignKeyRule.Cascade)
     {
         sqlBuilder.Append(" CASCADE");
     }
     else if (rule == ForeignKeyRule.SetNull)
     {
         sqlBuilder.Append(" SET NULL");
     }
     else
     {
         sqlBuilder.Append(" SET DEFAULT");
     }
 }
Beispiel #11
0
        protected virtual string GetForeignKeyRuleText(ForeignKeyRule rule)
        {
            switch (rule)
            {
            case ForeignKeyRule.None:
                return("NO ACTION");

            case ForeignKeyRule.Restrict:
                return("RESTRICT");

            case ForeignKeyRule.Cascade:
                return("CASCADE");

            case ForeignKeyRule.SetDefault:
                return("SET DEFAULT");

            case ForeignKeyRule.SetNull:
                return("SET NULL");

            default:
                return(string.Empty);
            }
        }
 private static void CheckForeignKey(ForeignKeySchema foreignKey, string foreignKeyName, ForeignKeyRule rule)
 {
     foreignKey.Name.Should().Be(foreignKeyName, "Meno cudzieho kľúča musí byť správne.");
     foreignKey.PrimaryKeyTableName.Should().Be("ParentTable", "Meno tabuľky s primárnym kľúčom musí byť správne.");
     foreignKey.PrimaryKeyTableColumns.Should().Equal(new string[] { "Id" });
     foreignKey.ForeignKeyTableColumns.Should().Equal(new string[] { "ParentId" });
     foreignKey.DeleteRule.Should().Be(rule, "Pravidlo pri vymazaní (DELETE RULE) musí byť správne.");
     foreignKey.UpdateRule.Should().Be(rule, "Pravidlo pri vymazaní (UPDATE RULE) musí byť správne.");
 }
 protected override void WriteUpdateRule(ForeignKeyRule updateRule)
 {
 }
Beispiel #14
0
        private void AddRelationship(string name, IPropertySymbol foreignKey, IPropertySymbol refTable, string description, ForeignKeyRule deleteRule, ForeignKeyRule updateRule)
        {
            var document = CurrentNode.AddRelationship(name, foreignKey, refTable, description, deleteRule, updateRule);

            TryApplyChanges(document);
            NavigatableMarker = DbMapper.CreateTableMarker(CurrentNode.Name);
        }
            private static DbForeignKeyConstraint CreateForeignKeyConstraint <TKey>(string name, string description, TKey foreignKey, Model <TKey> refTableModel, ForeignKeyRule deleteRule, ForeignKeyRule updateRule)
                where TKey : CandidateKey
            {
                Debug.Assert(foreignKey != null);
                Debug.Assert(refTableModel != null);

                var model = foreignKey.ParentModel;
                var foreignKeyConstraint = new DbForeignKeyConstraint(name, description, foreignKey, refTableModel.PrimaryKey, deleteRule, updateRule);

                Debug.Assert(!(refTableModel != model && string.IsNullOrEmpty(foreignKeyConstraint.ReferencedTableName)));
                return(foreignKeyConstraint);
            }
Beispiel #16
0
 protected virtual void WriteDeleteRule(ForeignKeyRule deleteRule)
 {
     Write(" ON DELETE " + GetForeignKeyRuleText(deleteRule));
 }
Beispiel #17
0
 protected virtual void WriteUpdateRule(ForeignKeyRule updateRule)
 {
     Write(" ON UPDATE " + GetForeignKeyRuleText(updateRule));
 }
Beispiel #18
0
 public virtual Document AddRelationship(string name, IPropertySymbol foreignKey, IPropertySymbol refTable,
                                         string description, ForeignKeyRule deleteRule, ForeignKeyRule updateRule)
 {
     return(Mapper.Document);
 }
Beispiel #19
0
 internal Document AddRelationship(IPropertySymbol dbTable, string name, IPropertySymbol foreignKey, IPropertySymbol refTable,
     string description, ForeignKeyRule deleteRule, ForeignKeyRule updateRule)
 {
     return AddRelationshipAsync(dbTable, name, foreignKey, refTable, description, deleteRule, updateRule).Result;
 }
Beispiel #20
0
 public override Document AddRelationship(string name, IPropertySymbol foreignKey, IPropertySymbol refTable, string description, ForeignKeyRule deleteRule, ForeignKeyRule updateRule)
 {
     Debug.Assert(CanAddRelationship);
     return Mapper.AddRelationship(_table, name, foreignKey, refTable, description, deleteRule, updateRule);
 }