Пример #1
0
        /// <summary>
        /// Залогировать информацию обо всех индексах
        /// </summary>
        /// <param name="modelIndex">Информация об индексе сущности</param>
        private void LogEntityIndexeInfo(IndexInfo modelIndex)
        {
            _logger.Log(LogLevel.Info, "Индекс: {0} IsClustered: {1} IsUnique: {2}",
                        modelIndex.Name, modelIndex.IsClustered, modelIndex.IsUnique);

            var indexColumnBuilder = new StringBuilder();

            if (modelIndex.IndexMemberInfoCollection.Any())
            {
                indexColumnBuilder.Append("ON [");
                foreach (var memberInfo in modelIndex.IndexMemberInfoCollection)
                {
                    indexColumnBuilder.Append(string.Format("{0} {1} {2}",
                                                            memberInfo.ColumnInfo.Name, memberInfo.ColumnOrder, memberInfo.SortDirection == SortDirection.Asc ? "ASC" : "DESC"));

                    if (!memberInfo.Equals(modelIndex.IndexMemberInfoCollection.Last()))
                    {
                        indexColumnBuilder.Append(",");
                    }
                }
                indexColumnBuilder.Append("]");
            }

            if (modelIndex.IndexIncludeInfoCollection.Any())
            {
                indexColumnBuilder.Append(" INCLUDE [");
                foreach (var includeInfo in modelIndex.IndexIncludeInfoCollection)
                {
                    indexColumnBuilder.Append(string.Format("{0} {1}", includeInfo.ColumnInfo.Name, includeInfo.ColumnOrder));

                    if (!includeInfo.Equals(modelIndex.IndexIncludeInfoCollection.Last()))
                    {
                        indexColumnBuilder.Append(",");
                    }
                }
                indexColumnBuilder.Append("]");
            }

            _logger.Log(LogLevel.Info, indexColumnBuilder.ToString());
        }
Пример #2
0
        /// <summary>
        /// Залогировать информацию обо всех индексах
        /// </summary>
        /// <param name="modelIndex">Информация об индексе сущности</param>
        private void LogEntityIndexeInfo(IndexInfo modelIndex)
        {
            _logger.Log(LogLevel.Info, "Индекс: {0} IsClustered: {1} IsUnique: {2}",
                                          modelIndex.Name, modelIndex.IsClustered, modelIndex.IsUnique);

            var indexColumnBuilder = new StringBuilder();
            if (modelIndex.IndexMemberInfoCollection.Any())
            {
                indexColumnBuilder.Append("ON [");
                foreach (var memberInfo in modelIndex.IndexMemberInfoCollection)
                {
                    indexColumnBuilder.Append(string.Format("{0} {1} {2}",
                        memberInfo.ColumnInfo.Name, memberInfo.ColumnOrder, memberInfo.SortDirection == SortDirection.Asc ? "ASC" : "DESC"));

                    if (!memberInfo.Equals(modelIndex.IndexMemberInfoCollection.Last()))
                        indexColumnBuilder.Append(",");
                }
                indexColumnBuilder.Append("]");
            }

            if (modelIndex.IndexIncludeInfoCollection.Any())
            {
                indexColumnBuilder.Append(" INCLUDE [");
                foreach (var includeInfo in modelIndex.IndexIncludeInfoCollection)
                {
                    indexColumnBuilder.Append(string.Format("{0} {1}", includeInfo.ColumnInfo.Name, includeInfo.ColumnOrder));

                    if (!includeInfo.Equals(modelIndex.IndexIncludeInfoCollection.Last()))
                        indexColumnBuilder.Append(",");
                }
                indexColumnBuilder.Append("]");
            }

            _logger.Log(LogLevel.Info, indexColumnBuilder.ToString());
        }
