Skip to content

Commit f2b8324

Browse files
committed
response optimization
1 parent 27964c6 commit f2b8324

1 file changed

Lines changed: 31 additions & 19 deletions

File tree

NpgsqlRest/MiddlewareExtension.cs

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,7 @@ await options.ValidateParametersAsync(new ParameterValidationValues(
614614
else // end if (routine.IsVoid)
615615
{
616616
command.AllResultTypesAreUnknown = true;
617+
StringBuilder response = new();
617618
await using var reader = await command.ExecuteReaderAsync();
618619
if (routine.ReturnsSet == false && routine.ColumnCount == 1 && routine.ReturnsRecordType is false)
619620
{
@@ -647,20 +648,22 @@ await options.ValidateParametersAsync(new ParameterValidationValues(
647648
}
648649
if (value is not null)
649650
{
650-
await context.Response.WriteAsync(value);
651+
response.Append(value);
651652
}
652653
else
653654
{
654655
if (endpoint.TextResponseNullHandling == TextResponseNullHandling.NullLiteral)
655656
{
656-
await context.Response.WriteAsync("null");
657+
response.Append("null");
657658
}
658659
else if (endpoint.TextResponseNullHandling == TextResponseNullHandling.NoContent)
659660
{
660661
context.Response.StatusCode = (int)HttpStatusCode.NoContent;
661662
}
662663
// else OK empty string
663664
}
665+
666+
await context.Response.WriteAsync(response.ToString());
664667
await context.Response.CompleteAsync();
665668
return;
666669
}
@@ -692,15 +695,16 @@ await options.ValidateParametersAsync(new ParameterValidationValues(
692695
context.Response.StatusCode = (int)HttpStatusCode.OK;
693696
if (routine.ReturnsSet)
694697
{
695-
await context.Response.WriteAsync("[");
698+
response.Append('[');
696699
}
697700
bool first = true;
698701
var routineReturnRecordCount = routine.ColumnCount;
702+
699703
while (await reader.ReadAsync())
700704
{
701705
if (!first)
702706
{
703-
await context.Response.WriteAsync(",");
707+
response.Append(',');
704708
}
705709
else
706710
{
@@ -716,81 +720,89 @@ await options.ValidateParametersAsync(new ParameterValidationValues(
716720
{
717721
if (i == 0)
718722
{
719-
await context.Response.WriteAsync("{");
723+
response.Append('{');
720724
}
721-
await context.Response.WriteAsync(string.Concat("\"", endpoint.ReturnRecordNames[i], "\":"));
725+
response.Append('\"');
726+
response.Append(endpoint.ReturnRecordNames[i]);
727+
response.Append("\":");
722728
}
723729

724730
var descriptor = routine.ColumnsTypeDescriptor[i];
725731
if (value == DBNull.Value)
726732
{
727-
await context.Response.WriteAsync("null");
733+
response.Append("null");
728734
}
729735
else if (descriptor.IsArray && value is not null)
730736
{
731737
raw = PgConverters.PgArrayToJsonArray(ref raw, ref descriptor);
732-
await context.Response.WriteAsync(raw);
738+
response.Append(raw);
733739
}
734740
else if ((descriptor.IsNumeric || descriptor.IsBoolean || descriptor.IsJson) && value is not null)
735741
{
736742
if (descriptor.IsBoolean)
737743
{
738744
if (string.Equals(raw, "t", StringComparison.Ordinal))
739745
{
740-
await context.Response.WriteAsync("true");
746+
response.Append("true");
741747
}
742748
else if (string.Equals(raw, "f", StringComparison.Ordinal))
743749
{
744-
await context.Response.WriteAsync("false");
750+
response.Append("false");
745751
}
746752
else
747753
{
748-
await context.Response.WriteAsync(raw);
754+
response.Append(raw);
749755
}
750756
}
751757
else
752758
{
753759
// numeric and json
754-
await context.Response.WriteAsync(raw);
760+
response.Append(raw);
755761
}
756762
}
757763
else
758764
{
759765
if (descriptor.ActualDbType == NpgsqlDbType.Unknown)
760766
{
761-
await context.Response.WriteAsync(PgConverters.PgUnknownToJsonArray(ref raw));
767+
response.Append(PgConverters.PgUnknownToJsonArray(ref raw));
762768
}
763769
else if (descriptor.NeedsEscape)
764770
{
765-
await context.Response.WriteAsync(PgConverters.SerializeString(ref raw));
771+
response.Append(PgConverters.SerializeString(ref raw));
766772
}
767773
else
768774
{
769775
if (descriptor.IsDateTime)
770776
{
771-
await context.Response.WriteAsync(string.Concat("\"", raw.Replace(' ', 'T'), "\""));
777+
response.Append('\"');
778+
response.Append(raw.Replace(' ', 'T'));
779+
response.Append('\"');
772780
}
773781
else
774782
{
775-
await context.Response.WriteAsync(string.Concat("\"", raw, "\""));
783+
response.Append('\"');
784+
response.Append(raw);
785+
response.Append('\"');
776786
}
777787
}
778788
}
779789
if (routine.ReturnsUnnamedSet == false && i == routine.ColumnCount - 1)
780790
{
781-
await context.Response.WriteAsync("}");
791+
response.Append('}');
782792
}
783793
if (i < routine.ColumnCount - 1)
784794
{
785-
await context.Response.WriteAsync(",");
795+
response.Append(',');
786796
}
787797
} // end for
788798

789799
} // end while
790800
if (routine.ReturnsSet)
791801
{
792-
await context.Response.WriteAsync("]");
802+
response.Append(']');
793803
}
804+
805+
await context.Response.WriteAsync(response.ToString());
794806
await context.Response.CompleteAsync();
795807
return;
796808
} // end if (routine.ReturnsRecord == true)

0 commit comments

Comments
 (0)