示例#1
0
        /// <summary>
        /// Verifies is the obj equals to this.
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override bool Equals(object obj)
        {
            if (!typeof(IndexDbo).IsInstanceOfType(obj))
            {
                return(false);
            }

            IndexDbo other = obj as IndexDbo;
            bool     rzlt  = this.CompactLargeObjects == other.CompactLargeObjects && this.DisallowPageLocks == other.DisallowPageLocks &&
                             this.DisallowRowLocks == other.DisallowRowLocks && this.FillFactor == other.FillFactor &&
                             this.FilterDefinition == other.FilterDefinition && this.IgnoreDuplicateKeys == other.IgnoreDuplicateKeys &&
                             this.IndexedColumns.Count == other.IndexedColumns.Count &&
                             this.IndexedColumns.TrueForAll(x => other.IndexedColumns.Contains(x)) &&
                             this.IndexKeyType == other.IndexKeyType && this.IsClustered == other.IsClustered &&
                             this.IsDisabled == other.IsDisabled && this.IsUnique == other.IsUnique &&
                             this.Name == other.Name;

            return(rzlt);
        }
        /// <summary>
        /// Creates index or primary key.
        /// <param name="tableName">Table name</param>
        /// <param name="dbo">Index descriptor, <see cref="IndexDbo"/></param>
        /// <param name="uniqueInTable">If the value is not empty - look for equal names in this table only </param>
        /// </summary>
        public static IndexDbo CreateTheIndex(string tableName, IndexDbo dbo, string uniqueInTable = null)
        {
            var srv = GetConnectedServer(SqlServerName, UserName, Password);
            var db = srv.Databases[DatabaseName];
            Table aTable = db.Tables[tableName];
            if (aTable == null)
                throw new Exception(String.Format("There is no table {0} in the {1} database.", tableName, DatabaseName));

            return CreateTheIndex(aTable, dbo, uniqueInTable);
        }
        /// <summary>
        /// Modifies trhe index
        /// </summary>
        /// <param name="tableName">Table name</param>
        /// <param name="dbo">Index description, <see cref="IndexDbo"/></param>
        /// <param name="disableDependencies"><code>true</code> - drop dependencies</param>
        /// <param name="newDbo">New, updated index</param>
        /// <returns>List of dropped foreign keys</returns>
        private static List<DroppedDependencyDbo> UpdateTheIndex(string tableName, IndexDbo dbo, bool disableDependencies,
            out IndexDbo newDbo)
        {
            var srv = GetConnectedServer(SqlServerName, UserName, Password);
            var db = srv.Databases[DatabaseName];
            Table aTable = db.Tables[tableName];
            if (aTable == null)
                throw new Exception(String.Format("There is no table {0} in the {1} database.", tableName, DatabaseName));
            IndexDbo oldIndex = GetIndexDescription(aTable, dbo.Name);
            if (oldIndex == dbo)
                throw new Exception("There is nothing to change.");

            List<DroppedDependencyDbo> droppedDependencies =  DeleteTheIndex(db, aTable, dbo.Name, disableDependencies);
            newDbo = CreateTheIndex(aTable, dbo, aTable.Name);
            return droppedDependencies;
        }
        public static IndexDbo GetIndexDescription(Table table, string indexName)
        {
            Index ind = table.Indexes[indexName];
            if (ind == null)
                throw new Exception(String.Format("Table '{0}' has no index '{1}'.", table.Name, indexName)); ;

            var dbo = new IndexDbo()
            {
                CompactLargeObjects = ind.CompactLargeObjects,
                DisallowPageLocks = ind.DisallowPageLocks,
                DisallowRowLocks = ind.DisallowRowLocks,
                FillFactor = ind.FillFactor,
                FilterDefinition = ind.FilterDefinition,
                IgnoreDuplicateKeys = ind.IgnoreDuplicateKeys,
                IndexKeyType = ind.IndexKeyType.ToString(),
                IsClustered = ind.IsClustered,
                IsDisabled = ind.IsDisabled,
                IsUnique = ind.IsUnique,
                Name = ind.Name,
            };
            foreach (IndexedColumn clmn in ind.IndexedColumns)
                dbo.IndexedColumns.Add(clmn.Name);

            return dbo;
        }
        public static IndexDbo CreateTheIndex(Table aTable, IndexDbo dbo, string uniqueInTable)
        {
            string errMessage = IsIndexUnique(dbo.Name, uniqueInTable);
            if (!String.IsNullOrEmpty(errMessage))
                throw new Exception(errMessage);

            if (dbo.IsUnique && !AreValuesUnique(aTable, dbo.IndexedColumns.ToArray()))
                throw new Exception(String.Format("Table '{0}' contains duplicates in selected set of columns.", aTable.Name));

            // See CreateNewPrimaryKey in the Utilities_1
            CreateNewPrimaryKey(aTable, dbo);
            return GetIndexDescription(aTable, dbo.Name);
        }
        private void ShowFields(string indexName)
        {
            this.currentIndexDbo = CfcWebService.GetIndexDescription(this.currentTable, this.currentIndex);
            this.lstFieldList4.DataSource = this.currentIndexDbo.IndexedColumns;
            this.lstFieldList4.DataBind();

            this.chkCompactLargeObjects4.Checked = currentIndexDbo.CompactLargeObjects; // 1
            this.chkDisallowPageLocks4.Checked = currentIndexDbo.DisallowPageLocks;     // 2
            this.chkDisallowRowLocks4.Checked = currentIndexDbo.DisallowRowLocks;       // 3
            this.txtFillFactor4.Text = currentIndexDbo.FillFactor.ToString();           // 4
            this.txtFilterDefinition4.Text = currentIndexDbo.FilterDefinition;          // 5
            this.chkIgnoreDuplicateKeys4.Checked = currentIndexDbo.IgnoreDuplicateKeys; // 6

            string tmp = currentIndexDbo.IndexKeyType.ToString();
            int i = ddlIndexKeyType4.Items.Count - 1;
            while (i > 0)
            {
                if (String.Compare(ddlIndexKeyType4.Items[i].Value, tmp, true) == 0)
                    break;
                else
                    i--;
            }
            ddlIndexKeyType4.SelectedIndex = i;                                         // 7

            this.chkIsClustered4.Checked = currentIndexDbo.IsClustered;                 // 8
            this.chkIsDisabled4.Checked = currentIndexDbo.IsDisabled;                   // 9
            this.chkIsUnique4.Checked = currentIndexDbo.IsUnique;                       // 10
        }
        /// <summary>
        /// Recreates primary key or index.
        /// </summary>
        /// <param name="table">Current table <see cref="Table"/></param>
        /// <param name="descriptor">Index descriptor</param>
        private static void CreateNewPrimaryKey(Table table, IndexDbo descriptor)
        {
            var indexKeyType = (IndexKeyType)Enum.Parse(typeof(IndexKeyType), descriptor.IndexKeyType);
            byte fillFactor = (byte)(descriptor.FillFactor ?? 0);

            var primaryKeyIndex = new Index(table, descriptor.Name)
            {
                CompactLargeObjects = descriptor.CompactLargeObjects,
                FillFactor = fillFactor > 0 ? fillFactor : (byte)50,
                FilterDefinition = descriptor.FilterDefinition,
                IgnoreDuplicateKeys = descriptor.IgnoreDuplicateKeys,
                IndexKeyType = indexKeyType,
                IsClustered = descriptor.IsClustered,
                IsUnique = descriptor.IsUnique,
            };
            foreach (string columnName in descriptor.IndexedColumns)
                primaryKeyIndex.IndexedColumns.Add(new IndexedColumn(primaryKeyIndex, columnName));
            primaryKeyIndex.Create();
            primaryKeyIndex.DisallowPageLocks = descriptor.DisallowPageLocks;
            primaryKeyIndex.DisallowRowLocks = descriptor.DisallowRowLocks;
            primaryKeyIndex.Alter();
            //if (descriptor.IsDisabled)
            //{
            //    primaryKeyIndex.Disable();
            //    table.Alter();
            //}
        }