Пример #3
0
        /// <summary>
        /// Получить информацию об индексах
        /// </summary>
        /// <param name="storageIndexItemInfoCollection">Информация об индексах из хранилища</param>
        /// <param name="tableInfo">Информация о таблице</param>
        /// <returns>Информацию об индексах</returns>
        private List<IndexInfo> GetStorageIndexInfo(IEnumerable<StorageIndexItem> storageIndexItemInfoCollection, TableInfo tableInfo)
        {
            var indexes = new List<IndexInfo>();

            //группируем по таблицам
            List<StorageIndexItem> tableIndexGroupInfo = storageIndexItemInfoCollection.Where(i => i.TableName == tableInfo.Name).ToList();

            //группируем по именам индексов
            var indexNameGroups = tableIndexGroupInfo.GroupBy(i => i.IndexName).ToList();

            foreach (var indexNameGroup in indexNameGroups)
            {
                var isClustered = false;
                var isUnique = false;

                var entityIndexInfo = new IndexInfo() { Name = indexNameGroup.Key, TableInfo = tableInfo };

                var indexMemberItems = indexNameGroup.Where(i => i.IsIncludedColumn == false).ToList();
                var indexIncludeItems = indexNameGroup.Where(i => i.IsIncludedColumn == true).ToList();

                indexMemberItems.Sort(CompareStorageIndexItemOrder);
                indexIncludeItems.Sort(CompareStorageIndexItemOrder);

                int memberColumnOrder = 0;
                int includeColumnOrder = 0;
                foreach (var indexMemberItem in indexMemberItems)
                {
                    isClustered = indexMemberItem.IndexType.Equals("CLUSTERED");
                    isUnique = indexMemberItem.IsUnique;

                    var indexMemberInfo = new IndexMemberInfo()
                    {
                        ColumnInfo = new ColumnInfo(tableInfo, indexMemberItem.ColumnName),
                        IndexName = indexMemberItem.IndexName,
                        ColumnOrder = memberColumnOrder,
                        SortDirection = indexMemberItem.IsDescending == true ? SortDirection.Desc : SortDirection.Asc
                    };
                    entityIndexInfo.IndexMemberInfoCollection.Add(indexMemberInfo);

                    memberColumnOrder += 1;
                }

                foreach (var indexIncludeItem in indexIncludeItems)
                {
                    isClustered = indexIncludeItem.IndexType.Equals("CLUSTERED");
                    isUnique = indexIncludeItem.IsUnique;

                    var indexMemberInfo = new IndexIncludeInfo()
                    {
                        ColumnInfo = new ColumnInfo(tableInfo, indexIncludeItem.ColumnName),
                        IndexName = indexIncludeItem.IndexName,
                        ColumnOrder = includeColumnOrder,
                    };
                    entityIndexInfo.IndexIncludeInfoCollection.Add(indexMemberInfo);

                    includeColumnOrder += 1;
                }
                entityIndexInfo.IsClustered = isClustered;
                entityIndexInfo.IsUnique = isUnique;

                indexes.Add(entityIndexInfo);
            }

            return indexes;
        }
