public void CopyReferenceDefinitions()
        {
            IReferenceDefinition sourceReference = new ReferenceDefinition("FK_Foo",
                "Foo", "Bar");
            sourceReference.FkTableSchema = "dbo";
            sourceReference.FkColumns.Add("ID");
            sourceReference.FkColumns.Add("IDx");

            sourceReference.FkTableSchema = "db";
            sourceReference.FkColumns.Add("FooID");
            sourceReference.FkColumns.Add("FooIDx");

            IReferenceDefinition targetReference = new ReferenceDefinition();

            SemanticModelUtil.Copy(sourceReference, targetReference);

            Assert.AreEqual(sourceReference.FkTable, targetReference.FkTable);
            Assert.AreEqual(sourceReference.FkTableSchema, targetReference.FkTableSchema);
            Assert.AreEqual(sourceReference.Name, targetReference.Name);
            Assert.AreEqual(sourceReference.PkTable, targetReference.PkTable);
            Assert.AreEqual(sourceReference.PkTableSchema, targetReference.PkTableSchema);

            CollectionAssert.AreEqual(sourceReference.PkColumns, targetReference.PkColumns);
            CollectionAssert.AreEqual(sourceReference.FkColumns, targetReference.FkColumns);
        }
        public void TestFixtureSetUp()
        {
            schema = new Schema();

            TableDefinition barTable = new TableDefinition("Bar");
            barTable.AddColumn(new ColumnDefinition("ID", "Bar", DbType.Int32, false, null, null, null, true, false));
            barTable.AddColumn(new ColumnDefinition("Name", DbType.String, false, 1000, null, null));
            barTable.AddColumn(new ColumnDefinition("ParentID", DbType.Int32, true, null, null, null));

            uqNameIndex = new IndexDefinition("UQ_Name", new IndexColumnDefinition("Name", SortDirection.Ascending));
            uqNameIndex.Unique = true;
            uqNameIndex.Clustered = true;
            barTable.AddIndex(uqNameIndex);

            ReferenceDefinition fkBarReference = new ReferenceDefinition("FK_Bar", "Bar", "Bar");
            fkBarReference.PkColumns.Add("ID");
            fkBarReference.FkColumns.Add("ParentID");

            schema.AddTable(barTable);
        }
        public void RemoveReference()
        {
            TableDefinition table = new TableDefinition("Foo");
            ReferenceDefinition reference = new ReferenceDefinition("IX_Bar");

            table.AddReference(reference);

            table.RemoveReference("XX_Bar");
            Assert.AreEqual(1, table.References.Count);

            table.RemoveReference("ix_Bar");
            Assert.AreEqual(0, table.References.Count);
        }
        public void GetReference()
        {
            TableDefinition table = new TableDefinition("Foo");
            ReferenceDefinition reference = new ReferenceDefinition("IX_Bar");

            table.AddReference(reference);

            Assert.AreSame(reference, table.GetReference("ix_bar"));
            Assert.AreSame(reference, table.GetReference("ix_bAR"));
            Assert.IsNull(table.GetReference("XX_Bar"));
        }
        private static IReferenceDefinition GetReferenceDefinition(Table table, ForeignKey foreignKey)
        {
            IReferenceDefinition referenceDefinition = new ReferenceDefinition(foreignKey.Name);
            referenceDefinition.PkTableSchema = foreignKey.ReferencedTableSchema;
            referenceDefinition.PkTable = foreignKey.ReferencedTable;
            referenceDefinition.FkTableSchema = table.Schema;
            referenceDefinition.FkTable = table.Name;

            foreach (ForeignKeyColumn foreignKeyColumn in foreignKey.Columns)
            {
                referenceDefinition.FkColumns.Add(foreignKeyColumn.Name);
                referenceDefinition.PkColumns.Add(foreignKeyColumn.ReferencedColumn);
            } // foreach

            return referenceDefinition;
        }
        /// <summary>
        /// Visits the given <paramref name="addReferenceNode"/>.
        /// </summary>
        /// <param name="addReferenceNode"></param>
        public override void Visit(IAddReferenceNode addReferenceNode)
        {
            ReferenceDefinition reference = new ReferenceDefinition(addReferenceNode.Name);

            BindForeignKeyTable(addReferenceNode, reference);
            BindForeignKeyColumns(addReferenceNode, reference);

            BindPrimaryKeyTable(addReferenceNode, reference);
            BindPrimaryKeyColumns(addReferenceNode, reference);

            if(addReferenceNode.Properties["on-update"] != null)
            {
                var onUpdate = AstNodePropertyUtil.AsString(addReferenceNode.Properties["on-update"].Value);
                addReferenceNode.OnUpdate = (ReferenceCascadeAction)Enum.Parse(
                    typeof(ReferenceCascadeAction),
                    onUpdate,
                    true);
            } // if

            if(addReferenceNode.Properties["on-delete"] != null)
            {
                var onDelete = AstNodePropertyUtil.AsString(addReferenceNode.Properties["on-delete"].Value);
                addReferenceNode.OnDelete = (ReferenceCascadeAction)Enum.Parse(
                    typeof(ReferenceCascadeAction),
                    onDelete,
                    true);
            } // if

            ITableDefinition table = Environment.Schema.GetTable(reference.FkTable);
            table.AddReference(reference);
        }