private TableMapping CreateAccountingRegisterTableMapping(ValueTableRow tableRow)
        {
            var queryTableName = tableRow.GetString("ИмяТаблицы");

            if (string.IsNullOrEmpty(queryTableName))
            {
                return(null);
            }
            var dbTableName = tableRow.GetString("ИмяТаблицыХранения");

            if (string.IsNullOrEmpty(dbTableName))
            {
                return(null);
            }
            var          fieldsTable             = new ValueTable(tableRow["Поля"]);
            var          propertyMappings        = new List <PropertyMapping>();
            const string registratorPropertyName = "Регистратор";
            const string valuePropertyName       = "Значение";

            propertyMappings.Add(new PropertyMapping(registratorPropertyName, null, CreateRegistratorLayout()));
            propertyMappings.Add(new PropertyMapping(valuePropertyName, null, CreateSubkontoLayout()));
            foreach (var fieldRow in fieldsTable)
            {
                var queryName = fieldRow.GetString("ИмяПоля");
                if (queryName == registratorPropertyName || queryName == valuePropertyName)
                {
                    continue;
                }
                var dbName = fieldRow.GetString("ИмяПоляХранения");
                if (string.IsNullOrWhiteSpace(dbName))
                {
                    throw new InvalidOperationException("assertion failure");
                }
                propertyMappings.Add(new PropertyMapping(queryName, new SingleLayout(dbName, null), null));
            }
            return(new TableMapping(queryTableName, dbTableName, TableType.Main, propertyMappings.ToArray()));
        }
        private TableMapping CreateDefaultTableMapping(ValueTableRow tableRow,
                                                       Dictionary <string, TableMapping> tableMappingByQueryName)
        {
            var queryTableName = tableRow.GetString("ИмяТаблицы");

            if (string.IsNullOrEmpty(queryTableName))
            {
                return(null);
            }
            var dbTableName = tableRow.GetString("ИмяТаблицыХранения");

            if (string.IsNullOrEmpty(dbTableName))
            {
                return(null);
            }
            var purpose = tableRow.GetString("Назначение");
            ConfigurationItemDescriptor descriptor;
            object    comObject;
            TableType tableType;

            if (purpose == "Основная" || purpose == "Константа")
            {
                var configurationName = ConfigurationName.ParseOrNull(queryTableName);
                if (!configurationName.HasValue)
                {
                    return(null);
                }
                tableType  = TableType.Main;
                descriptor = MetadataHelpers.GetDescriptorOrNull(configurationName.Value.Scope);
                if (descriptor == null)
                {
                    comObject = null;
                }
                else
                {
                    var configurationItem = globalContext.FindMetaByName(configurationName.Value);
                    comObject = configurationItem.ComObject;
                }
            }
            else if (purpose == "ТабличнаяЧасть")
            {
                descriptor = MetadataHelpers.tableSectionDescriptor;
                var fullname = TableSectionQueryNameToFullName(queryTableName);
                comObject = ComHelpers.Invoke(globalContext.Metadata, "НайтиПоПолномуИмени", fullname);
                if (comObject == null)
                {
                    return(null);
                }
                tableType = TableType.TableSection;
            }
            else
            {
                return(null);
            }
            var propertyDescriptors = comObject == null
                ? new Dictionary <string, string[]>()
                : MetadataHelpers.GetAttributes(comObject, descriptor)
                                      .ToDictionary(Call.Имя, GetConfigurationTypes);
            var propertyMappings = new ValueTable(tableRow["Поля"])
                                   .Select(x => new
            {
                queryName = x.GetString("ИмяПоля"),
                dbName    = x.GetString("ИмяПоляХранения")
            })
                                   .Where(x => !string.IsNullOrEmpty(x.queryName))
                                   .Where(x => !string.IsNullOrEmpty(x.dbName))
                                   .GroupBy(x => x.queryName,
                                            (x, y) => new
            {
                queryName = x,
                columns   = y.Select(z => z.dbName).ToArray()
            }, StringComparer.OrdinalIgnoreCase)
                                   .Select(x =>
            {
                var propertyName  = x.queryName.ExcludeSuffix("Кт").ExcludeSuffix("Дт");
                var propertyTypes = propertyName == "Счет"
                        ? new[] { "ПланСчетов.Хозрасчетный" }
                        : propertyDescriptors.GetOrDefault(propertyName);
                if (propertyTypes == null || propertyTypes.Length == 1)
                {
                    if (x.columns.Length != 1)
                    {
                        return(null);
                    }
                    var nestedTableName = propertyTypes == null ? null : propertyTypes[0];
                    var singleLayout    = new SingleLayout(x.columns[0], nestedTableName);
                    return(new PropertyMapping(x.queryName, singleLayout, null));
                }
                var unionLayout = x.queryName == "Регистратор"
                        ? new UnionLayout(
                    null,
                    GetColumnBySuffixOrNull("_RecorderTRef", x.columns),
                    GetColumnBySuffixOrNull("_RecorderRRef", x.columns),
                    propertyTypes)
                        : new UnionLayout(
                    GetColumnBySuffixOrNull("_type", x.columns),
                    GetColumnBySuffixOrNull("_rtref", x.columns),
                    GetColumnBySuffixOrNull("_rrref", x.columns),
                    propertyTypes);
                return(new PropertyMapping(x.queryName, null, unionLayout));
            })
                                   .NotNull()
                                   .ToList();

            if (tableType == TableType.TableSection)
            {
                if (!HasProperty(propertyMappings, PropertyNames.id))
                {
                    var refLayout = new SingleLayout(GetTableSectionIdColumnNameByTableName(dbTableName), null);
                    propertyMappings.Add(new PropertyMapping(PropertyNames.id, refLayout, null));
                }
                if (!HasProperty(propertyMappings, PropertyNames.area))
                {
                    var          mainQueryName = TableMapping.GetMainQueryNameByTableSectionQueryName(queryTableName);
                    TableMapping mainTableMapping;
                    if (!tableMappingByQueryName.TryGetValue(mainQueryName, out mainTableMapping))
                    {
                        return(null);
                    }
                    PropertyMapping mainAreaProperty;
                    if (!mainTableMapping.TryGetProperty(PropertyNames.area, out mainAreaProperty))
                    {
                        return(null);
                    }
                    propertyMappings.Add(mainAreaProperty);
                }
            }
            return(new TableMapping(queryTableName, dbTableName, tableType, propertyMappings.ToArray()));
        }
        private TableMapping CreateAccountingRegisterTableMapping(ValueTableRow tableRow)
        {
            var queryTableName = tableRow.GetString("ИмяТаблицы");

            if (string.IsNullOrEmpty(queryTableName))
            {
                return(null);
            }
            var dbTableName = tableRow.GetString("ИмяТаблицыХранения");

            if (string.IsNullOrEmpty(dbTableName))
            {
                return(null);
            }
            var fieldsTable      = new ValueTable(tableRow["Поля"]);
            var propertyMappings = new List <PropertyMapping>();
            var subkontoCount    = 0;

            foreach (var fieldRow in fieldsTable)
            {
                var queryName = fieldRow.GetString("ИмяПоля");
                var dbName    = fieldRow.GetString("ИмяПоляХранения");
                if (string.IsNullOrWhiteSpace(dbName))
                {
                    throw new InvalidOperationException("assertion failure");
                }
                if (dbName.EndsWith("_RTRef") || dbName.EndsWith("_RRRef"))
                {
                    continue;
                }
                UnionLayout unionLauout = null;
                if (string.IsNullOrWhiteSpace(queryName))
                {
                    if (dbName == "_AccountRRef")
                    {
                        queryName = "Счет";
                    }
                    else if (dbName == "_Period")
                    {
                        queryName = "Период";
                    }
                    else if (dbName == "_Splitter")
                    {
                        queryName = "Разделитель";
                    }
                    else if (dbName == "_Value1_TYPE")
                    {
                        queryName   = "Субконто1";
                        unionLauout = CreateSubkontoLayout(1);
                        subkontoCount++;
                    }
                    else if (dbName == "_Value2_TYPE")
                    {
                        queryName   = "Субконто2";
                        unionLauout = CreateSubkontoLayout(2);
                        subkontoCount++;
                    }
                    else if (dbName == "_Value3_TYPE")
                    {
                        queryName   = "Субконто3";
                        unionLauout = CreateSubkontoLayout(3);
                        subkontoCount++;
                    }
                    else
                    {
                        const string messageFormat = "unexpected empty query name for field, db name [{0}]";
                        throw new InvalidOperationException(string.Format(messageFormat, dbName));
                    }
                }
                SingleLayout singleLayout = null;
                if (unionLauout == null)
                {
                    string nestedTableName;
                    if (queryName == "Счет")
                    {
                        nestedTableName = "ПланСчетов.Хозрасчетный";
                    }
                    else if (queryName == "Организация")
                    {
                        nestedTableName = "Справочник.Организации";
                    }
                    else if (queryName == "Валюта")
                    {
                        nestedTableName = "Справочник.Валюты";
                    }
                    else if (queryName == "Подразделение")
                    {
                        nestedTableName = "Справочник.ПодразделенияОрганизаций";
                    }
                    else
                    {
                        nestedTableName = null;
                    }
                    singleLayout = new SingleLayout(dbName, nestedTableName);
                }
                if (dbName.ContainsIgnoringCase("Turnover"))
                {
                    queryName += "Оборот";
                    if (dbName.Contains("Dt"))
                    {
                        queryName += "Дт";
                    }
                    else if (dbName.Contains("Ct"))
                    {
                        queryName += "Кт";
                    }
                }
                propertyMappings.Add(new PropertyMapping(queryName, singleLayout, unionLauout));
            }
            if (queryTableName == AccountingRegisterTables.subkontoName)
            {
                queryTableName += subkontoCount;
            }
            return(new TableMapping(queryTableName, dbTableName, TableType.Main, propertyMappings.ToArray()));
        }