Пример #4
0
        /// <summary>
        /// Получить индексы для сущности
        /// </summary>
        /// <param name="entity">Сущность</param>
        /// <param name="entityMapping">Mapping сущности</param>
        /// <exception cref="BadIndexColumnOrderException">Ошибка упарядочивания столбцов индекса</exception>
        /// <returns>Коллекия индексов</returns>
        private List<IndexInfo> GetIndexInfoCollection(Type entity, EntityMapping entityMapping)
        {
            var indexes = new List<IndexInfo>();
            var indexMemberInfoCollection = new List<IndexMemberInfo>();
            var indexIncludeInfoCollection = new List<IndexIncludeInfo>();

            var propertyMappings = entityMapping.PropertyMappings;
            var properties = entity.GetProperties();
            var indexAttributes = entity.GetCustomAttributes(typeof (IndexAttribute), inherit: false) as IEnumerable<IndexAttribute>;

            foreach (var propertyMapping in propertyMappings)
            {
                var property = properties.First(p => p.Name == propertyMapping.Property.Name);
                var indexMemberAttributes = property.GetCustomAttributes(typeof(IndexMemberAttribute), inherit: false) as IEnumerable<IndexMemberAttribute>;
                foreach (var indexMemberAttribute in indexMemberAttributes)
                {
                    indexMemberInfoCollection.Add(new IndexMemberInfo()
                    {
                        ColumnInfo = new ColumnInfo(entityMapping.TableInfo, propertyMapping.ColumnName),
                        IndexName = indexMemberAttribute.IndexName,
                        ColumnOrder = indexMemberAttribute.MemberOrder,
                        SortDirection = indexMemberAttribute.SortDirection
                    });
                }

                var indexIncludeAttributes = property.GetCustomAttributes(typeof(IndexIncludeAttribute), inherit: false) as IEnumerable<IndexIncludeAttribute>;
                foreach (var indexIncludeAttribute in indexIncludeAttributes)
                {
                    indexIncludeInfoCollection.Add(new IndexIncludeInfo()
                    {
                        ColumnInfo = new ColumnInfo(entityMapping.TableInfo, propertyMapping.ColumnName),
                        IndexName = indexIncludeAttribute.IndexName,
                        ColumnOrder = indexIncludeAttribute.IncludeOrder,
                    });
                }
            }

            foreach (var indexAttribute in indexAttributes)
            {
                var entityIndexInfo = new IndexInfo()
                {
                    Name = indexAttribute.Name,
                    IsClustered = indexAttribute.IsClustered,
                    IsUnique = indexAttribute.IsUnique,
                    TableInfo = entityMapping.TableInfo
                };

                var currentIndexMemberInfoCollection = indexMemberInfoCollection.Where(a => a.IndexName == indexAttribute.Name).ToList();
                var currentIndexIncludeInfoCollection = indexIncludeInfoCollection.Where(a => a.IndexName == indexAttribute.Name).ToList();

                try
                {
                    currentIndexMemberInfoCollection.Sort(CompareIndexInfoOrder);
                    currentIndexIncludeInfoCollection.Sort(CompareIndexInfoOrder);
                }
                catch (Exception ex)
                {
                    throw new BadIndexColumnOrderException("Ошибка упарядочивания столбцов индекса", ex);
                }

                entityIndexInfo.IndexMemberInfoCollection.AddRange(currentIndexMemberInfoCollection);
                entityIndexInfo.IndexIncludeInfoCollection.AddRange(currentIndexIncludeInfoCollection);

                indexes.Add(entityIndexInfo);
            }
            return indexes;
        }
Пример #5
0
        /// <summary>
        /// Получить информацию об индексах
        /// </summary>
        /// <param name="storageIndexItemInfoCollection">Информация об индексах из хранилища</param>
        /// <param name="tableInfo">Информация о таблице</param>
        /// <returns>Информацию об индексах</returns>
        private List <IndexInfo> GetStorageIndexInfo(IEnumerable <StorageIndexItem> storageIndexItemInfoCollection, TableInfo tableInfo)
        {
            var indexes = new List <IndexInfo>();

            //группируем по таблицам
            List <StorageIndexItem> tableIndexGroupInfo = storageIndexItemInfoCollection.Where(i => i.TableName == tableInfo.Name).ToList();

            //группируем по именам индексов
            var indexNameGroups = tableIndexGroupInfo.GroupBy(i => i.IndexName).ToList();

            foreach (var indexNameGroup in indexNameGroups)
            {
                var isClustered = false;
                var isUnique    = false;

                var entityIndexInfo = new IndexInfo()
                {
                    Name = indexNameGroup.Key, TableInfo = tableInfo
                };

                var indexMemberItems  = indexNameGroup.Where(i => i.IsIncludedColumn == false).ToList();
                var indexIncludeItems = indexNameGroup.Where(i => i.IsIncludedColumn == true).ToList();

                indexMemberItems.Sort(CompareStorageIndexItemOrder);
                indexIncludeItems.Sort(CompareStorageIndexItemOrder);

                int memberColumnOrder  = 0;
                int includeColumnOrder = 0;
                foreach (var indexMemberItem in indexMemberItems)
                {
                    isClustered = indexMemberItem.IndexType.Equals("CLUSTERED");
                    isUnique    = indexMemberItem.IsUnique;

                    var indexMemberInfo = new IndexMemberInfo()
                    {
                        ColumnInfo    = new ColumnInfo(tableInfo, indexMemberItem.ColumnName),
                        IndexName     = indexMemberItem.IndexName,
                        ColumnOrder   = memberColumnOrder,
                        SortDirection = indexMemberItem.IsDescending == true ? SortDirection.Desc : SortDirection.Asc
                    };
                    entityIndexInfo.IndexMemberInfoCollection.Add(indexMemberInfo);

                    memberColumnOrder += 1;
                }

                foreach (var indexIncludeItem in indexIncludeItems)
                {
                    isClustered = indexIncludeItem.IndexType.Equals("CLUSTERED");
                    isUnique    = indexIncludeItem.IsUnique;

                    var indexMemberInfo = new IndexIncludeInfo()
                    {
                        ColumnInfo  = new ColumnInfo(tableInfo, indexIncludeItem.ColumnName),
                        IndexName   = indexIncludeItem.IndexName,
                        ColumnOrder = includeColumnOrder,
                    };
                    entityIndexInfo.IndexIncludeInfoCollection.Add(indexMemberInfo);

                    includeColumnOrder += 1;
                }
                entityIndexInfo.IsClustered = isClustered;
                entityIndexInfo.IsUnique    = isUnique;

                indexes.Add(entityIndexInfo);
            }

            return(indexes);
        }
