Exemple #1
0
 private bool TryGetReturnMapping(PgReturns r, out string value)
 {
     if (settings.Mapping.TryGetValue(r.Type, out value))
     {
         return(true);
     }
     return(settings.Mapping.TryGetValue(r.DataType, out value));
 }
        private string Build()
        {
            var modelBuilderTag = "{modelBuilderTag}";
            var extraNsTag      = "{extraNsTag}";

            var name    = _item.Name.ToUpperCamelCase();
            var builder = CreateModule(_namespace, extraNsTag);


            builder.Append(modelBuilderTag); // result classes placeholder

            OpenClass(builder, $"PgRoutine{name}", true);
            builder.AppendLine($"        public const string Name = \"{_item.Name}\";");
            builder.AppendLine();

            var count       = _item.Parameters.Count();
            var i           = 0;
            var modelsCount = 0;
            var totalModels = _item.Returns.Count(r => r.Record != null && r.Record.Any());

            foreach (var itemParameter in _item.Parameters)
            {
                _parameters = itemParameter;
                _returns    = _item.Returns[i];
                i++;
                _isVoid             = _returns.Type == "void";
                _noRecords          = _returns.Record == null || !_returns.Record.Any();
                _noParams           = _parameters == null || !_parameters.Any();
                _singleRecordResult = !_noRecords && (_returns.Record != null && _returns.Record.Count() == 1);
                _recordCount        = _returns.Record?.Count() ?? 0;

                string result;
                var    resultFields = new List <string>();
                if (!_noRecords && !_singleRecordResult)
                {
                    modelsCount++;
                    result = _returns.UserDefined ? _returns.Type.ToUpperCamelCase() : $"{name}Result{(totalModels == 1 ? "" : modelsCount.ToString())}";
                    if (ModelName == null)
                    {
                        ModelName = result;
                    }
                    if (!Models.Contains(result))
                    {
                        OpenClass(_modelBuilder, result);
                        foreach (var rec in _returns.Record.OrderBy(r => r.Ordinal))
                        {
                            var type      = GetType(rec, $"result type \"{rec.Name}\"");
                            var fieldName = rec.Name.ToUpperCamelCase();
                            _modelBuilder.AppendLine($"       public {type} {fieldName} {{ get; set; }}");
                            resultFields.Add(fieldName);
                        }

                        CloseClass(_modelBuilder);
                        _modelBuilder.AppendLine();
                        Models.Add(result);
                    }
                    else
                    {
                        foreach (var rec in _returns.Record.OrderBy(r => r.Ordinal))
                        {
                            resultFields.Add(rec.Name.ToUpperCamelCase());
                        }
                    }
                }
                else
                {
                    result = _isVoid ? "void" : GetType(_returns, "result type");
                }


                if (_settings.SyncMethod)
                {
                    BuildSyncMethod(builder, result, name, resultFields);
                }
                if (_settings.AsyncMethod)
                {
                    if (_settings.SyncMethod)
                    {
                        builder.AppendLine();
                    }
                    BuildAsyncMethod(builder, result, name, resultFields);
                }


                if (i < count)
                {
                    builder.AppendLine();
                }
            }

            CloseClass(builder);
            var content      = CloseModule(builder);
            var modelContent = _modelBuilder.ToString();

            if (_settings.ModelDir == null)
            {
                return(content.Replace(modelBuilderTag, modelContent).Replace(extraNsTag, ""));
            }

            var modelNamespace = "";

            if (string.IsNullOrEmpty(modelContent))
            {
                return(content.Replace(modelBuilderTag, "").Replace(extraNsTag, modelNamespace));
            }

            modelNamespace = string.Concat(_settings.Namespace, ".", _settings.ModelDir.Replace("/", ".").Replace("\\", "."));

            var modelBuilder = CreateModule(modelNamespace);

            modelNamespace = string.Concat("using ", modelNamespace, ";", NL);
            modelContent   = TrimEnd(modelContent, NL);
            modelBuilder.AppendLine(modelContent);
            ModelContent = CloseModule(modelBuilder);

            return(content.Replace(modelBuilderTag, "").Replace(extraNsTag, modelNamespace));
        }