Esempio n. 1
0
        private static List<IndexSchema> GetIndexes(SqlConnection con, TableSchema eTable)
        {
            var indexes = new List<IndexSchema>();
            var aHash = new Dictionary<string, bool>();
            string[] restrict4 = {null, null, null, null};

            // Indexes
            restrict4[0] = null;
            restrict4[1] = null;
            restrict4[2] = eTable.Name;
            restrict4[3] = null;
            var dtShema = SqlSchemaFactory.GetSchema(con, "Indexes", restrict4);
            for (var i = 0; i < dtShema.Rows.Count; i++)
            {
                var row = dtShema.Rows[i];
                if (Convert.ToBoolean(row["IS_STATISTICS"], CultureInfo.InvariantCulture) ||
                    Convert.ToBoolean(row["IS_AUTOSTATISTICS"], CultureInfo.InvariantCulture) ||
                    Convert.ToBoolean(row["IS_HYPOTTETICAL"], CultureInfo.InvariantCulture))
                    continue;

                var cName = row["INDEX_NAME"].ToString();
                if (eTable.IsKeyExist(cName, ConstraintType.Unique) ||
                    eTable.IsKeyExist(cName, ConstraintType.KeyPrimary) ||
                    eTable.IsKeyExist(cName, ConstraintType.KeyForeign))
                    continue;

                if (aHash.ContainsKey(cName))
                    continue;
                var eIndex = new IndexSchema();
                aHash.Add(cName, true);
                eIndex.Name = cName;
                eIndex.Unique = Convert.ToBoolean(row["IS_UNIQUE"], CultureInfo.InvariantCulture);
                eIndex.Clustered = Convert.ToBoolean(row["IS_CLUSTERED"], CultureInfo.InvariantCulture);
                //eIndex.isActive = !Convert.ToBoolean(cRow["IS_INACTIVE"], CultureInfo.InvariantCulture);

                var dtv = dtShema.DefaultView;
                dtv.RowFilter = string.Format("INDEX_NAME = '{0}'", cName);
                dtv.Sort = "COLUMN_ORDINAL_POSITION ASC";

                var columns = "";
                for (var y = 0; y < dtv.Count; y++)
                    columns += (dtv[y]["COLUMN_NAME"] +
                                (Convert.ToBoolean(dtv[y]["IS_DESCENDING"], CultureInfo.InvariantCulture) ? " DESC" : "") +
                                ", ");
                columns = columns.Remove(columns.Length - 2, 2);
                eIndex.Columns = columns;
                indexes.Add(eIndex);
            }
            return indexes;
        }