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 CopyIndexDefinitions()
        {
            IIndexDefinition sourceIndex = new IndexDefinition("IX_Foo",
                new IndexColumnDefinition("Foo", SortDirection.Ascending),
                new IndexColumnDefinition("ID", SortDirection.Descending));
            sourceIndex.Clustered = true;
            sourceIndex.Unique = true;

            IIndexDefinition targetIndex = new IndexDefinition();

            SemanticModelUtil.Copy(sourceIndex, targetIndex);

            Assert.AreEqual(sourceIndex.Clustered, targetIndex.Clustered);
            Assert.AreEqual(sourceIndex.Name, targetIndex.Name);
            Assert.AreEqual(sourceIndex.Table, targetIndex.Table);
            Assert.AreEqual(sourceIndex.Unique, targetIndex.Unique);

            for(int i = 0; i < sourceIndex.Columns.Count; ++i)
            {
                Assert.AreEqual(sourceIndex.Columns[i].Name, targetIndex.Columns[i].Name);
                Assert.AreEqual(sourceIndex.Columns[i].SortDirection, targetIndex.Columns[i].SortDirection);
            } // for
        }
        public void RemoveIndex()
        {
            TableDefinition table = new TableDefinition("Foo");
            IndexDefinition index = new IndexDefinition("IX_Bar");

            table.AddIndex(index);

            table.RemoveIndex("XX_Bar");
            Assert.AreEqual(1, table.Indexes.Count);

            table.RemoveIndex("ix_Bar");
            Assert.AreEqual(0, table.Indexes.Count);
        }
        public void GetIndex()
        {
            TableDefinition table = new TableDefinition("Foo");
            IndexDefinition index = new IndexDefinition("IX_Bar");

            table.AddIndex(index);

            Assert.AreSame(index, table.GetIndex("ix_bar"));
            Assert.AreSame(index, table.GetIndex("ix_bAR"));
            Assert.IsNull(table.GetIndex("XX_Bar"));
        }
        private static IIndexDefinition GetIndexDefinition(Table table, Index index)
        {
            IIndexDefinition indexDefinition = new IndexDefinition(index.Name);
            indexDefinition.Clustered = index.IsClustered;
            indexDefinition.Unique = index.IsUnique;

            foreach (IndexedColumn indexedColumn in index.IndexedColumns)
            {
                indexDefinition.Columns.Add(new IndexColumnDefinition(indexedColumn.Name,
                    indexedColumn.Descending ?
                        SortDirection.Descending :
                        SortDirection.Ascending));
            } // foreach

            return indexDefinition;
        }
        /// <summary>
        /// Visits the given <paramref name="addIndexNode"/>.
        /// </summary>
        /// <param name="addIndexNode"></param>
        public override void Visit(IAddIndexNode addIndexNode)
        {
            IndexDefinition indexDefinition = new IndexDefinition(addIndexNode.Name);

            //
            // If we have a "table" attribute, use it as a table name.
            if(addIndexNode.Properties[MdlSyntax.Table] != null)
            {
                indexDefinition.Table = addIndexNode.Table =
                    AstNodePropertyUtil.AsString(addIndexNode.Properties[MdlSyntax.Table].Value);
            } // if
            else if(addIndexNode.Parent is IAddColumnNode || addIndexNode.Parent is IAlterColumnNode)
            {
                indexDefinition.Table = addIndexNode.Table =
                    ((ITableNode)addIndexNode.Parent.Parent).Name;
            } // else if
            else if(addIndexNode.Parent is IAddTableNode || addIndexNode.Parent is IAlterTableNode)
            {
                indexDefinition.Table = addIndexNode.Table =
                    ((ITableNode)addIndexNode.Parent).Name;
            } // else if
            else
                throw CreateMdlCompilerException(MdlCompilerResources.CouldNotResolveTableForAddIndex,
                    addIndexNode.Name);

            ITableDefinition table = Environment.Schema.GetTable(indexDefinition.Table);
            table.AddIndex(indexDefinition);

            //
            // If our parent is IAddColumn or IAlterColumn, use it as indexed column
            if(addIndexNode.Parent is IAddColumnNode || addIndexNode.Parent is IAlterColumnNode)
            {
                string indexColumnName = ((IColumnNode)addIndexNode.Parent).Name;
                IndexColumnDefinition indexColumnDefinition = new IndexColumnDefinition(indexColumnName);

                indexDefinition.Columns.Add(indexColumnDefinition);
                addIndexNode.Columns.Add(indexColumnDefinition);
            } // if
            else if(addIndexNode.Properties[MdlSyntax.Column] != null)
            {
                IndexColumnDefinition indexColumnDefinition =
                    GetIndexColumnDefinition(addIndexNode.Properties[MdlSyntax.Column].Value);
                indexDefinition.Columns.Add(indexColumnDefinition);
                addIndexNode.Columns.Add(indexColumnDefinition);
            } // else if
            else if(addIndexNode.Properties[MdlSyntax.Columns] != null)
            {
                IListAstNodePropertyValue list = (IListAstNodePropertyValue)addIndexNode.Properties[MdlSyntax.Columns].Value;
                foreach(IAstNodePropertyValue value in list.Items)
                {
                    IndexColumnDefinition indexColumnDefinition = GetIndexColumnDefinition(value);
                    indexDefinition.Columns.Add(indexColumnDefinition);
                    addIndexNode.Columns.Add(indexColumnDefinition);
                } // foreach
            } // else if

            BindIndexProperties(addIndexNode, indexDefinition);
        }