Пример #6
0
        /// <summary>
        /// Получить индексы для сущности
        /// </summary>
        /// <param name="entity">Сущность</param>
        /// <param name="entityMapping">Mapping сущности</param>
        /// <exception cref="BadIndexColumnOrderException">Ошибка упарядочивания столбцов индекса</exception>
        /// <returns>Коллекия индексов</returns>
        private List <IndexInfo> GetIndexInfoCollection(Type entity, EntityMapping entityMapping)
        {
            var indexes = new List <IndexInfo>();
            var indexMemberInfoCollection  = new List <IndexMemberInfo>();
            var indexIncludeInfoCollection = new List <IndexIncludeInfo>();

            var propertyMappings = entityMapping.PropertyMappings;
            var properties       = entity.GetProperties();
            var indexAttributes  = entity.GetCustomAttributes(typeof(IndexAttribute), inherit: false) as IEnumerable <IndexAttribute>;

            foreach (var propertyMapping in propertyMappings)
            {
                var property = properties.First(p => p.Name == propertyMapping.Property.Name);
                var indexMemberAttributes = property.GetCustomAttributes(typeof(IndexMemberAttribute), inherit: false) as IEnumerable <IndexMemberAttribute>;
                foreach (var indexMemberAttribute in indexMemberAttributes)
                {
                    indexMemberInfoCollection.Add(new IndexMemberInfo()
                    {
                        ColumnInfo    = new ColumnInfo(entityMapping.TableInfo, propertyMapping.ColumnName),
                        IndexName     = indexMemberAttribute.IndexName,
                        ColumnOrder   = indexMemberAttribute.MemberOrder,
                        SortDirection = indexMemberAttribute.SortDirection
                    });
                }

                var indexIncludeAttributes = property.GetCustomAttributes(typeof(IndexIncludeAttribute), inherit: false) as IEnumerable <IndexIncludeAttribute>;
                foreach (var indexIncludeAttribute in indexIncludeAttributes)
                {
                    indexIncludeInfoCollection.Add(new IndexIncludeInfo()
                    {
                        ColumnInfo  = new ColumnInfo(entityMapping.TableInfo, propertyMapping.ColumnName),
                        IndexName   = indexIncludeAttribute.IndexName,
                        ColumnOrder = indexIncludeAttribute.IncludeOrder,
                    });
                }
            }

            foreach (var indexAttribute in indexAttributes)
            {
                var entityIndexInfo = new IndexInfo()
                {
                    Name        = indexAttribute.Name,
                    IsClustered = indexAttribute.IsClustered,
                    IsUnique    = indexAttribute.IsUnique,
                    TableInfo   = entityMapping.TableInfo
                };

                var currentIndexMemberInfoCollection  = indexMemberInfoCollection.Where(a => a.IndexName == indexAttribute.Name).ToList();
                var currentIndexIncludeInfoCollection = indexIncludeInfoCollection.Where(a => a.IndexName == indexAttribute.Name).ToList();

                try
                {
                    currentIndexMemberInfoCollection.Sort(CompareIndexInfoOrder);
                    currentIndexIncludeInfoCollection.Sort(CompareIndexInfoOrder);
                }
                catch (Exception ex)
                {
                    throw new BadIndexColumnOrderException("Ошибка упарядочивания столбцов индекса", ex);
                }

                entityIndexInfo.IndexMemberInfoCollection.AddRange(currentIndexMemberInfoCollection);
                entityIndexInfo.IndexIncludeInfoCollection.AddRange(currentIndexIncludeInfoCollection);

                indexes.Add(entityIndexInfo);
            }
            return(indexes);
        }