Пример #1
0
        public void InitByDataLink(DataLink link)
        {
            Layer      = $@"{link.LayerTable}";
            RootType   = TGHelper.ObjectType(link);
            TriggerKey = Regex.Replace(link.__PrimaryKey.ToString(), @"[^a-zA-Z]", string.Empty);
            if (TriggerKey.Length > 20)
            {
                TriggerKey = TriggerKey.Substring(0, 20);
            }

            foreach (var p in link.DataLinkParameter.Cast <DataLinkParameter>().Where(p => !p.LinkField))
            {
                if (!string.IsNullOrEmpty(p.ObjectField) && string.IsNullOrEmpty(p.Expression))
                {
                    AddSimpleField(p.LayerField, p.ObjectField);
                }
                else
                {
                    var match = Regex.Match(p.Expression, @"@(.+?)@");
                    int i     = 0;
                    while (match.Success)
                    {
                        AddSimpleField(p.LayerField + i++, match.Groups[1].Value);
                        match = match.NextMatch();
                    }
                }
            }
        }
        public virtual void InitByDataLink(DataLink link)
        {
            if (link == null || link.DataLinkParameter == null || link.DataLinkParameter.Count == 0 ||
                !link.DataLinkParameter.Cast <DataLinkParameter>().Any(x => x.LinkField))
            {
                throw new Exception("Нельзя генерировать триггеры без указания настроек и полей связи");
            }
            ClearWithoutLink = link.ClearWithoutLink;
            Layer            = $@"{LayerPrefix}{link.LayerTable}";

            RootType = TGHelper.ObjectType(link);

            LinkParams = new Dictionary <string, string>();
            foreach (var linkParam in link.DataLinkParameter.GetAllObjects().Cast <DataLinkParameter>().Where(p => p.LinkField))
            {
                if (!string.IsNullOrEmpty(linkParam.ObjectField))
                {
                    if (Information.CheckPropertyExist(RootType, linkParam.ObjectField))
                    {
                        var propertyType      = Information.GetPropertyType(RootType, linkParam.ObjectField);
                        var databaseFieldName = propertyType.IsSubclassOf(typeof(DataObject))
                            ? Information.GetPropertyStorageName(RootType, linkParam.ObjectField, 0)
                            : Information.GetPropertyStorageName(RootType, linkParam.ObjectField);
                        LinkParams.Add(linkParam.LayerField, "@" + TGHelper.DataBaseHelper.Format(databaseFieldName));
                    }
                    else
                    {
                        LogService.LogWarn($"Поле {linkParam.ObjectField} не найдено в классе {RootType.FullName}");
                    }
                }
                else
                {
                    if (!string.IsNullOrEmpty(linkParam.Expression))
                    {
                        var expression = linkParam.Expression;
                        var match      = Regex.Match(linkParam.Expression, @"@\b(\w+\.{0,1})+\b@");
                        while (match.Success)
                        {
                            var field = match.Value.Trim('@');
                            if (Information.CheckPropertyExist(RootType, field))
                            {
                                var propertyType      = Information.GetPropertyType(RootType, field);
                                var databaseFieldName = propertyType.IsSubclassOf(typeof(DataObject))
                                    ? Information.GetPropertyStorageName(RootType, field, 0)
                                    : Information.GetPropertyStorageName(RootType, field);

                                // оставим @ впереди, чтобы знать куда будет добавляться алиас
                                expression = expression.Replace(match.Value.TrimStart('@'), TGHelper.DataBaseHelper.Format(databaseFieldName));
                            }
                            else
                            {
                                LogService.LogWarn($"Поле {field} не найдено в классе {RootType.FullName}");
                            }

                            match = match.NextMatch();
                        }

                        LinkParams.Add(linkParam.LayerField, expression);
                    }
                }
            }

            TriggerKey = Regex.Replace(link.__PrimaryKey.ToString(), @"[^a-zA-Z]", string.Empty);
            if (TriggerKey.Length > 20)
            {
                TriggerKey = TriggerKey.Substring(0, 20);
            }
        }