private void RenderTableFunction(Procedure proc)
 {
     Write($"return GetTable<{proc.ResultTable.TypeName}>((MethodInfo)MethodBase.GetCurrentMethod()");
     RenderParameters(proc.ProcParameters, includeType: false);
     Write(");");
 }
        private void RenderProcedure(Procedure proc,
                                     string procName,
                                     List <Parameter> inputParameters,
                                     List <Parameter> outputParameters)
        {
            var resultName = "result";
            var retNo      = 0;

            while (proc.ProcParameters.Any(pp => pp.ParameterName == resultName))
            {
                resultName = "result" + ++retNo;
            }

            var hasOut = outputParameters.Any(pr => pr.IsOut);
            var prefix = $"var {resultName} = ";

            if (proc.ResultTable == null)
            {
                Write($"{prefix}ExecuteProc(\"{proc.ProcedureName}\"");
                PushIndent();
            }
            else
            {
                if (proc.ResultTable.OrderedColumns.Any(c => c.IsDuplicateOrEmpty))
                {
                    WriteLine($"{prefix}QueryProc(dataReader =>");
                    PushIndent();
                    WriteLine($"new {proc.ResultTable.TypeName}");
                    OpenScope();

                    var n     = 0;
                    var first = proc.ResultTable.OrderedColumns.FirstOrDefault();
                    foreach (var c in proc.ResultTable.OrderedColumns)
                    {
                        Write($"{c.MemberName} = Converter.ChangeTypeTo<{c.Type}>(dataReader.GetValue({n++}), MappingSchema)");
                        WriteLine(first != c ? "," : "");
                    }

                    CloseScope("},");
                    Write(procName);
                }
                else
                {
                    Write($"{prefix}QueryProc<{proc.ResultTable.TypeName}>({procName}");
                    PushIndent();
                }
            }

            if (inputParameters.Count > 0)
            {
                WriteLine(",");
            }

            var lastInput = inputParameters.LastOrDefault();

            foreach (var arg in inputParameters)
            {
                Write($"new DataParameter(\"{arg.SchemaName}\", {arg.ParameterName}, {"DataType." + arg.DataType})");

                if (arg.IsOut)
                {
                    Write(" { Direction = " + (arg.IsIn
                                                ? "ParameterDirection.InputOutput"
                                                : "ParameterDirection.Output"));

                    if (arg.Size != null &&
                        arg.Size.Value != 0)
                    {
                        Write(", Size = " + arg.Size.Value);
                    }

                    Write(" }");
                }
                WriteLine(arg != lastInput ? "," : "");
            }

            PopIndent();
            WriteLine(");");

            if (hasOut)
            {
                WriteLine();

                foreach (var pr in outputParameters)                //proc.ProcParameters.Where(_ => _.IsOut))
                {
                    WriteLine($"{pr.ParameterName} = Converter.ChangeTypeTo<{pr.ParameterType}>(((IDbDataParameter)Command.Parameters[\"{pr.SchemaName}\"]).Value);");
                }
            }

            WriteLine();
            WriteLine("return " + resultName + ";");
        }